Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020, ARM Limited, All Rights Reserved |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 3 | * 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 Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 21 | #include <test/macros.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #if defined(MBEDTLS_PLATFORM_C) |
| 25 | static mbedtls_platform_context platform_ctx; |
| 26 | #endif |
| 27 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 28 | int mbedtls_test_platform_setup( void ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 29 | { |
| 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 Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 37 | void mbedtls_test_platform_teardown( void ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 38 | { |
| 39 | #if defined(MBEDTLS_PLATFORM_C) |
| 40 | mbedtls_platform_teardown( &platform_ctx ); |
| 41 | #endif /* MBEDTLS_PLATFORM_C */ |
| 42 | } |
| 43 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 44 | int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 45 | { |
| 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 Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 78 | void mbedtls_test_hexify( unsigned char *obuf, |
| 79 | const unsigned char *ibuf, |
| 80 | int len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 81 | { |
| 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 Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 104 | unsigned char *mbedtls_test_zero_alloc( size_t len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 105 | { |
| 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 Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 117 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 118 | { |
| 119 | unsigned char *obuf; |
| 120 | |
| 121 | *olen = strlen( ibuf ) / 2; |
| 122 | |
| 123 | if( *olen == 0 ) |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 124 | return( mbedtls_test_zero_alloc( *olen ) ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 125 | |
| 126 | obuf = mbedtls_calloc( 1, *olen ); |
| 127 | TEST_HELPER_ASSERT( obuf != NULL ); |
| 128 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 129 | (void) mbedtls_test_unhexify( obuf, ibuf ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 130 | |
| 131 | return( obuf ); |
| 132 | } |
| 133 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 134 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 135 | uint32_t a_len, uint32_t b_len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 136 | { |
| 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 | } |