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