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