Generic changes

* fix compilation warning
* adjust SHA3-384 test error-codes
   The way the codes were constructed before, they were not unique.
* unify code
   Instead of having `ifdef`'s in the code, define our own wrapper around
   the keysource as required.
* add CMake option for help-text in wolfCrypt tests
* expose test `main()` as `wolfcrypt_test_main()`
* Don't overwrite previously set errors
* add FreeRTOS support for Xilinx demo
* move `fp_reverse` from `tfm.c` to `wolfmath.c` and rename to
  `mp_reverse`.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel
2022-05-04 16:12:59 +02:00
committed by Jacob Barthelmeh
parent 754d274d8c
commit f4e258d196
13 changed files with 105 additions and 34 deletions

View File

@ -1563,6 +1563,10 @@ add_option("WOLFSSL_CRYPT_TESTS_LIBS"
"Build static libraries from the wolfCrypt test and benchmark sources (default: disabled)" "Build static libraries from the wolfCrypt test and benchmark sources (default: disabled)"
"no" "yes;no") "no" "yes;no")
add_option("WOLFSSL_CRYPT_TESTS_HELP"
"Add help text to wolfCrypt test (default: disabled)"
"no" "yes;no")
# TODO: - LIBZ # TODO: - LIBZ
# - PKCS#11 # - PKCS#11
# - Cavium # - Cavium
@ -1983,6 +1987,9 @@ if(WOLFSSL_CRYPT_TESTS)
set_target_properties(wolfcrypttest_lib PROPERTIES OUTPUT_NAME "wolfcrypttest") set_target_properties(wolfcrypttest_lib PROPERTIES OUTPUT_NAME "wolfcrypttest")
target_link_libraries(wolfcrypttest_lib wolfssl) target_link_libraries(wolfcrypttest_lib wolfssl)
target_compile_options(wolfcrypttest_lib PRIVATE "-DNO_MAIN_DRIVER") target_compile_options(wolfcrypttest_lib PRIVATE "-DNO_MAIN_DRIVER")
if(WOLFSSL_CRYPT_TESTS_HELP)
target_compile_options(wolfcrypttest_lib PRIVATE "-DHAVE_WOLFCRYPT_TEST_OPTIONS")
endif()
# Make another library for the wolfCrypt benchmark code. # Make another library for the wolfCrypt benchmark code.
add_library(wolfcryptbench_lib add_library(wolfcryptbench_lib
@ -2002,6 +2009,9 @@ if(WOLFSSL_CRYPT_TESTS)
set_property(TARGET wolfcrypttest set_property(TARGET wolfcrypttest
PROPERTY RUNTIME_OUTPUT_NAME PROPERTY RUNTIME_OUTPUT_NAME
testwolfcrypt) testwolfcrypt)
if(WOLFSSL_CRYPT_TESTS_HELP)
target_compile_options(wolfcrypttest PRIVATE "-DHAVE_WOLFCRYPT_TEST_OPTIONS")
endif()
# Build wolfCrypt benchmark executable. # Build wolfCrypt benchmark executable.
add_executable(wolfcryptbench add_executable(wolfcryptbench

View File

@ -37,7 +37,9 @@
/* Xilinx SDK */ /* Xilinx SDK */
#define WOLFSSL_XILINX #define WOLFSSL_XILINX
#ifndef FREERTOS
#define SINGLE_THREADED #define SINGLE_THREADED
#endif
#define NO_FILESYSTEM #define NO_FILESYSTEM
/* Platform - remap printf */ /* Platform - remap printf */

View File

@ -22,6 +22,14 @@
#include "xil_printf.h" #include "xil_printf.h"
#include "xrtcpsu.h" #include "xrtcpsu.h"
#ifdef FREERTOS
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#endif
#include "wolfssl/wolfcrypt/settings.h" #include "wolfssl/wolfcrypt/settings.h"
#include "wolfssl/wolfcrypt/wc_port.h" #include "wolfssl/wolfcrypt/wc_port.h"
#include "wolfssl/wolfcrypt/error-crypt.h" #include "wolfssl/wolfcrypt/error-crypt.h"
@ -64,7 +72,36 @@ unsigned char my_rng_seed_gen(void)
/***************************************************************************** /*****************************************************************************
* Public functions * Public functions
****************************************************************************/ ****************************************************************************/
#ifdef FREERTOS
static void wolfssl_task( void *pvParameters );
static TaskHandle_t xWolfsslTask;
int main( void )
{
xTaskCreate(wolfssl_task, /* The function that implements the task. */
(const char*) "Tx", /* Text name for the task, provided to assist debugging only. */
configMINIMAL_STACK_SIZE, /* The stack allocated to the task. */
NULL, /* The task parameter is not used, so set to NULL. */
tskIDLE_PRIORITY, /* The task runs at the idle priority. */
&xWolfsslTask);
/* Start the task. */
vTaskStartScheduler();
/* If all is well, the scheduler will now be running, and the following line
will never be reached. If the following line does execute, then there was
insufficient FreeRTOS heap memory available for the idle and/or timer tasks
to be created. See the memory management section on the FreeRTOS web site
for more details. */
for (;;)
;
}
static void wolfssl_task( void *pvParameters )
#else
int main() int main()
#endif
{ {
uint8_t cmd; uint8_t cmd;
func_args args; func_args args;
@ -114,6 +151,7 @@ int main()
} }
wolfCrypt_Cleanup(); wolfCrypt_Cleanup();
#ifndef FREERTOS
return 0; return 0;
#endif
} }

View File

@ -4820,10 +4820,8 @@ int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
} }
#endif /* FREESCALE_LTC_AES_GCM */ #endif /* FREESCALE_LTC_AES_GCM */
#if defined(WOLFSSL_XILINX_CRYPT) #if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_AFALG_XILINX_AES)
wc_AesGcmSetKey_ex(aes, key, len, XSECURE_CSU_AES_KEY_SRC_KUP); wc_AesGcmSetKey_ex(aes, key, len, WOLFSSL_XILINX_AES_KEY_SRC);
#elif defined(WOLFSSL_AFALG_XILINX_AES)
wc_AesGcmSetKey_ex(aes, key, len, 0);
#endif #endif
#ifdef WOLF_CRYPTO_CB #ifdef WOLF_CRYPTO_CB

