Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/providers/implementations/rands/drbg_hmac.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2011-2024 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/proverr.h>
16
#include "prov/provider_util.h"
17
#include "internal/thread_once.h"
18
#include "prov/providercommon.h"
19
#include "prov/implementations.h"
20
#include "prov/provider_ctx.h"
21
#include "drbg_local.h"
22
23
static OSSL_FUNC_rand_newctx_fn drbg_hmac_new_wrapper;
24
static OSSL_FUNC_rand_freectx_fn drbg_hmac_free;
25
static OSSL_FUNC_rand_instantiate_fn drbg_hmac_instantiate_wrapper;
26
static OSSL_FUNC_rand_uninstantiate_fn drbg_hmac_uninstantiate_wrapper;
27
static OSSL_FUNC_rand_generate_fn drbg_hmac_generate_wrapper;
28
static OSSL_FUNC_rand_reseed_fn drbg_hmac_reseed_wrapper;
29
static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hmac_settable_ctx_params;
30
static OSSL_FUNC_rand_set_ctx_params_fn drbg_hmac_set_ctx_params;
31
static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hmac_gettable_ctx_params;
32
static OSSL_FUNC_rand_get_ctx_params_fn drbg_hmac_get_ctx_params;
33
static OSSL_FUNC_rand_verify_zeroization_fn drbg_hmac_verify_zeroization;
34
35
typedef struct rand_drbg_hmac_st {
36
    EVP_MAC_CTX *ctx;            /* H(x) = HMAC_hash OR H(x) = KMAC */
37
    PROV_DIGEST digest;          /* H(x) = hash(x) */
38
    size_t blocklen;
39
    unsigned char K[EVP_MAX_MD_SIZE];
40
    unsigned char V[EVP_MAX_MD_SIZE];
41
} PROV_DRBG_HMAC;
42
43
/*
44
 * Called twice by SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process.
45
 *
46
 * hmac is an object that holds the input/output Key and Value (K and V).
47
 * inbyte is 0x00 on the first call and 0x01 on the second call.
48
 * in1, in2, in3 are optional inputs that can be NULL.
49
 * in1len, in2len, in3len are the lengths of the input buffers.
50
 *
51
 * The returned K,V is:
52
 *   hmac->K = HMAC(hmac->K, hmac->V || inbyte || [in1] || [in2] || [in3])
53
 *   hmac->V = HMAC(hmac->K, hmac->V)
54
 *
55
 * Returns zero if an error occurs otherwise it returns 1.
56
 */
57
static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte,
58
                   const unsigned char *in1, size_t in1len,
59
                   const unsigned char *in2, size_t in2len,
60
                   const unsigned char *in3, size_t in3len)
61
596
{
62
596
    EVP_MAC_CTX *ctx = hmac->ctx;
63
64
596
    if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
65
            /* K = HMAC(K, V || inbyte || [in1] || [in2] || [in3]) */
66
596
            || !EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
67
596
            || !EVP_MAC_update(ctx, &inbyte, 1)
68
596
            || !(in1 == NULL || in1len == 0 || EVP_MAC_update(ctx, in1, in1len))
69
596
            || !(in2 == NULL || in2len == 0 || EVP_MAC_update(ctx, in2, in2len))
70
596
            || !(in3 == NULL || in3len == 0 || EVP_MAC_update(ctx, in3, in3len))
71
596
            || !EVP_MAC_final(ctx, hmac->K, NULL, sizeof(hmac->K)))
72
4
        return 0;
73
74
   /* V = HMAC(K, V) */
75
592
    return EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
76
592
           && EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
77
592
           && EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V));
78
596
}
79
80
/*
81
 * SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process
82
 *
83
 *
84
 * Updates the drbg objects Key(K) and Value(V) using the following algorithm:
85
 *   K,V = do_hmac(hmac, 0, in1, in2, in3)
86
 *   if (any input is not NULL)
87
 *     K,V = do_hmac(hmac, 1, in1, in2, in3)
88
 *
89
 * where in1, in2, in3 are optional input buffers that can be NULL.
90
 *       in1len, in2len, in3len are the lengths of the input buffers.
91
 *
92
 * Returns zero if an error occurs otherwise it returns 1.
93
 */
