1 /*
2  *  Copyright The Mbed TLS Contributors
3  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4  */
5 
6 #include <test/constant_flow.h>
7 #include <test/helpers.h>
8 #include <test/macros.h>
9 #include <string.h>
10 
11 #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
12 #include <psa/crypto.h>
13 #include <test/psa_crypto_helpers.h>
14 #endif
15 
16 /*----------------------------------------------------------------------------*/
17 /* Static global variables */
18 
19 #if defined(MBEDTLS_PLATFORM_C)
20 static mbedtls_platform_context platform_ctx;
21 #endif
22 
23 mbedtls_test_info_t mbedtls_test_info;
24 
25 /*----------------------------------------------------------------------------*/
26 /* Helper Functions */
27 
mbedtls_test_platform_setup(void)28 int mbedtls_test_platform_setup(void)
29 {
30     int ret = 0;
31 
32 #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
33     /* Make sure that injected entropy is present. Otherwise
34      * psa_crypto_init() will fail. This is not necessary for test suites
35      * that don't use PSA, but it's harmless (except for leaving a file
36      * behind). */
37     ret = mbedtls_test_inject_entropy_restore();
38     if (ret != 0) {
39         return ret;
40     }
41 #endif
42 
43 #if defined(MBEDTLS_PLATFORM_C)
44     ret = mbedtls_platform_setup(&platform_ctx);
45 #endif /* MBEDTLS_PLATFORM_C */
46 
47     return ret;
48 }
49 
mbedtls_test_platform_teardown(void)50 void mbedtls_test_platform_teardown(void)
51 {
52 #if defined(MBEDTLS_PLATFORM_C)
53     mbedtls_platform_teardown(&platform_ctx);
54 #endif /* MBEDTLS_PLATFORM_C */
55 }
56 
mbedtls_test_ascii2uc(const char c,unsigned char * uc)57 int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
58 {
59     if ((c >= '0') && (c <= '9')) {
60         *uc = c - '0';
61     } else if ((c >= 'a') && (c <= 'f')) {
62         *uc = c - 'a' + 10;
63     } else if ((c >= 'A') && (c <= 'F')) {
64         *uc = c - 'A' + 10;
65     } else {
66         return -1;
67     }
68 
69     return 0;
70 }
71 
mbedtls_test_fail(const char * test,int line_no,const char * filename)72 void mbedtls_test_fail(const char *test, int line_no, const char *filename)
73 {
74     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
75         /* We've already recorded the test as having failed. Don't
76          * overwrite any previous information about the failure. */
77         return;
78     }
79     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
80     mbedtls_test_info.test = test;
81     mbedtls_test_info.line_no = line_no;
82     mbedtls_test_info.filename = filename;
83 }
84 
mbedtls_test_skip(const char * test,int line_no,const char * filename)85 void mbedtls_test_skip(const char *test, int line_no, const char *filename)
86 {
87     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
88     mbedtls_test_info.test = test;
89     mbedtls_test_info.line_no = line_no;
90     mbedtls_test_info.filename = filename;
91 }
92 
mbedtls_test_set_step(unsigned long step)93 void mbedtls_test_set_step(unsigned long step)
94 {
95     mbedtls_test_info.step = step;
96 }
97 
98 #if defined(MBEDTLS_BIGNUM_C)
99 unsigned mbedtls_test_case_uses_negative_0 = 0;
100 #endif
101 
mbedtls_test_info_reset(void)102 void mbedtls_test_info_reset(void)
103 {
104     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
105     mbedtls_test_info.step = (unsigned long) (-1);
106     mbedtls_test_info.test = 0;
107     mbedtls_test_info.line_no = 0;
108     mbedtls_test_info.filename = 0;
109     memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
110     memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
111 #if defined(MBEDTLS_BIGNUM_C)
112     mbedtls_test_case_uses_negative_0 = 0;
113 #endif
114 }
115 
mbedtls_test_equal(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)116 int mbedtls_test_equal(const char *test, int line_no, const char *filename,
117                        unsigned long long value1, unsigned long long value2)
118 {
119     TEST_CF_PUBLIC(&value1, sizeof(value1));
120     TEST_CF_PUBLIC(&value2, sizeof(value2));
121 
122     if (value1 == value2) {
123         return 1;
124     }
125 
126     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
127         /* We've already recorded the test as having failed. Don't
128          * overwrite any previous information about the failure. */
129         return 0;
130     }
131     mbedtls_test_fail(test, line_no, filename);
132     (void) mbedtls_snprintf(mbedtls_test_info.line1,
133                             sizeof(mbedtls_test_info.line1),
134                             "lhs = 0x%016llx = %lld",
135                             value1, (long long) value1);
136     (void) mbedtls_snprintf(mbedtls_test_info.line2,
137                             sizeof(mbedtls_test_info.line2),
138                             "rhs = 0x%016llx = %lld",
139                             value2, (long long) value2);
140     return 0;
141 }
142 
mbedtls_test_le_u(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)143 int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
144                       unsigned long long value1, unsigned long long value2)
145 {
146     TEST_CF_PUBLIC(&value1, sizeof(value1));
147     TEST_CF_PUBLIC(&value2, sizeof(value2));
148 
149     if (value1 <= value2) {
150         return 1;
151     }
152 
153     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
154         /* We've already recorded the test as having failed. Don't
155          * overwrite any previous information about the failure. */
156         return 0;
157     }
158     mbedtls_test_fail(test, line_no, filename);
159     (void) mbedtls_snprintf(mbedtls_test_info.line1,
160                             sizeof(mbedtls_test_info.line1),
161                             "lhs = 0x%016llx = %llu",
162                             value1, value1);
163     (void) mbedtls_snprintf(mbedtls_test_info.line2,
164                             sizeof(mbedtls_test_info.line2),
165                             "rhs = 0x%016llx = %llu",
166                             value2, value2);
167     return 0;
168 }
169 
mbedtls_test_le_s(const char * test,int line_no,const char * filename,long long value1,long long value2)170 int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
171                       long long value1, long long value2)
172 {
173     TEST_CF_PUBLIC(&value1, sizeof(value1));
174     TEST_CF_PUBLIC(&value2, sizeof(value2));
175 
176     if (value1 <= value2) {
177         return 1;
178     }
179 
180     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
181         /* We've already recorded the test as having failed. Don't
182          * overwrite any previous information about the failure. */
183         return 0;
184     }
185     mbedtls_test_fail(test, line_no, filename);
186     (void) mbedtls_snprintf(mbedtls_test_info.line1,
187                             sizeof(mbedtls_test_info.line1),
188                             "lhs = 0x%016llx = %lld",
189                             (unsigned long long) value1, value1);
190     (void) mbedtls_snprintf(mbedtls_test_info.line2,
191                             sizeof(mbedtls_test_info.line2),
192                             "rhs = 0x%016llx = %lld",
193                             (unsigned long long) value2, value2);
194     return 0;
195 }
196 
mbedtls_test_unhexify(unsigned char * obuf,size_t obufmax,const char * ibuf,size_t * len)197 int mbedtls_test_unhexify(unsigned char *obuf,
198                           size_t obufmax,
199                           const char *ibuf,
200                           size_t *len)
201 {
202     unsigned char uc, uc2;
203 
204     *len = strlen(ibuf);
205 
206     /* Must be even number of bytes. */
207     if ((*len) & 1) {
208         return -1;
209     }
210     *len /= 2;
211 
212     if ((*len) > obufmax) {
213         return -1;
214     }
215 
216     while (*ibuf != 0) {
217         if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
218             return -1;
219         }
220 
221         if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
222             return -1;
223         }
224 
225         *(obuf++) = (uc << 4) | uc2;
226     }
227 
228     return 0;
229 }
230 
mbedtls_test_hexify(unsigned char * obuf,const unsigned char * ibuf,int len)231 void mbedtls_test_hexify(unsigned char *obuf,
232                          const unsigned char *ibuf,
233                          int len)
234 {
235     unsigned char l, h;
236 
237     while (len != 0) {
238         h = *ibuf / 16;
239         l = *ibuf % 16;
240 
241         if (h < 10) {
242             *obuf++ = '0' + h;
243         } else {
244             *obuf++ = 'a' + h - 10;
245         }
246 
247         if (l < 10) {
248             *obuf++ = '0' + l;
249         } else {
250             *obuf++ = 'a' + l - 10;
251         }
252 
253         ++ibuf;
254         len--;
255     }
256 }
257 
mbedtls_test_zero_alloc(size_t len)258 unsigned char *mbedtls_test_zero_alloc(size_t len)
259 {
260     void *p;
261     size_t actual_len = (len != 0) ? len : 1;
262 
263     p = mbedtls_calloc(1, actual_len);
264     TEST_HELPER_ASSERT(p != NULL);
265 
266     memset(p, 0x00, actual_len);
267 
268     return p;
269 }
270 
mbedtls_test_unhexify_alloc(const char * ibuf,size_t * olen)271 unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
272 {
273     unsigned char *obuf;
274     size_t len;
275 
276     *olen = strlen(ibuf) / 2;
277 
278     if (*olen == 0) {
279         return mbedtls_test_zero_alloc(*olen);
280     }
281 
282     obuf = mbedtls_calloc(1, *olen);
283     TEST_HELPER_ASSERT(obuf != NULL);
284     TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
285 
286     return obuf;
287 }
288 
mbedtls_test_hexcmp(uint8_t * a,uint8_t * b,uint32_t a_len,uint32_t b_len)289 int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
290                         uint32_t a_len, uint32_t b_len)
291 {
292     int ret = 0;
293     uint32_t i = 0;
294 
295     if (a_len != b_len) {
296         return -1;
297     }
298 
299     for (i = 0; i < a_len; i++) {
300         if (a[i] != b[i]) {
301             ret = -1;
302             break;
303         }
304     }
305     return ret;
306 }
307 
308 #if defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_err_add_check(int high,int low,const char * file,int line)309 void mbedtls_test_err_add_check(int high, int low,
310                                 const char *file, int line)
311 {
312     /* Error codes are always negative (a value of zero is a success) however
313      * their positive opposites can be easier to understand. The following
314      * examples given in comments have been made positive for ease of
315      * understanding. The structure of an error code is such:
316      *
317      *                                                shhhhhhhhlllllll
318      *
319      * s = sign bit.
320      * h = high level error code (includes high level module ID (bits 12..14)
321      *     and module-dependent error code (bits 7..11)).
322      * l = low level error code.
323      */
324     if (high > -0x1000 && high != 0) {
325         /* high < 0001000000000000
326          * No high level module ID bits are set.
327          */
328         mbedtls_test_fail("'high' is not a high-level error code",
329                           line, file);
330     } else if (high < -0x7F80) {
331         /* high > 0111111110000000
332          * Error code is greater than the largest allowed high level module ID.
333          */
334         mbedtls_test_fail("'high' error code is greater than 15 bits",
335                           line, file);
336     } else if ((high & 0x7F) != 0) {
337         /* high & 0000000001111111
338          * Error code contains low level error code bits.
339          */
340         mbedtls_test_fail("'high' contains a low-level error code",
341                           line, file);
342     } else if (low < -0x007F) {
343         /* low >  0000000001111111
344          * Error code contains high or module level error code bits.
345          */
346         mbedtls_test_fail("'low' error code is greater than 7 bits",
347                           line, file);
348     } else if (low > 0) {
349         mbedtls_test_fail("'low' error code is greater than zero",
350                           line, file);
351     }
352 }
353 #endif /* MBEDTLS_TEST_HOOKS */
354