2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2016-10-21 22:51:52 +00:00
|
|
|
|
|
|
|
from trezor.crypto.curve import curve25519
|
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2016-10-21 22:51:52 +00:00
|
|
|
class TestCryptoCurve25519(unittest.TestCase):
|
|
|
|
|
|
|
|
vectors = [
|
2023-06-28 10:46:29 +00:00
|
|
|
(
|
|
|
|
"38c9d9b17911de26ed812f5cc19c0029e8d016bcbc6078bc9db2af33f1761e4a",
|
|
|
|
"311b6248af8dabec5cc81eac5bf229925f6d218a12e0547fb1856e015cc76f5d",
|
|
|
|
"a93dbdb23e5c99da743e203bd391af79f2b83fb8d0fd6ec813371c71f08f2d4d",
|
|
|
|
),
|
2016-10-21 22:51:52 +00:00
|
|
|
]
|
|
|
|
|
2016-10-24 10:08:12 +00:00
|
|
|
def test_generate_secret(self):
|
|
|
|
for _ in range(100):
|
|
|
|
sk = curve25519.generate_secret()
|
|
|
|
self.assertTrue(len(sk) == 32)
|
|
|
|
self.assertTrue(sk[0] & 7 == 0 and sk[31] & 128 == 0 and sk[31] & 64 == 64)
|
|
|
|
|
2016-10-21 22:51:52 +00:00
|
|
|
def test_multiply(self):
|
|
|
|
for sk, pk, session in self.vectors:
|
|
|
|
session2 = curve25519.multiply(unhexlify(sk), unhexlify(pk))
|
|
|
|
self.assertEqual(session2, unhexlify(session))
|
|
|
|
|
2016-10-23 20:39:11 +00:00
|
|
|
def test_multiply_random(self):
|
|
|
|
for _ in range(100):
|
2016-10-24 10:08:12 +00:00
|
|
|
sk1 = curve25519.generate_secret()
|
|
|
|
sk2 = curve25519.generate_secret()
|
2016-10-23 20:39:11 +00:00
|
|
|
pk1 = curve25519.publickey(sk1)
|
|
|
|
pk2 = curve25519.publickey(sk2)
|
|
|
|
session1 = curve25519.multiply(sk1, pk2)
|
|
|
|
session2 = curve25519.multiply(sk2, pk1)
|
|
|
|
self.assertEqual(session1, session2)
|
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
if __name__ == "__main__":
|
2016-10-21 22:51:52 +00:00
|
|
|
unittest.main()
|