From 9aaeee0dc62189204f38097c815a0913fabe006c Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Thu, 30 Apr 2015 14:06:47 -0400 Subject: Add an Elliptic Curve Key Exchange Algorithm(ECDH) The ECDH Key Exchange algorithm as standardized in NIST publication 800-56A Revision 2 Includes tests with vectors from NIST. Signed-off-by: Simo Sorce --- docs/hazmat/primitives/asymmetric/ec.rst | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'docs/hazmat/primitives') diff --git a/docs/hazmat/primitives/asymmetric/ec.rst b/docs/hazmat/primitives/asymmetric/ec.rst index 6356c278..910ce5d8 100644 --- a/docs/hazmat/primitives/asymmetric/ec.rst +++ b/docs/hazmat/primitives/asymmetric/ec.rst @@ -122,6 +122,48 @@ Elliptic Curve Signature Algorithms :returns: A new instance of a :class:`EllipticCurvePublicKey` provider. +Elliptic Curve Key Exchange algorithm +------------------------------------- + +.. class:: ECDH(private_key) + + .. versionadded:: 1.1 + + The ECDH Key Exchange algorithm first standardized in NIST publication + `800-56A`_, and later in `800-56Ar2`_. + + :param private_key: An instance of :class:`EllipticCurvePrivateKey`. + + .. doctest:: + + >>> from cryptography.hazmat.backends import default_backend + >>> from cryptography.hazmat.primitives.asymmetric import ec + >>> private_key = ec.generate_private_key( + ... ec.SECP384R1(), default_backend() + ... ) + >>> peer_public_key = ec.generate_private_key( + ... ec.SECP384R1(), default_backend() + ... ).public_key() + >>> ecdh = ec.ECDH(private_key) + >>> sharedkey = ecdh.compute_key(peer_public_key) + + .. attribute:: private_key + + :type: :class:`EllipticCurvePrivateKey` + + The private key associated to this object + + .. method:: public_key() + + The public key associated to the object's private key. + + .. method:: compute_key(peer_public_key) + + :param peer_public_key: A :class:`EllipticCurvePublicKey` object. + + :returns: A ``bytes`` object containing the computed key. + + Elliptic Curves --------------- @@ -419,6 +461,8 @@ Key Interfaces .. _`FIPS 186-3`: http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf .. _`FIPS 186-4`: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf +.. _`800-56A`: http://csrc.nist.gov/publications/nistpubs/800-56A/SP800-56A_Revision1_Mar08-2007.pdf +.. _`800-56Ar2`: http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf .. _`some concern`: https://crypto.stackexchange.com/questions/10263/should-we-trust-the-nist-recommended-ecc-parameters .. _`less than 224 bits`: http://www.ecrypt.eu.org/ecrypt2/documents/D.SPA.20.pdf .. _`elliptic curve diffie-hellman is faster than diffie-hellman`: http://digitalcommons.unl.edu/cgi/viewcontent.cgi?article=1100&context=cseconfwork -- cgit v1.2.3 From 5cdfba5c8d06ed10510310de03e1df0265a89bcc Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 17 Oct 2015 16:33:04 -0400 Subject: a refactor to the API --- docs/hazmat/primitives/asymmetric/ec.rst | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'docs/hazmat/primitives') diff --git a/docs/hazmat/primitives/asymmetric/ec.rst b/docs/hazmat/primitives/asymmetric/ec.rst index 910ce5d8..9b2e61fb 100644 --- a/docs/hazmat/primitives/asymmetric/ec.rst +++ b/docs/hazmat/primitives/asymmetric/ec.rst @@ -125,14 +125,12 @@ Elliptic Curve Signature Algorithms Elliptic Curve Key Exchange algorithm ------------------------------------- -.. class:: ECDH(private_key) +.. class:: ECDH() .. versionadded:: 1.1 - The ECDH Key Exchange algorithm first standardized in NIST publication - `800-56A`_, and later in `800-56Ar2`_. - - :param private_key: An instance of :class:`EllipticCurvePrivateKey`. + The Elliptic Curve Diffie-Hellman Key Exchange algorithm first standardized + in NIST publication `800-56A`_, and later in `800-56Ar2`_. .. doctest:: @@ -144,24 +142,7 @@ Elliptic Curve Key Exchange algorithm >>> peer_public_key = ec.generate_private_key( ... ec.SECP384R1(), default_backend() ... ).public_key() - >>> ecdh = ec.ECDH(private_key) - >>> sharedkey = ecdh.compute_key(peer_public_key) - - .. attribute:: private_key - - :type: :class:`EllipticCurvePrivateKey` - - The private key associated to this object - - .. method:: public_key() - - The public key associated to the object's private key. - - .. method:: compute_key(peer_public_key) - - :param peer_public_key: A :class:`EllipticCurvePublicKey` object. - - :returns: A ``bytes`` object containing the computed key. + >>> shared_key = private_key.exchange(ec.ECDH(), peer_public_key) Elliptic Curves -- cgit v1.2.3 From 68b3441d90fa4230b62d19a43c49dd2aa155db47 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 17 Oct 2015 18:09:03 -0400 Subject: better document hte iface --- docs/hazmat/primitives/asymmetric/ec.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'docs/hazmat/primitives') diff --git a/docs/hazmat/primitives/asymmetric/ec.rst b/docs/hazmat/primitives/asymmetric/ec.rst index 9b2e61fb..525bd6cb 100644 --- a/docs/hazmat/primitives/asymmetric/ec.rst +++ b/docs/hazmat/primitives/asymmetric/ec.rst @@ -132,6 +132,9 @@ Elliptic Curve Key Exchange algorithm The Elliptic Curve Diffie-Hellman Key Exchange algorithm first standardized in NIST publication `800-56A`_, and later in `800-56Ar2`_. + For most applications the ``shared_key`` should be passed to a key + derivation function. + .. doctest:: >>> from cryptography.hazmat.backends import default_backend @@ -337,6 +340,22 @@ Key Interfaces :returns: :class:`~cryptography.hazmat.primitives.asymmetric.AsymmetricSignatureContext` + .. method:: exchange(algorithm, peer_public_key) + + Perform's a key exchange operation using the provided algorithm with + the peer's public key. + + For most applications the result should be passed to a key derivation + function. + + :param algorithm: The key exchange algorithm, currently only + :class:`~cryptography.hazmat.primitives.asymmetric.ec.ECDH` is + supported. + :param EllipticCurvePublicKey peer_public_key: The public key for the + peer. + + :returns bytes: A shared key. + .. method:: public_key() :return: :class:`EllipticCurvePublicKey` -- cgit v1.2.3