rand.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #ifndef OPENSSL_RAND_H
  10. #define OPENSSL_RAND_H
  11. #pragma once
  12. #include <openssl/macros.h>
  13. #ifndef OPENSSL_NO_DEPRECATED_3_0
  14. #define HEADER_RAND_H
  15. #endif
  16. #include <stdlib.h>
  17. #include <openssl/types.h>
  18. #include <openssl/e_os2.h>
  19. #include <openssl/randerr.h>
  20. #include <openssl/evp.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /*
  25. * Default security strength (in the sense of [NIST SP 800-90Ar1])
  26. *
  27. * NIST SP 800-90Ar1 supports the strength of the DRBG being smaller than that
  28. * of the cipher by collecting less entropy. The current DRBG implementation
  29. * does not take RAND_DRBG_STRENGTH into account and sets the strength of the
  30. * DRBG to that of the cipher.
  31. */
  32. #define RAND_DRBG_STRENGTH 256
  33. #ifndef OPENSSL_NO_DEPRECATED_3_0
  34. struct rand_meth_st {
  35. int (*seed)(const void *buf, int num);
  36. int (*bytes)(unsigned char *buf, int num);
  37. void (*cleanup)(void);
  38. int (*add)(const void *buf, int num, double randomness);
  39. int (*pseudorand)(unsigned char *buf, int num);
  40. int (*status)(void);
  41. };
  42. OSSL_DEPRECATEDIN_3_0 int RAND_set_rand_method(const RAND_METHOD *meth);
  43. OSSL_DEPRECATEDIN_3_0 const RAND_METHOD *RAND_get_rand_method(void);
  44. #ifndef OPENSSL_NO_ENGINE
  45. OSSL_DEPRECATEDIN_3_0 int RAND_set_rand_engine(ENGINE *engine);
  46. #endif
  47. OSSL_DEPRECATEDIN_3_0 RAND_METHOD *RAND_OpenSSL(void);
  48. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  49. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  50. #define RAND_cleanup() \
  51. while (0) \
  52. continue
  53. #endif
  54. int RAND_bytes(unsigned char *buf, int num);
  55. int RAND_priv_bytes(unsigned char *buf, int num);
  56. /*
  57. * Equivalent of RAND_priv_bytes() but additionally taking an OSSL_LIB_CTX and
  58. * a strength.
  59. */
  60. int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
  61. unsigned int strength);
  62. /*
  63. * Equivalent of RAND_bytes() but additionally taking an OSSL_LIB_CTX and
  64. * a strength.
  65. */
  66. int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
  67. unsigned int strength);
  68. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  69. OSSL_DEPRECATEDIN_1_1_0 int RAND_pseudo_bytes(unsigned char *buf, int num);
  70. #endif
  71. EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx);
  72. EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx);
  73. EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx);
  74. int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand);
  75. int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand);
  76. int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
  77. const char *cipher, const char *digest);
  78. int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
  79. const char *propq);
  80. void RAND_seed(const void *buf, int num);
  81. void RAND_keep_random_devices_open(int keep);
  82. #if defined(__ANDROID__) && defined(__NDK_FPABI__)
  83. __NDK_FPABI__ /* __attribute__((pcs("aapcs"))) on ARM */
  84. #endif
  85. void RAND_add(const void *buf, int num, double randomness);
  86. int RAND_load_file(const char *file, long max_bytes);
  87. int RAND_write_file(const char *file);
  88. const char *RAND_file_name(char *file, size_t num);
  89. int RAND_status(void);
  90. #ifndef OPENSSL_NO_EGD
  91. int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes);
  92. int RAND_egd(const char *path);
  93. int RAND_egd_bytes(const char *path, int bytes);
  94. #endif
  95. int RAND_poll(void);
  96. #if defined(_WIN32) && (defined(BASETYPES) || defined(_WINDEF_H))
  97. /* application has to include <windows.h> in order to use these */
  98. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  99. OSSL_DEPRECATEDIN_1_1_0 void RAND_screen(void);
  100. OSSL_DEPRECATEDIN_1_1_0 int RAND_event(UINT, WPARAM, LPARAM);
  101. #endif
  102. #endif
  103. int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *p);
  104. /* Which parameter to provider_random call */
  105. #define OSSL_PROV_RANDOM_PUBLIC 0
  106. #define OSSL_PROV_RANDOM_PRIVATE 1
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif