Coverage Report

Created: 2023-09-25 06:45

/src/openssl30/providers/implementations/rands/drbg_ctr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2011-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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 <openssl/aes.h>
16
#include <openssl/proverr.h>
17
#include "crypto/modes.h"
18
#include "internal/thread_once.h"
19
#include "prov/implementations.h"
20
#include "prov/providercommon.h"
21
#include "prov/provider_ctx.h"
22
#include "drbg_local.h"
23
24
static OSSL_FUNC_rand_newctx_fn drbg_ctr_new_wrapper;
25
static OSSL_FUNC_rand_freectx_fn drbg_ctr_free;
26
static OSSL_FUNC_rand_instantiate_fn drbg_ctr_instantiate_wrapper;
27
static OSSL_FUNC_rand_uninstantiate_fn drbg_ctr_uninstantiate_wrapper;
28
static OSSL_FUNC_rand_generate_fn drbg_ctr_generate_wrapper;
29
static OSSL_FUNC_rand_reseed_fn drbg_ctr_reseed_wrapper;
30
static OSSL_FUNC_rand_settable_ctx_params_fn drbg_ctr_settable_ctx_params;
31
static OSSL_FUNC_rand_set_ctx_params_fn drbg_ctr_set_ctx_params;
32
static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_ctr_gettable_ctx_params;
33
static OSSL_FUNC_rand_get_ctx_params_fn drbg_ctr_get_ctx_params;
34
static OSSL_FUNC_rand_verify_zeroization_fn drbg_ctr_verify_zeroization;
35
36
/*
37
 * The state of a DRBG AES-CTR.
38
 */
39
typedef struct rand_drbg_ctr_st {
40
    EVP_CIPHER_CTX *ctx_ecb;
41
    EVP_CIPHER_CTX *ctx_ctr;
42
    EVP_CIPHER_CTX *ctx_df;
43
    EVP_CIPHER *cipher_ecb;
44
    EVP_CIPHER *cipher_ctr;
45
    size_t keylen;
46
    int use_df;
47
    unsigned char K[32];
48
    unsigned char V[16];
49
    /* Temporary block storage used by ctr_df */
50
    unsigned char bltmp[16];
51
    size_t bltmp_pos;
52
    unsigned char KX[48];
53
} PROV_DRBG_CTR;
54
55
/*
56
 * Implementation of NIST SP 800-90A CTR DRBG.
57
 */
58
static void inc_128(PROV_DRBG_CTR *ctr)
59
145k
{
60
145k
    unsigned char *p = &ctr->V[0];
61
145k
    u32 n = 16, c = 1;
62
63
2.32M
    do {
64
2.32M
        --n;
65
2.32M
        c += p[n];
66
2.32M
        p[n] = (u8)c;
67
2.32M
        c >>= 8;
68
2.32M
    } while (n);
69
145k
}
70
71
static void ctr_XOR(PROV_DRBG_CTR *ctr, const unsigned char *in, size_t inlen)
72
47.5k
{
73
47.5k
    size_t i, n;
74
75
47.5k
    if (in == NULL || inlen == 0)
76
15.2k
        return;
77
78
    /*
79
     * Any zero padding will have no effect on the result as we
80
     * are XORing. So just process however much input we have.
81
     */
82
32.3k
    n = inlen < ctr->keylen ? inlen : ctr->keylen;
83
1.06M
    for (i = 0; i < n; i++)
84
1.03M
        ctr->K[i] ^= in[i];
85
32.3k
    if (inlen <= ctr->keylen)
86
1
        return;
87
88
32.3k
    n = inlen - ctr->keylen;
89
32.3k
    if (n > 16) {
90
        /* Should never happen */
91
0
        n = 16;
92
0
    }
93
549k
    for (i = 0; i < n; i++)
94
517k
        ctr->V[i] ^= in[i + ctr->keylen];
95
32.3k
}
96
97
/*
98
 * Process a complete block using BCC algorithm of SP 800-90A 10.3.3
99
 */
