blob: f38507904f95db46bd45c4e6f206bf051e47268d [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
22#if defined(MBEDTLS_PLATFORM_C)
23static mbedtls_platform_context platform_ctx;
24#endif
25
Ronald Crone9c09f12020-06-08 16:44:58 +020026int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020027{
28 int ret = 0;
29#if defined(MBEDTLS_PLATFORM_C)
30 ret = mbedtls_platform_setup( &platform_ctx );
31#endif /* MBEDTLS_PLATFORM_C */
32 return( ret );
33}
34
Ronald Crone9c09f12020-06-08 16:44:58 +020035void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020036{
37#if defined(MBEDTLS_PLATFORM_C)
38 mbedtls_platform_teardown( &platform_ctx );
39#endif /* MBEDTLS_PLATFORM_C */
40}
41
Ronald Crona0c25392020-06-18 10:10:46 +020042static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020043{
Ronald Crona0c25392020-06-18 10:10:46 +020044 if( ( c >= '0' ) && ( c <= '9' ) )
45 *uc = c - '0';
46 else if( ( c >= 'a' ) && ( c <= 'f' ) )
47 *uc = c - 'a' + 10;
48 else if( ( c >= 'A' ) && ( c <= 'F' ) )
49 *uc = c - 'A' + 10;
50 else
51 return( -1 );
52
53 return( 0 );
54}
55
56int mbedtls_test_unhexify( unsigned char *obuf,
57 size_t obufmax,
58 const char *ibuf,
59 size_t *len )
60{
61 unsigned char uc, uc2;
62
63 *len = strlen( ibuf );
64
65 /* Must be even number of bytes. */
66 if ( ( *len ) & 1 )
67 return( -1 );
68 *len /= 2;
69
70 if ( (*len) > obufmax )
71 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020072
73 while( *ibuf != 0 )
74 {
Ronald Crona0c25392020-06-18 10:10:46 +020075 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
76 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020077
Ronald Crona0c25392020-06-18 10:10:46 +020078 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
79 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +020080
Ronald Crona0c25392020-06-18 10:10:46 +020081 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +020082 }
83
Ronald Crona0c25392020-06-18 10:10:46 +020084 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +020085}
86
Ronald Cron72d628f2020-06-08 17:05:57 +020087void mbedtls_test_hexify( unsigned char *obuf,
88 const unsigned char *ibuf,
89 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +020090{
91 unsigned char l, h;
92
93 while( len != 0 )
94 {
95 h = *ibuf / 16;
96 l = *ibuf % 16;
97
98 if( h < 10 )
99 *obuf++ = '0' + h;
100 else
101 *obuf++ = 'a' + h - 10;
102
103 if( l < 10 )
104 *obuf++ = '0' + l;
105 else
106 *obuf++ = 'a' + l - 10;
107
108 ++ibuf;
109 len--;
110 }
111}
112
Ronald Cron690f3eb2020-06-10 10:42:18 +0200113unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200114{
115 void *p;
116 size_t actual_len = ( len != 0 ) ? len : 1;
117
118 p = mbedtls_calloc( 1, actual_len );
119 TEST_HELPER_ASSERT( p != NULL );
120
121 memset( p, 0x00, actual_len );
122
123 return( p );
124}
125
Ronald Crona256c702020-06-10 10:53:11 +0200126unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200127{
128 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200129 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200130
131 *olen = strlen( ibuf ) / 2;
132
133 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200134 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200135
136 obuf = mbedtls_calloc( 1, *olen );
137 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200138 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200139
140 return( obuf );
141}
142
Ronald Cronde70b162020-06-10 11:03:08 +0200143int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
144 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200145{
146 int ret = 0;
147 uint32_t i = 0;
148
149 if( a_len != b_len )
150 return( -1 );
151
152 for( i = 0; i < a_len; i++ )
153 {
154 if( a[i] != b[i] )
155 {
156 ret = -1;
157 break;
158 }
159 }
160 return ret;
161}