1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12
13 #include <stdio.h>
14 #include "crypto/rand_pool.h"
15 #include "openssl/crypto.h"
16 #include "openssl/types.h"
17
18 #ifdef OPENSSL_RAND_SEED_ENTROPY_CUSTOMER
rand_acquire_entropy_from_customer(RAND_POOL * pool)19 size_t rand_acquire_entropy_from_customer(RAND_POOL *pool)
20 {
21 size_t bytes_needed;
22 unsigned char *buffer;
23 int ret;
24
25 bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);
26 if (bytes_needed > 0) {
27 buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
28 if (buffer != NULL){
29 ret = (int)OPENSSL_RAND_SEED_ENTROPY_CUSTOMER(buffer, (uint32_t)bytes_needed);
30 if (ret == 0)
31 ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
32 }
33 }
34 return ossl_rand_pool_entropy_available(pool);
35 }
36
ossl_pool_acquire_entropy(RAND_POOL * pool)37 size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
38 {
39 size_t entropy_available;
40 entropy_available = rand_acquire_entropy_from_customer(pool);
41 if (entropy_available > 0)
42 return entropy_available;
43 return 0;
44 }
45 #endif
46
ossl_pool_add_nonce_data(RAND_POOL * pool)47 int ossl_pool_add_nonce_data(RAND_POOL *pool)
48 {
49 struct data_t {
50 pid_t pid;
51 CRYPTO_THREAD_ID tid;
52 uint64_t time;
53 };
54 struct data_t *data = malloc(sizeof(struct data_t));
55 if (data == NULL)
56 return 0;
57 (void)memset(data, 0, sizeof(struct data_t));
58 return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(struct data_t), 0);
59 }
60