| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <stdarg.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include "os_port.h" |
| 40 |
#include "crypto_misc.h" |
| 41 |
#ifdef CONFIG_WIN32_USE_CRYPTO_LIB |
| 42 |
#include "wincrypt.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifndef WIN32 |
| 46 |
static int rng_fd = -1; |
| 47 |
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB) |
| 48 |
static HCRYPTPROV gCryptProv; |
| 49 |
#endif |
| 50 |
|
| 51 |
#if (!defined(CONFIG_USE_DEV_URANDOM) && !defined(CONFIG_WIN32_USE_CRYPTO_LIB)) |
| 52 |
|
| 53 |
#define ENTROPY_POOL_SIZE 32 |
| 54 |
#define ENTROPY_COUNTER1 ((((uint64_t)tv.tv_sec)<<32) | tv.tv_usec) |
| 55 |
#define ENTROPY_COUNTER2 rand() |
| 56 |
static uint8_t entropy_pool[ENTROPY_POOL_SIZE]; |
| 57 |
#endif |
| 58 |
|
| 59 |
const char * const unsupported_str = "Error: Feature not supported\n"; |
| 60 |
|
| 61 |
#ifndef CONFIG_SSL_SKELETON_MODE |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
int get_file(const char *filename, uint8_t **buf) |
| 67 |
{ |
| 68 |
int total_bytes = 0; |
| 69 |
int bytes_read = 0; |
| 70 |
int filesize; |
| 71 |
FILE *stream = fopen(filename, "rb"); |
| 72 |
|
| 73 |
if (stream == NULL) |
| 74 |
{ |
| 75 |
#ifdef CONFIG_DEBUG |
| 76 |
printf("file '%s' does not exist\n", filename); TTY_FLUSH(); |
| 77 |
#endif |
| 78 |
return -1; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
fseek(stream, 0, SEEK_END); |
| 83 |
filesize = ftell(stream); |
| 84 |
*buf = (uint8_t *)malloc(filesize); |
| 85 |
fseek(stream, 0, SEEK_SET); |
| 86 |
|
| 87 |
do |
| 88 |
{ |
| 89 |
bytes_read = fread(*buf+total_bytes, 1, filesize-total_bytes, stream); |
| 90 |
total_bytes += bytes_read; |
| 91 |
} while (total_bytes < filesize && bytes_read > 0); |
| 92 |
|
| 93 |
fclose(stream); |
| 94 |
return filesize; |
| 95 |
} |
| 96 |
#endif |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
EXP_FUNC void STDCALL RNG_initialize() |
| 105 |
{ |
| 106 |
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM) |
| 107 |
rng_fd = open("/dev/urandom", O_RDONLY); |
| 108 |
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB) |
| 109 |
if (!CryptAcquireContext(&gCryptProv, |
| 110 |
NULL, NULL, PROV_RSA_FULL, 0)) |
| 111 |
{ |
| 112 |
if (GetLastError() == NTE_BAD_KEYSET && |
| 113 |
!CryptAcquireContext(&gCryptProv, |
| 114 |
NULL, |
| 115 |
NULL, |
| 116 |
PROV_RSA_FULL, |
| 117 |
CRYPT_NEWKEYSET)) |
| 118 |
{ |
| 119 |
printf("CryptoLib: %x\n", unsupported_str, GetLastError()); |
| 120 |
exit(1); |
| 121 |
} |
| 122 |
} |
| 123 |
#else |
| 124 |
|
| 125 |
|
| 126 |
int i = rand(); |
| 127 |
memset(entropy_pool, 0, ENTROPY_POOL_SIZE); |
| 128 |
memcpy(entropy_pool, &i, sizeof(int)); |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size) |
| 136 |
{ |
| 137 |
#if defined(WIN32) || defined(CONFIG_WIN32_USE_CRYPTO_LIB) |
| 138 |
int i; |
| 139 |
|
| 140 |
for (i = 0; i < ENTROPY_POOL_SIZE && i < size; i++) |
| 141 |
entropy_pool[i] ^= seed_buf[i]; |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
EXP_FUNC void STDCALL RNG_terminate(void) |
| 149 |
{ |
| 150 |
#ifndef WIN32 |
| 151 |
close(rng_fd); |
| 152 |
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB) |
| 153 |
CryptReleaseContext(gCryptProv, 0); |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data) |
| 161 |
{ |
| 162 |
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM) |
| 163 |
|
| 164 |
if (read(rng_fd, rand_data, num_rand_bytes) < 0) |
| 165 |
return -1; |
| 166 |
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB) |
| 167 |
|
| 168 |
CryptGenRandom(gCryptProv, num_rand_bytes, rand_data); |
| 169 |
#else |
| 170 |
|
| 171 |
|
| 172 |
AES_CTX rng_ctx; |
| 173 |
struct timeval tv; |
| 174 |
MD5_CTX rng_digest_ctx; |
| 175 |
uint8_t digest[MD5_SIZE]; |
| 176 |
uint64_t *ep; |
| 177 |
int i; |
| 178 |
|
| 179 |
|
| 180 |
gettimeofday(&tv, NULL); |
| 181 |
ep = (uint64_t *)entropy_pool; |
| 182 |
ep[0] ^= ENTROPY_COUNTER1; |
| 183 |
ep[1] ^= ENTROPY_COUNTER2; |
| 184 |
|
| 185 |
|
| 186 |
MD5_Init(&rng_digest_ctx); |
| 187 |
MD5_Update(&rng_digest_ctx, entropy_pool, ENTROPY_POOL_SIZE); |
| 188 |
MD5_Final(digest, &rng_digest_ctx); |
| 189 |
|
| 190 |
|
| 191 |
AES_set_key(&rng_ctx, digest, (const uint8_t *)ep, AES_MODE_128); |
| 192 |
memcpy(rand_data, entropy_pool, num_rand_bytes < ENTROPY_POOL_SIZE ? |
| 193 |
num_rand_bytes : ENTROPY_POOL_SIZE); |
| 194 |
AES_cbc_encrypt(&rng_ctx, rand_data, rand_data, num_rand_bytes); |
| 195 |
|
| 196 |
|
| 197 |
for (i = ENTROPY_POOL_SIZE-1; i >= MD5_SIZE ; i--) |
| 198 |
entropy_pool[i] = entropy_pool[i-MD5_SIZE]; |
| 199 |
|
| 200 |
|
| 201 |
memcpy(entropy_pool, digest, MD5_SIZE); |
| 202 |
#endif |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
int get_random_NZ(int num_rand_bytes, uint8_t *rand_data) |
| 210 |
{ |
| 211 |
int i; |
| 212 |
if (get_random(num_rand_bytes, rand_data)) |
| 213 |
return -1; |
| 214 |
|
| 215 |
for (i = 0; i < num_rand_bytes; i++) |
| 216 |
{ |
| 217 |
while (rand_data[i] == 0) |
| 218 |
rand_data[i] = (uint8_t)(rand()); |
| 219 |
} |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#if defined(CONFIG_DEBUG) |
| 228 |
int hex_finish; |
| 229 |
int hex_index; |
| 230 |
|
| 231 |
static void print_hex_init(int finish) |
| 232 |
{ |
| 233 |
hex_finish = finish; |
| 234 |
hex_index = 0; |
| 235 |
} |
| 236 |
|
| 237 |
static void print_hex(uint8_t hex) |
| 238 |
{ |
| 239 |
static int column; |
| 240 |
|
| 241 |
if (hex_index == 0) |
| 242 |
{ |
| 243 |
column = 0; |
| 244 |
} |
| 245 |
|
| 246 |
printf("%02x ", hex); |
| 247 |
if (++column == 8) |
| 248 |
{ |
| 249 |
printf(": "); |
| 250 |
} |
| 251 |
else if (column >= 16) |
| 252 |
{ |
| 253 |
printf("\n"); |
| 254 |
column = 0; |
| 255 |
} |
| 256 |
|
| 257 |
if (++hex_index >= hex_finish && column > 0) |
| 258 |
{ |
| 259 |
printf("\n"); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
EXP_FUNC void STDCALL print_blob(const char *format, |
| 273 |
const uint8_t *data, int size, ...) |
| 274 |
{ |
| 275 |
int i; |
| 276 |
char tmp[80]; |
| 277 |
va_list(ap); |
| 278 |
|
| 279 |
va_start(ap, size); |
| 280 |
sprintf(tmp, "%s\n", format); |
| 281 |
vprintf(tmp, ap); |
| 282 |
print_hex_init(size); |
| 283 |
for (i = 0; i < size; i++) |
| 284 |
{ |
| 285 |
print_hex(data[i]); |
| 286 |
} |
| 287 |
|
| 288 |
va_end(ap); |
| 289 |
TTY_FLUSH(); |
| 290 |
} |
| 291 |
#else |
| 292 |
#if defined(WIN32) |
| 293 |
|
| 294 |
EXP_FUNC void STDCALL print_blob(const char *format, const uint8_t *data, |
| 295 |
int size, ...) {} |
| 296 |
#endif |
| 297 |
#endif |
| 298 |
|
| 299 |
#if defined(CONFIG_SSL_HAS_PEM) || defined(CONFIG_HTTP_HAS_AUTHORIZATION) |
| 300 |
|
| 301 |
static const uint8_t map[128] = |
| 302 |
{ |
| 303 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 304 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 305 |
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 306 |
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
| 307 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 308 |
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, |
| 309 |
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 310 |
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, |
| 311 |
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 312 |
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 313 |
49, 50, 51, 255, 255, 255, 255, 255 |
| 314 |
}; |
| 315 |
|
| 316 |
EXP_FUNC int STDCALL base64_decode(const char *in, int len, |
| 317 |
uint8_t *out, int *outlen) |
| 318 |
{ |
| 319 |
int g, t, x, y, z; |
| 320 |
uint8_t c; |
| 321 |
int ret = -1; |
| 322 |
|
| 323 |
g = 3; |
| 324 |
for (x = y = z = t = 0; x < len; x++) |
| 325 |
{ |
| 326 |
if ((c = map[in[x]&0x7F]) == 0xff) |
| 327 |
continue; |
| 328 |
|
| 329 |
if (c == 254) |
| 330 |
{ |
| 331 |
c = 0; |
| 332 |
|
| 333 |
if (--g < 0) |
| 334 |
goto error; |
| 335 |
} |
| 336 |
else if (g != 3) |
| 337 |
goto error; |
| 338 |
|
| 339 |
t = (t<<6) | c; |
| 340 |
|
| 341 |
if (++y == 4) |
| 342 |
{ |
| 343 |
out[z++] = (uint8_t)((t>>16)&255); |
| 344 |
|
| 345 |
if (g > 1) |
| 346 |
out[z++] = (uint8_t)((t>>8)&255); |
| 347 |
|
| 348 |
if (g > 2) |
| 349 |
out[z++] = (uint8_t)(t&255); |
| 350 |
|
| 351 |
y = t = 0; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
if (z > *outlen) |
| 356 |
goto error; |
| 357 |
} |
| 358 |
|
| 359 |
if (y != 0) |
| 360 |
goto error; |
| 361 |
|
| 362 |
*outlen = z; |
| 363 |
ret = 0; |
| 364 |
|
| 365 |
error: |
| 366 |
#ifdef CONFIG_DEBUG |
| 367 |
if (ret < 0) |
| 368 |
printf("Error: Invalid base64\n"); TTY_FLUSH(); |
| 369 |
#endif |
| 370 |
TTY_FLUSH(); |
| 371 |
return ret; |
| 372 |
|
| 373 |
} |
| 374 |
#endif |
| 375 |
|