class TestSerialization(unittest.TestCase):
    """
    Test whether keys can be serialized and unserialized correctly.
    """
    def setUp(self):
        self.ec = ECCrypto()
        self.key = self.ec.generate_key(u"very-low")
        self.key_nacl = self.ec.generate_key(u"curve25519")

    def test_private_to_bin(self):
        """
        Check if M2Crypto derived key bins are valid.
        """
        private_bin = self.key.key_to_bin()

        self.assertTrue(self.ec.is_valid_private_bin(private_bin))

    def test_private_nacl_to_bin(self):
        """
        Check if libnacl derived key bins are valid.
        """
        private_bin = self.key_nacl.key_to_bin()

        self.assertTrue(self.ec.is_valid_private_bin(private_bin))

    def test_private_to_pem(self):
        """
        Check if keys can be serialized and loaded correctly in PEM format.
        """
        private_pem = self.key.key_to_pem()

        # Convert the PEM to a DER keystring
        prefix = "-----BEGIN EC PRIVATE KEY-----\n"
        postfix = "-----END EC PRIVATE KEY-----\n"
        keystring = private_pem[len(prefix):-len(postfix)].decode("BASE64")

        # Reconstruct a key with this keystring
        key = M2CryptoSK(keystring=keystring)

        self.assertEqual(private_pem, key.key_to_pem())

    def test_public_to_bin(self):
        """
        Check if M2Crypto derived public key bins are valid.
        """
        public_bin = self.key.pub().key_to_bin()

        self.assertTrue(self.ec.is_valid_public_bin(public_bin))

    def test_public_nacl_to_bin(self):
        """
        Check if libnacl derived public key bins are valid.
        """
        public_bin = self.key_nacl.pub().key_to_bin()

        self.assertTrue(self.ec.is_valid_public_bin(public_bin))

    def test_public_to_pem(self):
        """
        Check if public keys can be serialized and loaded correctly in PEM format.
        """
        public_pem = self.key.pub().key_to_pem()

        # Convert the PEM to a DER keystring
        prefix = "-----BEGIN PUBLIC KEY-----\n"
        postfix = "-----END PUBLIC KEY-----\n"
        keystring = public_pem[len(prefix):-len(postfix)].decode("BASE64")

        # Reconstruct a key with this keystring
        key = M2CryptoPK(keystring=keystring)

        self.assertEqual(public_pem, key.key_to_pem())
