]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptRsaExt.c
CryptoPkg: Update PK Cipher Wrappers work with opaque objects.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptRsaExt.c
index 30552e4f4b3f48fadc378371d120c6661c438759..ca32b1ecc30002f0c06a8eb490f91feb1cbdaf05 100644 (file)
@@ -7,7 +7,7 @@
   3) RsaCheckKey\r
   4) RsaPkcs1Sign\r
 \r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2017, 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
@@ -74,6 +74,7 @@ RsaGetKey (
   RsaKey  = (RSA *) RsaContext;\r
   Size    = *BnSize;\r
   *BnSize = 0;\r
+  BnKey   = NULL;\r
 \r
   switch (KeyTag) {\r
 \r
@@ -81,86 +82,66 @@ RsaGetKey (
   // RSA Public Modulus (N)\r
   //\r
   case RsaKeyN:\r
-    if (RsaKey->n == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->n;\r
+    RSA_get0_key (RsaKey, (const BIGNUM **)&BnKey, NULL, NULL);\r
     break;\r
 \r
   //\r
   // RSA Public Exponent (e)\r
   //\r
   case RsaKeyE:\r
-    if (RsaKey->e == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->e;\r
+    RSA_get0_key (RsaKey, NULL, (const BIGNUM **)&BnKey, NULL);\r
     break;\r
 \r
   //\r
   // RSA Private Exponent (d)\r
   //\r
   case RsaKeyD:\r
-    if (RsaKey->d == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->d;\r
+    RSA_get0_key (RsaKey, NULL, NULL, (const BIGNUM **)&BnKey);\r
     break;\r
 \r
   //\r
   // RSA Secret Prime Factor of Modulus (p)\r
   //\r
   case RsaKeyP:\r
-    if (RsaKey->p == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->p;\r
+    RSA_get0_factors (RsaKey, (const BIGNUM **)&BnKey, NULL);\r
     break;\r
 \r
   //\r
   // RSA Secret Prime Factor of Modules (q)\r
   //\r
   case RsaKeyQ:\r
-    if (RsaKey->q == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->q;\r
+    RSA_get0_factors (RsaKey, NULL, (const BIGNUM **)&BnKey);\r
     break;\r
 \r
   //\r
   // p's CRT Exponent (== d mod (p - 1))\r
   //\r
   case RsaKeyDp:\r
-    if (RsaKey->dmp1 == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->dmp1;\r
+    RSA_get0_crt_params (RsaKey, (const BIGNUM **)&BnKey, NULL, NULL);\r
     break;\r
 \r
   //\r
   // q's CRT Exponent (== d mod (q - 1))\r
   //\r
   case RsaKeyDq:\r
-    if (RsaKey->dmq1 == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->dmq1;\r
+    RSA_get0_crt_params (RsaKey, NULL, (const BIGNUM **)&BnKey, NULL);\r
     break;\r
 \r
   //\r
   // The CRT Coefficient (== 1/q mod p)\r
   //\r
   case RsaKeyQInv:\r
-    if (RsaKey->iqmp == NULL) {\r
-      return TRUE;\r
-    }\r
-    BnKey = RsaKey->iqmp;\r
+    RSA_get0_crt_params (RsaKey, NULL, NULL, (const BIGNUM **)&BnKey);\r
     break;\r
 \r
   default:\r
     return FALSE;\r
   }\r
 \r
+  if (BnKey == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
   *BnSize = Size;\r
   Size    = BN_num_bytes (BnKey);\r
 \r
@@ -170,10 +151,11 @@ RsaGetKey (
   }\r
 \r
   if (BigNumber == NULL) {\r
-    return FALSE;\r
+    *BnSize = Size;\r
+    return TRUE;\r
   }\r
   *BnSize = BN_bn2bin (BnKey, BigNumber) ;\r
-  \r
+\r
   return TRUE;\r
 }\r
 \r
@@ -216,14 +198,14 @@ RsaGenerateKey (
   if (RsaContext == NULL || ModulusLength > INT_MAX || PublicExponentSize > INT_MAX) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   KeyE = BN_new ();\r
   if (KeyE == NULL) {\r
     return FALSE;\r
   }\r
 \r
   RetVal = FALSE;\r
-  \r
+\r
   if (PublicExponent == NULL) {\r
     if (BN_set_word (KeyE, 0x10001) == 0) {\r
       goto _Exit;\r
@@ -276,7 +258,7 @@ RsaCheckKey (
   if (RsaContext == NULL) {\r
     return FALSE;\r
   }\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
@@ -337,17 +319,17 @@ RsaPkcs1Sign (
   }\r
 \r
   Rsa = (RSA *) RsaContext;\r
-  Size = BN_num_bytes (Rsa->n);\r
+  Size = RSA_size (Rsa);\r
 \r
   if (*SigSize < Size) {\r
     *SigSize = Size;\r
     return FALSE;\r
   }\r
-  \r
+\r
   if (Signature == NULL) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   //\r
   // Determine the message digest algorithm according to digest size.\r
   //   Only MD5, SHA-1 or SHA-256 algorithm is supported. \r
@@ -356,18 +338,18 @@ RsaPkcs1Sign (
   case MD5_DIGEST_SIZE:\r
     DigestType = NID_md5;\r
     break;\r
-    \r
+\r
   case SHA1_DIGEST_SIZE:\r
     DigestType = NID_sha1;\r
     break;\r
-    \r
+\r
   case SHA256_DIGEST_SIZE:\r
     DigestType = NID_sha256;\r
     break;\r
 \r
   default:\r
     return FALSE;\r
-  }  \r
+  }\r
 \r
   return (BOOLEAN) RSA_sign (\r
                      DigestType,\r