Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/rand/prov_seed.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-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 "crypto/rand.h"
11
#include "crypto/rand_pool.h"
12
#include <openssl/core_dispatch.h>
13
#include <openssl/err.h>
14
15
size_t ossl_rand_get_entropy(ossl_unused const OSSL_CORE_HANDLE *handle,
16
                             unsigned char **pout, int entropy,
17
                             size_t min_len, size_t max_len)
18
208
{
19
208
    size_t ret = 0;
20
208
    size_t entropy_available;
21
208
    RAND_POOL *pool;
22
23
208
    pool = ossl_rand_pool_new(entropy, 1, min_len, max_len);
24
208
    if (pool == NULL) {
25
0
        ERR_raise(ERR_LIB_RAND, ERR_R_MALLOC_FAILURE);
26
0
        return 0;
27
0
    }
28
29
    /* Get entropy by polling system entropy sources. */
30
208
    entropy_available = ossl_pool_acquire_entropy(pool);
31
32
208
    if (entropy_available > 0) {
33
208
        ret   = ossl_rand_pool_length(pool);
34
208
        *pout = ossl_rand_pool_detach(pool);
35
208
    }
36
37
208
    ossl_rand_pool_free(pool);
38
208
    return ret;
39
208
}
40
41
void ossl_rand_cleanup_entropy(ossl_unused const OSSL_CORE_HANDLE *handle,
42
                               unsigned char *buf, size_t len)
43
0
{
44
0
    OPENSSL_secure_clear_free(buf, len);
45
0
}
46
47
size_t ossl_rand_get_nonce(ossl_unused const OSSL_CORE_HANDLE *handle,
48
                           unsigned char **pout, size_t min_len, size_t max_len,
49
                           const void *salt, size_t salt_len)
50
100
{
51
100
    size_t ret = 0;
52
100
    RAND_POOL *pool;
53
54
100
    pool = ossl_rand_pool_new(0, 0, min_len, max_len);
55
100
    if (pool == NULL) {
56
0
        ERR_raise(ERR_LIB_RAND, ERR_R_MALLOC_FAILURE);
57
0
        return 0;
58
0
    }
59
60
100
    if (!ossl_pool_add_nonce_data(pool))
61
0
        goto err;
62
63
100
    if (salt != NULL && !ossl_rand_pool_add(pool, salt, salt_len, 0))
64
0
        goto err;
65
100
    ret   = ossl_rand_pool_length(pool);
66
100
    *pout = ossl_rand_pool_detach(pool);
67
100
 err:
68
100
    ossl_rand_pool_free(pool);
69
100
    return ret;
70
100
}
71
72
void ossl_rand_cleanup_nonce(ossl_unused const OSSL_CORE_HANDLE *handle,
73
                             unsigned char *buf, size_t len)
74
0
{
75
0
    OPENSSL_clear_free(buf, len);
76
0
}