• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CHECK_PARAMS)
12 #include <setjmp.h>
13 #endif
14 
15 #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
16 #include <psa/crypto.h>
17 #include <test/psa_crypto_helpers.h>
18 #endif
19 
20 /*----------------------------------------------------------------------------*/
21 /* Static global variables */
22 
23 #if defined(MBEDTLS_CHECK_PARAMS)
24 typedef struct {
25     uint8_t expected_call;
26     uint8_t expected_call_happened;
27 
28     jmp_buf state;
29 
30     mbedtls_test_param_failed_location_record_t location_record;
31 }
32 param_failed_ctx_t;
33 static param_failed_ctx_t param_failed_ctx;
34 #endif
35 
36 #if defined(MBEDTLS_PLATFORM_C)
37 static mbedtls_platform_context platform_ctx;
38 #endif
39 
40 mbedtls_test_info_t mbedtls_test_info;
41 
42 /*----------------------------------------------------------------------------*/
43 /* Helper Functions */
44 
mbedtls_test_platform_setup(void)45 int mbedtls_test_platform_setup(void)
46 {
47     int ret = 0;
48 
49 #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
50     /* Make sure that injected entropy is present. Otherwise
51      * psa_crypto_init() will fail. This is not necessary for test suites
52      * that don't use PSA, but it's harmless (except for leaving a file
53      * behind). */
54     ret = mbedtls_test_inject_entropy_restore();
55     if (ret != 0) {
56         return ret;
57     }
58 #endif
59 
60 #if defined(MBEDTLS_PLATFORM_C)
61     ret = mbedtls_platform_setup(&platform_ctx);
62 #endif /* MBEDTLS_PLATFORM_C */
63 
64     return ret;
65 }
66 
mbedtls_test_platform_teardown(void)67 void mbedtls_test_platform_teardown(void)
68 {
69 #if defined(MBEDTLS_PLATFORM_C)
70     mbedtls_platform_teardown(&platform_ctx);
71 #endif /* MBEDTLS_PLATFORM_C */
72 }
73 
ascii2uc(const char c,unsigned char * uc)74 static int ascii2uc(const char c, unsigned char *uc)
75 {
76     if ((c >= '0') && (c <= '9')) {
77         *uc = c - '0';
78     } else if ((c >= 'a') && (c <= 'f')) {
79         *uc = c - 'a' + 10;
80     } else if ((c >= 'A') && (c <= 'F')) {
81         *uc = c - 'A' + 10;
82     } else {
83         return -1;
84     }
85 
86     return 0;
87 }
88 
mbedtls_test_fail(const char * test,int line_no,const char * filename)89 void mbedtls_test_fail(const char *test, int line_no, const char *filename)
90 {
91     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
92         /* We've already recorded the test as having failed. Don't
93          * overwrite any previous information about the failure. */
94         return;
95     }
96     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
97     mbedtls_test_info.test = test;
98     mbedtls_test_info.line_no = line_no;
99     mbedtls_test_info.filename = filename;
100 }
101 
mbedtls_test_skip(const char * test,int line_no,const char * filename)102 void mbedtls_test_skip(const char *test, int line_no, const char *filename)
103 {
104     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
105     mbedtls_test_info.test = test;
106     mbedtls_test_info.line_no = line_no;
107     mbedtls_test_info.filename = filename;
108 }
109 
mbedtls_test_set_step(unsigned long step)110 void mbedtls_test_set_step(unsigned long step)
111 {
112     mbedtls_test_info.step = step;
113 }
114 
115 #if defined(MBEDTLS_BIGNUM_C)
116 unsigned mbedtls_test_case_uses_negative_0 = 0;
117 #endif
118 
mbedtls_test_info_reset(void)119 void mbedtls_test_info_reset(void)
120 {
121     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
122     mbedtls_test_info.step = (unsigned long) (-1);
123     mbedtls_test_info.test = 0;
124     mbedtls_test_info.line_no = 0;
125     mbedtls_test_info.filename = 0;
126     memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
127     memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
128 #if defined(MBEDTLS_BIGNUM_C)
129     mbedtls_test_case_uses_negative_0 = 0;
130 #endif
131 }
132 
mbedtls_test_equal(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)133 int mbedtls_test_equal(const char *test, int line_no, const char *filename,
134                        unsigned long long value1, unsigned long long value2)
135 {
136     TEST_CF_PUBLIC(&value1, sizeof(value1));
137     TEST_CF_PUBLIC(&value2, sizeof(value2));
138 
139     if (value1 == value2) {
140         return 1;
141     }
142 
143     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
144         /* We've already recorded the test as having failed. Don't
145          * overwrite any previous information about the failure. */
146         return 0;
147     }
148     mbedtls_test_fail(test, line_no, filename);
149     (void) mbedtls_snprintf(mbedtls_test_info.line1,
150                             sizeof(mbedtls_test_info.line1),
151                             "lhs = 0x%016llx = %lld",
152                             value1, (long long) value1);
153     (void) mbedtls_snprintf(mbedtls_test_info.line2,
154                             sizeof(mbedtls_test_info.line2),
155                             "rhs = 0x%016llx = %lld",
156                             value2, (long long) value2);
157     return 0;
158 }
159 
mbedtls_test_le_u(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)160 int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
161                       unsigned long long value1, unsigned long long value2)
162 {
163     TEST_CF_PUBLIC(&value1, sizeof(value1));
164     TEST_CF_PUBLIC(&value2, sizeof(value2));
165 
166     if (value1 <= value2) {
167         return 1;
168     }
169 
170     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
171         /* We've already recorded the test as having failed. Don't
172          * overwrite any previous information about the failure. */
173         return 0;
174     }
175     mbedtls_test_fail(test, line_no, filename);
176     (void) mbedtls_snprintf(mbedtls_test_info.line1,
177                             sizeof(mbedtls_test_info.line1),
178                             "lhs = 0x%016llx = %llu",
179                             value1, value1);
180     (void) mbedtls_snprintf(mbedtls_test_info.line2,
181                             sizeof(mbedtls_test_info.line2),
182                             "rhs = 0x%016llx = %llu",
183                             value2, value2);
184     return 0;
185 }
186 
mbedtls_test_le_s(const char * test,int line_no,const char * filename,long long value1,long long value2)187 int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
188                       long long value1, long long value2)
189 {
190     TEST_CF_PUBLIC(&value1, sizeof(value1));
191     TEST_CF_PUBLIC(&value2, sizeof(value2));
192 
193     if (value1 <= value2) {
194         return 1;
195     }
196 
197     if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
198         /* We've already recorded the test as having failed. Don't
199          * overwrite any previous information about the failure. */
200         return 0;
201     }
202     mbedtls_test_fail(test, line_no, filename);
203     (void) mbedtls_snprintf(mbedtls_test_info.line1,
204                             sizeof(mbedtls_test_info.line1),
205                             "lhs = 0x%016llx = %lld",
206                             (unsigned long long) value1, value1);
207     (void) mbedtls_snprintf(mbedtls_test_info.line2,
208                             sizeof(mbedtls_test_info.line2),
209                             "rhs = 0x%016llx = %lld",
210                             (unsigned long long) value2, value2);
211     return 0;
212 }
213 
mbedtls_test_unhexify(unsigned char * obuf,size_t obufmax,const char * ibuf,size_t * len)214 int mbedtls_test_unhexify(unsigned char *obuf,
215                           size_t obufmax,
216                           const char *ibuf,
217                           size_t *len)
218 {
219     unsigned char uc, uc2;
220 
221     *len = strlen(ibuf);
222 
223     /* Must be even number of bytes. */
224     if ((*len) & 1) {
225         return -1;
226     }
227     *len /= 2;
228 
229     if ((*len) > obufmax) {
230         return -1;
231     }
232 
233     while (*ibuf != 0) {
234         if (ascii2uc(*(ibuf++), &uc) != 0) {
235             return -1;
236         }
237 
238         if (ascii2uc(*(ibuf++), &uc2) != 0) {
239             return -1;
240         }
241 
242         *(obuf++) = (uc << 4) | uc2;
243     }
244 
245     return 0;
246 }
247 
mbedtls_test_hexify(unsigned char * obuf,const unsigned char * ibuf,int len)248 void mbedtls_test_hexify(unsigned char *obuf,
249                          const unsigned char *ibuf,
250                          int len)
251 {
252     unsigned char l, h;
253 
254     while (len != 0) {
255         h = *ibuf / 16;
256         l = *ibuf % 16;
257 
258         if (h < 10) {
259             *obuf++ = '0' + h;
260         } else {
261             *obuf++ = 'a' + h - 10;
262         }
263 
264         if (l < 10) {
265             *obuf++ = '0' + l;
266         } else {
267             *obuf++ = 'a' + l - 10;
268         }
269 
270         ++ibuf;
271         len--;
272     }
273 }
274 
mbedtls_test_zero_alloc(size_t len)275 unsigned char *mbedtls_test_zero_alloc(size_t len)
276 {
277     void *p;
278     size_t actual_len = (len != 0) ? len : 1;
279 
280     p = mbedtls_calloc(1, actual_len);
281     TEST_HELPER_ASSERT(p != NULL);
282 
283     memset(p, 0x00, actual_len);
284 
285     return p;
286 }
287 
mbedtls_test_unhexify_alloc(const char * ibuf,size_t * olen)288 unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
289 {
290     unsigned char *obuf;
291     size_t len;
292 
293     *olen = strlen(ibuf) / 2;
294 
295     if (*olen == 0) {
296         return mbedtls_test_zero_alloc(*olen);
297     }
298 
299     obuf = mbedtls_calloc(1, *olen);
300     TEST_HELPER_ASSERT(obuf != NULL);
301     TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
302 
303     return obuf;
304 }
305 
mbedtls_test_hexcmp(uint8_t * a,uint8_t * b,uint32_t a_len,uint32_t b_len)306 int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
307                         uint32_t a_len, uint32_t b_len)
308 {
309     int ret = 0;
310     uint32_t i = 0;
311 
312     if (a_len != b_len) {
313         return -1;
314     }
315 
316     for (i = 0; i < a_len; i++) {
317         if (a[i] != b[i]) {
318             ret = -1;
319             break;
320         }
321     }
322     return ret;
323 }
324 
325 #if defined(MBEDTLS_CHECK_PARAMS)
mbedtls_test_param_failed_get_location_record(mbedtls_test_param_failed_location_record_t * location_record)326 void mbedtls_test_param_failed_get_location_record(
327     mbedtls_test_param_failed_location_record_t *location_record)
328 {
329     *location_record = param_failed_ctx.location_record;
330 }
331 
mbedtls_test_param_failed_expect_call(void)332 void mbedtls_test_param_failed_expect_call(void)
333 {
334     param_failed_ctx.expected_call_happened = 0;
335     param_failed_ctx.expected_call = 1;
336 }
337 
mbedtls_test_param_failed_check_expected_call(void)338 int mbedtls_test_param_failed_check_expected_call(void)
339 {
340     param_failed_ctx.expected_call = 0;
341 
342     if (param_failed_ctx.expected_call_happened != 0) {
343         return 0;
344     }
345 
346     return -1;
347 }
348 
mbedtls_test_param_failed_get_state_buf(void)349 void *mbedtls_test_param_failed_get_state_buf(void)
350 {
351     return &param_failed_ctx.state;
352 }
353 
mbedtls_test_param_failed_reset_state(void)354 void mbedtls_test_param_failed_reset_state(void)
355 {
356     memset(param_failed_ctx.state, 0, sizeof(param_failed_ctx.state));
357 }
358 
mbedtls_param_failed(const char * failure_condition,const char * file,int line)359 void mbedtls_param_failed(const char *failure_condition,
360                           const char *file,
361                           int line)
362 {
363     /* Record the location of the failure */
364     param_failed_ctx.location_record.failure_condition = failure_condition;
365     param_failed_ctx.location_record.file = file;
366     param_failed_ctx.location_record.line = line;
367 
368     /* If we are testing the callback function...  */
369     if (param_failed_ctx.expected_call != 0) {
370         param_failed_ctx.expected_call = 0;
371         param_failed_ctx.expected_call_happened = 1;
372     } else {
373         /* ...else try a long jump. If the execution state has not been set-up
374          * or reset then the long jump buffer is all zero's and the call will
375          * with high probability fault, emphasizing there is something to look
376          * at.
377          */
378 
379         longjmp(param_failed_ctx.state, 1);
380     }
381 }
382 #endif /* MBEDTLS_CHECK_PARAMS */
383 
384 #if defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_err_add_check(int high,int low,const char * file,int line)385 void mbedtls_test_err_add_check(int high, int low,
386                                 const char *file, int line)
387 {
388     /* Error codes are always negative (a value of zero is a success) however
389      * their positive opposites can be easier to understand. The following
390      * examples given in comments have been made positive for ease of
391      * understanding. The structure of an error code is such:
392      *
393      *                                                shhhhhhhhlllllll
394      *
395      * s = sign bit.
396      * h = high level error code (includes high level module ID (bits 12..14)
397      *     and module-dependent error code (bits 7..11)).
398      * l = low level error code.
399      */
400     if (high > -0x1000 && high != 0) {
401         /* high < 0001000000000000
402          * No high level module ID bits are set.
403          */
404         mbedtls_test_fail("'high' is not a high-level error code",
405                           line, file);
406     } else if (high < -0x7F80) {
407         /* high > 0111111110000000
408          * Error code is greater than the largest allowed high level module ID.
409          */
410         mbedtls_test_fail("'high' error code is greater than 15 bits",
411                           line, file);
412     } else if ((high & 0x7F) != 0) {
413         /* high & 0000000001111111
414          * Error code contains low level error code bits.
415          */
416         mbedtls_test_fail("'high' contains a low-level error code",
417                           line, file);
418     } else if (low < -0x007F) {
419         /* low >  0000000001111111
420          * Error code contains high or module level error code bits.
421          */
422         mbedtls_test_fail("'low' error code is greater than 7 bits",
423                           line, file);
424     } else if (low > 0) {
425         mbedtls_test_fail("'low' error code is greater than zero",
426                           line, file);
427     }
428 }
429 #endif /* MBEDTLS_TEST_HOOKS */
430 
431 #if defined(MBEDTLS_BIGNUM_C)
mbedtls_test_read_mpi(mbedtls_mpi * X,const char * s)432 int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s)
433 {
434     int negative = 0;
435     /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
436      * This creates an invalid representation, which mbedtls_mpi_read_string()
437      * avoids but we want to be able to create that in test data. */
438     if (s[0] == '-') {
439         ++s;
440         negative = 1;
441     }
442     /* mbedtls_mpi_read_string() currently retains leading zeros.
443      * It always allocates at least one limb for the value 0. */
444     if (s[0] == 0) {
445         mbedtls_mpi_free(X);
446         return 0;
447     }
448     int ret = mbedtls_mpi_read_string(X, 16, s);
449     if (ret != 0) {
450         return ret;
451     }
452     if (negative) {
453         if (mbedtls_mpi_cmp_int(X, 0) == 0) {
454             ++mbedtls_test_case_uses_negative_0;
455         }
456         X->s = -1;
457     }
458     return 0;
459 }
460 #endif
461