94
static int drbg_hmac_update(PROV_DRBG *drbg,
95
                            const unsigned char *in1, size_t in1len,
96
                            const unsigned char *in2, size_t in2len,
97
                            const unsigned char *in3, size_t in3len)
98
380
{
99
380
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
100
101
    /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */
102
380
    if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len))
103
4
        return 0;
104
    /* (Step 3) If provided_data == NULL then return (K,V) */
105
376
    if (in1len == 0 && in2len == 0 && in3len == 0)
106
160
        return 1;
107
    /* (Steps 4-5) K = HMAC(K, V||0x01||provided_data). V = HMAC(K,V) */
108
216
    return do_hmac(hmac, 0x01, in1, in1len, in2, in2len, in3, in3len);
109
376
}
110
111
/*
112
 * SP800-90Ar1 10.1.2.3 HMAC_DRBG_Instantiate_Process:
113
 *
114
 * This sets the drbg Key (K) to all zeros, and Value (V) to all 1's.
115
 * and then calls (K,V) = drbg_hmac_update() with input parameters:
116
 *   ent = entropy data (Can be NULL) of length ent_len.
117
 *   nonce = nonce data (Can be NULL) of length nonce_len.
118
 *   pstr = personalization data (Can be NULL) of length pstr_len.
119
 *
120
 * Returns zero if an error occurs otherwise it returns 1.
121
 */
122
static int drbg_hmac_instantiate(PROV_DRBG *drbg,
123
                                 const unsigned char *ent, size_t ent_len,
124
                                 const unsigned char *nonce, size_t nonce_len,
125
                                 const unsigned char *pstr, size_t pstr_len)
126
{
127
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
128
129
    if (hmac->ctx == NULL) {
130
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
131
        return 0;
132
    }
133
134
    /* (Step 2) Key = 0x00 00...00 */
135
    memset(hmac->K, 0x00, hmac->blocklen);
136
    /* (Step 3) V = 0x01 01...01 */
137
    memset(hmac->V, 0x01, hmac->blocklen);
138
    /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */
139
    return drbg_hmac_update(drbg, ent, ent_len, nonce, nonce_len, pstr,
140
                            pstr_len);
141
}
142
143
static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength,
144
                                         int prediction_resistance,
145
                                         const unsigned char *pstr,
146
                                         size_t pstr_len,
147
                                         const OSSL_PARAM params[])
148
0
{
149
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
150
151
0
    if (!ossl_prov_is_running() || !drbg_hmac_set_ctx_params(drbg, params))
152
0
        return 0;
153
0
    return ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
154
0
                                      pstr, pstr_len);
155
0
}
156
157
/*
158
 * SP800-90Ar1 10.1.2.4 HMAC_DRBG_Reseed_Process:
159
 *
160
 * Reseeds the drbg's Key (K) and Value (V) by calling
161
 * (K,V) = drbg_hmac_update() with the following input parameters:
162
 *   ent = entropy input data (Can be NULL) of length ent_len.
163
 *   adin = additional input data (Can be NULL) of length adin_len.
164
 *
165
 * Returns zero if an error occurs otherwise it returns 1.
166
 */
167
static int drbg_hmac_reseed(PROV_DRBG *drbg,
168
                            const unsigned char *ent, size_t ent_len,
169
                            const unsigned char *adin, size_t adin_len)
170
49
{
171
    /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */
172
49
    return drbg_hmac_update(drbg, ent, ent_len, adin, adin_len, NULL, 0);
173
49
}
174
175
static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance,
176
                                    const unsigned char *ent, size_t ent_len,
177
                                    const unsigned char *adin, size_t adin_len)
178
37
{
179
37
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
180
181
37
    return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
182
37
                                 adin, adin_len);
183
37
}
184
185
/*
186
 * SP800-90Ar1 10.1.2.5 HMAC_DRBG_Generate_Process:
187
 *
188
 * Generates pseudo random bytes and updates the internal K,V for the drbg.
189
 * out is a buffer to fill with outlen bytes of pseudo random data.
190
 * adin is an additional_input string of size adin_len that may be NULL.
191
 *
192
 * Returns zero if an error occurs otherwise it returns 1.
193
 */
194
static int drbg_hmac_generate(PROV_DRBG *drbg,
195
                              unsigned char *out, size_t outlen,
196
                              const unsigned char *adin, size_t adin_len)
