| 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 | #include <config.h> |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <rand.h> |
| 37 | #include <heim_threads.h> |
| 38 | |
| 39 | #ifdef KRB5 |
| 40 | #include <krb5-types.h> |
| 41 | #endif |
| 42 | #include <roken.h> |
| 43 | |
| 44 | #include "randi.h" |
| 45 | #include "aes.h" |
| 46 | #include "sha.h" |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | #define NUM_POOLS32 32 |
| 82 | |
| 83 | |
| 84 | #define RESEED_INTERVAL100000 100000 /* 0.1 sec */ |
| 85 | |
| 86 | |
| 87 | #define RESEED_BYTES(1024*1024) (1024*1024) |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | #define POOL0_FILL(256/8) (256/8) |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | #define BLOCK32 32 |
| 101 | |
| 102 | |
| 103 | #define CIPH_BLOCK16 16 |
| 104 | |
| 105 | |
| 106 | #define MD_CTXSHA256_CTX SHA256_CTX |
| 107 | #define CIPH_CTXAES_KEY AES_KEY |
| 108 | |
| 109 | struct fortuna_state |
| 110 | { |
| 111 | unsigned char counter[CIPH_BLOCK16]; |
| 112 | unsigned char result[CIPH_BLOCK16]; |
| 113 | unsigned char key[BLOCK32]; |
| 114 | MD_CTXSHA256_CTX pool[NUM_POOLS32]; |
| 115 | CIPH_CTXAES_KEY ciph; |
| 116 | unsigned reseed_count; |
| 117 | struct timeval last_reseed_time; |
| 118 | unsigned pool0_bytes; |
| 119 | unsigned rnd_pos; |
| 120 | int tricks_done; |
| 121 | pid_t pid; |
| 122 | }; |
| 123 | typedef struct fortuna_state FState; |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | static void |
| 135 | ciph_init(CIPH_CTXAES_KEY * ctx, const unsigned char *key, int klen) |
| 136 | { |
| 137 | AES_set_encrypt_keyhc_AES_set_encrypt_key(key, klen * 8, ctx); |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | ciph_encrypt(CIPH_CTXAES_KEY * ctx, const unsigned char *in, unsigned char *out) |
| 142 | { |
| 143 | AES_encrypthc_AES_encrypt(in, out, ctx); |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | md_init(MD_CTXSHA256_CTX * ctx) |
| 148 | { |
| 149 | SHA256_Inithc_SHA256_Init(ctx); |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | md_update(MD_CTXSHA256_CTX * ctx, const unsigned char *data, int len) |
| 154 | { |
| 155 | SHA256_Updatehc_SHA256_Update(ctx, data, len); |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | md_result(MD_CTXSHA256_CTX * ctx, unsigned char *dst) |
| 160 | { |
| 161 | SHA256_CTX tmp; |
| 162 | |
| 163 | memcpy(&tmp, ctx, sizeof(*ctx)); |
| 164 | SHA256_Finalhc_SHA256_Final(dst, &tmp); |
| 165 | memset(&tmp, 0, sizeof(tmp)); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | static void |
| 172 | init_state(FState * st) |
| 173 | { |
| 174 | int i; |
| 175 | |
| 176 | memset(st, 0, sizeof(*st)); |
| 177 | for (i = 0; i < NUM_POOLS32; i++) |
| 178 | md_init(&st->pool[i]); |
| 179 | st->pid = getpid(); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | static void |
| 187 | inc_counter(FState * st) |
| 188 | { |
| 189 | uint32_t *val = (uint32_t *) st->counter; |
| 190 | |
| 191 | if (++val[0]) |
| 192 | return; |
| 193 | if (++val[1]) |
| 194 | return; |
| 195 | if (++val[2]) |
| 196 | return; |
| 197 | ++val[3]; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | static void |
| 204 | encrypt_counter(FState * st, unsigned char *dst) |
| 205 | { |
| 206 | ciph_encrypt(&st->ciph, st->counter, dst); |
| 207 | inc_counter(st); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | static int |
| 216 | enough_time_passed(FState * st) |
| 217 | { |
| 218 | int ok; |
| 219 | struct timeval tv; |
| 220 | struct timeval *last = &st->last_reseed_time; |
| 221 | |
| 222 | gettimeofday(&tv, NULL((void *)0)); |
| 223 | |
| 224 | |
| 225 | ok = 0; |
| 226 | if (tv.tv_sec > last->tv_sec + 1) |
| 227 | ok = 1; |
| 228 | else if (tv.tv_sec == last->tv_sec + 1) |
| 229 | { |
| 230 | if (1000000 + tv.tv_usec - last->tv_usec >= RESEED_INTERVAL100000) |
| 231 | ok = 1; |
| 232 | } |
| 233 | else if (tv.tv_usec - last->tv_usec >= RESEED_INTERVAL100000) |
| 234 | ok = 1; |
| 235 | |
| 236 | |
| 237 | if (ok) |
| 238 | memcpy(last, &tv, sizeof(tv)); |
| 239 | |
| 240 | memset(&tv, 0, sizeof(tv)); |
| 241 | |
| 242 | return ok; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | static void |
| 249 | reseed(FState * st) |
| 250 | { |
| 251 | unsigned k; |
| 252 | unsigned n; |
| 253 | MD_CTXSHA256_CTX key_md; |
| 254 | unsigned char buf[BLOCK32]; |
| 255 | |
| 256 | |
| 257 | st->pool0_bytes = 0; |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | n = ++st->reseed_count; |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | md_init(&key_md); |
| 268 | for (k = 0; k < NUM_POOLS32; k++) |
| 269 | { |
| 270 | md_result(&st->pool[k], buf); |
| 271 | md_update(&key_md, buf, BLOCK32); |
| 272 | |
| 273 | if (n & 1 || !n) |
| 274 | break; |
| 275 | n >>= 1; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | md_update(&key_md, st->key, BLOCK32); |
| 280 | |
| 281 | |
| 282 | md_update(&key_md, (const unsigned char *)&st->pid, sizeof(st->pid)); |
| 283 | |
| 284 | |
| 285 | md_result(&key_md, st->key); |
| 286 | |
| 287 | |
| 288 | ciph_init(&st->ciph, st->key, BLOCK32); |
| 289 | |
| 290 | memset(&key_md, 0, sizeof(key_md)); |
| 291 | memset(buf, 0, BLOCK32); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | static unsigned |
| 298 | get_rand_pool(FState * st) |
| 299 | { |
| 300 | unsigned rnd; |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | rnd = st->key[st->rnd_pos] % NUM_POOLS32; |
| 306 | |
| 307 | st->rnd_pos++; |
| 308 | if (st->rnd_pos >= BLOCK32) |
| 309 | st->rnd_pos = 0; |
| 310 | |
| 311 | return rnd; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | static void |
| 318 | add_entropy(FState * st, const unsigned char *data, unsigned len) |
| 319 | { |
| 320 | unsigned pos; |
| 321 | unsigned char hash[BLOCK32]; |
| 322 | MD_CTXSHA256_CTX md; |
| 323 | |
| 324 | |
| 325 | md_init(&md); |
| 326 | md_update(&md, data, len); |
| 327 | md_result(&md, hash); |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | if (st->reseed_count == 0) |
| 333 | pos = 0; |
| 334 | else |
| 335 | pos = get_rand_pool(st); |
| 336 | md_update(&st->pool[pos], hash, BLOCK32); |
| 337 | |
| 338 | if (pos == 0) |
| 339 | st->pool0_bytes += len; |
| 340 | |
| 341 | memset(hash, 0, BLOCK32); |
| 342 | memset(&md, 0, sizeof(md)); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | static void |
| 349 | rekey(FState * st) |
| 350 | { |
| 351 | encrypt_counter(st, st->key); |
| 352 | encrypt_counter(st, st->key + CIPH_BLOCK16); |
| 353 | ciph_init(&st->ciph, st->key, BLOCK32); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | static void |
| 363 | startup_tricks(FState * st) |
| 364 | { |
| 365 | int i; |
| 366 | unsigned char buf[BLOCK32]; |
| 367 | |
| 368 | |
| 369 | encrypt_counter(st, st->counter); |
| 370 | |
| 371 | |
| 372 | for (i = 1; i < NUM_POOLS32; i++) |
| 373 | { |
| 374 | encrypt_counter(st, buf); |
| 375 | encrypt_counter(st, buf + CIPH_BLOCK16); |
| 376 | md_update(&st->pool[i], buf, BLOCK32); |
| 377 | } |
| 378 | memset(buf, 0, BLOCK32); |
| 379 | |
| 380 | |
| 381 | rekey(st); |
| 382 | |
| 383 | |
| 384 | st->tricks_done = 1; |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | extract_data(FState * st, unsigned count, unsigned char *dst) |
| 389 | { |
| 390 | unsigned n; |
| 391 | unsigned block_nr = 0; |
| 392 | pid_t pid = getpid(); |
| 393 | |
| 394 | |
| 395 | if (st->pool0_bytes >= POOL0_FILL(256/8) || st->reseed_count == 0) |
| 396 | if (enough_time_passed(st)) |
| 397 | reseed(st); |
| 398 | |
| 399 | |
| 400 | if (!st->tricks_done) |
| 401 | startup_tricks(st); |
| 402 | |
| 403 | |
| 404 | if (pid != st->pid) { |
| 405 | st->pid = pid; |
| 406 | reseed(st); |
| 407 | } |
| 408 | |
| 409 | while (count > 0) |
| 410 | { |
| 411 | |
| 412 | encrypt_counter(st, st->result); |
| 413 | |
| 414 | |
| 415 | if (count > CIPH_BLOCK16) |
| 416 | n = CIPH_BLOCK16; |
| 417 | else |
| 418 | n = count; |
| 419 | memcpy(dst, st->result, n); |
| 420 | dst += n; |
| 421 | count -= n; |
| 422 | |
| 423 | |
| 424 | block_nr++; |
| 425 | if (block_nr > (RESEED_BYTES(1024*1024) / CIPH_BLOCK16)) |
| 426 | { |
| 427 | rekey(st); |
| 428 | block_nr = 0; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | rekey(st); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | static FState main_state; |
| 440 | static int init_done; |
| 441 | static int have_entropy; |
| 442 | #define FORTUNA_RESEED_BYTE10000 10000 |
| 443 | static unsigned resend_bytes; |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | static HEIMDAL_MUTEXint fortuna_mutex = HEIMDAL_MUTEX_INITIALIZER0; |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | #define INIT_BYTES128 128 |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | static int |
| 461 | fortuna_reseed(void) |
| 462 | { |
| 463 | int entropy_p = 0; |
| 464 | |
| 465 | if (!init_done) |
| 466 | abort(); |
| 467 | |
| 468 | #ifndef NO_RAND_UNIX_METHOD |
| 469 | { |
| 470 | unsigned char buf[INIT_BYTES128]; |
| 471 | if ((*hc_rand_unix_method.bytes)(buf, sizeof(buf)) == 1) { |
| 472 | add_entropy(&main_state, buf, sizeof(buf)); |
| 473 | entropy_p = 1; |
| Value stored to 'entropy_p' is never read |
| 474 | memset(buf, 0, sizeof(buf)); |
| 475 | } |
| 476 | } |
| 477 | #endif |
| 478 | #ifdef HAVE_ARC4RANDOM1 |
| 479 | { |
| 480 | uint32_t buf[INIT_BYTES128 / sizeof(uint32_t)]; |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++) |
| 484 | buf[i] = arc4random(); |
| 485 | add_entropy(&main_state, (void *)buf, sizeof(buf)); |
| 486 | entropy_p = 1; |
| 487 | } |
| 488 | #endif |
| 489 | #ifndef NO_RAND_EGD_METHOD |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | if (!entropy_p) { |
| 495 | unsigned char buf[INIT_BYTES128]; |
| 496 | if ((*hc_rand_egd_method.bytes)(buf, sizeof(buf)) == 1) { |
| 497 | add_entropy(&main_state, buf, sizeof(buf)); |
| 498 | entropy_p = 1; |
| 499 | memset(buf, 0, sizeof(buf)); |
| 500 | } |
| 501 | } |
| 502 | #endif |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | if (!entropy_p) { |
| 508 | |
| 509 | union { |
| 510 | unsigned char buf[INIT_BYTES128]; |
| 511 | unsigned char shad[1001]; |
| 512 | } u; |
| 513 | int fd; |
| 514 | |
| 515 | |
| 516 | if ((*hc_rand_timer_method.bytes)(u.buf, sizeof(u.buf)) == 1) |
| 517 | add_entropy(&main_state, u.buf, sizeof(u.buf)); |
| 518 | |
| 519 | fd = open("/etc/shadow", O_RDONLY0x0000, 0); |
| 520 | if (fd >= 0) { |
| 521 | ssize_t n; |
| 522 | rk_cloexec(fd); |
| 523 | |
| 524 | while ((n = read(fd, (char *)u.shad, sizeof(u.shad))) > 0) |
| 525 | add_entropy(&main_state, u.shad, sizeof(u.shad)); |
| 526 | close(fd); |
| 527 | } |
| 528 | |
| 529 | memset(&u, 0, sizeof(u)); |
| 530 | |
| 531 | entropy_p = 1; |
| 532 | } |
| 533 | { |
| 534 | pid_t pid = getpid(); |
| 535 | add_entropy(&main_state, (void *)&pid, sizeof(pid)); |
| 536 | } |
| 537 | { |
| 538 | struct timeval tv; |
| 539 | gettimeofday(&tv, NULL((void *)0)); |
| 540 | add_entropy(&main_state, (void *)&tv, sizeof(tv)); |
| 541 | } |
| 542 | #ifdef HAVE_GETUID1 |
| 543 | { |
| 544 | uid_t u = getuid(); |
| 545 | add_entropy(&main_state, (void *)&u, sizeof(u)); |
| 546 | } |
| 547 | #endif |
| 548 | return entropy_p; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | static int |
| 555 | fortuna_init(void) |
| 556 | { |
| 557 | if (!init_done) |
| 558 | { |
| 559 | init_state(&main_state); |
| 560 | init_done = 1; |
| 561 | } |
| 562 | if (!have_entropy) |
| 563 | have_entropy = fortuna_reseed(); |
| 564 | return (init_done && have_entropy); |
| 565 | } |
| 566 | |
| 567 | |
| 568 | |
| 569 | static void |
| 570 | fortuna_seed(const void *indata, int size) |
| 571 | { |
| 572 | HEIMDAL_MUTEX_lock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 573 | |
| 574 | fortuna_init(); |
| 575 | add_entropy(&main_state, indata, size); |
| 576 | if (size >= INIT_BYTES128) |
| 577 | have_entropy = 1; |
| 578 | |
| 579 | HEIMDAL_MUTEX_unlock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 580 | } |
| 581 | |
| 582 | static int |
| 583 | fortuna_bytes(unsigned char *outdata, int size) |
| 584 | { |
| 585 | int ret = 0; |
| 586 | |
| 587 | HEIMDAL_MUTEX_lock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 588 | |
| 589 | if (!fortuna_init()) |
| 590 | goto out; |
| 591 | |
| 592 | resend_bytes += size; |
| 593 | if (resend_bytes > FORTUNA_RESEED_BYTE10000 || resend_bytes < size) { |
| 594 | resend_bytes = 0; |
| 595 | fortuna_reseed(); |
| 596 | } |
| 597 | extract_data(&main_state, size, outdata); |
| 598 | ret = 1; |
| 599 | |
| 600 | out: |
| 601 | HEIMDAL_MUTEX_unlock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 602 | |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | static void |
| 607 | fortuna_cleanup(void) |
| 608 | { |
| 609 | HEIMDAL_MUTEX_lock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 610 | |
| 611 | init_done = 0; |
| 612 | have_entropy = 0; |
| 613 | memset(&main_state, 0, sizeof(main_state)); |
| 614 | |
| 615 | HEIMDAL_MUTEX_unlock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 616 | } |
| 617 | |
| 618 | static void |
| 619 | fortuna_add(const void *indata, int size, double entropi) |
| 620 | { |
| 621 | fortuna_seed(indata, size); |
| 622 | } |
| 623 | |
| 624 | static int |
| 625 | fortuna_pseudorand(unsigned char *outdata, int size) |
| 626 | { |
| 627 | return fortuna_bytes(outdata, size); |
| 628 | } |
| 629 | |
| 630 | static int |
| 631 | fortuna_status(void) |
| 632 | { |
| 633 | int result; |
| 634 | |
| 635 | HEIMDAL_MUTEX_lock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 636 | result = fortuna_init(); |
| 637 | HEIMDAL_MUTEX_unlock(&fortuna_mutex)do { (void)(&fortuna_mutex); } while(0); |
| 638 | |
| 639 | return result ? 1 : 0; |
| 640 | } |
| 641 | |
| 642 | const RAND_METHOD hc_rand_fortuna_method = { |
| 643 | fortuna_seed, |
| 644 | fortuna_bytes, |
| 645 | fortuna_cleanup, |
| 646 | fortuna_add, |
| 647 | fortuna_pseudorand, |
| 648 | fortuna_status |
| 649 | }; |
| 650 | |
| 651 | const RAND_METHOD * |
| 652 | RAND_fortuna_methodhc_RAND_fortuna_method(void) |
| 653 | { |
| 654 | return &hc_rand_fortuna_method; |
| 655 | } |