diff options
author | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2012-05-28 08:13:54 -0400 |
---|---|---|
committer | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2012-05-28 08:13:54 -0400 |
commit | 125a4f93fa9c798520d08fce9f04c08e3566d98d (patch) | |
tree | 76439b70ff3ede78d56beb06be1e4254ef5bbbb6 | |
parent | 9ebdaf29b74a47aaad3547a49bda58b94b46aacb (diff) | |
parent | 0c2625df74782b827ff569c422cf303734db6639 (diff) | |
download | pycrypto-125a4f93fa9c798520d08fce9f04c08e3566d98d.tar.gz |
Merge remote-tracking branch 'sebastinas/reenable-tests'
-rw-r--r-- | lib/Crypto/PublicKey/RSA.py | 8 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/Cipher/test_pkcs1_oaep.py | 11 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/PublicKey/test_importKey.py | 12 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/Util/test_asn1.py | 2 |
4 files changed, 19 insertions, 14 deletions
diff --git a/lib/Crypto/PublicKey/RSA.py b/lib/Crypto/PublicKey/RSA.py index 27c23be..bab9288 100644 --- a/lib/Crypto/PublicKey/RSA.py +++ b/lib/Crypto/PublicKey/RSA.py @@ -348,9 +348,9 @@ class _RSAobj(pubkey.pubkey): nb = long_to_bytes(self.n) if bord(eb[0]) & 0x80: eb=bchr(0x00)+eb if bord(nb[0]) & 0x80: nb=bchr(0x00)+nb - keyparts = [ 'ssh-rsa', eb, nb ] - keystring = ''.join([ struct.pack(">I",len(kp))+kp for kp in keyparts]) - return 'ssh-rsa '+binascii.b2a_base64(keystring)[:-1] + keyparts = [ b('ssh-rsa'), eb, nb ] + keystring = b('').join([ struct.pack(">I",len(kp))+kp for kp in keyparts]) + return b('ssh-rsa ')+binascii.b2a_base64(keystring)[:-1] # DER format is always used, even in case of PEM, which simply # encodes it into BASE64. @@ -400,7 +400,7 @@ class _RSAobj(pubkey.pubkey): pem += b('').join(chunks) pem += b("-----END " + keyType + " KEY-----") return pem - return ValueError("Unknown key format '%s'. Cannot export the RSA key." % format) + raise ValueError("Unknown key format '%s'. Cannot export the RSA key." % format) class RSAImplementation(object): """ diff --git a/lib/Crypto/SelfTest/Cipher/test_pkcs1_oaep.py b/lib/Crypto/SelfTest/Cipher/test_pkcs1_oaep.py index accca61..7ca5c15 100644 --- a/lib/Crypto/SelfTest/Cipher/test_pkcs1_oaep.py +++ b/lib/Crypto/SelfTest/Cipher/test_pkcs1_oaep.py @@ -314,11 +314,12 @@ class PKCS1_OAEP_Tests(unittest.TestCase): # Encrypt/Decrypt messages of length [0..128-2*20-2] for pt_len in xrange(0,128-2*20-2): pt = self.rng(pt_len) - ct = PKCS.encrypt(pt, self.key1024) - pt2 = PKCS.decrypt(ct, self.key1024) + cipher = PKCS.new(self.key1024) + ct = cipher.encrypt(pt) + pt2 = cipher.decrypt(ct) self.assertEqual(pt,pt2) - def testEncryptDecrypt1(self): + def testEncryptDecrypt2(self): # Helper function to monitor what's requested from RNG global asked def localRng(N): @@ -337,7 +338,7 @@ class PKCS1_OAEP_Tests(unittest.TestCase): self.assertEqual(cipher.decrypt(ct), pt) self.failUnless(asked > hashmod.digest_size) - def testEncryptDecrypt2(self): + def testEncryptDecrypt3(self): # Verify that OAEP supports labels pt = self.rng(35) xlabel = self.rng(22) @@ -345,7 +346,7 @@ class PKCS1_OAEP_Tests(unittest.TestCase): ct = cipher.encrypt(pt) self.assertEqual(cipher.decrypt(ct), pt) - def testEncryptDecrypt3(self): + def testEncryptDecrypt4(self): # Verify that encrypt() uses the custom MGF global mgfcalls # Helper function to monitor what's requested from MGF diff --git a/lib/Crypto/SelfTest/PublicKey/test_importKey.py b/lib/Crypto/SelfTest/PublicKey/test_importKey.py index 28a7eee..4710440 100644 --- a/lib/Crypto/SelfTest/PublicKey/test_importKey.py +++ b/lib/Crypto/SelfTest/PublicKey/test_importKey.py @@ -104,7 +104,7 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ== -----END PUBLIC KEY-----''' # Obtained using 'ssh-keygen -i -m PKCS8 -f rsaPublicKeyPEM' - rsaPublicKeyOpenSSH = '''ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQC/HieQCqCLI1EaXBKBrm2TMSw+/pE/ky6+1JLxLRa0YQwyjLbiCKtfRay+KVCDMpjzEiwZ94SS3t9A8OPBkDOF comment\n''' + rsaPublicKeyOpenSSH = b('''ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQC/HieQCqCLI1EaXBKBrm2TMSw+/pE/ky6+1JLxLRa0YQwyjLbiCKtfRay+KVCDMpjzEiwZ94SS3t9A8OPBkDOF comment\n''') # The private key, in PKCS#1 format encoded with DER rsaKeyDER = a2b_hex( @@ -298,7 +298,7 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ== self.assertEqual(openssh_1[0], openssh_2[0]) self.assertEqual(openssh_1[1], openssh_2[1]) - def testExportKey4(self): + def testExportKey6(self): key = self.rsa.construct([self.n, self.e, self.d, self.p, self.q, self.pInv]) # Tuple with index #1 is encrypted with 3DES t = map(b,self.rsaKeyEncryptedPEM[1]) @@ -307,16 +307,20 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ== pemKey = key.exportKey("PEM", t[0]) self.assertEqual(pemKey, t[1]) - def testExportKey5(self): + def testExportKey7(self): key = self.rsa.construct([self.n, self.e, self.d, self.p, self.q, self.pInv]) derKey = key.exportKey("DER", pkcs=8) self.assertEqual(derKey, self.rsaKeyDER8) - def testExportKey6(self): + def testExportKey8(self): key = self.rsa.construct([self.n, self.e, self.d, self.p, self.q, self.pInv]) pemKey = key.exportKey("PEM", pkcs=8) self.assertEqual(pemKey, b(self.rsaKeyPEM8)) + def testExportKey9(self): + key = self.rsa.construct([self.n, self.e, self.d, self.p, self.q, self.pInv]) + self.assertRaises(ValueError, key.exportKey, "invalid-format") + class ImportKeyTestsSlow(ImportKeyTests): def setUp(self): self.rsa = RSA.RSAImplementation(use_fast_math=0) diff --git a/lib/Crypto/SelfTest/Util/test_asn1.py b/lib/Crypto/SelfTest/Util/test_asn1.py index 578dabe..bbd0a39 100644 --- a/lib/Crypto/SelfTest/Util/test_asn1.py +++ b/lib/Crypto/SelfTest/Util/test_asn1.py @@ -156,7 +156,7 @@ class DerSequenceTests(unittest.TestCase): self.assertEqual(der[1:-1],[9]) self.assertEquals(der.encode(), b('0\x0A\x02\x02\x01\x80\x02\x01\x09\x02\x01\x08')) - def testEncode6(self): + def testEncode7(self): # One integer and another type (no matter what it is) der = DerSequence() der.append(0x180L) |