1- use core:: ptr:: { read_volatile, write_volatile} ;
1+ use core:: ptr:: { NonNull , read_volatile, write_volatile} ;
22use ast1060_pac:: Secure ;
33use proposed_traits:: ecdsa:: { EcdsaVerify , Curve , SignatureForCurve , PubKeyForCurve , ErrorType as EcdsaErrorType , Error , ErrorKind } ;
44use proposed_traits:: common:: { FromBytes , ToBytes , Endian , ErrorKind as CommonErrorKind , ErrorType as CommonErrorType , SerdeError as CommonSerdeError } ;
@@ -221,41 +221,39 @@ pub struct AspeedEcdsa<'a, D: DelayNs> {
221221 sram_base : NonNull < u32 > ,
222222 delay : D ,
223223}
224- secure: & ' a Secure ,
225- ecdsa_base: * mut u32 ,
226- sram_base: * mut u32 ,
227- delay: D ,
228- }
229224
230225impl < D : DelayNs > EcdsaErrorType for AspeedEcdsa < ' _ , D > {
231226 type Error = AspeedEcdsaError ;
232227}
233228
234229impl < ' a , D : DelayNs > AspeedEcdsa < ' a , D > {
235230 pub fn new ( secure : & ' a Secure , delay : D ) -> Self {
236- let ecdsa_base = ECDSA_BASE as * mut u32 ; // SBC base address
237- let sram_base = ECDSA_SRAM_BASE as * mut u32 ; // SRAM base address for ECDSA
231+ let ecdsa_base = unsafe { NonNull :: new_unchecked ( ECDSA_BASE as * mut u32 ) } ;
232+ let sram_base = unsafe { NonNull :: new_unchecked ( ECDSA_SRAM_BASE as * mut u32 ) } ;
233+
234+ // let ecdsa_base = ECDSA_BASE as *mut u32; // SBC base address
235+ // let sram_base = ECDSA_SRAM_BASE as *mut u32; // SRAM base address for ECDSA
238236 Self { secure, ecdsa_base, sram_base, delay }
239237 }
240238
241239 #[ inline( always) ]
242240 fn sec_rd ( & self , offset : usize ) -> u32 {
243241 unsafe {
244- read_volatile ( self . ecdsa_base . add ( offset / 4 ) )
242+ read_volatile ( self . ecdsa_base . as_ptr ( ) . add ( offset / 4 ) )
245243 }
246244 }
247245
248246 #[ inline( always) ]
249247 fn sec_wr ( & self , offset : usize , val : u32 ) {
250248 unsafe {
251- write_volatile ( self . ecdsa_base . add ( offset / 4 ) , val) ;
249+ write_volatile ( self . ecdsa_base . as_ptr ( ) . add ( offset / 4 ) , val) ;
252250 }
253251 }
254252
255253 #[ inline( always) ]
256254 fn sram_wr_u32 ( & self , offset : usize , val : u32 ) {
257255 unsafe {
258- write_volatile ( self . sram_base . add ( offset / 4 ) , val) ;
256+ write_volatile ( self . sram_base . as_ptr ( ) . add ( offset / 4 ) , val) ;
259257 }
260258 }
261259
@@ -264,7 +262,7 @@ impl<'a, D: DelayNs> AspeedEcdsa<'a, D> {
264262 for i in ( 0 ..Scalar48 :: LEN ) . step_by ( 4 ) {
265263 let val = u32:: from_le_bytes ( [ data[ i] , data[ i + 1 ] , data[ i + 2 ] , data[ i + 3 ] ] ) ;
266264 unsafe {
267- write_volatile ( self . sram_base . add ( ( offset + i) / 4 ) , val) ;
265+ write_volatile ( self . sram_base . as_ptr ( ) . add ( ( offset + i) / 4 ) , val) ;
268266 }
269267 }
270268 }
@@ -360,75 +358,3 @@ where
360358 }
361359 }
362360}
363-
364- where
365- C : Curve <Scalar = Scalar48 >,
366- C :: DigestType : DigestAlgorithm ,
367- <C :: DigestType as DigestAlgorithm >:: DigestOutput : AsRef <[ u8] >,
368- D : DelayNs ,
369- {
370- type PublicKey = PublicKey ;
371- type Signature = Signature ;
372-
373- fn verify (
374- & mut self ,
375- public_key : & Self :: PublicKey ,
376- digest : <C :: DigestType as DigestAlgorithm >:: DigestOutput ,
377- signature : & Self :: Signature ,
378- ) -> Result < ( ) , Self :: Error > {
379- const LEN : usize = Scalar48 :: LEN ;
380- unsafe {
381- let digest_bytes = digest. as_ref ( ) ;
382- if digest_bytes. len ( ) != LEN {
383- return Err ( AspeedEcdsaError :: BadInput ) ;
384- }
385-
386- let digest_array: & [ u8 ; LEN ] = digest_bytes. try_into ( ) . map_err ( |_| AspeedEcdsaError :: BadInput ) ?;
387-
388- self . sec_wr ( 0x7c , 0x0100f00b ) ;
389-
390- // Reset Engine
391- self . secure . secure0b4 ( ) . write ( |w| w. bits ( 0 ) ) ;
392- self . secure . secure0b4 ( ) . write ( |w| w. sec_boot_ecceng_enbl ( ) . set_bit ( ) ) ;
393- self . delay . delay_ns ( 5000 ) ;
394-
395- self . load_secp384r1_params ( ) ;
396-
397- self . sec_wr ( 0x7c , 0x0300f00b ) ;
398-
399- // Write qx, qy, r, s
400- self . sram_wr ( SRAM_DST_QX , & public_key. qx . 0 ) ;
401- self . sram_wr ( SRAM_DST_QY , & public_key. qy . 0 ) ;
402- self . sram_wr ( SRAM_DST_R , & signature. r . 0 ) ;
403- self . sram_wr ( SRAM_DST_S , & signature. s . 0 ) ;
404- self . sram_wr ( SRAM_DST_M , digest_array) ;
405-
406- self . sec_wr ( 0x7c , 0 ) ;
407-
408- // Write ECDSA instruction command
409- self . sram_wr_u32 ( 0x23c0 , 1 ) ;
410-
411- // Trigger ECDSA Engine
412- self . secure . secure0bc ( ) . write ( |w| w. sec_boot_ecceng_trigger_reg ( ) . set_bit ( ) ) ;
413- self . delay . delay_ns ( 5000 ) ;
414- self . secure . secure0bc ( ) . write ( |w| w. sec_boot_ecceng_trigger_reg ( ) . clear_bit ( ) ) ;
415-
416- // Poll
417- let mut retry = 1000 ;
418- while retry > 0 {
419- let status = self . secure . secure014 ( ) . read ( ) . bits ( ) ;
420- if status & ( 1 << 20 ) != 0 {
421- return if status & ( 1 << 21 ) != 0 {
422- Ok ( ( ) )
423- } else {
424- Err ( AspeedEcdsaError :: InvalidSignature )
425- } ;
426- }
427- retry -= 1 ;
428- self . delay . delay_ns ( 5000 ) ;
429- }
430-
431- Err ( AspeedEcdsaError :: Busy )
432- }
433- }
434- }
0 commit comments