CVS commit: src/common/lib/libc/hash/sha2

2013-06-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jun  9 19:46:57 UTC 2013

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

Log Message:
no need for the local defs of he enc functions; compat_defs.h provides them.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 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.23 src/common/lib/libc/hash/sha2/sha2.c:1.24
--- src/common/lib/libc/hash/sha2/sha2.c:1.23	Sat Jun  8 22:58:58 2013
+++ src/common/lib/libc/hash/sha2/sha2.c	Sun Jun  9 15:46:56 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.23 2013/06/09 02:58:58 christos Exp $ */
+/* $NetBSD: sha2.c,v 1.24 2013/06/09 19:46:56 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.23 2013/06/09 02:58:58 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.24 2013/06/09 19:46:56 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.23 2013/06/09 02:58:58 christos Exp $);
+__RCSID($NetBSD: sha2.c,v 1.24 2013/06/09 19:46:56 christos Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -62,29 +62,8 @@ __RCSID($NetBSD: sha2.c,v 1.23 2013/06/
 #include sys/types.h
 #include sys/sha2.h
 
-#if HAVE_NBTOOL_CONFIG_H
-# if HAVE_SYS_ENDIAN_H
-#  include sys/endian.h
-# else
-#  undef be32dec
-#  undef be64dec
-
-static __inline uint32_t __unused
-be32dec(const void *buf)
-{
-	const uint8_t *p = __CAST(const uint8_t *, buf);
-
-	return ((p[0]  24) | (p[1]  16) | (p[2]  8) | p[3]);
-}
-
-static __inline uint64_t __unused
-be64dec(const void *buf)
-{
-	const uint8_t *p = (const uint8_t *)buf;
-
-	return ((__CAST(uint64_t, be32dec(p))  32) | be32dec(p + 4));
-}
-# endif
+#if HAVE_SYS_ENDIAN_H
+# include sys/endian.h
 #endif
 
 /*** SHA-256/384/512 Various Length Definitions ***/



CVS commit: src/common/lib/libc/hash/sha2

2013-06-08 Thread Christos Zoulas
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 

CVS commit: src/common/lib/libc/hash/sha2

2013-06-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Jun  7 22:40:34 UTC 2013

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

