From d462d9f08cc8fd534ccae14c878f9c7c18112237 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 18 May 2012 14:33:54 -0700 Subject: [PATCH] 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. --- ChangeLog | 4 ++++ lib/sha512.c | 5 +++-- lib/u64.h | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index fbe9c1d3a..c1d7fdfc9 100644 --- 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 diff --git a/lib/sha512.c b/lib/sha512.c index 0c0779c35..193450712 100644 --- a/lib/sha512.c +++ b/lib/sha512.c @@ -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))) diff --git a/lib/u64.h b/lib/u64.h index dadd6d741..f5ec9ebcb 100644 --- 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) -- 2.11.0