100
__owur static int ctr_BCC_block(PROV_DRBG_CTR *ctr, unsigned char *out,
101
                                const unsigned char *in, int len)
102
64.6k
{
103
64.6k
    int i, outlen = AES_BLOCK_SIZE;
104
105
3.16M
    for (i = 0; i < len; i++)
106
3.10M
        out[i] ^= in[i];
107
108
64.6k
    if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, len)
109
64.6k
        || outlen != len)
110
0
        return 0;
111
64.6k
    return 1;
112
64.6k
}
113
114
115
/*
116
 * Handle several BCC operations for as much data as we need for K and X
117
 */
118
__owur static int ctr_BCC_blocks(PROV_DRBG_CTR *ctr, const unsigned char *in)
119
48.5k
{
120
48.5k
    unsigned char in_tmp[48];
121
48.5k
    unsigned char num_of_blk = 2;
122
123
48.5k
    memcpy(in_tmp, in, 16);
124
48.5k
    memcpy(in_tmp + 16, in, 16);
125
48.5k
    if (ctr->keylen != 16) {
126
48.5k
        memcpy(in_tmp + 32, in, 16);
127
48.5k
        num_of_blk = 3;
128
48.5k
    }
129
48.5k
    return ctr_BCC_block(ctr, ctr->KX, in_tmp, AES_BLOCK_SIZE * num_of_blk);
130
48.5k
}
131
132
/*
133
 * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
134
 * see 10.3.1 stage 7.
135
 */
136
__owur static int ctr_BCC_init(PROV_DRBG_CTR *ctr)
137
16.1k
{
138
16.1k
    unsigned char bltmp[48] = {0};
139
16.1k
    unsigned char num_of_blk;
140
141
16.1k
    memset(ctr->KX, 0, 48);
142
16.1k
    num_of_blk = ctr->keylen == 16 ? 2 : 3;
143
16.1k
    bltmp[(AES_BLOCK_SIZE * 1) + 3] = 1;
144
16.1k
    bltmp[(AES_BLOCK_SIZE * 2) + 3] = 2;
145
16.1k
    return ctr_BCC_block(ctr, ctr->KX, bltmp, num_of_blk * AES_BLOCK_SIZE);
146
16.1k
}
147
148
/*
149
 * Process several blocks into BCC algorithm, some possibly partial
150
 */
151
__owur static int ctr_BCC_update(PROV_DRBG_CTR *ctr,
152
                                 const unsigned char *in, size_t inlen)
153
64.6k
{
154
64.6k
    if (in == NULL || inlen == 0)
155
32.3k
        return 1;
156
157
    /* If we have partial block handle it first */
158
32.3k
    if (ctr->bltmp_pos) {
159
16.1k
        size_t left = 16 - ctr->bltmp_pos;
160
161
        /* If we now have a complete block process it */
162
16.1k
        if (inlen >= left) {
163
16.1k
            memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);
164
16.1k
            if (!ctr_BCC_blocks(ctr, ctr->bltmp))
165
0
                return 0;
166
16.1k
            ctr->bltmp_pos = 0;
167
16.1k
            inlen -= left;
168
16.1k
            in += left;
169
16.1k
        }
170
16.1k
    }
171
172
    /* Process zero or more complete blocks */
173
48.5k
    for (; inlen >= 16; in += 16, inlen -= 16) {
174
16.1k
        if (!ctr_BCC_blocks(ctr, in))
175
0
            return 0;
176
16.1k
    }
177
178
    /* Copy any remaining partial block to the temporary buffer */
179
32.3k
    if (inlen > 0) {
180
16.1k
        memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);
181
16.1k
        ctr->bltmp_pos += inlen;
182
16.1k
    }
183
32.3k
    return 1;
184
32.3k
}
185
186
__owur static int ctr_BCC_final(PROV_DRBG_CTR *ctr)
187
16.1k
{
188
16.1k
    if (ctr->bltmp_pos) {
189
16.1k
        memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);
190
16.1k
        if (!ctr_BCC_blocks(ctr, ctr->bltmp))
191
0
            return 0;
192
16.1k
    }
