Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/rand/ctrdrbg.c

raw
1 // Copyright (c) 2017, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/ctrdrbg.h>
5
6 #include <openssl/type_check.h>
7 #include <openssl/mem.h>
8
9 #include "internal.h"
10 #include "../cipher/internal.h"
11
12
13 // Section references in this file refer to SP 800-90Ar1:
14 // http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf
15
16 // See table 3.
17 static const uint64_t kMaxReseedCount = UINT64_C(1) << 48;
18
19 CTR_DRBG_STATE *CTR_DRBG_new(const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
20 const uint8_t *personalization,
21 size_t personalization_len) {
22 SET_DIT_AUTO_RESET;
23 CTR_DRBG_STATE *drbg = OPENSSL_malloc(sizeof(CTR_DRBG_STATE));
24 if (drbg == NULL ||
25 !CTR_DRBG_init(drbg, entropy, personalization, personalization_len)) {
26 CTR_DRBG_free(drbg);
27 return NULL;
28 }
29
30 return drbg;
31 }
32
33 void CTR_DRBG_free(CTR_DRBG_STATE *state) {
34 SET_DIT_AUTO_RESET;
35 OPENSSL_free(state);
36 }
37
38 int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
39 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
40 const uint8_t *personalization, size_t personalization_len) {
41 SET_DIT_AUTO_RESET;
42
43 if (buffers_alias(entropy, CTR_DRBG_ENTROPY_LEN,
44 personalization, personalization_len)) {
45 return 0;
46 }
47
48 // Section 10.2.1.3.1
49 if (personalization_len > CTR_DRBG_ENTROPY_LEN) {
50 return 0;
51 }
52
53 uint8_t seed_material[CTR_DRBG_ENTROPY_LEN];
54 OPENSSL_memcpy(seed_material, entropy, CTR_DRBG_ENTROPY_LEN);
55
56 for (size_t i = 0; i < personalization_len; i++) {
57 seed_material[i] ^= personalization[i];
58 }
59
60 // Section 10.2.1.2
61
62 // kInitMask is the result of encrypting blocks with big-endian value 1, 2
63 // and 3 with the all-zero AES-256 key.
64 static const uint8_t kInitMask[CTR_DRBG_ENTROPY_LEN] = {
65 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1,
66 0xc4, 0xcb, 0x73, 0x8b, 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
67 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18, 0x72, 0x60, 0x03, 0xca,
68 0x37, 0xa6, 0x2a, 0x74, 0xd1, 0xa2, 0xf5, 0x8e, 0x75, 0x06, 0x35, 0x8e,
69 };
70
71 for (size_t i = 0; i < sizeof(kInitMask); i++) {
72 seed_material[i] ^= kInitMask[i];
73 }
74
75 drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, seed_material, 32);
76 OPENSSL_memcpy(drbg->counter, seed_material + 32, 16);
77 drbg->reseed_counter = 1;
78
79 OPENSSL_cleanse(seed_material, sizeof(seed_material));
80 return 1;
81 }
82
83 OPENSSL_STATIC_ASSERT(CTR_DRBG_ENTROPY_LEN % AES_BLOCK_SIZE == 0,
84 not_a_multiple_of_AES_block_size)
85
86 // ctr_inc adds |n| to the last four bytes of |drbg->counter|, treated as a
87 // big-endian number.
88 static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) {
89 uint32_t ctr = CRYPTO_load_u32_be(drbg->counter + 12);
90 CRYPTO_store_u32_be(drbg->counter + 12, ctr + n);
91 }
92
93 static int ctr_drbg_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
94 size_t data_len) {
95 // Per section 10.2.1.2, |data_len| must be |CTR_DRBG_ENTROPY_LEN|. Here, we
96 // allow shorter inputs and right-pad them with zeros. This is equivalent to
97 // the specified algorithm but saves a copy in |CTR_DRBG_generate|.
98 if (data_len > CTR_DRBG_ENTROPY_LEN) {
99 return 0;
100 }
101
102 uint8_t temp[CTR_DRBG_ENTROPY_LEN];
103 for (size_t i = 0; i < CTR_DRBG_ENTROPY_LEN; i += AES_BLOCK_SIZE) {
104 ctr32_add(drbg, 1);
105 drbg->block(drbg->counter, temp + i, &drbg->ks);
106 }
107
108 for (size_t i = 0; i < data_len; i++) {
109 temp[i] ^= data[i];
110 }
111
112 drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, temp, 32);
113 OPENSSL_memcpy(drbg->counter, temp + 32, 16);
114
115 OPENSSL_cleanse(temp, sizeof(temp));
116 return 1;
117 }
118
119 int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
120 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
121 const uint8_t *additional_data,
122 size_t additional_data_len) {
123 SET_DIT_AUTO_RESET;
124
125 if (buffers_alias(entropy, CTR_DRBG_ENTROPY_LEN,
126 additional_data, additional_data_len)) {
127 return 0;
128 }
129
130 // Section 10.2.1.4
131 uint8_t entropy_copy[CTR_DRBG_ENTROPY_LEN];
132
133 if (additional_data_len > 0) {
134 if (additional_data_len > CTR_DRBG_ENTROPY_LEN) {
135 return 0;
136 }
137
138 OPENSSL_memcpy(entropy_copy, entropy, CTR_DRBG_ENTROPY_LEN);
139 for (size_t i = 0; i < additional_data_len; i++) {
140 entropy_copy[i] ^= additional_data[i];
141 }
142
143 entropy = entropy_copy;
144 }
145
146 if (!ctr_drbg_update(drbg, entropy, CTR_DRBG_ENTROPY_LEN)) {
147 OPENSSL_cleanse(entropy_copy, sizeof(entropy_copy));
148 return 0;
149 }
150
151 drbg->reseed_counter = 1;
152
153 OPENSSL_cleanse(entropy_copy, sizeof(entropy_copy));
154 return 1;
155 }
156
157 int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
158 const uint8_t *additional_data,
159 size_t additional_data_len) {
160 SET_DIT_AUTO_RESET;
161 // See 9.3.1
162 if (out_len > CTR_DRBG_MAX_GENERATE_LENGTH) {
163 return 0;
164 }
165
166 // See 10.2.1.5.1
167 if (drbg->reseed_counter > kMaxReseedCount) {
168 return 0;
169 }
170
171 if (additional_data_len != 0 &&
172 !ctr_drbg_update(drbg, additional_data, additional_data_len)) {
173 return 0;
174 }
175
176 // kChunkSize is used to interact better with the cache. Since the AES-CTR
177 // code assumes that it's encrypting rather than just writing keystream, the
178 // buffer has to be zeroed first. Without chunking, large reads would zero
179 // the whole buffer, flushing the L1 cache, and then do another pass (missing
180 // the cache every time) to “encrypt” it. The code can avoid this by
181 // chunking.
182 static const size_t kChunkSize = 8 * 1024;
183
184 while (out_len >= AES_BLOCK_SIZE) {
185 size_t todo = kChunkSize;
186 if (todo > out_len) {
187 todo = out_len;
188 }
189
190 todo &= ~(AES_BLOCK_SIZE-1);
191 const size_t num_blocks = todo / AES_BLOCK_SIZE;
192
193 if (drbg->ctr) {
194 OPENSSL_memset(out, 0, todo);
195 ctr32_add(drbg, 1);
196 drbg->ctr(out, out, num_blocks, &drbg->ks, drbg->counter);
197 ctr32_add(drbg, (uint32_t)(num_blocks - 1));
198 } else {
199 for (size_t i = 0; i < todo; i += AES_BLOCK_SIZE) {
200 ctr32_add(drbg, 1);
201 drbg->block(drbg->counter, out + i, &drbg->ks);
202 }
203 }
204
205 out += todo;
206 out_len -= todo;
207 }
208
209 if (out_len > 0) {
210 uint8_t block[AES_BLOCK_SIZE];
211 ctr32_add(drbg, 1);
212 drbg->block(drbg->counter, block, &drbg->ks);
213
214 OPENSSL_memcpy(out, block, out_len);
215 OPENSSL_cleanse(block, sizeof(block));
216 }
217
218 // Right-padding |additional_data| in step 2.2 is handled implicitly by
219 // |ctr_drbg_update|, to save a copy.
220 if (!ctr_drbg_update(drbg, additional_data, additional_data_len)) {
221 return 0;
222 }
223
224 drbg->reseed_counter++;
225 FIPS_service_indicator_update_state();
226 return 1;
227 }
228
229 void CTR_DRBG_clear(CTR_DRBG_STATE *drbg) {
230 OPENSSL_cleanse(drbg, sizeof(CTR_DRBG_STATE));
231 }