make rng global and get version from LIBWOLFSSL_VERSION_HEX

This commit is contained in:
JacobBarthelmeh
2024-03-06 17:52:31 +07:00
parent 11c9035a62
commit ea9dfecc79
4 changed files with 44 additions and 17 deletions

View File

@ -28,6 +28,7 @@
#endif #endif
#include <wolfssl/wolfcrypt/settings.h> #include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/version.h>
#include <wolfssl/wolfcrypt/port/autosar/Csm.h> #include <wolfssl/wolfcrypt/port/autosar/Csm.h>
#include <wolfssl/wolfcrypt/port/autosar/CryIf.h> #include <wolfssl/wolfcrypt/port/autosar/CryIf.h>
#include <wolfssl/wolfcrypt/port/autosar/Crypto.h> #include <wolfssl/wolfcrypt/port/autosar/Crypto.h>
@ -50,9 +51,9 @@ void CryIf_GetVersionInfo(Std_VersionInfoType* ver)
if (ver != NULL) { if (ver != NULL) {
ver->vendorID = 0; /* no vendor or module ID */ ver->vendorID = 0; /* no vendor or module ID */
ver->moduleID = 0; ver->moduleID = 0;
ver->sw_major_version = 5; ver->sw_major_version = (LIBWOLFSSL_VERSION_HEX >> 24) & 0xFFF;
ver->sw_minor_version = 6; ver->sw_minor_version = (LIBWOLFSSL_VERSION_HEX >> 12) & 0xFFF;
ver->sw_patch_version = 6; ver->sw_patch_version = (LIBWOLFSSL_VERSION_HEX) & 0xFFF;
} }
} }

View File

@ -100,7 +100,7 @@ static int GetKey(Crypto_JobType* job, uint32 eId, uint8 **key, uint32 *keySz)
return -1; return -1;
} }
// @TODO sanity checks on setup... uint8 redirectionConfig; /* @TODO sanity checks on setup... uint8 redirectionConfig; */
switch (eid) { switch (eid) {
case job->jobRedirectionInfoRef->inputKeyElementId: case job->jobRedirectionInfoRef->inputKeyElementId:
if (job->jobRedirectionInfoRef->inputKeyId >= MAX_KEYSTORE) { if (job->jobRedirectionInfoRef->inputKeyId >= MAX_KEYSTORE) {
@ -331,11 +331,13 @@ Std_ReturnType wolfSSL_Crypto(Crypto_JobType* job)
return ret; return ret;
} }
static WC_RNG rng;
static wolfSSL_Mutex rngMutex;
static volatile byte rngInit = 0;
/* returns E_OK on success */ /* returns E_OK on success */
Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job) Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
{ {
WC_RNG rng;
int ret; int ret;
uint8 *out = job->jobPrimitiveInputOutput.outputPtr; uint8 *out = job->jobPrimitiveInputOutput.outputPtr;
@ -346,10 +348,31 @@ Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
return E_NOT_OK; return E_NOT_OK;
} }
ret = wc_InitRng_ex(&rng, NULL, 0); if (rngInit == 1) {
if (ret != 0) { if (wc_LockMutex(&rngMutex) != 0) {
WOLFSSL_MSG("Error initializing RNG"); WOLFSSL_MSG("Error locking RNG mutex");
return E_NOT_OK; return E_NOT_OK;
}
}
if (rngInit == 0) {
if (wc_InitMutex(&rngMutex) != 0) {
WOLFSSL_MSG("Error initializing RNG mutex");
return E_NOT_OK;
}
if (wc_LockMutex(&rngMutex) != 0) {
WOLFSSL_MSG("Error locking RNG mutex");
return E_NOT_OK;
}
ret = wc_InitRng_ex(&rng, NULL, 0);
if (ret != 0) {
WOLFSSL_MSG("Error initializing RNG");
wc_UnLockMutex(&rngMutex);
return E_NOT_OK;
}
rngInit = 1;
} }
ret = wc_RNG_GenerateBlock(&rng, out, *outSz); ret = wc_RNG_GenerateBlock(&rng, out, *outSz);
@ -359,14 +382,16 @@ Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
if (ret != 0) { if (ret != 0) {
WOLFSSL_MSG("Error free'ing RNG"); WOLFSSL_MSG("Error free'ing RNG");
} }
rngInit = 0;
wc_UnLockMutex(&rngMutex);
return E_NOT_OK; return E_NOT_OK;
} }
ret = wc_FreeRng(&rng); if (wc_UnLockMutex(&rngMutex) != 0) {
if (ret != 0) { WOLFSSL_MSG("Error unlocking RNG mutex");
WOLFSSL_MSG("Error free'ing RNG");
return E_NOT_OK; return E_NOT_OK;
} }
return E_OK; return E_OK;
} }

View File

@ -26,6 +26,7 @@
#include <wolfssl/wolfcrypt/settings.h> #include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/logging.h> #include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/version.h>
#include <wolfssl/wolfcrypt/port/autosar/Csm.h> #include <wolfssl/wolfcrypt/port/autosar/Csm.h>
#include <wolfssl/wolfcrypt/port/autosar/CryIf.h> #include <wolfssl/wolfcrypt/port/autosar/CryIf.h>
@ -162,9 +163,9 @@ void Csm_GetVersionInfo(Std_VersionInfoType* version)
if (version != NULL) { if (version != NULL) {
version->vendorID = 0; /* no vendor or module ID */ version->vendorID = 0; /* no vendor or module ID */
version->moduleID = 0; version->moduleID = 0;
version->sw_major_version = 5; version->sw_major_version = (LIBWOLFSSL_VERSION_HEX >> 24) & 0xFFF;
version->sw_minor_version = 6; version->sw_minor_version = (LIBWOLFSSL_VERSION_HEX >> 12) & 0xFFF;
version->sw_patch_version = 6; version->sw_patch_version = (LIBWOLFSSL_VERSION_HEX) & 0xFFF;
} }
} }

View File

@ -121,8 +121,8 @@ static int update_test(void)
0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74, 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20
}; };
const uint8 key[] = "0123456789abcdef "; /* align */ const uint8 key[] = "0123456789abcdef ";
const uint8 iv[] = "1234567890abcdef "; /* align */ const uint8 iv[] = "1234567890abcdef ";
XMEMSET(cipher, 0, BLOCK_SIZE); XMEMSET(cipher, 0, BLOCK_SIZE);
XMEMSET(plain, 0, BLOCK_SIZE); XMEMSET(plain, 0, BLOCK_SIZE);