1 | /*
|
---|
2 | * Copyright 2020-2023 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 | {
|
---|
22 | size_t ret = 0;
|
---|
23 | size_t entropy_available;
|
---|
24 | RAND_POOL *pool;
|
---|
25 |
|
---|
26 | pool = ossl_rand_pool_new(entropy, 1, min_len, max_len);
|
---|
27 | if (pool == NULL) {
|
---|
28 | ERR_raise(ERR_LIB_RAND, ERR_R_MALLOC_FAILURE);
|
---|
29 | return 0;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /* Get entropy by polling system entropy sources. */
|
---|
33 | entropy_available = ossl_pool_acquire_entropy(pool);
|
---|
34 |
|
---|
35 | if (entropy_available > 0) {
|
---|
36 | ret = ossl_rand_pool_length(pool);
|
---|
37 | *pout = ossl_rand_pool_detach(pool);
|
---|
38 | }
|
---|
39 |
|
---|
40 | ossl_rand_pool_free(pool);
|
---|
41 | return ret;
|
---|
42 | }
|
---|
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 | {
|
---|
48 | EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx);
|
---|
49 |
|
---|
50 | if (rng != NULL && evp_rand_can_seed(rng))
|
---|
51 | return evp_rand_get_seed(rng, pout, entropy, min_len, max_len,
|
---|
52 | 0, NULL, 0);
|
---|
53 | else
|
---|
54 | return ossl_rand_get_entropy(ctx, pout, entropy, min_len, max_len);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void ossl_rand_cleanup_entropy(ossl_unused OSSL_LIB_CTX *ctx,
|
---|
58 | unsigned char *buf, size_t len)
|
---|
59 | {
|
---|
60 | OPENSSL_secure_clear_free(buf, len);
|
---|
61 | }
|
---|
62 |
|
---|
63 | void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx,
|
---|
64 | unsigned char *buf, size_t len)
|
---|
65 | {
|
---|
66 | EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx);
|
---|
67 |
|
---|
68 | if (rng != NULL && evp_rand_can_seed(rng))
|
---|
69 | evp_rand_clear_seed(rng, buf, len);
|
---|
70 | else
|
---|
71 | OPENSSL_secure_clear_free(buf, len);
|
---|
72 | }
|
---|
73 |
|
---|
74 | size_t ossl_rand_get_nonce(ossl_unused OSSL_LIB_CTX *ctx,
|
---|
75 | unsigned char **pout,
|
---|
76 | size_t min_len, ossl_unused size_t max_len,
|
---|
77 | const void *salt, size_t salt_len)
|
---|
78 | {
|
---|
79 | size_t ret = 0;
|
---|
80 | RAND_POOL *pool;
|
---|
81 |
|
---|
82 | pool = ossl_rand_pool_new(0, 0, min_len, max_len);
|
---|
83 | if (pool == NULL) {
|
---|
84 | ERR_raise(ERR_LIB_RAND, ERR_R_MALLOC_FAILURE);
|
---|
85 | return 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (!ossl_pool_add_nonce_data(pool))
|
---|
89 | goto err;
|
---|
90 |
|
---|
91 | if (salt != NULL && !ossl_rand_pool_add(pool, salt, salt_len, 0))
|
---|
92 | goto err;
|
---|
93 | ret = ossl_rand_pool_length(pool);
|
---|
94 | *pout = ossl_rand_pool_detach(pool);
|
---|
95 | err:
|
---|
96 | ossl_rand_pool_free(pool);
|
---|
97 | return ret;
|
---|
98 | }
|
---|
99 |
|
---|
100 | size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx,
|
---|
101 | unsigned char **pout,
|
---|
102 | size_t min_len, size_t max_len,
|
---|
103 | const void *salt, size_t salt_len)
|
---|
104 | {
|
---|
105 | unsigned char *buf;
|
---|
106 | EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx);
|
---|
107 |
|
---|
108 | if (rng == NULL)
|
---|
109 | return ossl_rand_get_nonce(ctx, pout, min_len, max_len, salt, salt_len);
|
---|
110 |
|
---|
111 | if ((buf = OPENSSL_malloc(min_len)) == NULL)
|
---|
112 | return 0;
|
---|
113 |
|
---|
114 | if (!EVP_RAND_generate(rng, buf, min_len, 0, 0, salt, salt_len)) {
|
---|
115 | OPENSSL_free(buf);
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 | *pout = buf;
|
---|
119 | return min_len;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void ossl_rand_cleanup_nonce(ossl_unused OSSL_LIB_CTX *ctx,
|
---|
123 | unsigned char *buf, size_t len)
|
---|
124 | {
|
---|
125 | OPENSSL_clear_free(buf, len);
|
---|
126 | }
|
---|
127 |
|
---|
128 | void ossl_rand_cleanup_user_nonce(ossl_unused OSSL_LIB_CTX *ctx,
|
---|
129 | unsigned char *buf, size_t len)
|
---|
130 | {
|
---|
131 | OPENSSL_clear_free(buf, len);
|
---|
132 | }
|
---|