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