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