• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2015, 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_CRYPTO_RAND_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
17 
18 #include <openssl/aes.h>
19 
20 #include "../../internal.h"
21 #include "../modes/internal.h"
22 
23 #if defined(__cplusplus)
24 extern "C" {
25 #endif
26 
27 
28 /* RAND_bytes_with_additional_data samples from the RNG after mixing 32 bytes
29  * from |user_additional_data| in. */
30 void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
31                                      const uint8_t user_additional_data[32]);
32 
33 /* CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating
34  * system. */
35 void CRYPTO_sysrand(uint8_t *buf, size_t len);
36 
37 /* rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has
38  * been enabled via |RAND_enable_fork_unsafe_buffering|. */
39 int rand_fork_unsafe_buffering_enabled(void);
40 
41 /* CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP
42  * 800-90Ar1. */
43 typedef struct {
44   alignas(16) AES_KEY ks;
45   block128_f block;
46   ctr128_f ctr;
47   union {
48     uint8_t bytes[16];
49     uint32_t words[4];
50   } counter;
51   uint64_t reseed_counter;
52 } CTR_DRBG_STATE;
53 
54 /* See SP 800-90Ar1, table 3. */
55 #define CTR_DRBG_ENTROPY_LEN 48
56 #define CTR_DRBG_MAX_GENERATE_LENGTH 65536
57 
58 /* CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of
59  * entropy in |entropy| and, optionally, a personalization string up to
60  * |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero
61  * on error. */
62 OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
63                                  const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
64                                  const uint8_t *personalization,
65                                  size_t personalization_len);
66 
67 /* CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy
68  * in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of
69  * additional data. It returns one on success or zero on error. */
70 OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
71                                    const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
72                                    const uint8_t *additional_data,
73                                    size_t additional_data_len);
74 
75 /* CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional
76  * data (if any) and then writes |out_len| random bytes to |out|, where
77  * |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or
78  * zero on error. */
79 OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out,
80                                      size_t out_len,
81                                      const uint8_t *additional_data,
82                                      size_t additional_data_len);
83 
84 /* CTR_DRBG_clear zeroises the state of |drbg|. */
85 OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg);
86 
87 
88 #if defined(__cplusplus)
89 }  /* extern C */
90 #endif
91 
92 #endif  /* OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H */
93