]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c
CryptoPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptDh.c
... / ...
CommitLineData
1/** @file\r
2 Diffie-Hellman Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "InternalCryptLib.h"\r
10#include <openssl/bn.h>\r
11#include <openssl/dh.h>\r
12\r
13/**\r
14 Allocates and Initializes one Diffie-Hellman Context for subsequent use.\r
15\r
16 @return Pointer to the Diffie-Hellman Context that has been initialized.\r
17 If the allocations fails, DhNew() returns NULL.\r
18\r
19**/\r
20VOID *\r
21EFIAPI\r
22DhNew (\r
23 VOID\r
24 )\r
25{\r
26 //\r
27 // Allocates & Initializes DH Context by OpenSSL DH_new()\r
28 //\r
29 return (VOID *) DH_new ();\r
30}\r
31\r
32/**\r
33 Release the specified DH context.\r
34\r
35 If DhContext is NULL, then return FALSE.\r
36\r
37 @param[in] DhContext Pointer to the DH context to be released.\r
38\r
39**/\r
40VOID\r
41EFIAPI\r
42DhFree (\r
43 IN VOID *DhContext\r
44 )\r
45{\r
46 //\r
47 // Free OpenSSL DH Context\r
48 //\r
49 DH_free ((DH *) DhContext);\r
50}\r
51\r
52/**\r
53 Generates DH parameter.\r
54\r
55 Given generator g, and length of prime number p in bits, this function generates p,\r
56 and sets DH context according to value of g and p.\r
57\r
58 Before this function can be invoked, pseudorandom number generator must be correctly\r
59 initialized by RandomSeed().\r
60\r
61 If DhContext is NULL, then return FALSE.\r
62 If Prime is NULL, then return FALSE.\r
63\r
64 @param[in, out] DhContext Pointer to the DH context.\r
65 @param[in] Generator Value of generator.\r
66 @param[in] PrimeLength Length in bits of prime to be generated.\r
67 @param[out] Prime Pointer to the buffer to receive the generated prime number.\r
68\r
69 @retval TRUE DH parameter generation succeeded.\r
70 @retval FALSE Value of Generator is not supported.\r
71 @retval FALSE PRNG fails to generate random prime number with PrimeLength.\r
72\r
73**/\r
74BOOLEAN\r
75EFIAPI\r
76DhGenerateParameter (\r
77 IN OUT VOID *DhContext,\r
78 IN UINTN Generator,\r
79 IN UINTN PrimeLength,\r
80 OUT UINT8 *Prime\r
81 )\r
82{\r
83 BOOLEAN RetVal;\r
84 BIGNUM *BnP;\r
85\r
86 //\r
87 // Check input parameters.\r
88 //\r
89 if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
90 return FALSE;\r
91 }\r
92\r
93 if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {\r
94 return FALSE;\r
95 }\r
96\r
97 RetVal = (BOOLEAN) DH_generate_parameters_ex (DhContext, (UINT32) PrimeLength, (UINT32) Generator, NULL);\r
98 if (!RetVal) {\r
99 return FALSE;\r
100 }\r
101\r
102 DH_get0_pqg (DhContext, (const BIGNUM **)&BnP, NULL, NULL);\r
103 BN_bn2bin (BnP, Prime);\r
104\r
105 return TRUE;\r
106}\r
107\r
108/**\r
109 Sets generator and prime parameters for DH.\r
110\r
111 Given generator g, and prime number p, this function and sets DH\r
112 context accordingly.\r
113\r
114 If DhContext is NULL, then return FALSE.\r
115 If Prime is NULL, then return FALSE.\r
116\r
117 @param[in, out] DhContext Pointer to the DH context.\r
118 @param[in] Generator Value of generator.\r
119 @param[in] PrimeLength Length in bits of prime to be generated.\r
120 @param[in] Prime Pointer to the prime number.\r
121\r
122 @retval TRUE DH parameter setting succeeded.\r
123 @retval FALSE Value of Generator is not supported.\r
124 @retval FALSE Value of Generator is not suitable for the Prime.\r
125 @retval FALSE Value of Prime is not a prime number.\r
126 @retval FALSE Value of Prime is not a safe prime number.\r
127\r
128**/\r
129BOOLEAN\r
130EFIAPI\r
131DhSetParameter (\r
132 IN OUT VOID *DhContext,\r
133 IN UINTN Generator,\r
134 IN UINTN PrimeLength,\r
135 IN CONST UINT8 *Prime\r
136 )\r
137{\r
138 DH *Dh;\r
139 BIGNUM *BnP;\r
140 BIGNUM *BnG;\r
141\r
142 //\r
143 // Check input parameters.\r
144 //\r
145 if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
146 return FALSE;\r
147 }\r
148\r
149 if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {\r
150 return FALSE;\r
151 }\r
152\r
153 //\r
154 // Set the generator and prime parameters for DH object.\r
155 //\r
156 Dh = (DH *)DhContext;\r
157 BnP = BN_bin2bn ((const unsigned char *)Prime, (int)(PrimeLength / 8), NULL);\r
158 BnG = BN_bin2bn ((const unsigned char *)&Generator, 1, NULL);\r
159 if ((BnP == NULL) || (BnG == NULL) || !DH_set0_pqg (Dh, BnP, NULL, BnG)) {\r
160 goto Error;\r
161 }\r
162\r
163 return TRUE;\r
164\r
165Error:\r
166 BN_free (BnP);\r
167 BN_free (BnG);\r
168\r
169 return FALSE;\r
170}\r
171\r
172/**\r
173 Generates DH public key.\r
174\r
175 This function generates random secret exponent, and computes the public key, which is\r
176 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.\r
177 If the PublicKey buffer is too small to hold the public key, FALSE is returned and\r
178 PublicKeySize is set to the required buffer size to obtain the public key.\r
179\r
180 If DhContext is NULL, then return FALSE.\r
181 If PublicKeySize is NULL, then return FALSE.\r
182 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.\r
183\r
184 @param[in, out] DhContext Pointer to the DH context.\r
185 @param[out] PublicKey Pointer to the buffer to receive generated public key.\r
186 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.\r
187 On output, the size of data returned in PublicKey buffer in bytes.\r
188\r
189 @retval TRUE DH public key generation succeeded.\r
190 @retval FALSE DH public key generation failed.\r
191 @retval FALSE PublicKeySize is not large enough.\r
192\r
193**/\r
194BOOLEAN\r
195EFIAPI\r
196DhGenerateKey (\r
197 IN OUT VOID *DhContext,\r
198 OUT UINT8 *PublicKey,\r
199 IN OUT UINTN *PublicKeySize\r
200 )\r
201{\r
202 BOOLEAN RetVal;\r
203 DH *Dh;\r
204 BIGNUM *DhPubKey;\r
205 INTN Size;\r
206\r
207 //\r
208 // Check input parameters.\r
209 //\r
210 if (DhContext == NULL || PublicKeySize == NULL) {\r
211 return FALSE;\r
212 }\r
213\r
214 if (PublicKey == NULL && *PublicKeySize != 0) {\r
215 return FALSE;\r
216 }\r
217\r
218 Dh = (DH *) DhContext;\r
219\r
220 RetVal = (BOOLEAN) DH_generate_key (DhContext);\r
221 if (RetVal) {\r
222 DH_get0_key (Dh, (const BIGNUM **)&DhPubKey, NULL);\r
223 Size = BN_num_bytes (DhPubKey);\r
224 if ((Size > 0) && (*PublicKeySize < (UINTN) Size)) {\r
225 *PublicKeySize = Size;\r
226 return FALSE;\r
227 }\r
228\r
229 if (PublicKey != NULL) {\r
230 BN_bn2bin (DhPubKey, PublicKey);\r
231 }\r
232 *PublicKeySize = Size;\r
233 }\r
234\r
235 return RetVal;\r
236}\r
237\r
238/**\r
239 Computes exchanged common key.\r
240\r
241 Given peer's public key, this function computes the exchanged common key, based on its own\r
242 context including value of prime modulus and random secret exponent.\r
243\r
244 If DhContext is NULL, then return FALSE.\r
245 If PeerPublicKey is NULL, then return FALSE.\r
246 If KeySize is NULL, then return FALSE.\r
247 If Key is NULL, then return FALSE.\r
248 If KeySize is not large enough, then return FALSE.\r
249\r
250 @param[in, out] DhContext Pointer to the DH context.\r
251 @param[in] PeerPublicKey Pointer to the peer's public key.\r
252 @param[in] PeerPublicKeySize Size of peer's public key in bytes.\r
253 @param[out] Key Pointer to the buffer to receive generated key.\r
254 @param[in, out] KeySize On input, the size of Key buffer in bytes.\r
255 On output, the size of data returned in Key buffer in bytes.\r
256\r
257 @retval TRUE DH exchanged key generation succeeded.\r
258 @retval FALSE DH exchanged key generation failed.\r
259 @retval FALSE KeySize is not large enough.\r
260\r
261**/\r
262BOOLEAN\r
263EFIAPI\r
264DhComputeKey (\r
265 IN OUT VOID *DhContext,\r
266 IN CONST UINT8 *PeerPublicKey,\r
267 IN UINTN PeerPublicKeySize,\r
268 OUT UINT8 *Key,\r
269 IN OUT UINTN *KeySize\r
270 )\r
271{\r
272 BIGNUM *Bn;\r
273 INTN Size;\r
274\r
275 //\r
276 // Check input parameters.\r
277 //\r
278 if (DhContext == NULL || PeerPublicKey == NULL || KeySize == NULL || Key == NULL) {\r
279 return FALSE;\r
280 }\r
281\r
282 if (PeerPublicKeySize > INT_MAX) {\r
283 return FALSE;\r
284 }\r
285\r
286 Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL);\r
287 if (Bn == NULL) {\r
288 return FALSE;\r
289 }\r
290\r
291 Size = DH_compute_key (Key, Bn, DhContext);\r
292 if (Size < 0) {\r
293 BN_free (Bn);\r
294 return FALSE;\r
295 }\r
296\r
297 if (*KeySize < (UINTN) Size) {\r
298 *KeySize = Size;\r
299 BN_free (Bn);\r
300 return FALSE;\r
301 }\r
302\r
303 *KeySize = Size;\r
304 BN_free (Bn);\r
305 return TRUE;\r
306}\r