From 4587a55486a1c0367778c67fec50d895bf0dbd13 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Fri, 17 Nov 2023 17:34:23 +0900 Subject: [PATCH] core/crypto/sm3: API cleanup - sm3.Sm3_Context -> sm3.Context --- core/crypto/sm3/sm3.odin | 18 ++++++++++-------- vendor/botan/sm3/sm3.odin | 39 ++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/core/crypto/sm3/sm3.odin b/core/crypto/sm3/sm3.odin index 943172482..7a7a0b8a6 100644 --- a/core/crypto/sm3/sm3.odin +++ b/core/crypto/sm3/sm3.odin @@ -31,7 +31,7 @@ hash_string :: proc(data: string) -> [DIGEST_SIZE]byte { // computed hash hash_bytes :: proc(data: []byte) -> [DIGEST_SIZE]byte { hash: [DIGEST_SIZE]byte - ctx: Sm3_Context + ctx: Context init(&ctx) update(&ctx, data) final(&ctx, hash[:]) @@ -49,7 +49,7 @@ hash_string_to_buffer :: proc(data: string, hash: []byte) { // computed hash into the second parameter. // It requires that the destination buffer is at least as big as the digest size hash_bytes_to_buffer :: proc(data, hash: []byte) { - ctx: Sm3_Context + ctx: Context init(&ctx) update(&ctx, data) final(&ctx, hash) @@ -59,10 +59,12 @@ hash_bytes_to_buffer :: proc(data, hash: []byte) { // hash from its contents hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) { hash: [DIGEST_SIZE]byte - ctx: Sm3_Context + ctx: Context init(&ctx) + buf := make([]byte, 512) defer delete(buf) + read := 1 for read > 0 { read, _ = io.read(s, buf) @@ -100,7 +102,7 @@ hash :: proc { Low level API */ -init :: proc(ctx: ^Sm3_Context) { +init :: proc(ctx: ^Context) { ctx.state[0] = IV[0] ctx.state[1] = IV[1] ctx.state[2] = IV[2] @@ -116,7 +118,7 @@ init :: proc(ctx: ^Sm3_Context) { ctx.is_initialized = true } -update :: proc(ctx: ^Sm3_Context, data: []byte) { +update :: proc(ctx: ^Context, data: []byte) { assert(ctx.is_initialized) data := data @@ -141,7 +143,7 @@ update :: proc(ctx: ^Sm3_Context, data: []byte) { } } -final :: proc(ctx: ^Sm3_Context, hash: []byte) { +final :: proc(ctx: ^Context, hash: []byte) { assert(ctx.is_initialized) if len(hash) < DIGEST_SIZE { @@ -176,7 +178,7 @@ final :: proc(ctx: ^Sm3_Context, hash: []byte) { BLOCK_SIZE :: 64 -Sm3_Context :: struct { +Context :: struct { state: [8]u32, x: [BLOCK_SIZE]byte, bitlength: u64, @@ -192,7 +194,7 @@ IV := [8]u32 { } @(private) -block :: proc "contextless" (ctx: ^Sm3_Context, buf: []byte) { +block :: proc "contextless" (ctx: ^Context, buf: []byte) { buf := buf w: [68]u32 diff --git a/vendor/botan/sm3/sm3.odin b/vendor/botan/sm3/sm3.odin index dd6da9e63..961d4f3f9 100644 --- a/vendor/botan/sm3/sm3.odin +++ b/vendor/botan/sm3/sm3.odin @@ -32,11 +32,10 @@ hash_string :: proc "contextless" (data: string) -> [DIGEST_SIZE]byte { // computed hash hash_bytes :: proc "contextless" (data: []byte) -> [DIGEST_SIZE]byte { hash: [DIGEST_SIZE]byte - ctx: botan.hash_t - botan.hash_init(&ctx, botan.HASH_SM3, 0) - botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data))) - botan.hash_final(ctx, &hash[0]) - botan.hash_destroy(ctx) + ctx: Context + init(&ctx) + update(&ctx, data) + final(&ctx, hash[:]) return hash } @@ -52,31 +51,29 @@ hash_string_to_buffer :: proc(data: string, hash: []byte) { // It requires that the destination buffer is at least as big as the digest size hash_bytes_to_buffer :: proc(data, hash: []byte) { assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size") - ctx: botan.hash_t - botan.hash_init(&ctx, botan.HASH_SM3, 0) - botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data))) - botan.hash_final(ctx, &hash[0]) - botan.hash_destroy(ctx) + ctx: Context + init(&ctx) + update(&ctx, data) + final(&ctx, hash[:]) } // hash_stream will read the stream in chunks and compute a // hash from its contents hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) { hash: [DIGEST_SIZE]byte - ctx: botan.hash_t - botan.hash_init(&ctx, botan.HASH_SM3, 0) + ctx: Context + init(&ctx) buf := make([]byte, 512) defer delete(buf) i := 1 for i > 0 { i, _ = io.read(s, buf) if i > 0 { - botan.hash_update(ctx, len(buf) == 0 ? nil : &buf[0], uint(i)) - } + update(&ctx, buf[:i]) + } } - botan.hash_final(ctx, &hash[0]) - botan.hash_destroy(ctx) - return hash, true + final(&ctx, hash[:]) + return hash, true } // hash_file will read the file provided by the given handle @@ -105,17 +102,17 @@ hash :: proc { Low level API */ -Sm3_Context :: botan.hash_t +Context :: botan.hash_t -init :: proc "contextless" (ctx: ^botan.hash_t) { +init :: proc "contextless" (ctx: ^Context) { botan.hash_init(ctx, botan.HASH_SM3, 0) } -update :: proc "contextless" (ctx: ^botan.hash_t, data: []byte) { +update :: proc "contextless" (ctx: ^Context, data: []byte) { botan.hash_update(ctx^, len(data) == 0 ? nil : &data[0], uint(len(data))) } -final :: proc "contextless" (ctx: ^botan.hash_t, hash: []byte) { +final :: proc "contextless" (ctx: ^Context, hash: []byte) { botan.hash_final(ctx^, &hash[0]) botan.hash_destroy(ctx^) } \ No newline at end of file