X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=CryptoPkg%2FLibrary%2FBaseCryptLib%2FPk%2FCryptDh.c;h=abd581d93f25c8fe907439cad7361ce28c0741d0;hp=9cbf182c70f5b292a39ab2b67107208fa0173e26;hb=630f67ddfea296ccb59d7863796210e130eec67e;hpb=1cae0c83bbd88822076542e2715077ca2a8bcadb diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c index 9cbf182c70..abd581d93f 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 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2018, 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 @@ -16,7 +16,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include - /** Allocates and Initializes one Diffie-Hellman Context for subsequent use. @@ -61,7 +60,7 @@ DhFree ( Given generator g, and length of prime number p in bits, this function generates p, and sets DH context according to value of g and p. - + Before this function can be invoked, pseudorandom number generator must be correctly initialized by RandomSeed(). @@ -73,7 +72,7 @@ DhFree ( @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. @@ -88,6 +87,7 @@ DhGenerateParameter ( ) { BOOLEAN RetVal; + BIGNUM *BnP; // // Check input parameters. @@ -105,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; } @@ -124,7 +125,7 @@ DhGenerateParameter ( @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. @@ -141,7 +142,8 @@ DhSetParameter ( ) { DH *Dh; - BIGNUM *Bn; + BIGNUM *BnP; + BIGNUM *BnG; // // Check input parameters. @@ -149,57 +151,34 @@ DhSetParameter ( if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) { return FALSE; } - + if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) { return FALSE; } - Bn = NULL; - - Dh = (DH *) DhContext; - Dh->g = NULL; - Dh->p = BN_new (); - if (Dh->p == NULL) { - goto Error; - } - - Dh->g = BN_new (); - if (Dh->g == NULL) { - goto Error; - } - - Bn = BN_bin2bn (Prime, (UINT32) (PrimeLength / 8), Dh->p); - if (Bn == NULL) { - goto Error; - } - - if (BN_set_word (Dh->g, (UINT32) Generator) == 0) { + // + // 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); - if (Dh->p != NULL) { - BN_free (Dh->p); - } - - if (Dh->g != NULL) { - BN_free (Dh->g); - } - - if (Bn != NULL) { - BN_free (Bn); - } - return FALSE; } /** Generates DH public key. - This function generates random secret exponent, and computes the public key, which is + This function generates random secret exponent, and computes the public key, which is returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly. 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. @@ -228,6 +207,7 @@ DhGenerateKey ( { BOOLEAN RetVal; DH *Dh; + BIGNUM *DhPubKey; INTN Size; // @@ -240,18 +220,21 @@ DhGenerateKey ( if (PublicKey == NULL && *PublicKeySize != 0) { return FALSE; } - + Dh = (DH *) DhContext; RetVal = (BOOLEAN) DH_generate_key (DhContext); if (RetVal) { - Size = 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; } - - BN_bn2bin (Dh->pub_key, PublicKey); + + if (PublicKey != NULL) { + BN_bn2bin (DhPubKey, PublicKey); + } *PublicKeySize = Size; } @@ -262,7 +245,7 @@ DhGenerateKey ( Computes exchanged common key. 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. + context including value of prime modulus and random secret exponent. If DhContext is NULL, then return FALSE. If PeerPublicKey is NULL, then return FALSE. @@ -305,7 +288,7 @@ DhComputeKey ( if (PeerPublicKeySize > INT_MAX) { return FALSE; } - + Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL); if (Bn == NULL) { return FALSE;