TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptDh.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <openssl/bn.h>
11#include <openssl/dh.h>
12
20VOID *
21EFIAPI
23 VOID
24 )
25{
26 //
27 // Allocates & Initializes DH Context by OpenSSL DH_new()
28 //
29 return (VOID *)DH_new ();
30}
31
40VOID
41EFIAPI
43 IN VOID *DhContext
44 )
45{
46 //
47 // Free OpenSSL DH Context
48 //
49 DH_free ((DH *)DhContext);
50}
51
74BOOLEAN
75EFIAPI
77 IN OUT VOID *DhContext,
79 IN UINTN PrimeLength,
80 OUT UINT8 *Prime
81 )
82{
83 BOOLEAN RetVal;
84 BIGNUM *BnP;
85
86 //
87 // Check input parameters.
88 //
89 if ((DhContext == NULL) || (Prime == NULL) || (PrimeLength > INT_MAX)) {
90 return FALSE;
91 }
92
93 if ((Generator != DH_GENERATOR_2) && (Generator != DH_GENERATOR_5)) {
94 return FALSE;
95 }
96
97 RetVal = (BOOLEAN)DH_generate_parameters_ex (DhContext, (UINT32)PrimeLength, (UINT32)Generator, NULL);
98 if (!RetVal) {
99 return FALSE;
100 }
101
102 DH_get0_pqg (DhContext, (const BIGNUM **)&BnP, NULL, NULL);
103 BN_bn2bin (BnP, Prime);
104
105 return TRUE;
106}
107
129BOOLEAN
130EFIAPI
132 IN OUT VOID *DhContext,
134 IN UINTN PrimeLength,
135 IN CONST UINT8 *Prime
136 )
137{
138 DH *Dh;
139 BIGNUM *BnP;
140 BIGNUM *BnG;
141
142 //
143 // Check input parameters.
144 //
145 if ((DhContext == NULL) || (Prime == NULL) || (PrimeLength > INT_MAX)) {
146 return FALSE;
147 }
148
149 if ((Generator != DH_GENERATOR_2) && (Generator != DH_GENERATOR_5)) {
150 return FALSE;
151 }
152
153 //
154 // Set the generator and prime parameters for DH object.
155 //
156 Dh = (DH *)DhContext;
157 BnP = BN_bin2bn ((const unsigned char *)Prime, (int)(PrimeLength / 8), NULL);
158 BnG = BN_bin2bn ((const unsigned char *)&Generator, 1, NULL);
159 if ((BnP == NULL) || (BnG == NULL) || !DH_set0_pqg (Dh, BnP, NULL, BnG)) {
160 goto Error;
161 }
162
163 return TRUE;
164
165Error:
166 BN_free (BnP);
167 BN_free (BnG);
168
169 return FALSE;
170}
171
194BOOLEAN
195EFIAPI
197 IN OUT VOID *DhContext,
198 OUT UINT8 *PublicKey,
199 IN OUT UINTN *PublicKeySize
200 )
201{
202 BOOLEAN RetVal;
203 DH *Dh;
204 BIGNUM *DhPubKey;
205 INTN Size;
206
207 //
208 // Check input parameters.
209 //
210 if ((DhContext == NULL) || (PublicKeySize == NULL)) {
211 return FALSE;
212 }
213
214 if ((PublicKey == NULL) && (*PublicKeySize != 0)) {
215 return FALSE;
216 }
217
218 Dh = (DH *)DhContext;
219
220 RetVal = (BOOLEAN)DH_generate_key (DhContext);
221 if (RetVal) {
222 DH_get0_key (Dh, (const BIGNUM **)&DhPubKey, NULL);
223 Size = BN_num_bytes (DhPubKey);
224 if ((Size > 0) && (*PublicKeySize < (UINTN)Size)) {
225 *PublicKeySize = Size;
226 return FALSE;
227 }
228
229 if (PublicKey != NULL) {
230 BN_bn2bin (DhPubKey, PublicKey);
231 }
232
233 *PublicKeySize = Size;
234 }
235
236 return RetVal;
237}
238
263BOOLEAN
264EFIAPI
266 IN OUT VOID *DhContext,
267 IN CONST UINT8 *PeerPublicKey,
268 IN UINTN PeerPublicKeySize,
269 OUT UINT8 *Key,
270 IN OUT UINTN *KeySize
271 )
272{
273 BIGNUM *Bn;
274 INTN Size;
275
276 //
277 // Check input parameters.
278 //
279 if ((DhContext == NULL) || (PeerPublicKey == NULL) || (KeySize == NULL) || (Key == NULL)) {
280 return FALSE;
281 }
282
283 if (PeerPublicKeySize > INT_MAX) {
284 return FALSE;
285 }
286
287 Bn = BN_bin2bn (PeerPublicKey, (UINT32)PeerPublicKeySize, NULL);
288 if (Bn == NULL) {
289 return FALSE;
290 }
291
292 Size = DH_compute_key (Key, Bn, DhContext);
293 if (Size < 0) {
294 BN_free (Bn);
295 return FALSE;
296 }
297
298 if (*KeySize < (UINTN)Size) {
299 *KeySize = Size;
300 BN_free (Bn);
301 return FALSE;
302 }
303
304 *KeySize = Size;
305 BN_free (Bn);
306 return TRUE;
307}
UINT64 UINTN
INT64 INTN
BOOLEAN EFIAPI DhGenerateParameter(IN OUT VOID *DhContext, IN UINTN Generator, IN UINTN PrimeLength, OUT UINT8 *Prime)
Definition: CryptDh.c:76
VOID *EFIAPI DhNew(VOID)
Definition: CryptDh.c:22
BOOLEAN EFIAPI DhGenerateKey(IN OUT VOID *DhContext, OUT UINT8 *PublicKey, IN OUT UINTN *PublicKeySize)
Definition: CryptDh.c:196
VOID EFIAPI DhFree(IN VOID *DhContext)
Definition: CryptDh.c:42
BOOLEAN EFIAPI DhSetParameter(IN OUT VOID *DhContext, IN UINTN Generator, IN UINTN PrimeLength, IN CONST UINT8 *Prime)
Definition: CryptDh.c:131
BOOLEAN EFIAPI DhComputeKey(IN OUT VOID *DhContext, IN CONST UINT8 *PeerPublicKey, IN UINTN PeerPublicKeySize, OUT UINT8 *Key, IN OUT UINTN *KeySize)
Definition: CryptDh.c:265
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284