crypto: fix bug in large buffer handling
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 18 May 2012 21:33:54 +0000 (14:33 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 18 May 2012 21:34:31 +0000 (14:34 -0700)
* lib/sha512.c (sha512_process_block):
Don't assume the buffer length is less than 2**32.
Here, the bug is present only in the rare case where the host does
not support uint64_t; use u64size to work around the problem.
* lib/u64.h (u64size): New macro.

ChangeLog
lib/sha512.c
lib/u64.h

index fbe9c1d..c1d7fdf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,6 +9,10 @@
        * lib/sha1.c (sha1_process_block):
        * lib/sha256.c (sha256_process_block):
        Don't assume the buffer length is less than 2**32.
+       * lib/sha512.c (sha512_process_block): Likewise.
+       Here, the bug is present only in the rare case where the host does
+       not support uint64_t; use u64size to work around the problem.
+       * lib/u64.h (u64size): New macro.
 
 2012-05-15  Pádraig Brady  <P@draigBrady.com>
 
index 0c0779c..1934507 100644 (file)
@@ -485,12 +485,13 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
   u64 f = ctx->state[5];
   u64 g = ctx->state[6];
   u64 h = ctx->state[7];
+  u64 len64 = u64size (len);
 
   /* First increment the byte count.  FIPS PUB 180-2 specifies the possible
      length of the file up to 2^128 bits.  Here we only compute the
      number of bytes.  Do a double word increment.  */
-  ctx->total[0] = u64plus (ctx->total[0], u64lo (len));
-  if (u64lt (ctx->total[0], u64lo (len)))
+  ctx->total[0] = u64plus (ctx->total[0], len64);
+  if (u64lt (ctx->total[0], len64))
     ctx->total[1] = u64plus (ctx->total[1], u64lo (1));
 
 #define S0(x) u64xor (u64rol(x, 63), u64xor (u64rol (x, 56), u64shr (x, 7)))
index dadd6d7..f5ec9eb 100644 (file)
--- a/lib/u64.h
+++ b/lib/u64.h
@@ -30,6 +30,7 @@ typedef uint64_t u64;
 # define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo)))
 # define u64init(hi, lo) u64hilo (hi, lo)
 # define u64lo(x) ((u64) (x))
+# define u64size(x) u64lo (x)
 # define u64lt(x, y) ((x) < (y))
 # define u64and(x, y) ((x) & (y))
 # define u64or(x, y) ((x) | (y))
@@ -72,6 +73,16 @@ u64lo (uint32_t lo)
   return r;
 }
 
+/* Return a u64 value representing SIZE.  */
+static inline u64
+u64size (size_t size)
+{
+  u64 r;
+  r.hi = size >> 31 >> 1;
+  r.lo = size;
+  return r;
+}
+
 /* Return X < Y.  */
 static inline int
 u64lt (u64 x, u64 y)