2 * Copyright 2011-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/crypto.h>
13 #include <openssl/err.h>
14 #include <openssl/rand.h>
16 #include "internal/thread_once.h"
19 * Mapping of NIST SP 800-90A DRBG to OpenSSL RAND_METHOD.
24 * The default global DRBG and its auto-init/auto-cleanup.
26 static DRBG_CTX ossl_drbg;
28 static CRYPTO_ONCE ossl_drbg_init = CRYPTO_ONCE_STATIC_INIT;
30 DEFINE_RUN_ONCE_STATIC(do_ossl_drbg_init)
34 ossl_drbg.lock = CRYPTO_THREAD_lock_new();
35 st &= ossl_drbg.lock != NULL;
36 st &= RAND_DRBG_set(&ossl_drbg, NID_aes_128_ctr, 0) == 1;
40 void rand_drbg_cleanup(void)
42 CRYPTO_THREAD_lock_free(ossl_drbg.lock);
45 static void inc_128(DRBG_CTR_CTX *cctx)
49 unsigned char *p = &cctx->V[15];
51 for (i = 0; i < 16; i++, p--) {
56 /* If we didn't wrap around, we're done. */
62 static void ctr_XOR(DRBG_CTR_CTX *cctx, const unsigned char *in, size_t inlen)
66 if (in == NULL || inlen == 0)
70 * Any zero padding will have no effect on the result as we
71 * are XORing. So just process however much input we have.
73 n = inlen < cctx->keylen ? inlen : cctx->keylen;
74 for (i = 0; i < n; i++)
76 if (inlen <= cctx->keylen)
79 n = inlen - cctx->keylen;
81 /* Should never happen */
84 for (i = 0; i < n; i++)
85 cctx->V[i] ^= in[i + cctx->keylen];
89 * Process a complete block using BCC algorithm of SP 800-90A 10.3.3
91 static void ctr_BCC_block(DRBG_CTR_CTX *cctx, unsigned char *out,
92 const unsigned char *in)
96 for (i = 0; i < 16; i++)
98 AES_encrypt(out, out, &cctx->df_ks);
103 * Handle several BCC operations for as much data as we need for K and X
105 static void ctr_BCC_blocks(DRBG_CTR_CTX *cctx, const unsigned char *in)
107 ctr_BCC_block(cctx, cctx->KX, in);
108 ctr_BCC_block(cctx, cctx->KX + 16, in);
109 if (cctx->keylen != 16)
110 ctr_BCC_block(cctx, cctx->KX + 32, in);
114 * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
115 * see 10.3.1 stage 7.
117 static void ctr_BCC_init(DRBG_CTR_CTX *cctx)
119 memset(cctx->KX, 0, 48);
120 memset(cctx->bltmp, 0, 16);
121 ctr_BCC_block(cctx, cctx->KX, cctx->bltmp);
123 ctr_BCC_block(cctx, cctx->KX + 16, cctx->bltmp);
124 if (cctx->keylen != 16) {
126 ctr_BCC_block(cctx, cctx->KX + 32, cctx->bltmp);
131 * Process several blocks into BCC algorithm, some possibly partial
133 static void ctr_BCC_update(DRBG_CTR_CTX *cctx,
134 const unsigned char *in, size_t inlen)
136 if (in == NULL || inlen == 0)
139 /* If we have partial block handle it first */
140 if (cctx->bltmp_pos) {
141 size_t left = 16 - cctx->bltmp_pos;
143 /* If we now have a complete block process it */
145 memcpy(cctx->bltmp + cctx->bltmp_pos, in, left);
146 ctr_BCC_blocks(cctx, cctx->bltmp);
153 /* Process zero or more complete blocks */
154 for (; inlen >= 16; in += 16, inlen -= 16) {
155 ctr_BCC_blocks(cctx, in);
158 /* Copy any remaining partial block to the temporary buffer */
160 memcpy(cctx->bltmp + cctx->bltmp_pos, in, inlen);
161 cctx->bltmp_pos += inlen;
165 static void ctr_BCC_final(DRBG_CTR_CTX *cctx)
167 if (cctx->bltmp_pos) {
168 memset(cctx->bltmp + cctx->bltmp_pos, 0, 16 - cctx->bltmp_pos);
169 ctr_BCC_blocks(cctx, cctx->bltmp);
173 static void ctr_df(DRBG_CTR_CTX *cctx,
174 const unsigned char *in1, size_t in1len,
175 const unsigned char *in2, size_t in2len,
176 const unsigned char *in3, size_t in3len)
178 static unsigned char c80 = 0x80;
180 unsigned char *p = cctx->bltmp;
189 inlen = in1len + in2len + in3len;
190 /* Initialise L||N in temporary block */
191 *p++ = (inlen >> 24) & 0xff;
192 *p++ = (inlen >> 16) & 0xff;
193 *p++ = (inlen >> 8) & 0xff;
196 /* NB keylen is at most 32 bytes */
200 *p = (unsigned char)((cctx->keylen + 16) & 0xff);
202 ctr_BCC_update(cctx, in1, in1len);
203 ctr_BCC_update(cctx, in2, in2len);
204 ctr_BCC_update(cctx, in3, in3len);
205 ctr_BCC_update(cctx, &c80, 1);
208 AES_set_encrypt_key(cctx->KX, cctx->keylen * 8, &cctx->df_kxks);
209 /* X follows key K */
210 AES_encrypt(cctx->KX + cctx->keylen, cctx->KX, &cctx->df_kxks);
211 AES_encrypt(cctx->KX, cctx->KX + 16, &cctx->df_kxks);
212 if (cctx->keylen != 16)
213 AES_encrypt(cctx->KX + 16, cctx->KX + 32, &cctx->df_kxks);
217 * NB the no-df Update in SP800-90A specifies a constant input length
218 * of seedlen, however other uses of this algorithm pad the input with
219 * zeroes if necessary and have up to two parameters XORed together,
220 * handle both cases in this function instead.
222 static void ctr_update(DRBG_CTX *dctx,
223 const unsigned char *in1, size_t in1len,
224 const unsigned char *in2, size_t in2len,
225 const unsigned char *nonce, size_t noncelen)
227 DRBG_CTR_CTX *cctx = &dctx->ctr;
229 /* ks is already setup for correct key */
231 AES_encrypt(cctx->V, cctx->K, &cctx->ks);
233 /* If keylen longer than 128 bits need extra encrypt */
234 if (cctx->keylen != 16) {
236 AES_encrypt(cctx->V, cctx->K + 16, &cctx->ks);
239 AES_encrypt(cctx->V, cctx->V, &cctx->ks);
241 /* If 192 bit key part of V is on end of K */
242 if (cctx->keylen == 24) {
243 memcpy(cctx->V + 8, cctx->V, 8);
244 memcpy(cctx->V, cctx->K + 24, 8);
247 if (dctx->flags & RAND_DRBG_FLAG_CTR_USE_DF) {
248 /* If no input reuse existing derived value */
249 if (in1 != NULL || nonce != NULL || in2 != NULL)
250 ctr_df(cctx, in1, in1len, nonce, noncelen, in2, in2len);
251 /* If this a reuse input in1len != 0 */
253 ctr_XOR(cctx, cctx->KX, dctx->seedlen);
255 ctr_XOR(cctx, in1, in1len);
256 ctr_XOR(cctx, in2, in2len);
259 AES_set_encrypt_key(cctx->K, dctx->strength, &cctx->ks);
262 int ctr_instantiate(DRBG_CTX *dctx,
263 const unsigned char *ent, size_t entlen,
264 const unsigned char *nonce, size_t noncelen,
265 const unsigned char *pers, size_t perslen)
267 DRBG_CTR_CTX *cctx = &dctx->ctr;
269 memset(cctx->K, 0, sizeof(cctx->K));
270 memset(cctx->V, 0, sizeof(cctx->V));
271 AES_set_encrypt_key(cctx->K, dctx->strength, &cctx->ks);
272 ctr_update(dctx, ent, entlen, pers, perslen, nonce, noncelen);
276 int ctr_reseed(DRBG_CTX *dctx,
277 const unsigned char *ent, size_t entlen,
278 const unsigned char *adin, size_t adinlen)
280 ctr_update(dctx, ent, entlen, adin, adinlen, NULL, 0);
284 int ctr_generate(DRBG_CTX *dctx,
285 unsigned char *out, size_t outlen,
286 const unsigned char *adin, size_t adinlen)
288 DRBG_CTR_CTX *cctx = &dctx->ctr;
290 if (adin != NULL && adinlen != 0) {
291 ctr_update(dctx, adin, adinlen, NULL, 0, NULL, 0);
292 /* This means we reuse derived value */
293 if (dctx->flags & RAND_DRBG_FLAG_CTR_USE_DF) {
304 /* Use K as temp space as it will be updated */
305 AES_encrypt(cctx->V, cctx->K, &cctx->ks);
306 memcpy(out, cctx->K, outlen);
309 AES_encrypt(cctx->V, out, &cctx->ks);
316 ctr_update(dctx, adin, adinlen, NULL, 0, NULL, 0);
320 int ctr_uninstantiate(DRBG_CTX *dctx)
322 memset(&dctx->ctr, 0, sizeof(dctx->ctr));
326 int ctr_init(DRBG_CTX *dctx)
328 DRBG_CTR_CTX *cctx = &dctx->ctr;
333 /* This can't happen, but silence the compiler warning. */
335 case NID_aes_128_ctr:
338 case NID_aes_192_ctr:
341 case NID_aes_256_ctr:
346 cctx->keylen = keylen;
347 dctx->strength = keylen * 8;
348 dctx->blocklength = 16;
349 dctx->seedlen = keylen + 16;
351 if (dctx->flags & RAND_DRBG_FLAG_CTR_USE_DF) {
352 /* df initialisation */
353 static unsigned char df_key[32] = {
354 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
355 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
356 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
357 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f
359 /* Set key schedule for df_key */
360 AES_set_encrypt_key(df_key, dctx->strength, &cctx->df_ks);
362 dctx->min_entropy = cctx->keylen;
363 dctx->max_entropy = DRBG_MAX_LENGTH;
364 dctx->min_nonce = dctx->min_entropy / 2;
365 dctx->max_nonce = DRBG_MAX_LENGTH;
366 dctx->max_pers = DRBG_MAX_LENGTH;
367 dctx->max_adin = DRBG_MAX_LENGTH;
369 dctx->min_entropy = dctx->seedlen;
370 dctx->max_entropy = dctx->seedlen;
374 dctx->max_pers = dctx->seedlen;
375 dctx->max_adin = dctx->seedlen;
378 dctx->max_request = 1 << 16;
379 dctx->reseed_interval = MAX_RESEED;
385 * The following function tie the DRBG code into the RAND_METHOD
388 DRBG_CTX *RAND_DRBG_get_default(void)
390 if (!RUN_ONCE(&ossl_drbg_init, do_ossl_drbg_init))
395 static int drbg_bytes(unsigned char *out, int count)
397 DRBG_CTX *dctx = RAND_DRBG_get_default();
400 CRYPTO_THREAD_write_lock(dctx->lock);
404 if (count > (int)dctx->max_request)
405 rcnt = dctx->max_request;
408 ret = RAND_DRBG_generate(dctx, out, rcnt, 0, NULL, 0);
416 CRYPTO_THREAD_unlock(dctx->lock);
420 static int drbg_status(void)
422 DRBG_CTX *dctx = RAND_DRBG_get_default();
425 CRYPTO_THREAD_write_lock(dctx->lock);
426 ret = dctx->status == DRBG_STATUS_READY ? 1 : 0;
427 CRYPTO_THREAD_unlock(dctx->lock);
431 static void drbg_cleanup(void)
433 DRBG_CTX *dctx = RAND_DRBG_get_default();
435 CRYPTO_THREAD_write_lock(dctx->lock);
436 RAND_DRBG_uninstantiate(dctx);
437 CRYPTO_THREAD_unlock(dctx->lock);
440 static const RAND_METHOD rand_drbg_meth =
450 const RAND_METHOD *RAND_drbg(void)
452 return &rand_drbg_meth;