193
16.1k
    return 1;
194
16.1k
}
195
196
__owur static int ctr_df(PROV_DRBG_CTR *ctr,
197
                         const unsigned char *in1, size_t in1len,
198
                         const unsigned char *in2, size_t in2len,
199
                         const unsigned char *in3, size_t in3len)
200
16.1k
{
201
16.1k
    static unsigned char c80 = 0x80;
202
16.1k
    size_t inlen;
203
16.1k
    unsigned char *p = ctr->bltmp;
204
16.1k
    int outlen = AES_BLOCK_SIZE;
205
206
16.1k
    if (!ctr_BCC_init(ctr))
207
0
        return 0;
208
16.1k
    if (in1 == NULL)
209
0
        in1len = 0;
210
16.1k
    if (in2 == NULL)
211
16.1k
        in2len = 0;
212
16.1k
    if (in3 == NULL)
213
16.1k
        in3len = 0;
214
16.1k
    inlen = in1len + in2len + in3len;
215
    /* Initialise L||N in temporary block */
216
16.1k
    *p++ = (inlen >> 24) & 0xff;
217
16.1k
    *p++ = (inlen >> 16) & 0xff;
218
16.1k
    *p++ = (inlen >> 8) & 0xff;
219
16.1k
    *p++ = inlen & 0xff;
220
221
    /* NB keylen is at most 32 bytes */
222
16.1k
    *p++ = 0;
223
16.1k
    *p++ = 0;
224
16.1k
    *p++ = 0;
225
16.1k
    *p = (unsigned char)((ctr->keylen + 16) & 0xff);
226
16.1k
    ctr->bltmp_pos = 8;
227
16.1k
    if (!ctr_BCC_update(ctr, in1, in1len)
228
16.1k
        || !ctr_BCC_update(ctr, in2, in2len)
229
16.1k
        || !ctr_BCC_update(ctr, in3, in3len)
230
16.1k
        || !ctr_BCC_update(ctr, &c80, 1)
231
16.1k
        || !ctr_BCC_final(ctr))
232
0
        return 0;
233
    /* Set up key K */
234
16.1k
    if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->KX, NULL, -1))
235
0
        return 0;
236
    /* X follows key K */
237
16.1k
    if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX, &outlen, ctr->KX + ctr->keylen,
238
16.1k
                          AES_BLOCK_SIZE)
239
16.1k
        || outlen != AES_BLOCK_SIZE)
240
0
        return 0;
241
16.1k
    if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 16, &outlen, ctr->KX,
242
16.1k
                          AES_BLOCK_SIZE)
243
16.1k
        || outlen != AES_BLOCK_SIZE)
244
0
        return 0;
245
16.1k
    if (ctr->keylen != 16)
246
16.1k
        if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 32, &outlen,
247
16.1k
                              ctr->KX + 16, AES_BLOCK_SIZE)
248
16.1k
            || outlen != AES_BLOCK_SIZE)
249
0
            return 0;
250
16.1k
    return 1;
251
16.1k
}
252
253
/*
254
 * NB the no-df Update in SP800-90A specifies a constant input length
255
 * of seedlen, however other uses of this algorithm pad the input with
256
 * zeroes if necessary and have up to two parameters XORed together,
257
 * so we handle both cases in this function instead.
258
 */
259
__owur static int ctr_update(PROV_DRBG *drbg,
260
                             const unsigned char *in1, size_t in1len,
261
                             const unsigned char *in2, size_t in2len,
262
                             const unsigned char *nonce, size_t noncelen)
