Debian Codegraph @

src/lib/crypto/OSSLECDH.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 OSSLECDH.cpp
29
30 OpenSSL Diffie-Hellman asymmetric algorithm implementation
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_ECC
35 #include "log.h"
36 #include "OSSLECDH.h"
37 #include "CryptoFactory.h"
38 #include "ECParameters.h"
39 #include "OSSLECKeyPair.h"
40 #include "OSSLUtil.h"
41 #include <algorithm>
42 #include <openssl/ecdh.h>
43 #include <openssl/pem.h>
44 #include <openssl/err.h>
45 #ifdef WITH_FIPS
46 #include <openssl/fips.h>
47 #endif
48
49 // Signing functions
50 bool OSSLECDH::signInit(PrivateKey* /*privateKey*/, const AsymMech::Type /*mechanism*/,
51 const void* /* param = NULL */, const size_t /* paramLen = 0 */)
52 {
53 ERROR_MSG("ECDH does not support signing");
54
55 return false;
56 }
57
58 bool OSSLECDH::signUpdate(const ByteString& /*dataToSign*/)
59 {
60 ERROR_MSG("ECDH does not support signing");
61
62 return false;
63 }
64
65 bool OSSLECDH::signFinal(ByteString& /*signature*/)
66 {
67 ERROR_MSG("ECDH does not support signing");
68
69 return false;
70 }
71
72 // Verification functions
73 bool OSSLECDH::verifyInit(PublicKey* /*publicKey*/, const AsymMech::Type /*mechanism*/,
74 const void* /* param = NULL */, const size_t /* paramLen = 0 */)
75 {
76 ERROR_MSG("ECDH does not support verifying");
77
78 return false;
79 }
80
81 bool OSSLECDH::verifyUpdate(const ByteString& /*originalData*/)
82 {
83 ERROR_MSG("ECDH does not support verifying");
84
85 return false;
86 }
87
88 bool OSSLECDH::verifyFinal(const ByteString& /*signature*/)
89 {
90 ERROR_MSG("ECDH does not support verifying");
91
92 return false;
93 }
94
95 // Encryption functions
96 bool OSSLECDH::encrypt(PublicKey* /*publicKey*/, const ByteString& /*data*/,
97 ByteString& /*encryptedData*/, const AsymMech::Type /*padding*/)
98 {
99 ERROR_MSG("ECDH does not support encryption");
100
101 return false;
102 }
103
104 // Decryption functions
105 bool OSSLECDH::decrypt(PrivateKey* /*privateKey*/, const ByteString& /*encryptedData*/,
106 ByteString& /*data*/, const AsymMech::Type /*padding*/)
107 {
108 ERROR_MSG("ECDH does not support decryption");
109
110 return false;
111 }
112
113 // Key factory
114 bool OSSLECDH::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */)
115 {
116 // Check parameters
117 if ((ppKeyPair == NULL) ||
118 (parameters == NULL))
119 {
120 return false;
121 }
122
123 if (!parameters->areOfType(ECParameters::type))
124 {
125 ERROR_MSG("Invalid parameters supplied for ECDH key generation");
126
127 return false;
128 }
129
130 ECParameters* params = (ECParameters*) parameters;
131
132 // Generate the key-pair
133 EC_KEY* eckey = EC_KEY_new();
134
135 if (eckey == NULL)
136 {
137 ERROR_MSG("Failed to instantiate OpenSSL ECDH object");
138
139 return false;
140 }
141
142 EC_GROUP* grp = OSSL::byteString2grp(params->getEC());
143 EC_KEY_set_group(eckey, grp);
144 EC_GROUP_free(grp);
145
146 if (!EC_KEY_generate_key(eckey))
147 {
148 ERROR_MSG("ECDH key generation failed (0x%08X)", ERR_get_error());
149
150 EC_KEY_free(eckey);
151
152 return false;
153 }
154
155 // Create an asymmetric key-pair object to return
156 OSSLECKeyPair* kp = new OSSLECKeyPair();
157
158 ((OSSLECPublicKey*) kp->getPublicKey())->setFromOSSL(eckey);
159 ((OSSLECPrivateKey*) kp->getPrivateKey())->setFromOSSL(eckey);
160
161 *ppKeyPair = kp;
162
163 // Release the key
164 EC_KEY_free(eckey);
165
166 return true;
167 }
168
169 bool OSSLECDH::deriveKey(SymmetricKey **ppSymmetricKey, PublicKey* publicKey, PrivateKey* privateKey)
170 {
171 // Check parameters
172 if ((ppSymmetricKey == NULL) ||
173 (publicKey == NULL) ||
174 (privateKey == NULL))
175 {
176 return false;
177 }
178
179 // Get keys
180 EC_KEY *pub = ((OSSLECPublicKey *)publicKey)->getOSSLKey();
181 EC_KEY *priv = ((OSSLECPrivateKey *)privateKey)->getOSSLKey();
182 if (pub == NULL || EC_KEY_get0_public_key(pub) == NULL || priv == NULL)
183 {
184 ERROR_MSG("Failed to get OpenSSL ECDH keys");
185
186 return false;
187 }
188
189 // Use the OpenSSL implementation and not any engine
190 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
191
192 #ifdef WITH_FIPS
193 if (FIPS_mode())
194 {
195 ECDH_set_method(pub, FIPS_ecdh_openssl());
196 ECDH_set_method(priv, FIPS_ecdh_openssl());
197 }
198 else
199 {
200 ECDH_set_method(pub, ECDH_OpenSSL());
201 ECDH_set_method(priv, ECDH_OpenSSL());
202 }
203 #else
204 ECDH_set_method(pub, ECDH_OpenSSL());
205 ECDH_set_method(priv, ECDH_OpenSSL());
206 #endif
207
208 #else
209 EC_KEY_set_method(pub, EC_KEY_OpenSSL());
210 EC_KEY_set_method(priv, EC_KEY_OpenSSL());
211 #endif
212
213 // Derive the secret
214 ByteString secret, derivedSecret;
215 int size = ((OSSLECPublicKey *)publicKey)->getOrderLength();
216 secret.wipe(size);
217 derivedSecret.wipe(size);
218 int keySize = ECDH_compute_key(&derivedSecret[0], derivedSecret.size(), EC_KEY_get0_public_key(pub), priv, NULL);
219
220 if (keySize <= 0)
221 {
222 ERROR_MSG("ECDH key derivation failed (0x%08X)", ERR_get_error());
223
224 return false;
225 }
226
227 // We compensate that OpenSSL removes leading zeros
228 memcpy(&secret[0] + size - keySize, &derivedSecret[0], keySize);
229
230 *ppSymmetricKey = new SymmetricKey(secret.size() * 8);
231 if (*ppSymmetricKey == NULL)
232 return false;
233 if (!(*ppSymmetricKey)->setKeyBits(secret))
234 {
235 delete *ppSymmetricKey;
236 *ppSymmetricKey = NULL;
237 return false;
238 }
239
240 return true;
241 }
242
243 unsigned long OSSLECDH::getMinKeySize()
244 {
245 // Smallest EC group is secp112r1
246 return 112;
247 }
248
249 unsigned long OSSLECDH::getMaxKeySize()
250 {
251 // Biggest EC group is secp521r1
252 return 521;
253 }
254
255 bool OSSLECDH::reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData)
256 {
257 // Check input
258 if ((ppKeyPair == NULL) ||
259 (serialisedData.size() == 0))
260 {
261 return false;
262 }
263
264 ByteString dPub = ByteString::chainDeserialise(serialisedData);
265 ByteString dPriv = ByteString::chainDeserialise(serialisedData);
266
267 OSSLECKeyPair* kp = new OSSLECKeyPair();
268
269 bool rv = true;
270
271 if (!((ECPublicKey*) kp->getPublicKey())->deserialise(dPub))
272 {
273 rv = false;
274 }
275
276 if (!((ECPrivateKey*) kp->getPrivateKey())->deserialise(dPriv))
277 {
278 rv = false;
279 }
280
281 if (!rv)
282 {
283 delete kp;
284
285 return false;
286 }
287
288 *ppKeyPair = kp;
289
290 return true;
291 }
292
293 bool OSSLECDH::reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData)
294 {
295 // Check input
296 if ((ppPublicKey == NULL) ||
297 (serialisedData.size() == 0))
298 {
299 return false;
300 }
301
302 OSSLECPublicKey* pub = new OSSLECPublicKey();
303
304 if (!pub->deserialise(serialisedData))
305 {
306 delete pub;
307
308 return false;
309 }
310
311 *ppPublicKey = pub;
312
313 return true;
314 }
315
316 bool OSSLECDH::reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData)
317 {
318 // Check input
319 if ((ppPrivateKey == NULL) ||
320 (serialisedData.size() == 0))
321 {
322 return false;
323 }
324
325 OSSLECPrivateKey* priv = new OSSLECPrivateKey();
326
327 if (!priv->deserialise(serialisedData))
328 {
329 delete priv;
330
331 return false;
332 }
333
334 *ppPrivateKey = priv;
335
336 return true;
337 }
338
339 PublicKey* OSSLECDH::newPublicKey()
340 {
341 return (PublicKey*) new OSSLECPublicKey();
342 }
343
344 PrivateKey* OSSLECDH::newPrivateKey()
345 {
346 return (PrivateKey*) new OSSLECPrivateKey();
347 }
348
349 AsymmetricParameters* OSSLECDH::newParameters()
350 {
351 return (AsymmetricParameters*) new ECParameters();
352 }
353
354 bool OSSLECDH::reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData)
355 {
356 // Check input parameters
357 if ((ppParams == NULL) || (serialisedData.size() == 0))
358 {
359 return false;
360 }
361
362 ECParameters* params = new ECParameters();
363
364 if (!params->deserialise(serialisedData))
365 {
366 delete params;
367
368 return false;
369 }
370
371 *ppParams = params;
372
373 return true;
374 }
375 #endif