ecc

Elliptic Curve Cryptograpy library from scratch in python.
Log | Files | Refs | README | LICENSE

test_ecdh.py (2915B)


      1 import pytest
      2 from hypothesis import given
      3 from hypothesis import strategies as st
      4 
      5 from ecc import Curve, PrivateKey, generate_keypair, generate_shared_secret, secp256k1
      6 
      7 # --- Test Class for Organization ---
      8 
      9 
     10 class TestECDH:
     11     """Tests the Elliptic Curve Diffie-Hellman key exchange implementation."""
     12 
     13     def test_shared_secret_agreement(self):
     14         """
     15         Tests the fundamental property of ECDH: two parties must arrive
     16         at the exact same shared secret.
     17         """
     18         # 1. Alice generates her key pair
     19         alice_priv, alice_pub = generate_keypair(secp256k1)
     20 
     21         # 2. Bob generates his key pair
     22         bob_priv, bob_pub = generate_keypair(secp256k1)
     23 
     24         # 3. Alice computes the shared secret using her private key and Bob's public key.
     25         secret_by_alice = generate_shared_secret(alice_priv, bob_pub)
     26 
     27         # 4. Bob computes the shared secret using his private key and Alice's public key.
     28         secret_by_bob = generate_shared_secret(bob_priv, alice_pub)
     29 
     30         # 5. The secrets must be identical and have the correct length.
     31         assert len(secret_by_alice) == 32
     32         assert secret_by_alice == secret_by_bob
     33 
     34     def test_ecdh_fails_with_different_curves(self):
     35         """
     36         Tests that the ECDH exchange raises a ValueError if the keys
     37         are not on the same curve.
     38         """
     39         # Create a small toy curve for the test
     40         toy_curve = Curve(a=2, b=2, P=17, G=(5, 1), n=19)
     41 
     42         alice_priv, _ = generate_keypair(secp256k1)
     43         _, bob_pub = generate_keypair(toy_curve)
     44 
     45         with pytest.raises(
     46             ValueError, match="Keys must be on the same curve for ECDH."
     47         ):
     48             generate_shared_secret(alice_priv, bob_pub)
     49 
     50     def test_kdf_produces_different_keys_with_different_info(self):
     51         """
     52         Tests that the HKDF correctly produces different keys when the
     53         'info' parameter is different.
     54         """
     55         alice_priv, _ = generate_keypair(secp256k1)
     56         _, bob_pub = generate_keypair(secp256k1)
     57 
     58         # Generate a key for one context
     59         secret1 = alice_priv.ecdh(bob_pub, info=b"for encryption")
     60 
     61         # Generate a key for a different context
     62         secret2 = alice_priv.ecdh(bob_pub, info=b"for authentication")
     63 
     64         assert secret1 != secret2
     65 
     66 
     67 # --- Property-Based Testing ---
     68 
     69 # A strategy to generate a valid private key for secp256k1
     70 private_keys = st.builds(PrivateKey, curve=st.just(secp256k1))
     71 
     72 
     73 @given(alice_priv=private_keys, bob_priv=private_keys)
     74 def test_shared_secret_agreement_property(alice_priv, bob_priv):
     75     """
     76     Property: For any two generated key pairs, both parties must
     77     always arrive at the same shared secret.
     78     """
     79     alice_pub = alice_priv.public_key
     80     bob_pub = bob_priv.public_key
     81 
     82     secret_by_alice = alice_priv.ecdh(bob_pub)
     83     secret_by_bob = bob_priv.ecdh(alice_pub)
     84 
     85     assert secret_by_alice == secret_by_bob