Implement ECDH with curve25519

This commit is contained in:
Juhani Krekelä 2020-06-25 11:34:55 +03:00
parent a1a314ba95
commit 09ad93fbeb
1 changed files with 25 additions and 0 deletions

25
ecdh_curve25519.py Normal file
View File

@ -0,0 +1,25 @@
import x25519
basepoint = b'\x09' + b'\x00' * 31
def derive_pubkey(seckey):
return x25519.x25519(seckey, basepoint)
def ecdh(pubkey, seckey):
shared_secret = x25519.x25519(seckey, pubkey)
ored = 0
for i in shared_secret:
ored |= i
if ored == 0:
raise ValueError
else:
return shared_secret
if __name__ == '__main__':
alice_sk = bytes.fromhex('77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a')
alice_pk = derive_pubkey(alice_sk)
bob_sk = bytes.fromhex('5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb')
bob_pk = derive_pubkey(bob_sk)
print(ecdh(alice_pk, bob_sk).hex() == '4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742')
print(ecdh(bob_pk, alice_sk).hex() == '4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742')