View File

@ -192,6 +192,9 @@ int wc_SignatureVerifyHash(
ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig,
(RsaKey*)key, cc310_hashModeRSA(hash_type, 1)); (RsaKey*)key, cc310_hashModeRSA(hash_type, 1));
} }
if (ret != 0) {
ret = SIG_VERIFY_E;
}
#else #else
word32 plain_len = hash_len; word32 plain_len = hash_len;
@ -242,8 +245,7 @@ int wc_SignatureVerifyHash(
} }
#endif /* WOLFSSL_CRYPTOCELL */ #endif /* WOLFSSL_CRYPTOCELL */
if (ret != 0) { if (ret != 0) {
WOLFSSL_MSG("RSA Signature Verify difference!"); WOLFSSL_MSG("RSA Signature Verify failed!");
ret = SIG_VERIFY_E;
} }
#else #else
ret = SIG_TYPE_E; ret = SIG_TYPE_E;

View File

@ -3810,7 +3810,7 @@ int fp_to_unsigned_bin(fp_int *a, unsigned char *b)
fp_init_copy(t, a); fp_init_copy(t, a);
x = fp_to_unsigned_bin_at_pos(0, t, b); x = fp_to_unsigned_bin_at_pos(0, t, b);
fp_reverse (b, x); mp_reverse (b, x);
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT); XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
@ -3856,7 +3856,7 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c)
b[x] = (unsigned char) (t->dp[0] & 255); b[x] = (unsigned char) (t->dp[0] & 255);
fp_div_2d (t, 8, t, NULL); fp_div_2d (t, 8, t, NULL);
} }
fp_reverse (b, x); mp_reverse (b, x);
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT); XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
@ -4106,23 +4106,6 @@ void fp_rshd(fp_int *a, int x)
fp_clamp(a); fp_clamp(a);
} }
/* reverse an array, used for radix code */
void fp_reverse (unsigned char *s, int len)
{
int ix, iy;
unsigned char t;
ix = 0;
iy = len - 1;
while (ix < iy) {
t = s[ix];
s[ix] = s[iy];
s[iy] = t;
++ix;
--iy;
}
}
/* c = a - b */ /* c = a - b */
int fp_sub_d(fp_int *a, fp_digit b, fp_int *c) int fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
@ -5848,7 +5831,7 @@ int mp_toradix (mp_int *a, char *str, int radix)
/* reverse the digits of the string. In this case _s points /* reverse the digits of the string. In this case _s points
* to the first digit [excluding the sign] of the number] * to the first digit [excluding the sign] of the number]
*/ */
fp_reverse ((unsigned char *)_s, digs); mp_reverse ((unsigned char *)_s, digs);
/* append a NULL so the string is properly terminated */ /* append a NULL so the string is properly terminated */
*str = '\0'; *str = '\0';

View File

