rand_lcl.h 3.2 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9 10
 */

#ifndef HEADER_RAND_LCL_H
11
# define HEADER_RAND_LCL_H
12

R
Rich Salz 已提交
13 14 15 16 17
# include <openssl/aes.h>
# include <openssl/evp.h>
# include <openssl/sha.h>
# include <openssl/hmac.h>
# include <openssl/ec.h>
R
Richard Levitte 已提交
18
# include "internal/rand.h"
R
Rich Salz 已提交
19

R
Rich Salz 已提交
20 21
/* Amount of randomness (in bytes) we want for initial seeding. */
# define RANDOMNESS_NEEDED              (128 / 8)
22

23 24 25
/* Maximum count allowed in reseeding */
#define MAX_RESEED (1 << 24)

R
Rich Salz 已提交
26
/* DRBG status values */
R
Rich Salz 已提交
27 28 29 30
# define DRBG_STATUS_UNINITIALISED	0
# define DRBG_STATUS_READY		1
# define DRBG_STATUS_RESEED		2
# define DRBG_STATUS_ERROR		3
R
Rich Salz 已提交
31 32

/* A default maximum length: larger than any reasonable value used in pratice */
R
Rich Salz 已提交
33
# define DRBG_MAX_LENGTH                0x7ffffff0
R
Rich Salz 已提交
34

R
Rich Salz 已提交
35 36 37
/*
 * The context for DRBG AES-CTR
 */
R
Rich Salz 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
typedef struct drbg_ctr_ctx_st {
    AES_KEY ks;
    size_t keylen;
    unsigned char K[32];
    unsigned char V[16];
    /* Temp variables used by derivation function */
    AES_KEY df_ks;
    AES_KEY df_kxks;
    /* Temporary block storage used by ctr_df */
    unsigned char bltmp[16];
    size_t bltmp_pos;
    unsigned char KX[48];
} DRBG_CTR_CTX;

R
Rich Salz 已提交
52 53 54 55

/*
 * The context for all DRBG's
 */
R
Rich Salz 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
struct drbg_ctx_st {
    CRYPTO_RWLOCK *lock;
    DRBG_CTX *parent;
    int nid; /* the NID of the underlying algorithm */
    unsigned int flags; /* various external flags */

    /* The following parameters are setup by mechanism drbg_init() call */
    int strength;
    size_t blocklength;
    size_t max_request;
    size_t min_entropy, max_entropy;
    size_t min_nonce, max_nonce;
    size_t max_pers, max_adin;
    unsigned int reseed_counter;
    unsigned int reseed_interval;
    size_t seedlen;
    int status;

    /* Application data: typically (only?) used by test get_entropy */
    CRYPTO_EX_DATA ex_data;

    /* Implementation specific structures */
    DRBG_CTR_CTX ctr;

    /* entropy gathering function */
B
Benjamin Kaduk 已提交
81
    RAND_DRBG_get_entropy_fn get_entropy;
R
Rich Salz 已提交
82
    /* Indicates we have finished with entropy buffer */
B
Benjamin Kaduk 已提交
83
    RAND_DRBG_cleanup_entropy_fn cleanup_entropy;
R
Rich Salz 已提交
84
    /* nonce gathering function */
B
Benjamin Kaduk 已提交
85
    RAND_DRBG_get_nonce_fn get_nonce;
R
Rich Salz 已提交
86
    /* Indicates we have finished with nonce buffer */
B
Benjamin Kaduk 已提交
87
    RAND_DRBG_cleanup_nonce_fn cleanup_nonce;
R
Rich Salz 已提交
88
};
R
Rich Salz 已提交
89

90

R
Rich Salz 已提交
91
extern RAND_METHOD openssl_rand_meth;
R
Rich Salz 已提交
92 93
void rand_drbg_cleanup(void);

R
Rich Salz 已提交
94 95 96 97 98
/* Hardware-based seeding functions. */
void rand_rdtsc(void);
int rand_rdcpu(void);

/* DRBG functions implementing AES-CTR */
R
Rich Salz 已提交
99 100 101 102 103 104 105 106 107 108 109 110
int ctr_init(DRBG_CTX *dctx);
int ctr_uninstantiate(DRBG_CTX *dctx);
int ctr_instantiate(DRBG_CTX *dctx,
                    const unsigned char *ent, size_t entlen,
                    const unsigned char *nonce, size_t noncelen,
                    const unsigned char *pers, size_t perslen);
int ctr_reseed(DRBG_CTX *dctx,
               const unsigned char *ent, size_t entlen,
               const unsigned char *adin, size_t adinlen);
int ctr_generate(DRBG_CTX *dctx,
                 unsigned char *out, size_t outlen,
                 const unsigned char *adin, size_t adinlen);
111 112

#endif