wolfssl/IDE/XilinxSDK/wolfssl_example.c
Steffen Jaeckel 0e57e9858f Integrate Xilinx Versal
* add Versal specific glue
   The same structure of an "XSecure client" is used throughout the API's,
   therefor define it once and re-use in all clients.
* integrate Versal AES-GCM engine
* integrate Versal SHA3-384 engine
* add versal support to tests
  - There's no intermediate-hash API for Versal.
* add specific test with large AAD
   Test only with `n*16 byte` wide chunks of AAD, so it gets processed in the
   hardware engine.
* add specific test with misaligned AES-GCM arguments
* integrate Versal RSA engine
* disable failing RSA test-case when Xilinx Crypto is enabled
* introduce define `WOLFSSL_XILINX_CRYPT_VERSAL`
* integrate Versal TRNG engine
* allow using Versal TRNG w/o wolfcrypt DRBG
   Versal TRNG already provides a HRNG mode which does the same as the
   wolfcrypt DRBG implementation.
* add support for user-supplied nonce to Versal TRNG
* add `wc_XsecureErrorToString()` to map PLM error codes to messages.
* integrate Versal EcDSA engine
* update tests to work with Versal EcDSA
   If deterministic K is enabled, the tests failed here since the Versal
   EcDSA engine doesn't support the SECP256R1 curve yet.
* Xilinx crypto engines like aligned memory very much
   Make this a default choice, not via the user configuration.
* add Xilinx-specific `WOLFSSL_MSG()` equivalent
   `WOLFSSL_XIL_MSG()` does the same as `WOLFSSL_MSG()` besides waiting for
   1 second before printing to stdout, since the PLM maybe prints to same and
   outputs would be mixed up.
   This waiting can be disabled by defining `WOLFSSL_XIL_MSG_NO_SLEEP`.
* add option to enable DPA CounterMeasures in AES-GCM crypto engine
* add "command mode" to Xilinx bare-metal example
* update Xilinx default user settings
* add script to execute benchmarks
* add scripts to create graphics
* add Vitis 2022.1 example projects

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
2022-09-29 09:39:57 -06:00

218 lines
6.9 KiB
C

/* wolfssl_example.c
*
* Copyright (C) 2006-2022 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include "xil_printf.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/wc_port.h"
#include "wolfssl/wolfcrypt/error-crypt.h"
#include "wolfssl/wolfcrypt/logging.h"
#include "wolfcrypt/test/test.h"
#include "wolfcrypt/benchmark/benchmark.h"
/*****************************************************************************
* Configuration
****************************************************************************/
/*****************************************************************************
* Private types/enumerations/variables
****************************************************************************/
/*****************************************************************************
* Public types/enumerations/variables
****************************************************************************/
typedef struct func_args
{
int argc;
char **argv;
int return_code;
} func_args;
const char menu1[] = "\n"
"\tb. WolfCrypt Benchmark\n"
"\tt. WolfCrypt Test\n"
"\n"
"\tc. Command mode\n";
/*****************************************************************************
* Private functions
****************************************************************************/
#if !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
/* Test RNG Seed Function */
/* TODO: Must provide real seed to RNG */
unsigned char my_rng_seed_gen(void)
{
static unsigned int kTestSeed = 1;
return kTestSeed++;
}
#endif
/*****************************************************************************
* Public functions
****************************************************************************/
#ifdef FREERTOS
static void wolfssl_task( void *pvParameters );
static TaskHandle_t xWolfsslTask;
int main( void )
{
xil_printf("\nStarting up FreeRTOS\n");
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()
#endif
{
uint8_t cmd[128], c;
char *arg[sizeof(cmd)/2 + 1];
size_t cmdlen, argnum, n;
func_args args;
int (*func)(int argc, char** argv);
int command_mode = 0;
#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
#endif
/* initialize wolfSSL */
wolfCrypt_Init();
while (1) {
memset(&args, 0, sizeof(args));
args.return_code = NOT_COMPILED_IN; /* default */
if (!command_mode) {
xil_printf("\n\t\t\t\tMENU\n");
xil_printf(menu1);
xil_printf("Please select one of the above options:\n");
xil_printf("Both 'b' and 't' allow arguments as if you'd call the regular shell version.\n");
}
cmdlen = 0;
do {
c = inbyte();
cmd[cmdlen] = c;
if (!command_mode) outbyte(c);
cmdlen++;
cmdlen %= sizeof(cmd);
} while (c != '\n' && c != '\r');
if (cmdlen > 2) {
outbyte('\n');
/* Poor man's argv parser */
XMEMSET(arg, 0, sizeof(arg));
if (cmd[1] == ' ') {
if (cmd[0] == 'b') {
arg[0] = "wolfcrypt_benchmark";
func = wolfcrypt_benchmark_main;
} else if (cmd[0] == 't') {
arg[0] = "wolfcrypt_test";
func = wolfcrypt_test_main;
}
if (arg[0] != NULL) {
argnum = 1;
for (n = 2; n < cmdlen; ++n) {
switch (cmd[n]) {
case ' ':
case '\t':
case '\r':
case '\n':
cmd[n] = '\0';
if (arg[argnum] != NULL)
argnum++;
break;
default:
if (arg[argnum] == NULL)
arg[argnum] = (char*)&cmd[n];
break;
}
}
func(argnum, arg);
}
}
} else {
switch (cmd[0]) {
case 't':
xil_printf("Running wolfCrypt Tests...\n");
#ifndef NO_CRYPT_TEST
args.return_code = 0;
wolfcrypt_test(&args);
#endif
xil_printf("Crypt Test: Return code %d\n", args.return_code);
break;
case 'b':
xil_printf("Running wolfCrypt Benchmarks...\n");
#ifndef NO_CRYPT_BENCHMARK
args.return_code = 0;
benchmark_test(&args);
#endif
xil_printf("Benchmark Test: Return code %d\n", args.return_code);
break;
case 'c':
if (!command_mode) xil_printf("Entering command mode, enter 'c<enter>' to return to manual mode\n");
command_mode ^= 1;
break;
default:
if (!command_mode) xil_printf("\nSelection out of range\n");
break;
}
}
}
wolfCrypt_Cleanup();
#ifndef FREERTOS
return 0;
#endif
}