]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Cipher/CryptTdes.c
Fix several issues in BaseCryptLib:
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Cipher / CryptTdes.c
index 5535ab368650d5f2c517c404c639949323028a11..f89094a58117b96e02f2ad0dc6abbb1670ba142f 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   TDES Wrapper Implementation over OpenSSL.\r
 \r
 /** @file\r
   TDES Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\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
 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
@@ -37,16 +37,16 @@ TdesGetContextSize (
   Initializes user-supplied memory as TDES context for subsequent use.\r
 \r
   This function initializes user-supplied memory pointed by TdesContext as TDES context.\r
   Initializes user-supplied memory as TDES context for subsequent use.\r
 \r
   This function initializes user-supplied memory pointed by TdesContext as TDES context.\r
-  In addtion, it sets up all TDES key materials for subsequent encryption and decryption\r
+  In addition, it sets up all TDES key materials for subsequent encryption and decryption\r
   operations.\r
   There are 3 key options as follows:\r
   KeyLength = 64,  Keying option 1: K1 == K2 == K3 (Backward compatibility with DES)\r
   KeyLength = 128, Keying option 2: K1 != K2 and K3 = K1 (Less Security)\r
   KeyLength = 192  Keying option 3: K1 != K2 != K3 (Strongest)\r
 \r
   operations.\r
   There are 3 key options as follows:\r
   KeyLength = 64,  Keying option 1: K1 == K2 == K3 (Backward compatibility with DES)\r
   KeyLength = 128, Keying option 2: K1 != K2 and K3 = K1 (Less Security)\r
   KeyLength = 192  Keying option 3: K1 != K2 != K3 (Strongest)\r
 \r
-  If TdesContext is NULL, then ASSERT().\r
-  If Key is NULL, then ASSERT().\r
-  If KeyLength is not valid, then ASSERT().\r
+  If TdesContext 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]  TdesContext  Pointer to TDES context being initialized.\r
   @param[in]   Key          Pointer to the user-supplied TDES key.\r
 \r
   @param[out]  TdesContext  Pointer to TDES context being initialized.\r
   @param[in]   Key          Pointer to the user-supplied TDES key.\r
@@ -66,16 +66,19 @@ TdesInit (
 {\r
   DES_key_schedule  *KeySchedule;\r
 \r
 {\r
   DES_key_schedule  *KeySchedule;\r
 \r
-  ASSERT (TdesContext != NULL);\r
-  ASSERT (Key != NULL);\r
-  ASSERT ((KeyLength == 64) || (KeyLength == 128) || (KeyLength == 192));\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (TdesContext == NULL || Key == NULL || (KeyLength != 64 && KeyLength != 128 && KeyLength != 192)) {\r
+    return FALSE;\r
+  }\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
 \r
   //\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
 \r
   //\r
-  // \r
+  // If input Key is a weak key, return error.\r
   //\r
   //\r
-  if (DES_is_weak_key ((const_DES_cblock *) Key)) {\r
+  if (DES_is_weak_key ((const_DES_cblock *) Key) == 1) {\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
@@ -87,7 +90,7 @@ TdesInit (
     return TRUE;\r
   }\r
 \r
     return TRUE;\r
   }\r
 \r
-  if (DES_is_weak_key ((const_DES_cblock *) Key + 8)) {\r
+  if (DES_is_weak_key ((const_DES_cblock *) Key + 8) == 1) {\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
@@ -98,7 +101,7 @@ TdesInit (
     return TRUE;\r
   }\r
 \r
     return TRUE;\r
   }\r
 \r
-  if (DES_is_weak_key ((const_DES_cblock *) Key + 16)) {\r
+  if (DES_is_weak_key ((const_DES_cblock *) Key + 16) == 1) {\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
@@ -117,10 +120,10 @@ TdesInit (
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
-  If TdesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (8 bytes), then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If TdesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (8 bytes), then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be encrypted.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be encrypted.\r
@@ -142,10 +145,12 @@ TdesEcbEncrypt (
 {\r
   DES_key_schedule  *KeySchedule;\r
 \r
 {\r
   DES_key_schedule  *KeySchedule;\r
 \r
-  ASSERT (TdesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % TDES_BLOCK_SIZE) == 0);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (TdesContext == NULL || Input == NULL || (InputSize % TDES_BLOCK_SIZE) != 0 || Output == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
 \r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
 \r
@@ -176,10 +181,10 @@ TdesEcbEncrypt (
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
-  If TdesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (8 bytes), then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If TdesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (8 bytes), then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be decrypted.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be decrypted.\r
@@ -201,10 +206,12 @@ TdesEcbDecrypt (
 {\r
   DES_key_schedule  *KeySchedule;\r
 \r
 {\r
   DES_key_schedule  *KeySchedule;\r
 \r
-  ASSERT (TdesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % TDES_BLOCK_SIZE) == 0);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (TdesContext == NULL || Input == NULL || (InputSize % TDES_BLOCK_SIZE) != 0 || Output == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
 \r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
 \r
@@ -236,11 +243,11 @@ TdesEcbDecrypt (
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
-  If TdesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (8 bytes), then ASSERT().\r
-  If Ivec is NULL, then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If TdesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (8 bytes), then return FALSE.\r
+  If Ivec is NULL, then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be encrypted.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be encrypted.\r
@@ -265,11 +272,16 @@ TdesCbcEncrypt (
   DES_key_schedule  *KeySchedule;\r
   UINT8             IvecBuffer[TDES_BLOCK_SIZE];\r
 \r
   DES_key_schedule  *KeySchedule;\r
   UINT8             IvecBuffer[TDES_BLOCK_SIZE];\r
 \r
-  ASSERT (TdesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % TDES_BLOCK_SIZE) == 0);\r
-  ASSERT (Ivec != NULL);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (TdesContext == NULL || Input == NULL || (InputSize % TDES_BLOCK_SIZE) != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (Ivec == NULL || Output == NULL || InputSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
   CopyMem (IvecBuffer, Ivec, TDES_BLOCK_SIZE);\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
   CopyMem (IvecBuffer, Ivec, TDES_BLOCK_SIZE);\r
@@ -299,11 +311,11 @@ TdesCbcEncrypt (
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
   TdesContext should be already correctly initialized by TdesInit(). Behavior with\r
   invalid TDES context is undefined.\r
 \r
-  If TdesContext is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If InputSize is not multiple of block size (8 bytes), then ASSERT().\r
-  If Ivec is NULL, then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If TdesContext is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If InputSize is not multiple of block size (8 bytes), then return FALSE.\r
+  If Ivec is NULL, then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be encrypted.\r
 \r
   @param[in]   TdesContext  Pointer to the TDES context.\r
   @param[in]   Input        Pointer to the buffer containing the data to be encrypted.\r
@@ -328,11 +340,16 @@ TdesCbcDecrypt (
   DES_key_schedule  *KeySchedule;\r
   UINT8             IvecBuffer[TDES_BLOCK_SIZE];\r
 \r
   DES_key_schedule  *KeySchedule;\r
   UINT8             IvecBuffer[TDES_BLOCK_SIZE];\r
 \r
-  ASSERT (TdesContext != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT ((InputSize % TDES_BLOCK_SIZE) == 0);\r
-  ASSERT (Ivec != NULL);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (TdesContext == NULL || Input == NULL || (InputSize % TDES_BLOCK_SIZE) != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (Ivec == NULL || Output == NULL || InputSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
   CopyMem (IvecBuffer, Ivec, TDES_BLOCK_SIZE);\r
 \r
   KeySchedule = (DES_key_schedule *) TdesContext;\r
   CopyMem (IvecBuffer, Ivec, TDES_BLOCK_SIZE);\r