Merge pull request #10975 from Johnsonajibi/fix-sha256-4gib-overflow

sha256: fold high bits of ilen into total[1] on 64-bit platforms
This commit is contained in:
Manuel Pégourié-Gonnard
2026-10-08 07:03:33 +00:00
committed by GitHub
5 changed files with 28 additions and 0 deletions
@@ -0,0 +1,12 @@
Bugfix
* Fix MD5, SHA1, SHA224, SHA256 and RIPEMD160 computing the wrong
digest when 4GiB of data or more are processed in a single call.
That can happen either when using the one-shot APIs
(psa_hash_compute(), psa_hash_compare(), mbedtls_md(),
mbedtls_md5(), mbedtls_sha1(), mbedtls_ripemd160(),
mbedtls_sha256()) with more than 4GiB of input, or when using the
streaming API and passing more than 4GiB in a single call to the
update function (psa_hash_update(), mbedtls_md_update(),
mbedtls_md5_update(), mbedtls_sha1_update(),
mbedtls_ripemd160_update(), mbedtls_sha256_update()). Reported by
TimothyPMann in #10964.
+4
View File
@@ -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) {
+4
View File
@@ -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);
+4
View File
@@ -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);
+4
View File
@@ -667,6 +667,10 @@ int mbedtls_sha256_update(mbedtls_sha256_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);