core/crypto/sha2: Add scaffolding for SHA512 acceleration

This commit is contained in:
Yawning Angel
2026-03-16 14:58:23 +09:00
parent a030fb6596
commit ccb56de19a
3 changed files with 32 additions and 8 deletions

View File

@@ -44,7 +44,8 @@ Context_256 :: struct {
length: u64,
md_bits: int,
is_initialized: bool,
is_hw_accelerated: bool,
is_initialized: bool,
}
// Context_512 is a SHA-384, SHA-512 or SHA-512/256 instance.
@@ -55,7 +56,8 @@ Context_512 :: struct {
length: u64,
md_bits: int,
is_initialized: bool,
is_hw_accelerated: bool,
is_initialized: bool,
}
// init_224 initializes a Context_256 for SHA-224.
@@ -88,6 +90,9 @@ init_512_256 :: proc(ctx: ^Context_512) {
_init(ctx)
}
@(private)
ERR_HW_NOT_SUPPORTED :: "crypto/sha2: hardware implementation unsupported"
@(private)
_init :: proc(ctx: ^$T) {
when T == Context_256 {
@@ -113,6 +118,8 @@ _init :: proc(ctx: ^$T) {
case:
panic("crypto/sha2: invalid digest output length")
}
ctx.is_hw_accelerated = is_hardware_accelerated_256()
} else when T == Context_512 {
switch ctx.md_bits {
case 256:
@@ -148,6 +155,8 @@ _init :: proc(ctx: ^$T) {
case:
panic("crypto/sha2: invalid digest output length")
}
ctx.is_hw_accelerated = is_hardware_accelerated_512()
}
ctx.length = 0
@@ -399,7 +408,7 @@ SHA512_F4 :: #force_inline proc "contextless" (x: u64) -> u64 {
@(private)
sha2_transf :: proc "contextless" (ctx: ^$T, data: []byte) #no_bounds_check {
when T == Context_256 {
if is_hardware_accelerated_256() {
if ctx.is_hw_accelerated {
sha256_transf_hw(ctx, data)
return
}
@@ -410,6 +419,11 @@ sha2_transf :: proc "contextless" (ctx: ^$T, data: []byte) #no_bounds_check {
CURR_BLOCK_SIZE :: BLOCK_SIZE_256
} else when T == Context_512 {
if ctx.is_hw_accelerated {
sha512_transf_hw(ctx, data)
return
}
w: [SHA512_ROUNDS]u64
wv: [8]u64
t1, t2: u64

View File

@@ -3,15 +3,13 @@
#+build !arm32
package sha2
@(private = "file")
ERR_HW_NOT_SUPPORTED :: "crypto/sha2: hardware implementation unsupported"
// is_hardware_accelerated_256 returns true if and only if () hardware accelerated
// SHA-224/SHA-256 is supported.
// is_hardware_accelerated_256 returns true if and only if () hardware
// accelerated SHA-224/SHA-256 is supported.
is_hardware_accelerated_256 :: proc "contextless" () -> bool {
return false
}
@(private)
sha256_transf_hw :: proc "contextless" (ctx: ^Context_256, data: []byte) {
panic_contextless(ERR_HW_NOT_SUPPORTED)
}

View File

@@ -0,0 +1,12 @@
package sha2
// is_hardware_accelerated_512 returns true if and only if (⟺) hardware
// accelerated SHA-384/SHA-512/SHA-512/256 are supported.
is_hardware_accelerated_512 :: proc "contextless" () -> bool {
return false
}
@(private)
sha512_transf_hw :: proc "contextless" (ctx: ^Context_512, data: []byte) {
panic_contextless(ERR_HW_NOT_SUPPORTED)
}