Exemple #1
0
    def test_OOP(self):
        sk_s = unhexlify("4ba96b0b5303328c7405220598a587c4"
                         "acb06ed9a9601d149f85400195f1ec3d"
                         "a66d161e090652b054740748f059f92a"
                         "5b731f1c27b05571f6d942e4f8b7b264")
        sk = ed25519.SigningKey(sk_s)
        self.assertEqual(len(sk.to_bytes()), 64)
        self.assertEqual(sk.to_bytes(), sk_s)

        sk2_seed = unhexlify("4ba96b0b5303328c7405220598a587c4"
                             "acb06ed9a9601d149f85400195f1ec3d")
        sk2 = ed25519.SigningKey(sk2_seed)
        self.assertEqual(sk2.to_bytes(), sk.to_bytes())

        vk = sk.get_verifying_key()
        self.assertEqual(len(vk.to_bytes()), 32)
        exp_vks = unhexlify("a66d161e090652b054740748f059f92a"
                            "5b731f1c27b05571f6d942e4f8b7b264")
        self.assertEqual(vk.to_bytes(), exp_vks)
        self.assertEqual(ed25519.VerifyingKey(vk.to_bytes()), vk)
        msg = b"hello world"
        sig = sk.sign(msg)
        self.assertEqual(len(sig), 64)
        exp_sig = unhexlify("6eaffe94f2972b35158b6aaa9b69c1da"
                            "97f0896aca29c41b1dd7b32e6c9e2ff6"
                            "76fc8d8b034709cdcc37d8aeb86bebfb"
                            "173ace3c319e211ea1d7e8d8884c1808")
        self.assertEqual(sig, exp_sig)
        self.assertEqual(vk.verify(sig, msg), None)  # also, don't throw
        self.assertRaises(ed25519.BadSignatureError, vk.verify, sig,
                          msg + b".. NOT!")
Exemple #2
0
 def test_publickey(self):
     seed = unhexlify("4ba96b0b5303328c7405220598a587c4"
                      "acb06ed9a9601d149f85400195f1ec3d")
     sk = ed25519.SigningKey(seed)
     self.assertEqual(hexlify(sk.to_bytes()),
                      (b"4ba96b0b5303328c7405220598a587c4"
                       b"acb06ed9a9601d149f85400195f1ec3d"
                       b"a66d161e090652b054740748f059f92a"
                       b"5b731f1c27b05571f6d942e4f8b7b264"))
     self.assertEqual(hexlify(sk.to_seed()),
                      (b"4ba96b0b5303328c7405220598a587c4"
                       b"acb06ed9a9601d149f85400195f1ec3d"))
     self.assertRaises(ValueError, ed25519.SigningKey, b"wrong length")
     sk2 = ed25519.SigningKey(seed)
     self.assertEqual(sk, sk2)
Exemple #3
0
 def check1(encoding, expected):
     PREFIX = "private0-"
     p = sk1.to_ascii(PREFIX, encoding)
     self.assertEqual(p, expected)
     sk2 = ed25519.SigningKey(p, prefix=PREFIX, encoding=encoding)
     self.assertEqual(repr(sk1.to_bytes()), repr(sk2.to_bytes()))
     self.assertEqual(sk1, sk2)
Exemple #4
0
    def test_encoding(self):
        sk_s = b"\x88" * 32  # usually urandom(32)
        sk1 = ed25519.SigningKey(sk_s)
        vk1 = sk1.get_verifying_key()

        def check1(encoding, expected):
            PREFIX = "private0-"
            p = sk1.to_ascii(PREFIX, encoding)
            self.assertEqual(p, expected)
            sk2 = ed25519.SigningKey(p, prefix=PREFIX, encoding=encoding)
            self.assertEqual(repr(sk1.to_bytes()), repr(sk2.to_bytes()))
            self.assertEqual(sk1, sk2)

        check1("base64",
               b"private0-iIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIg")
        check1(
            "base32",
            b"private0-rceirceirceirceirceirceirceirceirceirceirceirceircea")
        check1(
            "hex",
            b"private0-8888888888888888888888888888888888888888888888888888888888888888"
        )

        def check2(encoding, expected):
            PREFIX = "public0-"
            p = vk1.to_ascii(PREFIX, encoding)
            self.assertEqual(p, expected)
            vk2 = ed25519.VerifyingKey(p, prefix=PREFIX, encoding=encoding)
            self.assertEqual(repr(vk1.to_bytes()), repr(vk2.to_bytes()))
            self.assertEqual(vk1, vk2)

        check2("base64",
               b"public0-skkdlQKuKGMKK6yy4MdFEP/N0yjDNP8+E5PnWy0x59w")
        check2(
            "base32",
            b"public0-wjer3ficvyuggcrlvszobr2fcd743uziym2p6pqtsptvwljr47oa")
        check2(
            "hex",
            b"public0-b2491d9502ae28630a2bacb2e0c74510ffcdd328c334ff3e1393e75b2d31e7dc"
        )

        def check3(encoding, expected):
            msg = b"msg"
            PREFIX = "sig0-"
            sig = sk1.sign(msg, PREFIX, encoding)
            self.assertEqual(sig, expected)
            vk1.verify(sig, msg, PREFIX, encoding)

        check3(
            "base64",
            b"sig0-MNfdUir6tMlaYQ+/p8KANJ5d+bk8g2al76v5MeJCo6RiywxURda3sU580CyiW2FBG/Q7kDRswgYqxbkQw3o5CQ"
        )
        check3(
            "base32",
            b"sig0-gdl52urk7k2mswtbb672pquagspf36nzhsbwnjppvp4tdyscuosgfsymkrc5nn5rjz6nalfclnqucg7uhoidi3gcayvmloiqyn5dsci"
        )
        check3(
            "hex",
            b"sig0-30d7dd522afab4c95a610fbfa7c280349e5df9b93c8366a5efabf931e242a3a462cb0c5445d6b7b14e7cd02ca25b61411bf43b90346cc2062ac5b910c37a3909"
        )
