blob: 08d88a5dc21cc9af727e674d0de5fdc87c2090a2 [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 Cron72d628f2020-06-08 17:05:57 +020044int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
Ronald Cronf40529d2020-06-09 16:27:37 +020045{
46 unsigned char c, c2;
47 int len = strlen( ibuf ) / 2;
48 TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
49
50 while( *ibuf != 0 )
51 {
52 c = *ibuf++;
53 if( c >= '0' && c <= '9' )
54 c -= '0';
55 else if( c >= 'a' && c <= 'f' )
56 c -= 'a' - 10;
57 else if( c >= 'A' && c <= 'F' )
58 c -= 'A' - 10;
59 else
60 TEST_HELPER_ASSERT( 0 );
61
62 c2 = *ibuf++;
63 if( c2 >= '0' && c2 <= '9' )
64 c2 -= '0';
65 else if( c2 >= 'a' && c2 <= 'f' )
66 c2 -= 'a' - 10;
67 else if( c2 >= 'A' && c2 <= 'F' )
68 c2 -= 'A' - 10;
69 else
70 TEST_HELPER_ASSERT( 0 );
71
72 *obuf++ = ( c << 4 ) | c2;
73 }
74
75 return len;
76}
77
Ronald Cron72d628f2020-06-08 17:05:57 +020078void mbedtls_test_hexify( unsigned char *obuf,
79 const unsigned char *ibuf,
80 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +020081{
82 unsigned char l, h;
83
84 while( len != 0 )
85 {
86 h = *ibuf / 16;
87 l = *ibuf % 16;
88
89 if( h < 10 )
90 *obuf++ = '0' + h;
91 else
92 *obuf++ = 'a' + h - 10;
93
94 if( l < 10 )
95 *obuf++ = '0' + l;
96 else
97 *obuf++ = 'a' + l - 10;
98
99 ++ibuf;
100 len--;
101 }
102}
103
Ronald Cron690f3eb2020-06-10 10:42:18 +0200104unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200105{
106 void *p;
107 size_t actual_len = ( len != 0 ) ? len : 1;
108
109 p = mbedtls_calloc( 1, actual_len );
110 TEST_HELPER_ASSERT( p != NULL );
111
112 memset( p, 0x00, actual_len );
113
114 return( p );
115}
116
Ronald Crona256c702020-06-10 10:53:11 +0200117unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200118{
119 unsigned char *obuf;
120
121 *olen = strlen( ibuf ) / 2;
122
123 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200124 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200125
126 obuf = mbedtls_calloc( 1, *olen );
127 TEST_HELPER_ASSERT( obuf != NULL );
128
Ronald Cron72d628f2020-06-08 17:05:57 +0200129 (void) mbedtls_test_unhexify( obuf, ibuf );
Ronald Cronf40529d2020-06-09 16:27:37 +0200130
131 return( obuf );
132}
133
Ronald Cronde70b162020-06-10 11:03:08 +0200134int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
135 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200136{
137 int ret = 0;
138 uint32_t i = 0;
139
140 if( a_len != b_len )
141 return( -1 );
142
143 for( i = 0; i < a_len; i++ )
144 {
145 if( a[i] != b[i] )
146 {
147 ret = -1;
148 break;
149 }
150 }
151 return ret;
152}