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