Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020, ARM Limited, All Rights Reserved |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 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 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 18 | */ |
| 19 | |
| 20 | #include <test/helpers.h> |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 21 | #include <test/macros.h> |
| 22 | #include <string.h> |
| 23 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 25 | #include <setjmp.h> |
| 26 | #endif |
| 27 | |
| 28 | /*----------------------------------------------------------------------------*/ |
| 29 | /* Static global variables */ |
| 30 | |
| 31 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 32 | typedef 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 | } |
| 41 | param_failed_ctx_t; |
| 42 | static param_failed_ctx_t param_failed_ctx; |
| 43 | #endif |
| 44 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_PLATFORM_C) |
| 46 | static mbedtls_platform_context platform_ctx; |
| 47 | #endif |
| 48 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 49 | /*----------------------------------------------------------------------------*/ |
| 50 | /* Helper Functions */ |
| 51 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 52 | int mbedtls_test_platform_setup( void ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 53 | { |
| 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 Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 61 | void mbedtls_test_platform_teardown( void ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 62 | { |
| 63 | #if defined(MBEDTLS_PLATFORM_C) |
| 64 | mbedtls_platform_teardown( &platform_ctx ); |
| 65 | #endif /* MBEDTLS_PLATFORM_C */ |
| 66 | } |
| 67 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 68 | static int ascii2uc(const char c, unsigned char *uc) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 69 | { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 70 | 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 | |
| 82 | int 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 Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 98 | |
| 99 | while( *ibuf != 0 ) |
| 100 | { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 101 | if ( ascii2uc( *(ibuf++), &uc ) != 0 ) |
| 102 | return( -1 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 103 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 104 | if ( ascii2uc( *(ibuf++), &uc2 ) != 0 ) |
| 105 | return( -1 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 106 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 107 | *(obuf++) = ( uc << 4 ) | uc2; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 110 | return( 0 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 113 | void mbedtls_test_hexify( unsigned char *obuf, |
| 114 | const unsigned char *ibuf, |
| 115 | int len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 116 | { |
| 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 Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 139 | unsigned char *mbedtls_test_zero_alloc( size_t len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 140 | { |
| 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 Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 152 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 153 | { |
| 154 | unsigned char *obuf; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 155 | size_t len; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 156 | |
| 157 | *olen = strlen( ibuf ) / 2; |
| 158 | |
| 159 | if( *olen == 0 ) |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 160 | return( mbedtls_test_zero_alloc( *olen ) ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 161 | |
| 162 | obuf = mbedtls_calloc( 1, *olen ); |
| 163 | TEST_HELPER_ASSERT( obuf != NULL ); |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 164 | TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 165 | |
| 166 | return( obuf ); |
| 167 | } |
| 168 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 169 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 170 | uint32_t a_len, uint32_t b_len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 171 | { |
| 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 Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 188 | |
| 189 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 190 | void 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 | |
| 196 | void 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 | |
| 202 | int 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 | |
| 212 | void* mbedtls_test_param_failed_get_state_buf( void ) |
| 213 | { |
| 214 | return ¶m_failed_ctx.state[0]; |
| 215 | } |
| 216 | |
| 217 | void mbedtls_test_param_failed_reset_state( void ) |
| 218 | { |
| 219 | memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) ); |
| 220 | } |
| 221 | |
| 222 | void 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 */ |