blob: b9abf19aa38d1c73dbe8828fb2583b1440cf8076 [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
24#if defined(MBEDTLS_PLATFORM_C)
25static mbedtls_platform_context platform_ctx;
26#endif
27
Ronald Crone9c09f12020-06-08 16:44:58 +020028int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020029{
30 int ret = 0;
31#if defined(MBEDTLS_PLATFORM_C)
32 ret = mbedtls_platform_setup( &platform_ctx );
33#endif /* MBEDTLS_PLATFORM_C */
34 return( ret );
35}
36
Ronald Crone9c09f12020-06-08 16:44:58 +020037void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020038{
39#if defined(MBEDTLS_PLATFORM_C)
40 mbedtls_platform_teardown( &platform_ctx );
41#endif /* MBEDTLS_PLATFORM_C */
42}
43
Ronald Crona0c25392020-06-18 10:10:46 +020044static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020045{
Ronald Crona0c25392020-06-18 10:10:46 +020046 if( ( c >= '0' ) && ( c <= '9' ) )
47 *uc = c - '0';
48 else if( ( c >= 'a' ) && ( c <= 'f' ) )
49 *uc = c - 'a' + 10;
50 else if( ( c >= 'A' ) && ( c <= 'F' ) )
51 *uc = c - 'A' + 10;
52 else
53 return( -1 );
54
55 return( 0 );
56}
57
58int mbedtls_test_unhexify( unsigned char *obuf,
59 size_t obufmax,
60 const char *ibuf,
61 size_t *len )
62{
63 unsigned char uc, uc2;
64
65 *len = strlen( ibuf );
66
67 /* Must be even number of bytes. */
68 if ( ( *len ) & 1 )
69 return( -1 );
70 *len /= 2;
71
72 if ( (*len) > obufmax )
73 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020074
75 while( *ibuf != 0 )
76 {
Ronald Crona0c25392020-06-18 10:10:46 +020077 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
78 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020079
Ronald Crona0c25392020-06-18 10:10:46 +020080 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
81 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020082
Ronald Crona0c25392020-06-18 10:10:46 +020083 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +020084 }
85
Ronald Crona0c25392020-06-18 10:10:46 +020086 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +020087}
88
Ronald Cron72d628f2020-06-08 17:05:57 +020089void mbedtls_test_hexify( unsigned char *obuf,
90 const unsigned char *ibuf,
91 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +020092{
93 unsigned char l, h;
94
95 while( len != 0 )
96 {
97 h = *ibuf / 16;
98 l = *ibuf % 16;
99
100 if( h < 10 )
101 *obuf++ = '0' + h;
102 else
103 *obuf++ = 'a' + h - 10;
104
105 if( l < 10 )
106 *obuf++ = '0' + l;
107 else
108 *obuf++ = 'a' + l - 10;
109
110 ++ibuf;
111 len--;
112 }
113}
114
Ronald Cron690f3eb2020-06-10 10:42:18 +0200115unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200116{
117 void *p;
118 size_t actual_len = ( len != 0 ) ? len : 1;
119
120 p = mbedtls_calloc( 1, actual_len );
121 TEST_HELPER_ASSERT( p != NULL );
122
123 memset( p, 0x00, actual_len );
124
125 return( p );
126}
127
Ronald Crona256c702020-06-10 10:53:11 +0200128unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200129{
130 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200131 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200132
133 *olen = strlen( ibuf ) / 2;
134
135 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200136 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200137
138 obuf = mbedtls_calloc( 1, *olen );
139 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200140 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200141
142 return( obuf );
143}
144
Ronald Cronde70b162020-06-10 11:03:08 +0200145int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
146 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200147{
148 int ret = 0;
149 uint32_t i = 0;
150
151 if( a_len != b_len )
152 return( -1 );
153
154 for( i = 0; i < a_len; i++ )
155 {
156 if( a[i] != b[i] )
157 {
158 ret = -1;
159 break;
160 }
161 }
162 return ret;
163}