def testExportKey14(self):
     # Export and re-import the encrypted key. It must match.
     # DER envelope, PKCS#8, PKCS#8 encryption
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     outkey = key.export_key('DER', 'test', pkcs=8)
     inkey = RSA.importKey(outkey, 'test')
     self.assertEqual(key.n, inkey.n)
     self.assertEqual(key.e, inkey.e)
     self.assertEqual(key.d, inkey.d)
Exemple #2
0
 def testDecrypt1(self):
     # Verify decryption using all test vectors
     for test in self._testData:
         # Build the key
         comps = [int(rws(test[0][x]), 16) for x in ('n', 'e', 'd')]
         key = RSA.construct(comps)
         # The real test
         cipher = PKCS.new(key, test[4])
         pt = cipher.decrypt(t2b(test[2]))
         self.assertEqual(pt, t2b(test[1]))
 def testExportKey12(self):
     # Export and re-import the encrypted key. It must match.
     # PEM envelope, PKCS#8, old PEM encryption
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     outkey = key.export_key('PEM', 'test', pkcs=8)
     self.assertTrue(tostr(outkey).find('4,ENCRYPTED') != -1)
     self.assertTrue(tostr(outkey).find('BEGIN PRIVATE KEY') != -1)
     inkey = RSA.importKey(outkey, 'test')
     self.assertEqual(key.n, inkey.n)
     self.assertEqual(key.e, inkey.e)
     self.assertEqual(key.d, inkey.d)
 def testExportKey13(self):
     # Export and re-import the encrypted key. It must match.
     # PEM envelope, PKCS#8, PKCS#8 encryption
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     outkey = key.export_key(
         'PEM',
         'test',
         pkcs=8,
         protection='PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC')
     self.assertTrue(tostr(outkey).find('4,ENCRYPTED') == -1)
     self.assertTrue(
         tostr(outkey).find('BEGIN ENCRYPTED PRIVATE KEY') != -1)
     inkey = RSA.importKey(outkey, 'test')
     self.assertEqual(key.n, inkey.n)
     self.assertEqual(key.e, inkey.e)
     self.assertEqual(key.d, inkey.d)
Exemple #5
0
    def testEncrypt1(self):
        # Verify encryption using all test vectors
        for test in self._testData:
            # Build the key
            comps = [int(rws(test[0][x]), 16) for x in ('n', 'e')]
            key = RSA.construct(comps)

            # RNG that takes its random numbers from a pool given
            # at initialization
            class randGen:
                def __init__(self, data):
                    self.data = data
                    self.idx = 0

                def __call__(self, N):
                    r = self.data[self.idx:N]
                    self.idx += N
                    return r

            # The real test
            cipher = PKCS.new(key, test[4], randfunc=randGen(t2b(test[3])))
            ct = cipher.encrypt(t2b(test[1]))
            self.assertEqual(ct, t2b(test[2]))
Exemple #6
0
                                 "Signature Verification 186-3",
                                 { 'shaalg' : lambda x: x,
                                   'd' : lambda x: int(x),
                                   'result' : lambda x: x })


for count, tv in enumerate(test_vectors_verify):
    if isinstance(tv, str):
        continue
    if hasattr(tv, "n"):
        modulus = tv.n
        continue

    hash_module = load_hash_by_name(tv.shaalg.upper())
    hash_obj = hash_module.new(tv.msg)
    public_key = RSA.construct([bytes_to_long(x) for x in (modulus, tv.e)])
    verifier = pkcs1_15.new(public_key)

    def positive_test(self, hash_obj=hash_obj, verifier=verifier, signature=tv.s):
        verifier.verify(hash_obj, signature)

    def negative_test(self, hash_obj=hash_obj, verifier=verifier, signature=tv.s):
        self.assertRaises(ValueError, verifier.verify, hash_obj, signature)

    if tv.result == 'f':
        setattr(FIPS_PKCS1_Verify_Tests_KAT, "test_negative_%d" % count, negative_test)
    else:
        setattr(FIPS_PKCS1_Verify_Tests_KAT, "test_positive_%d" % count, positive_test)


class FIPS_PKCS1_Sign_Tests(unittest.TestCase):
 def test_exportKey(self):
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     self.assertEqual(key.export_key(), key.exportKey())
 def testExportKey15(self):
     # Verify that that error an condition is detected when trying to
     # use a password with DER encoding and PKCS#1.
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     self.assertRaises(ValueError, key.export_key, 'DER', 'test', 1)
 def testExportKey9(self):
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     self.assertRaises(ValueError, key.export_key, "invalid-format")
 def testExportKey8(self):
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     pemKey = key.export_key("PEM", pkcs=8)
     self.assertEqual(pemKey, b(self.rsaKeyPEM8))
 def testExportKey7(self):
     key = RSA.construct(
         [self.n, self.e, self.d, self.p, self.q, self.pInv])
     derKey = key.export_key("DER", pkcs=8)
     self.assertEqual(derKey, self.rsaKeyDER8)
 def testExportKey5(self):
     key = RSA.construct([self.n, self.e])
     openssh_1 = key.export_key("OpenSSH").split()
     openssh_2 = self.rsaPublicKeyOpenSSH.split()
     self.assertEqual(openssh_1[0], openssh_2[0])
     self.assertEqual(openssh_1[1], openssh_2[1])
 def testExportKey4(self):
     key = RSA.construct([self.n, self.e])
     pemKey = key.export_key("PEM")
     self.assertEqual(pemKey, b(self.rsaPublicKeyPEM))
 def testExportKey2(self):
     key = RSA.construct([self.n, self.e])
     derKey = key.export_key("DER")
     self.assertEqual(derKey, self.rsaPublicKeyDER)