/src/openssl/crypto/rand/rand_meth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2021 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 <openssl/evp.h> |
11 | | #include <openssl/rand.h> |
12 | | #include "rand_local.h" |
13 | | |
14 | | /* Implements the default OpenSSL RAND_add() method */ |
15 | | static int drbg_add(const void *buf, int num, double randomness) |
16 | 0 | { |
17 | 0 | EVP_RAND_CTX *drbg = RAND_get0_primary(NULL); |
18 | |
|
19 | 0 | if (drbg == NULL || num <= 0) |
20 | 0 | return 0; |
21 | | |
22 | 0 | return EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); |
23 | 0 | } |
24 | | |
25 | | /* Implements the default OpenSSL RAND_seed() method */ |
26 | | static int drbg_seed(const void *buf, int num) |
27 | 0 | { |
28 | 0 | return drbg_add(buf, num, num); |
29 | 0 | } |
30 | | |
31 | | /* Implements the default OpenSSL RAND_status() method */ |
32 | | static int drbg_status(void) |
33 | 0 | { |
34 | 0 | EVP_RAND_CTX *drbg = RAND_get0_primary(NULL); |
35 | |
|
36 | 0 | if (drbg == NULL) |
37 | 0 | return 0; |
38 | | |
39 | 0 | return EVP_RAND_get_state(drbg) == EVP_RAND_STATE_READY ? 1 : 0; |
40 | 0 | } |
41 | | |
42 | | /* Implements the default OpenSSL RAND_bytes() method */ |
43 | | static int drbg_bytes(unsigned char *out, int count) |
44 | 0 | { |
45 | 0 | EVP_RAND_CTX *drbg = RAND_get0_public(NULL); |
46 | |
|
47 | 0 | if (drbg == NULL) |
48 | 0 | return 0; |
49 | | |
50 | 0 | return EVP_RAND_generate(drbg, out, count, 0, 0, NULL, 0); |
51 | 0 | } |
52 | | |
53 | | RAND_METHOD ossl_rand_meth = { |
54 | | drbg_seed, |
55 | | drbg_bytes, |
56 | | NULL, |
57 | | drbg_add, |
58 | | drbg_bytes, |
59 | | drbg_status |
60 | | }; |
61 | | |
62 | | RAND_METHOD *RAND_OpenSSL(void) |
63 | 1.05M | { |
64 | 1.05M | return &ossl_rand_meth; |
65 | 1.05M | } |