]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rand/drbg_rand.c
Move ossl_assert
[thirdparty/openssl.git] / crypto / rand / drbg_rand.c
CommitLineData
12fb8c3d
RS
1/*
2 * Copyright 2011-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdlib.h>
11#include <string.h>
12#include <openssl/crypto.h>
13#include <openssl/err.h>
14#include <openssl/rand.h>
15#include "rand_lcl.h"
16#include "internal/thread_once.h"
17
18/*
19 * Mapping of NIST SP 800-90A DRBG to OpenSSL RAND_METHOD.
20 */
21
22
23/*
24 * The default global DRBG and its auto-init/auto-cleanup.
25 */
26static DRBG_CTX ossl_drbg;
27
28static CRYPTO_ONCE ossl_drbg_init = CRYPTO_ONCE_STATIC_INIT;
29
30DEFINE_RUN_ONCE_STATIC(do_ossl_drbg_init)
31{
8389ec4b
RS
32 int st = 1;
33
12fb8c3d 34 ossl_drbg.lock = CRYPTO_THREAD_lock_new();
8389ec4b
RS
35 st &= ossl_drbg.lock != NULL;
36 st &= RAND_DRBG_set(&ossl_drbg, NID_aes_128_ctr, 0) == 1;
37 return st;
12fb8c3d
RS
38}
39
40void rand_drbg_cleanup(void)
41{
42 CRYPTO_THREAD_lock_free(ossl_drbg.lock);
43}
44
45static void inc_128(DRBG_CTR_CTX *cctx)
46{
47 int i;
48 unsigned char c;
49 unsigned char *p = &cctx->V[15];
50
51 for (i = 0; i < 16; i++, p--) {
52 c = *p;
53 c++;
54 *p = c;
55 if (c != 0) {
56 /* If we didn't wrap around, we're done. */
57 break;
58 }
59 }
60}
61
62static void ctr_XOR(DRBG_CTR_CTX *cctx, const unsigned char *in, size_t inlen)
63{
64 size_t i, n;
65
66 if (in == NULL || inlen == 0)
67 return;
68
69 /*
70 * Any zero padding will have no effect on the result as we
71 * are XORing. So just process however much input we have.
72 */
73 n = inlen < cctx->keylen ? inlen : cctx->keylen;
74 for (i = 0; i < n; i++)
75 cctx->K[i] ^= in[i];
76 if (inlen <= cctx->keylen)
77 return;
78
79 n = inlen - cctx->keylen;
80 if (n > 16) {
81 /* Should never happen */
82 n = 16;
83 }
b8a437ff 84 for (i = 0; i < n; i++)
12fb8c3d
RS
85 cctx->V[i] ^= in[i + cctx->keylen];
86}
87
88/*
89 * Process a complete block using BCC algorithm of SP 800-90A 10.3.3
90 */
91static void ctr_BCC_block(DRBG_CTR_CTX *cctx, unsigned char *out,
92 const unsigned char *in)
93{
94 int i;
95
96 for (i = 0; i < 16; i++)
97 out[i] ^= in[i];
98 AES_encrypt(out, out, &cctx->df_ks);
99}
100
101
102/*
103 * Handle several BCC operations for as much data as we need for K and X
104 */
105static void ctr_BCC_blocks(DRBG_CTR_CTX *cctx, const unsigned char *in)
106{
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);
111}
112
113/*
114 * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
115 * see 10.3.1 stage 7.
116 */
117static void ctr_BCC_init(DRBG_CTR_CTX *cctx)
118{
119 memset(cctx->KX, 0, 48);
120 memset(cctx->bltmp, 0, 16);
121 ctr_BCC_block(cctx, cctx->KX, cctx->bltmp);
122 cctx->bltmp[3] = 1;
123 ctr_BCC_block(cctx, cctx->KX + 16, cctx->bltmp);
124 if (cctx->keylen != 16) {
125 cctx->bltmp[3] = 2;
126 ctr_BCC_block(cctx, cctx->KX + 32, cctx->bltmp);
127 }
128}
129
130/*
131 * Process several blocks into BCC algorithm, some possibly partial
132 */
133static void ctr_BCC_update(DRBG_CTR_CTX *cctx,
134 const unsigned char *in, size_t inlen)
135{
136 if (in == NULL || inlen == 0)
137 return;
138
139 /* If we have partial block handle it first */
140 if (cctx->bltmp_pos) {
141 size_t left = 16 - cctx->bltmp_pos;
142
143 /* If we now have a complete block process it */
144 if (inlen >= left) {
145 memcpy(cctx->bltmp + cctx->bltmp_pos, in, left);
146 ctr_BCC_blocks(cctx, cctx->bltmp);
147 cctx->bltmp_pos = 0;
148 inlen -= left;
149 in += left;
150 }
151 }
152
153 /* Process zero or more complete blocks */
154 for (; inlen >= 16; in += 16, inlen -= 16) {
155 ctr_BCC_blocks(cctx, in);
156 }
157
158 /* Copy any remaining partial block to the temporary buffer */
159 if (inlen > 0) {
160 memcpy(cctx->bltmp + cctx->bltmp_pos, in, inlen);
161 cctx->bltmp_pos += inlen;
162 }
163}
164
165static void ctr_BCC_final(DRBG_CTR_CTX *cctx)
166{
167 if (cctx->bltmp_pos) {
168 memset(cctx->bltmp + cctx->bltmp_pos, 0, 16 - cctx->bltmp_pos);
169 ctr_BCC_blocks(cctx, cctx->bltmp);
170 }
171}
172
173static 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)
177{
178 static unsigned char c80 = 0x80;
179 size_t inlen;
180 unsigned char *p = cctx->bltmp;
181
182 ctr_BCC_init(cctx);
183 if (in1 == NULL)
184 in1len = 0;
185 if (in2 == NULL)
186 in2len = 0;
187 if (in3 == NULL)
188 in3len = 0;
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;
194 *p++ = inlen & 0xff;
195
196 /* NB keylen is at most 32 bytes */
197 *p++ = 0;
198 *p++ = 0;
199 *p++ = 0;
200 *p = (unsigned char)((cctx->keylen + 16) & 0xff);
201 cctx->bltmp_pos = 8;
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);
206 ctr_BCC_final(cctx);
207 /* Set up key K */
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);
214}
215
216/*
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.
221 */
222static 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)
226{
227 DRBG_CTR_CTX *cctx = &dctx->ctr;
228
229 /* ks is already setup for correct key */
230 inc_128(cctx);
231 AES_encrypt(cctx->V, cctx->K, &cctx->ks);
232
233 /* If keylen longer than 128 bits need extra encrypt */
234 if (cctx->keylen != 16) {
235 inc_128(cctx);
236 AES_encrypt(cctx->V, cctx->K + 16, &cctx->ks);
237 }
238 inc_128(cctx);
239 AES_encrypt(cctx->V, cctx->V, &cctx->ks);
240
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);
245 }
246
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 */
252 if (in1len)
253 ctr_XOR(cctx, cctx->KX, dctx->seedlen);
254 } else {
255 ctr_XOR(cctx, in1, in1len);
256 ctr_XOR(cctx, in2, in2len);
257 }
258
259 AES_set_encrypt_key(cctx->K, dctx->strength, &cctx->ks);
260}
261
262int 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)
266{
267 DRBG_CTR_CTX *cctx = &dctx->ctr;
268
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);
273 return 1;
274}
275
276int ctr_reseed(DRBG_CTX *dctx,
277 const unsigned char *ent, size_t entlen,
278 const unsigned char *adin, size_t adinlen)
279{
280 ctr_update(dctx, ent, entlen, adin, adinlen, NULL, 0);
281 return 1;
282}
283
284int ctr_generate(DRBG_CTX *dctx,
285 unsigned char *out, size_t outlen,
286 const unsigned char *adin, size_t adinlen)
287{
288 DRBG_CTR_CTX *cctx = &dctx->ctr;
289
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) {
294 adin = NULL;
295 adinlen = 1;
296 }
297 } else {
298 adinlen = 0;
299 }
300
301 for ( ; ; ) {
302 inc_128(cctx);
303 if (outlen < 16) {
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);
307 break;
308 }
309 AES_encrypt(cctx->V, out, &cctx->ks);
310 out += 16;
311 outlen -= 16;
312 if (outlen == 0)
313 break;
314 }
315
316 ctr_update(dctx, adin, adinlen, NULL, 0, NULL, 0);
317 return 1;
318}
319
320int ctr_uninstantiate(DRBG_CTX *dctx)
321{
322 memset(&dctx->ctr, 0, sizeof(dctx->ctr));
323 return 1;
324}
325
326int ctr_init(DRBG_CTX *dctx)
327{
328 DRBG_CTR_CTX *cctx = &dctx->ctr;
329 size_t keylen;
330
331 switch (dctx->nid) {
332 default:
333 /* This can't happen, but silence the compiler warning. */
334 return -1;
335 case NID_aes_128_ctr:
336 keylen = 16;
337 break;
338 case NID_aes_192_ctr:
339 keylen = 24;
340 break;
341 case NID_aes_256_ctr:
342 keylen = 32;
343 break;
344 }
345
346 cctx->keylen = keylen;
347 dctx->strength = keylen * 8;
348 dctx->blocklength = 16;
349 dctx->seedlen = keylen + 16;
350
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
358 };
359 /* Set key schedule for df_key */
360 AES_set_encrypt_key(df_key, dctx->strength, &cctx->df_ks);
361
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;
368 } else {
369 dctx->min_entropy = dctx->seedlen;
370 dctx->max_entropy = dctx->seedlen;
371 /* Nonce not used */
372 dctx->min_nonce = 0;
373 dctx->max_nonce = 0;
374 dctx->max_pers = dctx->seedlen;
375 dctx->max_adin = dctx->seedlen;
376 }
377
378 dctx->max_request = 1 << 16;
4c75ee85 379 dctx->reseed_interval = MAX_RESEED;
12fb8c3d
RS
380 return 1;
381}
382
383
384/*
385 * The following function tie the DRBG code into the RAND_METHOD
386 */
387
388DRBG_CTX *RAND_DRBG_get_default(void)
389{
390 if (!RUN_ONCE(&ossl_drbg_init, do_ossl_drbg_init))
391 return NULL;
392 return &ossl_drbg;
393}
394
395static int drbg_bytes(unsigned char *out, int count)
396{
397 DRBG_CTX *dctx = RAND_DRBG_get_default();
398 int ret = 0;
399
400 CRYPTO_THREAD_write_lock(dctx->lock);
401 do {
402 size_t rcnt;
403
404 if (count > (int)dctx->max_request)
405 rcnt = dctx->max_request;
406 else
407 rcnt = count;
408 ret = RAND_DRBG_generate(dctx, out, rcnt, 0, NULL, 0);
409 if (!ret)
410 goto err;
411 out += rcnt;
412 count -= rcnt;
413 } while (count);
414 ret = 1;
415err:
416 CRYPTO_THREAD_unlock(dctx->lock);
417 return ret;
418}
419
420static int drbg_status(void)
421{
422 DRBG_CTX *dctx = RAND_DRBG_get_default();
423 int ret;
424
425 CRYPTO_THREAD_write_lock(dctx->lock);
426 ret = dctx->status == DRBG_STATUS_READY ? 1 : 0;
427 CRYPTO_THREAD_unlock(dctx->lock);
428 return ret;
429}
430
431static void drbg_cleanup(void)
432{
433 DRBG_CTX *dctx = RAND_DRBG_get_default();
434
435 CRYPTO_THREAD_write_lock(dctx->lock);
436 RAND_DRBG_uninstantiate(dctx);
437 CRYPTO_THREAD_unlock(dctx->lock);
438}
439
440static const RAND_METHOD rand_drbg_meth =
441{
442 NULL,
443 drbg_bytes,
444 drbg_cleanup,
445 NULL,
446 drbg_bytes,
447 drbg_status
448};
449
450const RAND_METHOD *RAND_drbg(void)
451{
452 return &rand_drbg_meth;
453}