1 | /*
|
---|
2 | * Copyright 1995-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 | #ifndef OSSL_PROVIDER_RAND_POOL_H
|
---|
11 | # define OSSL_PROVIDER_RAND_POOL_H
|
---|
12 | # ifndef RT_WITHOUT_PRAGMA_ONCE /* VBOX */
|
---|
13 | # pragma once
|
---|
14 | # endif /* VBOX */
|
---|
15 |
|
---|
16 | # include <stdio.h>
|
---|
17 | # include <openssl/rand.h>
|
---|
18 |
|
---|
19 | /*
|
---|
20 | * Maximum allocation size for RANDOM_POOL buffers
|
---|
21 | *
|
---|
22 | * The max_len value for the buffer provided to the rand_drbg_get_entropy()
|
---|
23 | * callback is currently 2^31 bytes (2 gigabytes), if a derivation function
|
---|
24 | * is used. Since this is much too large to be allocated, the ossl_rand_pool_new()
|
---|
25 | * function chooses more modest values as default pool length, bounded
|
---|
26 | * by RAND_POOL_MIN_LENGTH and RAND_POOL_MAX_LENGTH
|
---|
27 | *
|
---|
28 | * The choice of the RAND_POOL_FACTOR is large enough such that the
|
---|
29 | * RAND_POOL can store a random input which has a lousy entropy rate of
|
---|
30 | * 8/256 (= 0.03125) bits per byte. This input will be sent through the
|
---|
31 | * derivation function which 'compresses' the low quality input into a
|
---|
32 | * high quality output.
|
---|
33 | *
|
---|
34 | * The factor 1.5 below is the pessimistic estimate for the extra amount
|
---|
35 | * of entropy required when no get_nonce() callback is defined.
|
---|
36 | */
|
---|
37 | # define RAND_POOL_FACTOR 256
|
---|
38 | # define RAND_POOL_MAX_LENGTH (RAND_POOL_FACTOR * \
|
---|
39 | 3 * (RAND_DRBG_STRENGTH / 16))
|
---|
40 | /*
|
---|
41 | * = (RAND_POOL_FACTOR * \
|
---|
42 | * 1.5 * (RAND_DRBG_STRENGTH / 8))
|
---|
43 | */
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * Initial allocation minimum.
|
---|
47 | *
|
---|
48 | * There is a distinction between the secure and normal allocation minimums.
|
---|
49 | * Ideally, the secure allocation size should be a power of two. The normal
|
---|
50 | * allocation size doesn't have any such restriction.
|
---|
51 | *
|
---|
52 | * The secure value is based on 128 bits of secure material, which is 16 bytes.
|
---|
53 | * Typically, the DRBGs will set a minimum larger than this so optimal
|
---|
54 | * allocation ought to take place (for full quality seed material).
|
---|
55 | *
|
---|
56 | * The normal value has been chosen by noticing that the rand_drbg_get_nonce
|
---|
57 | * function is usually the largest of the built in allocation (twenty four
|
---|
58 | * bytes and then appending another sixteen bytes). This means the buffer ends
|
---|
59 | * with 40 bytes. The value of forty eight is comfortably above this which
|
---|
60 | * allows some slack in the platform specific values used.
|
---|
61 | */
|
---|
62 | # define RAND_POOL_MIN_ALLOCATION(secure) ((secure) ? 16 : 48)
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * The 'random pool' acts as a dumb container for collecting random
|
---|
66 | * input from various entropy sources. It is the callers duty to 1) initialize
|
---|
67 | * the random pool, 2) pass it to the polling callbacks, 3) seed the RNG, and
|
---|
68 | * 4) cleanup the random pool again.
|
---|
69 | *
|
---|
70 | * The random pool contains no locking mechanism because its scope and
|
---|
71 | * lifetime is intended to be restricted to a single stack frame.
|
---|
72 | */
|
---|
73 | typedef struct rand_pool_st {
|
---|
74 | unsigned char *buffer; /* points to the beginning of the random pool */
|
---|
75 | size_t len; /* current number of random bytes contained in the pool */
|
---|
76 |
|
---|
77 | int attached; /* true pool was attached to existing buffer */
|
---|
78 | int secure; /* 1: allocated on the secure heap, 0: otherwise */
|
---|
79 |
|
---|
80 | size_t min_len; /* minimum number of random bytes requested */
|
---|
81 | size_t max_len; /* maximum number of random bytes (allocated buffer size) */
|
---|
82 | size_t alloc_len; /* current number of bytes allocated */
|
---|
83 | size_t entropy; /* current entropy count in bits */
|
---|
84 | size_t entropy_requested; /* requested entropy count in bits */
|
---|
85 | } RAND_POOL;
|
---|
86 |
|
---|
87 | RAND_POOL *ossl_rand_pool_new(int entropy_requested, int secure,
|
---|
88 | size_t min_len, size_t max_len);
|
---|
89 | RAND_POOL *ossl_rand_pool_attach(const unsigned char *buffer, size_t len,
|
---|
90 | size_t entropy);
|
---|
91 | void ossl_rand_pool_free(RAND_POOL *pool);
|
---|
92 |
|
---|
93 | const unsigned char *ossl_rand_pool_buffer(RAND_POOL *pool);
|
---|
94 | unsigned char *ossl_rand_pool_detach(RAND_POOL *pool);
|
---|
95 | void ossl_rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer);
|
---|
96 |
|
---|
97 | size_t ossl_rand_pool_entropy(RAND_POOL *pool);
|
---|
98 | size_t ossl_rand_pool_length(RAND_POOL *pool);
|
---|
99 |
|
---|
100 | size_t ossl_rand_pool_entropy_available(RAND_POOL *pool);
|
---|
101 | size_t ossl_rand_pool_entropy_needed(RAND_POOL *pool);
|
---|
102 | /* |entropy_factor| expresses how many bits of data contain 1 bit of entropy */
|
---|
103 | size_t ossl_rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor);
|
---|
104 | size_t ossl_rand_pool_bytes_remaining(RAND_POOL *pool);
|
---|
105 |
|
---|
106 | int ossl_rand_pool_add(RAND_POOL *pool,
|
---|
107 | const unsigned char *buffer, size_t len, size_t entropy);
|
---|
108 | unsigned char *ossl_rand_pool_add_begin(RAND_POOL *pool, size_t len);
|
---|
109 | int ossl_rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy);
|
---|
110 |
|
---|
111 | #endif /* OSSL_PROVIDER_RAND_POOL_H */
|
---|