Coverage Report

Created: 2026-10-16 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/rand/prov_seed.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2026 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 "rand_local.h"
11
#include "crypto/evp.h"
12
#include "crypto/rand.h"
13
#include "crypto/rand_pool.h"
14
#include "internal/core.h"
15
#include <openssl/core_dispatch.h>
16
#include <openssl/err.h>
17
18
size_t ossl_rand_get_entropy(ossl_unused OSSL_LIB_CTX *ctx,
19
    unsigned char **pout, int entropy,
20
    size_t min_len, size_t max_len)
21
0
{
22
0
    size_t ret = 0;
23
0
    size_t entropy_available;
24
0
    RAND_POOL *pool;
25
26
0
    pool = ossl_rand_pool_new(entropy, 1, min_len, max_len);
27
0
    if (pool == NULL) {
28
0
        ERR_raise(ERR_LIB_RAND, ERR_R_RAND_LIB);
29
0
        return 0;
30
0
    }
31
32
    /* Get entropy by polling system entropy sources. */
33
0
    entropy_available = ossl_pool_acquire_entropy(pool);
34
35
0
    if (entropy_available > 0) {
36
0
        ret = ossl_rand_pool_length(pool);
37
0
        *pout = ossl_rand_pool_detach(pool);
38
0
    }
39
40
0
    ossl_rand_pool_free(pool);
41
0
    return ret;
42
0
}
43
44
size_t ossl_rand_get_user_entropy(OSSL_LIB_CTX *ctx,
45
    unsigned char **pout, int entropy,
46
    size_t min_len, size_t max_len)
47
0
{
48
0
    EVP_RAND_CTX *rng;
49
50
0
    if (ossl_rand_seed_source_strict(ctx)) {
51
        /*
52
         * With strict seeding the configured seed source must be used,
53
         * even when the request arrives before anything instantiated it.
54
         */
55
0
        rng = ossl_rand_get0_seed(ctx);
56
0
        if (rng == NULL || !evp_rand_can_seed(rng)) {
57
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_ENTROPY);
58
0
            return 0;
59
0
        }
60
0
    } else {
61
0
        rng = ossl_rand_get0_seed_noncreating(ctx);
62
0
        if (rng == NULL || !evp_rand_can_seed(rng))
63
0
            return ossl_rand_get_entropy(ctx, pout, entropy, min_len, max_len);
64
0
    }
65
0
    return evp_rand_get_seed(rng, pout, entropy, min_len, max_len,
66
0
        0, NULL, 0);
67
0
}
68
69
void ossl_rand_cleanup_entropy(ossl_unused OSSL_LIB_CTX *ctx,
70
    unsigned char *buf, size_t len)
71
0
{
72
0
    OPENSSL_secure_clear_free(buf, len);
73
0
}
74
75
void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx,
76
    unsigned char *buf, size_t len)
77
0
{
78
0
    EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx);
79
80
0
    if (rng != NULL && evp_rand_can_seed(rng))
81
0
        evp_rand_clear_seed(rng, buf, len);
82
0
    else
83
0
        OPENSSL_secure_clear_free(buf, len);
84
0
}
85
86
size_t ossl_rand_get_nonce(ossl_unused OSSL_LIB_CTX *ctx,
87
    unsigned char **pout,
88
    size_t min_len, ossl_unused size_t max_len,
89
    const void *salt, size_t salt_len)
90
0
{
91
0
    size_t ret = 0;
92
0
    RAND_POOL *pool;
93
94
0
    pool = ossl_rand_pool_new(0, 0, min_len, max_len);
95
0
    if (pool == NULL) {
96
0
        ERR_raise(ERR_LIB_RAND, ERR_R_RAND_LIB);
97
0
        return 0;
98
0
    }
99
100
0
    if (!ossl_pool_add_nonce_data(pool))
101
0
        goto err;
102
103
0
    if (salt != NULL && !ossl_rand_pool_add(pool, salt, salt_len, 0))
104
0
        goto err;
105
0
    ret = ossl_rand_pool_length(pool);
106
0
    *pout = ossl_rand_pool_detach(pool);
107
0
err:
108
0
    ossl_rand_pool_free(pool);
109
0
    return ret;
110
0
}
111
112
size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx,
113
    unsigned char **pout,
114
    size_t min_len, size_t max_len,
115
    const void *salt, size_t salt_len)
116
0
{
117
0
    unsigned char *buf;
118
0
    EVP_RAND_CTX *rng;
119
120
0
    if (ossl_rand_seed_source_strict(ctx)) {
121
        /* See ossl_rand_get_user_entropy() */
122
0
        rng = ossl_rand_get0_seed(ctx);
123
0
        if (rng == NULL) {
124
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_NONCE);
125
0
            return 0;
126
0
        }
127
0
    } else {
128
0
        rng = ossl_rand_get0_seed_noncreating(ctx);
129
0
        if (rng == NULL)
130
0
            return ossl_rand_get_nonce(ctx, pout, min_len, max_len,
131
0
                salt, salt_len);
132
0
    }
133
134
0
    if ((buf = OPENSSL_malloc(min_len)) == NULL)
135
0
        return 0;
136
137
0
    if (!EVP_RAND_generate(rng, buf, min_len, 0, 0, salt, salt_len)) {
138
0
        OPENSSL_free(buf);
139
0
        return 0;
140
0
    }
141
0
    *pout = buf;
142
0
    return min_len;
143
0
}
144
145
void ossl_rand_cleanup_nonce(ossl_unused OSSL_LIB_CTX *ctx,
146
    unsigned char *buf, size_t len)
147
0
{
148
0
    OPENSSL_clear_free(buf, len);
149
0
}
150
151
void ossl_rand_cleanup_user_nonce(ossl_unused OSSL_LIB_CTX *ctx,
152
    unsigned char *buf, size_t len)
153
0
{
154
0
    OPENSSL_clear_free(buf, len);
155
0
}