]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/IpSecDxe/IpSecCryptIo.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / IpSecDxe / IpSecCryptIo.c
index 8396c5900391651e435f42e4b9188ac228311a1e..b87e2ca8d4c2719a292a9f71e5c2db245554adf8 100644 (file)
@@ -1,15 +1,9 @@
 /** @file\r
   Common interfaces to call Security library.\r
 \r
-  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
 \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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -19,7 +13,7 @@
 //\r
 GLOBAL_REMOVE_IF_UNREFERENCED ENCRYPT_ALGORITHM mIpsecEncryptAlgorithmList[IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE] = {\r
   {IKE_EALG_NULL, 0, 0, 1, NULL, NULL, NULL, NULL},\r
-  {IKE_EALG_NONE, 0, 0, 1, NULL, NULL, NULL, NULL},  \r
+  {IKE_EALG_NONE, 0, 0, 1, NULL, NULL, NULL, NULL},\r
   {IKE_EALG_3DESCBC, 24, 8, 8, TdesGetContextSize, TdesInit, TdesCbcEncrypt, TdesCbcDecrypt},\r
   {IKE_EALG_AESCBC, 16, 16, 16, AesGetContextSize, AesInit, AesCbcEncrypt, AesCbcDecrypt}\r
 };\r
@@ -45,7 +39,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED HASH_ALGORITHM mIpsecHashAlgorithmList[IPSEC_HASH_
 BOOLEAN  mInitialRandomSeed = FALSE;\r
 \r
 /**\r
-  Get the block size of specified encryption alogrithm.\r
+  Get the block size of specified encryption algorithm.\r
 \r
   @param[in]  AlgorithmId          The encryption algorithm ID.\r
 \r
@@ -69,7 +63,7 @@ IpSecGetEncryptBlockSize (
 }\r
 \r
 /**\r
-  Get the key length of the specified encryption alogrithm.\r
+  Get the key length of the specified encryption algorithm.\r
 \r
   @param[in]  AlgorithmId          The encryption algorithm ID.\r
 \r
@@ -93,7 +87,7 @@ IpSecGetEncryptKeyLength (
 }\r
 \r
 /**\r
-  Get the IV size of the specified encryption alogrithm.\r
+  Get the IV size of the specified encryption algorithm.\r
 \r
   @param[in]  AlgorithmId          The encryption algorithm ID.\r
 \r
@@ -144,7 +138,7 @@ IpSecGetHmacDigestLength (
 }\r
 \r
 /**\r
-  Get the ICV size of the specified Authenticaion alogrithm.\r
+  Get the ICV size of the specified Authenticaion algorithm.\r
 \r
   @param[in]  AlgorithmId          The Authentication algorithm ID.\r
 \r
@@ -186,17 +180,17 @@ IpSecGenerateIv (
   if (IvSize != 0) {\r
     return IpSecCryptoIoGenerateRandomBytes (IvBuffer, IvSize);\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Get index of the specified encryption alogrithm from the mIpsecEncryptAlgorithemList.\r
+  Get index of the specified encryption algorithm from the mIpsecEncryptAlgorithmList.\r
 \r
   @param[in]  AlgorithmId          The encryption algorithm ID.\r
 \r
   @return the index.\r
-  \r
+\r
 **/\r
 UINTN\r
 IpSecGetIndexFromEncList (\r
@@ -204,23 +198,23 @@ IpSecGetIndexFromEncList (
   )\r
 {\r
   UINT8 Index;\r
-  \r
+\r
   for (Index = 0; Index < IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE; Index++) {\r
     if (AlgorithmId == mIpsecEncryptAlgorithmList[Index].AlgorithmId) {\r
       return Index;\r
     }\r
   }\r
-  \r
+\r
   return (UINTN) -1;\r
 }\r
 \r
 /**\r
-  Get index of the specified encryption alogrithm from the mIpsecAuthAlgorithemList.\r
+  Get index of the specified encryption algorithm from the mIpsecAuthAlgorithmList.\r
 \r
   @param[in]  AlgorithmId          The encryption algorithm ID.\r
 \r
   @return the index.\r
-  \r
+\r
 **/\r
 UINTN\r
 IpSecGetIndexFromAuthList (\r
@@ -228,7 +222,7 @@ IpSecGetIndexFromAuthList (
   )\r
 {\r
   UINT8 Index;\r
-  \r
+\r
   for (Index = 0; Index < IPSEC_AUTH_ALGORITHM_LIST_SIZE; Index++) {\r
     if (AlgorithmId == mIpsecAuthAlgorithmList[Index].AlgorithmId) {\r
       //\r
@@ -237,7 +231,7 @@ IpSecGetIndexFromAuthList (
       return Index;\r
     }\r
   }\r
-  \r
+\r
   return (UINTN) -1;\r
 }\r
 \r
@@ -245,14 +239,14 @@ IpSecGetIndexFromAuthList (
   Encrypt the buffer.\r
 \r
   This function calls relevant encryption interface from CryptoLib according to\r
-  the input alogrithm ID. The InData should be multiple of block size. This function\r
+  the input algorithm ID. The InData should be multiple of block size. This function\r
   doesn't perform the padding. If it has the Ivec data, the length of it should be\r
   same with the block size. The block size is different from the different algorithm.\r
 \r
-  @param[in]       AlgorithmId    The Alogrithem identification defined in RFC.\r
+  @param[in]       AlgorithmId    The Algorithm identification defined in RFC.\r
   @param[in]       Key            Pointer to the buffer containing encrypting key.\r
   @param[in]       KeyBits        The length of the key in bits.\r
-  @param[in]       Ivec           Point to the buffer containning the Initializeion\r
+  @param[in]       Ivec           Point to the buffer containing the Initialization\r
                                   Vector (IV) data.\r
   @param[in]       InData         Point to the buffer containing the data to be\r
                                   encrypted.\r
@@ -275,14 +269,14 @@ IpSecCryptoIoEncrypt (
   IN       UINTN      InDataLength,\r
      OUT   UINT8      *OutData\r
   )\r
-{  \r
+{\r
   UINTN         Index;\r
   UINTN         ContextSize;\r
   UINT8         *Context;\r
   EFI_STATUS    Status;\r
-  \r
+\r
   Status = EFI_UNSUPPORTED;\r
-  \r
+\r
   switch (AlgorithmId) {\r
 \r
   case IKE_EALG_NULL:\r
@@ -323,7 +317,7 @@ IpSecCryptoIoEncrypt (
   if (Context != NULL) {\r
     FreePool (Context);\r
   }\r
-  \r
+\r
   return Status;\r
 }\r
 \r
@@ -331,14 +325,14 @@ IpSecCryptoIoEncrypt (
   Decrypts the buffer.\r
 \r
   This function calls relevant Decryption interface from CryptoLib according to\r
-  the input alogrithm ID. The InData should be multiple of block size. This function\r
+  the input algorithm ID. The InData should be multiple of block size. This function\r
   doesn't perform the padding. If it has the Ivec data, the length of it should be\r
   same with the block size. The block size is different from the different algorithm.\r
 \r
-  @param[in]       AlgorithmId    The Alogrithem identification defined in RFC.\r
+  @param[in]       AlgorithmId    The Algorithm identification defined in RFC.\r
   @param[in]       Key            Pointer to the buffer containing encrypting key.\r
   @param[in]       KeyBits        The length of the key in bits.\r
-  @param[in]       Ivec           Point to the buffer containning the Initializeion\r
+  @param[in]       Ivec           Point to the buffer containing the Initialization\r
                                   Vector (IV) data.\r
   @param[in]       InData         Point to the buffer containing the data to be\r
                                   decrypted.\r
@@ -361,7 +355,7 @@ IpSecCryptoIoDecrypt (
   IN       UINTN      InDataLength,\r
      OUT   UINT8      *OutData\r
   )\r
-{  \r
+{\r
   UINTN         Index;\r
   UINTN         ContextSize;\r
   UINT8         *Context;\r
@@ -397,7 +391,7 @@ IpSecCryptoIoDecrypt (
     //\r
     if (mIpsecEncryptAlgorithmList[Index].CipherInitiate (Context, Key, KeyBits)) {\r
       if (mIpsecEncryptAlgorithmList[Index].CipherDecrypt (Context, InData, InDataLength, Ivec, OutData)) {\r
-        Status = EFI_SUCCESS;      \r
+        Status = EFI_SUCCESS;\r
       }\r
     }\r
     break;\r
@@ -417,10 +411,10 @@ IpSecCryptoIoDecrypt (
   Digests the Payload with key and store the result into the OutData.\r
 \r
   This function calls relevant Hmac interface from CryptoLib according to\r
-  the input alogrithm ID. It computes all datas from InDataFragment and output\r
+  the input algorithm ID. It computes all datas from InDataFragment and output\r
   the result into the OutData buffer. If the OutDataSize is larger than the related\r
-  HMAC alogrithm output size, return EFI_INVALID_PARAMETER.\r
-  \r
+  HMAC algorithm output size, return EFI_INVALID_PARAMETER.\r
+\r
   @param[in]      AlgorithmId     The authentication Identification.\r
   @param[in]      Key             Pointer of the authentication key.\r
   @param[in]      KeyLength       The length of the Key in bytes.\r
@@ -519,8 +513,8 @@ IpSecCryptoIoHmac (
       }\r
 \r
       goto Exit;\r
-    }    \r
-      \r
+    }\r
+\r
   default:\r
     return Status;\r
   }\r
@@ -540,9 +534,9 @@ Exit:
   Digests the Payload and store the result into the OutData.\r
 \r
   This function calls relevant Hash interface from CryptoLib according to\r
-  the input alogrithm ID. It computes all datas from InDataFragment and output\r
+  the input algorithm ID. It computes all datas from InDataFragment and output\r
   the result into the OutData buffer. If the OutDataSize is larger than the related\r
-  Hash alogrithm output size, return EFI_INVALID_PARAMETER.\r
+  Hash algorithm output size, return EFI_INVALID_PARAMETER.\r
 \r
   @param[in]      AlgorithmId     The authentication Identification.\r
   @param[in]      InDataFragment  A list contains all data to be authenticated.\r
@@ -577,11 +571,11 @@ IpSecCryptoIoHash (
 \r
   Status      = EFI_UNSUPPORTED;\r
   OutHashData = NULL;\r
-  \r
+\r
   OutHashSize = IpSecGetHmacDigestLength (AlgorithmId);\r
   //\r
   // If the expected hash data size is larger than the related Hash algorithm\r
-  // output length, return EFI_INVALID_PARAMETER.  \r
+  // output length, return EFI_INVALID_PARAMETER.\r
   //\r
   if (OutDataSize > OutHashSize) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -590,7 +584,7 @@ IpSecCryptoIoHash (
   if (OutHashData == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   switch (AlgorithmId) {\r
 \r
   case IKE_AALG_NONE:\r
@@ -611,7 +605,7 @@ IpSecCryptoIoHash (
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Exit;\r
     }\r
-    \r
+\r
     //\r
     // Initiate Hash context and hash the input data.\r
     //\r
@@ -631,13 +625,13 @@ IpSecCryptoIoHash (
         // In some cases, like the Icv computing, the Icv size might be less than\r
         // the key length size, so copy the part of hash data to the OutData.\r
         //\r
-        CopyMem (OutData, OutHashData, OutDataSize);            \r
+        CopyMem (OutData, OutHashData, OutDataSize);\r
         Status = EFI_SUCCESS;\r
       }\r
-      \r
-      goto Exit;        \r
-    }    \r
-    \r
+\r
+      goto Exit;\r
+    }\r
+\r
   default:\r
     return Status;\r
   }\r
@@ -657,14 +651,14 @@ Exit:
   Generates the Diffie-Hellman public key.\r
 \r
   This function first initiate a DHContext, then call the DhSetParameter() to set\r
-  the prime and primelenght, at end call the DhGenerateKey() to generates random\r
+  the prime and primelength, at end call the DhGenerateKey() to generates random\r
   secret exponent, and computes the public key. The output returned via parameter\r
   PublicKey and PublicKeySize. DH context is updated accordingly. If the PublicKey\r
   buffer is too small to hold the public key, EFI_INVALID_PARAMETER is returned\r
   and PublicKeySize is set to the required buffer size to obtain the public key.\r
 \r
   @param[in, out] DhContext       Pointer to the DH context.\r
-  @param[in]      Generator       Vlaue of generator.\r
+  @param[in]      Generator       Value of generator.\r
   @param[in]      PrimeLength     Length in bits of prime to be generated.\r
   @param[in]      Prime           Pointer to the buffer to receive the generated\r
                                   prime number.\r
@@ -673,7 +667,7 @@ Exit:
                                   For out, the size of data returned in PublicKey\r
                                   buffer in bytes.\r
 \r
-  @retval EFI_SUCCESS             The operation perfoms successfully.\r
+  @retval EFI_SUCCESS             The operation performs successfully.\r
   @retval Otherwise               The operation is failed.\r
 \r
 **/\r
@@ -685,10 +679,10 @@ IpSecCryptoIoDhGetPublicKey (
   IN CONST UINT8  *Prime,\r
      OUT   UINT8  *PublicKey,\r
   IN OUT   UINTN  *PublicKeySize\r
-  ) \r
+  )\r
 {\r
   EFI_STATUS   Status;\r
-  \r
+\r
   *DhContext = DhNew ();\r
   ASSERT (*DhContext != NULL);\r
   if (!DhSetParameter (*DhContext, Generator, PrimeLength, Prime)) {\r
@@ -707,7 +701,7 @@ Exit:
     DhFree (*DhContext);\r
     DhContext = NULL;\r
   }\r
-  \r
+\r
   return Status;\r
 }\r
 \r
@@ -725,13 +719,13 @@ Exit:
                                     For out, the size of data returned in Key\r
                                     buffer in bytes.\r
 \r
-  @retval EFI_SUCCESS              The operation perfoms successfully.\r
+  @retval EFI_SUCCESS              The operation performs successfully.\r
   @retval Otherwise                The operation is failed.\r
 \r
 **/\r
 EFI_STATUS\r
 IpSecCryptoIoDhComputeKey (\r
-  IN   OUT   UINT8  *DhContext,     \r
+  IN   OUT   UINT8  *DhContext,\r
   IN   CONST UINT8  *PeerPublicKey,\r
   IN         UINTN  PeerPublicKeySize,\r
        OUT   UINT8  *Key,\r
@@ -750,15 +744,15 @@ IpSecCryptoIoDhComputeKey (
 \r
   @param[in, out]     DhContext         Pointer to the DH context to be freed.\r
 \r
-  @retval EFI_SUCCESS              The operation perfoms successfully.\r
+  @retval EFI_SUCCESS              The operation performs successfully.\r
   @retval EFI_INVALID_PARAMETER    The DhContext is NULL.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 IpSecCryptoIoFreeDh (\r
   IN   OUT   UINT8  **DhContext\r
   )\r
-{ \r
+{\r
   if (*DhContext == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -773,9 +767,9 @@ IpSecCryptoIoFreeDh (
   If the Random Generator wasn't initiated, initiate it first, then call RandomBytes.\r
 \r
   @param[out]  OutBuffer        Pointer to buffer to receive random value.\r
-  @param[in]   Bytes            Size of randome bytes to generate.\r
+  @param[in]   Bytes            Size of random bytes to generate.\r
 \r
-  @retval EFI_SUCCESS              The operation perfoms successfully.\r
+  @retval EFI_SUCCESS              The operation performs successfully.\r
   @retval Otherwise                The operation is failed.\r
 \r
 **/\r
@@ -807,7 +801,7 @@ IpSecCryptoIoGenerateRandomBytes (
   @param[in]      KeyPwdSize      The size of Key Password in bytes.\r
   @param[out]     OutData         The pointer to the signed data.\r
   @param[in, out] OutDataSize     Pointer to contain the size of out data.\r
\r
+\r
 **/\r
 VOID\r
 IpSecCryptoIoAuthDataWithCertificate (\r
@@ -824,7 +818,7 @@ IpSecCryptoIoAuthDataWithCertificate (
   UINT8         *RsaContext;\r
   UINT8         *Signature;\r
   UINTN         SigSize;\r
-   \r
+\r
   SigSize   = 0;\r
   RsaContext = NULL;\r
 \r
@@ -844,12 +838,12 @@ IpSecCryptoIoAuthDataWithCertificate (
   //\r
   // Sign data\r
   //\r
-  Signature = NULL;  \r
+  Signature = NULL;\r
   if (!RsaPkcs1Sign (RsaContext, InData, InDataSize, Signature, &SigSize)) {\r
     Signature = AllocateZeroPool (SigSize);\r
   } else {\r
     return;\r
-  } \r
+  }\r
 \r
   RsaPkcs1Sign (RsaContext, InData, InDataSize, Signature, &SigSize);\r
 \r
@@ -869,14 +863,14 @@ IpSecCryptoIoAuthDataWithCertificate (
   @param[in]     CertLen         The size of Certificate in bytes.\r
   @param[in]     InCa            Pointer to the CA certificate\r
   @param[in]     CaLen           The size of CA certificate in bytes.\r
-  @param[in]     InData          Pointer to octect message hash to be checked.\r
+  @param[in]     InData          Pointer to octet message hash to be checked.\r
   @param[in]     InDataSize      Size of the message hash in bytes.\r
-  @param[in]     Singnature      The pointer to the RSA PKCS1-V1_5 signature to be verifed.\r
+  @param[in]     Singnature      The pointer to the RSA PKCS1-V1_5 signature to be verified.\r
   @param[in]     SigSize         Size of signature in bytes.\r
 \r
   @retval  TRUE   Valid signature encoded in PKCS1-v1_5.\r
   @retval  FALSE  Invalid signature or invalid RSA context.\r
\r
+\r
 **/\r
 BOOLEAN\r
 IpSecCryptoIoVerifySignDataByCertificate (\r
@@ -912,7 +906,7 @@ IpSecCryptoIoVerifySignDataByCertificate (
   // Retrieve the RSA public Key from Certificate\r
   //\r
   RsaGetPublicKeyFromX509 ((CONST UINT8 *)InCert, CertLen, (VOID **)&RsaContext);\r
\r
+\r
   //\r
   // Verify data\r
   //\r
@@ -964,11 +958,14 @@ IpSecCryptoIoGetPublicKeyFromCert (
   }\r
 \r
   *PublicKeyLen = 0;\r
\r
+\r
   RsaGetKey (RsaContext, RsaKeyN, NULL, PublicKeyLen);\r
\r
+\r
   *PublicKey = AllocateZeroPool (*PublicKeyLen);\r
-  ASSERT (*PublicKey != NULL);\r
+  if (*PublicKey == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
 \r
   if (!RsaGetKey (RsaContext, RsaKeyN, *PublicKey, PublicKeyLen)) {\r
     Status = EFI_INVALID_PARAMETER;\r
@@ -989,10 +986,10 @@ EXIT:
   @param[in]     CertSize          The size of the X509 certificate in bytes.\r
   @param[out]    CertSubject       Pointer to the retrieved certificate subject.\r
   @param[out]    SubjectSize       The size of Certificate Subject in bytes.\r
-  \r
+\r
   @retval  EFI_SUCCESS            Retrieved the certificate subject successfully.\r
   @retval  EFI_INVALID_PARAMETER  The certificate is malformed.\r
\r
+\r
 **/\r
 EFI_STATUS\r
 IpSecCryptoIoGetSubjectFromCert (\r