Example #2
0
class TestECCrypto(unittest.TestCase):

    m2crypto_key = ECCrypto().generate_key(u"very-low")
    libnacl_key = ECCrypto().generate_key(u"curve25519")

    def setUp(self):
        self.ecc = ECCrypto()

    def test_available(self):
        """
        Check if the required curves are available.
        """
        available = self.ecc.security_levels

        self.assertIn(u"very-low", available)
        self.assertIn(u"low", available)
        self.assertIn(u"medium", available)
        self.assertIn(u"high", available)
        self.assertIn(u"curve25519", available)

    def test_generate_m2crypto(self):
        """
        Check if M2Crypto backend keys can be generated correctly.
        """
        self.assertIsInstance(TestECCrypto.m2crypto_key, Key)
        self.assertIsInstance(TestECCrypto.m2crypto_key, PrivateKey)
        self.assertIsInstance(TestECCrypto.m2crypto_key, PublicKey)
        self.assertIsInstance(TestECCrypto.m2crypto_key, M2CryptoSK)
        self.assertIsInstance(TestECCrypto.m2crypto_key, M2CryptoPK)

    def test_generate_nacl(self):
        """
        Check if libnacl backend keys can be generated correctly.
        """
        self.assertIsInstance(TestECCrypto.libnacl_key, Key)
        self.assertIsInstance(TestECCrypto.libnacl_key, PrivateKey)
        self.assertIsInstance(TestECCrypto.libnacl_key, PublicKey)
        self.assertIsInstance(TestECCrypto.libnacl_key, LibNaCLSK)
        self.assertIsInstance(TestECCrypto.libnacl_key, LibNaCLPK)

    def test_generate_bogus(self):
        """
        Check if a bogus curve produces a RuntimeError
        """
        self.assertRaises(RuntimeError, self.ecc.generate_key, u"idontexist")

    def test_key_to_bin_m2crypto(self):
        """
        Check if ECCrypto correctly detects an M2Crypto key for bin.
        """
        key_bin = self.ecc.key_to_bin(TestECCrypto.m2crypto_key)

        self.assertEqual(key_bin, TestECCrypto.m2crypto_key.key_to_bin())

    def test_key_to_bin_nacl(self):
        """
        Check if ECCrypto correctly detects an libnacl key for bin.
        """
        key_bin = self.ecc.key_to_bin(TestECCrypto.libnacl_key)

        self.assertEqual(key_bin, TestECCrypto.libnacl_key.key_to_bin())

    def test_key_to_hash_m2crypto(self):
        """
        Check if ECCrypto correctly detects an M2Crypto key for hash.
        """
        key_hash = self.ecc.key_to_hash(TestECCrypto.m2crypto_key)

        self.assertEqual(key_hash, TestECCrypto.m2crypto_key.key_to_hash())

    def test_key_to_hash_nacl(self):
        """
        Check if ECCrypto correctly detects an libnacl key for hash.
        """
        key_hash = self.ecc.key_to_hash(TestECCrypto.libnacl_key)

        self.assertEqual(key_hash, TestECCrypto.libnacl_key.key_to_hash())

    def test_is_valid_private_bin_m2crypto(self):
        """
        Check if ECCrypto can detect a valid M2Crypto private key.
        """
        self.assertTrue(
            self.ecc.is_valid_private_bin(
                TestECCrypto.m2crypto_key.key_to_bin()))

    def test_is_valid_private_bin_m2crypto_public(self):
        """
        Check if ECCrypto doesn't detect a valid public M2Crypto key as a private key.
        """
        self.assertFalse(
            self.ecc.is_valid_private_bin(
                TestECCrypto.m2crypto_key.pub().key_to_bin()))

    def test_is_valid_private_bin_nacl(self):
        """
        Check if ECCrypto can detect a valid libnacl private key.
        """
        self.assertTrue(
            self.ecc.is_valid_private_bin(
                TestECCrypto.libnacl_key.key_to_bin()))

    def test_is_valid_private_bin_nacl_public(self):
        """
        Check if ECCrypto doesn't detect a valid public libnacl key as a private key.
        """
        self.assertFalse(
            self.ecc.is_valid_private_bin(
                TestECCrypto.libnacl_key.pub().key_to_bin()))

    def test_is_valid_public_bin_m2crypto(self):
        """
        Check if ECCrypto doesn't detect a valid M2Crypto private key as a public key.
        """
        self.assertFalse(
            self.ecc.is_valid_public_bin(
                TestECCrypto.m2crypto_key.key_to_bin()))

    def test_is_valid_public_bin_m2crypto_public(self):
        """
        Check if ECCrypto detects a valid public M2Crypto key as a public key.
        """
        self.assertTrue(
            self.ecc.is_valid_public_bin(
                TestECCrypto.m2crypto_key.pub().key_to_bin()))

    def test_is_valid_public_bin_nacl(self):
        """
        Check if ECCrypto doesn't detect a valid libnacl private key as a public key.
        """
        self.assertFalse(
            self.ecc.is_valid_public_bin(
                TestECCrypto.libnacl_key.key_to_bin()))

    def test_is_valid_public_bin_nacl_public(self):
        """
        Check if ECCrypto detects a valid public libnacl key as a public key.
        """
        self.assertTrue(
            self.ecc.is_valid_public_bin(
                TestECCrypto.libnacl_key.pub().key_to_bin()))