197
0
{
198
0
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
199
0
    EVP_MAC_CTX *ctx = hmac->ctx;
200
0
    const unsigned char *temp = hmac->V;
201
202
    /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */
203
0
    if (adin != NULL
204
0
            && adin_len > 0
205
0
            && !drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0))
206
0
        return 0;
207
208
    /*
209
     * (Steps 3-5) temp = NULL
210
     *             while (len(temp) < outlen) {
211
     *                 V = HMAC(K, V)
212
     *                 temp = temp || V
213
     *             }
214
     */
215
0
    for (;;) {
216
0
        if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
217
0
            || !EVP_MAC_update(ctx, temp, hmac->blocklen))
218
0
            return 0;
219
220
0
        if (outlen > hmac->blocklen) {
221
0
            if (!EVP_MAC_final(ctx, out, NULL, outlen))
222
0
                return 0;
223
0
            temp = out;
224
0
        } else {
225
0
            if (!EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V)))
226
0
                return 0;
227
0
            memcpy(out, hmac->V, outlen);
228
0
            break;
229
0
        }
230
0
        out += hmac->blocklen;
231
0
        outlen -= hmac->blocklen;
232
0
    }
233
    /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */
234
0
    if (!drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0))
235
0
        return 0;
236
237
0
    return 1;
238
0
}
239
240
static int drbg_hmac_generate_wrapper
241
    (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
242
     int prediction_resistance, const unsigned char *adin, size_t adin_len)
243
48
{
244
48
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
245
246
48
    return ossl_prov_drbg_generate(drbg, out, outlen, strength,
247
48
                                   prediction_resistance, adin, adin_len);
248
48
}
249
250
static int drbg_hmac_uninstantiate(PROV_DRBG *drbg)
251
0
{
252
0
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
253
254
0
    OPENSSL_cleanse(hmac->K, sizeof(hmac->K));
255
0
    OPENSSL_cleanse(hmac->V, sizeof(hmac->V));
256
0
    return ossl_prov_drbg_uninstantiate(drbg);
257
0
}
258
259
static int drbg_hmac_uninstantiate_wrapper(void *vdrbg)
260
0
{
261
0
    return drbg_hmac_uninstantiate((PROV_DRBG *)vdrbg);
262
0
}
263
264
static int drbg_hmac_verify_zeroization(void *vdrbg)
265
0
{
266
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
267
0
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
268
269
0
    PROV_DRBG_VERYIFY_ZEROIZATION(hmac->K);
270
0
    PROV_DRBG_VERYIFY_ZEROIZATION(hmac->V);
271
0
    return 1;
272
0
}
273
274
static int drbg_hmac_new(PROV_DRBG *drbg)
275
64
{
276
64
    PROV_DRBG_HMAC *hmac;
277
278
64
    hmac = OPENSSL_secure_zalloc(sizeof(*hmac));
279
64
    if (hmac == NULL) {
280
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
281
0
        return 0;
282
0
    }
283
284
64
    drbg->data = hmac;
285
    /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
286
64
    drbg->max_entropylen = DRBG_MAX_LENGTH;
287
64
    drbg->max_noncelen = DRBG_MAX_LENGTH;
288
64
    drbg->max_perslen = DRBG_MAX_LENGTH;
289
64
    drbg->max_adinlen = DRBG_MAX_LENGTH;
290
291
    /* Maximum number of bits per request = 2^19  = 2^16 bytes */
292
64
    drbg->max_request = 1 << 16;
293
64
    return 1;
294
64
}
295
296
static void *drbg_hmac_new_wrapper(void *provctx, void *parent,
297
                                   const OSSL_DISPATCH *parent_dispatch)
298
64
{
299
64
    return ossl_rand_drbg_new(provctx, parent, parent_dispatch,
300
64
                              &drbg_hmac_new, &drbg_hmac_free,
301
64
                              &drbg_hmac_instantiate, &drbg_hmac_uninstantiate,
302
64
                              &drbg_hmac_reseed, &drbg_hmac_generate);
303
64
}
304
305
static void drbg_hmac_free(void *vdrbg)
306
64
{
307
64
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
308
64
    PROV_DRBG_HMAC *hmac;
309
310
64
    if (drbg != NULL && (hmac = (PROV_DRBG_HMAC *)drbg->data) != NULL) {
311
64
        EVP_MAC_CTX_free(hmac->ctx);
312
64
        ossl_prov_digest_reset(&hmac->digest);
313
64
        OPENSSL_secure_clear_free(hmac, sizeof(*hmac));
314
64
    }
315
64
    ossl_rand_drbg_free(drbg);
316
64
}
317
318
static int drbg_hmac_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
319
{
320
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
321
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
322
    const char *name;
323
    const EVP_MD *md;
324
    OSSL_PARAM *p;
325
326
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAC);
327
    if (p != NULL) {
328
        if (hmac->ctx == NULL)
329
            return 0;
330
        name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(hmac->ctx));
