xref: /aosp_15_r20/external/boringssl/src/include/openssl/ctrdrbg.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2022, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef OPENSSL_HEADER_CTRDRBG_H
16 #define OPENSSL_HEADER_CTRDRBG_H
17 
18 #include <openssl/base.h>
19 
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
23 
24 
25 // FIPS pseudo-random number generator.
26 
27 
28 // CTR-DRBG state objects.
29 //
30 // CTR_DRBG_STATE contains the state of a FIPS AES-CTR-based pseudo-random
31 // number generator. If BoringSSL was built in FIPS mode then this is a FIPS
32 // Approved algorithm.
33 
34 // CTR_DRBG_ENTROPY_LEN is the number of bytes of input entropy. See SP
35 // 800-90Ar1, table 3.
36 #define CTR_DRBG_ENTROPY_LEN 48
37 
38 // CTR_DRBG_MAX_GENERATE_LENGTH is the maximum number of bytes that can be
39 // generated in a single call to |CTR_DRBG_generate|.
40 #define CTR_DRBG_MAX_GENERATE_LENGTH 65536
41 
42 // CTR_DRBG_new returns an initialized |CTR_DRBG_STATE|, or NULL if either
43 // allocation failed or if |personalization_len| is invalid.
44 OPENSSL_EXPORT CTR_DRBG_STATE *CTR_DRBG_new(
45     const uint8_t entropy[CTR_DRBG_ENTROPY_LEN], const uint8_t *personalization,
46     size_t personalization_len);
47 
48 // CTR_DRBG_free frees |state| if non-NULL, or else does nothing.
49 OPENSSL_EXPORT void CTR_DRBG_free(CTR_DRBG_STATE* state);
50 
51 // CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy
52 // in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of
53 // additional data. It returns one on success or zero on error.
54 OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
55                                    const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
56                                    const uint8_t *additional_data,
57                                    size_t additional_data_len);
58 
59 // CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional
60 // data (if any) and then writes |out_len| random bytes to |out|, where
61 // |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or
62 // zero on error.
63 OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out,
64                                      size_t out_len,
65                                      const uint8_t *additional_data,
66                                      size_t additional_data_len);
67 
68 // CTR_DRBG_clear zeroises the state of |drbg|.
69 OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg);
70 
71 
72 #if defined(__cplusplus)
73 }  // extern C
74 
75 extern "C++" {
76 BSSL_NAMESPACE_BEGIN
77 BORINGSSL_MAKE_DELETER(CTR_DRBG_STATE, CTR_DRBG_free)
78 BSSL_NAMESPACE_END
79 }  // extern C++
80 #endif
81 
82 #endif  // OPENSSL_HEADER_CTRDRBG_H
83