2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef HEADER_RAND_LCL_H
11 # define HEADER_RAND_LCL_H
13 # include <openssl/aes.h>
14 # include <openssl/evp.h>
15 # include <openssl/sha.h>
16 # include <openssl/hmac.h>
17 # include <openssl/ec.h>
18 # include "internal/rand.h"
21 * Amount of randomness (in bytes) we want for initial seeding.
22 * This is based on the fact that we use AES-128 as the CRBG, and
23 * that we use the derivation function. If either of those changes,
24 * (see rand_init() in rand_lib.c), change this.
26 # define RANDOMNESS_NEEDED 16
28 /* How many times to read the TSC as a randomness source. */
29 # define TSC_READ_COUNT 4
31 /* Maximum amount of randomness to hold in RAND_BYTES_BUFFER. */
32 # define MAX_RANDOMNESS_HELD (4 * RANDOMNESS_NEEDED)
34 /* Maximum count allowed in reseeding */
35 # define MAX_RESEED (1 << 24)
37 /* How often we call RAND_poll() in drbg_entropy_from_system */
38 # define RAND_POLL_RETRIES 8
40 /* Max size of entropy, addin, etc. Larger than any reasonable value */
41 # define DRBG_MAX_LENGTH 0x7ffffff0
44 /* DRBG status values */
45 typedef enum drbg_status_e {
54 * A buffer of random bytes to be fed as "entropy" into the DRBG. RAND_add()
55 * adds data to the buffer, and the drbg_entropy_from_system() pulls data from
56 * the buffer. We have a separate data structure because of the way the
57 * API is defined; otherwise we'd run into deadlocks (RAND_bytes ->
58 * RAND_DRBG_generate* -> drbg_entropy_from_system -> RAND_poll -> RAND_add ->
59 * drbg_add*; the functions with an asterisk lock).
61 typedef struct rand_bytes_buffer_st {
70 * The state of a DRBG AES-CTR.
72 typedef struct rand_drbg_ctr_st {
77 /* Temp variables used by derivation function */
80 /* Temporary block storage used by ctr_df */
81 unsigned char bltmp[16];
88 * The state of all types of DRBGs, even though we only have CTR mode
94 int nid; /* the underlying algorithm */
96 unsigned short flags; /* various external flags */
100 * This is a fixed-size buffer, but we malloc to make it a little
101 * harder to find; a classic security/performance trade-off.
104 unsigned char *randomness;
107 * The following parameters are setup by the per-type "init" function.
109 * Currently the only type is CTR_DRBG, its init function is ctr_init().
111 * The parameters are closely related to the ones described in
112 * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one
113 * crucial difference: In the NIST standard, all counts are given
114 * in bits, whereas in OpenSSL entropy counts are given in bits
115 * and buffer lengths are given in bytes.
117 * Since this difference has lead to some confusion in the past,
118 * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])
119 * the 'len' suffix has been added to all buffer sizes for
125 size_t min_entropylen, max_entropylen;
126 size_t min_noncelen, max_noncelen;
127 size_t max_perslen, max_adinlen;
128 unsigned int reseed_counter;
129 unsigned int reseed_interval;
133 /* Application data, mainly used in the KATs. */
134 CRYPTO_EX_DATA ex_data;
136 /* Implementation specific structures; was a union, but inline for now */
139 /* Callback functions. See comments in rand_lib.c */
140 RAND_DRBG_get_entropy_fn get_entropy;
141 RAND_DRBG_cleanup_entropy_fn cleanup_entropy;
142 RAND_DRBG_get_nonce_fn get_nonce;
143 RAND_DRBG_cleanup_nonce_fn cleanup_nonce;
146 /* The global RAND method, and the global buffer and DRBG instance. */
147 extern RAND_METHOD rand_meth;
148 extern RAND_BYTES_BUFFER rand_bytes;
149 extern RAND_DRBG rand_drbg;
150 extern RAND_DRBG priv_drbg;
152 /* How often we've forked (only incremented in child). */
153 extern int rand_fork_count;
155 /* Hardware-based seeding functions. */
156 void rand_read_tsc(RAND_poll_cb rand_add, void *arg);
157 int rand_read_cpu(RAND_poll_cb rand_add, void *arg);
159 /* DRBG entropy callbacks. */
160 void drbg_release_entropy(RAND_DRBG *drbg, unsigned char *out);
161 size_t drbg_entropy_from_parent(RAND_DRBG *drbg,
162 unsigned char **pout,
163 int entropy, size_t min_len, size_t max_len);
164 size_t drbg_entropy_from_system(RAND_DRBG *drbg,
165 unsigned char **pout,
166 int entropy, size_t min_len, size_t max_len);
168 /* DRBG functions implementing AES-CTR */
169 int ctr_init(RAND_DRBG *drbg);
170 int ctr_uninstantiate(RAND_DRBG *drbg);
171 int ctr_instantiate(RAND_DRBG *drbg,
172 const unsigned char *entropy, size_t entropylen,
173 const unsigned char *nonce, size_t noncelen,
174 const unsigned char *pers, size_t perslen);
175 int ctr_reseed(RAND_DRBG *drbg,
176 const unsigned char *entropy, size_t entropylen,
177 const unsigned char *adin, size_t adinlen);
178 int ctr_generate(RAND_DRBG *drbg,
179 unsigned char *out, size_t outlen,
180 const unsigned char *adin, size_t adinlen);