On Sat, Dec 21, 2013 at 22:40, Ted Unangst wrote:
> I also point out that all the HashFinal functions check for a null
> digest and don't do anything in that case. This seems stupid to
> me, I think such programs should crash. I didn't change anything
> yet because they are at least all consistent. Thoughts?

Here's the deep cleanse version. The behavior with null digest was
never documented.

(in sha512, the removal of the block introduces a mix of declarations
and statements. i have left this as is, now that all base compilers
support it, rather than further complicate the ifdef.)

Index: md4.c
===================================================================
RCS file: /cvs/src/lib/libc/hash/md4.c,v
retrieving revision 1.7
diff -u -p -r1.7 md4.c
--- md4.c       8 Aug 2005 08:05:35 -0000       1.7
+++ md4.c       22 Dec 2013 23:03:22 -0000
@@ -127,11 +127,9 @@ MD4Final(unsigned char digest[MD4_DIGEST
        int i;
 
        MD4Pad(ctx);
-       if (digest != NULL) {
-               for (i = 0; i < 4; i++)
-                       PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
-               memset(ctx, 0, sizeof(*ctx));
-       }
+       for (i = 0; i < 4; i++)
+               PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
+       memset(ctx, 0, sizeof(*ctx));
 }
 
 
Index: md5.c
===================================================================
RCS file: /cvs/src/lib/libc/hash/md5.c,v
retrieving revision 1.8
diff -u -p -r1.8 md5.c
--- md5.c       8 Aug 2005 08:05:35 -0000       1.8
+++ md5.c       22 Dec 2013 23:03:22 -0000
@@ -126,11 +126,9 @@ MD5Final(unsigned char digest[MD5_DIGEST
        int i;
 
        MD5Pad(ctx);
-       if (digest != NULL) {
-               for (i = 0; i < 4; i++)
-                       PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
-               memset(ctx, 0, sizeof(*ctx));
-       }
+       for (i = 0; i < 4; i++)
+               PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
+       memset(ctx, 0, sizeof(*ctx));
 }
 
 
Index: rmd160.c
===================================================================
RCS file: /cvs/src/lib/libc/hash/rmd160.c,v
retrieving revision 1.18
diff -u -p -r1.18 rmd160.c
--- rmd160.c    8 Aug 2005 08:05:35 -0000       1.18
+++ rmd160.c    22 Dec 2013 23:03:23 -0000
@@ -151,11 +151,9 @@ RMD160Final(u_int8_t digest[RMD160_DIGES
        int i;
 
        RMD160Pad(ctx);
-       if (digest != NULL) {
-               for (i = 0; i < 5; i++)
-                       PUT_32BIT_LE(digest + i*4, ctx->state[i]);
-               memset(ctx, 0, sizeof (*ctx));
-       }
+       for (i = 0; i < 5; i++)
+               PUT_32BIT_LE(digest + i*4, ctx->state[i]);
+       memset(ctx, 0, sizeof (*ctx));
 }
 
 void
Index: sha1.c
===================================================================
RCS file: /cvs/src/lib/libc/hash/sha1.c,v
retrieving revision 1.22
diff -u -p -r1.22 sha1.c
--- sha1.c      27 Aug 2012 21:05:43 -0000      1.22
+++ sha1.c      22 Dec 2013 23:03:23 -0000
@@ -165,11 +165,9 @@ SHA1Final(u_int8_t digest[SHA1_DIGEST_LE
        u_int i;
 
        SHA1Pad(context);
-       if (digest) {
-               for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
-                       digest[i] = (u_int8_t)
-                          ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
-               }
-               memset(context, 0, sizeof(*context));
+       for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
+               digest[i] = (u_int8_t)
+                  ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
        }
+       memset(context, 0, sizeof(*context));
 }
Index: sha2.c
===================================================================
RCS file: /cvs/src/lib/libc/hash/sha2.c,v
retrieving revision 1.15
diff -u -p -r1.15 sha2.c
--- sha2.c      22 Dec 2013 22:55:51 -0000      1.15
+++ sha2.c      22 Dec 2013 23:03:23 -0000
@@ -302,19 +302,16 @@ SHA224Final(u_int8_t digest[SHA224_DIGES
 {
        SHA224Pad(context);
 
-       /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != NULL) {
 #if BYTE_ORDER == LITTLE_ENDIAN
-               int     i;
+       int     i;
 
-               /* Convert TO host byte order */
-               for (i = 0; i < 7; i++)
-                       BE_32_TO_8(digest + i * 4, context->state.st32[i]);
+       /* Convert TO host byte order */
+       for (i = 0; i < 7; i++)
+               BE_32_TO_8(digest + i * 4, context->state.st32[i]);
 #else
-               memcpy(digest, context->state.st32, SHA224_DIGEST_LENGTH);
+       memcpy(digest, context->state.st32, SHA224_DIGEST_LENGTH);
 #endif
-               memset(context, 0, sizeof(*context));
-       }
+       memset(context, 0, sizeof(*context));
 }
 #endif /* SHA256_ONLY */
 
@@ -580,19 +577,16 @@ SHA256Final(u_int8_t digest[SHA256_DIGES
 {
        SHA256Pad(context);
 
-       /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != NULL) {
 #if BYTE_ORDER == LITTLE_ENDIAN
-               int     i;
+       int     i;
 
-               /* Convert TO host byte order */
-               for (i = 0; i < 8; i++)
-                       BE_32_TO_8(digest + i * 4, context->state.st32[i]);
+       /* Convert TO host byte order */
+       for (i = 0; i < 8; i++)
+               BE_32_TO_8(digest + i * 4, context->state.st32[i]);
 #else
-               memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
+       memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
 #endif
-               memset(context, 0, sizeof(*context));
-       }
+       memset(context, 0, sizeof(*context));
 }
 
 
@@ -860,19 +854,16 @@ SHA512Final(u_int8_t digest[SHA512_DIGES
 {
        SHA512Pad(context);
 
-       /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != NULL) {
 #if BYTE_ORDER == LITTLE_ENDIAN
-               int     i;
+       int     i;
 
-               /* Convert TO host byte order */
-               for (i = 0; i < 8; i++)
-                       BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+       /* Convert TO host byte order */
+       for (i = 0; i < 8; i++)
+               BE_64_TO_8(digest + i * 8, context->state.st64[i]);
 #else
-               memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
+       memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
 #endif
-               memset(context, 0, sizeof(*context));
-       }
+       memset(context, 0, sizeof(*context));
 }
 
 
@@ -895,19 +886,15 @@ SHA384Final(u_int8_t digest[SHA384_DIGES
 {
        SHA384Pad(context);
 
-       /* If no digest buffer is passed, we don't bother doing this: */
-       if (digest != NULL) {
 #if BYTE_ORDER == LITTLE_ENDIAN
-               int     i;
+       int     i;
 
-               /* Convert TO host byte order */
-               for (i = 0; i < 6; i++)
-                       BE_64_TO_8(digest + i * 8, context->state.st64[i]);
+       /* Convert TO host byte order */
+       for (i = 0; i < 6; i++)
+               BE_64_TO_8(digest + i * 8, context->state.st64[i]);
 #else
-               memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
+       memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
 #endif
-       }
-
        /* Zero out state data */
        memset(context, 0, sizeof(*context));
 }

Reply via email to