Log Message:
PR/47908: Gary Grebus: SHA256_Transform and SHA512_Transform are called
by openssl with unaligned buffers. All other Transforms can handle unaligned
buffers so make these handle them too.
XXX[1]: any better fixes are welcome
XXX[2]: pullup-5, pullup-6


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 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.21 src/common/lib/libc/hash/sha2/sha2.c:1.22
--- src/common/lib/libc/hash/sha2/sha2.c:1.21	Sun Jan 24 16:11:18 2010
+++ src/common/lib/libc/hash/sha2/sha2.c	Fri Jun  7 18:40:34 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 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.21 2010/01/24 21:11:18 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 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.21 2010/01/24 21:11:18 joerg Exp $);
+__RCSID($NetBSD: sha2.c,v 1.22 2013/06/07 22:40:34 christos Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -63,13 +63,13 @@ __RCSID($NetBSD: sha2.c,v 1.21 2010/01/
 #include sys/sha2.h
 
 #if HAVE_NBTOOL_CONFIG_H
-#  if HAVE_SYS_ENDIAN_H
-#include sys/endian.h
-#  else
-#   undef htobe32
-#   undef htobe64
-#   undef be32toh
-#   undef be64toh
+# if HAVE_SYS_ENDIAN_H
+#  include sys/endian.h
+# else
+#  undef htobe32
+#  undef htobe64
+#  undef be32toh
+#  undef be64toh
 
 static uint32_t
 htobe32(uint32_t x)
@@ -104,7 +104,15 @@ be64toh(uint64_t x)
 {
 	return htobe64(x);
 }
-#  endif
+# 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 ***/
@@ -326,7 +334,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(*data);		\
+	W256[j] = be32toh(*align(data));		\
 	++data;	\
 	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  K256[j] + W256[j]; \
@@ -351,6 +359,7 @@ 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;
 
@@ -411,6 +420,7 @@ 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;
 
@@ -426,7 +436,7 @@ SHA256_Transform(SHA256_CTX *context, co
 
 	j = 0;
 	do {
-		W256[j] = be32toh(*data);
+		W256[j] = be32toh(*align(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];
@@ -672,7 +682,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(*data);		\
+	W512[j] = be64toh(*align(data));		\
 	++data;	\
 	T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
  K512[j] + W512[j]; \
@@ -697,6 +707,7 @@ 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];
@@ -754,6 +765,7 @@ 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];
@@ -767,7 +779,7 @@ SHA512_Transform(SHA512_CTX *context, co
 
 	j = 0;
 	do {
-		W512[j] = be64toh(*data);
+		W512[j] = be64toh(*align(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];



CVS commit: src/common/lib/libc/hash/sha2

2010-01-24 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Sun Jan 24 21:11:18 UTC 2010

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

Log Message:
Fix unaligned access in *_Final for SHA224/SHA256/SHA384.
Remaining part of PR 42273. Tested by snj.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 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.20 src/common/lib/libc/hash/sha2/sha2.c:1.21
--- src/common/lib/libc/hash/sha2/sha2.c:1.20	Fri Nov  6 20:31:18 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Sun Jan 24 21:11:18 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.20 2009/11/06 20:31:18 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg 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.20 2009/11/06 20:31:18 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg Exp $);
 
 #include sys/param.h	/* XXX: to pull machine/macros.h for vax memset(9) */
 #include lib/libkern/libkern.h
@@ -51,7 +51,7 @@
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.20 2009/11/06 20:31:18 joerg Exp $);
+__RCSID($NetBSD: sha2.c,v 1.21 2010/01/24 21:11:18 joerg Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -555,7 +555,6 @@
 static int
 SHA224_256_Final(uint8_t digest[], SHA256_CTX *context, size_t len)
 {
-	uint32_t	*d = (void *)digest;
 	unsigned int	usedspace;
 	size_t i;
 
@@ -603,7 +602,7 @@
 		SHA256_Transform(context, (uint32_t *)(void *)context-buffer);
 
 		for (i = 0; i  len / 4; i++)
-			d[i] = htobe32(context-state[i]);
+			be32enc(digest + 4 * i, context-state[i]);
 	}
 
 	/* Clean up state data: */
@@ -990,7 +989,6 @@
 int
 SHA384_Final(uint8_t digest[], SHA384_CTX *context)
 {
-	uint64_t	*d = (void *)digest;
 	size_t i;
 
 	/* If no digest buffer is passed, we don't bother doing this: */
@@ -999,7 +997,7 @@
 
 		/* Save the hash data for output: */
 		for (i = 0; i  6; ++i)
-			d[i] = be64toh(context-state[i]);
+			be64enc(digest + 8 * i, context-state[i]);
 	}
 
 	/* Zero out state data */



CVS commit: src/common/lib/libc/hash/sha2

2009-06-25 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Thu Jun 25 14:05:18 UTC 2009

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

Log Message:
Fix initialisation of SHA224.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 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.17 src/common/lib/libc/hash/sha2/sha2.c:1.18
--- src/common/lib/libc/hash/sha2/sha2.c:1.17	Fri Jun 19 05:09:09 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Thu Jun 25 14:05:18 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.17 2009/06/19 05:09:09 tsutsui Exp $ */
+/* $NetBSD: sha2.c,v 1.18 2009/06/25 14:05:18 joerg 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.17 2009/06/19 05:09:09 tsutsui Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.18 2009/06/25 14:05:18 joerg Exp $);
 
 #include sys/param.h	/* XXX: to pull machine/macros.h for vax memset(9) */
 #include lib/libkern/libkern.h
@@ -51,7 +51,7 @@
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.17 2009/06/19 05:09:09 tsutsui Exp $);
+__RCSID($NetBSD: sha2.c,v 1.18 2009/06/25 14:05:18 joerg Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -624,9 +624,10 @@
 	if (context == NULL)
 		return 1;
 
+	/* The state and buffer size are driven by SHA256, not by SHA224. */
 	memcpy(context-state, sha224_initial_hash_value,
-	(size_t)(SHA224_DIGEST_LENGTH));
-	memset(context-buffer, 0, (size_t)(SHA224_BLOCK_LENGTH));
+	(size_t)(SHA256_DIGEST_LENGTH));
+	memset(context-buffer, 0, (size_t)(SHA256_BLOCK_LENGTH));
 	context-bitcount = 0;
 
 	return 1;



CVS commit: src/common/lib/libc/hash/sha2

2009-06-18 Thread Izumi Tsutsui
Module Name:src
Committed By:   tsutsui
Date:   Fri Jun 19 05:09:09 UTC 2009

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

Log Message:
- move #include sys/param.h inside #if defined(_KERNEL) section
  where it's actually required
- add XXX comments that notes sys/param.h is required for vax memset(9)


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 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.16 src/common/lib/libc/hash/sha2/sha2.c:1.17
--- src/common/lib/libc/hash/sha2/sha2.c:1.16	Thu Jun 18 15:22:24 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Fri Jun 19 05:09:09 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.16 2009/06/18 15:22:24 he Exp $ */
+/* $NetBSD: sha2.c,v 1.17 2009/06/19 05:09:09 tsutsui Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -41,17 +41,17 @@
 #endif
 
 #include sys/cdefs.h
-#include sys/param.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.16 2009/06/18 15:22:24 he Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.17 2009/06/19 05:09:09 tsutsui Exp $);
 
+#include sys/param.h	/* XXX: to pull machine/macros.h for vax memset(9) */
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.16 2009/06/18 15:22:24 he Exp $);
+__RCSID($NetBSD: sha2.c,v 1.17 2009/06/19 05:09:09 tsutsui Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h



CVS commit: src/common/lib/libc/hash/sha2

2009-06-14 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Sun Jun 14 14:04:07 UTC 2009

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

Log Message:
Fix copypaste errors


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 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.14 src/common/lib/libc/hash/sha2/sha2.c:1.15
--- src/common/lib/libc/hash/sha2/sha2.c:1.14	Thu Jun 11 22:40:42 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Sun Jun 14 14:04:07 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.14 2009/06/11 22:40:42 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.15 2009/06/14 14:04:07 martin Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -43,14 +43,14 @@
 #include sys/cdefs.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.14 2009/06/11 22:40:42 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.15 2009/06/14 14:04:07 martin Exp $);
 
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.14 2009/06/11 22:40:42 joerg Exp $);
+__RCSID($NetBSD: sha2.c,v 1.15 2009/06/14 14:04:07 martin Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -162,7 +162,7 @@
  * only.
  */
 static void SHA512_Last(SHA512_CTX *);
-void SHA224_Transform(SHA224_CTX *, const uint64_t*);
+void SHA224_Transform(SHA224_CTX *, const uint32_t*);
 void SHA256_Transform(SHA256_CTX *, const uint32_t*);
 void SHA384_Transform(SHA384_CTX *, const uint64_t*);
 void SHA512_Transform(SHA512_CTX *, const uint64_t*);
@@ -638,9 +638,9 @@
 }
 
 void
-SHA224_Transform(SHA224_CTX *context, const uint64_t *data)
+SHA224_Transform(SHA224_CTX *context, const uint32_t *data)
 {
-	SHA224_Transform((SHA256_CTX *)context, data);
+	SHA256_Transform((SHA256_CTX *)context, data);
 }
 
 int



CVS commit: src/common/lib/libc/hash/sha2

2009-06-11 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Thu Jun 11 18:46:37 UTC 2009

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

Log Message:
Simplify by using sys/endian.h functions. Fixes Big Endian case for SHA2.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 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.8 src/common/lib/libc/hash/sha2/sha2.c:1.9
--- src/common/lib/libc/hash/sha2/sha2.c:1.8	Tue May 26 08:04:11 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Thu Jun 11 18:46:37 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.8 2009/05/26 08:04:11 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.9 2009/06/11 18:46:37 joerg Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -39,14 +39,14 @@
 #include sys/cdefs.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.8 2009/05/26 08:04:11 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.9 2009/06/11 18:46:37 joerg Exp $);
 
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.8 2009/05/26 08:04:11 joerg Exp $);
+__RCSID($NetBSD: sha2.c,v 1.9 2009/06/11 18:46:37 joerg Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -58,6 +58,7 @@
 #include sys/types.h
 #include sys/param.h
 #include sys/sha2.h
+#include sys/endian.h
 
 /*
  * ASSERT NOTE:
@@ -151,24 +152,6 @@
 #define SHA384_SHORT_BLOCK_LENGTH	(SHA384_BLOCK_LENGTH - 16)
 #define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
 
-
-/*** ENDIAN REVERSAL MACROS ***/
-#if BYTE_ORDER == LITTLE_ENDIAN
-#define REVERSE32(w,x)	{ \
-	sha2_word32 tmp = (w); \
-	tmp = (tmp  16) | (tmp  16); \
-	(x) = (sha2_word32)(((tmp  0xff00ff00UL)  8) | ((tmp  0x00ff00ffUL)  8)); \
-}
-#define REVERSE64(w,x)	{ \
-	sha2_word64 tmp = (w); \
-	tmp = (tmp  32) | (tmp  32); \
-	tmp = (sha2_word64)(((tmp  0xff00ff00ff00ff00ULL)  8) | \
-	  ((tmp  0x00ff00ff00ff00ffULL)  8)); \
-	(x) = (sha2_word64)(((tmp  0xULL)  16) | \
-	  ((tmp  0xULL)  16)); \
-}
-#endif /* BYTE_ORDER == LITTLE_ENDIAN */
-
 /*
  * Macro for incrementally adding the unsigned 64-bit integer n to the
  * unsigned 128-bit integer (represented using a two-element array of
@@ -392,28 +375,15 @@
 
 /* Unrolled SHA-256 round macros: */
 
-#if BYTE_ORDER == LITTLE_ENDIAN
-
 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\
-	REVERSE32(*data++, W256[j]); \
+	W256[j] = be32toh(*data);		\
+	++data;	\
 	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
  K256[j] + W256[j]; \
 	(d) += T1; \
 	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
 	j++
 
-
-#else /* BYTE_ORDER == LITTLE_ENDIAN */
-
-#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)	\
-	T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
-	 K256[j] + (W256[j] = *data++); \
-	(d) += T1; \
-	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
-	j++
-
-#endif /* BYTE_ORDER == LITTLE_ENDIAN */
-
 #define ROUND256(a,b,c,d,e,f,g,h)	\
 	s0 = W256[(j+1)0x0f]; \
 	s0 = sigma0_256(s0); \
@@ -502,15 +472,10 @@
 
 	j = 0;
 	do {
-#if BYTE_ORDER == LITTLE_ENDIAN
-		/* Copy data while converting to host byte order */
-		REVERSE32(*data++,W256[j]);
+		W256[j] = be32toh(*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];
-#else /* BYTE_ORDER == LITTLE_ENDIAN */
-		/* Apply the SHA-256 compression function to update a..h with copy */
-		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
-#endif /* BYTE_ORDER == LITTLE_ENDIAN */
 		T2 = Sigma0_256(a) + Maj(a, b, c);
 		h = g;
 		g = f;
@@ -639,6 +604,7 @@
 static int SHA224_256_Final(sha2_byte digest[], SHA256_CTX* context, size_t len) {
 	sha2_word32	*d = (void *)digest;
 	unsigned int	usedspace;
+	size_t i;
 
 	/* Sanity check: */
 	assert(context != (SHA256_CTX*)0);
@@ -646,10 +612,7 @@
 	/* If no digest buffer is passed, we don't bother doing this: */
 	if (digest != (sha2_byte*)0) {
 		usedspace = (unsigned int)((context-bitcount  3) % SHA256_BLOCK_LENGTH);
-#if BYTE_ORDER == LITTLE_ENDIAN
-		/* Convert FROM host byte order */
-		REVERSE64(context-bitcount,context-bitcount);
-#endif
+		context-bitcount = htobe64(context-bitcount);
 		if (usedspace  0) {
 			/* Begin padding with a 1 bit: */
 			context-buffer[usedspace++] = 0x80;
@@ -680,18 +643,8 @@
 		/* Final transform: */
 		SHA256_Transform(context, (sha2_word32*)(void *)context-buffer);
 
-#if BYTE_ORDER == LITTLE_ENDIAN
-		{
-			/* Convert TO host byte order */
-			size_t	j;
-			for (j = 0; j  len / 4; j++) {
-REVERSE32(context-state[j],context-state[j]);
-*d++ = context-state[j];
-			}
-		}
-#else
-		memcpy(d, context-state, len / 4);
-#endif
+		for (i = 

CVS commit: src/common/lib/libc/hash/sha2

2009-06-11 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Thu Jun 11 19:05:17 UTC 2009

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

Log Message:
KNF


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 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.10 src/common/lib/libc/hash/sha2/sha2.c:1.11
--- src/common/lib/libc/hash/sha2/sha2.c:1.10	Thu Jun 11 19:02:04 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Thu Jun 11 19:05:17 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.10 2009/06/11 19:02:04 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.11 2009/06/11 19:05:17 joerg Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -39,14 +39,14 @@
 #include sys/cdefs.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.10 2009/06/11 19:02:04 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.11 2009/06/11 19:05:17 joerg Exp $);
 
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.10 2009/06/11 19:02:04 joerg Exp $);
+__RCSID($NetBSD: sha2.c,v 1.11 2009/06/11 19:05:17 joerg Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -256,7 +256,9 @@
 #endif
 
 /*** SHA-224: */
-int SHA224_Init(SHA256_CTX* context) {
+int 
+SHA224_Init(SHA256_CTX* context)
+{
 	if (context == (SHA256_CTX*)0) {
 		return 1;
 	}
@@ -268,7 +270,9 @@
 }
 
 /*** SHA-256: */
-int SHA256_Init(SHA256_CTX* context) {
+int
+SHA256_Init(SHA256_CTX* context)
+{
 	if (context == (SHA256_CTX*)0) {
 		return 1;
 	}
@@ -279,7 +283,9 @@
 	return 1;
 }
 
-void SHA224_Transform(SHA224_CTX* context, const uint64_t* data) {
+void
+SHA224_Transform(SHA224_CTX* context, const uint64_t* data)
+{
 	SHA224_Transform((SHA256_CTX*)context, data);
 }
 
@@ -307,7 +313,9 @@
 	(h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
 	j++
 
-void SHA256_Transform(SHA256_CTX* context, const uint32_t* data) {
+void 
+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;
@@ -365,7 +373,9 @@
 
 #else /* SHA2_UNROLL_TRANSFORM */
 
-void SHA256_Transform(SHA256_CTX* context, const uint32_t* data) {
+void
+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;
@@ -440,11 +450,15 @@
 
 #endif /* SHA2_UNROLL_TRANSFORM */
 
-int SHA224_Update(SHA256_CTX *context, const uint8_t *data, size_t len) {
+int
+SHA224_Update(SHA256_CTX *context, const uint8_t *data, size_t len)
+{
 	return SHA256_Update(context, data, len);
 }
 
-int SHA256_Update(SHA256_CTX* context, const uint8_t *data, size_t len) {
+int
+SHA256_Update(SHA256_CTX* context, const uint8_t *data, size_t len)
+{
 	unsigned int	freespace, usedspace;
 
 	if (len == 0) {
@@ -510,7 +524,9 @@
 	return 1;
 }
 
-static int SHA224_256_Final(uint8_t digest[], SHA256_CTX* context, size_t len) {
+static int
+SHA224_256_Final(uint8_t digest[], SHA256_CTX* context, size_t len)
+{
 	uint32_t	*d = (void *)digest;
 	unsigned int	usedspace;
 	size_t i;
@@ -560,16 +576,22 @@
 	return 1;
 }
 
-int SHA224_Final(uint8_t digest[], SHA256_CTX* context) {
+int
+SHA224_Final(uint8_t digest[], SHA256_CTX* context)
+{
 	return SHA224_256_Final(digest, context, SHA224_DIGEST_LENGTH);
 }
 
-int SHA256_Final(uint8_t digest[], SHA256_CTX* context) {
+int
+SHA256_Final(uint8_t digest[], SHA256_CTX* context)
+{
 	return SHA224_256_Final(digest, context, SHA256_DIGEST_LENGTH);
 }
 
 /*** SHA-512: */
-int SHA512_Init(SHA512_CTX* context) {
+int
+SHA512_Init(SHA512_CTX* context)
+{
 	if (context == NULL)
 		return 1;
 
@@ -603,7 +625,9 @@
 	(h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
 	j++
 
-void SHA512_Transform(SHA512_CTX* context, const uint64_t* data) {
+void
+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;
@@ -658,7 +682,9 @@
 
 #else /* SHA2_UNROLL_TRANSFORM */
 
-void SHA512_Transform(SHA512_CTX* context, const uint64_t* data) {
+void
+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;
@@ -731,7 +757,9 @@
 
 #endif /* SHA2_UNROLL_TRANSFORM */
 
-int SHA512_Update(SHA512_CTX* context, const uint8_t *data, size_t len) {
+int
+SHA512_Update(SHA512_CTX* context, const uint8_t *data, size_t len)
+{
 	unsigned int	freespace, usedspace;
 
 	if (len == 0) {
@@ -796,7 +824,9 @@
 	return 1;
 }
 
-static void SHA512_Last(SHA512_CTX* 

CVS commit: src/common/lib/libc/hash/sha2

2009-06-11 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Jun 11 20:35:29 UTC 2009

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

Log Message:
- fix sha224 functions that used sha256 ctx, and add casts.
- fix sha224 function lengths.
- wrap long lines
- use NULL
- fix whitespace
- put all 224 functions together in the same spirit as 384


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 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.11 src/common/lib/libc/hash/sha2/sha2.c:1.12
--- src/common/lib/libc/hash/sha2/sha2.c:1.11	Thu Jun 11 15:05:17 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Thu Jun 11 16:35:29 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.11 2009/06/11 19:05:17 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.12 2009/06/11 20:35:29 christos Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -39,14 +39,14 @@
 #include sys/cdefs.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.11 2009/06/11 19:05:17 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.12 2009/06/11 20:35:29 christos Exp $);
 
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.11 2009/06/11 19:05:17 joerg Exp $);
+__RCSID($NetBSD: sha2.c,v 1.12 2009/06/11 20:35:29 christos Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -113,11 +113,11 @@
  * library -- they are intended for private internal visibility/use
  * only.
  */
-static void SHA512_Last(SHA512_CTX*);
-void SHA224_Transform(SHA224_CTX*, const uint64_t*);
-void SHA256_Transform(SHA256_CTX*, const uint32_t*);
-void SHA384_Transform(SHA384_CTX*, const uint64_t*);
-void SHA512_Transform(SHA512_CTX*, const uint64_t*);
+static void SHA512_Last(SHA512_CTX *);
+void SHA224_Transform(SHA224_CTX *, const uint64_t*);
+void SHA256_Transform(SHA256_CTX *, const uint32_t*);
+void SHA384_Transform(SHA384_CTX *, const uint64_t*);
+void SHA512_Transform(SHA512_CTX *, const uint64_t*);
 
 
 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS /
@@ -255,40 +255,21 @@
 __weak_alias(SHA512_Transform,_SHA512_Transform)
 #endif
 
-/*** SHA-224: */
-int 
-SHA224_Init(SHA256_CTX* context)
-{
-	if (context == (SHA256_CTX*)0) {
-		return 1;
-	}
-	memcpy(context-state, sha224_initial_hash_value, (size_t)(SHA256_DIGEST_LENGTH));
-	memset(context-buffer, 0, (size_t)(SHA256_BLOCK_LENGTH));
-	context-bitcount = 0;
-
-	return 1;
-}
-
 /*** SHA-256: */
 int
-SHA256_Init(SHA256_CTX* context)
+SHA256_Init(SHA256_CTX *context)
 {
-	if (context == (SHA256_CTX*)0) {
+	if (context == NULL)
 		return 1;
-	}
-	memcpy(context-state, sha256_initial_hash_value, (size_t)(SHA256_DIGEST_LENGTH));
+
+	memcpy(context-state, sha256_initial_hash_value,
+	(size_t)(SHA256_DIGEST_LENGTH));
 	memset(context-buffer, 0, (size_t)(SHA256_BLOCK_LENGTH));
 	context-bitcount = 0;
 
 	return 1;
 }
 
-void
-SHA224_Transform(SHA224_CTX* context, const uint64_t* data)
-{
-	SHA224_Transform((SHA256_CTX*)context, data);
-}
-
 #ifdef SHA2_UNROLL_TRANSFORM
 
 /* Unrolled SHA-256 round macros: */
@@ -314,7 +295,7 @@
 	j++
 
 void 
-SHA256_Transform(SHA256_CTX* context, const uint32_t* data)
+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;
@@ -374,7 +355,7 @@
 #else /* SHA2_UNROLL_TRANSFORM */
 
 void
-SHA256_Transform(SHA256_CTX* context, const uint32_t* data)
+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;
@@ -451,13 +432,7 @@
 #endif /* SHA2_UNROLL_TRANSFORM */
 
 int
-SHA224_Update(SHA256_CTX *context, const uint8_t *data, size_t len)
-{
-	return SHA256_Update(context, data, len);
-}
-
-int
-SHA256_Update(SHA256_CTX* context, const uint8_t *data, size_t len)
+SHA256_Update(SHA256_CTX *context, const uint8_t *data, size_t len)
 {
 	unsigned int	freespace, usedspace;
 
@@ -474,11 +449,13 @@
 
 		if (len = freespace) {
 			/* Fill the buffer completely and process it */
-			memcpy(context-buffer[usedspace], data, (size_t)(freespace));
+			memcpy(context-buffer[usedspace], data,
+			(size_t)(freespace));
 			context-bitcount += freespace  3;
 			len -= freespace;
 			data += freespace;
-			SHA256_Transform(context, (uint32_t *)(void *)context-buffer);
+			SHA256_Transform(context,
+			(uint32_t *)(void *)context-buffer);
 		} else {
 			/* The buffer is not yet full */
 			memcpy(context-buffer[usedspace], data, len);
@@ -525,7 +502,7 @@
 }
 
 static int
-SHA224_256_Final(uint8_t digest[], SHA256_CTX* context, size_t len)
+SHA224_256_Final(uint8_t digest[], 

CVS commit: src/common/lib/libc/hash/sha2

2009-06-11 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Jun 11 20:43:00 UTC 2009

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

Log Message:
use memcpy for bitcount writes to the buffer to avoid type punning issues.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 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.12 src/common/lib/libc/hash/sha2/sha2.c:1.13
--- src/common/lib/libc/hash/sha2/sha2.c:1.12	Thu Jun 11 16:35:29 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Thu Jun 11 16:43:00 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.12 2009/06/11 20:35:29 christos Exp $ */
+/* $NetBSD: sha2.c,v 1.13 2009/06/11 20:43:00 christos Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -39,14 +39,14 @@
 #include sys/cdefs.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.12 2009/06/11 20:35:29 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.13 2009/06/11 20:43:00 christos Exp $);
 
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.12 2009/06/11 20:35:29 christos Exp $);
+__RCSID($NetBSD: sha2.c,v 1.13 2009/06/11 20:43:00 christos Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -545,8 +545,8 @@
 			*context-buffer = 0x80;
 		}
 		/* Set the bit count: */
-		*(uint64_t *)(void *)context-buffer[SHA256_SHORT_BLOCK_LENGTH]
-		= context-bitcount;
+		memcpy(context-buffer[SHA256_SHORT_BLOCK_LENGTH],
+		context-bitcount, sizeof(context-bitcount));
 
 		/* Final transform: */
 		SHA256_Transform(context, (uint32_t *)(void *)context-buffer);
@@ -879,10 +879,10 @@
 		*context-buffer = 0x80;
 	}
 	/* Store the length of input data (in bits): */
-	*(uint64_t *)(void *)context-buffer[SHA512_SHORT_BLOCK_LENGTH] =
-	context-bitcount[1];
-	*(uint64_t *)(void *)context-buffer[SHA512_SHORT_BLOCK_LENGTH+8] =
-	context-bitcount[0];
+	memcpy(context-buffer[SHA512_SHORT_BLOCK_LENGTH],
+	context-bitcount[1], sizeof(context-bitcount[1]));
+	memcpy(context-buffer[SHA512_SHORT_BLOCK_LENGTH + 8],
+	context-bitcount[0], sizeof(context-bitcount[0]));
 
 	/* Final transform: */
 	SHA512_Transform(context, (uint64_t *)(void *)context-buffer);



CVS commit: src/common/lib/libc/hash/sha2

2009-06-11 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Thu Jun 11 22:40:42 UTC 2009

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

Log Message:
Provide fallback definitions of be[32|64]toh and htobe[32|64] for
systems without sys/endian.h.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 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.13 src/common/lib/libc/hash/sha2/sha2.c:1.14
--- src/common/lib/libc/hash/sha2/sha2.c:1.13	Thu Jun 11 20:43:00 2009
+++ src/common/lib/libc/hash/sha2/sha2.c	Thu Jun 11 22:40:42 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.13 2009/06/11 20:43:00 christos Exp $ */
+/* $NetBSD: sha2.c,v 1.14 2009/06/11 22:40:42 joerg Exp $ */
 /*	$KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $	*/
 
 /*
@@ -36,17 +36,21 @@
  *
  */
 
+#if HAVE_NBTOOL_CONFIG_H
+#include nbtool_config.h
+#endif
+
 #include sys/cdefs.h
 
 #if defined(_KERNEL) || defined(_STANDALONE)
-__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.13 2009/06/11 20:43:00 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: sha2.c,v 1.14 2009/06/11 22:40:42 joerg Exp $);
 
 #include lib/libkern/libkern.h
 
 #else
 
 #if defined(LIBC_SCCS)  !defined(lint)
-__RCSID($NetBSD: sha2.c,v 1.13 2009/06/11 20:43:00 christos Exp $);
+__RCSID($NetBSD: sha2.c,v 1.14 2009/06/11 22:40:42 joerg Exp $);
 #endif /* LIBC_SCCS and not lint */
 
 #include namespace.h
@@ -56,7 +60,51 @@
 
 #include sys/types.h
 #include sys/sha2.h
-#include sys/endian.h
+
+#if HAVE_NBTOOL_CONFIG_H
+#  if HAVE_SYS_ENDIAN_H
+#include sys/endian.h
+#  else
+#   undef htobe32
+#   undef htobe64
+#   undef be32toh
+#   undef be64toh
+
+static uint32_t
+htobe32(uint32_t x)
+{
+	uint8_t p[4];
+	memcpy(p, x, 4);
+
+	return ((p[0]  24) | (p[1]  16) | (p[2]  8) | p[3]);
+}
+
+static uint64_t
+htobe64(uint64_t x)
+{
+	uint8_t p[8];
+	uint32_t u, v;
+	memcpy(p, x, 8);
+
+	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);
+}
+
+static uint32_t
+be32toh(uint32_t x)
+{
+	return htobe32(x);
+}
+
+static uint64_t
+be64toh(uint64_t x)
+{
+	return htobe64(x);
+}
+#  endif
+#endif
 
 /*** SHA-256/384/512 Various Length Definitions ***/
 /* NOTE: Most of these are in sha2.h */