def _check_decryption(self, rsaObj):
        plaintext = bytes_to_long(a2b_hex(self.plaintext))
        ciphertext = bytes_to_long(a2b_hex(self.ciphertext))

        # Test plain decryption
        new_plaintext = rsaObj._decrypt(ciphertext)
        self.assertEqual(plaintext, new_plaintext)
Esempio n. 2
0
 def _test_signing(self, dsaObj):
     k = bytes_to_long(a2b_hex(self.k))
     m_hash = bytes_to_long(a2b_hex(self.m_hash))
     r = bytes_to_long(a2b_hex(self.r))
     s = bytes_to_long(a2b_hex(self.s))
     (r_out, s_out) = dsaObj._sign(m_hash, k)
     self.assertEqual((r, s), (r_out, s_out))
    def _check_encryption(self, rsaObj):
        plaintext = a2b_hex(self.plaintext)
        ciphertext = a2b_hex(self.ciphertext)

        # Test encryption
        new_ciphertext2 = rsaObj._encrypt(bytes_to_long(plaintext))
        self.assertEqual(bytes_to_long(ciphertext), new_ciphertext2)
Esempio n. 4
0
    def _check_public_key(self, dsaObj):
        k = bytes_to_long(a2b_hex(self.k))
        m_hash = bytes_to_long(a2b_hex(self.m_hash))

        # Check capabilities
        self.assertEqual(0, dsaObj.has_private())
        self.assertEqual(1, dsaObj.can_sign())
        self.assertEqual(0, dsaObj.can_encrypt())

        # Check that private parameters are all missing
        self.assertEqual(0, hasattr(dsaObj, 'x'))

        # Sanity check key data
        self.assertEqual(1, dsaObj.p > dsaObj.q)  # p > q
        self.assertEqual(160, size(dsaObj.q))  # size(q) == 160 bits
        self.assertEqual(0, (dsaObj.p - 1) % dsaObj.q)  # q is a divisor of p-1

        # Public-only key objects should raise an error when .sign() is called
        self.assertRaises(TypeError, dsaObj._sign, m_hash, k)

        # Check __eq__ and __ne__
        self.assertEqual(dsaObj.publickey() == dsaObj.publickey(),
                         True)  # assert_
        self.assertEqual(dsaObj.publickey() != dsaObj.publickey(),
                         False)  # failIf
    def setUp(self):
        global RSA, Random, bytes_to_long
        from crypto.PublicKey import RSA
        from crypto import Random
        from crypto.Util.number import bytes_to_long, inverse
        self.n = bytes_to_long(a2b_hex(self.modulus))
        self.p = bytes_to_long(a2b_hex(self.prime_factor))

        # Compute q, d, and u from n, e, and p
        self.q = self.n // self.p
        self.d = inverse(self.e, (self.p-1)*(self.q-1))
        self.u = inverse(self.p, self.q)    # u = e**-1 (mod q)

        self.rsa = RSA
Esempio n. 6
0
def t2b(t):
    """Convert a text string with bytes in hex form to a byte string"""
    clean = b(rws(t))
    if len(clean) % 2 == 1:
        print(clean)
        raise ValueError("Even number of characters expected")
    return a2b_hex(clean)
    def _check_public_key(self, rsaObj):
        ciphertext = a2b_hex(self.ciphertext)

        # Check capabilities
        self.assertEqual(0, rsaObj.has_private())

        # Check rsaObj.[ne] -> rsaObj.[ne] mapping
        self.assertEqual(rsaObj.n, rsaObj.n)
        self.assertEqual(rsaObj.e, rsaObj.e)

        # Check that private parameters are all missing
        self.assertEqual(0, hasattr(rsaObj, 'd'))
        self.assertEqual(0, hasattr(rsaObj, 'p'))
        self.assertEqual(0, hasattr(rsaObj, 'q'))
        self.assertEqual(0, hasattr(rsaObj, 'u'))

        # Sanity check key data
        self.assertEqual(1, rsaObj.e > 1)   # e > 1

        # Public keys should not be able to sign or decrypt
        self.assertRaises(TypeError, rsaObj._decrypt,
                bytes_to_long(ciphertext))

        # Check __eq__ and __ne__
        self.assertEqual(rsaObj.publickey() == rsaObj.publickey(),True) # assert_
        self.assertEqual(rsaObj.publickey() != rsaObj.publickey(),False) # failIf
Esempio n. 8
0
 def test_construct_4tuple(self):
     """DSA (default implementation) constructed key (4-tuple)"""
     (y, g, p, q) = [
         bytes_to_long(a2b_hex(param))
         for param in (self.y, self.g, self.p, self.q)
     ]
     dsaObj = self.dsa.construct((y, g, p, q))
     self._test_verification(dsaObj)
Esempio n. 9
0
    def test_construct_bad_key5(self):
        (y, g, p, q, x) = [
            bytes_to_long(a2b_hex(param))
            for param in (self.y, self.g, self.p, self.q, self.x)
        ]
        tup = (y, g, p, q, x + 1)
        self.assertRaises(ValueError, self.dsa.construct, tup)

        tup = (y, g, p, q, q + 10)
        self.assertRaises(ValueError, self.dsa.construct, tup)
    def _exercise_primitive(self, rsaObj):
        # Since we're using a randomly-generated key, we can't check the test
        # vector, but we can make sure encryption and decryption are inverse
        # operations.
        ciphertext = bytes_to_long(a2b_hex(self.ciphertext))

        # Test decryption
        plaintext = rsaObj._decrypt(ciphertext)

        # Test encryption (2 arguments)
        new_ciphertext2 = rsaObj._encrypt(plaintext)
        self.assertEqual(ciphertext, new_ciphertext2)
Esempio n. 11
0
 def convert_tv(self, tv, as_longs=0):
     """Convert a test vector from textual form (hexadecimal ascii
     to either integers or byte strings."""
     key_comps = 'p', 'g', 'y', 'x'
     tv2 = {}
     for c in list(tv.keys()):
         tv2[c] = a2b_hex(tv[c])
         if as_longs or c in key_comps or c in ('sig1', 'sig2'):
             tv2[c] = bytes_to_long(tv2[c])
     tv2['key'] = []
     for c in key_comps:
         tv2['key'] += [tv2[c]]
         del tv2[c]
     return tv2
    def _exercise_public_primitive(self, rsaObj):
        plaintext = a2b_hex(self.plaintext)

        # Test encryption (2 arguments)
        new_ciphertext2 = rsaObj._encrypt(bytes_to_long(plaintext))
Esempio n. 13
0
 def _test_verification(self, dsaObj):
     m_hash = bytes_to_long(a2b_hex(self.m_hash))
     r = bytes_to_long(a2b_hex(self.r))
     s = bytes_to_long(a2b_hex(self.s))
     self.assertTrue(dsaObj._verify(m_hash, (r, s)))
     self.assertFalse(dsaObj._verify(m_hash + 1, (r, s)))