]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptDh.c
index 942b3d103e260e49b942efe2395e8717d6570b2e..27322888934d03ded82504a26bff2b8e0a4d389b 100644 (file)
@@ -1,21 +1,15 @@
 /** @file\r
   Diffie-Hellman Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "InternalCryptLib.h"\r
+#include <openssl/bn.h>\r
 #include <openssl/dh.h>\r
 \r
-\r
 /**\r
   Allocates and Initializes one Diffie-Hellman Context for subsequent use.\r
 \r
@@ -32,7 +26,7 @@ DhNew (
   //\r
   // Allocates & Initializes DH Context by OpenSSL DH_new()\r
   //\r
-  return (VOID *) DH_new ();\r
+  return (VOID *)DH_new ();\r
 }\r
 \r
 /**\r
@@ -52,7 +46,7 @@ DhFree (
   //\r
   // Free OpenSSL DH Context\r
   //\r
-  DH_free ((DH *) DhContext);\r
+  DH_free ((DH *)DhContext);\r
 }\r
 \r
 /**\r
@@ -60,7 +54,7 @@ DhFree (
 \r
   Given generator g, and length of prime number p in bits, this function generates p,\r
   and sets DH context according to value of g and p.\r
-  \r
+\r
   Before this function can be invoked, pseudorandom number generator must be correctly\r
   initialized by RandomSeed().\r
 \r
@@ -72,7 +66,7 @@ DhFree (
   @param[in]       PrimeLength  Length in bits of prime to be generated.\r
   @param[out]      Prime        Pointer to the buffer to receive the generated prime number.\r
 \r
-  @retval TRUE   DH pamameter generation succeeded.\r
+  @retval TRUE   DH parameter generation succeeded.\r
   @retval FALSE  Value of Generator is not supported.\r
   @retval FALSE  PRNG fails to generate random prime number with PrimeLength.\r
 \r
@@ -86,25 +80,27 @@ DhGenerateParameter (
   OUT     UINT8  *Prime\r
   )\r
 {\r
-  BOOLEAN RetVal;\r
+  BOOLEAN  RetVal;\r
+  BIGNUM   *BnP;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
+  if ((DhContext == NULL) || (Prime == NULL) || (PrimeLength > INT_MAX)) {\r
     return FALSE;\r
   }\r
 \r
-  if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {\r
+  if ((Generator != DH_GENERATOR_2) && (Generator != DH_GENERATOR_5)) {\r
     return FALSE;\r
   }\r
 \r
-  RetVal = (BOOLEAN) DH_generate_parameters_ex (DhContext, (UINT32) PrimeLength, (UINT32) Generator, NULL);\r
+  RetVal = (BOOLEAN)DH_generate_parameters_ex (DhContext, (UINT32)PrimeLength, (UINT32)Generator, NULL);\r
   if (!RetVal) {\r
     return FALSE;\r
   }\r
 \r
-  BN_bn2bin (((DH *) DhContext)->p, Prime);\r
+  DH_get0_pqg (DhContext, (const BIGNUM **)&BnP, NULL, NULL);\r
+  BN_bn2bin (BnP, Prime);\r
 \r
   return TRUE;\r
 }\r
@@ -123,7 +119,7 @@ DhGenerateParameter (
   @param[in]       PrimeLength  Length in bits of prime to be generated.\r
   @param[in]       Prime        Pointer to the prime number.\r
 \r
-  @retval TRUE   DH pamameter setting succeeded.\r
+  @retval TRUE   DH parameter setting succeeded.\r
   @retval FALSE  Value of Generator is not supported.\r
   @retval FALSE  Value of Generator is not suitable for the Prime.\r
   @retval FALSE  Value of Prime is not a prime number.\r
@@ -140,65 +136,43 @@ DhSetParameter (
   )\r
 {\r
   DH      *Dh;\r
-  BIGNUM  *Bn;\r
+  BIGNUM  *BnP;\r
+  BIGNUM  *BnG;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
-    return FALSE;\r
-  }\r
-  \r
-  if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {\r
+  if ((DhContext == NULL) || (Prime == NULL) || (PrimeLength > INT_MAX)) {\r
     return FALSE;\r
   }\r
 \r
-  Bn = NULL;\r
-\r
-  Dh = (DH *) DhContext;\r
-  Dh->g = NULL;\r
-  Dh->p = BN_new ();\r
-  if (Dh->p == NULL) {\r
-    goto Error;\r
-  }\r
-  \r
-  Dh->g = BN_new ();\r
-  if (Dh->g == NULL) {\r
-    goto Error;\r
-  }\r
-\r
-  Bn = BN_bin2bn (Prime, (UINT32) (PrimeLength / 8), Dh->p);\r
-  if (Bn == NULL) {\r
-    goto Error;\r
+  if ((Generator != DH_GENERATOR_2) && (Generator != DH_GENERATOR_5)) {\r
+    return FALSE;\r
   }\r
 \r
-  if (BN_set_word (Dh->g, (UINT32) Generator) == 0) {\r
+  //\r
+  // Set the generator and prime parameters for DH object.\r
+  //\r
+  Dh  = (DH *)DhContext;\r
+  BnP = BN_bin2bn ((const unsigned char *)Prime, (int)(PrimeLength / 8), NULL);\r
+  BnG = BN_bin2bn ((const unsigned char *)&Generator, 1, NULL);\r
+  if ((BnP == NULL) || (BnG == NULL) || !DH_set0_pqg (Dh, BnP, NULL, BnG)) {\r
     goto Error;\r
   }\r
 \r
   return TRUE;\r
 \r
 Error:\r
+  BN_free (BnP);\r
+  BN_free (BnG);\r
 \r
-  if (Dh->p != NULL) {\r
-    BN_free (Dh->p);\r
-  }\r
-\r
-  if (Dh->g != NULL) {\r
-    BN_free (Dh->g);\r
-  }\r
-\r
-  if (Bn != NULL) {\r
-    BN_free (Bn);\r
-  }\r
-  \r
   return FALSE;\r
 }\r
 \r
 /**\r
   Generates DH public key.\r
 \r
-  This function generates random secret exponent, and computes the public key, which is \r
+  This function generates random secret exponent, and computes the public key, which is\r
   returned via parameter PublicKey and PublicKeySize. DH context is updated accordingly.\r
   If the PublicKey buffer is too small to hold the public key, FALSE is returned and\r
   PublicKeySize is set to the required buffer size to obtain the public key.\r
@@ -225,32 +199,37 @@ DhGenerateKey (
   IN OUT  UINTN  *PublicKeySize\r
   )\r
 {\r
-  BOOLEAN RetVal;\r
-  DH      *Dh;\r
-  INTN    Size;\r
+  BOOLEAN  RetVal;\r
+  DH       *Dh;\r
+  BIGNUM   *DhPubKey;\r
+  INTN     Size;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || PublicKeySize == NULL) {\r
+  if ((DhContext == NULL) || (PublicKeySize == NULL)) {\r
     return FALSE;\r
   }\r
 \r
-  if (PublicKey == NULL && *PublicKeySize != 0) {\r
+  if ((PublicKey == NULL) && (*PublicKeySize != 0)) {\r
     return FALSE;\r
   }\r
-  \r
-  Dh = (DH *) DhContext;\r
 \r
-  RetVal = (BOOLEAN) DH_generate_key (DhContext);\r
+  Dh = (DH *)DhContext;\r
+\r
+  RetVal = (BOOLEAN)DH_generate_key (DhContext);\r
   if (RetVal) {\r
-    Size = BN_num_bytes (Dh->pub_key);\r
-    if ((Size > 0) && (*PublicKeySize < (UINTN) Size)) {\r
+    DH_get0_key (Dh, (const BIGNUM **)&DhPubKey, NULL);\r
+    Size = BN_num_bytes (DhPubKey);\r
+    if ((Size > 0) && (*PublicKeySize < (UINTN)Size)) {\r
       *PublicKeySize = Size;\r
       return FALSE;\r
     }\r
-    \r
-    BN_bn2bin (Dh->pub_key, PublicKey);\r
+\r
+    if (PublicKey != NULL) {\r
+      BN_bn2bin (DhPubKey, PublicKey);\r
+    }\r
+\r
     *PublicKeySize = Size;\r
   }\r
 \r
@@ -261,7 +240,7 @@ DhGenerateKey (
   Computes exchanged common key.\r
 \r
   Given peer's public key, this function computes the exchanged common key, based on its own\r
-  context including value of prime modulus and random secret exponent. \r
+  context including value of prime modulus and random secret exponent.\r
 \r
   If DhContext is NULL, then return FALSE.\r
   If PeerPublicKey is NULL, then return FALSE.\r
@@ -297,15 +276,15 @@ DhComputeKey (
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || PeerPublicKey == NULL || KeySize == NULL || Key == NULL) {\r
+  if ((DhContext == NULL) || (PeerPublicKey == NULL) || (KeySize == NULL) || (Key == NULL)) {\r
     return FALSE;\r
   }\r
 \r
   if (PeerPublicKeySize > INT_MAX) {\r
     return FALSE;\r
   }\r
-  \r
-  Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL);\r
+\r
+  Bn = BN_bin2bn (PeerPublicKey, (UINT32)PeerPublicKeySize, NULL);\r
   if (Bn == NULL) {\r
     return FALSE;\r
   }\r
@@ -316,7 +295,7 @@ DhComputeKey (
     return FALSE;\r
   }\r
 \r
-  if (*KeySize < (UINTN) Size) {\r
+  if (*KeySize < (UINTN)Size) {\r
     *KeySize = Size;\r
     BN_free (Bn);\r
     return FALSE;\r