aboutsummaryrefslogtreecommitdiff
path: root/core/crypto/_blake2
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2023-11-17 16:27:58 +0900
committerYawning Angel <yawning@schwanenlied.me>2023-11-17 19:31:51 +0900
commitaa821991b81fc7e8222347fbd0130e80478a4339 (patch)
tree74a44e7bf003f2c07727a3c075d3441c53e71910 /core/crypto/_blake2
parentb71afdc3ee8648def5de5b8df72e8e25790217f6 (diff)
core/crypto/blake2: API cleanup and bug fixes
- blake2s.Blake2s_Context -> blake2s.Context - blake2b.Blake2b_Context -> blake2b.Context - Fix the BLAKE2s low level API (context type was incorrect) - Support the configurable output size
Diffstat (limited to 'core/crypto/_blake2')
-rw-r--r--core/crypto/_blake2/blake2.odin24
1 files changed, 16 insertions, 8 deletions
diff --git a/core/crypto/_blake2/blake2.odin b/core/crypto/_blake2/blake2.odin
index ce6f88f20..13b58dba9 100644
--- a/core/crypto/_blake2/blake2.odin
+++ b/core/crypto/_blake2/blake2.odin
@@ -86,8 +86,14 @@ BLAKE2B_IV := [8]u64 {
init :: proc(ctx: ^$T) {
when T == Blake2s_Context {
block_size :: BLAKE2S_BLOCK_SIZE
+ max_size :: BLAKE2S_SIZE
} else when T == Blake2b_Context {
block_size :: BLAKE2B_BLOCK_SIZE
+ max_size :: BLAKE2B_SIZE
+ }
+
+ if ctx.cfg.size > max_size {
+ panic("blake2: requested output size exceeeds algorithm max")
}
p := make([]byte, block_size)
@@ -192,13 +198,12 @@ final :: proc(ctx: ^$T, hash: []byte) {
assert(ctx.is_initialized)
when T == Blake2s_Context {
- if len(hash) < BLAKE2S_SIZE {
+ if len(hash) < int(ctx.cfg.size) {
panic("crypto/blake2s: invalid destination digest size")
}
blake2s_final(ctx, hash)
- }
- when T == Blake2b_Context {
- if len(hash) < BLAKE2B_SIZE {
+ } else when T == Blake2b_Context {
+ if len(hash) < int(ctx.cfg.size) {
panic("crypto/blake2b: invalid destination digest size")
}
blake2b_final(ctx, hash)
@@ -228,9 +233,11 @@ blake2s_final :: proc "contextless" (ctx: ^Blake2s_Context, hash: []byte) {
blocks(ctx, ctx.x[:])
+ dst: [BLAKE2S_SIZE]byte
for i := 0; i < BLAKE2S_SIZE / 4; i += 1 {
- endian.unchecked_put_u32le(hash[i * 4:], ctx.h[i])
+ endian.unchecked_put_u32le(dst[i * 4:], ctx.h[i])
}
+ copy(hash, dst[:])
}
@(private)
@@ -254,17 +261,18 @@ blake2b_final :: proc "contextless" (ctx: ^Blake2b_Context, hash: []byte) {
blocks(ctx, ctx.x[:])
+ dst: [BLAKE2B_SIZE]byte
for i := 0; i < BLAKE2B_SIZE / 8; i += 1 {
- endian.unchecked_put_u64le(hash[i * 8:], ctx.h[i])
+ endian.unchecked_put_u64le(dst[i * 8:], ctx.h[i])
}
+ copy(hash, dst[:])
}
@(private)
blocks :: proc "contextless" (ctx: ^$T, p: []byte) {
when T == Blake2s_Context {
blake2s_blocks(ctx, p)
- }
- when T == Blake2b_Context {
+ } else when T == Blake2b_Context {
blake2b_blocks(ctx, p)
}
}