mbed TLS Build
tests/suites/helpers.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | #line 1 "helpers.function" |
markrad | 0:cdf462088d13 | 2 | /*----------------------------------------------------------------------------*/ |
markrad | 0:cdf462088d13 | 3 | /* Headers */ |
markrad | 0:cdf462088d13 | 4 | |
markrad | 0:cdf462088d13 | 5 | #include <stdlib.h> |
markrad | 0:cdf462088d13 | 6 | |
markrad | 0:cdf462088d13 | 7 | #if defined(MBEDTLS_PLATFORM_C) |
markrad | 0:cdf462088d13 | 8 | #include "mbedtls/platform.h" |
markrad | 0:cdf462088d13 | 9 | #else |
markrad | 0:cdf462088d13 | 10 | #include <stdio.h> |
markrad | 0:cdf462088d13 | 11 | #define mbedtls_fprintf fprintf |
markrad | 0:cdf462088d13 | 12 | #define mbedtls_snprintf snprintf |
markrad | 0:cdf462088d13 | 13 | #define mbedtls_calloc calloc |
markrad | 0:cdf462088d13 | 14 | #define mbedtls_free free |
markrad | 0:cdf462088d13 | 15 | #define mbedtls_exit exit |
markrad | 0:cdf462088d13 | 16 | #define mbedtls_time time |
markrad | 0:cdf462088d13 | 17 | #define mbedtls_time_t time_t |
markrad | 0:cdf462088d13 | 18 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
markrad | 0:cdf462088d13 | 19 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
markrad | 0:cdf462088d13 | 20 | #endif |
markrad | 0:cdf462088d13 | 21 | |
markrad | 0:cdf462088d13 | 22 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
markrad | 0:cdf462088d13 | 23 | #include "mbedtls/memory_buffer_alloc.h" |
markrad | 0:cdf462088d13 | 24 | #endif |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | #ifdef _MSC_VER |
markrad | 0:cdf462088d13 | 27 | #include <basetsd.h> |
markrad | 0:cdf462088d13 | 28 | typedef UINT32 uint32_t; |
markrad | 0:cdf462088d13 | 29 | #define strncasecmp _strnicmp |
markrad | 0:cdf462088d13 | 30 | #define strcasecmp _stricmp |
markrad | 0:cdf462088d13 | 31 | #else |
markrad | 0:cdf462088d13 | 32 | #include <stdint.h> |
markrad | 0:cdf462088d13 | 33 | #endif |
markrad | 0:cdf462088d13 | 34 | |
markrad | 0:cdf462088d13 | 35 | #include <string.h> |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
markrad | 0:cdf462088d13 | 38 | #include <unistd.h> |
markrad | 0:cdf462088d13 | 39 | #endif |
markrad | 0:cdf462088d13 | 40 | |
markrad | 0:cdf462088d13 | 41 | /*----------------------------------------------------------------------------*/ |
markrad | 0:cdf462088d13 | 42 | /* Constants */ |
markrad | 0:cdf462088d13 | 43 | |
markrad | 0:cdf462088d13 | 44 | #define DEPENDENCY_SUPPORTED 0 |
markrad | 0:cdf462088d13 | 45 | #define DEPENDENCY_NOT_SUPPORTED 1 |
markrad | 0:cdf462088d13 | 46 | |
markrad | 0:cdf462088d13 | 47 | #define KEY_VALUE_MAPPING_FOUND 0 |
markrad | 0:cdf462088d13 | 48 | #define KEY_VALUE_MAPPING_NOT_FOUND -1 |
markrad | 0:cdf462088d13 | 49 | |
markrad | 0:cdf462088d13 | 50 | #define DISPATCH_TEST_SUCCESS 0 |
markrad | 0:cdf462088d13 | 51 | #define DISPATCH_TEST_FN_NOT_FOUND 1 |
markrad | 0:cdf462088d13 | 52 | #define DISPATCH_INVALID_TEST_DATA 2 |
markrad | 0:cdf462088d13 | 53 | #define DISPATCH_UNSUPPORTED_SUITE 3 |
markrad | 0:cdf462088d13 | 54 | |
markrad | 0:cdf462088d13 | 55 | |
markrad | 0:cdf462088d13 | 56 | /*----------------------------------------------------------------------------*/ |
markrad | 0:cdf462088d13 | 57 | /* Macros */ |
markrad | 0:cdf462088d13 | 58 | |
markrad | 0:cdf462088d13 | 59 | #define TEST_ASSERT( TEST ) \ |
markrad | 0:cdf462088d13 | 60 | do { \ |
markrad | 0:cdf462088d13 | 61 | if( ! (TEST) ) \ |
markrad | 0:cdf462088d13 | 62 | { \ |
markrad | 0:cdf462088d13 | 63 | test_fail( #TEST, __LINE__, __FILE__ ); \ |
markrad | 0:cdf462088d13 | 64 | goto exit; \ |
markrad | 0:cdf462088d13 | 65 | } \ |
markrad | 0:cdf462088d13 | 66 | } while( 0 ) |
markrad | 0:cdf462088d13 | 67 | |
markrad | 0:cdf462088d13 | 68 | #define assert(a) if( !( a ) ) \ |
markrad | 0:cdf462088d13 | 69 | { \ |
markrad | 0:cdf462088d13 | 70 | mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ |
markrad | 0:cdf462088d13 | 71 | __FILE__, __LINE__, #a ); \ |
markrad | 0:cdf462088d13 | 72 | mbedtls_exit( 1 ); \ |
markrad | 0:cdf462088d13 | 73 | } |
markrad | 0:cdf462088d13 | 74 | |
markrad | 0:cdf462088d13 | 75 | /* |
markrad | 0:cdf462088d13 | 76 | * 32-bit integer manipulation macros (big endian) |
markrad | 0:cdf462088d13 | 77 | */ |
markrad | 0:cdf462088d13 | 78 | #ifndef GET_UINT32_BE |
markrad | 0:cdf462088d13 | 79 | #define GET_UINT32_BE(n,b,i) \ |
markrad | 0:cdf462088d13 | 80 | { \ |
markrad | 0:cdf462088d13 | 81 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
markrad | 0:cdf462088d13 | 82 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
markrad | 0:cdf462088d13 | 83 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
markrad | 0:cdf462088d13 | 84 | | ( (uint32_t) (b)[(i) + 3] ); \ |
markrad | 0:cdf462088d13 | 85 | } |
markrad | 0:cdf462088d13 | 86 | #endif |
markrad | 0:cdf462088d13 | 87 | |
markrad | 0:cdf462088d13 | 88 | #ifndef PUT_UINT32_BE |
markrad | 0:cdf462088d13 | 89 | #define PUT_UINT32_BE(n,b,i) \ |
markrad | 0:cdf462088d13 | 90 | { \ |
markrad | 0:cdf462088d13 | 91 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
markrad | 0:cdf462088d13 | 92 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
markrad | 0:cdf462088d13 | 93 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
markrad | 0:cdf462088d13 | 94 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
markrad | 0:cdf462088d13 | 95 | } |
markrad | 0:cdf462088d13 | 96 | #endif |
markrad | 0:cdf462088d13 | 97 | |
markrad | 0:cdf462088d13 | 98 | |
markrad | 0:cdf462088d13 | 99 | /*----------------------------------------------------------------------------*/ |
markrad | 0:cdf462088d13 | 100 | /* Global variables */ |
markrad | 0:cdf462088d13 | 101 | |
markrad | 0:cdf462088d13 | 102 | static int test_errors = 0; |
markrad | 0:cdf462088d13 | 103 | |
markrad | 0:cdf462088d13 | 104 | |
markrad | 0:cdf462088d13 | 105 | /*----------------------------------------------------------------------------*/ |
markrad | 0:cdf462088d13 | 106 | /* Helper Functions */ |
markrad | 0:cdf462088d13 | 107 | |
markrad | 0:cdf462088d13 | 108 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
markrad | 0:cdf462088d13 | 109 | static int redirect_output( FILE** out_stream, const char* path ) |
markrad | 0:cdf462088d13 | 110 | { |
markrad | 0:cdf462088d13 | 111 | int stdout_fd = dup( fileno( *out_stream ) ); |
markrad | 0:cdf462088d13 | 112 | |
markrad | 0:cdf462088d13 | 113 | if( stdout_fd == -1 ) |
markrad | 0:cdf462088d13 | 114 | { |
markrad | 0:cdf462088d13 | 115 | return -1; |
markrad | 0:cdf462088d13 | 116 | } |
markrad | 0:cdf462088d13 | 117 | |
markrad | 0:cdf462088d13 | 118 | fflush( *out_stream ); |
markrad | 0:cdf462088d13 | 119 | fclose( *out_stream ); |
markrad | 0:cdf462088d13 | 120 | *out_stream = fopen( path, "w" ); |
markrad | 0:cdf462088d13 | 121 | |
markrad | 0:cdf462088d13 | 122 | if( *out_stream == NULL ) |
markrad | 0:cdf462088d13 | 123 | { |
markrad | 0:cdf462088d13 | 124 | return -1; |
markrad | 0:cdf462088d13 | 125 | } |
markrad | 0:cdf462088d13 | 126 | |
markrad | 0:cdf462088d13 | 127 | return stdout_fd; |
markrad | 0:cdf462088d13 | 128 | } |
markrad | 0:cdf462088d13 | 129 | |
markrad | 0:cdf462088d13 | 130 | static int restore_output( FILE** out_stream, int old_fd ) |
markrad | 0:cdf462088d13 | 131 | { |
markrad | 0:cdf462088d13 | 132 | fflush( *out_stream ); |
markrad | 0:cdf462088d13 | 133 | fclose( *out_stream ); |
markrad | 0:cdf462088d13 | 134 | |
markrad | 0:cdf462088d13 | 135 | *out_stream = fdopen( old_fd, "w" ); |
markrad | 0:cdf462088d13 | 136 | if( *out_stream == NULL ) |
markrad | 0:cdf462088d13 | 137 | { |
markrad | 0:cdf462088d13 | 138 | return -1; |
markrad | 0:cdf462088d13 | 139 | } |
markrad | 0:cdf462088d13 | 140 | |
markrad | 0:cdf462088d13 | 141 | return 0; |
markrad | 0:cdf462088d13 | 142 | } |
markrad | 0:cdf462088d13 | 143 | |
markrad | 0:cdf462088d13 | 144 | static void close_output( FILE* out_stream ) |
markrad | 0:cdf462088d13 | 145 | { |
markrad | 0:cdf462088d13 | 146 | fclose( out_stream ); |
markrad | 0:cdf462088d13 | 147 | } |
markrad | 0:cdf462088d13 | 148 | #endif /* __unix__ || __APPLE__ __MACH__ */ |
markrad | 0:cdf462088d13 | 149 | |
markrad | 0:cdf462088d13 | 150 | static int unhexify( unsigned char *obuf, const char *ibuf ) |
markrad | 0:cdf462088d13 | 151 | { |
markrad | 0:cdf462088d13 | 152 | unsigned char c, c2; |
markrad | 0:cdf462088d13 | 153 | int len = strlen( ibuf ) / 2; |
markrad | 0:cdf462088d13 | 154 | assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */ |
markrad | 0:cdf462088d13 | 155 | |
markrad | 0:cdf462088d13 | 156 | while( *ibuf != 0 ) |
markrad | 0:cdf462088d13 | 157 | { |
markrad | 0:cdf462088d13 | 158 | c = *ibuf++; |
markrad | 0:cdf462088d13 | 159 | if( c >= '0' && c <= '9' ) |
markrad | 0:cdf462088d13 | 160 | c -= '0'; |
markrad | 0:cdf462088d13 | 161 | else if( c >= 'a' && c <= 'f' ) |
markrad | 0:cdf462088d13 | 162 | c -= 'a' - 10; |
markrad | 0:cdf462088d13 | 163 | else if( c >= 'A' && c <= 'F' ) |
markrad | 0:cdf462088d13 | 164 | c -= 'A' - 10; |
markrad | 0:cdf462088d13 | 165 | else |
markrad | 0:cdf462088d13 | 166 | assert( 0 ); |
markrad | 0:cdf462088d13 | 167 | |
markrad | 0:cdf462088d13 | 168 | c2 = *ibuf++; |
markrad | 0:cdf462088d13 | 169 | if( c2 >= '0' && c2 <= '9' ) |
markrad | 0:cdf462088d13 | 170 | c2 -= '0'; |
markrad | 0:cdf462088d13 | 171 | else if( c2 >= 'a' && c2 <= 'f' ) |
markrad | 0:cdf462088d13 | 172 | c2 -= 'a' - 10; |
markrad | 0:cdf462088d13 | 173 | else if( c2 >= 'A' && c2 <= 'F' ) |
markrad | 0:cdf462088d13 | 174 | c2 -= 'A' - 10; |
markrad | 0:cdf462088d13 | 175 | else |
markrad | 0:cdf462088d13 | 176 | assert( 0 ); |
markrad | 0:cdf462088d13 | 177 | |
markrad | 0:cdf462088d13 | 178 | *obuf++ = ( c << 4 ) | c2; |
markrad | 0:cdf462088d13 | 179 | } |
markrad | 0:cdf462088d13 | 180 | |
markrad | 0:cdf462088d13 | 181 | return len; |
markrad | 0:cdf462088d13 | 182 | } |
markrad | 0:cdf462088d13 | 183 | |
markrad | 0:cdf462088d13 | 184 | static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len ) |
markrad | 0:cdf462088d13 | 185 | { |
markrad | 0:cdf462088d13 | 186 | unsigned char l, h; |
markrad | 0:cdf462088d13 | 187 | |
markrad | 0:cdf462088d13 | 188 | while( len != 0 ) |
markrad | 0:cdf462088d13 | 189 | { |
markrad | 0:cdf462088d13 | 190 | h = *ibuf / 16; |
markrad | 0:cdf462088d13 | 191 | l = *ibuf % 16; |
markrad | 0:cdf462088d13 | 192 | |
markrad | 0:cdf462088d13 | 193 | if( h < 10 ) |
markrad | 0:cdf462088d13 | 194 | *obuf++ = '0' + h; |
markrad | 0:cdf462088d13 | 195 | else |
markrad | 0:cdf462088d13 | 196 | *obuf++ = 'a' + h - 10; |
markrad | 0:cdf462088d13 | 197 | |
markrad | 0:cdf462088d13 | 198 | if( l < 10 ) |
markrad | 0:cdf462088d13 | 199 | *obuf++ = '0' + l; |
markrad | 0:cdf462088d13 | 200 | else |
markrad | 0:cdf462088d13 | 201 | *obuf++ = 'a' + l - 10; |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | ++ibuf; |
markrad | 0:cdf462088d13 | 204 | len--; |
markrad | 0:cdf462088d13 | 205 | } |
markrad | 0:cdf462088d13 | 206 | } |
markrad | 0:cdf462088d13 | 207 | |
markrad | 0:cdf462088d13 | 208 | /** |
markrad | 0:cdf462088d13 | 209 | * Allocate and zeroize a buffer. |
markrad | 0:cdf462088d13 | 210 | * |
markrad | 0:cdf462088d13 | 211 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
markrad | 0:cdf462088d13 | 212 | * |
markrad | 0:cdf462088d13 | 213 | * For convenience, dies if allocation fails. |
markrad | 0:cdf462088d13 | 214 | */ |
markrad | 0:cdf462088d13 | 215 | static unsigned char *zero_alloc( size_t len ) |
markrad | 0:cdf462088d13 | 216 | { |
markrad | 0:cdf462088d13 | 217 | void *p; |
markrad | 0:cdf462088d13 | 218 | size_t actual_len = ( len != 0 ) ? len : 1; |
markrad | 0:cdf462088d13 | 219 | |
markrad | 0:cdf462088d13 | 220 | p = mbedtls_calloc( 1, actual_len ); |
markrad | 0:cdf462088d13 | 221 | assert( p != NULL ); |
markrad | 0:cdf462088d13 | 222 | |
markrad | 0:cdf462088d13 | 223 | memset( p, 0x00, actual_len ); |
markrad | 0:cdf462088d13 | 224 | |
markrad | 0:cdf462088d13 | 225 | return( p ); |
markrad | 0:cdf462088d13 | 226 | } |
markrad | 0:cdf462088d13 | 227 | |
markrad | 0:cdf462088d13 | 228 | /** |
markrad | 0:cdf462088d13 | 229 | * Allocate and fill a buffer from hex data. |
markrad | 0:cdf462088d13 | 230 | * |
markrad | 0:cdf462088d13 | 231 | * The buffer is sized exactly as needed. This allows to detect buffer |
markrad | 0:cdf462088d13 | 232 | * overruns (including overreads) when running the test suite under valgrind. |
markrad | 0:cdf462088d13 | 233 | * |
markrad | 0:cdf462088d13 | 234 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
markrad | 0:cdf462088d13 | 235 | * |
markrad | 0:cdf462088d13 | 236 | * For convenience, dies if allocation fails. |
markrad | 0:cdf462088d13 | 237 | */ |
markrad | 0:cdf462088d13 | 238 | static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen ) |
markrad | 0:cdf462088d13 | 239 | { |
markrad | 0:cdf462088d13 | 240 | unsigned char *obuf; |
markrad | 0:cdf462088d13 | 241 | |
markrad | 0:cdf462088d13 | 242 | *olen = strlen( ibuf ) / 2; |
markrad | 0:cdf462088d13 | 243 | |
markrad | 0:cdf462088d13 | 244 | if( *olen == 0 ) |
markrad | 0:cdf462088d13 | 245 | return( zero_alloc( *olen ) ); |
markrad | 0:cdf462088d13 | 246 | |
markrad | 0:cdf462088d13 | 247 | obuf = mbedtls_calloc( 1, *olen ); |
markrad | 0:cdf462088d13 | 248 | assert( obuf != NULL ); |
markrad | 0:cdf462088d13 | 249 | |
markrad | 0:cdf462088d13 | 250 | (void) unhexify( obuf, ibuf ); |
markrad | 0:cdf462088d13 | 251 | |
markrad | 0:cdf462088d13 | 252 | return( obuf ); |
markrad | 0:cdf462088d13 | 253 | } |
markrad | 0:cdf462088d13 | 254 | |
markrad | 0:cdf462088d13 | 255 | /** |
markrad | 0:cdf462088d13 | 256 | * This function just returns data from rand(). |
markrad | 0:cdf462088d13 | 257 | * Although predictable and often similar on multiple |
markrad | 0:cdf462088d13 | 258 | * runs, this does not result in identical random on |
markrad | 0:cdf462088d13 | 259 | * each run. So do not use this if the results of a |
markrad | 0:cdf462088d13 | 260 | * test depend on the random data that is generated. |
markrad | 0:cdf462088d13 | 261 | * |
markrad | 0:cdf462088d13 | 262 | * rng_state shall be NULL. |
markrad | 0:cdf462088d13 | 263 | */ |
markrad | 0:cdf462088d13 | 264 | static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len ) |
markrad | 0:cdf462088d13 | 265 | { |
markrad | 0:cdf462088d13 | 266 | #if !defined(__OpenBSD__) |
markrad | 0:cdf462088d13 | 267 | size_t i; |
markrad | 0:cdf462088d13 | 268 | |
markrad | 0:cdf462088d13 | 269 | if( rng_state != NULL ) |
markrad | 0:cdf462088d13 | 270 | rng_state = NULL; |
markrad | 0:cdf462088d13 | 271 | |
markrad | 0:cdf462088d13 | 272 | for( i = 0; i < len; ++i ) |
markrad | 0:cdf462088d13 | 273 | output[i] = rand(); |
markrad | 0:cdf462088d13 | 274 | #else |
markrad | 0:cdf462088d13 | 275 | if( rng_state != NULL ) |
markrad | 0:cdf462088d13 | 276 | rng_state = NULL; |
markrad | 0:cdf462088d13 | 277 | |
markrad | 0:cdf462088d13 | 278 | arc4random_buf( output, len ); |
markrad | 0:cdf462088d13 | 279 | #endif /* !OpenBSD */ |
markrad | 0:cdf462088d13 | 280 | |
markrad | 0:cdf462088d13 | 281 | return( 0 ); |
markrad | 0:cdf462088d13 | 282 | } |
markrad | 0:cdf462088d13 | 283 | |
markrad | 0:cdf462088d13 | 284 | /** |
markrad | 0:cdf462088d13 | 285 | * This function only returns zeros |
markrad | 0:cdf462088d13 | 286 | * |
markrad | 0:cdf462088d13 | 287 | * rng_state shall be NULL. |
markrad | 0:cdf462088d13 | 288 | */ |
markrad | 0:cdf462088d13 | 289 | static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len ) |
markrad | 0:cdf462088d13 | 290 | { |
markrad | 0:cdf462088d13 | 291 | if( rng_state != NULL ) |
markrad | 0:cdf462088d13 | 292 | rng_state = NULL; |
markrad | 0:cdf462088d13 | 293 | |
markrad | 0:cdf462088d13 | 294 | memset( output, 0, len ); |
markrad | 0:cdf462088d13 | 295 | |
markrad | 0:cdf462088d13 | 296 | return( 0 ); |
markrad | 0:cdf462088d13 | 297 | } |
markrad | 0:cdf462088d13 | 298 | |
markrad | 0:cdf462088d13 | 299 | typedef struct |
markrad | 0:cdf462088d13 | 300 | { |
markrad | 0:cdf462088d13 | 301 | unsigned char *buf; |
markrad | 0:cdf462088d13 | 302 | size_t length; |
markrad | 0:cdf462088d13 | 303 | } rnd_buf_info; |
markrad | 0:cdf462088d13 | 304 | |
markrad | 0:cdf462088d13 | 305 | /** |
markrad | 0:cdf462088d13 | 306 | * This function returns random based on a buffer it receives. |
markrad | 0:cdf462088d13 | 307 | * |
markrad | 0:cdf462088d13 | 308 | * rng_state shall be a pointer to a rnd_buf_info structure. |
markrad | 0:cdf462088d13 | 309 | * |
markrad | 0:cdf462088d13 | 310 | * The number of bytes released from the buffer on each call to |
markrad | 0:cdf462088d13 | 311 | * the random function is specified by per_call. (Can be between |
markrad | 0:cdf462088d13 | 312 | * 1 and 4) |
markrad | 0:cdf462088d13 | 313 | * |
markrad | 0:cdf462088d13 | 314 | * After the buffer is empty it will return rand(); |
markrad | 0:cdf462088d13 | 315 | */ |
markrad | 0:cdf462088d13 | 316 | static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len ) |
markrad | 0:cdf462088d13 | 317 | { |
markrad | 0:cdf462088d13 | 318 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
markrad | 0:cdf462088d13 | 319 | size_t use_len; |
markrad | 0:cdf462088d13 | 320 | |
markrad | 0:cdf462088d13 | 321 | if( rng_state == NULL ) |
markrad | 0:cdf462088d13 | 322 | return( rnd_std_rand( NULL, output, len ) ); |
markrad | 0:cdf462088d13 | 323 | |
markrad | 0:cdf462088d13 | 324 | use_len = len; |
markrad | 0:cdf462088d13 | 325 | if( len > info->length ) |
markrad | 0:cdf462088d13 | 326 | use_len = info->length; |
markrad | 0:cdf462088d13 | 327 | |
markrad | 0:cdf462088d13 | 328 | if( use_len ) |
markrad | 0:cdf462088d13 | 329 | { |
markrad | 0:cdf462088d13 | 330 | memcpy( output, info->buf, use_len ); |
markrad | 0:cdf462088d13 | 331 | info->buf += use_len; |
markrad | 0:cdf462088d13 | 332 | info->length -= use_len; |
markrad | 0:cdf462088d13 | 333 | } |
markrad | 0:cdf462088d13 | 334 | |
markrad | 0:cdf462088d13 | 335 | if( len - use_len > 0 ) |
markrad | 0:cdf462088d13 | 336 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); |
markrad | 0:cdf462088d13 | 337 | |
markrad | 0:cdf462088d13 | 338 | return( 0 ); |
markrad | 0:cdf462088d13 | 339 | } |
markrad | 0:cdf462088d13 | 340 | |
markrad | 0:cdf462088d13 | 341 | /** |
markrad | 0:cdf462088d13 | 342 | * Info structure for the pseudo random function |
markrad | 0:cdf462088d13 | 343 | * |
markrad | 0:cdf462088d13 | 344 | * Key should be set at the start to a test-unique value. |
markrad | 0:cdf462088d13 | 345 | * Do not forget endianness! |
markrad | 0:cdf462088d13 | 346 | * State( v0, v1 ) should be set to zero. |
markrad | 0:cdf462088d13 | 347 | */ |
markrad | 0:cdf462088d13 | 348 | typedef struct |
markrad | 0:cdf462088d13 | 349 | { |
markrad | 0:cdf462088d13 | 350 | uint32_t key[16]; |
markrad | 0:cdf462088d13 | 351 | uint32_t v0, v1; |
markrad | 0:cdf462088d13 | 352 | } rnd_pseudo_info; |
markrad | 0:cdf462088d13 | 353 | |
markrad | 0:cdf462088d13 | 354 | /** |
markrad | 0:cdf462088d13 | 355 | * This function returns random based on a pseudo random function. |
markrad | 0:cdf462088d13 | 356 | * This means the results should be identical on all systems. |
markrad | 0:cdf462088d13 | 357 | * Pseudo random is based on the XTEA encryption algorithm to |
markrad | 0:cdf462088d13 | 358 | * generate pseudorandom. |
markrad | 0:cdf462088d13 | 359 | * |
markrad | 0:cdf462088d13 | 360 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
markrad | 0:cdf462088d13 | 361 | */ |
markrad | 0:cdf462088d13 | 362 | static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len ) |
markrad | 0:cdf462088d13 | 363 | { |
markrad | 0:cdf462088d13 | 364 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
markrad | 0:cdf462088d13 | 365 | uint32_t i, *k, sum, delta=0x9E3779B9; |
markrad | 0:cdf462088d13 | 366 | unsigned char result[4], *out = output; |
markrad | 0:cdf462088d13 | 367 | |
markrad | 0:cdf462088d13 | 368 | if( rng_state == NULL ) |
markrad | 0:cdf462088d13 | 369 | return( rnd_std_rand( NULL, output, len ) ); |
markrad | 0:cdf462088d13 | 370 | |
markrad | 0:cdf462088d13 | 371 | k = info->key; |
markrad | 0:cdf462088d13 | 372 | |
markrad | 0:cdf462088d13 | 373 | while( len > 0 ) |
markrad | 0:cdf462088d13 | 374 | { |
markrad | 0:cdf462088d13 | 375 | size_t use_len = ( len > 4 ) ? 4 : len; |
markrad | 0:cdf462088d13 | 376 | sum = 0; |
markrad | 0:cdf462088d13 | 377 | |
markrad | 0:cdf462088d13 | 378 | for( i = 0; i < 32; i++ ) |
markrad | 0:cdf462088d13 | 379 | { |
markrad | 0:cdf462088d13 | 380 | info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) ) |
markrad | 0:cdf462088d13 | 381 | + info->v1 ) ^ ( sum + k[sum & 3] ); |
markrad | 0:cdf462088d13 | 382 | sum += delta; |
markrad | 0:cdf462088d13 | 383 | info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) ) |
markrad | 0:cdf462088d13 | 384 | + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] ); |
markrad | 0:cdf462088d13 | 385 | } |
markrad | 0:cdf462088d13 | 386 | |
markrad | 0:cdf462088d13 | 387 | PUT_UINT32_BE( info->v0, result, 0 ); |
markrad | 0:cdf462088d13 | 388 | memcpy( out, result, use_len ); |
markrad | 0:cdf462088d13 | 389 | len -= use_len; |
markrad | 0:cdf462088d13 | 390 | out += 4; |
markrad | 0:cdf462088d13 | 391 | } |
markrad | 0:cdf462088d13 | 392 | |
markrad | 0:cdf462088d13 | 393 | return( 0 ); |
markrad | 0:cdf462088d13 | 394 | } |
markrad | 0:cdf462088d13 | 395 | |
markrad | 0:cdf462088d13 | 396 | static void test_fail( const char *test, int line_no, const char* filename ) |
markrad | 0:cdf462088d13 | 397 | { |
markrad | 0:cdf462088d13 | 398 | test_errors++; |
markrad | 0:cdf462088d13 | 399 | if( test_errors == 1 ) |
markrad | 0:cdf462088d13 | 400 | mbedtls_fprintf( stdout, "FAILED\n" ); |
markrad | 0:cdf462088d13 | 401 | mbedtls_fprintf( stdout, " %s\n at line %d, %s\n", test, line_no, |
markrad | 0:cdf462088d13 | 402 | filename ); |
markrad | 0:cdf462088d13 | 403 | } |
markrad | 0:cdf462088d13 | 404 |