Exemple #5
0
    def test_prefix(self):
        sk1, vk1 = ed25519.create_keypair()
        PREFIX = b"private0-"
        p = sk1.to_bytes(PREFIX)
        # that gives us a binary string with a prefix
        self.assertTrue(p[:len(PREFIX)] == PREFIX, repr(p))
        sk2 = ed25519.SigningKey(p, prefix=PREFIX)
        self.assertEqual(sk1, sk2)
        self.assertEqual(repr(sk1.to_bytes()), repr(sk2.to_bytes()))
        self.assertRaises(ed25519.BadPrefixError,
                          ed25519.SigningKey,
                          p,
                          prefix=b"WRONG-")
        # SigningKey.to_seed() can do a prefix too
        p = sk1.to_seed(PREFIX)
        self.assertTrue(p[:len(PREFIX)] == PREFIX, repr(p))
        sk3 = ed25519.SigningKey(p, prefix=PREFIX)
        self.assertEqual(sk1, sk3)
        self.assertEqual(repr(sk1.to_bytes()), repr(sk3.to_bytes()))
        self.assertRaises(ed25519.BadPrefixError,
                          ed25519.SigningKey,
                          p,
                          prefix=b"WRONG-")

        # verifying keys can do this too
        PREFIX = b"public0-"
        p = vk1.to_bytes(PREFIX)
        self.assertTrue(p.startswith(PREFIX), repr(p))
        vk2 = ed25519.VerifyingKey(p, prefix=PREFIX)
        self.assertEqual(vk1, vk2)
        self.assertEqual(repr(vk1.to_bytes()), repr(vk2.to_bytes()))
        self.assertRaises(ed25519.BadPrefixError,
                          ed25519.VerifyingKey,
                          p,
                          prefix=b"WRONG-")

        # and signatures
        PREFIX = b"sig0-"
        p = sk1.sign(b"msg", PREFIX)
        self.assertTrue(p.startswith(PREFIX), repr(p))
        vk1.verify(p, b"msg", PREFIX)
        self.assertRaises(ed25519.BadPrefixError,
                          vk1.verify,
                          p,
                          b"msg",
                          prefix=b"WRONG-")
Exemple #6
0
    def test_object_identity(self):
        sk1_s = unhexlify("ef32972ae3f1252a5aa1395347ea008c"
                          "bd2fed0773a4ea45e2d2d06c8cf8fbd4"
                          "c024601a9c5b854fb100ff3116cf4f22"
                          "a311565f027391cb49d3bbe11c44399d")
        sk2_s = unhexlify("3d550c158900b4c2922b6656d2f80572"
                          "89de4ee65043745179685ae7d29b944d"
                          "672b8a2cb23f9e75e1d46ce249cd9c04"
                          "68f816f1c734a102822b60e18b41eacd")
        sk1a = ed25519.SigningKey(sk1_s)
        sk1b = ed25519.SigningKey(sk1_s)
        vk1a = sk1a.get_verifying_key()
        vk1b = sk1b.get_verifying_key()
        sk2 = ed25519.SigningKey(sk2_s)
        vk2 = sk2.get_verifying_key()
        self.assertEqual(sk1a, sk1b)
        self.assertNotEqual(sk1a, sk2)
        self.assertEqual(vk1a, vk1b)
        self.assertNotEqual(vk1a, vk2)

        self.assertNotEqual(sk2, b"not a SigningKey")
        self.assertNotEqual(vk2, b"not a VerifyingKey")