fd708d397cd0fc87d8d413cd5cc6f344111289bb
[openssl.git] / include / openssl / rand_drbg.h
1 /*
2  * Copyright 2017-2020 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_DRBG_H
11 # define OPENSSL_RAND_DRBG_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 #  define HEADER_DRBG_RAND_H
17 # endif
18
19 # include <time.h>
20 # include <openssl/types.h>
21 # include <openssl/obj_mac.h>
22
23 /*
24  * RAND_DRBG  flags
25  *
26  * Note: if new flags are added, the constant `rand_drbg_used_flags`
27  *       in drbg_lib.c needs to be updated accordingly.
28  */
29
30 /* In CTR mode, disable derivation function ctr_df */
31 # define RAND_DRBG_FLAG_CTR_NO_DF            0x1
32 /*
33  * This flag is only used when a digest NID is specified (i.e: not a CTR cipher)
34  * Selects DRBG_HMAC if this is set otherwise use DRBG_HASH.
35  */
36 # define RAND_DRBG_FLAG_HMAC                 0x2
37
38 /* Used by RAND_DRBG_set_defaults() to set the master DRBG type and flags. */
39 # define RAND_DRBG_FLAG_MASTER               0x4
40 /* Used by RAND_DRBG_set_defaults() to set the public DRBG type and flags. */
41 # define RAND_DRBG_FLAG_PUBLIC               0x8
42 /* Used by RAND_DRBG_set_defaults() to set the private DRBG type and flags. */
43 # define RAND_DRBG_FLAG_PRIVATE              0x10
44
45 # ifndef OPENSSL_NO_DEPRECATED_3_0
46 /* This #define was replaced by an internal constant and should not be used. */
47 #  define RAND_DRBG_USED_FLAGS  (RAND_DRBG_FLAG_CTR_NO_DF)
48 # endif
49
50 /*
51  * Default security strength (in the sense of [NIST SP 800-90Ar1])
52  *
53  * NIST SP 800-90Ar1 supports the strength of the DRBG being smaller than that
54  * of the cipher by collecting less entropy. The current DRBG implementation
55  * does not take RAND_DRBG_STRENGTH into account and sets the strength of the
56  * DRBG to that of the cipher.
57  *
58  * RAND_DRBG_STRENGTH is currently only used for the legacy RAND
59  * implementation.
60  *
61  * Currently supported ciphers are: NID_aes_128_ctr, NID_aes_192_ctr and
62  * NID_aes_256_ctr.
63  * The digest types for DRBG_hash or DRBG_hmac are: NID_sha1, NID_sha224,
64  * NID_sha256, NID_sha384, NID_sha512, NID_sha512_224, NID_sha512_256,
65  * NID_sha3_224, NID_sha3_256, NID_sha3_384 and NID_sha3_512.
66  */
67 # define RAND_DRBG_STRENGTH             256
68 /* Default drbg type */
69 # define RAND_DRBG_TYPE                 NID_aes_256_ctr
70 /* Default drbg flags */
71 # define RAND_DRBG_FLAGS                0
72
73
74 # ifdef  __cplusplus
75 extern "C" {
76 # endif
77
78 /*
79  * Object lifetime functions.
80  */
81 RAND_DRBG *RAND_DRBG_new_ex(OPENSSL_CTX *ctx, int type, unsigned int flags,
82                             RAND_DRBG *parent);
83 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent);
84 DEPRECATEDIN_3_0(int RAND_DRBG_set(RAND_DRBG *drbg, int type,
85                                    unsigned int flags))
86 int RAND_DRBG_set_defaults(int type, unsigned int flags);
87 int RAND_DRBG_instantiate(RAND_DRBG *drbg,
88                           const unsigned char *pers, size_t perslen);
89 int RAND_DRBG_uninstantiate(RAND_DRBG *drbg);
90 void RAND_DRBG_free(RAND_DRBG *drbg);
91
92 /*
93  * Object "use" functions.
94  */
95 int RAND_DRBG_reseed(RAND_DRBG *drbg,
96                      const unsigned char *adin, size_t adinlen,
97                      int prediction_resistance);
98 int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
99                        int prediction_resistance,
100                        const unsigned char *adin, size_t adinlen);
101 int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen);
102
103 int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval);
104 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval);
105
106 int RAND_DRBG_set_reseed_defaults(
107                                   unsigned int master_reseed_interval,
108                                   unsigned int slave_reseed_interval,
109                                   time_t master_reseed_time_interval,
110                                   time_t slave_reseed_time_interval
111                                   );
112
113 RAND_DRBG *OPENSSL_CTX_get0_master_drbg(OPENSSL_CTX *ctx);
114 RAND_DRBG *OPENSSL_CTX_get0_public_drbg(OPENSSL_CTX *ctx);
115 RAND_DRBG *OPENSSL_CTX_get0_private_drbg(OPENSSL_CTX *ctx);
116 RAND_DRBG *RAND_DRBG_get0_master(void);
117 RAND_DRBG *RAND_DRBG_get0_public(void);
118 RAND_DRBG *RAND_DRBG_get0_private(void);
119
120 /*
121  * EXDATA
122  */
123 # define RAND_DRBG_get_ex_new_index(l, p, newf, dupf, freef) \
124     CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RAND_DRBG, l, p, newf, dupf, freef)
125 int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg);
126 void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx);
127
128 /*
129  * Callback function typedefs
130  */
131 typedef size_t (*RAND_DRBG_get_entropy_fn)(RAND_DRBG *drbg,
132                                            unsigned char **pout,
133                                            int entropy, size_t min_len,
134                                            size_t max_len,
135                                            int prediction_resistance);
136 typedef void (*RAND_DRBG_cleanup_entropy_fn)(RAND_DRBG *ctx,
137                                              unsigned char *out, size_t outlen);
138 typedef size_t (*RAND_DRBG_get_nonce_fn)(RAND_DRBG *drbg, unsigned char **pout,
139                                          int entropy, size_t min_len,
140                                          size_t max_len);
141 typedef void (*RAND_DRBG_cleanup_nonce_fn)(RAND_DRBG *drbg,
142                                            unsigned char *out, size_t outlen);
143
144 int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
145                             RAND_DRBG_get_entropy_fn get_entropy,
146                             RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
147                             RAND_DRBG_get_nonce_fn get_nonce,
148                             RAND_DRBG_cleanup_nonce_fn cleanup_nonce);
149
150
151 int RAND_DRBG_set_callback_data(RAND_DRBG *drbg, void *data);
152
153 void *RAND_DRBG_get_callback_data(RAND_DRBG *drbg);
154
155 # ifdef  __cplusplus
156 }
157 # endif
158
159 #endif