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