Finite Field Diffie-Hellman¶
Toy implementation of Diffie-Hellman key exchange over finite fields \(\Zmod{p}\).
Warning
This is a toy implementation for educational use only! Do not use this implementation, or any cryptographic features of Sage, in any setting where security is needed!
AUTHORS:
Vincent Macri (2024-07-30): initial version
Brian Heckel (2025-12-06): converted to inherit from KeyExchangeBase class
- class sage.crypto.public_key.key_exchange.finite_field_diffie_hellman.FiniteFieldDiffieHellman(p: Integer | int, generator: Integer | IntegerMod_abstract | int, proof: bool | None = None)[source]¶
Bases:
CommutativeKeyExchangeBaseCreate an instance of the Diffie-Hellman key exchange scheme using the given prime
pand baseg.INPUT:
p– prime integer defining the field \(\GF{p}\) that the key exchanges will be performed over, must be at least 5generator– base for the key exchange, (coerceable to) an element of \(\GF{p}\) from \(2\) to \(p - 2\)proof– whether to require a proof thatpis prime. IfFalse, a probabilistic test is used to check thatpis prime. This should be set toFalsewhen using large (cryptographic size) primes, otherwise checking primality will take too long. If this is not specified, then the default behaviour is to use the current value of \(proof.arithmetic()\).
REFERENCES:
For more information, see Section 8.1 of [PP2010].
EXAMPLES:
sage: DH = key_exchange.FiniteFieldDiffieHellman(13, 2) doctest:...: FutureWarning: SageMath's key exchange functionality is experimental and might change in the future. See https://github.com/sagemath/sage/issues/41218 for details.
This is an example of a full key exchange using a cryptographically large prime. This is the prime from the 8192-bit MODP group in RFC 3526 (see [KK2003]):
sage: p = 2^8192 - 2^8128 - 1 + 2^64 * (round(2^8062 * pi) + 4743158) sage: DH = key_exchange.FiniteFieldDiffieHellman(p, 2, proof=False) sage: alice_sk = DH.secret_key() sage: alice_pk = DH.public_key(alice_sk) sage: bob_sk = DH.secret_key() sage: bob_pk = DH.public_key(bob_sk) sage: alice_shared_secret = DH.compute_shared_secret(alice_sk, bob_pk) sage: bob_shared_secret = DH.compute_shared_secret(bob_sk, alice_pk) sage: alice_shared_secret == bob_shared_secret True
Compute the shared secret using the given public key and secret keys.
INPUT:
pk– public keysk– secret key
EXAMPLES:
sage: DH = key_exchange.FiniteFieldDiffieHellman(17, 3) sage: DH.compute_shared_secret(11, 13) 4
- parameters()[source]¶
Returns a list of the prime
pused, the base generatorgand the field used.EXAMPLES:
sage: DH = key_exchange.FiniteFieldDiffieHellman(17, 3) sage: DH.parameters() (Finite Field of size 17, 3)