@@ -95,7 +95,7 @@ exporting keys.
95
95
RSA
96
96
~~~
97
97
98
- PyCrypto example:
98
+ ` PyCrypto ` example:
99
99
100
100
::
101
101
@@ -123,7 +123,7 @@ PyCrypto example:
123
123
ECDSA
124
124
~~~~~
125
125
126
- oscrypto example:
126
+ ` oscrypto ` example:
127
127
128
128
::
129
129
@@ -147,12 +147,73 @@ oscrypto example:
147
147
key = load_public_key(encode_ec_public_key(pub))
148
148
ecdsa_verify(key, signature, b'Data to sign', 'sha1')
149
149
150
+ ECDH
151
+ ~~~~
152
+
153
+ Smartcard-HSM can generate a shared key via ECDH key exchange.
154
+
155
+ .. warning ::
156
+
157
+ Where possible, e.g. over networks, you should use ephemeral keys,
158
+ to allow for perfect forward secrecy. Smartcard HSM's ECDH is only useful
159
+ when need to repeatedly retrieve the same shared secret, e.g. encrypting
160
+ files in a hybrid cryptosystem.
161
+
162
+ `cryptography ` example:
163
+
164
+ ::
165
+
166
+ from cryptography.hazmat.backends import default_backend
167
+ from cryptography.hazmat.primitives.asymmetric import ec
168
+ from cryptography.hazmat.primitives.serialization import \
169
+ Encoding, PublicFormat, load_der_public_key
170
+
171
+ # Retrieve our keypair, with our public key encoded for interchange
172
+ alice_priv = self.session.get_key(key_type=KeyType.EC,
173
+ object_class=ObjectClass.PRIVATE_KEY)
174
+ alice_pub = self.session.get_key(key_type=KeyType.EC,
175
+ object_class=ObjectClass.PUBLIC_KEY)
176
+ alice_pub = encode_ec_public_key(alice_pub)
177
+
178
+ # Bob generates a keypair, with their public key encoded for
179
+ # interchange
180
+ bob_priv = ec.generate_private_key(ec.SECP256R1,
181
+ default_backend())
182
+ bob_pub = bob_priv.public_key().public_bytes(
183
+ Encoding.DER,
184
+ PublicFormat.SubjectPublicKeyInfo,
185
+ )
186
+
187
+ # Bob converts Alice's key to internal format and generates their
188
+ # shared key
189
+ bob_shared_key = bob_priv.exchange(
190
+ ec.ECDH(),
191
+ load_der_public_key(alice_pub, default_backend()),
192
+ )
193
+
194
+ key = alice_priv.derive_key(
195
+ KeyType.GENERIC_SECRET, 256,
196
+ mechanism_param=(
197
+ KDF.NULL, None,
198
+ # SmartcardHSM doesn't accept DER-encoded EC_POINTs for derivation
199
+ decode_ec_public_key(bob_pub, encode_ec_point=False)
200
+ [Attribute.EC_POINT],
201
+ ),
202
+ )
203
+ alice_shared_key = key[Attribute.VALUE]
204
+
205
+ When decoding the other user's `EC_POINT ` for passing into the key derivation
206
+ the standard says to pass a raw octet string (set `encode_ec_point ` to False),
207
+ however some PKCS #11 implementations require a DER-encoded octet string
208
+ (i.e. the format of the :attr: `pkcs11.constants.Attribute.EC_POINT ` attribute).
209
+
150
210
Encrypting Files
151
211
----------------
152
212
153
213
The device only supports asymmetric mechanisms. To do file encryption, you
154
- will need to generate AES keys locally, which you can encrypt with the
155
- public key (this is how the Nitrokey storage key works).
214
+ will need to generate AES keys locally, which you can encrypt with your RSA
215
+ public key (this is how the Nitrokey storage key works); or by using ECDH
216
+ to generate a shared secret from a locally generated public key.
156
217
157
218
Debugging
158
219
---------
0 commit comments