CRNGT: continuous DRBG tests for providers
[openssl.git] / providers / implementations / rands / drbg_local.h
1 /*
2  * Copyright 1995-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 OSSL_CRYPTO_PROV_LOCAL_H
11 # define OSSL_CRYPTO_PROV_LOCAL_H
12
13 # include <openssl/evp.h>
14 # include <openssl/core_numbers.h>
15 # include <openssl/core_names.h>
16 # include <openssl/params.h>
17 # include "internal/tsan_assist.h"
18
19 # include "internal/numbers.h"
20
21 /* How many times to read the TSC as a randomness source. */
22 # define TSC_READ_COUNT                 4
23
24 /* Maximum reseed intervals */
25 # define MAX_RESEED_INTERVAL                     (1 << 24)
26 # define MAX_RESEED_TIME_INTERVAL                (1 << 20) /* approx. 12 days */
27
28 /* Default reseed intervals */
29 # define MASTER_RESEED_INTERVAL                  (1 << 8)
30 # define SLAVE_RESEED_INTERVAL                   (1 << 16)
31 # define MASTER_RESEED_TIME_INTERVAL             (60*60)   /* 1 hour */
32 # define SLAVE_RESEED_TIME_INTERVAL              (7*60)    /* 7 minutes */
33
34 /*
35  * The number of bytes that constitutes an atomic lump of entropy with respect
36  * to the FIPS 140-2 section 4.9.2 Conditional Tests.  The size is somewhat
37  * arbitrary, the smaller the value, the less entropy is consumed on first
38  * read but the higher the probability of the test failing by accident.
39  *
40  * The value is in bytes.
41  */
42 #define CRNGT_BUFSIZ    16
43
44 /*
45  * Maximum input size for the DRBG (entropy, nonce, personalization string)
46  *
47  * NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes.
48  *
49  * We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes.
50  */
51 # define DRBG_MAX_LENGTH                         INT32_MAX
52
53 /* The default nonce */
54 #ifdef CHARSET_EBCDIC
55 # define DRBG_DEFAULT_PERS_STRING      { 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, \
56      0x4c, 0x20, 0x4e, 0x49, 0x53, 0x54, 0x20, 0x53, 0x50, 0x20, 0x38, 0x30, \
57      0x30, 0x2d, 0x39, 0x30, 0x41, 0x20, 0x44, 0x52, 0x42, 0x47, 0x00};
58 #else
59 # define DRBG_DEFAULT_PERS_STRING                "OpenSSL NIST SP 800-90A DRBG"
60 #endif
61
62 typedef struct prov_drbg_st PROV_DRBG;
63
64 /* DRBG status values */
65 typedef enum drbg_status_e {
66     DRBG_UNINITIALISED,
67     DRBG_READY,
68     DRBG_ERROR
69 } DRBG_STATUS;
70
71 /*
72  * The DRBG methods
73  */
74
75 typedef struct rand_drbg_hmac_st {
76     EVP_MD *md;
77     HMAC_CTX *ctx;
78     size_t blocklen;
79     unsigned char K[EVP_MAX_MD_SIZE];
80     unsigned char V[EVP_MAX_MD_SIZE];
81 } PROV_DRBG_HMAC;
82
83 /*
84  * The state of a DRBG AES-CTR.
85  */
86 typedef struct rand_drbg_ctr_st {
87     EVP_CIPHER_CTX *ctx_ecb;
88     EVP_CIPHER_CTX *ctx_ctr;
89     EVP_CIPHER_CTX *ctx_df;
90     EVP_CIPHER *cipher_ecb;
91     EVP_CIPHER *cipher_ctr;
92     size_t keylen;
93     unsigned char K[32];
94     unsigned char V[16];
95     /* Temporary block storage used by ctr_df */
96     unsigned char bltmp[16];
97     size_t bltmp_pos;
98     unsigned char KX[48];
99 } PROV_DRBG_CTR;
100
101
102 /*
103  * The state of all types of DRBGs, even though we only have CTR mode
104  * right now.
105  */
106 struct prov_drbg_st {
107     CRYPTO_RWLOCK *lock;
108     /* The library context this DRBG is associated with, if any */
109     OPENSSL_CTX *libctx;
110     void *parent;
111     const OSSL_DISPATCH *parent_dispatch;
112     int secure; /* 1: allocated on the secure heap, 0: otherwise */
113     /*
114      * Stores the return value of openssl_get_fork_id() as of when we last
115      * reseeded.  The DRBG reseeds automatically whenever drbg->fork_id !=
116      * openssl_get_fork_id().  Used to provide fork-safety and reseed this
117      * DRBG in the child process.
118      */
119     int fork_id;
120     unsigned short flags; /* various external flags */
121
122     /*
123      * The random_data is used by PROV_add()/drbg_add() to attach random
124      * data to the global drbg, such that the rand_drbg_get_entropy() callback
125      * can pull it during instantiation and reseeding. This is necessary to
126      * reconcile the different philosophies of the PROV and the PROV_DRBG
127      * with respect to how randomness is added to the RNG during reseeding
128      * (see PR #4328).
129      */
130     struct rand_pool_st *seed_pool;
131
132     /*
133      * Auxiliary pool for additional data.
134      */
135     struct rand_pool_st *adin_pool;
136
137     /*
138      * The following parameters are setup by the per-type "init" function.
139      *
140      * The supported types and their init functions are:
141      *    (1) CTR_DRBG:  drbg_ctr_init().
142      *    (2) HMAC_DRBG: drbg_hmac_init().
143      *    (3) HASH_DRBG: drbg_hash_init().
144      *
145      * The parameters are closely related to the ones described in
146      * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one
147      * crucial difference: In the NIST standard, all counts are given
148      * in bits, whereas in OpenSSL entropy counts are given in bits
149      * and buffer lengths are given in bytes.
150      *
151      * Since this difference has lead to some confusion in the past,
152      * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])
153      * the 'len' suffix has been added to all buffer sizes for
154      * clarification.
155      */
156
157     int strength;
158     size_t max_request;
159     size_t min_entropylen, max_entropylen;
160     size_t min_noncelen, max_noncelen;
161     size_t max_perslen, max_adinlen;
162
163     /*
164      * Counts the number of generate requests since the last reseed
165      * (Starts at 1). This value is the reseed_counter as defined in
166      * NIST SP 800-90Ar1
167      */
168     unsigned int reseed_gen_counter;
169     /*
170      * Maximum number of generate requests until a reseed is required.
171      * This value is ignored if it is zero.
172      */
173     unsigned int reseed_interval;
174     /* Stores the time when the last reseeding occurred */
175     time_t reseed_time;
176     /*
177      * Specifies the maximum time interval (in seconds) between reseeds.
178      * This value is ignored if it is zero.
179      */
180     time_t reseed_time_interval;
181     /*
182      * Counts the number of reseeds since instantiation.
183      * This value is ignored if it is zero.
184      *
185      * This counter is used only for seed propagation from the <master> DRBG
186      * to its two children, the <public> and <private> DRBG. This feature is
187      * very special and its sole purpose is to ensure that any randomness which
188      * is added by PROV_add() or PROV_seed() will have an immediate effect on
189      * the output of PROV_bytes() resp. PROV_priv_bytes().
190      */
191     TSAN_QUALIFIER unsigned int reseed_prop_counter;
192     unsigned int reseed_next_counter;
193
194     size_t seedlen;
195     DRBG_STATUS state;
196
197     void *data;
198
199 #ifndef FIPS_MODULE
200     /* Application data, mainly used in the KATs. */
201     CRYPTO_EX_DATA ex_data;
202 #endif
203 };
204
205 /* DRBG helpers */
206 int rand_drbg_restart(PROV_DRBG *drbg,
207                       const unsigned char *buffer, size_t len, size_t entropy);
208 size_t rand_drbg_seedlen(PROV_DRBG *drbg);
209
210 PROV_DRBG *prov_rand_drbg_new(void *provctx, int secure, void *parent,
211                               const OSSL_DISPATCH *parent_dispatch,
212                               int (*dnew)(PROV_DRBG *ctx, int secure));
213 void prov_rand_free(PROV_DRBG *drbg);
214
215 int PROV_DRBG_instantiate(PROV_DRBG *drbg, int strength,
216                           int prediction_resistance,
217                           const unsigned char *pers, size_t perslen,
218                           int (*ifnc)(PROV_DRBG *drbg,
219                                       const unsigned char *ent, size_t ent_len,
220                                       const unsigned char *nonce,
221                                       size_t nonce_len,
222                                       const unsigned char *pstr,
223                                       size_t pstr_len));
224
225 int PROV_DRBG_reseed(PROV_DRBG *drbg, int prediction_resistance,
226                      const unsigned char *ent, size_t ent_len,
227                      const unsigned char *adin, size_t adinlen,
228                      int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent,
229                                    size_t ent_len, const unsigned char *adin,
230                                    size_t adin_len));
231
232 int PROV_DRBG_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
233                        int strength, int prediction_resistance,
234                        const unsigned char *adin, size_t adinlen,
235                        int (*generate)(PROV_DRBG *, unsigned char *out,
236                                        size_t outlen, const unsigned char *adin,
237                                        size_t adin_len),
238                        int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent,
239                                      size_t ent_len, const unsigned char *adin,
240                                      size_t adin_len));
241
242 /* locking api */
243 OSSL_OP_rand_enable_locking_fn drbg_enable_locking;
244 OSSL_OP_rand_lock_fn drbg_lock;
245 OSSL_OP_rand_unlock_fn drbg_unlock;
246
247 int drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]);
248 int drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]);
249
250 #define OSSL_PARAM_DRBG_SETABLE_CTX_COMMON                                      \
251     OSSL_PARAM_uint(OSSL_RAND_PARAM_RESEED_REQUESTS, NULL),             \
252     OSSL_PARAM_uint64(OSSL_RAND_PARAM_RESEED_TIME_INTERVAL, NULL)
253
254 #define OSSL_PARAM_DRBG_GETABLE_CTX_COMMON                              \
255     OSSL_PARAM_int(OSSL_RAND_PARAM_STATUS, NULL),                       \
256     OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),                    \
257     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),               \
258     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MIN_ENTROPYLEN, NULL),            \
259     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_ENTROPYLEN, NULL),            \
260     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MIN_NONCELEN, NULL),              \
261     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_NONCELEN, NULL),              \
262     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_PERSLEN, NULL),               \
263     OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_ADINLEN, NULL),               \
264     OSSL_PARAM_uint(OSSL_RAND_PARAM_RESEED_CTR, NULL),                  \
265     OSSL_PARAM_uint(OSSL_RAND_PARAM_RESEED_REQUESTS, NULL),             \
266     OSSL_PARAM_uint64(OSSL_RAND_PARAM_RESEED_TIME_INTERVAL, NULL)
267
268 size_t prov_crngt_get_entropy(PROV_DRBG *drbg,
269                               unsigned char **pout,
270                               int entropy, size_t min_len, size_t max_len,
271                               int prediction_resistance);
272 void prov_crngt_cleanup_entropy(PROV_DRBG *drbg,
273                                 unsigned char *out, size_t outlen);
274
275 /*
276  * Entropy call back for the FIPS 140-2 section 4.9.2 Conditional Tests.
277  * These need to be exposed for the unit tests.
278  */
279 #if 0
280 int rand_crngt_get_entropy_cb(OPENSSL_CTX *ctx, PROV_POOL *pool,
281                               unsigned char *buf, unsigned char *md,
282                               unsigned int *md_size);
283 extern int (*crngt_get_entropy)(OPENSSL_CTX *ctx, PROV_POOL *pool,
284                                 unsigned char *buf, unsigned char *md,
285                                 unsigned int *md_size);
286 #endif
287 #endif