diff --git a/unit05_key_exchange/src/d_01.py b/unit05_key_exchange/src/d_01.py index 0a51f30..4ae129f 100644 --- a/unit05_key_exchange/src/d_01.py +++ b/unit05_key_exchange/src/d_01.py @@ -1,18 +1,42 @@ -from os import urandom -from eccsnacks.curve25519 import scalarmult, scalarmult_base +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.backends import default_backend + import binascii +import sys -a = urandom(32) -a_pub = scalarmult_base(a) -b = urandom(32) -b_pub = scalarmult_base(b) +size=32 # 256 bit key -k_ab = scalarmult(a, b_pub) -k_ba = scalarmult(b, a_pub) +Bob_private_key = ec.generate_private_key(ec.SECP256K1(),default_backend()) +Alice_private_key = ec.generate_private_key(ec.SECP256K1(),default_backend()) -print "Bob public: ",binascii.hexlify(b_pub) -print "Alice public: ",binascii.hexlify(a_pub) -print "Bob shared: ",binascii.hexlify(k_ba) -print "Alice shared: ",binascii.hexlify(k_ab) +Bob_shared_key = Bob_private_key.exchange(ec.ECDH(), Alice_private_key.public_key()) + +Bob_derived_key = HKDF(algorithm=hashes.SHA256(),length=size,salt=None,info=b'',backend=default_backend()).derive(Bob_shared_key) + +Alice_shared_key = Alice_private_key.exchange(ec.ECDH(), Bob_private_key.public_key()) + +Alice_derived_key = HKDF(algorithm=hashes.SHA256(),length=size,salt=None,info=b'',backend=default_backend()).derive(Alice_shared_key) + +print ("Name of curve: ",Bob_private_key.public_key().curve.name) +print (f"Generated key size: {size} bytes ({size*8} bits)") + +vals = Bob_private_key.private_numbers() +print (f"\nBob private key value: {vals.private_value}") +vals=Bob_private_key.public_key() +enc_point=binascii.b2a_hex(vals.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo)).decode() +print("Bob's public key: ",enc_point) + +vals = Alice_private_key.private_numbers() +print (f"\nAlice private key value: {vals.private_value}") +vals=Alice_private_key.public_key() +enc_point=binascii.b2a_hex(vals.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo)).decode() +print("Alice's public key: ",enc_point) + + +print ("\nBob's derived key: ",binascii.b2a_hex(Bob_derived_key).decode()) +print("Alice's derived key: ",binascii.b2a_hex(Alice_derived_key).decode())