forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwolfcrypt_support.c
More file actions
125 lines (113 loc) · 3.78 KB
/
Copy pathwolfcrypt_support.c
File metadata and controls
125 lines (113 loc) · 3.78 KB
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
/* wolfcrypt_support.c
*
* Support infrastructure for wolfCrypt test and benchmark
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot 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.
*
* wolfBoot 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 <stdint.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/types.h>
#if defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK)
/* ========== TIME FUNCTIONS ========== */
/*
* Time implementation strategy:
* 1. If WOLFCRYPT_SECURE_MODE - use secure world API
* 2. If target has SysTick/timer - use hardware timer
* 3. Fallback - simple counter (not accurate for benchmarks)
*/
#if defined(WOLFCRYPT_SECURE_MODE)
/* Use secure mode API for time */
#include "wolfboot/wc_secure.h"
#elif defined(TARGET_va416x0)
/* Use Vorago SDK HAL_time_ms (incremented by SysTick_Handler every 1ms) */
extern volatile uint64_t HAL_time_ms;
#else
/* Simple tick counter fallback */
static volatile unsigned int tick_counter = 0;
#endif
/* my_time() - Used by wolfCrypt ASN.c for certificate time checking
* Returns: Current time in seconds since epoch (or counter value)
*/
unsigned long my_time(unsigned long* timer)
{
#if defined(WOLFCRYPT_SECURE_MODE)
/* Get time from secure world */
unsigned long t = wolfBoot_nsc_get_time();
if (timer) *timer = t;
return t;
#elif defined(TARGET_va416x0)
unsigned long t = (unsigned long)(HAL_time_ms / 1000);
if (timer) *timer = t;
return t;
#else
/* Simple incrementing counter */
tick_counter++;
if (timer) *timer = tick_counter;
return tick_counter;
#endif
}
#ifdef WOLFCRYPT_BENCHMARK
/* current_time() - Used by wolfCrypt benchmark tool for timing measurements
* Parameter: reset - if non-zero, reset the timer
* Returns: Current time in seconds (floating point)
*/
double current_time(int reset)
{
#if defined(WOLFCRYPT_SECURE_MODE)
return wolfBoot_nsc_current_time(reset);
#elif defined(TARGET_va416x0)
(void)reset;
/* Use Vorago SDK SysTick-based millisecond counter */
return (double)HAL_time_ms / 1000.0;
#else
/* Simple counter-based timing */
if (reset)
tick_counter = 0;
else
tick_counter++;
return (double)tick_counter;
#endif
}
#endif /* WOLFCRYPT_BENCHMARK */
/* ========== RNG SEED FUNCTIONS ========== */
/*
* RNG seed generation strategy:
* 1. If WOLFCRYPT_SECURE_MODE - use secure TRNG
* 2. If target has TRNG - use hardware random
* 3. Fallback - pseudo-random based on time (NOT cryptographically secure)
*/
/* Simple incrementing RNG for testing (not cryptographically secure).
* Fixed non-zero seed for deterministic test/benchmark results. */
static uint32_t test_rng_counter = 0x12345678;
/* my_rng_seed_gen() - Generate random seed/data for test/benchmark
* This is NOT cryptographically secure - only for testing!
* Returns: 0 on success
*/
int my_rng_seed_gen(unsigned char* output, unsigned int sz)
{
unsigned int i;
for (i = 0; i < sz; i++) {
if ((i % 4) == 0) {
test_rng_counter++;
}
output[i] = (unsigned char)(test_rng_counter >> ((i % 4) * 8));
}
return 0;
}
#endif /* WOLFCRYPT_TEST || WOLFCRYPT_BENCHMARK */