]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsa.c
1. Remove conducting ASSERT in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptRsa.c
index 0b561c35807eb732882ba8098b04aea504ebb62f..048335318a1a4460f6dd3f883e3549953a5d6b79 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
 \r
 /** @file\r
   RSA Asymmetric Cipher Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 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
@@ -60,7 +60,7 @@ RsaNew (
 /**\r
   Release the specified RSA context.\r
 \r
 /**\r
   Release the specified RSA context.\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
 \r
   @param[in]  RsaContext  Pointer to the RSA context to be released.\r
 \r
 \r
   @param[in]  RsaContext  Pointer to the RSA context to be released.\r
 \r
@@ -71,8 +71,6 @@ RsaFree (
   IN  VOID  *RsaContext\r
   )\r
 {\r
   IN  VOID  *RsaContext\r
   )\r
 {\r
-  ASSERT (RsaContext != NULL);\r
-\r
   //\r
   // Free OpenSSL RSA Context\r
   //\r
   //\r
   // Free OpenSSL RSA Context\r
   //\r
@@ -87,7 +85,7 @@ RsaFree (
   represented in RSA PKCS#1).\r
   If BigNumber is NULL, then the specified key componenet in RSA context is cleared.\r
 \r
   represented in RSA PKCS#1).\r
   If BigNumber is NULL, then the specified key componenet in RSA context is cleared.\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
 \r
   @param[in, out]  RsaContext  Pointer to RSA context being set.\r
   @param[in]       KeyTag      Tag of RSA key component being set.\r
 \r
   @param[in, out]  RsaContext  Pointer to RSA context being set.\r
   @param[in]       KeyTag      Tag of RSA key component being set.\r
@@ -113,10 +111,11 @@ RsaSetKey (
   RSA  *RsaKey;\r
 \r
   //\r
   RSA  *RsaKey;\r
 \r
   //\r
-  // ASSERT if RsaContext is NULL\r
+  // Check input parameters.\r
   //\r
   //\r
-  ASSERT (RsaContext != NULL);\r
-\r
+  if (RsaContext == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   RsaKey = (RSA *)RsaContext;\r
   //\r
 \r
   RsaKey = (RSA *)RsaContext;\r
   //\r
@@ -256,9 +255,9 @@ RsaSetKey (
   If the BigNumber buffer is too small to hold the contents of the key, FALSE\r
   is returned and BnSize is set to the required buffer size to obtain the key.\r
 \r
   If the BigNumber buffer is too small to hold the contents of the key, FALSE\r
   is returned and BnSize is set to the required buffer size to obtain the key.\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
-  If BnSize is NULL, then ASSERT().\r
-  If BnSize is large enough but BigNumber is NULL, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
+  If BnSize is NULL, then return FALSE.\r
+  If BnSize is large enough but BigNumber is NULL, then return FALSE.\r
 \r
   @param[in, out]  RsaContext  Pointer to RSA context being set.\r
   @param[in]       KeyTag      Tag of RSA key component being set.\r
 \r
   @param[in, out]  RsaContext  Pointer to RSA context being set.\r
   @param[in]       KeyTag      Tag of RSA key component being set.\r
@@ -284,8 +283,12 @@ RsaGetKey (
   BIGNUM *BnKey;\r
   UINTN  Size;\r
 \r
   BIGNUM *BnKey;\r
   UINTN  Size;\r
 \r
-  ASSERT (RsaContext != NULL);\r
-  ASSERT (BnSize != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (RsaContext == NULL || BnSize == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   RsaKey  = (RSA *) RsaContext;\r
   Size    = *BnSize;\r
 \r
   RsaKey  = (RSA *) RsaContext;\r
   Size    = *BnSize;\r
@@ -385,7 +388,9 @@ RsaGetKey (
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
-  ASSERT (BigNumber != NULL);\r
+  if (BigNumber == NULL) {\r
+    return FALSE;\r
+  }\r
   *BnSize = BN_bn2bin (BnKey, BigNumber) ;\r
   \r
   return TRUE;\r
   *BnSize = BN_bn2bin (BnKey, BigNumber) ;\r
   \r
   return TRUE;\r
@@ -401,7 +406,7 @@ RsaGetKey (
   Before this function can be invoked, pseudorandom number generator must be correctly\r
   initialized by RandomSeed().\r
 \r
   Before this function can be invoked, pseudorandom number generator must be correctly\r
   initialized by RandomSeed().\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
 \r
   @param[in, out]  RsaContext           Pointer to RSA context being set.\r
   @param[in]       ModulusLength        Length of RSA modulus N in bits.\r
 \r
   @param[in, out]  RsaContext           Pointer to RSA context being set.\r
   @param[in]       ModulusLength        Length of RSA modulus N in bits.\r
@@ -424,8 +429,13 @@ RsaGenerateKey (
   BIGNUM   *KeyE;\r
   BOOLEAN  RetVal;\r
 \r
   BIGNUM   *KeyE;\r
   BOOLEAN  RetVal;\r
 \r
-  ASSERT (RsaContext != NULL);\r
-\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (RsaContext == NULL) {\r
+    return FALSE;\r
+  }\r
+  \r
   KeyE = BN_new ();\r
   if (PublicExponent == NULL) {\r
     BN_set_word (KeyE, 0x10001);\r
   KeyE = BN_new ();\r
   if (PublicExponent == NULL) {\r
     BN_set_word (KeyE, 0x10001);\r
@@ -451,7 +461,7 @@ RsaGenerateKey (
   - Whether n = p * q\r
   - Whether d*e = 1  mod lcm(p-1,q-1)\r
 \r
   - Whether n = p * q\r
   - Whether d*e = 1  mod lcm(p-1,q-1)\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
 \r
   @param[in]  RsaContext  Pointer to RSA context to check.\r
 \r
 \r
   @param[in]  RsaContext  Pointer to RSA context to check.\r
 \r
@@ -467,8 +477,13 @@ RsaCheckKey (
 {\r
   UINTN  Reason;\r
 \r
 {\r
   UINTN  Reason;\r
 \r
-  ASSERT (RsaContext != NULL);\r
-\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (RsaContext == NULL) {\r
+    return FALSE;\r
+  }\r
+  \r
   if  (RSA_check_key ((RSA *) RsaContext) != 1) {\r
     Reason = ERR_GET_REASON (ERR_peek_last_error ());\r
     if (Reason == RSA_R_P_NOT_PRIME ||\r
   if  (RSA_check_key ((RSA *) RsaContext) != 1) {\r
     Reason = ERR_GET_REASON (ERR_peek_last_error ());\r
     if (Reason == RSA_R_P_NOT_PRIME ||\r
@@ -502,8 +517,12 @@ DigestInfoEncoding (
   CONST UINT8  *HashDer;\r
   UINTN        DerSize;\r
 \r
   CONST UINT8  *HashDer;\r
   UINTN        DerSize;\r
 \r
-  ASSERT (Message != NULL);\r
-  ASSERT (DigestInfo != NULL);\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (Message == NULL || DigestInfo == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   //\r
   // The original message length is used to determine the hash algorithm since\r
 \r
   //\r
   // The original message length is used to determine the hash algorithm since\r
@@ -543,10 +562,10 @@ DigestInfoEncoding (
   If the Signature buffer is too small to hold the contents of signature, FALSE\r
   is returned and SigSize is set to the required buffer size to obtain the signature.\r
 \r
   If the Signature buffer is too small to hold the contents of signature, FALSE\r
   is returned and SigSize is set to the required buffer size to obtain the signature.\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
-  If MessageHash is NULL, then ASSERT().\r
-  If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then ASSERT().\r
-  If SigSize is large enough but Signature is NULL, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
+  If MessageHash is NULL, then return FALSE.\r
+  If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
+  If SigSize is large enough but Signature is NULL, then return FALSE.\r
 \r
   @param[in]       RsaContext   Pointer to RSA context for signature generation.\r
   @param[in]       MessageHash  Pointer to octet message hash to be signed.\r
 \r
   @param[in]       RsaContext   Pointer to RSA context for signature generation.\r
   @param[in]       MessageHash  Pointer to octet message hash to be signed.\r
@@ -574,11 +593,13 @@ RsaPkcs1Sign (
   UINTN    Size;\r
   INTN     ReturnVal;\r
 \r
   UINTN    Size;\r
   INTN     ReturnVal;\r
 \r
-  ASSERT (RsaContext != NULL);\r
-  ASSERT (MessageHash != NULL);\r
-  ASSERT ((HashSize == MD5_DIGEST_SIZE) ||\r
-          (HashSize == SHA1_DIGEST_SIZE) ||\r
-          (HashSize == SHA256_DIGEST_SIZE));\r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (RsaContext == NULL || MessageHash == NULL ||\r
+    (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE)) {\r
+    return FALSE;\r
+  }\r
 \r
   Rsa = (RSA *) RsaContext;\r
   Size = BN_num_bytes (Rsa->n);\r
 \r
   Rsa = (RSA *) RsaContext;\r
   Size = BN_num_bytes (Rsa->n);\r
@@ -588,7 +609,9 @@ RsaPkcs1Sign (
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
-  ASSERT (Signature != NULL);\r
+  if (Signature == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
   Size = DigestInfoEncoding (MessageHash, HashSize, Signature);\r
 \r
 \r
   Size = DigestInfoEncoding (MessageHash, HashSize, Signature);\r
 \r
@@ -612,10 +635,10 @@ RsaPkcs1Sign (
   Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
   RSA PKCS#1.\r
 \r
   Verifies the RSA-SSA signature with EMSA-PKCS1-v1_5 encoding scheme defined in\r
   RSA PKCS#1.\r
 \r
-  If RsaContext is NULL, then ASSERT().\r
-  If MessageHash is NULL, then ASSERT().\r
-  If Signature is NULL, then ASSERT().\r
-  If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then ASSERT().\r
+  If RsaContext is NULL, then return FALSE.\r
+  If MessageHash is NULL, then return FALSE.\r
+  If Signature is NULL, then return FALSE.\r
+  If HashSize is not equal to the size of MD5, SHA-1 or SHA-256 digest, then return FALSE.\r
 \r
   @param[in]  RsaContext   Pointer to RSA context for signature verification.\r
   @param[in]  MessageHash  Pointer to octet message hash to be checked.\r
 \r
   @param[in]  RsaContext   Pointer to RSA context for signature verification.\r
   @param[in]  MessageHash  Pointer to octet message hash to be checked.\r
@@ -640,19 +663,21 @@ RsaPkcs1Verify (
   INTN     Length;\r
 \r
   //\r
   INTN     Length;\r
 \r
   //\r
-  // ASSERT if RsaContext, MessageHash or Signature is NULL\r
+  // Check input parameters.\r
   //\r
   //\r
-  ASSERT (RsaContext  != NULL);\r
-  ASSERT (MessageHash != NULL);\r
-  ASSERT (Signature   != NULL);\r
+  if (RsaContext == NULL || MessageHash == NULL || Signature == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
 \r
+  \r
   //\r
   //\r
-  // ASSERT if unsupported hash size:\r
+  // Check for unsupported hash size:\r
   //    Only MD5, SHA-1 or SHA-256 digest size is supported\r
   //\r
   //    Only MD5, SHA-1 or SHA-256 digest size is supported\r
   //\r
-  ASSERT ((HashSize == MD5_DIGEST_SIZE) || (HashSize == SHA1_DIGEST_SIZE) ||\r
-          (HashSize == SHA256_DIGEST_SIZE));\r
-\r
+  if (HashSize != MD5_DIGEST_SIZE && HashSize != SHA1_DIGEST_SIZE && HashSize != SHA256_DIGEST_SIZE) {\r
+    return FALSE;\r
+  }\r
+  \r
   //\r
   // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key\r
   //\r
   //\r
   // RSA PKCS#1 Signature Decoding using OpenSSL RSA Decryption with Public Key\r
   //\r