]> Savannah Git Hosting - gnulib.git/commitdiff
md5, sha1, sha256, sha512: Add comments regarding correctness.
authorBruno Haible <bruno@clisp.org>
Fri, 31 Mar 2017 20:03:49 +0000 (22:03 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 31 Mar 2017 20:40:40 +0000 (22:40 +0200)
* lib/md5.h (buflen): Add comments regarding range.
* lib/sha1.h (buflen): Likewise.
* lib/sha256.h (buflen): Likewise.
* lib/sha512.h (buflen): Likewise.
* lib/md5.c (md5_process_bytes): Add comment why memmove is not needed.
* lib/sha1.c (sha1_process_bytes): Likewise.
* lib/sha256.c (sha256_process_bytes): Likewise.
* lib/sha512.c (sha512_process_bytes): Likewise.
Reported by Coverity via Tim Rühsen.

ChangeLog
lib/md5.c
lib/md5.h
lib/sha1.c
lib/sha1.h
lib/sha256.c
lib/sha256.h
lib/sha512.c
lib/sha512.h

index ad7ae9e095dc41d67f07ab4ac86d1cde42287379..7c15292c174bf21166f6d0d9a97585a24bec991c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2017-03-31  Bruno Haible  <bruno@clisp.org>
+
+       md5, sha1, sha256, sha512: Add comments regarding correctness.
+       * lib/md5.h (buflen): Add comments regarding range.
+       * lib/sha1.h (buflen): Likewise.
+       * lib/sha256.h (buflen): Likewise.
+       * lib/sha512.h (buflen): Likewise.
+       * lib/md5.c (md5_process_bytes): Add comment why memmove is not needed.
+       * lib/sha1.c (sha1_process_bytes): Likewise.
+       * lib/sha256.c (sha256_process_bytes): Likewise.
+       * lib/sha512.c (sha512_process_bytes): Likewise.
+       Reported by Coverity via Tim Rühsen.
+
 2017-03-22  Paul Eggert  <eggert@cs.ucla.edu>
 
        getopt: merge from glibc
index 9f5237ec5bde44f653e032678265f1f852592fe1..86509571b028795dd6a247694d242f35ccef74b5 100644 (file)
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -246,7 +246,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
           md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
           ctx->buflen &= 63;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~63],
                   ctx->buflen);
@@ -288,6 +289,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
         {
           md5_process_block (ctx->buffer, 64, ctx);
           left_over -= 64;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 64.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
index 9c2c09815ab016d5d78c0a24e4d3fda269d16480..543a3664432958090e998df50b8981a2ac77f15a 100644 (file)
--- a/lib/md5.h
+++ b/lib/md5.h
@@ -74,8 +74,8 @@ struct md5_ctx
   uint32_t D;
 
   uint32_t total[2];
-  uint32_t buflen;
-  uint32_t buffer[32];
+  uint32_t buflen;     /* ≥ 0, ≤ 128 */
+  uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
 };
 
 /*
index 87c57719e8a25a7a196e99624bfbdafc825ebd7b..690865084895ccd1f2d4a1365c467aca6bd8c317 100644 (file)
@@ -233,7 +233,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
           sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
           ctx->buflen &= 63;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~63],
                   ctx->buflen);
@@ -275,6 +276,8 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
         {
           sha1_process_block (ctx->buffer, 64, ctx);
           left_over -= 64;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 64.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
index 38e82f363491deb9b2c40fd51615d3c6e96ed658..0deb7ba0f180bc57c863d6190dbaede2c7e2faa0 100644 (file)
@@ -46,8 +46,8 @@ struct sha1_ctx
   uint32_t E;
 
   uint32_t total[2];
-  uint32_t buflen;
-  uint32_t buffer[32];
+  uint32_t buflen;     /* ≥ 0, ≤ 128 */
+  uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
 };
 
 /* Initialize structure containing state of computation. */
index 03d3899ad27a2f143e0fbf292793d3d40930483a..c0fb8beecfe0591ae68d6dda74ca08fe95a7d222 100644 (file)
@@ -366,7 +366,8 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
           sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 
           ctx->buflen &= 63;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~63],
                   ctx->buflen);
@@ -408,6 +409,8 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
         {
           sha256_process_block (ctx->buffer, 64, ctx);
           left_over -= 64;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 64.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
index ffb91fa47efb9632a85af8316b2dabb22004d38b..348b76ef265984a75eab1e99de4ed92177231e83 100644 (file)
@@ -44,8 +44,8 @@ struct sha256_ctx
   uint32_t state[8];
 
   uint32_t total[2];
-  size_t buflen;
-  uint32_t buffer[32];
+  size_t buflen;       /* ≥ 0, ≤ 128 */
+  uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
 };
 
 /* Initialize structure containing state of computation. */
index 6876bfdb08c73147f32b9a1101bc8d6b549fb256..dbde67183b78a8015f3864a68c0b9f518ac8ba7d 100644 (file)
@@ -374,7 +374,8 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
           sha512_process_block (ctx->buffer, ctx->buflen & ~127, ctx);
 
           ctx->buflen &= 127;
-          /* The regions in the following copy operation cannot overlap.  */
+          /* The regions in the following copy operation cannot overlap,
+             because ctx->buflen < 128 ≤ (left_over + add) & ~127.  */
           memcpy (ctx->buffer,
                   &((char *) ctx->buffer)[(left_over + add) & ~127],
                   ctx->buflen);
@@ -416,6 +417,8 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx)
         {
           sha512_process_block (ctx->buffer, 128, ctx);
           left_over -= 128;
+          /* The regions in the following copy operation cannot overlap,
+             because left_over ≤ 128.  */
           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
         }
       ctx->buflen = left_over;
index 121e6c3a252f04e83edc9154bb75a468f38e14f0..4460e6c9b76213654f9af4d47a065164fbdffaa0 100644 (file)
@@ -44,8 +44,8 @@ struct sha512_ctx
   u64 state[8];
 
   u64 total[2];
-  size_t buflen;
-  u64 buffer[32];
+  size_t buflen;  /* ≥ 0, ≤ 256 */
+  u64 buffer[32]; /* 256 bytes; the first buflen bytes are in use */
 };
 
 /* Initialize structure containing state of computation. */