File: wolfssl_example.c

package info (click to toggle)
wolfssl 5.8.4-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 117,604 kB
  • sloc: ansic: 1,584,954; asm: 481,206; sh: 11,586; cs: 6,596; xml: 3,878; perl: 3,291; makefile: 2,058; ada: 1,891; javascript: 748; python: 636; cpp: 131; ruby: 118; objc: 80; tcl: 73
file content (217 lines) | stat: -rw-r--r-- 7,036 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* wolfssl_example.c
 *
 * Copyright (C) 2006-2025 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 3 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
}