diff --git a/ChangeLog.d/sha256-update-ilen-over-4gib.txt b/ChangeLog.d/sha256-update-ilen-over-4gib.txt index 8769ea10ac..cad065a107 100644 --- a/ChangeLog.d/sha256-update-ilen-over-4gib.txt +++ b/ChangeLog.d/sha256-update-ilen-over-4gib.txt @@ -1,6 +1,5 @@ Bugfix - * Fix mbedtls_sha256_update() computing the wrong digest when a single - call is passed 4GiB (2^32 bytes) or more of input on a 64-bit - platform: the upper 32 bits of the input length were silently - dropped from the running byte count used at finalization. Reported - by TimothyPMann in #10964. + * Fix mbedtls_md5_update(), mbedtls_sha1_update(), + mbedtls_sha256_update() and mbedtls_ripemd160_update() computing the + wrong digest when a single call is passed 4GiB or more of input on a + 64-bit platform. Reported by TimothyPMann in #10964. diff --git a/library/md5.c b/library/md5.c index e4a87a2e09..088061930c 100644 --- a/library/md5.c +++ b/library/md5.c @@ -222,6 +222,10 @@ int mbedtls_md5_update(mbedtls_md5_context *ctx, ctx->total[1]++; } +#if SIZE_MAX > 0xFFFFFFFF + ctx->total[1] += (uint32_t) (ilen >> 32); +#endif + if (left && ilen >= fill) { memcpy((void *) (ctx->buffer + left), input, fill); if ((ret = mbedtls_internal_md5_process(ctx, ctx->buffer)) != 0) { diff --git a/library/ripemd160.c b/library/ripemd160.c index b4fc3cdba1..6a8f3123d3 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -285,6 +285,10 @@ int mbedtls_ripemd160_update(mbedtls_ripemd160_context *ctx, ctx->total[1]++; } +#if SIZE_MAX > 0xFFFFFFFF + ctx->total[1] += (uint32_t) (ilen >> 32); +#endif + if (left && ilen >= fill) { memcpy((void *) (ctx->buffer + left), input, fill); diff --git a/library/sha1.c b/library/sha1.c index dfbe481f39..fe28022ae2 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -257,6 +257,10 @@ int mbedtls_sha1_update(mbedtls_sha1_context *ctx, ctx->total[1]++; } +#if SIZE_MAX > 0xFFFFFFFF + ctx->total[1] += (uint32_t) (ilen >> 32); +#endif + if (left && ilen >= fill) { memcpy((void *) (ctx->buffer + left), input, fill);