Skip to content

Commit 8c6de41

Browse files
authored
Merge pull request #7051 from JacobBarthelmeh/mb
fix and enhancement for AES-GCM use with Xilsecure
2 parents 5e8fca4 + 3f10496 commit 8c6de41

8 files changed

Lines changed: 85 additions & 10 deletions

File tree

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9093,6 +9093,7 @@ AM_CONDITIONAL([BUILD_HPKE],[test "x$ENABLED_HPKE" = "xyes" || test "x$ENABLED_U
90939093
AM_CONDITIONAL([BUILD_DTLS],[test "x$ENABLED_DTLS" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
90949094
AM_CONDITIONAL([BUILD_MAXQ10XX],[test "x$ENABLED_MAXQ10XX" = "xyes"])
90959095
AM_CONDITIONAL([BUILD_ARIA],[test "x$ENABLED_ARIA" = "xyes"])
9096+
AM_CONDITIONAL([BUILD_XILINX],[test "x$ENABLED_XILINX" = "xyes"])
90969097

90979098
if test "$ENABLED_REPRODUCIBLE_BUILD" != "yes" &&
90989099
(test "$ax_enable_debug" = "yes" ||

src/include.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,10 +881,14 @@ endif
881881

882882
endif !BUILD_CRYPTONLY
883883

884+
if BUILD_XILINX
885+
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/xilinx/xil-aesgcm.c
886+
endif
884887

885888
endif !BUILD_FIPS_RAND
886889

887890
if BUILD_ARIA
888891
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/aria/aria-crypt.c
889892
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/aria/aria-cryptocb.c
890893
endif
894+

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

Lines changed: 28 additions & 5 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 */
@@ -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

wolfcrypt/src/random.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,26 @@ static WC_INLINE word64 Entropy_TimeHiRes(void)
809809
);
810810
return cnt;
811811
}
812+
#elif !defined(ENTROPY_MEMUSE_THREAD) && defined(__MICROBLAZE__)
813+
814+
#define LPD_SCNTR_BASE_ADDRESS 0xFF250000
815+
816+
/* Get the high resolution time counter.
817+
* Collect ticks from LPD_SCNTR
818+
* @return 64-bit tick count.
819+
*/
820+
static WC_INLINE word64 Entropy_TimeHiRes(void)
821+
{
822+
word64 cnt;
823+
word32 *ptr;
824+
825+
ptr = (word32*)LPD_SCNTR_BASE_ADDRESS;
826+
cnt = *(ptr+1);
827+
cnt = cnt << 32;
828+
cnt |= *ptr;
829+
830+
return cnt;
831+
}
812832
#elif !defined(ENTROPY_MEMUSE_THREAD) && (_POSIX_C_SOURCE >= 199309L)
813833
/* Get the high resolution time counter.
814834
*
@@ -3515,6 +3535,26 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
35153535
* extern int myRngFunc(byte* output, word32 sz);
35163536
*/
35173537

3538+
#elif defined(__MICROBLAZE__)
3539+
#warning weak source of entropy
3540+
#define LPD_SCNTR_BASE_ADDRESS 0xFF250000
3541+
3542+
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3543+
{
3544+
word32* cnt;
3545+
word32 i;
3546+
3547+
/* using current time with srand */
3548+
cnt = (word32*)LPD_SCNTR_BASE_ADDRESS;
3549+
srand(*cnt | *(cnt+1));
3550+
3551+
for (i = 0; i < sz; i++)
3552+
output[i] = rand();
3553+
3554+
(void)os;
3555+
return 0;
3556+
}
3557+
35183558
#elif defined(WOLFSSL_ZEPHYR)
35193559

35203560
#include <version.h>

wolfcrypt/src/sp_int.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7031,7 +7031,7 @@ int sp_mod_d(const sp_int* a, sp_int_digit d, sp_int_digit* r)
70317031

70327032
#if defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA) || \
70337033
(!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
7034-
!defined(WOLFSSL_RSA_PUBLIC_ONLY))
7034+
!defined(WOLFSSL_RSA_PUBLIC_ONLY)) || defined(WOLFSSL_SP_INVMOD)
70357035
/* Divides a by 2 and stores in r: r = a >> 1
70367036
*
70377037
* @param [in] a SP integer to divide.
@@ -19254,7 +19254,7 @@ int sp_prime_is_prime_ex(const sp_int* a, int trials, int* result, WC_RNG* rng)
1925419254
}
1925519255
#endif /* WOLFSSL_SP_PRIME_GEN */
1925619256

19257-
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
19257+
#if !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN)
1925819258

1925919259
/* Calculates the Greatest Common Denominator (GCD) of a and b into r.
1926019260
*

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

wolfssl/wolfcrypt/sp_int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ MP_API int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap);
10671067
MP_API int sp_prime_is_prime(const sp_int* a, int t, int* result);
10681068
MP_API int sp_prime_is_prime_ex(const sp_int* a, int t, int* result,
10691069
WC_RNG* rng);
1070-
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
1070+
#if !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN)
10711071
MP_API int sp_gcd(const sp_int* a, const sp_int* b, sp_int* r);
10721072
#endif
10731073
#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \

0 commit comments

Comments
 (0)