2 Diffie-Hellman Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/bn.h>
11 #include <openssl/dh.h>
14 Allocates and Initializes one Diffie-Hellman Context for subsequent use.
16 @return Pointer to the Diffie-Hellman Context that has been initialized.
17 If the allocations fails, DhNew() returns NULL.
27 // Allocates & Initializes DH Context by OpenSSL DH_new()
29 return (VOID
*)DH_new ();
33 Release the specified DH context.
35 If DhContext is NULL, then return FALSE.
37 @param[in] DhContext Pointer to the DH context to be released.
47 // Free OpenSSL DH Context
49 DH_free ((DH
*)DhContext
);
53 Generates DH parameter.
55 Given generator g, and length of prime number p in bits, this function generates p,
56 and sets DH context according to value of g and p.
58 Before this function can be invoked, pseudorandom number generator must be correctly
59 initialized by RandomSeed().
61 If DhContext is NULL, then return FALSE.
62 If Prime is NULL, then return FALSE.
64 @param[in, out] DhContext Pointer to the DH context.
65 @param[in] Generator Value of generator.
66 @param[in] PrimeLength Length in bits of prime to be generated.
67 @param[out] Prime Pointer to the buffer to receive the generated prime number.
69 @retval TRUE DH parameter generation succeeded.
70 @retval FALSE Value of Generator is not supported.
71 @retval FALSE PRNG fails to generate random prime number with PrimeLength.
77 IN OUT VOID
*DhContext
,
87 // Check input parameters.
89 if ((DhContext
== NULL
) || (Prime
== NULL
) || (PrimeLength
> INT_MAX
)) {
93 if ((Generator
!= DH_GENERATOR_2
) && (Generator
!= DH_GENERATOR_5
)) {
97 RetVal
= (BOOLEAN
)DH_generate_parameters_ex (DhContext
, (UINT32
)PrimeLength
, (UINT32
)Generator
, NULL
);
102 DH_get0_pqg (DhContext
, (const BIGNUM
**)&BnP
, NULL
, NULL
);
103 BN_bn2bin (BnP
, Prime
);
109 Sets generator and prime parameters for DH.
111 Given generator g, and prime number p, this function and sets DH
114 If DhContext is NULL, then return FALSE.
115 If Prime is NULL, then return FALSE.
117 @param[in, out] DhContext Pointer to the DH context.
118 @param[in] Generator Value of generator.
119 @param[in] PrimeLength Length in bits of prime to be generated.
120 @param[in] Prime Pointer to the prime number.
122 @retval TRUE DH parameter setting succeeded.
123 @retval FALSE Value of Generator is not supported.
124 @retval FALSE Value of Generator is not suitable for the Prime.
125 @retval FALSE Value of Prime is not a prime number.
126 @retval FALSE Value of Prime is not a safe prime number.
132 IN OUT VOID
*DhContext
,
134 IN UINTN PrimeLength
,
135 IN CONST UINT8
*Prime
143 // Check input parameters.
145 if ((DhContext
== NULL
) || (Prime
== NULL
) || (PrimeLength
> INT_MAX
)) {
149 if ((Generator
!= DH_GENERATOR_2
) && (Generator
!= DH_GENERATOR_5
)) {
154 // Set the generator and prime parameters for DH object.
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
)) {
173 Generates DH public key.
175 This function generates random secret exponent, and computes the public key, which is
176 returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.
177 If the PublicKey buffer is too small to hold the public key, FALSE is returned and
178 PublicKeySize is set to the required buffer size to obtain the public key.
180 If DhContext is NULL, then return FALSE.
181 If PublicKeySize is NULL, then return FALSE.
182 If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.
184 @param[in, out] DhContext Pointer to the DH context.
185 @param[out] PublicKey Pointer to the buffer to receive generated public key.
186 @param[in, out] PublicKeySize On input, the size of PublicKey buffer in bytes.
187 On output, the size of data returned in PublicKey buffer in bytes.
189 @retval TRUE DH public key generation succeeded.
190 @retval FALSE DH public key generation failed.
191 @retval FALSE PublicKeySize is not large enough.
197 IN OUT VOID
*DhContext
,
198 OUT UINT8
*PublicKey
,
199 IN OUT UINTN
*PublicKeySize
208 // Check input parameters.
210 if ((DhContext
== NULL
) || (PublicKeySize
== NULL
)) {
214 if ((PublicKey
== NULL
) && (*PublicKeySize
!= 0)) {
218 Dh
= (DH
*)DhContext
;
220 RetVal
= (BOOLEAN
)DH_generate_key (DhContext
);
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
;
229 if (PublicKey
!= NULL
) {
230 BN_bn2bin (DhPubKey
, PublicKey
);
233 *PublicKeySize
= Size
;
240 Computes exchanged common key.
242 Given peer's public key, this function computes the exchanged common key, based on its own
243 context including value of prime modulus and random secret exponent.
245 If DhContext is NULL, then return FALSE.
246 If PeerPublicKey is NULL, then return FALSE.
247 If KeySize is NULL, then return FALSE.
248 If Key is NULL, then return FALSE.
249 If KeySize is not large enough, then return FALSE.
251 @param[in, out] DhContext Pointer to the DH context.
252 @param[in] PeerPublicKey Pointer to the peer's public key.
253 @param[in] PeerPublicKeySize Size of peer's public key in bytes.
254 @param[out] Key Pointer to the buffer to receive generated key.
255 @param[in, out] KeySize On input, the size of Key buffer in bytes.
256 On output, the size of data returned in Key buffer in bytes.
258 @retval TRUE DH exchanged key generation succeeded.
259 @retval FALSE DH exchanged key generation failed.
260 @retval FALSE KeySize is not large enough.
266 IN OUT VOID
*DhContext
,
267 IN CONST UINT8
*PeerPublicKey
,
268 IN UINTN PeerPublicKeySize
,
270 IN OUT UINTN
*KeySize
277 // Check input parameters.
279 if ((DhContext
== NULL
) || (PeerPublicKey
== NULL
) || (KeySize
== NULL
) || (Key
== NULL
)) {
283 if (PeerPublicKeySize
> INT_MAX
) {
287 Bn
= BN_bin2bn (PeerPublicKey
, (UINT32
)PeerPublicKeySize
, NULL
);
292 Size
= DH_compute_key (Key
, Bn
, DhContext
);
298 if (*KeySize
< (UINTN
)Size
) {