mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
core/crypto/sm3: API cleanup
- sm3.Sm3_Context -> sm3.Context
This commit is contained in:
@@ -31,7 +31,7 @@ hash_string :: proc(data: string) -> [DIGEST_SIZE]byte {
|
|||||||
// computed hash
|
// computed hash
|
||||||
hash_bytes :: proc(data: []byte) -> [DIGEST_SIZE]byte {
|
hash_bytes :: proc(data: []byte) -> [DIGEST_SIZE]byte {
|
||||||
hash: [DIGEST_SIZE]byte
|
hash: [DIGEST_SIZE]byte
|
||||||
ctx: Sm3_Context
|
ctx: Context
|
||||||
init(&ctx)
|
init(&ctx)
|
||||||
update(&ctx, data)
|
update(&ctx, data)
|
||||||
final(&ctx, hash[:])
|
final(&ctx, hash[:])
|
||||||
@@ -49,7 +49,7 @@ hash_string_to_buffer :: proc(data: string, hash: []byte) {
|
|||||||
// computed hash into the second parameter.
|
// computed hash into the second parameter.
|
||||||
// It requires that the destination buffer is at least as big as the digest size
|
// It requires that the destination buffer is at least as big as the digest size
|
||||||
hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
||||||
ctx: Sm3_Context
|
ctx: Context
|
||||||
init(&ctx)
|
init(&ctx)
|
||||||
update(&ctx, data)
|
update(&ctx, data)
|
||||||
final(&ctx, hash)
|
final(&ctx, hash)
|
||||||
@@ -59,10 +59,12 @@ hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
|||||||
// hash from its contents
|
// hash from its contents
|
||||||
hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
|
hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
|
||||||
hash: [DIGEST_SIZE]byte
|
hash: [DIGEST_SIZE]byte
|
||||||
ctx: Sm3_Context
|
ctx: Context
|
||||||
init(&ctx)
|
init(&ctx)
|
||||||
|
|
||||||
buf := make([]byte, 512)
|
buf := make([]byte, 512)
|
||||||
defer delete(buf)
|
defer delete(buf)
|
||||||
|
|
||||||
read := 1
|
read := 1
|
||||||
for read > 0 {
|
for read > 0 {
|
||||||
read, _ = io.read(s, buf)
|
read, _ = io.read(s, buf)
|
||||||
@@ -100,7 +102,7 @@ hash :: proc {
|
|||||||
Low level API
|
Low level API
|
||||||
*/
|
*/
|
||||||
|
|
||||||
init :: proc(ctx: ^Sm3_Context) {
|
init :: proc(ctx: ^Context) {
|
||||||
ctx.state[0] = IV[0]
|
ctx.state[0] = IV[0]
|
||||||
ctx.state[1] = IV[1]
|
ctx.state[1] = IV[1]
|
||||||
ctx.state[2] = IV[2]
|
ctx.state[2] = IV[2]
|
||||||
@@ -116,7 +118,7 @@ init :: proc(ctx: ^Sm3_Context) {
|
|||||||
ctx.is_initialized = true
|
ctx.is_initialized = true
|
||||||
}
|
}
|
||||||
|
|
||||||
update :: proc(ctx: ^Sm3_Context, data: []byte) {
|
update :: proc(ctx: ^Context, data: []byte) {
|
||||||
assert(ctx.is_initialized)
|
assert(ctx.is_initialized)
|
||||||
|
|
||||||
data := data
|
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)
|
assert(ctx.is_initialized)
|
||||||
|
|
||||||
if len(hash) < DIGEST_SIZE {
|
if len(hash) < DIGEST_SIZE {
|
||||||
@@ -176,7 +178,7 @@ final :: proc(ctx: ^Sm3_Context, hash: []byte) {
|
|||||||
|
|
||||||
BLOCK_SIZE :: 64
|
BLOCK_SIZE :: 64
|
||||||
|
|
||||||
Sm3_Context :: struct {
|
Context :: struct {
|
||||||
state: [8]u32,
|
state: [8]u32,
|
||||||
x: [BLOCK_SIZE]byte,
|
x: [BLOCK_SIZE]byte,
|
||||||
bitlength: u64,
|
bitlength: u64,
|
||||||
@@ -192,7 +194,7 @@ IV := [8]u32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
block :: proc "contextless" (ctx: ^Sm3_Context, buf: []byte) {
|
block :: proc "contextless" (ctx: ^Context, buf: []byte) {
|
||||||
buf := buf
|
buf := buf
|
||||||
|
|
||||||
w: [68]u32
|
w: [68]u32
|
||||||
|
|||||||
Vendored
+18
-21
@@ -32,11 +32,10 @@ hash_string :: proc "contextless" (data: string) -> [DIGEST_SIZE]byte {
|
|||||||
// computed hash
|
// computed hash
|
||||||
hash_bytes :: proc "contextless" (data: []byte) -> [DIGEST_SIZE]byte {
|
hash_bytes :: proc "contextless" (data: []byte) -> [DIGEST_SIZE]byte {
|
||||||
hash: [DIGEST_SIZE]byte
|
hash: [DIGEST_SIZE]byte
|
||||||
ctx: botan.hash_t
|
ctx: Context
|
||||||
botan.hash_init(&ctx, botan.HASH_SM3, 0)
|
init(&ctx)
|
||||||
botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
|
update(&ctx, data)
|
||||||
botan.hash_final(ctx, &hash[0])
|
final(&ctx, hash[:])
|
||||||
botan.hash_destroy(ctx)
|
|
||||||
return 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
|
// It requires that the destination buffer is at least as big as the digest size
|
||||||
hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
||||||
assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size")
|
assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size")
|
||||||
ctx: botan.hash_t
|
ctx: Context
|
||||||
botan.hash_init(&ctx, botan.HASH_SM3, 0)
|
init(&ctx)
|
||||||
botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
|
update(&ctx, data)
|
||||||
botan.hash_final(ctx, &hash[0])
|
final(&ctx, hash[:])
|
||||||
botan.hash_destroy(ctx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_stream will read the stream in chunks and compute a
|
// hash_stream will read the stream in chunks and compute a
|
||||||
// hash from its contents
|
// hash from its contents
|
||||||
hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
|
hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
|
||||||
hash: [DIGEST_SIZE]byte
|
hash: [DIGEST_SIZE]byte
|
||||||
ctx: botan.hash_t
|
ctx: Context
|
||||||
botan.hash_init(&ctx, botan.HASH_SM3, 0)
|
init(&ctx)
|
||||||
buf := make([]byte, 512)
|
buf := make([]byte, 512)
|
||||||
defer delete(buf)
|
defer delete(buf)
|
||||||
i := 1
|
i := 1
|
||||||
for i > 0 {
|
for i > 0 {
|
||||||
i, _ = io.read(s, buf)
|
i, _ = io.read(s, buf)
|
||||||
if i > 0 {
|
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])
|
final(&ctx, hash[:])
|
||||||
botan.hash_destroy(ctx)
|
return hash, true
|
||||||
return hash, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_file will read the file provided by the given handle
|
// hash_file will read the file provided by the given handle
|
||||||
@@ -105,17 +102,17 @@ hash :: proc {
|
|||||||
Low level API
|
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)
|
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)))
|
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_final(ctx^, &hash[0])
|
||||||
botan.hash_destroy(ctx^)
|
botan.hash_destroy(ctx^)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user