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