From af7cd4f35fb5b24c8dc09e0ab7fc851ed6550982 Mon Sep 17 00:00:00 2001 From: christos Date: Sun, 9 Jun 2013 02:58:58 +0000 Subject: [PATCH] Use be{32,64}enc, suggested by joerg. --- common/lib/libc/hash/sha2/sha2.c | 64 ++++++++------------------------ 1 file changed, 16 insertions(+), 48 deletions(-) diff --git a/common/lib/libc/hash/sha2/sha2.c b/common/lib/libc/hash/sha2/sha2.c index 745bc34d7f97..57e9c883f043 100644 --- a/common/lib/libc/hash/sha2/sha2.c +++ b/common/lib/libc/hash/sha2/sha2.c @@ -1,4 +1,4 @@ -/* $NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 christos Exp $ */ +/* $NetBSD: sha2.c,v 1.23 2013/06/09 02:58:58 christos Exp $ */ /* $KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $ */ /* @@ -43,7 +43,7 @@ #include #if defined(_KERNEL) || defined(_STANDALONE) -__KERNEL_RCSID(0, "$NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sha2.c,v 1.23 2013/06/09 02:58:58 christos Exp $"); #include /* XXX: to pull for vax memset(9) */ #include @@ -51,7 +51,7 @@ __KERNEL_RCSID(0, "$NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 christos Exp $"); #else #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 christos Exp $"); +__RCSID("$NetBSD: sha2.c,v 1.23 2013/06/09 02:58:58 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #include "namespace.h" @@ -66,55 +66,27 @@ __RCSID("$NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 christos Exp $"); # if HAVE_SYS_ENDIAN_H # include # else -# undef htobe32 -# undef htobe64 -# undef be32toh -# undef be64toh +# undef be32dec +# undef be64dec -static uint32_t -htobe32(uint32_t x) +static __inline uint32_t __unused +be32dec(const void *buf) { - uint8_t p[4]; - memcpy(p, &x, 4); + const uint8_t *p = __CAST(const uint8_t *, buf); return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); } -static uint64_t -htobe64(uint64_t x) +static __inline uint64_t __unused +be64dec(const void *buf) { - uint8_t p[8]; - uint32_t u, v; - memcpy(p, &x, 8); + const uint8_t *p = (const uint8_t *)buf; - u = ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); - v = ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); - - return ((((uint64_t)u) << 32) | v); + return ((__CAST(uint64_t, be32dec(p)) << 32) | be32dec(p + 4)); } - -static uint32_t -be32toh(uint32_t x) -{ - return htobe32(x); -} - -static uint64_t -be64toh(uint64_t x) -{ - return htobe64(x); -} -# define align(a) (&adata, (a)) # endif #endif -#ifndef align -# define align(a) \ - (((uintptr_t)(a) & (sizeof(adata) - 1)) ? \ - (memcpy(&adata, (a), sizeof(adata)), &adata) : \ - (a)) -#endif - /*** SHA-256/384/512 Various Length Definitions ***********************/ /* NOTE: Most of these are in sha2.h */ #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) @@ -334,7 +306,7 @@ SHA256_Init(SHA256_CTX *context) /* Unrolled SHA-256 round macros: */ #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \ - W256[j] = be32toh(*align(data)); \ + W256[j] = be32dec(data); \ ++data; \ T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \ K256[j] + W256[j]; \ @@ -359,7 +331,6 @@ SHA256_Transform(SHA256_CTX *context, const uint32_t *data) uint32_t a, b, c, d, e, f, g, h, s0, s1; uint32_t T1, *W256; int j; - uint32_t adata; W256 = (uint32_t *)context->buffer; @@ -420,7 +391,6 @@ SHA256_Transform(SHA256_CTX *context, const uint32_t *data) uint32_t a, b, c, d, e, f, g, h, s0, s1; uint32_t T1, T2, *W256; int j; - uint32_t adata; W256 = (uint32_t *)(void *)context->buffer; @@ -436,7 +406,7 @@ SHA256_Transform(SHA256_CTX *context, const uint32_t *data) j = 0; do { - W256[j] = be32toh(*align(data)); + W256[j] = be32dec(data); ++data; /* Apply the SHA-256 compression function to update a..h */ T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j]; @@ -682,7 +652,7 @@ SHA512_Init(SHA512_CTX *context) /* Unrolled SHA-512 round macros: */ #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \ - W512[j] = be64toh(*align(data)); \ + W512[j] = be64dec(data); \ ++data; \ T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \ K512[j] + W512[j]; \ @@ -707,7 +677,6 @@ SHA512_Transform(SHA512_CTX *context, const uint64_t *data) uint64_t a, b, c, d, e, f, g, h, s0, s1; uint64_t T1, *W512 = (uint64_t *)context->buffer; int j; - uint64_t adata; /* Initialize registers with the prev. intermediate value */ a = context->state[0]; @@ -765,7 +734,6 @@ SHA512_Transform(SHA512_CTX *context, const uint64_t *data) uint64_t a, b, c, d, e, f, g, h, s0, s1; uint64_t T1, T2, *W512 = (void *)context->buffer; int j; - uint64_t adata; /* Initialize registers with the prev. intermediate value */ a = context->state[0]; @@ -779,7 +747,7 @@ SHA512_Transform(SHA512_CTX *context, const uint64_t *data) j = 0; do { - W512[j] = be64toh(*align(data)); + W512[j] = be64dec(data); ++data; /* Apply the SHA-512 compression function to update a..h */ T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];