blob: fff065a44025eff619e62c07c89ef1cab6898016 [file] [log] [blame]
Bence Szépkúti86974652020-06-15 11:59:37 +02001/*
2 * Copyright (C) 2020, ARM Limited, All Rights Reserved
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.
16 *
17 * This file is part of mbed TLS (https://tls.mbed.org)
18 */
19
20#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020021#include <test/macros.h>
22#include <string.h>
23
Ronald Crona1236142020-07-01 16:01:21 +020024#if defined(MBEDTLS_CHECK_PARAMS)
25#include <setjmp.h>
26#endif
27
28/*----------------------------------------------------------------------------*/
29/* Static global variables */
30
31#if defined(MBEDTLS_CHECK_PARAMS)
32typedef struct
33{
34 uint8_t expected_call;
35 uint8_t expected_call_happened;
36
37 jmp_buf state;
38
39 mbedtls_test_param_failed_location_record_t location_record;
40}
41param_failed_ctx_t;
42static param_failed_ctx_t param_failed_ctx;
43#endif
44
Ronald Cronf40529d2020-06-09 16:27:37 +020045#if defined(MBEDTLS_PLATFORM_C)
46static mbedtls_platform_context platform_ctx;
47#endif
48
Ronald Crona1236142020-07-01 16:01:21 +020049/*----------------------------------------------------------------------------*/
50/* Helper Functions */
51
Ronald Crone9c09f12020-06-08 16:44:58 +020052int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020053{
54 int ret = 0;
55#if defined(MBEDTLS_PLATFORM_C)
56 ret = mbedtls_platform_setup( &platform_ctx );
57#endif /* MBEDTLS_PLATFORM_C */
58 return( ret );
59}
60
Ronald Crone9c09f12020-06-08 16:44:58 +020061void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020062{
63#if defined(MBEDTLS_PLATFORM_C)
64 mbedtls_platform_teardown( &platform_ctx );
65#endif /* MBEDTLS_PLATFORM_C */
66}
67
Ronald Crona0c25392020-06-18 10:10:46 +020068static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020069{
Ronald Crona0c25392020-06-18 10:10:46 +020070 if( ( c >= '0' ) && ( c <= '9' ) )
71 *uc = c - '0';
72 else if( ( c >= 'a' ) && ( c <= 'f' ) )
73 *uc = c - 'a' + 10;
74 else if( ( c >= 'A' ) && ( c <= 'F' ) )
75 *uc = c - 'A' + 10;
76 else
77 return( -1 );
78
79 return( 0 );
80}
81
82int mbedtls_test_unhexify( unsigned char *obuf,
83 size_t obufmax,
84 const char *ibuf,
85 size_t *len )
86{
87 unsigned char uc, uc2;
88
89 *len = strlen( ibuf );
90
91 /* Must be even number of bytes. */
92 if ( ( *len ) & 1 )
93 return( -1 );
94 *len /= 2;
95
96 if ( (*len) > obufmax )
97 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020098
99 while( *ibuf != 0 )
100 {
Ronald Crona0c25392020-06-18 10:10:46 +0200101 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
102 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200103
Ronald Crona0c25392020-06-18 10:10:46 +0200104 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
105 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200106
Ronald Crona0c25392020-06-18 10:10:46 +0200107 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200108 }
109
Ronald Crona0c25392020-06-18 10:10:46 +0200110 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200111}
112
Ronald Cron72d628f2020-06-08 17:05:57 +0200113void mbedtls_test_hexify( unsigned char *obuf,
114 const unsigned char *ibuf,
115 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200116{
117 unsigned char l, h;
118
119 while( len != 0 )
120 {
121 h = *ibuf / 16;
122 l = *ibuf % 16;
123
124 if( h < 10 )
125 *obuf++ = '0' + h;
126 else
127 *obuf++ = 'a' + h - 10;
128
129 if( l < 10 )
130 *obuf++ = '0' + l;
131 else
132 *obuf++ = 'a' + l - 10;
133
134 ++ibuf;
135 len--;
136 }
137}
138
Ronald Cron690f3eb2020-06-10 10:42:18 +0200139unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200140{
141 void *p;
142 size_t actual_len = ( len != 0 ) ? len : 1;
143
144 p = mbedtls_calloc( 1, actual_len );
145 TEST_HELPER_ASSERT( p != NULL );
146
147 memset( p, 0x00, actual_len );
148
149 return( p );
150}
151
Ronald Crona256c702020-06-10 10:53:11 +0200152unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200153{
154 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200155 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200156
157 *olen = strlen( ibuf ) / 2;
158
159 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200160 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200161
162 obuf = mbedtls_calloc( 1, *olen );
163 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200164 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200165
166 return( obuf );
167}
168
Ronald Cronde70b162020-06-10 11:03:08 +0200169int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
170 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200171{
172 int ret = 0;
173 uint32_t i = 0;
174
175 if( a_len != b_len )
176 return( -1 );
177
178 for( i = 0; i < a_len; i++ )
179 {
180 if( a[i] != b[i] )
181 {
182 ret = -1;
183 break;
184 }
185 }
186 return ret;
187}
Ronald Crona1236142020-07-01 16:01:21 +0200188
189#if defined(MBEDTLS_CHECK_PARAMS)
190void mbedtls_test_param_failed_get_location_record(
191 mbedtls_test_param_failed_location_record_t *location_record )
192{
193 *location_record = param_failed_ctx.location_record;
194}
195
196void mbedtls_test_param_failed_expect_call( void )
197{
198 param_failed_ctx.expected_call_happened = 0;
199 param_failed_ctx.expected_call = 1;
200}
201
202int mbedtls_test_param_failed_check_expected_call( void )
203{
204 param_failed_ctx.expected_call = 0;
205
206 if( param_failed_ctx.expected_call_happened != 0 )
207 return( 0 );
208
209 return( -1 );
210}
211
212void* mbedtls_test_param_failed_get_state_buf( void )
213{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200214 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200215}
216
217void mbedtls_test_param_failed_reset_state( void )
218{
219 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
220}
221
222void mbedtls_param_failed( const char *failure_condition,
223 const char *file,
224 int line )
225{
226 /* Record the location of the failure */
227 param_failed_ctx.location_record.failure_condition = failure_condition;
228 param_failed_ctx.location_record.file = file;
229 param_failed_ctx.location_record.line = line;
230
231 /* If we are testing the callback function... */
232 if( param_failed_ctx.expected_call != 0 )
233 {
234 param_failed_ctx.expected_call = 0;
235 param_failed_ctx.expected_call_happened = 1;
236 }
237 else
238 {
239 /* ...else try a long jump. If the execution state has not been set-up
240 * or reset then the long jump buffer is all zero's and the call will
241 * with high probability fault, emphasizing there is something to look
242 * at.
243 */
244
245 longjmp( param_failed_ctx.state, 1 );
246 }
247}
248#endif /* MBEDTLS_CHECK_PARAMS */