d00397da626a7621431179075c7e6687db770cb9
[openssl.git] / doc / man3 / RAND_DRBG_set_callbacks.pod
1 =pod
2
3 =head1 NAME
4
5 RAND_DRBG_set_callbacks,
6 RAND_DRBG_set_callback_data,
7 RAND_DRBG_get_callback_data,
8 RAND_DRBG_get_entropy_fn,
9 RAND_DRBG_cleanup_entropy_fn,
10 RAND_DRBG_get_nonce_fn,
11 RAND_DRBG_cleanup_nonce_fn
12 - set callbacks for reseeding
13
14 =head1 SYNOPSIS
15
16  #include <openssl/rand_drbg.h>
17
18
19  int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
20                              RAND_DRBG_get_entropy_fn get_entropy,
21                              RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
22                              RAND_DRBG_get_nonce_fn get_nonce,
23                              RAND_DRBG_cleanup_nonce_fn cleanup_nonce);
24
25  int RAND_DRBG_set_callback_data(RAND_DRBG *drbg, void *ctx);
26
27  void *RAND_DRBG_get_callback_data(RAND_DRBG *drbg);
28
29 =head2 Callback Functions
30
31  typedef size_t (*RAND_DRBG_get_entropy_fn)(
32                        RAND_DRBG *drbg,
33                        unsigned char **pout,
34                        int entropy,
35                        size_t min_len, size_t max_len,
36                        int prediction_resistance);
37
38  typedef void (*RAND_DRBG_cleanup_entropy_fn)(
39                      RAND_DRBG *drbg,
40                      unsigned char *out, size_t outlen);
41
42  typedef size_t (*RAND_DRBG_get_nonce_fn)(
43                        RAND_DRBG *drbg,
44                        unsigned char **pout,
45                        int entropy,
46                        size_t min_len, size_t max_len);
47
48  typedef void (*RAND_DRBG_cleanup_nonce_fn)(
49                      RAND_DRBG *drbg,
50                      unsigned char *out, size_t outlen);
51
52
53
54 =head1 DESCRIPTION
55
56 RAND_DRBG_set_callbacks() sets the callbacks for obtaining fresh entropy and
57 the nonce when reseeding the given B<drbg>.
58 The callback functions are implemented and provided by the caller.
59 Their parameter lists need to match the function prototypes above.
60
61 RAND_DRBG_set_callback_data() can be used to store a pointer to some context
62 specific data, which can subsequently be retrieved by the entropy and nonce
63 callbacks using RAND_DRBG_get_callback_data().
64 The ownership of the context data remains with the caller, i.e., it is the
65 caller's responsibility to keep it available as long as it is needed by the
66 callbacks and free it after use.
67 For more information about the the callback data see the NOTES section.
68
69 Setting the callbacks or the callback data is allowed only if the DRBG has
70 not been initialized yet.
71 Otherwise, the operation will fail.
72 To change the settings for one of the three shared DRBGs it is necessary to call
73 RAND_DRBG_uninstantiate() first.
74
75 The B<get_entropy>() callback is called by the B<drbg> when it requests fresh
76 random input.
77 It is expected that the callback allocates and fills a random buffer of size
78 B<min_len> <= size <= B<max_len> (in bytes) which contains at least B<entropy>
79 bits of randomness.
80 The B<prediction_resistance> flag indicates whether the reseeding was
81 triggered by a prediction resistance request.
82
83 The buffer's address is to be returned in *B<pout> and the number of collected
84 randomness bytes as return value.
85
86 If the callback fails to acquire at least B<entropy> bits of randomness,
87 it must indicate an error by returning a buffer length of 0.
88
89 If B<prediction_resistance> was requested and the random source of the DRBG
90 does not satisfy the conditions requested by [NIST SP 800-90C], then
91 it must also indicate an error by returning a buffer length of 0.
92 See NOTES section for more details.
93
94 The B<cleanup_entropy>() callback is called from the B<drbg> to to clear and
95 free the buffer allocated previously by get_entropy().
96 The values B<out> and B<outlen> are the random buffer's address and length,
97 as returned by the get_entropy() callback.
98
99 The B<get_nonce>() and B<cleanup_nonce>() callbacks are used to obtain a nonce
100 and free it again. A nonce is only required for instantiation (not for reseeding)
101 and only in the case where the DRBG uses a derivation function.
102 The callbacks are analogous to get_entropy() and cleanup_entropy(),
103 except for the missing prediction_resistance flag.
104
105 If the derivation function is disabled, then no nonce is used for instantiation,
106 and the B<get_nonce>() and B<cleanup_nonce>() callbacks can be omitted by
107 setting them to NULL.
108
109
110 =head1 RETURN VALUES
111
112 RAND_DRBG_set_callbacks() returns 1 on success, and 0 on failure.
113
114 RAND_DRBG_set_callback_data() returns 1 on success, and 0 on failure.
115
116 RAND_DRBG_get_callback_data() returns the pointer to the callback data,
117 which is NULL if none has been set previously.
118
119 =head1 NOTES
120
121 It is important that B<cleanup_entropy>() and B<cleanup_nonce>() clear the buffer
122 contents safely before freeing it, in order not to leave sensitive information
123 about the DRBG's state in memory.
124
125 A request for prediction resistance can only be satisfied by pulling fresh
126 entropy from a live entropy source (section 5.5.2 of [NIST SP 800-90C]).
127 It is up to the user to ensure that a live entropy source is configured
128 and is being used.
129
130 The derivation function is disabled during initialization by calling the
131 RAND_DRBG_set() function with the RAND_DRBG_FLAG_CTR_NO_DF flag.
132 For more information on the derivation function and when it can be omitted,
133 see [NIST SP 800-90A Rev. 1]. Roughly speaking it can be omitted if the random
134 source has "full entropy", i.e., contains 8 bits of entropy per byte.
135
136 Even if a nonce is required, the B<get_nonce>() and B<cleanup_nonce>()
137 callbacks can be omitted by setting them to NULL.
138 In this case the DRBG will automatically request an extra amount of entropy
139 (using the B<get_entropy>() and B<cleanup_entropy>() callbacks) which it will
140 utilize for the nonce, following the recommendations of [NIST SP 800-90A Rev. 1],
141 section 8.6.7.
142
143 The callback data is a rather specialized feature, because in general the
144 random sources don't (and in fact, they must not) depend on any state provided
145 by the DRBG.
146 There are however exceptional cases where this feature is useful, most notably
147 for implementing known answer tests (KATs) or deterministic signatures like
148 those specified in RFC6979, which require passing a specified entropy and nonce
149 for instantiating the DRBG.
150
151 =head1 SEE ALSO
152
153 L<RAND_DRBG_new(3)>,
154 L<RAND_DRBG_reseed(3)>,
155 L<RAND_DRBG(7)>
156
157 =head1 HISTORY
158
159 The RAND_DRBG functions were added in OpenSSL 1.1.1.
160
161 =head1 COPYRIGHT
162
163 Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
164
165 Licensed under the Apache License 2.0 (the "License").  You may not use
166 this file except in compliance with the License.  You can obtain a copy
167 in the file LICENSE in the source distribution or at
168 L<https://www.openssl.org/source/license.html>.
169
170 =cut