]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/openssl/rand_drbg.h
RAND_DRBG: add a callback data for entropy and nonce callbacks
[thirdparty/openssl.git] / include / openssl / rand_drbg.h
CommitLineData
12fb8c3d 1/*
6738bf14 2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
12fb8c3d 3 *
48f4ad77 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
12fb8c3d
RS
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
ae4186b0
DMSP
10#ifndef OPENSSL_RAND_DRBG_H
11# define OPENSSL_RAND_DRBG_H
d86167ec
DMSP
12# pragma once
13
14# include <openssl/macros.h>
936c2b9e 15# ifndef OPENSSL_NO_DEPRECATED_3_0
d86167ec
DMSP
16# define HEADER_DRBG_RAND_H
17# endif
12fb8c3d 18
6decf943 19# include <time.h>
50cd4768 20# include <openssl/types.h>
cf3d6ef7 21# include <openssl/obj_mac.h>
6decf943 22
c402e943
DMSP
23/*
24 * RAND_DRBG flags
25 *
26 * Note: if new flags are added, the constant `rand_drbg_used_flags`
27 * in drbg_lib.c needs to be updated accordingly.
28 */
6decf943 29
8164d91d 30/* In CTR mode, disable derivation function ctr_df */
6decf943 31# define RAND_DRBG_FLAG_CTR_NO_DF 0x1
8bf36651
SL
32/*
33 * This flag is only used when a digest NID is specified (i.e: not a CTR cipher)
34 * Selects DRBG_HMAC if this is set otherwise use DRBG_HASH.
35 */
36# define RAND_DRBG_FLAG_HMAC 0x2
12fb8c3d 37
8bf36651
SL
38/* Used by RAND_DRBG_set_defaults() to set the master DRBG type and flags. */
39# define RAND_DRBG_FLAG_MASTER 0x4
40/* Used by RAND_DRBG_set_defaults() to set the public DRBG type and flags. */
41# define RAND_DRBG_FLAG_PUBLIC 0x8
42/* Used by RAND_DRBG_set_defaults() to set the private DRBG type and flags. */
43# define RAND_DRBG_FLAG_PRIVATE 0x10
c402e943 44
936c2b9e 45# ifndef OPENSSL_NO_DEPRECATED_3_0
c402e943
DMSP
46/* This #define was replaced by an internal constant and should not be used. */
47# define RAND_DRBG_USED_FLAGS (RAND_DRBG_FLAG_CTR_NO_DF)
48# endif
31393fd9 49
c16de9d8
DMSP
50/*
51 * Default security strength (in the sense of [NIST SP 800-90Ar1])
c16de9d8 52 *
32bda2b2 53 * NIST SP 800-90Ar1 supports the strength of the DRBG being smaller than that
8bf36651
SL
54 * of the cipher by collecting less entropy. The current DRBG implementation
55 * does not take RAND_DRBG_STRENGTH into account and sets the strength of the
56 * DRBG to that of the cipher.
c16de9d8 57 *
32bda2b2
KR
58 * RAND_DRBG_STRENGTH is currently only used for the legacy RAND
59 * implementation.
60 *
61 * Currently supported ciphers are: NID_aes_128_ctr, NID_aes_192_ctr and
8bf36651
SL
62 * NID_aes_256_ctr.
63 * The digest types for DRBG_hash or DRBG_hmac are: NID_sha1, NID_sha224,
64 * NID_sha256, NID_sha384, NID_sha512, NID_sha512_224, NID_sha512_256,
65 * NID_sha3_224, NID_sha3_256, NID_sha3_384 and NID_sha3_512.
c16de9d8 66 */
32bda2b2 67# define RAND_DRBG_STRENGTH 256
8bf36651 68/* Default drbg type */
31393fd9 69# define RAND_DRBG_TYPE NID_aes_256_ctr
8bf36651 70/* Default drbg flags */
31393fd9 71# define RAND_DRBG_FLAGS 0
c16de9d8 72
6decf943
DMSP
73
74# ifdef __cplusplus
75extern "C" {
76# endif
77
75e2c877
RS
78/*
79 * Object lifetime functions.
80 */
da0d114c
MC
81RAND_DRBG *RAND_DRBG_new_ex(OPENSSL_CTX *ctx, int type, unsigned int flags,
82 RAND_DRBG *parent);
83RAND_DRBG *RAND_DRBG_secure_new_ex(OPENSSL_CTX *ctx, int type,
84 unsigned int flags, RAND_DRBG *parent);
75e2c877 85RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent);
4f9dabbf 86RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent);
75e2c877 87int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags);
31393fd9 88int RAND_DRBG_set_defaults(int type, unsigned int flags);
75e2c877 89int RAND_DRBG_instantiate(RAND_DRBG *drbg,
12fb8c3d 90 const unsigned char *pers, size_t perslen);
75e2c877
RS
91int RAND_DRBG_uninstantiate(RAND_DRBG *drbg);
92void RAND_DRBG_free(RAND_DRBG *drbg);
93
94/*
95 * Object "use" functions.
96 */
97int RAND_DRBG_reseed(RAND_DRBG *drbg,
eb238134
KR
98 const unsigned char *adin, size_t adinlen,
99 int prediction_resistance);
75e2c877 100int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
12fb8c3d
RS
101 int prediction_resistance,
102 const unsigned char *adin, size_t adinlen);
20928ff6
KR
103int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen);
104
a93ba405 105int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval);
08a65d96 106int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval);
a93ba405 107
4917e911
DMSP
108int RAND_DRBG_set_reseed_defaults(
109 unsigned int master_reseed_interval,
110 unsigned int slave_reseed_interval,
111 time_t master_reseed_time_interval,
112 time_t slave_reseed_time_interval
113 );
114
da0d114c
MC
115RAND_DRBG *OPENSSL_CTX_get0_master_drbg(OPENSSL_CTX *ctx);
116RAND_DRBG *OPENSSL_CTX_get0_public_drbg(OPENSSL_CTX *ctx);
117RAND_DRBG *OPENSSL_CTX_get0_private_drbg(OPENSSL_CTX *ctx);
a93ba405
DMSP
118RAND_DRBG *RAND_DRBG_get0_master(void);
119RAND_DRBG *RAND_DRBG_get0_public(void);
120RAND_DRBG *RAND_DRBG_get0_private(void);
75e2c877
RS
121
122/*
123 * EXDATA
124 */
6decf943 125# define RAND_DRBG_get_ex_new_index(l, p, newf, dupf, freef) \
d318389e 126 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RAND_DRBG, l, p, newf, dupf, freef)
a73d990e
DMSP
127int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg);
128void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx);
12fb8c3d 129
75e2c877 130/*
6decf943 131 * Callback function typedefs
75e2c877 132 */
a73d990e 133typedef size_t (*RAND_DRBG_get_entropy_fn)(RAND_DRBG *drbg,
75e2c877 134 unsigned char **pout,
16960a9b 135 int entropy, size_t min_len,
eb238134
KR
136 size_t max_len,
137 int prediction_resistance);
75e2c877 138typedef void (*RAND_DRBG_cleanup_entropy_fn)(RAND_DRBG *ctx,
6969a3f4 139 unsigned char *out, size_t outlen);
a73d990e 140typedef size_t (*RAND_DRBG_get_nonce_fn)(RAND_DRBG *drbg, unsigned char **pout,
16960a9b
BK
141 int entropy, size_t min_len,
142 size_t max_len);
a73d990e 143typedef void (*RAND_DRBG_cleanup_nonce_fn)(RAND_DRBG *drbg,
6969a3f4 144 unsigned char *out, size_t outlen);
16960a9b 145
a73d990e 146int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
16960a9b
BK
147 RAND_DRBG_get_entropy_fn get_entropy,
148 RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
149 RAND_DRBG_get_nonce_fn get_nonce,
150 RAND_DRBG_cleanup_nonce_fn cleanup_nonce);
12fb8c3d 151
c16de9d8 152
30a9d5d1
DMSP
153int RAND_DRBG_set_callback_data(RAND_DRBG *drbg, void *data);
154
155void *RAND_DRBG_get_callback_data(RAND_DRBG *drbg);
156
6decf943
DMSP
157# ifdef __cplusplus
158}
159# endif
c16de9d8 160
12fb8c3d 161#endif