Add DRBG random method
[oweals/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 "include/internal/rand.h"
19
20 /* we require 256 bits of randomness */
21 # define RANDOMNESS_NEEDED (256 / 8)
22
23 /* DRBG status values */
24 #define DRBG_STATUS_UNINITIALISED       0
25 #define DRBG_STATUS_READY               1
26 #define DRBG_STATUS_RESEED              2
27 #define DRBG_STATUS_ERROR               3
28
29 /* A default maximum length: larger than any reasonable value used in pratice */
30 #define DRBG_MAX_LENGTH                 0x7ffffff0
31
32 typedef struct drbg_ctr_ctx_st {
33     AES_KEY ks;
34     size_t keylen;
35     unsigned char K[32];
36     unsigned char V[16];
37     /* Temp variables used by derivation function */
38     AES_KEY df_ks;
39     AES_KEY df_kxks;
40     /* Temporary block storage used by ctr_df */
41     unsigned char bltmp[16];
42     size_t bltmp_pos;
43     unsigned char KX[48];
44 } DRBG_CTR_CTX;
45
46 struct drbg_ctx_st {
47     CRYPTO_RWLOCK *lock;
48     DRBG_CTX *parent;
49     int nid; /* the NID of the underlying algorithm */
50     unsigned int flags; /* various external flags */
51
52     /* The following parameters are setup by mechanism drbg_init() call */
53     int strength;
54     size_t blocklength;
55     size_t max_request;
56     size_t min_entropy, max_entropy;
57     size_t min_nonce, max_nonce;
58     size_t max_pers, max_adin;
59     unsigned int reseed_counter;
60     unsigned int reseed_interval;
61     size_t seedlen;
62     int status;
63
64     /* Application data: typically (only?) used by test get_entropy */
65     CRYPTO_EX_DATA ex_data;
66
67     /* Implementation specific structures */
68     DRBG_CTR_CTX ctr;
69
70     /* entropy gathering function */
71     size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
72             int entropy, size_t min_len, size_t max_len);
73     /* Indicates we have finished with entropy buffer */
74     void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
75
76     /* nonce gathering function */
77     size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
78             int entropy, size_t min_len, size_t max_len);
79     /* Indicates we have finished with nonce buffer */
80     void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
81 };
82
83
84 extern RAND_METHOD openssl_rand_meth;
85 void rand_drbg_cleanup(void);
86
87 int ctr_init(DRBG_CTX *dctx);
88 int drbg_hash_init(DRBG_CTX *dctx);
89 int drbg_hmac_init(DRBG_CTX *dctx);
90 int ctr_uninstantiate(DRBG_CTX *dctx);
91 int ctr_instantiate(DRBG_CTX *dctx,
92                     const unsigned char *ent, size_t entlen,
93                     const unsigned char *nonce, size_t noncelen,
94                     const unsigned char *pers, size_t perslen);
95 int ctr_reseed(DRBG_CTX *dctx,
96                const unsigned char *ent, size_t entlen,
97                const unsigned char *adin, size_t adinlen);
98 int ctr_generate(DRBG_CTX *dctx,
99                  unsigned char *out, size_t outlen,
100                  const unsigned char *adin, size_t adinlen);
101
102 #endif