changeset 16861:660cb8105779

crypto: fix bug in large buffer handling * 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.
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 18 May 2012 14:33:54 -0700
parents a3feab896112
children e07426642eaa
files ChangeLog lib/sha512.c lib/u64.h
diffstat 3 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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>
 
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -485,12 +485,13 @@
   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)))
--- a/lib/u64.h
+++ b/lib/u64.h
@@ -30,6 +30,7 @@
 # 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 @@
   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)