From patchwork Thu Mar 26 04:52:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shawn Landden X-Patchwork-Id: 454853 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 53AA114009B for ; Thu, 26 Mar 2015 15:54:38 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=LT0xFv/Z; dkim-adsp=none (unprotected policy); dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=g3WoTyIKkBUzApXnof/xu/3tFAXs10A qD2noShb8zmZr4VOMe8NcozyADIh6g9KScsgBF9pfY+uezNmrHrThJkwrqTTXzji xH6vF/MelQnXW25WbumbZqZA51O/xt3EuT2MXNCj8CixRHQxbjCQRlXGVjeVGoId XFSbHhheigfo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=MgeyfoVrqxjqDOpwOR3zopuWjVM=; b=LT0xF v/Z/kwWg+biPTT/9IgCHC6O7bZ+VdACKCXvvqWf9eeiL9YSJszOtsVb2kxWU24eO g5fK3lu/7mC1xMS6uckTRvcHEfFKthKkIrhTYKBZLEP5uVGO5jmn9GydeYWG88Qv 07q0Laurr7gE8LmzdFYMKMGiImgxXTd+pSvbE4= Received: (qmail 75218 invoked by alias); 26 Mar 2015 04:54:18 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 74889 invoked by uid 89); 26 Mar 2015 04:53:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=2.4 required=5.0 tests=BAYES_00, FREEMAIL_FROM, KAM_FROM_URIBL_PCCC, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 X-HELO: mail-pa0-f44.google.com X-Received: by 10.68.239.65 with SMTP id vq1mr23275253pbc.49.1427345586837; Wed, 25 Mar 2015 21:53:06 -0700 (PDT) From: Shawn Landden To: libc-alpha@sourceware.org Cc: Shawn Landden Subject: [PATCH] sha2: new header Date: Wed, 25 Mar 2015 21:52:59 -0700 Message-Id: <1427345579-222402-1-git-send-email-shawn@churchofgit.com> In-Reply-To: <20150325161229.GB23507@brightrain.aerifal.cx> References: <20150325161229.GB23507@brightrain.aerifal.cx> Export the SHA2 family of functions. --- crypt/sha2.c | 16 ++++++++++++++++ crypt/sha256-crypt.c | 2 +- crypt/sha256.c | 4 ++-- crypt/sha256.h | 15 ++++++++++----- crypt/sha256test.c | 9 ++++----- crypt/sha512-crypt.c | 2 +- crypt/sha512.c | 4 ++-- crypt/sha512.h | 12 ++++++++---- crypt/sha512test.c | 7 +++---- include/sha2.h | 32 ++++++++++++++++++++++++++++++++ 10 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 crypt/sha2.c create mode 100644 include/sha2.h diff --git a/crypt/sha2.c b/crypt/sha2.c new file mode 100644 index 0000000..7b5ec6d --- /dev/null +++ b/crypt/sha2.c @@ -0,0 +1,16 @@ +#include +#include + +void sha256_init(sha256_ctx *s) { + __sha256_init_ctx(s); +} +void sha256_update(sha256_ctx *s, const void *d, size_t n) { +__sha256_process_bytes (s, d, n) +} +void sha256_final(sha256_ctx *s, void *md); +void *sha256(const void *d, size_t n, void *md); + +void sha512_init(sha512_ctx *s); +void sha512_update(sha512_ctx *s, const void *d, size_t n); +void sha512_final(sha512_ctx *s, void *md); +void *sha512(const void *d, size_t n, void *md); diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c index d90e291..7b2f078 100644 --- a/crypt/sha256-crypt.c +++ b/crypt/sha256-crypt.c @@ -68,7 +68,7 @@ typedef int PRBool; __sha256_init_ctx (ctxp) # define sha256_process_bytes(buf, len, ctxp, nss_ctxp) \ - __sha256_process_bytes(buf, len, ctxp) + __sha256_process_bytes(ctxp, buf, len) # define sha256_finish_ctx(ctxp, nss_ctxp, result) \ __sha256_finish_ctx (ctxp, result) diff --git a/crypt/sha256.c b/crypt/sha256.c index b6db8b2..3745973 100644 --- a/crypt/sha256.c +++ b/crypt/sha256.c @@ -145,10 +145,10 @@ __sha256_finish_ctx (ctx, resbuf) void -__sha256_process_bytes (buffer, len, ctx) +__sha256_process_bytes (ctx, buffer, len) + struct sha256_ctx *ctx; const void *buffer; size_t len; - struct sha256_ctx *ctx; { /* When we already have some bits in our internal buffer concatenate both inputs first. */ diff --git a/crypt/sha256.h b/crypt/sha256.h index 27e0fe6..cc08831 100644 --- a/crypt/sha256.h +++ b/crypt/sha256.h @@ -38,32 +38,37 @@ struct sha256_ctx #define TOTAL64_high (BYTE_ORDER == LITTLE_ENDIAN) uint32_t total[2]; }; - uint32_t buflen; union { char buffer[128]; uint32_t buffer32[32]; uint64_t buffer64[16]; }; + uint32_t buflen; }; /* Initialize structure containing state of computation. (FIPS 180-2: 5.3.2) */ -extern void __sha256_init_ctx (struct sha256_ctx *ctx) __THROW; +extern void __sha256_init_ctx (struct sha256_ctx *__restrict ctx) __THROW; +weak_alias(__sha256_init_ctx, sha256_init) /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void __sha256_process_bytes (const void *buffer, size_t len, - struct sha256_ctx *ctx) __THROW; +extern void __sha256_process_bytes (struct sha256_ctx *__restrict ctx, const void *__restrict buffer, + size_t len) __THROW; +weak_alias(__sha256_process_bytes, sha256_update) /* Process the remaining bytes in the buffer and put result from CTX in first 32 bytes following RESBUF. + For sha2-224 truncate from end after calling. + IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */ -extern void *__sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf) +extern void *__sha256_finish_ctx (struct sha256_ctx *__restrict ctx, void *__restrict resbuf) __THROW; +weak_alias(__sha256_finish_ctx, sha256_finish) #endif /* sha256.h */ diff --git a/crypt/sha256test.c b/crypt/sha256test.c index 39e8030..85ac3da 100644 --- a/crypt/sha256test.c +++ b/crypt/sha256test.c @@ -52,8 +52,7 @@ main (void) for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt) { __sha256_init_ctx (&ctx); - __sha256_process_bytes (tests[cnt].input, strlen (tests[cnt].input), - &ctx); + __sha256_process_bytes (&ctx, tests[cnt].input, strlen (tests[cnt].input)); __sha256_finish_ctx (&ctx, sum); if (memcmp (tests[cnt].result, sum, 32) != 0) { @@ -63,7 +62,7 @@ main (void) __sha256_init_ctx (&ctx); for (int i = 0; tests[cnt].input[i] != '\0'; ++i) - __sha256_process_bytes (&tests[cnt].input[i], 1, &ctx); + __sha256_process_bytes (&ctx, &tests[cnt].input[i], 1); __sha256_finish_ctx (&ctx, sum); if (memcmp (tests[cnt].result, sum, 32) != 0) { @@ -77,7 +76,7 @@ main (void) memset (buf, 'a', sizeof (buf)); __sha256_init_ctx (&ctx); for (int i = 0; i < 1000; ++i) - __sha256_process_bytes (buf, sizeof (buf), &ctx); + __sha256_process_bytes (&ctx, buf, sizeof (buf)); __sha256_finish_ctx (&ctx, sum); static const char expected[32] = "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67" @@ -90,7 +89,7 @@ main (void) __sha256_init_ctx (&ctx); for (int i = 0; i < 100000; ++i) - __sha256_process_bytes (buf, 10, &ctx); + __sha256_process_bytes (&ctx, buf, 10); __sha256_finish_ctx (&ctx, sum); if (memcmp (expected, sum, 32) != 0) { diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c index 9c581ab..98b121e 100644 --- a/crypt/sha512-crypt.c +++ b/crypt/sha512-crypt.c @@ -68,7 +68,7 @@ typedef int PRBool; __sha512_init_ctx (ctxp) # define sha512_process_bytes(buf, len, ctxp, nss_ctxp) \ - __sha512_process_bytes(buf, len, ctxp) + __sha512_process_bytes(ctxp, buf, len) # define sha512_finish_ctx(ctxp, nss_ctxp, result) \ __sha512_finish_ctx (ctxp, result) diff --git a/crypt/sha512.c b/crypt/sha512.c index 608de82..45106a5 100644 --- a/crypt/sha512.c +++ b/crypt/sha512.c @@ -167,10 +167,10 @@ __sha512_finish_ctx (ctx, resbuf) void -__sha512_process_bytes (buffer, len, ctx) +__sha512_process_bytes (ctx, buffer, len) + struct sha512_ctx *ctx; const void *buffer; size_t len; - struct sha512_ctx *ctx; { /* When we already have some bits in our internal buffer concatenate both inputs first. */ diff --git a/crypt/sha512.h b/crypt/sha512.h index 159f000..b87710a 100644 --- a/crypt/sha512.h +++ b/crypt/sha512.h @@ -43,6 +43,7 @@ struct sha512_ctx uint64_t total[2]; }; uint64_t buflen; + uint8_t __padding[8]; union { char buffer[256]; @@ -52,21 +53,24 @@ struct sha512_ctx /* Initialize structure containing state of computation. (FIPS 180-2: 5.3.3) */ -extern void __sha512_init_ctx (struct sha512_ctx *ctx) __THROW; +extern void __sha512_init_ctx (struct sha512_ctx *__restrict ctx) __THROW; +weak_alias(__sha512_init_ctx, sha512_init) /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 128. */ -extern void __sha512_process_bytes (const void *buffer, size_t len, - struct sha512_ctx *ctx) __THROW; +extern void __sha512_process_bytes (struct sha512_ctx *__restrict ctx, + const void *__restrict buffer, size_t len) __THROW; +weak_alias(__sha512_process_bytes, sha512_update) /* Process the remaining bytes in the buffer and put result from CTX in first 64 bytes following RESBUF. IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 64 bits value. */ -extern void *__sha512_finish_ctx (struct sha512_ctx *ctx, void *resbuf) +extern void *__sha512_finish_ctx (struct sha512_ctx *ctx, void *__restrict resbuf) __THROW; +weak_alias(__sha512_finish_ctx, sha512_finish) #endif /* sha512.h */ diff --git a/crypt/sha512test.c b/crypt/sha512test.c index 792e9a7..866389c 100644 --- a/crypt/sha512test.c +++ b/crypt/sha512test.c @@ -71,8 +71,7 @@ main (void) for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt) { __sha512_init_ctx (&ctx); - __sha512_process_bytes (tests[cnt].input, strlen (tests[cnt].input), - &ctx); + __sha512_process_bytes (&ctx, tests[cnt].input, strlen (tests[cnt].input)); __sha512_finish_ctx (&ctx, sum); if (memcmp (tests[cnt].result, sum, 64) != 0) { @@ -82,7 +81,7 @@ main (void) __sha512_init_ctx (&ctx); for (int i = 0; tests[cnt].input[i] != '\0'; ++i) - __sha512_process_bytes (&tests[cnt].input[i], 1, &ctx); + __sha512_process_bytes (&ctx, &tests[cnt].input[i], 1); __sha512_finish_ctx (&ctx, sum); if (memcmp (tests[cnt].result, sum, 64) != 0) { @@ -96,7 +95,7 @@ main (void) memset (buf, 'a', sizeof (buf)); __sha512_init_ctx (&ctx); for (int i = 0; i < 1000; ++i) - __sha512_process_bytes (buf, sizeof (buf), &ctx); + __sha512_process_bytes (&ctx, buf, sizeof (buf)); __sha512_finish_ctx (&ctx, sum); static const char expected[64] = "\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63" diff --git a/include/sha2.h b/include/sha2.h new file mode 100644 index 0000000..ad7d97d --- /dev/null +++ b/include/sha2.h @@ -0,0 +1,32 @@ +#ifndef _SHA2_H +#define _SHA2_H + +#include + +typedef struct { + char __internal_state[176]; +} sha256_ctx __attribute__((align(16)); + +/* 256 bits is 32 bytes + * for sha2-224 truncate from end after calling sha256_final() + */ +void *sha256(const void *__restrict d, size_t n, void *__restrict md); +void sha256_init(sha256_ctx *s); +void sha256_update(sha256_ctx *__restrict s, const void *__restrict d, size_t n); +/* md must be 32-bit aligned */ +void sha256_final(sha256_ctx *__restrict s, void *__restrict md); + +typedef struct { + char __internal_state[352]; +} sha512_ctx __attribute__((align(16)); + +/* 512 bits is 64 bytes + * for sha2-384 truncate from end after calling sha512_final() + */ +void *sha512(const void *__restrict d, size_t n, void *__restrict md); +void sha512_init(sha512_ctx *s); +void sha512_update(sha512_ctx *__restrict s, const void *__restrict d, size_t n); +/* md must be 64-bit aligned */ +void sha512_final(sha512_ctx *__restrict s, void *__restrict md); + +#endif