rand_lib.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/opensslconf.h>
  13. #include "internal/rand.h"
  14. #include <openssl/engine.h>
  15. #ifndef OPENSSL_NO_ENGINE
  16. /* non-NULL if default_RAND_meth is ENGINE-provided */
  17. static ENGINE *funct_ref = NULL;
  18. #endif
  19. static const RAND_METHOD *default_RAND_meth = NULL;
  20. int RAND_set_rand_method(const RAND_METHOD *meth)
  21. {
  22. #ifndef OPENSSL_NO_ENGINE
  23. ENGINE_finish(funct_ref);
  24. funct_ref = NULL;
  25. #endif
  26. default_RAND_meth = meth;
  27. return 1;
  28. }
  29. const RAND_METHOD *RAND_get_rand_method(void)
  30. {
  31. if (!default_RAND_meth) {
  32. #ifndef OPENSSL_NO_ENGINE
  33. ENGINE *e = ENGINE_get_default_RAND();
  34. if (e) {
  35. default_RAND_meth = ENGINE_get_RAND(e);
  36. if (default_RAND_meth == NULL) {
  37. ENGINE_finish(e);
  38. e = NULL;
  39. }
  40. }
  41. if (e)
  42. funct_ref = e;
  43. else
  44. #endif
  45. default_RAND_meth = RAND_OpenSSL();
  46. }
  47. return default_RAND_meth;
  48. }
  49. #ifndef OPENSSL_NO_ENGINE
  50. int RAND_set_rand_engine(ENGINE *engine)
  51. {
  52. const RAND_METHOD *tmp_meth = NULL;
  53. if (engine) {
  54. if (!ENGINE_init(engine))
  55. return 0;
  56. tmp_meth = ENGINE_get_RAND(engine);
  57. if (tmp_meth == NULL) {
  58. ENGINE_finish(engine);
  59. return 0;
  60. }
  61. }
  62. /* This function releases any prior ENGINE so call it first */
  63. RAND_set_rand_method(tmp_meth);
  64. funct_ref = engine;
  65. return 1;
  66. }
  67. #endif
  68. void rand_cleanup_int(void)
  69. {
  70. const RAND_METHOD *meth = RAND_get_rand_method();
  71. if (meth && meth->cleanup)
  72. meth->cleanup();
  73. RAND_set_rand_method(NULL);
  74. }
  75. void RAND_seed(const void *buf, int num)
  76. {
  77. const RAND_METHOD *meth = RAND_get_rand_method();
  78. if (meth && meth->seed)
  79. meth->seed(buf, num);
  80. }
  81. void RAND_add(const void *buf, int num, double entropy)
  82. {
  83. const RAND_METHOD *meth = RAND_get_rand_method();
  84. if (meth && meth->add)
  85. meth->add(buf, num, entropy);
  86. }
  87. int RAND_bytes(unsigned char *buf, int num)
  88. {
  89. const RAND_METHOD *meth = RAND_get_rand_method();
  90. if (meth && meth->bytes)
  91. return meth->bytes(buf, num);
  92. return (-1);
  93. }
  94. #if OPENSSL_API_COMPAT < 0x10100000L
  95. int RAND_pseudo_bytes(unsigned char *buf, int num)
  96. {
  97. const RAND_METHOD *meth = RAND_get_rand_method();
  98. if (meth && meth->pseudorand)
  99. return meth->pseudorand(buf, num);
  100. return (-1);
  101. }
  102. #endif
  103. int RAND_status(void)
  104. {
  105. const RAND_METHOD *meth = RAND_get_rand_method();
  106. if (meth && meth->status)
  107. return meth->status();
  108. return 0;
  109. }