core/crypto/siphash: odinfmt (NFC)

This commit is contained in:
Yawning Angel
2023-11-17 16:53:29 +09:00
parent d50380709d
commit 391b3090c9
+31 -18
View File
@@ -30,7 +30,7 @@ sum_string_1_3 :: proc(msg, key: string) -> u64 {
// sum_bytes_1_3 will hash the given message with the key and return // sum_bytes_1_3 will hash the given message with the key and return
// the computed hash as a u64 // the computed hash as a u64
sum_bytes_1_3 :: proc (msg, key: []byte) -> u64 { sum_bytes_1_3 :: proc(msg, key: []byte) -> u64 {
ctx: Context ctx: Context
hash: u64 hash: u64
init(&ctx, key, 1, 3) init(&ctx, key, 1, 3)
@@ -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,13 +65,13 @@ 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
} }
// verify_bytes will check if the supplied tag matches with the output you // verify_bytes 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_bytes_1_3 :: proc (tag, msg, key: []byte) -> bool { verify_bytes_1_3 :: proc(tag, msg, key: []byte) -> bool {
derived_tag: [8]byte derived_tag: [8]byte
sum_bytes_to_buffer_1_3(msg, key, derived_tag[:]) sum_bytes_to_buffer_1_3(msg, key, derived_tag[:])
return crypto.compare_constant_time(derived_tag[:], tag) == 1 return crypto.compare_constant_time(derived_tag[:], tag) == 1
@@ -87,7 +90,7 @@ sum_string_2_4 :: proc(msg, key: string) -> u64 {
// sum_bytes_2_4 will hash the given message with the key and return // sum_bytes_2_4 will hash the given message with the key and return
// the computed hash as a u64 // the computed hash as a u64
sum_bytes_2_4 :: proc (msg, key: []byte) -> u64 { sum_bytes_2_4 :: proc(msg, key: []byte) -> u64 {
ctx: Context ctx: Context
hash: u64 hash: u64
init(&ctx, key, 2, 4) init(&ctx, key, 2, 4)
@@ -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,13 +136,13 @@ 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
} }
// verify_bytes will check if the supplied tag matches with the output you // verify_bytes 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_bytes_2_4 :: proc (tag, msg, key: []byte) -> bool { verify_bytes_2_4 :: proc(tag, msg, key: []byte) -> bool {
derived_tag: [8]byte derived_tag: [8]byte
sum_bytes_to_buffer_2_4(msg, key, derived_tag[:]) sum_bytes_to_buffer_2_4(msg, key, derived_tag[:])
return crypto.compare_constant_time(derived_tag[:], tag) == 1 return crypto.compare_constant_time(derived_tag[:], tag) == 1
@@ -162,7 +168,7 @@ sum_string_4_8 :: proc(msg, key: string) -> u64 {
// sum_bytes_4_8 will hash the given message with the key and return // sum_bytes_4_8 will hash the given message with the key and return
// the computed hash as a u64 // the computed hash as a u64
sum_bytes_4_8 :: proc (msg, key: []byte) -> u64 { sum_bytes_4_8 :: proc(msg, key: []byte) -> u64 {
ctx: Context ctx: Context
hash: u64 hash: u64
init(&ctx, key, 4, 8) init(&ctx, key, 4, 8)
@@ -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,13 +203,13 @@ 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
} }
// verify_bytes will check if the supplied tag matches with the output you // verify_bytes 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_bytes_4_8 :: proc (tag, msg, key: []byte) -> bool { verify_bytes_4_8 :: proc(tag, msg, key: []byte) -> bool {
derived_tag: [8]byte derived_tag: [8]byte
sum_bytes_to_buffer_4_8(msg, key, derived_tag[:]) sum_bytes_to_buffer_4_8(msg, key, derived_tag[:])
return crypto.compare_constant_time(derived_tag[:], tag) == 1 return crypto.compare_constant_time(derived_tag[:], tag) == 1
@@ -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
@@ -242,13 +255,13 @@ update :: proc(ctx: ^Context, data: []byte) {
m = u64(ctx.buf[i] & 0xff) m = u64(ctx.buf[i] & 0xff)
i += 1 i += 1
for r in u64(1)..<8 { for r in u64(1) ..< 8 {
m |= u64(ctx.buf[i] & 0xff) << (r * 8) m |= u64(ctx.buf[i] & 0xff) << (r * 8)
i += 1 i += 1
} }
ctx.v3 ~= m ctx.v3 ~= m
for _ in 0..<ctx.c_rounds { for _ in 0 ..< ctx.c_rounds {
_compress(ctx) _compress(ctx)
} }
@@ -266,14 +279,14 @@ final :: proc(ctx: ^Context, dst: ^u64) {
ctx.v3 ~= m ctx.v3 ~= m
for _ in 0..<ctx.c_rounds { for _ in 0 ..< ctx.c_rounds {
_compress(ctx) _compress(ctx)
} }
ctx.v0 ~= m ctx.v0 ~= m
ctx.v2 ~= 0xff ctx.v2 ~= 0xff
for _ in 0..<ctx.d_rounds { for _ in 0 ..< ctx.d_rounds {
_compress(ctx) _compress(ctx)
} }