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 Use be{32, 64}enc, suggested by joerg.



details:   https://anonhg.NetBSD.org/src/rev/bd3a548b15b5
branches:  trunk
changeset: 787275:bd3a548b15b5
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Jun 09 02:58:58 2013 +0000

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

diffstat:

 common/lib/libc/hash/sha2/sha2.c |  64 ++++++++++------------------------------
 1 files changed, 16 insertions(+), 48 deletions(-)

diffs (160 lines):

diff -r 9893a2785b8a -r bd3a548b15b5 common/lib/libc/hash/sha2/sha2.c
--- a/common/lib/libc/hash/sha2/sha2.c  Sun Jun 09 01:13:47 2013 +0000
+++ b/common/lib/libc/hash/sha2/sha2.c  Sun Jun 09 02:58:58 2013 +0000
@@ -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 @@
 #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 @@
 # 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)
-{
-       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)
+static __inline uint64_t __unused
+be64dec(const void *buf)
 {
-       return htobe32(x);
-}
+       const uint8_t *p = (const uint8_t *)buf;
 
-static uint64_t
-be64toh(uint64_t x)
-{
-       return htobe64(x);
+       return ((__CAST(uint64_t, be32dec(p)) << 32) | be32dec(p + 4));
 }
-# 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 @@
 /* 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 @@
        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 @@
        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 @@
 
        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 @@
 
 /* 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 @@
        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 @@
        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 @@
 
        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];



Home | Main Index | Thread Index | Old Index