]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Cipher/CryptAes.c
CryptoPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Cipher / CryptAes.c
index e32063cd9883ba02362ad0e00e19315fa7f9f821..2515b34bb8c4bc7b5d7476fb7aff5fbf83b3a255 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   AES Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010, 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
@@ -38,13 +32,13 @@ AesGetContextSize (
   Initializes user-supplied memory as AES context for subsequent use.\r
 \r
   This function initializes user-supplied memory pointed by AesContext as AES context.\r
-  In addtion, it sets up all AES key materials for subsequent encryption and decryption\r
+  In addition, it sets up all AES key materials for subsequent encryption and decryption\r
   operations.\r
   There are 3 options for key length, 128 bits, 192 bits, and 256 bits.\r
 \r
-  If AesContext is NULL, then ASSERT().\r
-  If Key is NULL, then ASSERT().\r
-  If KeyLength is not valid, then ASSERT().\r
+  If AesContext is NULL, then return FALSE.\r
+  If Key is NULL, then return FALSE.\r
+  If KeyLength is not valid, then return FALSE.\r
 \r
   @param[out]  AesContext  Pointer to AES context being initialized.\r
   @param[in]   Key         Pointer to the user-supplied AES key.\r
@@ -64,12 +58,12 @@ AesInit (
 {\r
   AES_KEY  *AesKey;\r
 \r
-  ASSERT (AesContext != NULL);\r
   //\r
-  // AES Key Checking\r
+  // Check input parameters.\r
   //\r
-  ASSERT (Key != NULL);\r
-  ASSERT ((KeyLength == 128) || (KeyLength == 192) || (KeyLength == 256));\r
+  if (AesContext == NULL || Key == NULL || (KeyLength != 128 && KeyLength != 192 && KeyLength != 256)) {\r
+    return FALSE;\r
+  }\r
 \r
   //\r
   // Initialize AES encryption & decryption key schedule.\r
@@ -94,10 +88,10 @@ AesInit (
   AesContext should be already correctly initialized by AesInit(). Behavior with\r
   invalid AES context is undefined.\r
 \r
-  If AesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (16 bytes), then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If AesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (16 bytes), then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   AesContext  Pointer to the AES context.\r
   @param[in]   Input       Pointer to the buffer containing the data to be encrypted.\r
@@ -118,11 +112,13 @@ AesEcbEncrypt (
   )\r
 {\r
   AES_KEY  *AesKey;\r
-  \r
-  ASSERT (AesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % AES_BLOCK_SIZE) == 0);\r
-  ASSERT (Output != NULL);\r
+\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0 || Output == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   AesKey = (AES_KEY *) AesContext;\r
 \r
@@ -149,10 +145,10 @@ AesEcbEncrypt (
   AesContext should be already correctly initialized by AesInit(). Behavior with\r
   invalid AES context is undefined.\r
 \r
-  If AesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (16 bytes), then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If AesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (16 bytes), then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   AesContext  Pointer to the AES context.\r
   @param[in]   Input       Pointer to the buffer containing the data to be decrypted.\r
@@ -173,11 +169,13 @@ AesEcbDecrypt (
   )\r
 {\r
   AES_KEY  *AesKey;\r
-  \r
-  ASSERT (AesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % AES_BLOCK_SIZE) == 0);\r
-  ASSERT (Output != NULL);\r
+\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0 || Output == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   AesKey = (AES_KEY *) AesContext;\r
 \r
@@ -205,11 +203,11 @@ AesEcbDecrypt (
   AesContext should be already correctly initialized by AesInit(). Behavior with\r
   invalid AES context is undefined.\r
 \r
-  If AesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (16 bytes), then ASSERT().\r
-  If Ivec is NULL, then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If AesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (16 bytes), then return FALSE.\r
+  If Ivec is NULL, then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   AesContext  Pointer to the AES context.\r
   @param[in]   Input       Pointer to the buffer containing the data to be encrypted.\r
@@ -234,11 +232,16 @@ AesCbcEncrypt (
   AES_KEY  *AesKey;\r
   UINT8    IvecBuffer[AES_BLOCK_SIZE];\r
 \r
-  ASSERT (AesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % AES_BLOCK_SIZE) == 0);\r
-  ASSERT (Ivec != NULL);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (Ivec == NULL || Output == NULL || InputSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   AesKey = (AES_KEY *) AesContext;\r
   CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);\r
@@ -262,11 +265,11 @@ AesCbcEncrypt (
   AesContext should be already correctly initialized by AesInit(). Behavior with\r
   invalid AES context is undefined.\r
 \r
-  If AesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (16 bytes), then ASSERT().\r
-  If Ivec is NULL, then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If AesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (16 bytes), then return FALSE.\r
+  If Ivec is NULL, then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   AesContext  Pointer to the AES context.\r
   @param[in]   Input       Pointer to the buffer containing the data to be encrypted.\r
@@ -290,12 +293,17 @@ AesCbcDecrypt (
 {\r
   AES_KEY  *AesKey;\r
   UINT8    IvecBuffer[AES_BLOCK_SIZE];\r
-  \r
-  ASSERT (AesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % AES_BLOCK_SIZE) == 0);\r
-  ASSERT (Ivec != NULL);\r
-  ASSERT (Output != NULL);\r
+\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (Ivec == NULL || Output == NULL || InputSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   AesKey = (AES_KEY *) AesContext;\r
   CopyMem (IvecBuffer, Ivec, AES_BLOCK_SIZE);\r