263
16.0k
{
264
16.0k
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
265
16.0k
    int outlen = AES_BLOCK_SIZE;
266
16.0k
    unsigned char V_tmp[48], out[48];
267
16.0k
    unsigned char len;
268
269
    /* correct key is already set up. */
270
16.0k
    memcpy(V_tmp, ctr->V, 16);
271
16.0k
    inc_128(ctr);
272
16.0k
    memcpy(V_tmp + 16, ctr->V, 16);
273
16.0k
    if (ctr->keylen == 16) {
274
0
        len = 32;
275
16.0k
    } else {
276
16.0k
        inc_128(ctr);
277
16.0k
        memcpy(V_tmp + 32, ctr->V, 16);
278
16.0k
        len = 48;
279
16.0k
    }
280
16.0k
    if (!EVP_CipherUpdate(ctr->ctx_ecb, out, &outlen, V_tmp, len)
281
16.0k
            || outlen != len)
282
0
        return 0;
283
16.0k
    memcpy(ctr->K, out, ctr->keylen);
284
16.0k
    memcpy(ctr->V, out + ctr->keylen, 16);
285
286
16.0k
    if (ctr->use_df) {
287
        /* If no input reuse existing derived value */
288
8.41k
        if (in1 != NULL || nonce != NULL || in2 != NULL)
289
8
            if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
290
0
                return 0;
291
        /* If this a reuse input in1len != 0 */
292
8.41k
        if (in1len)
293
12
            ctr_XOR(ctr, ctr->KX, drbg->seedlen);
294
8.41k
    } else {
295
7.62k
        ctr_XOR(ctr, in1, in1len);
296
7.62k
        ctr_XOR(ctr, in2, in2len);
297
7.62k
    }
298
299
16.0k
    if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1)
300
16.0k
        || !EVP_CipherInit_ex(ctr->ctx_ctr, NULL, NULL, ctr->K, NULL, -1))
301
0
        return 0;
302
16.0k
    return 1;
303
16.0k
}
304
305
static int drbg_ctr_instantiate(PROV_DRBG *drbg,
306
                                const unsigned char *entropy, size_t entropylen,
307
                                const unsigned char *nonce, size_t noncelen,
308
                                const unsigned char *pers, size_t perslen)
309
6
{
310
6
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
311
312
6
    if (entropy == NULL)
313
0
        return 0;
314
315
6
    memset(ctr->K, 0, sizeof(ctr->K));
316
6
    memset(ctr->V, 0, sizeof(ctr->V));
317
6
    if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1))
318
0
        return 0;
319
320
6
    inc_128(ctr);
321
6
    if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))
322
0
        return 0;
323
6
    return 1;
324
6
}
325
326
static int drbg_ctr_instantiate_wrapper(void *vdrbg, unsigned int strength,
327
                                        int prediction_resistance,
328
                                        const unsigned char *pstr,
329
                                        size_t pstr_len,
330
                                        const OSSL_PARAM params[])
331
2
{
332
2
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
333
334
2
    if (!ossl_prov_is_running() || !drbg_ctr_set_ctx_params(drbg, params))
335
0
        return 0;
336
2
    return ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
337
2
                                      pstr, pstr_len);
338
2
}
339
340
static int drbg_ctr_reseed(PROV_DRBG *drbg,
341
                           const unsigned char *entropy, size_t entropylen,
342
                           const unsigned char *adin, size_t adinlen)
343
2
{
344
2
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
345
346
2
    if (entropy == NULL)
347
0
        return 0;
348
349
2
    inc_128(ctr);
350
2
    if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))
351
0
        return 0;
352
2
    return 1;
353
2
}
354
355
static int drbg_ctr_reseed_wrapper(void *vdrbg, int prediction_resistance,
356
                                   const unsigned char *ent, size_t ent_len,
357
                                   const unsigned char *adin, size_t adin_len)
358
0
{
359
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
360
361
0
    return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
362
0
                                 adin, adin_len);
363
0
}
364
365
static void ctr96_inc(unsigned char *counter)
366
0
{
367
0
    u32 n = 12, c = 1;
368
369
0
    do {
370
0
        --n;
371
0
        c += counter[n];
372
0
        counter[n] = (u8)c;
373
0
        c >>= 8;
374
0
    } while (n);
375
0
}
376
377
static int drbg_ctr_generate(PROV_DRBG *drbg,
378
                             unsigned char *out, size_t outlen,
379
                             const unsigned char *adin, size_t adinlen)
