blob: 5f1250110131a36671f2e4bcce7782b9fb4033a0 [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
Ronald Crone9c09f12020-06-08 16:44:58 +020027int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020028{
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
Ronald Crone9c09f12020-06-08 16:44:58 +020036void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020037{
38#if defined(MBEDTLS_PLATFORM_C)
39 mbedtls_platform_teardown( &platform_ctx );
40#endif /* MBEDTLS_PLATFORM_C */
41}
42
Ronald Cron72d628f2020-06-08 17:05:57 +020043int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
Ronald Cronf40529d2020-06-09 16:27:37 +020044{
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
Ronald Cron72d628f2020-06-08 17:05:57 +020077void mbedtls_test_hexify( unsigned char *obuf,
78 const unsigned char *ibuf,
79 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +020080{
81 unsigned char l, h;
82
83 while( len != 0 )
84 {
85 h = *ibuf / 16;
86 l = *ibuf % 16;
87
88 if( h < 10 )
89 *obuf++ = '0' + h;
90 else
91 *obuf++ = 'a' + h - 10;
92
93 if( l < 10 )
94 *obuf++ = '0' + l;
95 else
96 *obuf++ = 'a' + l - 10;
97
98 ++ibuf;
99 len--;
100 }
101}
102
Ronald Cron690f3eb2020-06-10 10:42:18 +0200103unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200104{
105 void *p;
106 size_t actual_len = ( len != 0 ) ? len : 1;
107
108 p = mbedtls_calloc( 1, actual_len );
109 TEST_HELPER_ASSERT( p != NULL );
110
111 memset( p, 0x00, actual_len );
112
113 return( p );
114}
115
Ronald Crona256c702020-06-10 10:53:11 +0200116unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200117{
118 unsigned char *obuf;
119
120 *olen = strlen( ibuf ) / 2;
121
122 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200123 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200124
125 obuf = mbedtls_calloc( 1, *olen );
126 TEST_HELPER_ASSERT( obuf != NULL );
127
Ronald Cron72d628f2020-06-08 17:05:57 +0200128 (void) mbedtls_test_unhexify( obuf, ibuf );
Ronald Cronf40529d2020-06-09 16:27:37 +0200129
130 return( obuf );
131}
132
133int hexcmp( uint8_t * a, uint8_t * b, uint32_t a_len, uint32_t b_len )
134{
135 int ret = 0;
136 uint32_t i = 0;
137
138 if( a_len != b_len )
139 return( -1 );
140
141 for( i = 0; i < a_len; i++ )
142 {
143 if( a[i] != b[i] )
144 {
145 ret = -1;
146 break;
147 }
148 }
149 return ret;
150}