/crypto/external/bsd/heimdal/dist/lib/krb5/crypto-rand.c

http://www.minix3.org/ · C · 111 lines · 57 code · 13 blank · 41 comment · 14 complexity · 5616485d2605680b94ae7c684b77cac5 MD5 · raw file

  1. /* $NetBSD: crypto-rand.c,v 1.1.1.1 2011/04/13 18:15:32 elric Exp $ */
  2. /*
  3. * Copyright (c) 1997 - 2008 Kungliga Tekniska Hรถgskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * 3. Neither the name of the Institute nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include "krb5_locl.h"
  35. #define ENTROPY_NEEDED 128
  36. static HEIMDAL_MUTEX crypto_mutex = HEIMDAL_MUTEX_INITIALIZER;
  37. static int
  38. seed_something(void)
  39. {
  40. char buf[1024], seedfile[256];
  41. /* If there is a seed file, load it. But such a file cannot be trusted,
  42. so use 0 for the entropy estimate */
  43. if (RAND_file_name(seedfile, sizeof(seedfile))) {
  44. int fd;
  45. fd = open(seedfile, O_RDONLY | O_BINARY | O_CLOEXEC);
  46. if (fd >= 0) {
  47. ssize_t ret;
  48. rk_cloexec(fd);
  49. ret = read(fd, buf, sizeof(buf));
  50. if (ret > 0)
  51. RAND_add(buf, ret, 0.0);
  52. close(fd);
  53. } else
  54. seedfile[0] = '\0';
  55. } else
  56. seedfile[0] = '\0';
  57. /* Calling RAND_status() will try to use /dev/urandom if it exists so
  58. we do not have to deal with it. */
  59. if (RAND_status() != 1) {
  60. #ifndef _WIN32
  61. krb5_context context;
  62. const char *p;
  63. /* Try using egd */
  64. if (!krb5_init_context(&context)) {
  65. p = krb5_config_get_string(context, NULL, "libdefaults",
  66. "egd_socket", NULL);
  67. if (p != NULL)
  68. RAND_egd_bytes(p, ENTROPY_NEEDED);
  69. krb5_free_context(context);
  70. }
  71. #else
  72. /* TODO: Once a Windows CryptoAPI RAND method is defined, we
  73. can use that and failover to another method. */
  74. #endif
  75. }
  76. if (RAND_status() == 1) {
  77. /* Update the seed file */
  78. if (seedfile[0])
  79. RAND_write_file(seedfile);
  80. return 0;
  81. } else
  82. return -1;
  83. }
  84. KRB5_LIB_FUNCTION void KRB5_LIB_CALL
  85. krb5_generate_random_block(void *buf, size_t len)
  86. {
  87. static int rng_initialized = 0;
  88. HEIMDAL_MUTEX_lock(&crypto_mutex);
  89. if (!rng_initialized) {
  90. if (seed_something())
  91. krb5_abortx(NULL, "Fatal: could not seed the "
  92. "random number generator");
  93. rng_initialized = 1;
  94. }
  95. HEIMDAL_MUTEX_unlock(&crypto_mutex);
  96. if (RAND_bytes(buf, len) <= 0)
  97. krb5_abortx(NULL, "Failed to generate random block");
  98. }