380
16.0k
{
381
16.0k
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
382
16.0k
    unsigned int ctr32, blocks;
383
16.0k
    int outl, buflen;
384
385
16.0k
    if (adin != NULL && adinlen != 0) {
386
4
        inc_128(ctr);
387
388
4
        if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
389
0
            return 0;
390
        /* This means we reuse derived value */
391
4
        if (ctr->use_df) {
392
4
            adin = NULL;
393
4
            adinlen = 1;
394
4
        }
395
16.0k
    } else {
396
16.0k
        adinlen = 0;
397
16.0k
    }
398
399
16.0k
    inc_128(ctr);
400
401
16.0k
    if (outlen == 0) {
402
0
        inc_128(ctr);
403
404
0
        if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
405
0
            return 0;
406
0
        return 1;
407
0
    }
408
409
16.0k
    memset(out, 0, outlen);
410
411
16.0k
    do {
412
16.0k
        if (!EVP_CipherInit_ex(ctr->ctx_ctr,
413
16.0k
                               NULL, NULL, NULL, ctr->V, -1))
414
0
            return 0;
415
416
        /*-
417
         * outlen has type size_t while EVP_CipherUpdate takes an
418
         * int argument and thus cannot be guaranteed to process more
419
         * than 2^31-1 bytes at a time. We process such huge generate
420
         * requests in 2^30 byte chunks, which is the greatest multiple
421
         * of AES block size lower than or equal to 2^31-1.
422
         */
423
16.0k
        buflen = outlen > (1U << 30) ? (1U << 30) : outlen;
424
16.0k
        blocks = (buflen + 15) / 16;
425
426
16.0k
        ctr32 = GETU32(ctr->V + 12) + blocks;
427
16.0k
        if (ctr32 < blocks) {
428
            /* 32-bit counter overflow into V. */
429
0
            if (ctr32 != 0) {
430
0
                blocks -= ctr32;
431
0
                buflen = blocks * 16;
432
0
                ctr32 = 0;
433
0
            }
434
0
            ctr96_inc(ctr->V);
435
0
        }
436
16.0k
        PUTU32(ctr->V + 12, ctr32);
437
438
16.0k
        if (!EVP_CipherUpdate(ctr->ctx_ctr, out, &outl, out, buflen)
439
16.0k
            || outl != buflen)
440
0
            return 0;
441
442
16.0k
        out += buflen;
443
16.0k
        outlen -= buflen;
444
16.0k
    } while (outlen);
445
446
16.0k
    if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
447
0
        return 0;
448
16.0k
    return 1;
449
16.0k
}
450
451
static int drbg_ctr_generate_wrapper
452
    (void *vdrbg, unsigned char *out, size_t outlen,
453
     unsigned int strength, int prediction_resistance,
454
     const unsigned char *adin, size_t adin_len)
