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