blob: b5ca1f8c1267e40509f5bfd9e895d68d2524f090 [file] [log] [blame]
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02001/* Copyright (C) 2020, ARM Limited, All Rights Reserved
2 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * This file is part of mbed TLS (https://tls.mbed.org)
17 */
18
19#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020020#include <test/macros.h>
21#include <string.h>
22
23#if defined(MBEDTLS_PLATFORM_C)
24static mbedtls_platform_context platform_ctx;
25#endif
26
27int platform_setup( void )
28{
29 int ret = 0;
30#if defined(MBEDTLS_PLATFORM_C)
31 ret = mbedtls_platform_setup( &platform_ctx );
32#endif /* MBEDTLS_PLATFORM_C */
33 return( ret );
34}
35
36void platform_teardown( void )
37{
38#if defined(MBEDTLS_PLATFORM_C)
39 mbedtls_platform_teardown( &platform_ctx );
40#endif /* MBEDTLS_PLATFORM_C */
41}
42
43int unhexify( unsigned char *obuf, const char *ibuf )
44{
45 unsigned char c, c2;
46 int len = strlen( ibuf ) / 2;
47 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
48
49 while( *ibuf != 0 )
50 {
51 c = *ibuf++;
52 if( c >= '0' && c <= '9' )
53 c -= '0';
54 else if( c >= 'a' && c <= 'f' )
55 c -= 'a' - 10;
56 else if( c >= 'A' && c <= 'F' )
57 c -= 'A' - 10;
58 else
59 TEST_HELPER_ASSERT( 0 );
60
61 c2 = *ibuf++;
62 if( c2 >= '0' && c2 <= '9' )
63 c2 -= '0';
64 else if( c2 >= 'a' && c2 <= 'f' )
65 c2 -= 'a' - 10;
66 else if( c2 >= 'A' && c2 <= 'F' )
67 c2 -= 'A' - 10;
68 else
69 TEST_HELPER_ASSERT( 0 );
70
71 *obuf++ = ( c << 4 ) | c2;
72 }
73
74 return len;
75}
76
77void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
78{
79 unsigned char l, h;
80
81 while( len != 0 )
82 {
83 h = *ibuf / 16;
84 l = *ibuf % 16;
85
86 if( h < 10 )
87 *obuf++ = '0' + h;
88 else
89 *obuf++ = 'a' + h - 10;
90
91 if( l < 10 )
92 *obuf++ = '0' + l;
93 else
94 *obuf++ = 'a' + l - 10;
95
96 ++ibuf;
97 len--;
98 }
99}
100
101unsigned char *zero_alloc( size_t len )
102{
103 void *p;
104 size_t actual_len = ( len != 0 ) ? len : 1;
105
106 p = mbedtls_calloc( 1, actual_len );
107 TEST_HELPER_ASSERT( p != NULL );
108
109 memset( p, 0x00, actual_len );
110
111 return( p );
112}
113
114unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
115{
116 unsigned char *obuf;
117
118 *olen = strlen( ibuf ) / 2;
119
120 if( *olen == 0 )
121 return( zero_alloc( *olen ) );
122
123 obuf = mbedtls_calloc( 1, *olen );
124 TEST_HELPER_ASSERT( obuf != NULL );
125
126 (void) unhexify( obuf, ibuf );
127
128 return( obuf );
129}
130
131int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
132{
133 int ret = 0;
134 uint32_t i = 0;
135
136 if( a_len != b_len )
137 return( -1 );
138
139 for( i = 0; i < a_len; i++ )
140 {
141 if( a[i] != b[i] )
142 {
143 ret = -1;
144 break;
145 }
146 }
147 return ret;
148}