patch-lib_krb5_crypto-rand_c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. $OpenBSD: patch-lib_krb5_crypto-rand_c,v 1.2 2016/12/17 14:58:31 ajacoutot Exp $
  2. Simpler: just use arc4random_buf(3).
  3. --- lib/krb5/crypto-rand.c.orig Sat Dec 17 14:01:13 2016
  4. +++ lib/krb5/crypto-rand.c Sat Dec 17 14:21:27 2016
  5. @@ -36,53 +36,6 @@
  6. #undef HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE
  7. #define HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE
  8. -#define ENTROPY_NEEDED 128
  9. -
  10. -static HEIMDAL_MUTEX crypto_mutex = HEIMDAL_MUTEX_INITIALIZER;
  11. -
  12. -static int
  13. -seed_something(void)
  14. -{
  15. -#ifndef NO_RANDFILE
  16. - char buf[1024], seedfile[256];
  17. -
  18. - /* If there is a seed file, load it. But such a file cannot be trusted,
  19. - so use 0 for the entropy estimate */
  20. - if (RAND_file_name(seedfile, sizeof(seedfile))) {
  21. - int fd;
  22. - fd = open(seedfile, O_RDONLY | O_BINARY | O_CLOEXEC);
  23. - if (fd >= 0) {
  24. - ssize_t ret;
  25. - rk_cloexec(fd);
  26. - ret = read(fd, buf, sizeof(buf));
  27. - if (ret > 0)
  28. - RAND_add(buf, ret, 0.0);
  29. - close(fd);
  30. - } else
  31. - seedfile[0] = '\0';
  32. - } else
  33. - seedfile[0] = '\0';
  34. -#endif
  35. -
  36. - /* Calling RAND_status() will try to use /dev/urandom if it exists so
  37. - we do not have to deal with it. */
  38. - if (RAND_status() != 1) {
  39. - /* TODO: Once a Windows CryptoAPI RAND method is defined, we
  40. - can use that and failover to another method. */
  41. - }
  42. -
  43. - if (RAND_status() == 1) {
  44. -#ifndef NO_RANDFILE
  45. - /* Update the seed file */
  46. - if (seedfile[0])
  47. - RAND_write_file(seedfile);
  48. -#endif
  49. -
  50. - return 0;
  51. - } else
  52. - return -1;
  53. -}
  54. -
  55. /**
  56. * Fill buffer buf with len bytes of PRNG randomness that is ok to use
  57. * for key generation, padding and public diclosing the randomness w/o
  58. @@ -103,24 +56,8 @@ HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE
  59. KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
  60. krb5_generate_random(void *buf, size_t len)
  61. {
  62. - static int rng_initialized = 0;
  63. - int ret;
  64. -
  65. - HEIMDAL_MUTEX_lock(&crypto_mutex);
  66. - if (!rng_initialized) {
  67. - if (seed_something()) {
  68. - HEIMDAL_MUTEX_unlock(&crypto_mutex);
  69. - return HEIM_ERR_RANDOM_OFFLINE;
  70. - }
  71. - rng_initialized = 1;
  72. - }
  73. - if (RAND_bytes(buf, len) <= 0)
  74. - ret = HEIM_ERR_RANDOM_OFFLINE;
  75. - else
  76. - ret = 0;
  77. - HEIMDAL_MUTEX_unlock(&crypto_mutex);
  78. -
  79. - return ret;
  80. + arc4random_buf(buf, len);
  81. + return (0); /* arc4random_buf(3) cannot fail */
  82. }
  83. /**