| Paul Bakker | e07c431 | 2013-07-03 14:00:49 +0200 | [diff] [blame] | 1 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) | 
|  | 2 | #include "polarssl/memory.h" | 
|  | 3 | #endif | 
|  | 4 |  | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 5 | #ifdef _MSC_VER | 
|  | 6 | #include <basetsd.h> | 
|  | 7 | typedef UINT32 uint32_t; | 
|  | 8 | #else | 
|  | 9 | #include <inttypes.h> | 
|  | 10 | #endif | 
|  | 11 |  | 
|  | 12 | /* | 
|  | 13 | * 32-bit integer manipulation macros (big endian) | 
|  | 14 | */ | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 15 | #ifndef GET_UINT32_BE | 
|  | 16 | #define GET_UINT32_BE(n,b,i)                            \ | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 17 | {                                                       \ | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 18 | (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \ | 
|  | 19 | | ( (uint32_t) (b)[(i) + 1] << 16 )             \ | 
|  | 20 | | ( (uint32_t) (b)[(i) + 2] <<  8 )             \ | 
|  | 21 | | ( (uint32_t) (b)[(i) + 3]       );            \ | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 22 | } | 
|  | 23 | #endif | 
|  | 24 |  | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 25 | #ifndef PUT_UINT32_BE | 
|  | 26 | #define PUT_UINT32_BE(n,b,i)                            \ | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 27 | {                                                       \ | 
|  | 28 | (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \ | 
|  | 29 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \ | 
|  | 30 | (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \ | 
|  | 31 | (b)[(i) + 3] = (unsigned char) ( (n)       );       \ | 
|  | 32 | } | 
|  | 33 | #endif | 
|  | 34 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 35 | static int unhexify(unsigned char *obuf, const char *ibuf) | 
| Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 36 | { | 
|  | 37 | unsigned char c, c2; | 
|  | 38 | int len = strlen(ibuf) / 2; | 
|  | 39 | assert(!(strlen(ibuf) %1)); // must be even number of bytes | 
|  | 40 |  | 
|  | 41 | while (*ibuf != 0) | 
|  | 42 | { | 
|  | 43 | c = *ibuf++; | 
|  | 44 | if( c >= '0' && c <= '9' ) | 
|  | 45 | c -= '0'; | 
|  | 46 | else if( c >= 'a' && c <= 'f' ) | 
|  | 47 | c -= 'a' - 10; | 
|  | 48 | else if( c >= 'A' && c <= 'F' ) | 
|  | 49 | c -= 'A' - 10; | 
|  | 50 | else | 
|  | 51 | assert( 0 ); | 
|  | 52 |  | 
|  | 53 | c2 = *ibuf++; | 
|  | 54 | if( c2 >= '0' && c2 <= '9' ) | 
|  | 55 | c2 -= '0'; | 
|  | 56 | else if( c2 >= 'a' && c2 <= 'f' ) | 
|  | 57 | c2 -= 'a' - 10; | 
|  | 58 | else if( c2 >= 'A' && c2 <= 'F' ) | 
|  | 59 | c2 -= 'A' - 10; | 
|  | 60 | else | 
|  | 61 | assert( 0 ); | 
|  | 62 |  | 
|  | 63 | *obuf++ = ( c << 4 ) | c2; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | return len; | 
|  | 67 | } | 
|  | 68 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 69 | static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len) | 
| Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 70 | { | 
|  | 71 | unsigned char l, h; | 
|  | 72 |  | 
|  | 73 | while (len != 0) | 
|  | 74 | { | 
|  | 75 | h = (*ibuf) / 16; | 
|  | 76 | l = (*ibuf) % 16; | 
|  | 77 |  | 
|  | 78 | if( h < 10 ) | 
|  | 79 | *obuf++ = '0' + h; | 
|  | 80 | else | 
|  | 81 | *obuf++ = 'a' + h - 10; | 
|  | 82 |  | 
|  | 83 | if( l < 10 ) | 
|  | 84 | *obuf++ = '0' + l; | 
|  | 85 | else | 
|  | 86 | *obuf++ = 'a' + l - 10; | 
|  | 87 |  | 
|  | 88 | ++ibuf; | 
|  | 89 | len--; | 
|  | 90 | } | 
|  | 91 | } | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 92 |  | 
|  | 93 | /** | 
|  | 94 | * This function just returns data from rand(). | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 95 | * Although predictable and often similar on multiple | 
|  | 96 | * runs, this does not result in identical random on | 
|  | 97 | * each run. So do not use this if the results of a | 
|  | 98 | * test depend on the random data that is generated. | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 99 | * | 
|  | 100 | * rng_state shall be NULL. | 
|  | 101 | */ | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 102 | 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] | 103 | { | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 104 | size_t i; | 
|  | 105 |  | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 106 | if( rng_state != NULL ) | 
|  | 107 | rng_state  = NULL; | 
|  | 108 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 109 | for( i = 0; i < len; ++i ) | 
|  | 110 | output[i] = rand(); | 
|  | 111 |  | 
|  | 112 | return( 0 ); | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
|  | 115 | /** | 
|  | 116 | * This function only returns zeros | 
|  | 117 | * | 
|  | 118 | * rng_state shall be NULL. | 
|  | 119 | */ | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 120 | static 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] | 121 | { | 
|  | 122 | if( rng_state != NULL ) | 
|  | 123 | rng_state  = NULL; | 
|  | 124 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 125 | memset( output, 0, len ); | 
|  | 126 |  | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 127 | return( 0 ); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | typedef struct | 
|  | 131 | { | 
|  | 132 | unsigned char *buf; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 133 | size_t length; | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 134 | } rnd_buf_info; | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 135 |  | 
|  | 136 | /** | 
|  | 137 | * This function returns random based on a buffer it receives. | 
|  | 138 | * | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 139 | * rng_state shall be a pointer to a rnd_buf_info structure. | 
|  | 140 | * | 
|  | 141 | * The number of bytes released from the buffer on each call to | 
|  | 142 | * the random function is specified by per_call. (Can be between | 
|  | 143 | * 1 and 4) | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 144 | * | 
|  | 145 | * After the buffer is empty it will return rand(); | 
|  | 146 | */ | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 147 | static 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] | 148 | { | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 149 | rnd_buf_info *info = (rnd_buf_info *) rng_state; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 150 | size_t use_len; | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 151 |  | 
|  | 152 | if( rng_state == NULL ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 153 | return( rnd_std_rand( NULL, output, len ) ); | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 154 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 155 | use_len = len; | 
|  | 156 | if( len > info->length ) | 
|  | 157 | use_len = info->length; | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 158 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 159 | if( use_len ) | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 160 | { | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 161 | memcpy( output, info->buf, use_len ); | 
|  | 162 | info->buf += use_len; | 
|  | 163 | info->length -= use_len; | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 166 | if( len - use_len > 0 ) | 
|  | 167 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); | 
|  | 168 |  | 
|  | 169 | return( 0 ); | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 170 | } | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 171 |  | 
|  | 172 | /** | 
|  | 173 | * Info structure for the pseudo random function | 
|  | 174 | * | 
|  | 175 | * Key should be set at the start to a test-unique value. | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 176 | * Do not forget endianness! | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 177 | * State( v0, v1 ) should be set to zero. | 
|  | 178 | */ | 
|  | 179 | typedef struct | 
|  | 180 | { | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 181 | uint32_t key[16]; | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 182 | uint32_t v0, v1; | 
|  | 183 | } rnd_pseudo_info; | 
|  | 184 |  | 
|  | 185 | /** | 
|  | 186 | * This function returns random based on a pseudo random function. | 
|  | 187 | * This means the results should be identical on all systems. | 
|  | 188 | * Pseudo random is based on the XTEA encryption algorithm to | 
|  | 189 | * generate pseudorandom. | 
|  | 190 | * | 
|  | 191 | * rng_state shall be a pointer to a rnd_pseudo_info structure. | 
|  | 192 | */ | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 193 | static 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] | 194 | { | 
|  | 195 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 196 | uint32_t i, *k, sum, delta=0x9E3779B9; | 
| Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 197 | unsigned char result[4]; | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 198 |  | 
|  | 199 | if( rng_state == NULL ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 200 | return( rnd_std_rand( NULL, output, len ) ); | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 201 |  | 
| Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 202 | k = info->key; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 203 |  | 
|  | 204 | while( len > 0 ) | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 205 | { | 
| Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 206 | size_t use_len = ( len > 4 ) ? 4 : len; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 207 | sum = 0; | 
|  | 208 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 209 | for( i = 0; i < 32; i++ ) | 
|  | 210 | { | 
|  | 211 | info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]); | 
|  | 212 | sum += delta; | 
|  | 213 | info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]); | 
|  | 214 | } | 
|  | 215 |  | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 216 | PUT_UINT32_BE( info->v0, result, 0 ); | 
| Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 217 | memcpy( output, result, use_len ); | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 218 | len -= use_len; | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 221 | return( 0 ); | 
| Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 222 | } | 
| Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 223 |  | 
|  | 224 | /** | 
|  | 225 | * This function returns a buffer given as a hex string. | 
|  | 226 | * | 
|  | 227 | * The buffer is reversed so that the following are equivalent: | 
|  | 228 | *   mpi_fill_random( x, len, not_rnd, str ); | 
|  | 229 | *   mpi_read_string( x, 16, str ); | 
|  | 230 | * (So, not random at all. Usefull to match test vectors.) | 
|  | 231 | * Based on unhexify(), just reversed (changes marked by "sic") | 
|  | 232 | */ | 
|  | 233 | static int not_rnd( void *in, unsigned char *out, size_t len ) | 
|  | 234 | { | 
|  | 235 | unsigned char *obuf; | 
|  | 236 | const char *ibuf = in; | 
|  | 237 | unsigned char c, c2; | 
|  | 238 | assert( len == strlen(ibuf) / 2 ); | 
|  | 239 | assert(!(strlen(ibuf) %1)); // must be even number of bytes | 
|  | 240 |  | 
|  | 241 | obuf = out + (len - 1); // sic | 
|  | 242 | while (*ibuf != 0) | 
|  | 243 | { | 
|  | 244 | c = *ibuf++; | 
|  | 245 | if( c >= '0' && c <= '9' ) | 
|  | 246 | c -= '0'; | 
|  | 247 | else if( c >= 'a' && c <= 'f' ) | 
|  | 248 | c -= 'a' - 10; | 
|  | 249 | else if( c >= 'A' && c <= 'F' ) | 
|  | 250 | c -= 'A' - 10; | 
|  | 251 | else | 
|  | 252 | assert( 0 ); | 
|  | 253 |  | 
|  | 254 | c2 = *ibuf++; | 
|  | 255 | if( c2 >= '0' && c2 <= '9' ) | 
|  | 256 | c2 -= '0'; | 
|  | 257 | else if( c2 >= 'a' && c2 <= 'f' ) | 
|  | 258 | c2 -= 'a' - 10; | 
|  | 259 | else if( c2 >= 'A' && c2 <= 'F' ) | 
|  | 260 | c2 -= 'A' - 10; | 
|  | 261 | else | 
|  | 262 | assert( 0 ); | 
|  | 263 |  | 
|  | 264 | *obuf-- = ( c << 4 ) | c2; // sic | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | return( 0 ); | 
|  | 268 | } |