Add --with-rand-seed
[openssl.git] / crypto / rand / rand_lcl.h
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #ifndef HEADER_RAND_LCL_H
11 # define HEADER_RAND_LCL_H
12
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"
19
20 /* Amount of randomness (in bytes) we want for initial seeding. */
21 # define RANDOMNESS_NEEDED              (128 / 8)
22
23 /* Maximum count allowed in reseeding */
24 #define MAX_RESEED (1 << 24)
25
26 /* DRBG status values */
27 # define DRBG_STATUS_UNINITIALISED      0
28 # define DRBG_STATUS_READY              1
29 # define DRBG_STATUS_RESEED             2
30 # define DRBG_STATUS_ERROR              3
31
32 /* A default maximum length: larger than any reasonable value used in pratice */
33 # define DRBG_MAX_LENGTH                0x7ffffff0
34
35 /*
36  * The context for DRBG AES-CTR
37  */
38 typedef struct drbg_ctr_ctx_st {
39     AES_KEY ks;
40     size_t keylen;
41     unsigned char K[32];
42     unsigned char V[16];
43     /* Temp variables used by derivation function */
44     AES_KEY df_ks;
45     AES_KEY df_kxks;
46     /* Temporary block storage used by ctr_df */
47     unsigned char bltmp[16];
48     size_t bltmp_pos;
49     unsigned char KX[48];
50 } DRBG_CTR_CTX;
51
52
53 /*
54  * The context for all DRBG's
55  */
56 struct drbg_ctx_st {
57     CRYPTO_RWLOCK *lock;
58     DRBG_CTX *parent;
59     int nid; /* the NID of the underlying algorithm */
60     unsigned int flags; /* various external flags */
61
62     /* The following parameters are setup by mechanism drbg_init() call */
63     int strength;
64     size_t blocklength;
65     size_t max_request;
66     size_t min_entropy, max_entropy;
67     size_t min_nonce, max_nonce;
68     size_t max_pers, max_adin;
69     unsigned int reseed_counter;
70     unsigned int reseed_interval;
71     size_t seedlen;
72     int status;
73
74     /* Application data: typically (only?) used by test get_entropy */
75     CRYPTO_EX_DATA ex_data;
76
77     /* Implementation specific structures */
78     DRBG_CTR_CTX ctr;
79
80     /* entropy gathering function */
81     RAND_DRBG_get_entropy_fn get_entropy;
82     /* Indicates we have finished with entropy buffer */
83     RAND_DRBG_cleanup_entropy_fn cleanup_entropy;
84     /* nonce gathering function */
85     RAND_DRBG_get_nonce_fn get_nonce;
86     /* Indicates we have finished with nonce buffer */
87     RAND_DRBG_cleanup_nonce_fn cleanup_nonce;
88 };
89
90
91 extern RAND_METHOD openssl_rand_meth;
92 void rand_drbg_cleanup(void);
93
94 /* Hardware-based seeding functions. */
95 void rand_rdtsc(void);
96 int rand_rdcpu(void);
97
98 /* DRBG functions implementing AES-CTR */
99 int ctr_init(DRBG_CTX *dctx);
100 int ctr_uninstantiate(DRBG_CTX *dctx);
101 int ctr_instantiate(DRBG_CTX *dctx,
102                     const unsigned char *ent, size_t entlen,
103                     const unsigned char *nonce, size_t noncelen,
104                     const unsigned char *pers, size_t perslen);
105 int ctr_reseed(DRBG_CTX *dctx,
106                const unsigned char *ent, size_t entlen,
107                const unsigned char *adin, size_t adinlen);
108 int ctr_generate(DRBG_CTX *dctx,
109                  unsigned char *out, size_t outlen,
110                  const unsigned char *adin, size_t adinlen);
111
112 #endif