/src/openssl41/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 | 1.06k | { |
22 | 1.06k | size_t ret = 0; |
23 | 1.06k | size_t entropy_available; |
24 | 1.06k | RAND_POOL *pool; |
25 | | |
26 | 1.06k | pool = ossl_rand_pool_new(entropy, 1, min_len, max_len); |
27 | 1.06k | 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 | 1.06k | entropy_available = ossl_pool_acquire_entropy(pool); |
34 | | |
35 | 1.06k | if (entropy_available > 0) { |
36 | 1.06k | ret = ossl_rand_pool_length(pool); |
37 | 1.06k | *pout = ossl_rand_pool_detach(pool); |
38 | 1.06k | } |
39 | | |
40 | 1.06k | ossl_rand_pool_free(pool); |
41 | 1.06k | return ret; |
42 | 1.06k | } |
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 | 530 | { |
48 | 530 | EVP_RAND_CTX *rng; |
49 | | |
50 | 530 | if (ossl_rand_seed_source_strict(ctx)) { |
51 | | /* |
52 | | * With strict seeding the seed source must be used, even when the |
53 | | * request arrives before anything instantiated it: create it now |
54 | | * and fail instead of silently substituting the operating system |
55 | | * entropy sources. |
56 | | */ |
57 | 0 | rng = ossl_rand_get0_seed(ctx); |
58 | 0 | if (rng == NULL || !evp_rand_can_seed(rng)) { |
59 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_ENTROPY); |
60 | 0 | return 0; |
61 | 0 | } |
62 | 530 | } else { |
63 | 530 | rng = ossl_rand_get0_seed_noncreating(ctx); |
64 | 530 | if (rng == NULL || !evp_rand_can_seed(rng)) |
65 | 256 | return ossl_rand_get_entropy(ctx, pout, entropy, min_len, max_len); |
66 | 530 | } |
67 | 274 | return evp_rand_get_seed(rng, pout, entropy, min_len, max_len, |
68 | 274 | 0, NULL, 0); |
69 | 530 | } |
70 | | |
71 | | void ossl_rand_cleanup_entropy(ossl_unused OSSL_LIB_CTX *ctx, |
72 | | unsigned char *buf, size_t len) |
73 | 0 | { |
74 | 0 | OPENSSL_secure_clear_free(buf, len); |
75 | 0 | } |
76 | | |
77 | | void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx, |
78 | | unsigned char *buf, size_t len) |
79 | 1.37k | { |
80 | 1.37k | EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx); |
81 | | |
82 | 1.37k | if (rng != NULL && evp_rand_can_seed(rng)) |
83 | 317 | evp_rand_clear_seed(rng, buf, len); |
84 | 1.06k | else |
85 | 1.06k | OPENSSL_secure_clear_free(buf, len); |
86 | 1.37k | } |
87 | | |
88 | | size_t ossl_rand_get_nonce(ossl_unused OSSL_LIB_CTX *ctx, |
89 | | unsigned char **pout, |
90 | | size_t min_len, ossl_unused size_t max_len, |
91 | | const void *salt, size_t salt_len) |
92 | 516 | { |
93 | 516 | size_t ret = 0; |
94 | 516 | RAND_POOL *pool; |
95 | | |
96 | 516 | pool = ossl_rand_pool_new(0, 0, min_len, max_len); |
97 | 516 | if (pool == NULL) { |
98 | 0 | ERR_raise(ERR_LIB_RAND, ERR_R_RAND_LIB); |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | 516 | if (!ossl_pool_add_nonce_data(pool)) |
103 | 0 | goto err; |
104 | | |
105 | 516 | if (salt != NULL && !ossl_rand_pool_add(pool, salt, salt_len, 0)) |
106 | 0 | goto err; |
107 | 516 | ret = ossl_rand_pool_length(pool); |
108 | 516 | *pout = ossl_rand_pool_detach(pool); |
109 | 516 | err: |
110 | 516 | ossl_rand_pool_free(pool); |
111 | 516 | return ret; |
112 | 516 | } |
113 | | |
114 | | size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx, |
115 | | unsigned char **pout, |
116 | | size_t min_len, size_t max_len, |
117 | | const void *salt, size_t salt_len) |
118 | 256 | { |
119 | 256 | unsigned char *buf; |
120 | 256 | EVP_RAND_CTX *rng; |
121 | | |
122 | 256 | if (ossl_rand_seed_source_strict(ctx)) { |
123 | | /* See ossl_rand_get_user_entropy() */ |
124 | 0 | rng = ossl_rand_get0_seed(ctx); |
125 | 0 | if (rng == NULL) { |
126 | 0 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_NONCE); |
127 | 0 | return 0; |
128 | 0 | } |
129 | 256 | } else { |
130 | 256 | rng = ossl_rand_get0_seed_noncreating(ctx); |
131 | 256 | if (rng == NULL) |
132 | 122 | return ossl_rand_get_nonce(ctx, pout, min_len, max_len, |
133 | 122 | salt, salt_len); |
134 | 256 | } |
135 | | |
136 | 134 | if ((buf = OPENSSL_malloc(min_len)) == NULL) |
137 | 0 | return 0; |
138 | | |
139 | 134 | if (!EVP_RAND_generate(rng, buf, min_len, 0, 0, salt, salt_len)) { |
140 | 0 | OPENSSL_free(buf); |
141 | 0 | return 0; |
142 | 0 | } |
143 | 134 | *pout = buf; |
144 | 134 | return min_len; |
145 | 134 | } |
146 | | |
147 | | void ossl_rand_cleanup_nonce(ossl_unused OSSL_LIB_CTX *ctx, |
148 | | unsigned char *buf, size_t len) |
149 | 0 | { |
150 | 0 | OPENSSL_clear_free(buf, len); |
151 | 0 | } |
152 | | |
153 | | void ossl_rand_cleanup_user_nonce(ossl_unused OSSL_LIB_CTX *ctx, |
154 | | unsigned char *buf, size_t len) |
155 | 668 | { |
156 | 668 | OPENSSL_clear_free(buf, len); |
157 | 668 | } |