TianoCore EDK2 master
Loading...
Searching...
No Matches
rand_pool.c
Go to the documentation of this file.
1
10#include "crypto/rand.h"
11#include <openssl/aes.h>
12
13#include <Uefi.h>
14#include <Library/RngLib.h>
15
29BOOLEAN
30EFIAPI
32 IN UINTN Length,
33 OUT UINT8 *RandBuffer
34 )
35{
36 BOOLEAN Ret;
37 UINT64 TempRand;
38
39 Ret = FALSE;
40
41 if (RandBuffer == NULL) {
42 DEBUG ((DEBUG_ERROR, "[OPENSSL_RAND_POOL] NULL RandBuffer. No random numbers are generated and your system is not secure\n"));
43 ASSERT (RandBuffer != NULL); // Since we can't generate random numbers, we should assert. Otherwise we will just blow up later.
44 return Ret;
45 }
46
47 while (Length > 0) {
48 // Use RngLib to get random number
49 Ret = GetRandomNumber64 (&TempRand);
50
51 if (!Ret) {
52 return Ret;
53 }
54
55 if (Length >= sizeof (TempRand)) {
56 *((UINT64 *)RandBuffer) = TempRand;
57 RandBuffer += sizeof (UINT64);
58 Length -= sizeof (TempRand);
59 } else {
60 CopyMem (RandBuffer, &TempRand, Length);
61 Length = 0;
62 }
63 }
64
65 return Ret;
66}
67
68/*
69 * Add random bytes to the pool to acquire requested amount of entropy
70 *
71 * This function is platform specific and tries to acquire the requested
72 * amount of entropy by polling platform specific entropy sources.
73 *
74 * This is OpenSSL required interface.
75 */
76size_t
77ossl_pool_acquire_entropy (
78 RAND_POOL *pool
79 )
80{
81 BOOLEAN Ret;
82 size_t Bytes_needed;
83 unsigned char *Buffer;
84
85 Bytes_needed = ossl_rand_pool_bytes_needed (pool, 1 /*entropy_factor*/);
86 if (Bytes_needed > 0) {
87 Buffer = ossl_rand_pool_add_begin (pool, Bytes_needed);
88
89 if (Buffer != NULL) {
90 Ret = RandGetBytes (Bytes_needed, Buffer);
91 if (FALSE == Ret) {
92 ossl_rand_pool_add_end (pool, 0, 0);
93 } else {
94 ossl_rand_pool_add_end (pool, Bytes_needed, 8 * Bytes_needed);
95 }
96 }
97 }
98
99 return ossl_rand_pool_entropy_available (pool);
100}
101
102/*
103 * Implementation for UEFI
104 *
105 * This is OpenSSL required interface.
106 */
107int
108ossl_pool_add_nonce_data (
109 RAND_POOL *pool
110 )
111{
112 UINT8 data[16];
113
114 RandGetBytes (sizeof (data), data);
115
116 return ossl_rand_pool_add (pool, (unsigned char *)&data, sizeof (data), 0);
117}
118
119/*
120 * Implementation for UEFI
121 *
122 * This is OpenSSL required interface.
123 */
124int
125rand_pool_add_additional_data (
126 RAND_POOL *pool
127 )
128{
129 UINT8 data[16];
130
131 RandGetBytes (sizeof (data), data);
132
133 return ossl_rand_pool_add (pool, (unsigned char *)&data, sizeof (data), 0);
134}
135
136/*
137 * Dummy Implementation for UEFI
138 *
139 * This is OpenSSL required interface.
140 */
141int
142ossl_rand_pool_init (
143 VOID
144 )
145{
146 return 1;
147}
148
149/*
150 * Dummy Implementation for UEFI
151 *
152 * This is OpenSSL required interface.
153 */
154VOID
155ossl_rand_pool_cleanup (
156 VOID
157 )
158{
159}
160
161/*
162 * Dummy Implementation for UEFI
163 *
164 * This is OpenSSL required interface.
165 */
166VOID
167ossl_rand_pool_keep_random_devices_open (
168 int keep
169 )
170{
171}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI GetRandomNumber64(OUT UINT64 *Rand)
Definition: RngLibTimer.c:142
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
STATIC BOOLEAN EFIAPI RandGetBytes(IN UINTN Length, OUT UINT8 *RandBuffer)
Definition: rand_pool.c:31