9292//! [NIST SP 800-38B]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
9393
9494use crate :: aws_lc:: {
95- CMAC_CTX_copy , CMAC_CTX_free , CMAC_CTX_new , CMAC_Final , CMAC_Init ,
95+ CMAC_CTX_copy , CMAC_CTX_new , CMAC_Final , CMAC_Init ,
9696 CMAC_Update , EVP_aes_128_cbc , EVP_aes_192_cbc , EVP_aes_256_cbc , EVP_des_ede3_cbc , CMAC_CTX ,
9797} ;
9898use crate :: error:: Unspecified ;
9999use crate :: fips:: indicator_check;
100+ use crate :: ptr:: LcPtr ;
100101use crate :: { constant_time, rand} ;
101102use core:: mem:: MaybeUninit ;
102103use core:: ptr:: null_mut;
@@ -187,46 +188,6 @@ impl AsRef<[u8]> for Tag {
187188 }
188189}
189190
190- struct LcCmacCtx ( * mut CMAC_CTX ) ;
191-
192- impl LcCmacCtx {
193- fn new ( ) -> Result < Self , Unspecified > {
194- let ptr = unsafe { CMAC_CTX_new ( ) } ;
195- if ptr. is_null ( ) {
196- return Err ( Unspecified ) ;
197- }
198- Ok ( LcCmacCtx ( ptr) )
199- }
200-
201- fn as_mut_ptr ( & mut self ) -> * mut CMAC_CTX {
202- self . 0
203- }
204-
205- fn try_clone ( & self ) -> Result < Self , Unspecified > {
206- let new_ctx = Self :: new ( ) ?;
207- unsafe {
208- if 1 != CMAC_CTX_copy ( new_ctx. 0 , self . 0 ) {
209- return Err ( Unspecified ) ;
210- }
211- }
212- Ok ( new_ctx)
213- }
214- }
215-
216- unsafe impl Send for LcCmacCtx { }
217-
218- impl Drop for LcCmacCtx {
219- fn drop ( & mut self ) {
220- unsafe { CMAC_CTX_free ( self . 0 ) }
221- }
222- }
223-
224- impl Clone for LcCmacCtx {
225- fn clone ( & self ) -> Self {
226- self . try_clone ( ) . expect ( "Unable to clone LcCmacCtx" )
227- }
228- }
229-
230191/// A key to use for CMAC signing.
231192//
232193// # FIPS
@@ -236,7 +197,19 @@ impl Clone for LcCmacCtx {
236197#[ derive( Clone ) ]
237198pub struct Key {
238199 algorithm : Algorithm ,
239- ctx : LcCmacCtx ,
200+ ctx : LcPtr < CMAC_CTX > ,
201+ }
202+
203+ impl Clone for LcPtr < CMAC_CTX > {
204+ fn clone ( & self ) -> Self {
205+ let mut new_ctx = LcPtr :: new ( unsafe { CMAC_CTX_new ( ) } ) . expect ( "CMAC_CTX_new failed" ) ;
206+ unsafe {
207+ if 1 != CMAC_CTX_copy ( * new_ctx. as_mut ( ) , * self . as_const ( ) ) {
208+ panic ! ( "CMAC_CTX_copy failed" ) ;
209+ }
210+ }
211+ new_ctx
212+ }
240213}
241214
242215unsafe impl Send for Key { }
@@ -292,12 +265,12 @@ impl Key {
292265 return Err ( Unspecified ) ;
293266 }
294267
295- let mut ctx = LcCmacCtx :: new ( ) ?;
268+ let mut ctx = LcPtr :: new ( unsafe { CMAC_CTX_new ( ) } ) ?;
296269
297270 unsafe {
298271 let cipher = ( algorithm. cipher_fn ) ( ) ;
299272 if 1 != CMAC_Init (
300- ctx. as_mut_ptr ( ) ,
273+ * ctx. as_mut ( ) ,
301274 key_value. as_ptr ( ) . cast ( ) ,
302275 key_value. len ( ) ,
303276 cipher,
@@ -366,7 +339,7 @@ impl Context {
366339 #[ inline]
367340 fn try_update ( & mut self , data : & [ u8 ] ) -> Result < ( ) , Unspecified > {
368341 unsafe {
369- if 1 != CMAC_Update ( self . key . ctx . as_mut_ptr ( ) , data. as_ptr ( ) , data. len ( ) ) {
342+ if 1 != CMAC_Update ( * self . key . ctx . as_mut ( ) , data. as_ptr ( ) , data. len ( ) ) {
370343 return Err ( Unspecified ) ;
371344 }
372345 }
@@ -402,7 +375,7 @@ impl Context {
402375
403376 unsafe {
404377 if 1 != indicator_check ! ( CMAC_Final (
405- self . key. ctx. as_mut_ptr ( ) ,
378+ * self . key. ctx. as_mut ( ) ,
406379 output. as_mut_ptr( ) ,
407380 out_len. as_mut_ptr( ) ,
408381 ) ) {
0 commit comments