455
16.0k
{
456
16.0k
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
457
458
16.0k
    return ossl_prov_drbg_generate(drbg, out, outlen, strength,
459
16.0k
                                   prediction_resistance, adin, adin_len);
460
16.0k
}
461
462
static int drbg_ctr_uninstantiate(PROV_DRBG *drbg)
463
2
{
464
2
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
465
466
2
    OPENSSL_cleanse(ctr->K, sizeof(ctr->K));
467
2
    OPENSSL_cleanse(ctr->V, sizeof(ctr->V));
468
2
    OPENSSL_cleanse(ctr->bltmp, sizeof(ctr->bltmp));
469
2
    OPENSSL_cleanse(ctr->KX, sizeof(ctr->KX));
470
2
    ctr->bltmp_pos = 0;
471
2
    return ossl_prov_drbg_uninstantiate(drbg);
472
2
}
473
474
static int drbg_ctr_uninstantiate_wrapper(void *vdrbg)
475
0
{
476
0
    return drbg_ctr_uninstantiate((PROV_DRBG *)vdrbg);
477
0
}
478
479
static int drbg_ctr_verify_zeroization(void *vdrbg)
480
0
{
481
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
482
0
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
483
484
0
    PROV_DRBG_VERYIFY_ZEROIZATION(ctr->K);
485
0
    PROV_DRBG_VERYIFY_ZEROIZATION(ctr->V);
486
0
    PROV_DRBG_VERYIFY_ZEROIZATION(ctr->bltmp);
487
0
    PROV_DRBG_VERYIFY_ZEROIZATION(ctr->KX);
488
0
    if (ctr->bltmp_pos != 0)
489
0
        return 0;
490
0
    return 1;
491
0
}
492
493
static int drbg_ctr_init_lengths(PROV_DRBG *drbg)
494
8
{
495
8
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
496
8
    int res = 1;
497
498
    /* Maximum number of bits per request = 2^19  = 2^16 bytes */
499
8
    drbg->max_request = 1 << 16;
500
8
    if (ctr->use_df) {
501
7
        drbg->min_entropylen = 0;
502
7
        drbg->max_entropylen = DRBG_MAX_LENGTH;
503
7
        drbg->min_noncelen = 0;
504
7
        drbg->max_noncelen = DRBG_MAX_LENGTH;
505
7
        drbg->max_perslen = DRBG_MAX_LENGTH;
506
7
        drbg->max_adinlen = DRBG_MAX_LENGTH;
507
508
7
        if (ctr->keylen > 0) {
509
3
            drbg->min_entropylen = ctr->keylen;
510
3
            drbg->min_noncelen = drbg->min_entropylen / 2;
511
3
        }
512
7
    } else {
513
1
        const size_t len = ctr->keylen > 0 ? drbg->seedlen : DRBG_MAX_LENGTH;
514
515
1
        drbg->min_entropylen = len;
516
1
        drbg->max_entropylen = len;
517
        /* Nonce not used */
518
1
        drbg->min_noncelen = 0;
519
1
        drbg->max_noncelen = 0;
520
1
        drbg->max_perslen = len;
521
1
        drbg->max_adinlen = len;
522
1
    }
523
8
    return res;
524
8
}
525
526
static int drbg_ctr_init(PROV_DRBG *drbg)
527
4
{
528
4
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
529
4
    size_t keylen;
530
531
4
    if (ctr->cipher_ctr == NULL) {
532
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
533
0
        return 0;
534
0
    }
535
4
    ctr->keylen = keylen = EVP_CIPHER_get_key_length(ctr->cipher_ctr);
536
4
    if (ctr->ctx_ecb == NULL)
537
4
        ctr->ctx_ecb = EVP_CIPHER_CTX_new();
538
4
    if (ctr->ctx_ctr == NULL)
539
4
        ctr->ctx_ctr = EVP_CIPHER_CTX_new();
540
4
    if (ctr->ctx_ecb == NULL || ctr->ctx_ctr == NULL) {
541
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
542
0
        goto err;
543
0
    }
544
545
4
    if (!EVP_CipherInit_ex(ctr->ctx_ecb,
546
4
                           ctr->cipher_ecb, NULL, NULL, NULL, 1)
547
4
        || !EVP_CipherInit_ex(ctr->ctx_ctr,
548
4
                              ctr->cipher_ctr, NULL, NULL, NULL, 1)) {
549
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_INITIALISE_CIPHERS);
550
0
        goto err;
551
0
    }
552
553
4
    drbg->strength = keylen * 8;
554
4
    drbg->seedlen = keylen + 16;
555
556
4
    if (ctr->use_df) {
557
        /* df initialisation */
558
3
        static const unsigned char df_key[32] = {
559
3
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
560
3
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
561
3
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
562
3
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
563
3
        };
564
565
3
        if (ctr->ctx_df == NULL)
566
3
            ctr->ctx_df = EVP_CIPHER_CTX_new();
567
3
        if (ctr->ctx_df == NULL) {
568
0
            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
569
0
            goto err;
570
0
        }
571
        /* Set key schedule for df_key */
572
3
        if (!EVP_CipherInit_ex(ctr->ctx_df,
573
3
                               ctr->cipher_ecb, NULL, df_key, NULL, 1)) {
574
0
            ERR_raise(ERR_LIB_PROV, PROV_R_DERIVATION_FUNCTION_INIT_FAILED);
575
0
            goto err;
576
0
        }
577
3
    }
