core/crypto/siphash: odinfmt (NFC)

This commit is contained in:
Yawning Angel
2023-11-17 16:53:29 +09:00
parent d50380709d
commit 391b3090c9
+21 -8
View File
@@ -48,7 +48,10 @@ sum_string_to_buffer_1_3 :: proc(msg, key: string, dst: []byte) {
// sum_bytes_to_buffer_1_3 will hash the given message with the key and write // sum_bytes_to_buffer_1_3 will hash the given message with the key and write
// the computed hash into the provided destination buffer // the computed hash into the provided destination buffer
sum_bytes_to_buffer_1_3 :: proc(msg, key, dst: []byte) { sum_bytes_to_buffer_1_3 :: proc(msg, key, dst: []byte) {
assert(len(dst) >= DIGEST_SIZE, "crypto/siphash: Destination buffer needs to be at least of size 8") assert(
len(dst) >= DIGEST_SIZE,
"crypto/siphash: Destination buffer needs to be at least of size 8",
)
hash := sum_bytes_1_3(msg, key) hash := sum_bytes_1_3(msg, key)
_collect_output(dst[:], hash) _collect_output(dst[:], hash)
} }
@@ -62,7 +65,7 @@ sum_1_3 :: proc {
// verify_u64_1_3 will check if the supplied tag matches with the output you // verify_u64_1_3 will check if the supplied tag matches with the output you
// will get from the provided message and key // will get from the provided message and key
verify_u64_1_3 :: proc (tag: u64 msg, key: []byte) -> bool { verify_u64_1_3 :: proc(tag: u64, msg, key: []byte) -> bool {
return sum_bytes_1_3(msg, key) == tag return sum_bytes_1_3(msg, key) == tag
} }
@@ -105,7 +108,10 @@ sum_string_to_buffer_2_4 :: proc(msg, key: string, dst: []byte) {
// sum_bytes_to_buffer_2_4 will hash the given message with the key and write // sum_bytes_to_buffer_2_4 will hash the given message with the key and write
// the computed hash into the provided destination buffer // the computed hash into the provided destination buffer
sum_bytes_to_buffer_2_4 :: proc(msg, key, dst: []byte) { sum_bytes_to_buffer_2_4 :: proc(msg, key, dst: []byte) {
assert(len(dst) >= DIGEST_SIZE, "crypto/siphash: Destination buffer needs to be at least of size 8") assert(
len(dst) >= DIGEST_SIZE,
"crypto/siphash: Destination buffer needs to be at least of size 8",
)
hash := sum_bytes_2_4(msg, key) hash := sum_bytes_2_4(msg, key)
_collect_output(dst[:], hash) _collect_output(dst[:], hash)
} }
@@ -130,7 +136,7 @@ sum :: proc {
// verify_u64_2_4 will check if the supplied tag matches with the output you // verify_u64_2_4 will check if the supplied tag matches with the output you
// will get from the provided message and key // will get from the provided message and key
verify_u64_2_4 :: proc (tag: u64 msg, key: []byte) -> bool { verify_u64_2_4 :: proc(tag: u64, msg, key: []byte) -> bool {
return sum_bytes_2_4(msg, key) == tag return sum_bytes_2_4(msg, key) == tag
} }
@@ -180,7 +186,10 @@ sum_string_to_buffer_4_8 :: proc(msg, key: string, dst: []byte) {
// sum_bytes_to_buffer_4_8 will hash the given message with the key and write // sum_bytes_to_buffer_4_8 will hash the given message with the key and write
// the computed hash into the provided destination buffer // the computed hash into the provided destination buffer
sum_bytes_to_buffer_4_8 :: proc(msg, key, dst: []byte) { sum_bytes_to_buffer_4_8 :: proc(msg, key, dst: []byte) {
assert(len(dst) >= DIGEST_SIZE, "crypto/siphash: Destination buffer needs to be at least of size 8") assert(
len(dst) >= DIGEST_SIZE,
"crypto/siphash: Destination buffer needs to be at least of size 8",
)
hash := sum_bytes_4_8(msg, key) hash := sum_bytes_4_8(msg, key)
_collect_output(dst[:], hash) _collect_output(dst[:], hash)
} }
@@ -194,7 +203,7 @@ sum_4_8 :: proc {
// verify_u64_4_8 will check if the supplied tag matches with the output you // verify_u64_4_8 will check if the supplied tag matches with the output you
// will get from the provided message and key // will get from the provided message and key
verify_u64_4_8 :: proc (tag: u64 msg, key: []byte) -> bool { verify_u64_4_8 :: proc(tag: u64, msg, key: []byte) -> bool {
return sum_bytes_4_8(msg, key) == tag return sum_bytes_4_8(msg, key) == tag
} }
@@ -219,10 +228,14 @@ init :: proc(ctx: ^Context, key: []byte, c_rounds, d_rounds: int) {
assert(len(key) == KEY_SIZE, "crypto/siphash: Invalid key size, want 16") assert(len(key) == KEY_SIZE, "crypto/siphash: Invalid key size, want 16")
ctx.c_rounds = c_rounds ctx.c_rounds = c_rounds
ctx.d_rounds = d_rounds ctx.d_rounds = d_rounds
is_valid_setting := (ctx.c_rounds == 1 && ctx.d_rounds == 3) || is_valid_setting :=
(ctx.c_rounds == 1 && ctx.d_rounds == 3) ||
(ctx.c_rounds == 2 && ctx.d_rounds == 4) || (ctx.c_rounds == 2 && ctx.d_rounds == 4) ||
(ctx.c_rounds == 4 && ctx.d_rounds == 8) (ctx.c_rounds == 4 && ctx.d_rounds == 8)
assert(is_valid_setting, "crypto/siphash: Incorrect rounds set up. Valid pairs are (1,3), (2,4) and (4,8)") assert(
is_valid_setting,
"crypto/siphash: Incorrect rounds set up. Valid pairs are (1,3), (2,4) and (4,8)",
)
ctx.k0 = util.U64_LE(key[:8]) ctx.k0 = util.U64_LE(key[:8])
ctx.k1 = util.U64_LE(key[8:]) ctx.k1 = util.U64_LE(key[8:])
ctx.v0 = 0x736f6d6570736575 ~ ctx.k0 ctx.v0 = 0x736f6d6570736575 ~ ctx.k0