Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/common/lib/libc/hash/sha2 PR/47908: Gary Grebus: SHA256_Tran...



details:   https://anonhg.NetBSD.org/src/rev/0bea582b2b6f
branches:  trunk
changeset: 787234:0bea582b2b6f
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Jun 07 22:40:34 2013 +0000

description:
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

diffstat:

 common/lib/libc/hash/sha2/sha2.c |  42 +++++++++++++++++++++++++--------------
 1 files changed, 27 insertions(+), 15 deletions(-)

diffs (133 lines):

diff -r e750b3ee42a0 -r 0bea582b2b6f common/lib/libc/hash/sha2/sha2.c
--- a/common/lib/libc/hash/sha2/sha2.c  Fri Jun 07 19:28:25 2013 +0000
+++ b/common/lib/libc/hash/sha2/sha2.c  Fri Jun 07 22:40:34 2013 +0000
@@ -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 @@
 #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 @@
 #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 @@
 {
        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 @@
 /* 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 @@
        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 @@
        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 @@
 
        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 @@
 
 /* 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 @@
        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 @@
        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 @@
 
        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];



Home | Main Index | Thread Index | Old Index