mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
core/crypto/sha2: odinfmt (NFC)
This commit is contained in:
+31
-16
@@ -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
|
||||||
@@ -603,7 +610,15 @@ PACK32 :: #force_inline proc "contextless"(b: []byte, x: ^u32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user