Skip to content

Commit ea9dfec

Browse files
make rng global and get version from LIBWOLFSSL_VERSION_HEX
1 parent 11c9035 commit ea9dfec

4 files changed

Lines changed: 44 additions & 17 deletions

File tree

wolfcrypt/src/port/autosar/cryif.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#endif
2929

3030
#include <wolfssl/wolfcrypt/settings.h>
31+
#include <wolfssl/version.h>
3132
#include <wolfssl/wolfcrypt/port/autosar/Csm.h>
3233
#include <wolfssl/wolfcrypt/port/autosar/CryIf.h>
3334
#include <wolfssl/wolfcrypt/port/autosar/Crypto.h>
@@ -50,9 +51,9 @@ void CryIf_GetVersionInfo(Std_VersionInfoType* ver)
5051
if (ver != NULL) {
5152
ver->vendorID = 0; /* no vendor or module ID */
5253
ver->moduleID = 0;
53-
ver->sw_major_version = 5;
54-
ver->sw_minor_version = 6;
55-
ver->sw_patch_version = 6;
54+
ver->sw_major_version = (LIBWOLFSSL_VERSION_HEX >> 24) & 0xFFF;
55+
ver->sw_minor_version = (LIBWOLFSSL_VERSION_HEX >> 12) & 0xFFF;
56+
ver->sw_patch_version = (LIBWOLFSSL_VERSION_HEX) & 0xFFF;
5657
}
5758
}
5859

wolfcrypt/src/port/autosar/crypto.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static int GetKey(Crypto_JobType* job, uint32 eId, uint8 **key, uint32 *keySz)
100100
return -1;
101101
}
102102

103-
// @TODO sanity checks on setup... uint8 redirectionConfig;
103+
/* @TODO sanity checks on setup... uint8 redirectionConfig; */
104104
switch (eid) {
105105
case job->jobRedirectionInfoRef->inputKeyElementId:
106106
if (job->jobRedirectionInfoRef->inputKeyId >= MAX_KEYSTORE) {
@@ -331,11 +331,13 @@ Std_ReturnType wolfSSL_Crypto(Crypto_JobType* job)
331331
return ret;
332332
}
333333

334+
static WC_RNG rng;
335+
static wolfSSL_Mutex rngMutex;
336+
static volatile byte rngInit = 0;
334337

335338
/* returns E_OK on success */
336339
Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
337340
{
338-
WC_RNG rng;
339341
int ret;
340342

341343
uint8 *out = job->jobPrimitiveInputOutput.outputPtr;
@@ -346,10 +348,31 @@ Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
346348
return E_NOT_OK;
347349
}
348350

349-
ret = wc_InitRng_ex(&rng, NULL, 0);
350-
if (ret != 0) {
351-
WOLFSSL_MSG("Error initializing RNG");
352-
return E_NOT_OK;
351+
if (rngInit == 1) {
352+
if (wc_LockMutex(&rngMutex) != 0) {
353+
WOLFSSL_MSG("Error locking RNG mutex");
354+
return E_NOT_OK;
355+
}
356+
}
357+
358+
if (rngInit == 0) {
359+
if (wc_InitMutex(&rngMutex) != 0) {
360+
WOLFSSL_MSG("Error initializing RNG mutex");
361+
return E_NOT_OK;
362+
}
363+
364+
if (wc_LockMutex(&rngMutex) != 0) {
365+
WOLFSSL_MSG("Error locking RNG mutex");
366+
return E_NOT_OK;
367+
}
368+
369+
ret = wc_InitRng_ex(&rng, NULL, 0);
370+
if (ret != 0) {
371+
WOLFSSL_MSG("Error initializing RNG");
372+
wc_UnLockMutex(&rngMutex);
373+
return E_NOT_OK;
374+
}
375+
rngInit = 1;
353376
}
354377

355378
ret = wc_RNG_GenerateBlock(&rng, out, *outSz);
@@ -359,14 +382,16 @@ Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
359382
if (ret != 0) {
360383
WOLFSSL_MSG("Error free'ing RNG");
361384
}
385+
rngInit = 0;
386+
wc_UnLockMutex(&rngMutex);
362387
return E_NOT_OK;
363388
}
364389

365-
ret = wc_FreeRng(&rng);
366-
if (ret != 0) {
367-
WOLFSSL_MSG("Error free'ing RNG");
390+
if (wc_UnLockMutex(&rngMutex) != 0) {
391+
WOLFSSL_MSG("Error unlocking RNG mutex");
368392
return E_NOT_OK;
369393
}
394+
370395
return E_OK;
371396
}
372397

wolfcrypt/src/port/autosar/csm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <wolfssl/wolfcrypt/settings.h>
2828
#include <wolfssl/wolfcrypt/logging.h>
29+
#include <wolfssl/version.h>
2930
#include <wolfssl/wolfcrypt/port/autosar/Csm.h>
3031
#include <wolfssl/wolfcrypt/port/autosar/CryIf.h>
3132

@@ -162,9 +163,9 @@ void Csm_GetVersionInfo(Std_VersionInfoType* version)
162163
if (version != NULL) {
163164
version->vendorID = 0; /* no vendor or module ID */
164165
version->moduleID = 0;
165-
version->sw_major_version = 5;
166-
version->sw_minor_version = 6;
167-
version->sw_patch_version = 6;
166+
version->sw_major_version = (LIBWOLFSSL_VERSION_HEX >> 24) & 0xFFF;
167+
version->sw_minor_version = (LIBWOLFSSL_VERSION_HEX >> 12) & 0xFFF;
168+
version->sw_patch_version = (LIBWOLFSSL_VERSION_HEX) & 0xFFF;
168169
}
169170
}
170171

wolfcrypt/src/port/autosar/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ static int update_test(void)
121121
0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
122122
0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20
123123
};
124-
const uint8 key[] = "0123456789abcdef "; /* align */
125-
const uint8 iv[] = "1234567890abcdef "; /* align */
124+
const uint8 key[] = "0123456789abcdef ";
125+
const uint8 iv[] = "1234567890abcdef ";
126126

127127
XMEMSET(cipher, 0, BLOCK_SIZE);
128128
XMEMSET(plain, 0, BLOCK_SIZE);

0 commit comments

Comments
 (0)