]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Cipher/CryptArc4.c
Fix several issues in BaseCryptLib:
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Cipher / CryptArc4.c
index fa8fd963dddb8172e549605f405d6e39e335d41b..f3c4d31a2d9c1598fd7f295fc187f28fe2bf57ed 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   ARC4 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
@@ -32,19 +32,19 @@ Arc4GetContextSize (
   // for backup copy. When Arc4Reset() is called, we can use the backup copy to restore\r
   // the working copy to the initial state.\r
   //\r
-  return (UINTN) (2 * sizeof(RC4_KEY));\r
+  return (UINTN) (2 * sizeof (RC4_KEY));\r
 }\r
 \r
 /**\r
   Initializes user-supplied memory as ARC4 context for subsequent use.\r
 \r
   This function initializes user-supplied memory pointed by Arc4Context as ARC4 context.\r
-  In addtion, it sets up all ARC4 key materials for subsequent encryption and decryption\r
+  In addition, it sets up all ARC4 key materials for subsequent encryption and decryption\r
   operations.\r
 \r
-  If Arc4Context is NULL, then ASSERT().\r
-  If Key is NULL, then ASSERT().\r
-  If KeySize does not in the range of [5, 256] bytes, then ASSERT().\r
+  If Arc4Context is NULL, then return FALSE.\r
+  If Key is NULL, then return FALSE.\r
+  If KeySize does not in the range of [5, 256] bytes, then return FALSE.\r
 \r
   @param[out]  Arc4Context  Pointer to ARC4 context being initialized.\r
   @param[in]   Key          Pointer to the user-supplied ARC4 key.\r
@@ -64,15 +64,18 @@ Arc4Init (
 {\r
   RC4_KEY  *Rc4Key;\r
 \r
-  ASSERT (Arc4Context != NULL);\r
-  ASSERT (Key != NULL);\r
-  ASSERT ((KeySize >= 5) && (KeySize <= 256));\r
+  //\r
+  // Check input parameters.\r
+  //  \r
+  if (Arc4Context == NULL || Key == NULL || (KeySize < 5 || KeySize > 256)) {\r
+    return FALSE;\r
+  }\r
 \r
   Rc4Key = (RC4_KEY *) Arc4Context;\r
 \r
   RC4_set_key (Rc4Key, (UINT32) KeySize, Key);\r
 \r
-  CopyMem (Rc4Key +  1, Rc4Key, sizeof(RC4_KEY));\r
+  CopyMem (Rc4Key +  1, Rc4Key, sizeof (RC4_KEY));\r
 \r
   return TRUE;\r
 }\r
@@ -85,9 +88,9 @@ Arc4Init (
   Arc4Context should be already correctly initialized by Arc4Init(). Behavior with\r
   invalid ARC4 context is undefined.\r
 \r
-  If Arc4Context is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If Arc4Context is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in, out]  Arc4Context  Pointer to the ARC4 context.\r
   @param[in]       Input        Pointer to the buffer containing the data to be encrypted.\r
@@ -109,9 +112,12 @@ Arc4Encrypt (
 {\r
   RC4_KEY  *Rc4Key;\r
 \r
-  ASSERT (Arc4Context != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Arc4Context == NULL || Input == NULL || Output == NULL || InputSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   Rc4Key = (RC4_KEY *) Arc4Context;\r
 \r
@@ -128,9 +134,9 @@ Arc4Encrypt (
   Arc4Context should be already correctly initialized by Arc4Init(). Behavior with\r
   invalid ARC4 context is undefined.\r
 \r
-  If Arc4Context is NULL, then ASSERT().\r
-  If Input is NULL, then ASSERT().\r
-  If Output is NULL, then ASSERT().\r
+  If Arc4Context is NULL, then return FALSE.\r
+  If Input is NULL, then return FALSE.\r
+  If Output is NULL, then return FALSE.\r
 \r
   @param[in, out]  Arc4Context  Pointer to the ARC4 context.\r
   @param[in]       Input        Pointer to the buffer containing the data to be decrypted.\r
@@ -152,9 +158,12 @@ Arc4Decrypt (
 {\r
   RC4_KEY  *Rc4Key;\r
 \r
-  ASSERT (Arc4Context != NULL);\r
-  ASSERT (Input != NULL);\r
-  ASSERT (Output != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Arc4Context == NULL || Input == NULL || Output == NULL || InputSize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
 \r
   Rc4Key = (RC4_KEY *) Arc4Context;\r
 \r
@@ -171,7 +180,7 @@ Arc4Decrypt (
   Contrary to ARC4Init(), Arc4Reset() requires no secret key as input, but ARC4 context\r
   should be already correctly initialized by ARC4Init().\r
 \r
-  If Arc4Context is NULL, then ASSERT().\r
+  If Arc4Context is NULL, then return FALSE.\r
 \r
   @param[in, out]  Arc4Context  Pointer to the ARC4 context.\r
 \r
@@ -187,11 +196,16 @@ Arc4Reset (
 {\r
   RC4_KEY  *Rc4Key;\r
 \r
-  ASSERT (Arc4Context != NULL);\r
-\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Arc4Context == NULL) {\r
+    return FALSE;\r
+  }\r
+  \r
   Rc4Key = (RC4_KEY *) Arc4Context;\r
 \r
-  CopyMem (Rc4Key, Rc4Key + 1, sizeof(RC4_KEY));\r
+  CopyMem (Rc4Key, Rc4Key + 1, sizeof (RC4_KEY));\r
 \r
   return TRUE;\r
 }\r