1 /* wolf_main.c
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 
23 #include <wolfssl/wolfcrypt/settings.h>
24 #include <wolfssl/wolfcrypt/random.h> /* for CUSTOM_RAND_TYPE */
25 
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 
31 
32 /* TIME CODE */
33 /* TODO: Implement real RTC */
34 /* Optionally you can define NO_ASN_TIME to disable all cert time checks */
35 static int gTimeMs;
hw_get_time_sec(void)36 static int hw_get_time_sec(void)
37 {
38     #warning Must implement your own time source if validating certificates
39 
40 	return ++gTimeMs;
41 }
42 
43 /* This is used by wolfCrypt asn.c for cert time checking */
my_time(unsigned long * timer)44 unsigned long my_time(unsigned long* timer)
45 {
46     (void)timer;
47     return hw_get_time_sec();
48 }
49 
50 #ifndef WOLFCRYPT_ONLY
51 /* This is used by TLS only */
LowResTimer(void)52 unsigned int LowResTimer(void)
53 {
54     return hw_get_time_sec();
55 }
56 #endif
57 
58 #ifndef NO_CRYPT_BENCHMARK
59 /* This is used by wolfCrypt benchmark tool only */
current_time(int reset)60 double current_time(int reset)
61 {
62     double timeNow;
63     int timeMs = gTimeMs;
64     (void)reset;
65     timeNow = (timeMs / 1000); // sec
66     timeNow += (double)(timeMs % 1000) / 1000; // ms
67     return timeNow;
68 }
69 #endif
70 
71 /* RNG CODE */
72 /* TODO: Implement real RNG */
73 static unsigned int gCounter;
hw_rand(void)74 unsigned int hw_rand(void)
75 {
76     #warning Must implement your own random source
77 
78     return ++gCounter;
79 }
80 
my_rng_seed_gen(void)81 unsigned int my_rng_seed_gen(void)
82 {
83     return hw_rand();
84 }
85 
my_rng_gen_block(unsigned char * output,unsigned int sz)86 int my_rng_gen_block(unsigned char* output, unsigned int sz)
87 {
88     uint32_t i = 0;
89 
90     while (i < sz)
91     {
92         /* If not aligned or there is odd/remainder */
93         if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz ||
94             ((uint32_t)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0
95         ) {
96             /* Single byte at a time */
97             output[i++] = (unsigned char)my_rng_seed_gen();
98         }
99         else {
100             /* Use native 8, 16, 32 or 64 copy instruction */
101             *((CUSTOM_RAND_TYPE*)&output[i]) = my_rng_seed_gen();
102             i += sizeof(CUSTOM_RAND_TYPE);
103         }
104     }
105 
106     return 0;
107 }
108 
109 
110 #ifdef XMALLOC_OVERRIDE
myMalloc(size_t n,void * heap,int type)111 void *myMalloc(size_t n, void* heap, int type)
112 {
113     (void)n;
114     (void)heap;
115     (void)type;
116 
117     #warning Must implement your own malloc
118 
119     return NULL;
120 }
myFree(void * p,void * heap,int type)121 void myFree(void *p, void* heap, int type)
122 {
123     (void)p;
124     (void)heap;
125     (void)type;
126 
127     #warning Must implement your own free
128 }
129 
130 /* Required for normal math (!USE_FAST_MATH) */
myRealloc(void * p,size_t n,void * heap,int type)131 void *myRealloc(void *p, size_t n, void* heap, int type)
132 {
133     (void)p;
134     (void)n;
135     (void)heap;
136     (void)type;
137 
138     #warning Must implement your own realloc
139 
140     return NULL;
141 }
142 #endif /* XMALLOC_OVERRIDE */
143