F-1723 F-1724 F-1725 F-2908 F-2910 F-2911 F-3461 F-3683 F-3684 F-3891 F-4605 F-4606 F-4607: fix CAAM, Renesas, SGX, and UEFI examples issues

pull/594/head
Emma Stensland 2026-07-06 15:00:46 -06:00 committed by Paul Adelsbach
parent 9ac1bcbc4e
commit 1cedd247eb
9 changed files with 48 additions and 32 deletions

View File

@ -91,7 +91,7 @@ size_t _REL_sizeof_sysheap = SIZEOF_HEAP;
void main(void) void main(void)
{ {
byte ret; int ret;
R_SYSTEM_ClockInit(); R_SYSTEM_ClockInit();
R_SYSTEM_TimerInit(); R_SYSTEM_TimerInit();

View File

@ -110,7 +110,8 @@ static int sign_with_rsa_key(RsaKey* pRsaKey, WC_RNG* rng, const char* msg)
int ret; int ret;
/* Hash message to b signed */ /* Hash message to b signed */
if (hash_msg(msg, hash) != 0 ) { ret = hash_msg(msg, hash);
if (ret != 0 ) {
goto sign_end; goto sign_end;
} }
@ -119,7 +120,7 @@ static int sign_with_rsa_key(RsaKey* pRsaKey, WC_RNG* rng, const char* msg)
ret = wc_RsaPSS_Sign(hash, sizeof(hash), pSignature, sizeof(pSignature), ret = wc_RsaPSS_Sign(hash, sizeof(hash), pSignature, sizeof(pSignature),
WC_HASH_TYPE_SHA256, WC_MGF1SHA256, pRsaKey, rng); WC_HASH_TYPE_SHA256, WC_MGF1SHA256, pRsaKey, rng);
if (ret <= 0) { if (ret <= 0) {
printf(" RSA private encryption failed with error %d\n"); printf(" RSA private encryption failed with error %d\n", ret);
goto sign_end; goto sign_end;
} }
@ -135,7 +136,8 @@ static int verify_with_rsa_public_key(RsaKey* pRsaKey, const char* msg)
byte pDecrypted[RSA_KEY_SIZE/8]; byte pDecrypted[RSA_KEY_SIZE/8];
byte* pt; byte* pt;
/* Hash message to be signed. */ /* Hash message to be signed. */
if (hash_msg(msg, hash) != 0) { ret = hash_msg(msg, hash);
if (ret != 0) {
goto verify_end; goto verify_end;
} }

View File

@ -336,7 +336,7 @@ int enc_wolfSSL_Cleanup(void)
/* free up all WOLFSSL_CTX's */ /* free up all WOLFSSL_CTX's */
for (id = 0; id < MAX_WOLFSSL_CTX; id++) for (id = 0; id < MAX_WOLFSSL_CTX; id++)
RemoveCTX(id); RemoveCTX(id);
wolfSSL_Cleanup(); return wolfSSL_Cleanup();
} }
void printf(const char *fmt, ...) void printf(const char *fmt, ...)
@ -349,16 +349,6 @@ void printf(const char *fmt, ...)
ocall_print_string(buf); ocall_print_string(buf);
} }
int sprintf(char* buf, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vsnprintf(buf, BUFSIZ, fmt, ap);
va_end(ap);
return ret;
}
double current_time(void) double current_time(void)
{ {
double curr; double curr;

View File

@ -27,7 +27,6 @@ extern "C" {
#endif #endif
void printf(const char *fmt, ...); void printf(const char *fmt, ...);
int sprintf(char* buf, const char *fmt, ...);
double current_time(void); double current_time(void);
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -93,7 +93,11 @@ static int createSignature(ecc_key* key, byte* sigOut, word32* sigOutSz,
int ret; int ret;
WC_RNG rng; WC_RNG rng;
wc_InitRng(&rng); ret = wc_InitRng(&rng);
if (ret != 0) {
printf("wc_InitRng failed with error %d\n", ret);
return ret;
}
ret = wc_ecc_sign_hash(msg, msgSz, sigOut, sigOutSz, &rng, key); ret = wc_ecc_sign_hash(msg, msgSz, sigOut, sigOutSz, &rng, key);
if (ret != 0) { if (ret != 0) {
printf("sign hash failed with error %d\n", ret); printf("sign hash failed with error %d\n", ret);
@ -164,7 +168,12 @@ int main(int argc, char** argv)
printf("Could not initialize wolfSSL library!\n"); printf("Could not initialize wolfSSL library!\n");
return -1; return -1;
} }
wc_InitRng(&rng); ret = wc_InitRng(&rng);
if (ret != 0) {
printf("wc_InitRng failed with error %d\n", ret);
wolfCrypt_Cleanup();
return -1;
}
XMEMSET(sig, 0, sigSz); XMEMSET(sig, 0, sigSz);
ret = createEccKey(&rng, &hardKey, 32, WOLFSSL_CAAM_DEVID); ret = createEccKey(&rng, &hardKey, 32, WOLFSSL_CAAM_DEVID);

View File

@ -97,7 +97,12 @@ int main(int argc, char** argv)
return -1; return -1;
} }
wc_InitRng(&rng); ret = wc_InitRng(&rng);
if (ret != 0) {
printf("wc_InitRng failed with error %d\n", ret);
wolfCrypt_Cleanup();
return -1;
}
ret = createEccKey(&rng, &hwKey, WOLFSSL_CAAM_DEVID); ret = createEccKey(&rng, &hwKey, WOLFSSL_CAAM_DEVID);
if (ret != 0) { if (ret != 0) {

View File

@ -158,6 +158,9 @@ int main(int argc, char** argv)
TestAesCbc(&enc, &dec); TestAesCbc(&enc, &dec);
wc_AesFree(&enc);
wc_AesFree(&dec);
wc_SECO_CloseHSM(); wc_SECO_CloseHSM();
wolfCrypt_Cleanup(); wolfCrypt_Cleanup();
return 0; return 0;

View File

@ -83,16 +83,16 @@ unsigned int calculateBufferSize(const char* msg, va_list args)
while (*p) { while (*p) {
if (*p == '%' && *(p + 1)) { if (*p == '%' && *(p + 1)) {
p++; // Move past '%' p++; /* Move past '%' */
if (*p == 's' || *p == 'a') { // Handle strings if (*p == 's' || *p == 'a') { /* Handle strings */
char* str = va_arg(args_copy, char*); char* str = va_arg(args_copy, char*);
if (str) { if (str) {
size += XSTRLEN(str); // Add the length of the string size += XSTRLEN(str); /* Add the length of the string */
} }
} else if (*p == 'd' || *p == 'u' || *p == 'x') { } else if (*p == 'd' || *p == 'u' || *p == 'x') {
va_arg(args_copy, int); // Skip integers va_arg(args_copy, int); /* Skip integers */
} else if (*p == 'f') { } else if (*p == 'f') {
va_arg(args_copy, double); // Skip doubles va_arg(args_copy, double); /* Skip doubles */
} }
} }
p++; p++;
@ -527,7 +527,7 @@ FILE* fopen(const char* filename, const char* mode)
parseAndReplace(filename, temp, "/", "\\"); parseAndReplace(filename, temp, "/", "\\");
uefi_printf_wolfssl("Filename After: %s\n", filename); uefi_printf_wolfssl("Filename After: %s\n", filename);
//parseAndReplace(filename, temp, "./", "\\"); /* parseAndReplace(filename, temp, "./", "\\"); */
#else #else
temp = (char*)filename; temp = (char*)filename;
#endif #endif
@ -628,6 +628,7 @@ int fclose(FILE* stream)
int ret = -1; int ret = -1;
(void)stream; (void)stream;
EFI_FILE_HANDLE* fPtr = NULL; EFI_FILE_HANDLE* fPtr = NULL;
EFI_STATUS status;
uefi_printf_debug("Inside custom fclose\n"); uefi_printf_debug("Inside custom fclose\n");
if (stream == NULL) { if (stream == NULL) {
@ -636,7 +637,8 @@ int fclose(FILE* stream)
} }
fPtr = (EFI_FILE_HANDLE*)stream; fPtr = (EFI_FILE_HANDLE*)stream;
uefi_call_wrapper((*fPtr)->Close, 1, *fPtr); status = uefi_call_wrapper((*fPtr)->Close, 1, *fPtr);
ret = EFI_ERROR(status) ? -1 : 0;
XFREE(fPtr, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(fPtr, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@ -1043,7 +1045,7 @@ size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream)
{ {
size_t ret = 0; /* Number of items successfully written */ size_t ret = 0; /* Number of items successfully written */
EFI_FILE_HANDLE* fPtr = NULL; EFI_FILE_HANDLE* fPtr = NULL;
//UINTN totalBytes = count * size * 8; /* Total bytes to write */ /* UINTN totalBytes = count * size * 8; -- Total bytes to write */
EFI_STATUS status; EFI_STATUS status;
uefi_printf_debug("Inside custom fwrite\n"); uefi_printf_debug("Inside custom fwrite\n");
@ -1335,7 +1337,7 @@ unsigned long long current_time(int reset)
/* Convert to total seconds */ /* Convert to total seconds */
unsigned long long total_seconds = unsigned long long total_seconds =
//(days_since_epoch * 86400) + // Uncomment to return epoch time /* (days_since_epoch * 86400) + -- Uncomment to return epoch time */
(time_uefi.Hour * 3600) + (time_uefi.Hour * 3600) +
(time_uefi.Minute * 60) + (time_uefi.Minute * 60) +
time_uefi.Second; time_uefi.Second;

View File

@ -13,10 +13,16 @@
#define uefi_printf(_f_, ...) Print(L##_f_, ##__VA_ARGS__) #define uefi_printf(_f_, ...) Print(L##_f_, ##__VA_ARGS__)
void char8_to_char16(const char* str8, wchar_t* str16) void char8_to_char16(const char* str8, wchar_t* str16, size_t cap)
{ {
size_t i; size_t i;
size_t size_str8 = strlen(str8); size_t size_str8 = strlen(str8);
if (cap == 0) {
return;
}
if (size_str8 > cap - 1) {
size_str8 = cap - 1;
}
for (i = 0; i < size_str8; ++i) { for (i = 0; i < size_str8; ++i) {
str16[i] = (wchar_t)str8[i]; str16[i] = (wchar_t)str8[i];
} }
@ -26,7 +32,7 @@ void char8_to_char16(const char* str8, wchar_t* str16)
void logging_cb(const int logLevel, const char *const logMessage) void logging_cb(const int logLevel, const char *const logMessage)
{ {
wchar_t str16[STR_SIZE]; wchar_t str16[STR_SIZE];
char8_to_char16(logMessage, str16); char8_to_char16(logMessage, str16, STR_SIZE);
uefi_printf("%s", str16); uefi_printf("%s", str16);
} }