pkgsrc-Changes-HG archive

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

[pkgsrc/trunk]: pkgsrc/pkgtools/libnbcompat Fix SHA256/SHA512 to work on stri...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/4f1f4c213d7c
branches:  trunk
changeset: 531045:4f1f4c213d7c
user:      joerg <joerg%pkgsrc.org@localhost>
date:      Wed Jul 18 14:09:55 2007 +0000

description:
Fix SHA256/SHA512 to work on strict alignment platforms. This was
exposed by the C version of audit-packages and report in PR pkg/36662.

diffstat:

 pkgtools/libnbcompat/Makefile     |   4 +-
 pkgtools/libnbcompat/files/sha2.c |  62 ++++++++++++++++++++++++++++++--------
 2 files changed, 51 insertions(+), 15 deletions(-)

diffs (100 lines):

diff -r a2c5e9adc28f -r 4f1f4c213d7c pkgtools/libnbcompat/Makefile
--- a/pkgtools/libnbcompat/Makefile     Wed Jul 18 13:13:04 2007 +0000
+++ b/pkgtools/libnbcompat/Makefile     Wed Jul 18 14:09:55 2007 +0000
@@ -1,11 +1,11 @@
-# $NetBSD: Makefile,v 1.51 2007/06/26 22:10:46 dmcmahill Exp $
+# $NetBSD: Makefile,v 1.52 2007/07/18 14:09:55 joerg Exp $
 #
 # NOTE: If you update this package, it is *mandatory* that you update
 #      pkgsrc/pkgtools/libnbcompat/files/README to reflect the actual
 #      list of tested and supported platforms.
 #
 
-DISTNAME=              libnbcompat-20070626
+DISTNAME=              libnbcompat-20070718
 CATEGORIES=            pkgtools devel
 MASTER_SITES=          # empty
 DISTFILES=             # empty
diff -r a2c5e9adc28f -r 4f1f4c213d7c pkgtools/libnbcompat/files/sha2.c
--- a/pkgtools/libnbcompat/files/sha2.c Wed Jul 18 13:13:04 2007 +0000
+++ b/pkgtools/libnbcompat/files/sha2.c Wed Jul 18 14:09:55 2007 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sha2.c,v 1.6 2007/05/07 16:38:47 joerg Exp $ */
+/* $NetBSD: sha2.c,v 1.7 2007/07/18 14:09:55 joerg Exp $ */
 /*     $KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $    */
 
 /*
@@ -495,12 +495,30 @@
                        return;
                }
        }
-       while (len >= SHA256_BLOCK_LENGTH) {
-               /* Process as many complete blocks as we can */
-               SHA256_Transform(context, (const sha2_word32*)(const void *)data);
-               context->bitcount += SHA256_BLOCK_LENGTH << 3;
-               len -= SHA256_BLOCK_LENGTH;
-               data += SHA256_BLOCK_LENGTH;
+       /*
+        * Process as many complete blocks as possible.
+        *
+        * Check alignment of the data pointer. If it is 32bit aligned,
+        * SHA256_Transform can be called directly on the data stream,
+        * otherwise enforce the alignment by copy into the buffer.
+        */
+       if ((uintptr_t)data % 4 == 0) {
+               while (len >= SHA256_BLOCK_LENGTH) {
+                       SHA256_Transform(context,
+                           (const sha2_word32 *)(const void *)data);
+                       context->bitcount += SHA256_BLOCK_LENGTH << 3;
+                       len -= SHA256_BLOCK_LENGTH;
+                       data += SHA256_BLOCK_LENGTH;
+               }
+       } else {
+               while (len >= SHA256_BLOCK_LENGTH) {
+                       memcpy(context->buffer, data, SHA256_BLOCK_LENGTH);
+                       SHA256_Transform(context,
+                           (const sha2_word32 *)(const void *)context->buffer);
+                       context->bitcount += SHA256_BLOCK_LENGTH << 3;
+                       len -= SHA256_BLOCK_LENGTH;
+                       data += SHA256_BLOCK_LENGTH;
+               }
        }
        if (len > 0) {
                /* There's left-overs, so save 'em */
@@ -785,12 +803,30 @@
                        return;
                }
        }
-       while (len >= SHA512_BLOCK_LENGTH) {
-               /* Process as many complete blocks as we can */
-               SHA512_Transform(context, (const sha2_word64*)(const void *)data);
-               ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
-               len -= SHA512_BLOCK_LENGTH;
-               data += SHA512_BLOCK_LENGTH;
+       /*
+        * Process as many complete blocks as possible.
+        *
+        * Check alignment of the data pointer. If it is 64bit aligned,
+        * SHA512_Transform can be called directly on the data stream,
+        * otherwise enforce the alignment by copy into the buffer.
+        */
+       if ((uintptr_t)data % 8 == 0) {
+               while (len >= SHA512_BLOCK_LENGTH) {
+                       SHA512_Transform(context,
+                           (const sha2_word64 *)(const void *)data);
+                       ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
+                       len -= SHA512_BLOCK_LENGTH;
+                       data += SHA512_BLOCK_LENGTH;
+               }
+       } else {
+               while (len >= SHA512_BLOCK_LENGTH) {
+                       memcpy(context->buffer, data, SHA512_BLOCK_LENGTH);
+                       SHA512_Transform(context,
+                           (const sha2_word64 *)(void *)context->buffer);
+                       ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
+                       len -= SHA512_BLOCK_LENGTH;
+                       data += SHA512_BLOCK_LENGTH;
+               }
        }
        if (len > 0) {
                /* There's left-overs, so save 'em */



Home | Main Index | Thread Index | Old Index