Module Name:    src
Committed By:   christos
Date:           Sun Jun  9 02:58:58 UTC 2013

Modified Files:
        src/common/lib/libc/hash/sha2: sha2.c

Log Message:
Use be{32,64}enc, suggested by joerg.


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/common/lib/libc/hash/sha2/sha2.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/hash/sha2/sha2.c
diff -u src/common/lib/libc/hash/sha2/sha2.c:1.22 src/common/lib/libc/hash/sha2/sha2.c:1.23
--- src/common/lib/libc/hash/sha2/sha2.c:1.22	Fri Jun  7 18:40:34 2013
+++ src/common/lib/libc/hash/sha2/sha2.c	Sat Jun  8 22:58:58 2013
@@ -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 <sys/cdefs.h>
 
 #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 <sys/param.h>	/* XXX: to pull <machine/macros.h> for vax memset(9) */
 #include <lib/libkern/libkern.h>
@@ -51,7 +51,7 @@ __KERNEL_RCSID(0, "$NetBSD: sha2.c,v 1.2
 #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/
 # if HAVE_SYS_ENDIAN_H
 #  include <sys/endian.h>
 # 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, co
 	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, co
 	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, co
 
 	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, co
 	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, co
 	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, co
 
 	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];

Reply via email to