578
4
    return drbg_ctr_init_lengths(drbg);
579
580
0
err:
581
0
    EVP_CIPHER_CTX_free(ctr->ctx_ecb);
582
0
    EVP_CIPHER_CTX_free(ctr->ctx_ctr);
583
0
    ctr->ctx_ecb = ctr->ctx_ctr = NULL;
584
0
    return 0;    
585
4
}
586
587
static int drbg_ctr_new(PROV_DRBG *drbg)
588
4
{
589
4
    PROV_DRBG_CTR *ctr;
590
591
4
    ctr = OPENSSL_secure_zalloc(sizeof(*ctr));
592
4
    if (ctr == NULL) {
593
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
594
0
        return 0;
595
0
    }
596
597
4
    ctr->use_df = 1;
598
4
    drbg->data = ctr;
599
4
    return drbg_ctr_init_lengths(drbg);
600
4
}
601
602
static void *drbg_ctr_new_wrapper(void *provctx, void *parent,
603
                                   const OSSL_DISPATCH *parent_dispatch)
604
4
{
605
4
    return ossl_rand_drbg_new(provctx, parent, parent_dispatch, &drbg_ctr_new,
606
4
                              &drbg_ctr_instantiate, &drbg_ctr_uninstantiate,
607
4
                              &drbg_ctr_reseed, &drbg_ctr_generate);
608
4
}
609
610
static void drbg_ctr_free(void *vdrbg)
611
4
{
612
4
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
613
4
    PROV_DRBG_CTR *ctr;
614
615
4
    if (drbg != NULL && (ctr = (PROV_DRBG_CTR *)drbg->data) != NULL) {
616
4
        EVP_CIPHER_CTX_free(ctr->ctx_ecb);
617
4
        EVP_CIPHER_CTX_free(ctr->ctx_ctr);
618
4
        EVP_CIPHER_CTX_free(ctr->ctx_df);
619
4
        EVP_CIPHER_free(ctr->cipher_ecb);
620
4
        EVP_CIPHER_free(ctr->cipher_ctr);
621
622
4
        OPENSSL_secure_clear_free(ctr, sizeof(*ctr));
623
4
    }
624
4
    ossl_rand_drbg_free(drbg);
625
4
}
626
627
static int drbg_ctr_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
628
16.8k
{
629
16.8k
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
630
16.8k
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
631
16.8k
    OSSL_PARAM *p;
632
633
16.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_USE_DF);
634
16.8k
    if (p != NULL && !OSSL_PARAM_set_int(p, ctr->use_df))
635
0
        return 0;
636
637
16.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_CIPHER);
638
16.8k
    if (p != NULL) {
639
0
        if (ctr->cipher_ctr == NULL
640
0
            || !OSSL_PARAM_set_utf8_string(p,
641
0
                                           EVP_CIPHER_get0_name(ctr->cipher_ctr)))
642
0
            return 0;
643
0
    }
644
645
16.8k
    return ossl_drbg_get_ctx_params(drbg, params);
646
16.8k
}
647
648
static const OSSL_PARAM *drbg_ctr_gettable_ctx_params(ossl_unused void *vctx,
649
                                                      ossl_unused void *provctx)
650
0
{
651
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
652
0
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),
653
0
        OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),
654
0
        OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
655
0
        OSSL_PARAM_END
656
0
    };
657
0
    return known_gettable_ctx_params;
658
0
}
659
660
static int drbg_ctr_set_ctx_params(void *vctx, const OSSL_PARAM params[])
661
2
{
662
2
    PROV_DRBG *ctx = (PROV_DRBG *)vctx;
663
2
    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)ctx->data;
664
2
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
665
2
    const OSSL_PARAM *p;