@ -71,6 +71,26 @@
#endif #endif
/* reverse an array, used for radix code */
void mp_reverse (unsigned char *s, int len)
{
int ix, iy;
unsigned char t;
if (s == NULL)
return;
ix = 0;
iy = len - 1;
while (ix < iy) {
t = s[ix];
s[ix] = s[iy];
s[iy] = t;
++ix;
--iy;
}
}
int get_digit_count(const mp_int* a) int get_digit_count(const mp_int* a)
{ {
if (a == NULL) if (a == NULL)

View File

@ -1535,7 +1535,14 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
#if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE) #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
int wolf_test_task(void) int wolf_test_task(void)
#else #else
#ifndef NO_MAIN_FUNCTION
int main(int argc, char** argv) int main(int argc, char** argv)
{
return wolfcrypt_test_main(argc, argv);
}
#endif
int wolfcrypt_test_main(int argc, char** argv)
#endif #endif
{ {
int ret; int ret;
@ -3346,21 +3353,21 @@ static int sha3_384_test(void)
ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input, ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
(word32)test_sha[i].inLen); (word32)test_sha[i].inLen);
if (ret != 0) if (ret != 0)
ERROR_OUT(-2801 - i, exit); ERROR_OUT(-2801 - (i * 10), exit);
#ifndef NO_INTM_HASH_TEST #ifndef NO_INTM_HASH_TEST
ret = wc_Sha3_384_GetHash(&sha, hashcopy); ret = wc_Sha3_384_GetHash(&sha, hashcopy);
if (ret != 0) if (ret != 0)
ERROR_OUT(-2802 - i, exit); ERROR_OUT(-2802 - (i * 10), exit);
#endif #endif
ret = wc_Sha3_384_Final(&sha, hash); ret = wc_Sha3_384_Final(&sha, hash);
if (ret != 0) if (ret != 0)
ERROR_OUT(-2803 - i, exit); ERROR_OUT(-2803 - (i * 10), exit);
if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0) if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
ERROR_OUT(-2804 - i, exit); ERROR_OUT(-2804 - (i * 10), exit);
#ifndef NO_INTM_HASH_TEST #ifndef NO_INTM_HASH_TEST
if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0) if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
ERROR_OUT(-2805 - i, exit); ERROR_OUT(-2805 - (i * 10), exit);
#endif #endif
} }

View File

@ -33,6 +33,9 @@ THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args);
#else #else
int wolfcrypt_test(void* args); int wolfcrypt_test(void* args);
#endif #endif
#ifndef NO_MAIN_DRIVER
int wolfcrypt_test_main(int argc, char** argv);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

View File

@ -68,7 +68,14 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
#ifdef WOLFSSL_XILINX_CRYPT #ifdef WOLFSSL_XILINX_CRYPT
#include "xsecure_aes.h" #include "xsecure_aes.h"
#endif #define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_CSU_AES_KEY_SRC_KUP
#endif /* WOLFSSL_XILINX_CRYPT */
#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_AFALG_XILINX_AES)
#if !defined(WOLFSSL_XILINX_AES_KEY_SRC)
#define WOLFSSL_XILINX_AES_KEY_SRC 0
#endif /* !defined(WOLFSSL_XILINX_AES_KEY_SRC) */
#endif /* all Xilinx crypto */
#ifdef WOLFSSL_SE050 #ifdef WOLFSSL_SE050
#include <wolfssl/wolfcrypt/port/nxp/se050_port.h> #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>

View File

@ -678,7 +678,6 @@ int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b);
/* VARIOUS LOW LEVEL STUFFS */ /* VARIOUS LOW LEVEL STUFFS */
int s_fp_add(fp_int *a, fp_int *b, fp_int *c); int s_fp_add(fp_int *a, fp_int *b, fp_int *c);
void s_fp_sub(fp_int *a, fp_int *b, fp_int *c); void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
void fp_reverse(unsigned char *s, int len);
int fp_mul_comba(fp_int *a, fp_int *b, fp_int *c); int fp_mul_comba(fp_int *a, fp_int *b, fp_int *c);

View File

@ -755,6 +755,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
#define XTIME(t1) xilinx_time((t1)) #define XTIME(t1) xilinx_time((t1))
#endif #endif
#include <time.h> #include <time.h>
time_t xilinx_time(time_t * timer);
#elif defined(HAVE_RTP_SYS) #elif defined(HAVE_RTP_SYS)
#include "os.h" /* dc_rtc_api needs */ #include "os.h" /* dc_rtc_api needs */

View File

@ -58,6 +58,7 @@ This library provides big integer math functions.
MP_API int get_digit_count(const mp_int* a); MP_API int get_digit_count(const mp_int* a);
MP_API mp_digit get_digit(const mp_int* a, int n); MP_API mp_digit get_digit(const mp_int* a, int n);
MP_API int get_rand_digit(WC_RNG* rng, mp_digit* d); MP_API int get_rand_digit(WC_RNG* rng, mp_digit* d);
WOLFSSL_LOCAL void mp_reverse(unsigned char *s, int len);
WOLFSSL_API int mp_cond_copy(mp_int* a, int copy, mp_int* b); WOLFSSL_API int mp_cond_copy(mp_int* a, int copy, mp_int* b);
WOLFSSL_API int mp_rand(mp_int* a, int digits, WC_RNG* rng); WOLFSSL_API int mp_rand(mp_int* a, int digits, WC_RNG* rng);