Gilles Peskine | e137ebc | 2021-01-29 21:12:52 +0100 | [diff] [blame^] | 1 | #line 2 "helpers.function" |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 2 | /*----------------------------------------------------------------------------*/ |
| 3 | /* Headers */ |
| 4 | |
Simon Butcher | edb7fd9 | 2016-05-17 13:35:51 +0100 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 8 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 9 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #define mbedtls_fprintf fprintf |
Simon Butcher | 2573136 | 2016-09-30 13:11:29 +0100 | [diff] [blame] | 12 | #define mbedtls_snprintf snprintf |
| 13 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #define mbedtls_free free |
| 15 | #define mbedtls_exit exit |
Simon Butcher | b2d5dd1 | 2016-04-27 13:35:37 +0100 | [diff] [blame] | 16 | #define mbedtls_time time |
| 17 | #define mbedtls_time_t time_t |
Janos Follath | 55abc21 | 2016-04-18 18:18:48 +0100 | [diff] [blame] | 18 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 19 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 20 | #endif |
| 21 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 22 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 23 | #include "mbedtls/memory_buffer_alloc.h" |
| 24 | #endif |
| 25 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 26 | #ifdef _MSC_VER |
| 27 | #include <basetsd.h> |
| 28 | typedef UINT32 uint32_t; |
Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 29 | #define strncasecmp _strnicmp |
| 30 | #define strcasecmp _stricmp |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 31 | #else |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 32 | #include <stdint.h> |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 37 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 38 | #include <unistd.h> |
| 39 | #endif |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | 426c2d4 | 2020-08-25 11:26:37 +0200 | [diff] [blame] | 41 | /* |
| 42 | * Define the two macros |
| 43 | * |
| 44 | * #define TEST_CF_SECRET(ptr, size) |
| 45 | * #define TEST_CF_PUBLIC(ptr, size) |
| 46 | * |
| 47 | * that can be used in tests to mark a memory area as secret (no branch or |
| 48 | * memory access should depend on it) or public (default, only needs to be |
| 49 | * marked explicitly when it was derived from secret data). |
| 50 | * |
| 51 | * Arguments: |
| 52 | * - ptr: a pointer to the memory area to be marked |
| 53 | * - size: the size in bytes of the memory area |
| 54 | * |
| 55 | * Implementation: |
| 56 | * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can |
| 57 | * re-use tools that were designed for checking use of uninitialized memory. |
| 58 | * This file contains two implementations: one based on MemorySanitizer, the |
| 59 | * other on valgrind's memcheck. If none of them is enabled, dummy macros that |
| 60 | * do nothing are defined for convenience. |
| 61 | */ |
Manuel Pégourié-Gonnard | 40597ce | 2020-07-28 10:53:06 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) |
| 63 | #include <sanitizer/msan_interface.h> |
| 64 | |
| 65 | /* Use macros to avoid messing up with origin tracking */ |
| 66 | #define TEST_CF_SECRET __msan_allocated_memory |
| 67 | // void __msan_allocated_memory(const volatile void* data, size_t size); |
| 68 | #define TEST_CF_PUBLIC __msan_unpoison |
| 69 | // void __msan_unpoison(const volatile void *a, size_t size); |
| 70 | |
Manuel Pégourié-Gonnard | 426c2d4 | 2020-08-25 11:26:37 +0200 | [diff] [blame] | 71 | #elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) |
| 72 | #include <valgrind/memcheck.h> |
| 73 | |
| 74 | #define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED |
| 75 | // VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len) |
| 76 | #define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED |
| 77 | // VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len) |
| 78 | |
| 79 | #else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN || |
| 80 | MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */ |
Manuel Pégourié-Gonnard | 40597ce | 2020-07-28 10:53:06 +0200 | [diff] [blame] | 81 | |
| 82 | #define TEST_CF_SECRET(ptr, size) |
| 83 | #define TEST_CF_PUBLIC(ptr, size) |
| 84 | |
| 85 | #endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */ |
| 86 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 87 | /*----------------------------------------------------------------------------*/ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 88 | /* Constants */ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 89 | |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 90 | #define DEPENDENCY_SUPPORTED 0 |
| 91 | #define DEPENDENCY_NOT_SUPPORTED 1 |
| 92 | |
| 93 | #define KEY_VALUE_MAPPING_FOUND 0 |
| 94 | #define KEY_VALUE_MAPPING_NOT_FOUND -1 |
| 95 | |
| 96 | #define DISPATCH_TEST_SUCCESS 0 |
| 97 | #define DISPATCH_TEST_FN_NOT_FOUND 1 |
| 98 | #define DISPATCH_INVALID_TEST_DATA 2 |
| 99 | #define DISPATCH_UNSUPPORTED_SUITE 3 |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | /*----------------------------------------------------------------------------*/ |
| 103 | /* Macros */ |
| 104 | |
| 105 | #define TEST_ASSERT( TEST ) \ |
| 106 | do { \ |
| 107 | if( ! (TEST) ) \ |
| 108 | { \ |
SimonB | 31a6c49 | 2016-05-02 21:32:44 +0100 | [diff] [blame] | 109 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 110 | goto exit; \ |
| 111 | } \ |
| 112 | } while( 0 ) |
| 113 | |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 114 | #define assert(a) if( !( a ) ) \ |
| 115 | { \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 117 | __FILE__, __LINE__, #a ); \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | mbedtls_exit( 1 ); \ |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ronald Cron | eb5d0e9 | 2020-04-06 10:34:22 +0200 | [diff] [blame] | 121 | #if defined(__GNUC__) |
| 122 | /* Test if arg and &(arg)[0] have the same type. This is true if arg is |
| 123 | * an array but not if it's a pointer. */ |
| 124 | #define IS_ARRAY_NOT_POINTER( arg ) \ |
| 125 | ( ! __builtin_types_compatible_p( __typeof__( arg ), \ |
| 126 | __typeof__( &( arg )[0] ) ) ) |
| 127 | #else |
| 128 | /* On platforms where we don't know how to implement this check, |
| 129 | * omit it. Oh well, a non-portable check is better than nothing. */ |
| 130 | #define IS_ARRAY_NOT_POINTER( arg ) 1 |
| 131 | #endif |
| 132 | |
| 133 | /* A compile-time constant with the value 0. If `const_expr` is not a |
| 134 | * compile-time constant with a nonzero value, cause a compile-time error. */ |
| 135 | #define STATIC_ASSERT_EXPR( const_expr ) \ |
makise-homura | 03c2b8f | 2020-08-23 00:28:45 +0300 | [diff] [blame] | 136 | ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) ) |
Ronald Cron | eb5d0e9 | 2020-04-06 10:34:22 +0200 | [diff] [blame] | 137 | /* Return the scalar value `value` (possibly promoted). This is a compile-time |
| 138 | * constant if `value` is. `condition` must be a compile-time constant. |
| 139 | * If `condition` is false, arrange to cause a compile-time error. */ |
| 140 | #define STATIC_ASSERT_THEN_RETURN( condition, value ) \ |
| 141 | ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) ) |
| 142 | |
| 143 | #define ARRAY_LENGTH_UNSAFE( array ) \ |
| 144 | ( sizeof( array ) / sizeof( *( array ) ) ) |
| 145 | /** Return the number of elements of a static or stack array. |
| 146 | * |
| 147 | * \param array A value of array (not pointer) type. |
| 148 | * |
| 149 | * \return The number of elements of the array. |
| 150 | */ |
| 151 | #define ARRAY_LENGTH( array ) \ |
| 152 | ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \ |
| 153 | ARRAY_LENGTH_UNSAFE( array ) ) ) |
| 154 | |
| 155 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 156 | /* |
| 157 | * 32-bit integer manipulation macros (big endian) |
| 158 | */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 159 | #ifndef GET_UINT32_BE |
| 160 | #define GET_UINT32_BE(n,b,i) \ |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 161 | { \ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 162 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
| 163 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
| 164 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
| 165 | | ( (uint32_t) (b)[(i) + 3] ); \ |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 169 | #ifndef PUT_UINT32_BE |
| 170 | #define PUT_UINT32_BE(n,b,i) \ |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 171 | { \ |
| 172 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 173 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 174 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 175 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 176 | } |
| 177 | #endif |
| 178 | |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 179 | |
| 180 | /*----------------------------------------------------------------------------*/ |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 181 | /* Global variables */ |
| 182 | |
Andres Amaya Garcia | 3f50f51 | 2017-10-01 16:42:29 +0100 | [diff] [blame] | 183 | |
| 184 | static struct |
| 185 | { |
| 186 | int failed; |
| 187 | const char *test; |
| 188 | const char *filename; |
| 189 | int line_no; |
| 190 | } |
| 191 | test_info; |
SimonB | 8ca7bc4 | 2016-04-17 23:24:50 +0100 | [diff] [blame] | 192 | |
| 193 | |
| 194 | /*----------------------------------------------------------------------------*/ |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 195 | /* Helper flags for complex dependencies */ |
| 196 | |
| 197 | /* Indicates whether we expect mbedtls_entropy_init |
| 198 | * to initialize some strong entropy source. */ |
| 199 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) || \ |
| 200 | ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ |
| 201 | ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \ |
| 202 | defined(MBEDTLS_HAVEGE_C) || \ |
| 203 | defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \ |
| 204 | defined(ENTROPY_NV_SEED) ) ) |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 205 | #define ENTROPY_HAVE_STRONG |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 206 | #endif |
| 207 | |
| 208 | |
| 209 | /*----------------------------------------------------------------------------*/ |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 210 | /* Helper Functions */ |
| 211 | |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 212 | void test_fail( const char *test, int line_no, const char* filename ) |
| 213 | { |
Gilles Peskine | d4c9fd1 | 2020-08-31 10:21:58 +0200 | [diff] [blame] | 214 | if( test_info.failed ) |
| 215 | { |
| 216 | /* We've already recorded the test as having failed. Don't |
| 217 | * overwrite any previous information about the failure. */ |
| 218 | return; |
| 219 | } |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 220 | test_info.failed = 1; |
| 221 | test_info.test = test; |
| 222 | test_info.line_no = line_no; |
| 223 | test_info.filename = filename; |
| 224 | } |
| 225 | |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 226 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 227 | static int redirect_output( FILE* out_stream, const char* path ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 228 | { |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 229 | int out_fd, dup_fd; |
| 230 | FILE* path_stream; |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 231 | |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 232 | out_fd = fileno( out_stream ); |
| 233 | dup_fd = dup( out_fd ); |
| 234 | |
| 235 | if( dup_fd == -1 ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 236 | { |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 237 | return( -1 ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 238 | } |
| 239 | |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 240 | path_stream = fopen( path, "w" ); |
| 241 | if( path_stream == NULL ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 242 | { |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 243 | close( dup_fd ); |
| 244 | return( -1 ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 245 | } |
| 246 | |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 247 | fflush( out_stream ); |
| 248 | if( dup2( fileno( path_stream ), out_fd ) == -1 ) |
| 249 | { |
| 250 | close( dup_fd ); |
| 251 | fclose( path_stream ); |
| 252 | return( -1 ); |
| 253 | } |
| 254 | |
| 255 | fclose( path_stream ); |
| 256 | return( dup_fd ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 257 | } |
| 258 | |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 259 | static int restore_output( FILE* out_stream, int dup_fd ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 260 | { |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 261 | int out_fd = fileno( out_stream ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 262 | |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 263 | fflush( out_stream ); |
| 264 | if( dup2( dup_fd, out_fd ) == -1 ) |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 265 | { |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 266 | close( out_fd ); |
| 267 | close( dup_fd ); |
| 268 | return( -1 ); |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 269 | } |
| 270 | |
gufe44 | b0ab8c2 | 2020-07-30 09:02:27 +0200 | [diff] [blame] | 271 | close( dup_fd ); |
| 272 | return( 0 ); |
Simon Butcher | e019296 | 2016-10-12 23:07:30 +0100 | [diff] [blame] | 273 | } |
Janos Follath | 8ca53b5 | 2016-10-05 10:57:49 +0100 | [diff] [blame] | 274 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
| 275 | |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 276 | int unhexify( unsigned char *obuf, const char *ibuf ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 277 | { |
| 278 | unsigned char c, c2; |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 279 | int len = strlen( ibuf ) / 2; |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 280 | assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 281 | |
Rich Evans | 4c09114 | 2015-02-02 12:04:10 +0000 | [diff] [blame] | 282 | while( *ibuf != 0 ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 283 | { |
| 284 | c = *ibuf++; |
| 285 | if( c >= '0' && c <= '9' ) |
| 286 | c -= '0'; |
| 287 | else if( c >= 'a' && c <= 'f' ) |
| 288 | c -= 'a' - 10; |
| 289 | else if( c >= 'A' && c <= 'F' ) |
| 290 | c -= 'A' - 10; |
| 291 | else |
| 292 | assert( 0 ); |
| 293 | |
| 294 | c2 = *ibuf++; |
| 295 | if( c2 >= '0' && c2 <= '9' ) |
| 296 | c2 -= '0'; |
| 297 | else if( c2 >= 'a' && c2 <= 'f' ) |
| 298 | c2 -= 'a' - 10; |
| 299 | else if( c2 >= 'A' && c2 <= 'F' ) |
| 300 | c2 -= 'A' - 10; |
| 301 | else |
| 302 | assert( 0 ); |
| 303 | |
| 304 | *obuf++ = ( c << 4 ) | c2; |
| 305 | } |
| 306 | |
| 307 | return len; |
| 308 | } |
| 309 | |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 310 | void hexify( unsigned char *obuf, const unsigned char *ibuf, int len ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 311 | { |
| 312 | unsigned char l, h; |
| 313 | |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 314 | while( len != 0 ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 315 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 316 | h = *ibuf / 16; |
| 317 | l = *ibuf % 16; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 318 | |
| 319 | if( h < 10 ) |
| 320 | *obuf++ = '0' + h; |
| 321 | else |
| 322 | *obuf++ = 'a' + h - 10; |
| 323 | |
| 324 | if( l < 10 ) |
| 325 | *obuf++ = '0' + l; |
| 326 | else |
| 327 | *obuf++ = 'a' + l - 10; |
| 328 | |
| 329 | ++ibuf; |
| 330 | len--; |
| 331 | } |
| 332 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 333 | |
| 334 | /** |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 335 | * Allocate and zeroize a buffer. |
| 336 | * |
| 337 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 338 | * |
| 339 | * For convenience, dies if allocation fails. |
| 340 | */ |
| 341 | static unsigned char *zero_alloc( size_t len ) |
| 342 | { |
| 343 | void *p; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 344 | size_t actual_len = ( len != 0 ) ? len : 1; |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 345 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 346 | p = mbedtls_calloc( 1, actual_len ); |
Paul Bakker | 4d0cfe8 | 2014-07-10 14:37:36 +0200 | [diff] [blame] | 347 | assert( p != NULL ); |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 348 | |
| 349 | memset( p, 0x00, actual_len ); |
| 350 | |
| 351 | return( p ); |
| 352 | } |
| 353 | |
| 354 | /** |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 355 | * Allocate and fill a buffer from hex data. |
| 356 | * |
| 357 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 358 | * overruns (including overreads) when running the test suite under valgrind. |
| 359 | * |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 360 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 361 | * |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 362 | * For convenience, dies if allocation fails. |
| 363 | */ |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 364 | unsigned char *unhexify_alloc( const char *ibuf, size_t *olen ) |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 365 | { |
| 366 | unsigned char *obuf; |
| 367 | |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 368 | *olen = strlen( ibuf ) / 2; |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 369 | |
Manuel Pégourié-Gonnard | 0dc5e0d | 2014-06-13 21:09:26 +0200 | [diff] [blame] | 370 | if( *olen == 0 ) |
| 371 | return( zero_alloc( *olen ) ); |
| 372 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 373 | obuf = mbedtls_calloc( 1, *olen ); |
Paul Bakker | 4d0cfe8 | 2014-07-10 14:37:36 +0200 | [diff] [blame] | 374 | assert( obuf != NULL ); |
Manuel Pégourié-Gonnard | 3d49b9d | 2014-06-06 14:48:09 +0200 | [diff] [blame] | 375 | |
| 376 | (void) unhexify( obuf, ibuf ); |
| 377 | |
| 378 | return( obuf ); |
| 379 | } |
| 380 | |
| 381 | /** |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 382 | * This function just returns data from rand(). |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 383 | * Although predictable and often similar on multiple |
| 384 | * runs, this does not result in identical random on |
| 385 | * each run. So do not use this if the results of a |
| 386 | * test depend on the random data that is generated. |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 387 | * |
| 388 | * rng_state shall be NULL. |
| 389 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 390 | static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 391 | { |
gufe44 | 206cb39 | 2020-08-03 17:56:50 +0200 | [diff] [blame] | 392 | #if !defined(__OpenBSD__) && !defined(__NetBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 393 | size_t i; |
| 394 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 395 | if( rng_state != NULL ) |
| 396 | rng_state = NULL; |
| 397 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 398 | for( i = 0; i < len; ++i ) |
| 399 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 400 | #else |
| 401 | if( rng_state != NULL ) |
| 402 | rng_state = NULL; |
| 403 | |
| 404 | arc4random_buf( output, len ); |
gufe44 | 206cb39 | 2020-08-03 17:56:50 +0200 | [diff] [blame] | 405 | #endif /* !OpenBSD && !NetBSD */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 406 | |
| 407 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 411 | * This function only returns zeros |
| 412 | * |
| 413 | * rng_state shall be NULL. |
| 414 | */ |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 415 | int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 416 | { |
| 417 | if( rng_state != NULL ) |
| 418 | rng_state = NULL; |
| 419 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 420 | memset( output, 0, len ); |
| 421 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 422 | return( 0 ); |
| 423 | } |
| 424 | |
| 425 | typedef struct |
| 426 | { |
| 427 | unsigned char *buf; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 428 | size_t length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 429 | } rnd_buf_info; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * This function returns random based on a buffer it receives. |
| 433 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 434 | * rng_state shall be a pointer to a rnd_buf_info structure. |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 435 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 436 | * The number of bytes released from the buffer on each call to |
| 437 | * the random function is specified by per_call. (Can be between |
| 438 | * 1 and 4) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 439 | * |
| 440 | * After the buffer is empty it will return rand(); |
| 441 | */ |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 442 | int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 443 | { |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 444 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 445 | size_t use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 446 | |
| 447 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 448 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 449 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 450 | use_len = len; |
| 451 | if( len > info->length ) |
| 452 | use_len = info->length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 453 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 454 | if( use_len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 455 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 456 | memcpy( output, info->buf, use_len ); |
| 457 | info->buf += use_len; |
| 458 | info->length -= use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 461 | if( len - use_len > 0 ) |
| 462 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); |
| 463 | |
| 464 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 465 | } |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * Info structure for the pseudo random function |
| 469 | * |
| 470 | * Key should be set at the start to a test-unique value. |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 471 | * Do not forget endianness! |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 472 | * State( v0, v1 ) should be set to zero. |
| 473 | */ |
| 474 | typedef struct |
| 475 | { |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 476 | uint32_t key[16]; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 477 | uint32_t v0, v1; |
| 478 | } rnd_pseudo_info; |
| 479 | |
| 480 | /** |
| 481 | * This function returns random based on a pseudo random function. |
| 482 | * This means the results should be identical on all systems. |
| 483 | * Pseudo random is based on the XTEA encryption algorithm to |
| 484 | * generate pseudorandom. |
| 485 | * |
| 486 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
| 487 | */ |
Simon Butcher | 638dceb | 2018-10-03 16:17:41 +0100 | [diff] [blame] | 488 | int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 489 | { |
| 490 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 491 | uint32_t i, *k, sum, delta=0x9E3779B9; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 492 | unsigned char result[4], *out = output; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 493 | |
| 494 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 495 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 496 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 497 | k = info->key; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 498 | |
| 499 | while( len > 0 ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 500 | { |
Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 501 | size_t use_len = ( len > 4 ) ? 4 : len; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 502 | sum = 0; |
| 503 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 504 | for( i = 0; i < 32; i++ ) |
| 505 | { |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 506 | info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) ) |
| 507 | + info->v1 ) ^ ( sum + k[sum & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 508 | sum += delta; |
Rich Evans | 4291445 | 2015-02-02 12:09:25 +0000 | [diff] [blame] | 509 | info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) ) |
| 510 | + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 513 | PUT_UINT32_BE( info->v0, result, 0 ); |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 514 | memcpy( out, result, use_len ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 515 | len -= use_len; |
Manuel Pégourié-Gonnard | 217a29c | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 516 | out += 4; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 519 | return( 0 ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 520 | } |
SimonB | 0269dad | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 521 | |