Example #1
0
    def test_emit(self):
        """Basic sanity check on the code emitters."""
        k = Ed25519.generate()

        ccode = io.StringIO()
        k.emit_c_public(ccode)
        self.assertIn("ed25519_pub_key", ccode.getvalue())
        self.assertIn("ed25519_pub_key_len", ccode.getvalue())

        rustcode = io.StringIO()
        k.emit_rust_public(rustcode)
        self.assertIn("ED25519_PUB_KEY", rustcode.getvalue())
Example #2
0
    def test_sig(self):
        k = Ed25519.generate()
        buf = b'This is the message'
        sig = k.raw_sign(buf)

        # The code doesn't have any verification, so verify this
        # manually.
        k.key.public_key().verify(signature=sig, data=buf)

        # Modify the message to make sure the signature fails.
        self.assertRaises(InvalidSignature,
                          k.key.public_key().verify,
                          signature=sig,
                          data=b'This is thE message')
Example #3
0
    def test_emit_pub(self):
        """Basic sanity check on the code emitters."""
        pubname = self.tname("public.pem")
        k = Ed25519.generate()
        k.export_public(pubname)

        k2 = load(pubname)

        ccode = io.StringIO()
        k2.emit_c_public(ccode)
        self.assertIn("ed25519_pub_key", ccode.getvalue())
        self.assertIn("ed25519_pub_key_len", ccode.getvalue())

        rustcode = io.StringIO()
        k2.emit_rust_public(rustcode)
        self.assertIn("ED25519_PUB_KEY", rustcode.getvalue())
Example #4
0
    def test_keygen(self):
        name1 = self.tname("keygen.pem")
        k = Ed25519.generate()
        k.export_private(name1, b'secret')

        self.assertIsNone(load(name1))

        k2 = load(name1, b'secret')

        pubname = self.tname('keygen-pub.pem')
        k2.export_public(pubname)
        pk2 = load(pubname)

        # We should be able to export the public key from the loaded
        # public key, but not the private key.
        pk2.export_public(self.tname('keygen-pub2.pem'))
        self.assertRaises(Ed25519UsageError,
                          pk2.export_private, self.tname('keygen-priv2.pem'))
Example #5
0
    def test_sig(self):
        k = Ed25519.generate()
        buf = b'This is the message'
        sha = hashlib.sha256()
        sha.update(buf)
        digest = sha.digest()
        sig = k.sign_digest(digest)

        # The code doesn't have any verification, so verify this
        # manually.
        k.key.public_key().verify(signature=sig, data=digest)

        # Modify the message to make sure the signature fails.
        sha = hashlib.sha256()
        sha.update(b'This is thE message')
        new_digest = sha.digest()
        self.assertRaises(InvalidSignature,
                          k.key.public_key().verify,
                          signature=sig,
                          data=new_digest)