Debian Codegraph @

Doc/src/protocol/dh.rst

raw
1 Diffie-Hellman Key Agreement
2 ============================
3
4 Diffie-Hellman (DH) is a key agreement scheme based on asymmetric cryptography.
5
6 It enables two parties (below referred to as U and V) to jointly compute a shared secret
7 across an untrusted communication channel,
8 without explicitly transmitting the secret itself or parts of it.
9
10 In other words, an adversary can monitor the channel, see all exchanged messages,
11 and still learn nothing about the shared secret.
12
13 The shared secret is not used directly.
14 The two parties will derive symmetric keys from it,
15 and use those to encrypt and authenticate subsequent messages.
16
17 .. figure:: dh.png
18 :align: center
19
20
21 .. note::
22
23 The Diffie-Hellman protocol is fairly generic and it can be implemented
24 in many ways.
25
26 This Python module implements Diffie-Hellman according to
27 `Chapter 6 of NIST SP 800-56A Rev. 3 <https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf>`_,
28 and only for elliptic curve cryptography (ECC CDH).
29
30 Examples
31 ~~~~~~~~~
32
33 First, a minimal example where only static keys are used::
34
35 from Crypto.PublicKey import ECC
36 from Crypto.Hash import SHAKE128
37 from Crypto.Protocol.DH import key_agreement
38
39 # This KDF has been agreed in advance
40 def kdf(x):
41 return SHAKE128.new(x).read(32)
42
43 # In a real scenario, this key already exists
44 U = ECC.generate(curve='p256')
45
46 # In a real scenario, this key is received from the peer
47 # and it is verified as authentic
48 V = ECC.generate(curve='p256').public_key()
49
50 session_key = key_agreement(static_priv=U, static_pub=V, kdf=kdf)
51
52 # session_key is an AES-256 key, which will be used to encrypt
53 # subsequent communications
54
55 NIST SP 800-56A recommends to bind the key agreement to some information
56 concerning the context of the operation, for instance a description of what the keys are used for::
57
58 from Crypto.Hash import TupleHash128
59 from Crypto.Protocol.DH import key_agreement
60 from functools import partial
61
62 # Random value (e.g., a nonce)
63 session_salt = b'XYZ'
64
65 # The unique identifier of each party
66 id_u = 'Client 12'
67 id_v = 'Server 25'
68
69 def kdf(x):
70 h = TupleHash128.new(digest_bytes=32)
71 h.update(x,
72 session_salt,
73 id_u.encode(), # To UTF-8
74 id_v.encode(), # To UTF-8
75 b'Email encryption',
76 b'TupleHash128',
77 b'AES256')
78 return h.digest()
79
80 # In a real scenario, this key already exists
81 U = ECC.generate(curve='p256')
82
83 # In a real scenario, this key is received from the peer
84 # and it is verified as authentic
85 V = ECC.generate(curve='p256').public_key()
86
87 key = key_agreement(static_priv=U, static_pub=V, kdf=kdf)
88
89 The examples above all used static keys only.
90 In order to achieve *Perfect Forward Secrecy*,
91 it is recommended that at least one party (but ideally both)
92 contributes to the key agreement with one ephemeral key
93 (that is, one key pair generated for the session, and destroyed at the end)::
94
95 from Crypto.PublicKey import ECC
96 from Crypto.Hash import SHAKE128
97 from Crypto.Protocol.DH import key_agreement
98
99 # This KDF has been agreed in advance
100 def kdf(x):
101 return SHAKE128.new(x).read(32)
102
103 # In a real scenario, this long-term key already exists
104 U_static = ECC.generate(curve='p256')
105
106 # This ephemeral key is generated only for this session
107 U_ephemeral = ECC.generate(curve='p256')
108
109 # In a real scenario, this long-term key is received from the peer
110 # and it is verified as authentic
111 V_static = ECC.generate(curve='p256').public_key()
112
113 # In a real scenario, the peer generated this ephemeral key only
114 # for this session. It doesn't need to be authenticated if the
115 # static key of V already has been.
116 V_ephemeral = ECC.generate(curve='p256').public_key()
117
118 session_key = key_agreement(static_priv=U_static,
119 static_pub=V_static,
120 eph_priv=U_priv,
121 eph_pub=U_pub,
122 kdf=kdf)
123
124 # session_key is an AES-256 key, which will be used to encrypt
125 # subsequent communications
126
127 Specification
128 ~~~~~~~~~~~~~
129
130 .. autofunction:: Crypto.Protocol.DH.key_agreement
131
132 Key Derivation Function
133 ~~~~~~~~~~~~~~~~~~~~~~~~
134
135 The ``kdf`` parameter must be a cryptographically secure key derivation function,
136 not password-based. For instance, some reasonable choices are:
137
138 * :ref:`hkdf`
139 * :ref:`shake128` or :ref:`shake256`
140 * :ref:`sp800-180-counter`
141
142 KDFs are typically fed with multiple inputs,
143 whereas the ``kdf`` function must take exactly one (of type ``bytes``).
144 Therefore, a temporary function ``kdf`` should be constructed
145 by fixing some of the inputs of the underlying KDF.
146 For example using ``functools``::
147
148 from Crypto.Protocol.KDF import HDKF
149 from Crypto.Hash import SHA256
150 import functools
151
152 kdf = functoools.partial(HDK,
153 key_len=32,
154 salt=b'nonce',
155 hashmod=SHA256,
156 num_keys=2,
157 context=b'Some context about the operation')
158
159 # Pass kdf to key_agreement()
160
161 In other cases, defining a local function can be sufficient::
162
163 import os
164 from Crypto.Hash import SHAKE128
165
166 # A non-secret value generated for the session
167 nonce = os.urandom(16)
168
169 def kdf(x):
170 return SHAKE128.new(x + nonce).read(32)
171
172 # Pass kdf to key_agreement()
173
174 Recommendations
175 ~~~~~~~~~~~~~~~~~~~~~~~
176
177 This module only provides the fundamental low-level primitive of the Diffie-Hellman key agreement.
178 When designing the procotol, users should ensure that:
179
180 - Each party is certain that the public key of the peer is authentic,
181 and that the risk of Man-in-the-Middle attacks is considered.
182
183 - The requirement for *Perfect Forward Secrecy* is understood and, if necessary,
184 ephemeral keys are generated, used in the key agreement, and then destroyed.
185
186 - The risk of *Key compromise impersonation* is understood.
187 With that attack, an adversary that compromises our static key,
188 can impersonate *any other user to us*.
189 The use of ephemeral keys addresses this risk too.
190
191 - Some form of *Key Confirmation* is included after the key agreement (e.g., with a MAC),
192 to immediately detect
193 if the shared secret has not being correctly calculated.
194
195 .. _NIST: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
196
197