Diffie-Hellman Key Exchange Scheme¶
This module contains a toy implementation of the Diffie-Hellman key exchange scheme.
AUTHORS:
Vincent Macri (2024-07-30): initial version
- class sage.crypto.key_exchange.diffie_hellman.DiffieHellman(p: Integer, g: Integer | IntegerMod_abstract, proof: bool = True)[source]¶
Bases:
KeyExchangeSchemeCreate an instance of the Diffie-Hellman key exchange scheme using the given prime
pand baseg.INPUT:
p– prime integer defining the field that the key exchanges will be performed over, must be at least 5g– base for the key exchange, (coerceable to) an element of from toproof– (default:True) whether to require a proof thatpis prime. IfFalse, a probabilistic test can be used for checking thatpis prime. This should be set toFalsewhen using large (cryptographic size) primes, otherwise checking primality will take too long.
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!
REFERENCES:
For more information, see Section 8.1 of [PP2010].
EXAMPLES:
sage: DH = key_exchange.DiffieHellman(13, 2) doctest:...: FutureWarning: This class/method/function is marked as experimental. It, its functionality or its interface might change without a formal deprecation. See https://github.com/sagemath/sage/issues/37305 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.DiffieHellman(p, 2, proof=False) sage: alice_sk = DH.generate_secret_key() sage: alice_pk = DH.generate_public_key(alice_sk) sage: bob_sk = DH.generate_secret_key() sage: bob_pk = DH.generate_public_key(bob_sk) sage: alice_shared_secret = DH.compute_shared_secret(bob_pk, alice_sk) sage: bob_shared_secret = DH.compute_shared_secret(alice_pk, bob_sk) 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.DiffieHellman(17, 3) sage: DH.compute_shared_secret(13, 11) 4
- field()[source]¶
Return the field this
DiffieHellmaninstance is working over.EXAMPLES:
sage: DH = key_exchange.DiffieHellman(5, 2) sage: DH.field() Finite Field of size 5
- generate_public_key(secret_key)[source]¶
Generate a Diffie-Hellman public key using the given secret key.
INPUT:
secret_key– the secret key to generate the public key with
EXAMPLES:
sage: DH = key_exchange.DiffieHellman(13, 2) sage: DH.generate_public_key(4) 3
- generator()[source]¶
Return the generator
gfor thisDiffieHellmaninstance.EXAMPLES:
sage: DH = key_exchange.DiffieHellman(7, 3) sage: DH.generator() 3
- parameters()[source]¶
Get the parameters
(p, g)for thisDiffieHellmaninstance.EXAMPLES:
sage: DH = key_exchange.DiffieHellman(7, 3) sage: DH.parameters() (7, 3)
- prime()[source]¶
Return the prime
pfor thisDiffieHellmaninstance.EXAMPLES:
sage: DH = key_exchange.DiffieHellman(7, 3) sage: DH.prime() 7
- subgroup_size()[source]¶
Calculates the size of the subgroup of
generated byself.generator().EXAMPLES:
This is an example of a
DiffieHellmaninstance where the subgroup size is :sage: DH = key_exchange.DiffieHellman(47, 2) sage: DH.subgroup_size() 23
This is an example of a
DiffieHellmaninstance where the subgroup size is :sage: DH = key_exchange.DiffieHellman(47, 5) sage: DH.subgroup_size() 46