core/crypto/sha2: odinfmt (NFC)

This commit is contained in:
Yawning Angel
2023-11-17 16:53:29 +09:00
parent b4e3da84c5
commit 14a46c6d5e
+45 -30
View File
@@ -11,9 +11,9 @@ package sha2
and in RFC 3874 <https://datatracker.ietf.org/doc/html/rfc3874> and in RFC 3874 <https://datatracker.ietf.org/doc/html/rfc3874>
*/ */
import "core:io"
import "core:mem" import "core:mem"
import "core:os" import "core:os"
import "core:io"
import "../util" import "../util"
@@ -55,7 +55,10 @@ hash_string_to_buffer_224 :: 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_224 :: proc(data, hash: []byte) { hash_bytes_to_buffer_224 :: proc(data, hash: []byte) {
assert(len(hash) >= DIGEST_SIZE_224, "Size of destination buffer is smaller than the digest size") assert(
len(hash) >= DIGEST_SIZE_224,
"Size of destination buffer is smaller than the digest size",
)
ctx: Sha256_Context ctx: Sha256_Context
ctx.is224 = true ctx.is224 = true
init(&ctx) init(&ctx)
@@ -134,7 +137,10 @@ hash_string_to_buffer_256 :: 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_256 :: proc(data, hash: []byte) { hash_bytes_to_buffer_256 :: proc(data, hash: []byte) {
assert(len(hash) >= DIGEST_SIZE_256, "Size of destination buffer is smaller than the digest size") assert(
len(hash) >= DIGEST_SIZE_256,
"Size of destination buffer is smaller than the digest size",
)
ctx: Sha256_Context ctx: Sha256_Context
ctx.is224 = false ctx.is224 = false
init(&ctx) init(&ctx)
@@ -213,7 +219,10 @@ hash_string_to_buffer_384 :: 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_384 :: proc(data, hash: []byte) { hash_bytes_to_buffer_384 :: proc(data, hash: []byte) {
assert(len(hash) >= DIGEST_SIZE_384, "Size of destination buffer is smaller than the digest size") assert(
len(hash) >= DIGEST_SIZE_384,
"Size of destination buffer is smaller than the digest size",
)
ctx: Sha512_Context ctx: Sha512_Context
ctx.is384 = true ctx.is384 = true
init(&ctx) init(&ctx)
@@ -292,7 +301,10 @@ hash_string_to_buffer_512 :: 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_512 :: proc(data, hash: []byte) { hash_bytes_to_buffer_512 :: proc(data, hash: []byte) {
assert(len(hash) >= DIGEST_SIZE_512, "Size of destination buffer is smaller than the digest size") assert(
len(hash) >= DIGEST_SIZE_512,
"Size of destination buffer is smaller than the digest size",
)
ctx: Sha512_Context ctx: Sha512_Context
ctx.is384 = false ctx.is384 = false
init(&ctx) init(&ctx)
@@ -420,28 +432,23 @@ update :: proc(ctx: ^$T, data: []byte) {
rem_len = new_len % CURR_BLOCK_SIZE rem_len = new_len % CURR_BLOCK_SIZE
if rem_len > 0 { if rem_len > 0 {
when T == Sha256_Context {copy(ctx.block[:], shifted_message[block_nb << 6:rem_len])} when T == Sha256_Context {copy(ctx.block[:], shifted_message[block_nb << 6:rem_len])} else when T == Sha512_Context {copy(ctx.block[:], shifted_message[block_nb << 7:rem_len])}
else when T == Sha512_Context {copy(ctx.block[:], shifted_message[block_nb << 7:rem_len])}
} }
ctx.length = rem_len ctx.length = rem_len
when T == Sha256_Context {ctx.tot_len += (block_nb + 1) << 6} when T == Sha256_Context {ctx.tot_len += (block_nb + 1) << 6} else when T == Sha512_Context {ctx.tot_len += (block_nb + 1) << 7}
else when T == Sha512_Context {ctx.tot_len += (block_nb + 1) << 7}
} }
final :: proc(ctx: ^$T, hash: []byte) { final :: proc(ctx: ^$T, hash: []byte) {
block_nb, pm_len, len_b: u32 block_nb, pm_len, len_b: u32
i: i32 i: i32
when T == Sha256_Context {CURR_BLOCK_SIZE :: SHA256_BLOCK_SIZE} when T == Sha256_Context {CURR_BLOCK_SIZE :: SHA256_BLOCK_SIZE} else when T == Sha512_Context {CURR_BLOCK_SIZE :: SHA512_BLOCK_SIZE}
else when T == Sha512_Context {CURR_BLOCK_SIZE :: SHA512_BLOCK_SIZE}
when T == Sha256_Context {block_nb = 1 + ((CURR_BLOCK_SIZE - 9) < (ctx.length % CURR_BLOCK_SIZE) ? 1 : 0)} when T == Sha256_Context {block_nb = 1 + ((CURR_BLOCK_SIZE - 9) < (ctx.length % CURR_BLOCK_SIZE) ? 1 : 0)} else when T == Sha512_Context {block_nb = 1 + ((CURR_BLOCK_SIZE - 17) < (ctx.length % CURR_BLOCK_SIZE) ? 1 : 0)}
else when T == Sha512_Context {block_nb = 1 + ((CURR_BLOCK_SIZE - 17) < (ctx.length % CURR_BLOCK_SIZE) ? 1 : 0)}
len_b = u32(ctx.tot_len + ctx.length) << 3 len_b = u32(ctx.tot_len + ctx.length) << 3
when T == Sha256_Context {pm_len = block_nb << 6} when T == Sha256_Context {pm_len = block_nb << 6} else when T == Sha512_Context {pm_len = block_nb << 7}
else when T == Sha512_Context {pm_len = block_nb << 7}
mem.set(rawptr(&(ctx.block[ctx.length:])[0]), 0, int(uint(pm_len) - ctx.length)) mem.set(rawptr(&(ctx.block[ctx.length:])[0]), 0, int(uint(pm_len) - ctx.length))
ctx.block[ctx.length] = 0x80 ctx.block[ctx.length] = 0x80
@@ -550,60 +557,68 @@ sha512_k := [80]u64 {
0x5fcb6fab3ad6faec, 0x6c44198c4a475817, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
} }
SHA256_CH :: #force_inline proc "contextless"(x, y, z: u32) -> u32 { SHA256_CH :: #force_inline proc "contextless" (x, y, z: u32) -> u32 {
return (x & y) ~ (~x & z) return (x & y) ~ (~x & z)
} }
SHA256_MAJ :: #force_inline proc "contextless"(x, y, z: u32) -> u32 { SHA256_MAJ :: #force_inline proc "contextless" (x, y, z: u32) -> u32 {
return (x & y) ~ (x & z) ~ (y & z) return (x & y) ~ (x & z) ~ (y & z)
} }
SHA512_CH :: #force_inline proc "contextless"(x, y, z: u64) -> u64 { SHA512_CH :: #force_inline proc "contextless" (x, y, z: u64) -> u64 {
return (x & y) ~ (~x & z) return (x & y) ~ (~x & z)
} }
SHA512_MAJ :: #force_inline proc "contextless"(x, y, z: u64) -> u64 { SHA512_MAJ :: #force_inline proc "contextless" (x, y, z: u64) -> u64 {
return (x & y) ~ (x & z) ~ (y & z) return (x & y) ~ (x & z) ~ (y & z)
} }
SHA256_F1 :: #force_inline proc "contextless"(x: u32) -> u32 { SHA256_F1 :: #force_inline proc "contextless" (x: u32) -> u32 {
return util.ROTR32(x, 2) ~ util.ROTR32(x, 13) ~ util.ROTR32(x, 22) return util.ROTR32(x, 2) ~ util.ROTR32(x, 13) ~ util.ROTR32(x, 22)
} }
SHA256_F2 :: #force_inline proc "contextless"(x: u32) -> u32 { SHA256_F2 :: #force_inline proc "contextless" (x: u32) -> u32 {
return util.ROTR32(x, 6) ~ util.ROTR32(x, 11) ~ util.ROTR32(x, 25) return util.ROTR32(x, 6) ~ util.ROTR32(x, 11) ~ util.ROTR32(x, 25)
} }
SHA256_F3 :: #force_inline proc "contextless"(x: u32) -> u32 { SHA256_F3 :: #force_inline proc "contextless" (x: u32) -> u32 {
return util.ROTR32(x, 7) ~ util.ROTR32(x, 18) ~ (x >> 3) return util.ROTR32(x, 7) ~ util.ROTR32(x, 18) ~ (x >> 3)
} }
SHA256_F4 :: #force_inline proc "contextless"(x: u32) -> u32 { SHA256_F4 :: #force_inline proc "contextless" (x: u32) -> u32 {
return util.ROTR32(x, 17) ~ util.ROTR32(x, 19) ~ (x >> 10) return util.ROTR32(x, 17) ~ util.ROTR32(x, 19) ~ (x >> 10)
} }
SHA512_F1 :: #force_inline proc "contextless"(x: u64) -> u64 { SHA512_F1 :: #force_inline proc "contextless" (x: u64) -> u64 {
return util.ROTR64(x, 28) ~ util.ROTR64(x, 34) ~ util.ROTR64(x, 39) return util.ROTR64(x, 28) ~ util.ROTR64(x, 34) ~ util.ROTR64(x, 39)
} }
SHA512_F2 :: #force_inline proc "contextless"(x: u64) -> u64 { SHA512_F2 :: #force_inline proc "contextless" (x: u64) -> u64 {
return util.ROTR64(x, 14) ~ util.ROTR64(x, 18) ~ util.ROTR64(x, 41) return util.ROTR64(x, 14) ~ util.ROTR64(x, 18) ~ util.ROTR64(x, 41)
} }
SHA512_F3 :: #force_inline proc "contextless"(x: u64) -> u64 { SHA512_F3 :: #force_inline proc "contextless" (x: u64) -> u64 {
return util.ROTR64(x, 1) ~ util.ROTR64(x, 8) ~ (x >> 7) return util.ROTR64(x, 1) ~ util.ROTR64(x, 8) ~ (x >> 7)
} }
SHA512_F4 :: #force_inline proc "contextless"(x: u64) -> u64 { SHA512_F4 :: #force_inline proc "contextless" (x: u64) -> u64 {
return util.ROTR64(x, 19) ~ util.ROTR64(x, 61) ~ (x >> 6) return util.ROTR64(x, 19) ~ util.ROTR64(x, 61) ~ (x >> 6)
} }
PACK32 :: #force_inline proc "contextless"(b: []byte, x: ^u32) { PACK32 :: #force_inline proc "contextless" (b: []byte, x: ^u32) {
x^ = u32(b[3]) | u32(b[2]) << 8 | u32(b[1]) << 16 | u32(b[0]) << 24 x^ = u32(b[3]) | u32(b[2]) << 8 | u32(b[1]) << 16 | u32(b[0]) << 24
} }
PACK64 :: #force_inline proc "contextless"(b: []byte, x: ^u64) { PACK64 :: #force_inline proc "contextless" (b: []byte, x: ^u64) {
x^ = u64(b[7]) | u64(b[6]) << 8 | u64(b[5]) << 16 | u64(b[4]) << 24 | u64(b[3]) << 32 | u64(b[2]) << 40 | u64(b[1]) << 48 | u64(b[0]) << 56 x^ =
u64(b[7]) |
u64(b[6]) << 8 |
u64(b[5]) << 16 |
u64(b[4]) << 24 |
u64(b[3]) << 32 |
u64(b[2]) << 40 |
u64(b[1]) << 48 |
u64(b[0]) << 56
} }
sha2_transf :: proc(ctx: ^$T, data: []byte, block_nb: uint) { sha2_transf :: proc(ctx: ^$T, data: []byte, block_nb: uint) {