666
2
    char *ecb;
667
2
    const char *propquery = NULL;
668
2
    int i, cipher_init = 0;
669
670
2
    if ((p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_USE_DF)) != NULL
671
2
            && OSSL_PARAM_get_int(p, &i)) {
672
        /* FIPS errors out in the drbg_ctr_init() call later */
673
0
        ctr->use_df = i != 0;
674
0
        cipher_init = 1;
675
0
    }
676
677
2
    if ((p = OSSL_PARAM_locate_const(params,
678
2
                                     OSSL_DRBG_PARAM_PROPERTIES)) != NULL) {
679
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
680
0
            return 0;
681
0
        propquery = (const char *)p->data;
682
0
    }
683
684
2
    if ((p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_CIPHER)) != NULL) {
685
2
        const char *base = (const char *)p->data;
686
2
        size_t ctr_str_len = sizeof("CTR") - 1;
687
2
        size_t ecb_str_len = sizeof("ECB") - 1;
688
689
2
        if (p->data_type != OSSL_PARAM_UTF8_STRING
690
2
                || p->data_size < ctr_str_len)
691
0
            return 0;
692
2
        if (OPENSSL_strcasecmp("CTR", base + p->data_size - ctr_str_len) != 0) {
693
0
            ERR_raise(ERR_LIB_PROV, PROV_R_REQUIRE_CTR_MODE_CIPHER);
694
0
            return 0;
695
0
        }
696
2
        if ((ecb = OPENSSL_strndup(base, p->data_size)) == NULL) {
697
0
            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
698
0
            return 0;
699
0
        }
700
2
        strcpy(ecb + p->data_size - ecb_str_len, "ECB");
701
2
        EVP_CIPHER_free(ctr->cipher_ecb);
702
2
        EVP_CIPHER_free(ctr->cipher_ctr);
703
2
        ctr->cipher_ctr = EVP_CIPHER_fetch(libctx, base, propquery);
704
2
        ctr->cipher_ecb = EVP_CIPHER_fetch(libctx, ecb, propquery);
705
2
        OPENSSL_free(ecb);
706
2
        if (ctr->cipher_ctr == NULL || ctr->cipher_ecb == NULL) {
707
0
            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_FIND_CIPHERS);
708
0
            return 0;
709
0
        }
710
2
        cipher_init = 1;
711
2
    }
712
713
2
    if (cipher_init && !drbg_ctr_init(ctx))
714
0
        return 0;
715
716
2
    return ossl_drbg_set_ctx_params(ctx, params);
717
2
}
718
719
static const OSSL_PARAM *drbg_ctr_settable_ctx_params(ossl_unused void *vctx,
720
                                                      ossl_unused void *provctx)
721
2
{
722
2
    static const OSSL_PARAM known_settable_ctx_params[] = {
723
2
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
724
2
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),
725
2
        OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),
726
2
        OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
727
2
        OSSL_PARAM_END
728
2
    };
729
2
    return known_settable_ctx_params;
730
2
}
731
732
const OSSL_DISPATCH ossl_drbg_ctr_functions[] = {
733
    { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_ctr_new_wrapper },
734
    { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_ctr_free },
735
    { OSSL_FUNC_RAND_INSTANTIATE,
736
      (void(*)(void))drbg_ctr_instantiate_wrapper },
737
    { OSSL_FUNC_RAND_UNINSTANTIATE,
738
      (void(*)(void))drbg_ctr_uninstantiate_wrapper },
739
    { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_ctr_generate_wrapper },
740
    { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_ctr_reseed_wrapper },
741
    { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
742
    { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
743
    { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
744
    { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
745
      (void(*)(void))drbg_ctr_settable_ctx_params },
746
    { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_ctr_set_ctx_params },
747
    { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
748
      (void(*)(void))drbg_ctr_gettable_ctx_params },
749
    { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_ctr_get_ctx_params },
750
    { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
751
      (void(*)(void))drbg_ctr_verify_zeroization },
752
    { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
753
    { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
754
    { 0, NULL }
755
};