@@ -135,7 +135,9 @@ int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len, word32 kup)
135135 aes -> xKeySize =
136136 len == AES_128_KEY_SIZE ? XSECURE_AES_KEY_SIZE_128 :
137137 XSECURE_AES_KEY_SIZE_256 ;
138- XMEMCPY (aes -> keyInit , key , len );
138+ if (key != NULL ) {
139+ XMEMCPY (aes -> keyInit , key , len );
140+ }
139141
140142 return 0 ;
141143}
@@ -478,7 +480,12 @@ int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len, word32 kup)
478480{
479481 XCsuDma_Config * con ;
480482
481- if (aes == NULL || key == NULL ) {
483+ if (aes == NULL ) {
484+ return BAD_FUNC_ARG ;
485+ }
486+
487+ if (kup == XSECURE_CSU_AES_KEY_SRC_KUP && key == NULL ) {
488+ WOLFSSL_MSG ("Expecting key buffer passed in if using KUP" );
482489 return BAD_FUNC_ARG ;
483490 }
484491
@@ -501,7 +508,9 @@ int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len, word32 kup)
501508
502509 aes -> keylen = len ;
503510 aes -> kup = kup ;
504- XMEMCPY ((byte * )(aes -> keyInit ), key , len );
511+ if (key != NULL ) {
512+ XMEMCPY ((byte * )(aes -> keyInit ), key , len );
513+ }
505514
506515 return 0 ;
507516}
@@ -538,18 +547,26 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out,
538547 return BAD_FUNC_ARG ;
539548 }
540549
550+ #ifndef NO_WOLFSSL_XILINX_TAG_MALLOC
541551 tmp = (byte * )XMALLOC (sz + AES_GCM_AUTH_SZ , aes -> heap ,
542552 DYNAMIC_TYPE_TMP_BUFFER );
543553 if (tmp == NULL ) {
544554 return MEMORY_E ;
545555 }
556+ #else
557+ /* if NO_WOLFSSL_XILINX_TAG_MALLOC is defined than it is assumed that
558+ * out buffer is large enough to hold both the cipher out and tag */
559+ tmp = out ;
560+ #endif
546561
547562 XSecure_AesInitialize (& (aes -> xilAes ), & (aes -> dma ), aes -> kup , (word32 * )iv ,
548563 aes -> keyInit );
549564 XSecure_AesEncryptData (& (aes -> xilAes ), tmp , in , sz );
550- XMEMCPY (out , tmp , sz );
551565 XMEMCPY (authTag , tmp + sz , authTagSz );
566+ #ifndef NO_WOLFSSL_XILINX_TAG_MALLOC
567+ XMEMCPY (out , tmp , sz );
552568 XFREE (tmp , aes -> heap , DYNAMIC_TYPE_TMP_BUFFER );
569+ #endif
553570 }
554571
555572 /* handle completing tag with any additional data */
@@ -610,7 +627,7 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out,
610627 /* calls to hardened crypto */
611628 XSecure_AesInitialize (& (aes -> xilAes ), & (aes -> dma ), aes -> kup ,
612629 (word32 * )iv , aes -> keyInit );
613- XSecure_AesDecryptData (& (aes -> xilAes ), out , in , sz , tag );
630+ ret = XSecure_AesDecryptData (& (aes -> xilAes ), out , in , sz , tag );
614631
615632 /* account for additional data */
616633 if (authIn != NULL && authInSz > 0 ) {
@@ -623,6 +640,12 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out,
623640 return AES_GCM_AUTH_E ;
624641 }
625642 }
643+ else {
644+ /* if no aad then check the result of the initial tag passed in */
645+ if (ret != XST_SUCCESS ) {
646+ return AES_GCM_AUTH_E ;
647+ }
648+ }
626649
627650 return 0 ;
628651
0 commit comments