From ccb56de19a386532228baf2c83a7d06493fb8abc Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Mon, 16 Mar 2026 14:58:23 +0900 Subject: [PATCH] core/crypto/sha2: Add scaffolding for SHA512 acceleration --- core/crypto/sha2/sha2.odin | 20 ++++++++++++++++--- ...pl_hw_gen.odin => sha256_impl_hw_gen.odin} | 8 +++----- core/crypto/sha2/sha512_impl_hw_gen.odin | 12 +++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) rename core/crypto/sha2/{sha2_impl_hw_gen.odin => sha256_impl_hw_gen.odin} (68%) create mode 100644 core/crypto/sha2/sha512_impl_hw_gen.odin diff --git a/core/crypto/sha2/sha2.odin b/core/crypto/sha2/sha2.odin index dc41462e4..a878e1c2a 100644 --- a/core/crypto/sha2/sha2.odin +++ b/core/crypto/sha2/sha2.odin @@ -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 diff --git a/core/crypto/sha2/sha2_impl_hw_gen.odin b/core/crypto/sha2/sha256_impl_hw_gen.odin similarity index 68% rename from core/crypto/sha2/sha2_impl_hw_gen.odin rename to core/crypto/sha2/sha256_impl_hw_gen.odin index d735e3c61..ad384caaa 100644 --- a/core/crypto/sha2/sha2_impl_hw_gen.odin +++ b/core/crypto/sha2/sha256_impl_hw_gen.odin @@ -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) } diff --git a/core/crypto/sha2/sha512_impl_hw_gen.odin b/core/crypto/sha2/sha512_impl_hw_gen.odin new file mode 100644 index 000000000..5fd518d80 --- /dev/null +++ b/core/crypto/sha2/sha512_impl_hw_gen.odin @@ -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) +}