diff options
| author | Yawning Angel <yawning@schwanenlied.me> | 2023-11-16 13:26:03 +0900 |
|---|---|---|
| committer | Yawning Angel <yawning@schwanenlied.me> | 2023-11-17 19:31:51 +0900 |
| commit | 71da3ef9255f6f66ff3124355fdf8c9d0b6ff9a7 (patch) | |
| tree | 9fb9dfaca475a5d174c22f393d59aba5fbcc7afa /core/crypto/sha2/sha2.odin | |
| parent | 582bd760b7141e26242e55a376956568c14ff13b (diff) | |
core/crypto/sha2: Fix overflow for large amounts of hashed data
Diffstat (limited to 'core/crypto/sha2/sha2.odin')
| -rw-r--r-- | core/crypto/sha2/sha2.odin | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/core/crypto/sha2/sha2.odin b/core/crypto/sha2/sha2.odin index 47ede9cf4..dd133fa5f 100644 --- a/core/crypto/sha2/sha2.odin +++ b/core/crypto/sha2/sha2.odin @@ -427,7 +427,8 @@ update :: proc(ctx: ^$T, data: []byte) { } final :: proc(ctx: ^$T, hash: []byte) { - block_nb, pm_len, len_b: u32 + block_nb, pm_len: uint + len_b: u64 if len(hash) * 8 < ctx.md_bits { panic("crypto/sha2: invalid destination digest size") @@ -437,15 +438,15 @@ final :: proc(ctx: ^$T, hash: []byte) { when T == Sha256_Context {block_nb = 1 + ((CURR_BLOCK_SIZE - 9) < (ctx.length % CURR_BLOCK_SIZE) ? 1 : 0)} else when T == Sha512_Context {block_nb = 1 + ((CURR_BLOCK_SIZE - 17) < (ctx.length % CURR_BLOCK_SIZE) ? 1 : 0)} - len_b = u32(ctx.tot_len + ctx.length) << 3 + len_b = u64(ctx.tot_len + ctx.length) << 3 when T == Sha256_Context {pm_len = block_nb << 6} else when T == Sha512_Context {pm_len = block_nb << 7} - mem.set(rawptr(&(ctx.block[ctx.length:])[0]), 0, int(uint(pm_len) - ctx.length)) + mem.set(rawptr(&(ctx.block[ctx.length:])[0]), 0, int(pm_len - ctx.length)) ctx.block[ctx.length] = 0x80 - endian.unchecked_put_u32be(ctx.block[pm_len - 4:], len_b) + endian.unchecked_put_u64be(ctx.block[pm_len - 8:], len_b) - sha2_transf(ctx, ctx.block[:], uint(block_nb)) + sha2_transf(ctx, ctx.block[:], block_nb) when T == Sha256_Context { for i := 0; i < ctx.md_bits / 32; i += 1 { |