blob: a18f1d4b83832266dcbec5d40d382ae3ae3f038d [file] [log] [blame]
Bence Szépkúti86974652020-06-15 11:59:37 +02001/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02002 * Copyright The Mbed TLS Contributors
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02003 * 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.
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020016 */
17
18#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020019#include <test/macros.h>
20#include <string.h>
21
Ronald Crona1236142020-07-01 16:01:21 +020022#if defined(MBEDTLS_CHECK_PARAMS)
23#include <setjmp.h>
24#endif
25
26/*----------------------------------------------------------------------------*/
27/* Static global variables */
28
29#if defined(MBEDTLS_CHECK_PARAMS)
30typedef struct
31{
32 uint8_t expected_call;
33 uint8_t expected_call_happened;
34
35 jmp_buf state;
36
37 mbedtls_test_param_failed_location_record_t location_record;
38}
39param_failed_ctx_t;
40static param_failed_ctx_t param_failed_ctx;
41#endif
42
Ronald Cronf40529d2020-06-09 16:27:37 +020043#if defined(MBEDTLS_PLATFORM_C)
44static mbedtls_platform_context platform_ctx;
45#endif
46
Ronald Crona1236142020-07-01 16:01:21 +020047/*----------------------------------------------------------------------------*/
48/* Helper Functions */
49
Ronald Crone9c09f12020-06-08 16:44:58 +020050int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020051{
52 int ret = 0;
53#if defined(MBEDTLS_PLATFORM_C)
54 ret = mbedtls_platform_setup( &platform_ctx );
55#endif /* MBEDTLS_PLATFORM_C */
56 return( ret );
57}
58
Ronald Crone9c09f12020-06-08 16:44:58 +020059void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020060{
61#if defined(MBEDTLS_PLATFORM_C)
62 mbedtls_platform_teardown( &platform_ctx );
63#endif /* MBEDTLS_PLATFORM_C */
64}
65
Ronald Crona0c25392020-06-18 10:10:46 +020066static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020067{
Ronald Crona0c25392020-06-18 10:10:46 +020068 if( ( c >= '0' ) && ( c <= '9' ) )
69 *uc = c - '0';
70 else if( ( c >= 'a' ) && ( c <= 'f' ) )
71 *uc = c - 'a' + 10;
72 else if( ( c >= 'A' ) && ( c <= 'F' ) )
73 *uc = c - 'A' + 10;
74 else
75 return( -1 );
76
77 return( 0 );
78}
79
80int mbedtls_test_unhexify( unsigned char *obuf,
81 size_t obufmax,
82 const char *ibuf,
83 size_t *len )
84{
85 unsigned char uc, uc2;
86
87 *len = strlen( ibuf );
88
89 /* Must be even number of bytes. */
90 if ( ( *len ) & 1 )
91 return( -1 );
92 *len /= 2;
93
94 if ( (*len) > obufmax )
95 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020096
97 while( *ibuf != 0 )
98 {
Ronald Crona0c25392020-06-18 10:10:46 +020099 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
100 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200101
Ronald Crona0c25392020-06-18 10:10:46 +0200102 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
103 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200104
Ronald Crona0c25392020-06-18 10:10:46 +0200105 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200106 }
107
Ronald Crona0c25392020-06-18 10:10:46 +0200108 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200109}
110
Ronald Cron72d628f2020-06-08 17:05:57 +0200111void mbedtls_test_hexify( unsigned char *obuf,
112 const unsigned char *ibuf,
113 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200114{
115 unsigned char l, h;
116
117 while( len != 0 )
118 {
119 h = *ibuf / 16;
120 l = *ibuf % 16;
121
122 if( h < 10 )
123 *obuf++ = '0' + h;
124 else
125 *obuf++ = 'a' + h - 10;
126
127 if( l < 10 )
128 *obuf++ = '0' + l;
129 else
130 *obuf++ = 'a' + l - 10;
131
132 ++ibuf;
133 len--;
134 }
135}
136
Ronald Cron690f3eb2020-06-10 10:42:18 +0200137unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200138{
139 void *p;
140 size_t actual_len = ( len != 0 ) ? len : 1;
141
142 p = mbedtls_calloc( 1, actual_len );
143 TEST_HELPER_ASSERT( p != NULL );
144
145 memset( p, 0x00, actual_len );
146
147 return( p );
148}
149
Ronald Crona256c702020-06-10 10:53:11 +0200150unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200151{
152 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200153 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200154
155 *olen = strlen( ibuf ) / 2;
156
157 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200158 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200159
160 obuf = mbedtls_calloc( 1, *olen );
161 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200162 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200163
164 return( obuf );
165}
166
Ronald Cronde70b162020-06-10 11:03:08 +0200167int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
168 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200169{
170 int ret = 0;
171 uint32_t i = 0;
172
173 if( a_len != b_len )
174 return( -1 );
175
176 for( i = 0; i < a_len; i++ )
177 {
178 if( a[i] != b[i] )
179 {
180 ret = -1;
181 break;
182 }
183 }
184 return ret;
185}
Ronald Crona1236142020-07-01 16:01:21 +0200186
187#if defined(MBEDTLS_CHECK_PARAMS)
188void mbedtls_test_param_failed_get_location_record(
189 mbedtls_test_param_failed_location_record_t *location_record )
190{
191 *location_record = param_failed_ctx.location_record;
192}
193
194void mbedtls_test_param_failed_expect_call( void )
195{
196 param_failed_ctx.expected_call_happened = 0;
197 param_failed_ctx.expected_call = 1;
198}
199
200int mbedtls_test_param_failed_check_expected_call( void )
201{
202 param_failed_ctx.expected_call = 0;
203
204 if( param_failed_ctx.expected_call_happened != 0 )
205 return( 0 );
206
207 return( -1 );
208}
209
210void* mbedtls_test_param_failed_get_state_buf( void )
211{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200212 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200213}
214
215void mbedtls_test_param_failed_reset_state( void )
216{
217 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
218}
219
220void mbedtls_param_failed( const char *failure_condition,
221 const char *file,
222 int line )
223{
224 /* Record the location of the failure */
225 param_failed_ctx.location_record.failure_condition = failure_condition;
226 param_failed_ctx.location_record.file = file;
227 param_failed_ctx.location_record.line = line;
228
229 /* If we are testing the callback function... */
230 if( param_failed_ctx.expected_call != 0 )
231 {
232 param_failed_ctx.expected_call = 0;
233 param_failed_ctx.expected_call_happened = 1;
234 }
235 else
236 {
237 /* ...else try a long jump. If the execution state has not been set-up
238 * or reset then the long jump buffer is all zero's and the call will
239 * with high probability fault, emphasizing there is something to look
240 * at.
241 */
242
243 longjmp( param_failed_ctx.state, 1 );
244 }
245}
246#endif /* MBEDTLS_CHECK_PARAMS */