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 | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 31 | int unhexify(unsigned char *obuf, const char *ibuf) |
| 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 | |
| 65 | void hexify(unsigned char *obuf, const unsigned char *ibuf, int len) |
| 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 | 95a11f8 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 100 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 101 | size_t i; |
| 102 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 103 | if( rng_state != NULL ) |
| 104 | rng_state = NULL; |
| 105 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 106 | for( i = 0; i < len; ++i ) |
| 107 | output[i] = rand(); |
Paul Bakker | 95a11f8 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 108 | #else |
| 109 | if( rng_state != NULL ) |
| 110 | rng_state = NULL; |
| 111 | |
| 112 | arc4random_buf( output, len ); |
| 113 | #endif /* !OpenBSD */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 114 | |
| 115 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * This function only returns zeros |
| 120 | * |
| 121 | * rng_state shall be NULL. |
| 122 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 123 | 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] | 124 | { |
| 125 | if( rng_state != NULL ) |
| 126 | rng_state = NULL; |
| 127 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 128 | memset( output, 0, len ); |
| 129 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 130 | return( 0 ); |
| 131 | } |
| 132 | |
| 133 | typedef struct |
| 134 | { |
| 135 | unsigned char *buf; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 136 | size_t length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 137 | } rnd_buf_info; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * This function returns random based on a buffer it receives. |
| 141 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 142 | * rng_state shall be a pointer to a rnd_buf_info structure. |
| 143 | * |
| 144 | * The number of bytes released from the buffer on each call to |
| 145 | * the random function is specified by per_call. (Can be between |
| 146 | * 1 and 4) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 147 | * |
| 148 | * After the buffer is empty it will return rand(); |
| 149 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 150 | 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] | 151 | { |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 152 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 153 | size_t use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 154 | |
| 155 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 156 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 157 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 158 | use_len = len; |
| 159 | if( len > info->length ) |
| 160 | use_len = info->length; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 161 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 162 | if( use_len ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 163 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 164 | memcpy( output, info->buf, use_len ); |
| 165 | info->buf += use_len; |
| 166 | info->length -= use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 169 | if( len - use_len > 0 ) |
| 170 | return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); |
| 171 | |
| 172 | return( 0 ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 173 | } |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Info structure for the pseudo random function |
| 177 | * |
| 178 | * Key should be set at the start to a test-unique value. |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 179 | * Do not forget endianness! |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 180 | * State( v0, v1 ) should be set to zero. |
| 181 | */ |
| 182 | typedef struct |
| 183 | { |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 184 | uint32_t key[16]; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 185 | uint32_t v0, v1; |
| 186 | } rnd_pseudo_info; |
| 187 | |
| 188 | /** |
| 189 | * This function returns random based on a pseudo random function. |
| 190 | * This means the results should be identical on all systems. |
| 191 | * Pseudo random is based on the XTEA encryption algorithm to |
| 192 | * generate pseudorandom. |
| 193 | * |
| 194 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
| 195 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 196 | 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] | 197 | { |
| 198 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 199 | uint32_t i, *k, sum, delta=0x9E3779B9; |
Manuel Pégourié-Gonnard | ec8f2ff | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 200 | unsigned char result[4], *out = output; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 201 | |
| 202 | if( rng_state == NULL ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 203 | return( rnd_std_rand( NULL, output, len ) ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 204 | |
Paul Bakker | b3dcbc1 | 2011-03-13 16:57:25 +0000 | [diff] [blame] | 205 | k = info->key; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 206 | |
| 207 | while( len > 0 ) |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 208 | { |
Paul Bakker | 40dd530 | 2012-05-15 15:02:38 +0000 | [diff] [blame] | 209 | size_t use_len = ( len > 4 ) ? 4 : len; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 210 | sum = 0; |
| 211 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 212 | for( i = 0; i < 32; i++ ) |
| 213 | { |
| 214 | info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]); |
| 215 | sum += delta; |
| 216 | info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]); |
| 217 | } |
| 218 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 219 | PUT_UINT32_BE( info->v0, result, 0 ); |
Manuel Pégourié-Gonnard | ec8f2ff | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 220 | memcpy( out, result, use_len ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 221 | len -= use_len; |
Manuel Pégourié-Gonnard | ec8f2ff | 2014-01-03 11:59:09 +0100 | [diff] [blame] | 222 | out += 4; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 225 | return( 0 ); |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 226 | } |