@@ -298,6 +298,17 @@ int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len, const byte* iv,
298298
299299#if defined(HAVE_AESGCM ) || defined(HAVE_AESCCM )
300300
301+ #ifndef NO_RNG
302+ static WC_INLINE void IncCtr (byte * ctr , word32 ctrSz )
303+ {
304+ int i ;
305+ for (i = (int )ctrSz - 1 ; i >= 0 ; i -- ) {
306+ if (++ ctr [i ])
307+ break ;
308+ }
309+ }
310+ #endif
311+
301312static int AesAuthSetKey (Aes * aes , const byte * key , word32 keySz )
302313{
303314 byte nonce [AES_BLOCK_SIZE ];
@@ -517,9 +528,9 @@ static int AesAuthEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
517528 ROM_AESKey1Set (AES_BASE , aes -> key , aes -> keylen - 8 );
518529
519530 ret = ROM_AESDataProcessAuth (AES_BASE ,
520- (unsigned int * )in_a , (unsigned int * )out_a , inSz ,
531+ (unsigned int * )in_a , (unsigned int * )out_a , inSz ,
521532 (unsigned int * )authIn_a , authInSz ,
522- (unsigned int * )tmpTag );
533+ (unsigned int * )tmpTag );
523534 wolfSSL_TI_unlockCCM ();
524535
525536 if (ret == false) {
@@ -619,9 +630,9 @@ static int AesAuthDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
619630 ROM_AESIVSet (AES_BASE , aes -> reg );
620631 ROM_AESKey1Set (AES_BASE , aes -> key , aes -> keylen - 8 );
621632 ret = ROM_AESDataProcessAuth (AES_BASE ,
622- (unsigned int * )in_a , (unsigned int * )out_a , inSz ,
633+ (unsigned int * )in_a , (unsigned int * )out_a , inSz ,
623634 (unsigned int * )authIn_a , authInSz ,
624- (unsigned int * )tmpTag );
635+ (unsigned int * )tmpTag );
625636 wolfSSL_TI_unlockCCM ();
626637
627638 if ((ret == false) || (XMEMCMP (authTag , tmpTag , authTagSz ) != 0 )) {
@@ -685,14 +696,6 @@ int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
685696}
686697
687698#ifndef NO_RNG
688- static WC_INLINE void IncCtr (byte * ctr , word32 ctrSz )
689- {
690- int i ;
691- for (i = (int )ctrSz - 1 ; i >= 0 ; i -- ) {
692- if (++ ctr [i ])
693- break ;
694- }
695- }
696699static WARN_UNUSED_RESULT WC_INLINE int CheckAesGcmIvSize (int ivSz ) {
697700 return (ivSz == GCM_NONCE_MIN_SZ ||
698701 ivSz == GCM_NONCE_MID_SZ ||
@@ -890,6 +893,73 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
890893 return AesAuthDecrypt (aes , out , in , inSz , nonce , nonceSz , authTag , authTagSz ,
891894 authIn , authInSz , AES_CFG_MODE_CCM );
892895}
896+
897+ /* abstract functions that call lower level AESCCM functions */
898+ #ifndef WC_NO_RNG
899+
900+ int wc_AesCcmSetNonce (Aes * aes , const byte * nonce , word32 nonceSz )
901+ {
902+ int ret = 0 ;
903+
904+ if (aes == NULL || nonce == NULL ||
905+ nonceSz < CCM_NONCE_MIN_SZ || nonceSz > CCM_NONCE_MAX_SZ ) {
906+
907+ ret = BAD_FUNC_ARG ;
908+ }
909+
910+ if (ret == 0 ) {
911+ XMEMCPY (aes -> reg , nonce , nonceSz );
912+ aes -> nonceSz = nonceSz ;
913+
914+ /* Invocation counter should be 2^61 */
915+ aes -> invokeCtr [0 ] = 0 ;
916+ aes -> invokeCtr [1 ] = 0xE0000000 ;
917+ }
918+
919+ return ret ;
920+ }
921+
922+
923+ int wc_AesCcmEncrypt_ex (Aes * aes , byte * out , const byte * in , word32 sz ,
924+ byte * ivOut , word32 ivOutSz ,
925+ byte * authTag , word32 authTagSz ,
926+ const byte * authIn , word32 authInSz )
927+ {
928+ int ret = 0 ;
929+
930+ if (aes == NULL || out == NULL ||
931+ (in == NULL && sz != 0 ) ||
932+ ivOut == NULL ||
933+ (authIn == NULL && authInSz != 0 ) ||
934+ (ivOutSz != aes -> nonceSz )) {
935+
936+ ret = BAD_FUNC_ARG ;
937+ }
938+
939+ if (ret == 0 ) {
940+ aes -> invokeCtr [0 ]++ ;
941+ if (aes -> invokeCtr [0 ] == 0 ) {
942+ aes -> invokeCtr [1 ]++ ;
943+ if (aes -> invokeCtr [1 ] == 0 )
944+ ret = AES_CCM_OVERFLOW_E ;
945+ }
946+ }
947+
948+ if (ret == 0 ) {
949+ ret = wc_AesCcmEncrypt (aes , out , in , sz ,
950+ (byte * )aes -> reg , aes -> nonceSz ,
951+ authTag , authTagSz ,
952+ authIn , authInSz );
953+ if (ret == 0 ) {
954+ XMEMCPY (ivOut , aes -> reg , aes -> nonceSz );
955+ IncCtr ((byte * )aes -> reg , aes -> nonceSz );
956+ }
957+ }
958+
959+ return ret ;
960+ }
961+ #endif /* !WC_NO_RNG */
962+
893963#endif /* HAVE_AESCCM */
894964
895965int wc_AesInit (Aes * aes , void * heap , int devId )
0 commit comments