summaryrefslogtreecommitdiff
path: root/lib/Crypto
diff options
context:
space:
mode:
authorLegrandin <gooksankoo@hoiptorrow.mailexpire.com>2011-01-16 21:47:55 +0100
committerDwayne C. Litzenberger <dlitz@dlitz.net>2011-10-17 22:32:51 -0400
commitf01bc4a629768b68d10a94791c609dc03593d2b1 (patch)
tree69ae6c3a784512b261968381a26f3c958bdfce00 /lib/Crypto
parent61f296be7d9d5f0b20be189e2cc69d6b2f9b2e69 (diff)
downloadpycrypto-f01bc4a629768b68d10a94791c609dc03593d2b1.tar.gz
Add test cases to prove that an imported RSA private key really behaves like one (BUG 702835).
Conflicts: lib/Crypto/SelfTest/PublicKey/test_importKey.py
Diffstat (limited to 'lib/Crypto')
-rw-r--r--lib/Crypto/SelfTest/PublicKey/test_importKey.py25
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/Crypto/SelfTest/PublicKey/test_importKey.py b/lib/Crypto/SelfTest/PublicKey/test_importKey.py
index 791e101..f11fbc1 100644
--- a/lib/Crypto/SelfTest/PublicKey/test_importKey.py
+++ b/lib/Crypto/SelfTest/PublicKey/test_importKey.py
@@ -28,6 +28,7 @@ from Crypto.PublicKey import RSA
from Crypto.SelfTest.st_common import *
from Crypto.SelfTest.st_common import list_test_cases, a2b_hex, b2a_hex
from Crypto.Util.py3compat import *
+from Crypto.Util.number import inverse
class ImportKeyTests(unittest.TestCase):
@@ -73,7 +74,11 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
d = long('09 44 83 12 9F 11 4D ED F6 7E DA BC 23 01 BC 5A 88 E5 E6 60 1D D7 01 62 20 EA D9 FD 4B FC 6F DE B7 58 93 89 8A E4 1C 54 DD BD BF 15 39 F8 CC BD 18 F6 7B 44 0D E1 AC 30 44 02 81 D4 0C FA C8 39'.replace(" ",""),16)
p = long('00 F2 0F 2F 3E 1D A6 18 83 F6 29 80 92 2B D8 DF 54 5C E4 07 C7 26 24 11 03 B5 E2 C5 37 23 12 4A 23'.replace(" ",""),16)
q = long('00 CA 1F E9 24 79 2C FC C9 6B FA B7 4F 34 4A 68 B4 18 DF 57 83 38 06 48 06 00 0F E2 A5 C9 9A 02 37'.replace(" ",""),16)
- coeff = long('00 BD 9F 40 A7 64 22 7A 21 96 2A 4A DD 07 E4 DE FE 43 ED 91 A3 AE 27 BB 05 7F 39 24 1F 33 AB 01 C1'.replace(" ",""),16)
+
+ # This is q^{-1} mod p). fastmath and slowmath use pInv (p^{-1}
+ # mod q) instead!
+ qInv = long('00 BD 9F 40 A7 64 22 7A 21 96 2A 4A DD 07 E4 DE FE 43 ED 91 A3 AE 27 BB 05 7F 39 24 1F 33 AB 01 C1'.replace(" ",""),16)
+ pInv = inverse(p,q)
def testImportKey1(self):
key = RSA.importKey(self.rsaKeyDER)
@@ -83,7 +88,6 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
self.assertEqual(key.d, self.d)
self.assertEqual(key.p, self.p)
self.assertEqual(key.q, self.q)
- self.assertEqual(key.u, self.coeff)
def testImportKey2(self):
key = RSA.importKey(self.rsaPublicKeyDER)
@@ -99,7 +103,6 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
self.assertEqual(key.d, self.d)
self.assertEqual(key.p, self.p)
self.assertEqual(key.q, self.q)
- self.assertEqual(key.u, self.coeff)
def testImportKey4(self):
key = RSA.importKey(b(self.rsaPublicKeyPEM))
@@ -107,9 +110,21 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
+ def testImportKey5(self):
+ """Verifies that the imported key is still a valid RSA pair"""
+ key = RSA.importKey(self.rsaKeyPEM)
+ idem = key.encrypt(key.decrypt("Test"),0)
+ self.assertEqual(idem[0],"Test")
+
+ def testImportKey6(self):
+ """Verifies that the imported key is still a valid RSA pair"""
+ key = RSA.importKey(self.rsaKeyDER)
+ idem = key.encrypt(key.decrypt("Test"),0)
+ self.assertEqual(idem[0],"Test")
+
###
def testExportKey1(self):
- key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.coeff])
+ key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
derKey = key.exportKey("DER")
self.assertEqual(derKey, self.rsaKeyDER)
@@ -119,7 +134,7 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
self.assertEqual(derKey, self.rsaPublicKeyDER)
def testExportKey3(self):
- key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.coeff])
+ key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
pemKey = key.exportKey("PEM")
self.assertEqual(pemKey, b(self.rsaKeyPEM))