331
        if (!OSSL_PARAM_set_utf8_string(p, name))
332
            return 0;
333
    }
334
335
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
336
    if (p != NULL) {
337
        md = ossl_prov_digest_md(&hmac->digest);
338
        if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
339
            return 0;
340
    }
341
342
    return ossl_drbg_get_ctx_params(drbg, params);
343
}
344
345
static const OSSL_PARAM *drbg_hmac_gettable_ctx_params(ossl_unused void *vctx,
346
                                                       ossl_unused void *p_ctx)
347
0
{
348
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
349
0
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
350
0
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
351
0
        OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
352
0
        OSSL_PARAM_END
353
0
    };
354
0
    return known_gettable_ctx_params;
355
0
}
356
357
static int drbg_hmac_set_ctx_params(void *vctx, const OSSL_PARAM params[])
358
{
359
    PROV_DRBG *ctx = (PROV_DRBG *)vctx;
360
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)ctx->data;
361
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
362
    const EVP_MD *md;
363
364
    if (!ossl_prov_digest_load_from_params(&hmac->digest, params, libctx))
365
        return 0;
366
367
    /*
368
     * Confirm digest is allowed. We allow all digests that are not XOF
369
     * (such as SHAKE).  In FIPS mode, the fetch will fail for non-approved
370
     * digests.
371
     */
372
    md = ossl_prov_digest_md(&hmac->digest);
373
    if (md != NULL && (EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) {
374
        ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
375
        return 0;
376
    }
377
378
    if (!ossl_prov_macctx_load_from_params(&hmac->ctx, params,
379
                                           NULL, NULL, NULL, libctx))
380
        return 0;
381
382
    if (hmac->ctx != NULL) {
383
        /* These are taken from SP 800-90 10.1 Table 2 */
384
        hmac->blocklen = EVP_MD_get_size(md);
385
        /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
386
        ctx->strength = 64 * (int)(hmac->blocklen >> 3);
387
        if (ctx->strength > 256)
388
            ctx->strength = 256;
389
        ctx->seedlen = hmac->blocklen;
390
        ctx->min_entropylen = ctx->strength / 8;
391
        ctx->min_noncelen = ctx->min_entropylen / 2;
392
    }
393
394
    return ossl_drbg_set_ctx_params(ctx, params);
395
}
396
397
static const OSSL_PARAM *drbg_hmac_settable_ctx_params(ossl_unused void *vctx,
398
                                                       ossl_unused void *p_ctx)
399
64
{
400
64
    static const OSSL_PARAM known_settable_ctx_params[] = {
401
64
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
402
64
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
403
64
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
404
64
        OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
405
64
        OSSL_PARAM_END
406
64
    };
407
64
    return known_settable_ctx_params;
408
64
}
409
410
const OSSL_DISPATCH ossl_drbg_ossl_hmac_functions[] = {
411
    { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hmac_new_wrapper },
412
    { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hmac_free },
413
    { OSSL_FUNC_RAND_INSTANTIATE,
414
      (void(*)(void))drbg_hmac_instantiate_wrapper },
415
    { OSSL_FUNC_RAND_UNINSTANTIATE,
416
      (void(*)(void))drbg_hmac_uninstantiate_wrapper },
417
    { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hmac_generate_wrapper },
418
    { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hmac_reseed_wrapper },
419
    { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
420
    { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
421
    { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
422
    { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
423
      (void(*)(void))drbg_hmac_settable_ctx_params },
424
    { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hmac_set_ctx_params },
425
    { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
426
      (void(*)(void))drbg_hmac_gettable_ctx_params },
427
    { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hmac_get_ctx_params },
428
    { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
429
      (void(*)(void))drbg_hmac_verify_zeroization },
430
    { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
431
    { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
432
    { 0, NULL }
433
};