@@ -17,6 +17,7 @@ use openprot_hal_blocking::mac::{
1717 Error as MacError , ErrorKind as MacErrorKind , ErrorType as MacErrorType , KeyHandle ,
1818} ;
1919use openprot_hal_blocking:: mac:: { HmacSha2_256 , HmacSha2_384 , HmacSha2_512 } ;
20+ use openprot_platform_traits_hubris:: { HubrisCryptoError , HubrisDigestDevice } ;
2021use sha2:: { Digest as Sha2Digest , Sha256 , Sha384 , Sha512 } ;
2122
2223/// A type implementing RustCrypto-based hash/digest owned traits.
@@ -114,7 +115,9 @@ impl SecureOwnedKey {
114115 }
115116
116117 let mut data = [ 0u8 ; 128 ] ;
117- data[ ..bytes. len ( ) ] . copy_from_slice ( bytes) ;
118+ data. get_mut ( ..bytes. len ( ) )
119+ . ok_or ( CryptoError :: InvalidKeyLength ) ?
120+ . copy_from_slice ( bytes) ;
118121
119122 Ok ( Self {
120123 data,
@@ -129,14 +132,16 @@ impl SecureOwnedKey {
129132 }
130133
131134 let mut data = [ 0u8 ; 128 ] ;
132- data[ ..N ] . copy_from_slice ( & array) ;
135+ data. get_mut ( ..N )
136+ . ok_or ( CryptoError :: InvalidKeyLength ) ?
137+ . copy_from_slice ( & array) ;
133138
134139 Ok ( Self { data, len : N } )
135140 }
136141
137142 /// Get the key bytes as a slice (only the valid portion)
138143 pub fn as_bytes ( & self ) -> & [ u8 ] {
139- & self . data [ .. self . len ]
144+ self . data . get ( .. self . len ) . unwrap_or ( & [ ] )
140145 }
141146
142147 /// Get the key length
@@ -621,3 +626,55 @@ mod tests {
621626 assert_eq ! ( result. unwrap_err( ) , CryptoError :: InvalidKeyLength ) ;
622627 }
623628}
629+
630+ /// Hardware capabilities for RustCrypto software implementation
631+ // TODO: Uncomment when DigestHardwareCapabilities is available
632+ // impl DigestHardwareCapabilities for RustCryptoController {
633+ // const MAX_CONCURRENT_SESSIONS: usize = 1; // Single-session software implementation
634+ // const HAS_HARDWARE_ACCELERATION: bool = false; // Software-only
635+ // const PLATFORM_NAME: &'static str = "RustCrypto Software";
636+ // }
637+ /// Hubris platform integration for RustCrypto controller
638+ impl HubrisDigestDevice for RustCryptoController {
639+ type DigestContext256 = DigestContext256 ;
640+ type DigestContext384 = DigestContext384 ;
641+ type DigestContext512 = DigestContext512 ;
642+
643+ type HmacKey = SecureOwnedKey ;
644+ type HmacContext256 = MacContext256 ;
645+ type HmacContext384 = MacContext384 ;
646+ type HmacContext512 = MacContext512 ;
647+
648+ fn init_digest_sha256 ( self ) -> Result < Self :: DigestContext256 , HubrisCryptoError > {
649+ DigestInit :: init ( self , Sha2_256 ) . map_err ( |_| HubrisCryptoError :: HardwareFailure )
650+ }
651+
652+ fn init_digest_sha384 ( self ) -> Result < Self :: DigestContext384 , HubrisCryptoError > {
653+ DigestInit :: init ( self , Sha2_384 ) . map_err ( |_| HubrisCryptoError :: HardwareFailure )
654+ }
655+
656+ fn init_digest_sha512 ( self ) -> Result < Self :: DigestContext512 , HubrisCryptoError > {
657+ DigestInit :: init ( self , Sha2_512 ) . map_err ( |_| HubrisCryptoError :: HardwareFailure )
658+ }
659+
660+ fn init_hmac_sha256 (
661+ self ,
662+ key : Self :: HmacKey ,
663+ ) -> Result < Self :: HmacContext256 , HubrisCryptoError > {
664+ MacInit :: init ( self , HmacSha2_256 , key) . map_err ( |_| HubrisCryptoError :: HardwareFailure )
665+ }
666+
667+ fn init_hmac_sha384 (
668+ self ,
669+ key : Self :: HmacKey ,
670+ ) -> Result < Self :: HmacContext384 , HubrisCryptoError > {
671+ MacInit :: init ( self , HmacSha2_384 , key) . map_err ( |_| HubrisCryptoError :: HardwareFailure )
672+ }
673+
674+ fn init_hmac_sha512 (
675+ self ,
676+ key : Self :: HmacKey ,
677+ ) -> Result < Self :: HmacContext512 , HubrisCryptoError > {
678+ MacInit :: init ( self , HmacSha2_512 , key) . map_err ( |_| HubrisCryptoError :: HardwareFailure )
679+ }
680+ }
0 commit comments