X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptDh.c;h=391efd5c14550f16bd9466ad076d2313a3b3bb83;hp=b7e164cf43770dc8a38bf4aca58bd25148ee0db8;hb=a9fb7b7803763e045bd626ec7df3ce4900e3e927;hpb=a8c4464502aabcbda7032daddc772a1bc7386bdf diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c index b7e164cf43..391efd5c14 100644 --- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c +++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c @@ -1,7 +1,7 @@ /** @file Diffie-Hellman Wrapper Implementation over OpenSSL. -Copyright (c) 2010, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -13,9 +13,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ #include "InternalCryptLib.h" +#include #include - /** Allocates and Initializes one Diffie-Hellman Context for subsequent use. @@ -32,13 +32,13 @@ DhNew ( // // Allocates & Initializes DH Context by OpenSSL DH_new() // - return (VOID *)DH_new (); + return (VOID *) DH_new (); } /** Release the specified DH context. - If DhContext is NULL, then ASSERT(). + If DhContext is NULL, then return FALSE. @param[in] DhContext Pointer to the DH context to be released. @@ -52,7 +52,7 @@ DhFree ( // // Free OpenSSL DH Context // - DH_free ((DH *)DhContext); + DH_free ((DH *) DhContext); } /** @@ -64,15 +64,15 @@ DhFree ( Before this function can be invoked, pseudorandom number generator must be correctly initialized by RandomSeed(). - If DhContext is NULL, then ASSERT(). - If Prime is NULL, then ASSERT(). + If DhContext is NULL, then return FALSE. + If Prime is NULL, then return FALSE. @param[in, out] DhContext Pointer to the DH context. @param[in] Generator Value of generator. @param[in] PrimeLength Length in bits of prime to be generated. @param[out] Prime Pointer to the buffer to receive the generated prime number. - @retval TRUE DH pamameter generation succeeded. + @retval TRUE DH parameter generation succeeded. @retval FALSE Value of Generator is not supported. @retval FALSE PRNG fails to generate random prime number with PrimeLength. @@ -87,6 +87,14 @@ DhGenerateParameter ( ) { BOOLEAN RetVal; + BIGNUM *BnP; + + // + // Check input parameters. + // + if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) { + return FALSE; + } if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) { return FALSE; @@ -97,7 +105,8 @@ DhGenerateParameter ( return FALSE; } - BN_bn2bin (((DH *) DhContext)->p, Prime); + DH_get0_pqg (DhContext, (const BIGNUM **)&BnP, NULL, NULL); + BN_bn2bin (BnP, Prime); return TRUE; } @@ -108,15 +117,15 @@ DhGenerateParameter ( Given generator g, and prime number p, this function and sets DH context accordingly. - If DhContext is NULL, then ASSERT(). - If Prime is NULL, then ASSERT(). + If DhContext is NULL, then return FALSE. + If Prime is NULL, then return FALSE. @param[in, out] DhContext Pointer to the DH context. @param[in] Generator Value of generator. @param[in] PrimeLength Length in bits of prime to be generated. @param[in] Prime Pointer to the prime number. - @retval TRUE DH pamameter setting succeeded. + @retval TRUE DH parameter setting succeeded. @retval FALSE Value of Generator is not supported. @retval FALSE Value of Generator is not suitable for the Prime. @retval FALSE Value of Prime is not a prime number. @@ -132,20 +141,38 @@ DhSetParameter ( IN CONST UINT8 *Prime ) { - DH *Dh; + DH *Dh; + BIGNUM *BnP; + BIGNUM *BnG; - if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) { + // + // Check input parameters. + // + if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) { return FALSE; } - Dh = (DH *) DhContext; - Dh->p = BN_new(); - Dh->g = BN_new(); + if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) { + return FALSE; + } - BN_bin2bn (Prime, (UINT32) (PrimeLength / 8), Dh->p); - BN_set_word (Dh->g, (UINT32) Generator); + // + // Set the generator and prime parameters for DH object. + // + Dh = (DH *)DhContext; + BnP = BN_bin2bn ((const unsigned char *)Prime, (int)(PrimeLength / 8), NULL); + BnG = BN_bin2bn ((const unsigned char *)&Generator, 1, NULL); + if ((BnP == NULL) || (BnG == NULL) || !DH_set0_pqg (Dh, BnP, NULL, BnG)) { + goto Error; + } return TRUE; + +Error: + BN_free (BnP); + BN_free (BnG); + + return FALSE; } /** @@ -156,9 +183,9 @@ DhSetParameter ( If the PublicKey buffer is too small to hold the public key, FALSE is returned and PublicKeySize is set to the required buffer size to obtain the public key. - If DhContext is NULL, then ASSERT(). - If PublicKeySize is NULL, then ASSERT(). - If PublicKeySize is large enough but PublicKey is NULL, then ASSERT(). + If DhContext is NULL, then return FALSE. + If PublicKeySize is NULL, then return FALSE. + If PublicKeySize is large enough but PublicKey is NULL, then return FALSE. @param[in, out] DhContext Pointer to the DH context. @param[out] PublicKey Pointer to the buffer to receive generated public key. @@ -180,14 +207,35 @@ DhGenerateKey ( { BOOLEAN RetVal; DH *Dh; + BIGNUM *DhPubKey; + INTN Size; + + // + // Check input parameters. + // + if (DhContext == NULL || PublicKeySize == NULL) { + return FALSE; + } + + if (PublicKey == NULL && *PublicKeySize != 0) { + return FALSE; + } Dh = (DH *) DhContext; - *PublicKeySize = 0; RetVal = (BOOLEAN) DH_generate_key (DhContext); if (RetVal) { - BN_bn2bin (Dh->pub_key, PublicKey); - *PublicKeySize = BN_num_bytes (Dh->pub_key); + DH_get0_key (Dh, (const BIGNUM **)&DhPubKey, NULL); + Size = BN_num_bytes (DhPubKey); + if ((Size > 0) && (*PublicKeySize < (UINTN) Size)) { + *PublicKeySize = Size; + return FALSE; + } + + if (PublicKey != NULL) { + BN_bn2bin (DhPubKey, PublicKey); + } + *PublicKeySize = Size; } return RetVal; @@ -199,10 +247,11 @@ DhGenerateKey ( Given peer's public key, this function computes the exchanged common key, based on its own context including value of prime modulus and random secret exponent. - If DhContext is NULL, then ASSERT(). - If PeerPublicKey is NULL, then ASSERT(). - If KeySize is NULL, then ASSERT(). - If KeySize is large enough but Key is NULL, then ASSERT(). + If DhContext is NULL, then return FALSE. + If PeerPublicKey is NULL, then return FALSE. + If KeySize is NULL, then return FALSE. + If Key is NULL, then return FALSE. + If KeySize is not large enough, then return FALSE. @param[in, out] DhContext Pointer to the DH context. @param[in] PeerPublicKey Pointer to the peer's public key. @@ -227,12 +276,37 @@ DhComputeKey ( ) { BIGNUM *Bn; + INTN Size; + // + // Check input parameters. + // + if (DhContext == NULL || PeerPublicKey == NULL || KeySize == NULL || Key == NULL) { + return FALSE; + } + + if (PeerPublicKeySize > INT_MAX) { + return FALSE; + } + Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL); + if (Bn == NULL) { + return FALSE; + } + + Size = DH_compute_key (Key, Bn, DhContext); + if (Size < 0) { + BN_free (Bn); + return FALSE; + } - *KeySize = (BOOLEAN) DH_compute_key (Key, Bn, DhContext); + if (*KeySize < (UINTN) Size) { + *KeySize = Size; + BN_free (Bn); + return FALSE; + } + *KeySize = Size; BN_free (Bn); - return TRUE; }