1- use core:: ptr:: { read_volatile, write_volatile, write_bytes} ;
1+ use core:: ptr:: { NonNull , read_volatile, write_volatile, write_bytes} ;
22use ast1060_pac:: Secure ;
33use proposed_traits:: common:: { FromBytes , ToBytes , Endian , ErrorKind as CommonErrorKind , ErrorType as CommonErrorType , SerdeError as CommonSerdeError } ;
4- use proposed_traits:: rsa:: { RsaKeys , RsaSignature , RsaKeyGen , RsaSign , RsaVerify , RsaSize , PaddingMode , Error , ErrorKind , ErrorType as RsaErrorType } ;
4+ use proposed_traits:: rsa:: { RsaKeys , RsaSignature , RsaMessage , RsaKeyGen , RsaSign , RsaVerify , RsaSize , PaddingMode , Error , ErrorKind , ErrorType as RsaErrorType } ;
55use embedded_hal:: delay:: DelayNs ;
66
77const RSA_SRAM_BASE : usize = 0x7900_0000 ; // SBC base address
@@ -75,9 +75,6 @@ impl CommonErrorType for RsaDigest {
7575 type Error = SignatureSerdeError ;
7676}
7777
78- pub trait SerializeDeserialize : ToBytes + FromBytes { }
79- impl < T : ToBytes + FromBytes > SerializeDeserialize for T { }
80-
8178impl ToBytes for RsaDigest {
8279 fn to_bytes ( & self , dest : & mut [ u8 ] , _endian : Endian ) -> Result < ( ) , Self :: Error > {
8380 if dest. len ( ) < self . len {
@@ -110,17 +107,49 @@ impl FromBytes for RsaDigest {
110107 }
111108}
112109
110+ impl ToBytes for RsaSignatureData {
111+ fn to_bytes ( & self , dest : & mut [ u8 ] , _endian : Endian ) -> Result < ( ) , Self :: Error > {
112+ if dest. len ( ) < self . len {
113+ return Err ( SignatureSerdeError :: BufferTooSmall ) ;
114+ }
115+ for i in 0 ..self . len {
116+ dest[ i] = self . data [ self . len - i - 1 ] ;
117+ }
118+ Ok ( ( ) )
119+ }
120+ }
121+
122+ impl FromBytes for RsaSignatureData {
123+ fn from_bytes ( bytes : & [ u8 ] , _endian : Endian ) -> Result < Self , Self :: Error > {
124+ if bytes. len ( ) > 512 {
125+ return Err ( SignatureSerdeError :: BufferTooSmall ) ;
126+ }
127+ let mut data = [ 0u8 ; 512 ] ;
128+ for ( i, b) in bytes. iter ( ) . rev ( ) . enumerate ( ) {
129+ data[ i] = * b;
130+ }
131+ Ok ( Self {
132+ data,
133+ len : bytes. len ( ) ,
134+ } )
135+ }
136+ }
137+
138+ impl CommonErrorType for RsaSignatureData {
139+ type Error = SignatureSerdeError ;
140+ }
141+
113142pub struct AspeedRsa < ' a , D : DelayNs > {
114143 pub secure : & ' a Secure ,
115- sram_base : * mut u8 ,
144+ sram_base : NonNull < u8 > ,
116145 delay : D ,
117146}
118147
119148impl < ' a , D : DelayNs > AspeedRsa < ' a , D > {
120149 pub fn new ( secure : & ' a Secure , delay : D ) -> Self {
121150 Self {
122151 secure,
123- sram_base : RSA_SRAM_BASE as * mut u8 ,
152+ sram_base : unsafe { NonNull :: new_unchecked ( RSA_SRAM_BASE as * mut u8 ) } ,
124153 delay,
125154 }
126155 }
@@ -196,20 +225,20 @@ impl <'a, D: DelayNs> AspeedRsa<'a, D> {
196225
197226 unsafe {
198227 for i in 0 ..SRAM_SIZE {
199- write_volatile ( self . sram_base . add ( i) , 0 ) ;
228+ write_volatile ( self . sram_base . as_ptr ( ) . add ( i) , 0 ) ;
200229 }
201230
202231 for ( i, byte) in e_or_d. iter ( ) . rev ( ) . enumerate ( ) {
203232 // print byte
204- write_volatile ( self . sram_base . add ( SRAM_DST_EXPONENT + i) , * byte) ;
233+ write_volatile ( self . sram_base . as_ptr ( ) . add ( SRAM_DST_EXPONENT + i) , * byte) ;
205234 }
206235
207236 for ( i, byte) in m. iter ( ) . rev ( ) . enumerate ( ) {
208- write_volatile ( self . sram_base . add ( SRAM_DST_MODULUS + i) , * byte) ;
237+ write_volatile ( self . sram_base . as_ptr ( ) . add ( SRAM_DST_MODULUS + i) , * byte) ;
209238 }
210239
211240 for ( i, byte) in input. iter ( ) . rev ( ) . enumerate ( ) {
212- write_volatile ( self . sram_base . add ( SRAM_DST_DATA + i) , * byte) ;
241+ write_volatile ( self . sram_base . as_ptr ( ) . add ( SRAM_DST_DATA + i) , * byte) ;
213242 }
214243
215244 let key_len = ( ed_bits << 16 ) | m_bits;
@@ -234,7 +263,7 @@ impl <'a, D: DelayNs> AspeedRsa<'a, D> {
234263 let mut i = 0 ;
235264
236265 for j in ( 0 ..RSA_MAX_LEN ) . rev ( ) {
237- let byte = read_volatile ( self . sram_base . add ( SRAM_DST_RESULT + j) ) ;
266+ let byte = read_volatile ( self . sram_base . as_ptr ( ) . add ( SRAM_DST_RESULT + j) ) ;
238267 if byte == 0 && leading_zero {
239268 continue ;
240269 }
@@ -246,7 +275,7 @@ impl <'a, D: DelayNs> AspeedRsa<'a, D> {
246275 out_len += 1 ;
247276 }
248277
249- write_bytes ( self . sram_base , 0 , SRAM_SIZE ) ;
278+ write_bytes ( self . sram_base . as_ptr ( ) , 0 , SRAM_SIZE ) ;
250279
251280 Ok ( out_len)
252281 }
@@ -257,6 +286,10 @@ impl <D: DelayNs> RsaErrorType for AspeedRsa<'_, D> {
257286 type Error = RsaDriverError ;
258287}
259288
289+ impl < D : DelayNs > RsaMessage for AspeedRsa < ' _ , D > {
290+ type Message = RsaDigest ;
291+ }
292+
260293impl < ' a , D : DelayNs > RsaKeys for AspeedRsa < ' a , D > {
261294 type PrivateKey = RsaPrivateKey < ' a > ;
262295 type PublicKey = RsaPublicKey < ' a > ;
@@ -274,7 +307,6 @@ impl <D: DelayNs> RsaKeyGen for AspeedRsa<'_, D> {
274307}
275308
276309impl < D : DelayNs > RsaSign for AspeedRsa < ' _ , D > {
277- type Message = RsaDigest ;
278310 /// Performs RSA signature generation using PKCS#1 v1.5 padding and a private key.
279311 ///
280312 /// This function pads the input message digest using PKCS#1 v1.5, triggers the RSA engine
@@ -345,8 +377,6 @@ impl <D: DelayNs> RsaSign for AspeedRsa<'_, D> {
345377}
346378
347379impl < D : DelayNs > RsaVerify for AspeedRsa < ' _ , D > {
348- type Message = RsaDigest ;
349-
350380 /// Verifies an RSA signature using the provided public key and digest.
351381 ///
352382 /// This function performs RSA public-key decryption (i.e., modular exponentiation)
0 commit comments