Skip to content

Commit f8dbc7f

Browse files
use of device key with AES-GCM and add way to avoid malloc for tag
1 parent 567243d commit f8dbc7f

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

wolfcrypt/src/port/xilinx/xil-aesgcm.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

wolfssl/wolfcrypt/aes.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ WOLFSSL_LOCAL void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,
8585
#ifdef WOLFSSL_XILINX_CRYPT_VERSAL
8686
#include <wolfssl/wolfcrypt/port/xilinx/xil-versal-glue.h>
8787
#include <xsecure_aesclient.h>
88-
#define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_AES_USER_KEY_0
88+
#if !defined(WOLFSSL_XILINX_AES_KEY_SRC)
89+
#define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_AES_USER_KEY_0
90+
#endif
8991
#else /* versal */
9092
#include <xsecure_aes.h>
91-
#define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_CSU_AES_KEY_SRC_KUP
93+
#if !defined(WOLFSSL_XILINX_AES_KEY_SRC)
94+
#define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_CSU_AES_KEY_SRC_KUP
95+
#endif
9296
#endif /* !versal */
9397
#endif /* WOLFSSL_XILINX_CRYPT */
9498

wolfssl/wolfcrypt/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,10 @@ extern void uITRON4_free(void *p) ;
18441844
#if !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
18451845
#define NO_DEV_RANDOM
18461846
#endif
1847+
#undef NO_WOLFSSL_DIR
18471848
#define NO_WOLFSSL_DIR
1849+
1850+
#undef HAVE_AESGCM
18481851
#define HAVE_AESGCM
18491852
#endif
18501853

0 commit comments

Comments
 (0)