Debian Codegraph @

src/lib/crypto/OSSLDH.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 OSSLDH.cpp
29
30 OpenSSL Diffie-Hellman asymmetric algorithm implementation
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "OSSLDH.h"
36 #include "CryptoFactory.h"
37 #include "DHParameters.h"
38 #include "OSSLComp.h"
39 #include "OSSLDHKeyPair.h"
40 #include "OSSLUtil.h"
41 #include <algorithm>
42 #include <openssl/dh.h>
43 #include <openssl/pem.h>
44 #include <openssl/err.h>
45
46 // Signing functions
47 bool OSSLDH::signInit(PrivateKey* /*privateKey*/, const AsymMech::Type /*mechanism*/,
48 const void* /* param = NULL */, const size_t /* paramLen = 0 */)
49 {
50 ERROR_MSG("DH does not support signing");
51
52 return false;
53 }
54
55 bool OSSLDH::signUpdate(const ByteString& /*dataToSign*/)
56 {
57 ERROR_MSG("DH does not support signing");
58
59 return false;
60 }
61
62 bool OSSLDH::signFinal(ByteString& /*signature*/)
63 {
64 ERROR_MSG("DH does not support signing");
65
66 return false;
67 }
68
69 // Verification functions
70 bool OSSLDH::verifyInit(PublicKey* /*publicKey*/, const AsymMech::Type /*mechanism*/,
71 const void* /* param = NULL */, const size_t /* paramLen = 0 */)
72 {
73 ERROR_MSG("DH does not support verifying");
74
75 return false;
76 }
77
78 bool OSSLDH::verifyUpdate(const ByteString& /*originalData*/)
79 {
80 ERROR_MSG("DH does not support verifying");
81
82 return false;
83 }
84
85 bool OSSLDH::verifyFinal(const ByteString& /*signature*/)
86 {
87 ERROR_MSG("DH does not support verifying");
88
89 return false;
90 }
91
92 // Encryption functions
93 bool OSSLDH::encrypt(PublicKey* /*publicKey*/, const ByteString& /*data*/,
94 ByteString& /*encryptedData*/, const AsymMech::Type /*padding*/)
95 {
96 ERROR_MSG("DH does not support encryption");
97
98 return false;
99 }
100
101 // Decryption functions
102 bool OSSLDH::decrypt(PrivateKey* /*privateKey*/, const ByteString& /*encryptedData*/,
103 ByteString& /*data*/, const AsymMech::Type /*padding*/)
104 {
105 ERROR_MSG("DH does not support decryption");
106
107 return false;
108 }
109
110 // Key factory
111 bool OSSLDH::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */)
112 {
113 // Check parameters
114 if ((ppKeyPair == NULL) ||
115 (parameters == NULL))
116 {
117 return false;
118 }
119
120 if (!parameters->areOfType(DHParameters::type))
121 {
122 ERROR_MSG("Invalid parameters supplied for DH key generation");
123
124 return false;
125 }
126
127 DHParameters* params = (DHParameters*) parameters;
128
129 // Generate the key-pair
130 DH* dh = DH_new();
131 if (dh == NULL)
132 {
133 ERROR_MSG("Failed to instantiate OpenSSL DH object");
134
135 return false;
136 }
137
138 BIGNUM* bn_p = OSSL::byteString2bn(params->getP());
139 BIGNUM* bn_g = OSSL::byteString2bn(params->getG());
140
141 if (!DH_set0_pqg(dh, bn_p, NULL, bn_g))
142 {
143 ERROR_MSG("DH set pqg failed (0x%08X)", ERR_get_error());
144
145 BN_free(bn_p);
146 BN_free(bn_g);
147 DH_free(dh);
148
149 return false;
150 }
151
152 if (params->getXBitLength() > 0)
153 {
154 if (!DH_set_length(dh, params->getXBitLength()))
155 {
156 ERROR_MSG("DH set length failed (0x%08X)", ERR_get_error());
157
158 DH_free(dh);
159
160 return false;
161 }
162 }
163
164 if (DH_generate_key(dh) != 1)
165 {
166 ERROR_MSG("DH key generation failed (0x%08X)", ERR_get_error());
167
168 DH_free(dh);
169
170 return false;
171 }
172
173 // Create an asymmetric key-pair object to return
174 OSSLDHKeyPair* kp = new OSSLDHKeyPair();
175
176 ((OSSLDHPublicKey*) kp->getPublicKey())->setFromOSSL(dh);
177 ((OSSLDHPrivateKey*) kp->getPrivateKey())->setFromOSSL(dh);
178
179 *ppKeyPair = kp;
180
181 // Release the key
182 DH_free(dh);
183
184 return true;
185 }
186
187 bool OSSLDH::deriveKey(SymmetricKey **ppSymmetricKey, PublicKey* publicKey, PrivateKey* privateKey)
188 {
189 // Check parameters
190 if ((ppSymmetricKey == NULL) ||
191 (publicKey == NULL) ||
192 (privateKey == NULL))
193 {
194 return false;
195 }
196
197 // Get keys
198 DH *pub = ((OSSLDHPublicKey *)publicKey)->getOSSLKey();
199 DH *priv = ((OSSLDHPrivateKey *)privateKey)->getOSSLKey();
200 if (pub == NULL || priv == NULL)
201 {
202 ERROR_MSG("Failed to get OpenSSL DH keys");
203
204 return false;
205 }
206 const BIGNUM* bn_pub_key = NULL;
207 DH_get0_key(pub, &bn_pub_key, NULL);
208 if (bn_pub_key == NULL)
209 {
210 ERROR_MSG("Failed to get OpenSSL DH keys");
211
212 return false;
213 }
214
215 // Derive the secret
216 ByteString secret, derivedSecret;
217 int size = DH_size(priv);
218 secret.wipe(size);
219 derivedSecret.wipe(size);
220 int keySize = DH_compute_key(&derivedSecret[0], bn_pub_key, priv);
221
222 if (keySize <= 0)
223 {
224 ERROR_MSG("DH key derivation failed (0x%08X)", ERR_get_error());
225
226 return false;
227 }
228
229 // We compensate that OpenSSL removes leading zeros
230 memcpy(&secret[0] + size - keySize, &derivedSecret[0], keySize);
231
232 *ppSymmetricKey = new SymmetricKey(secret.size() * 8);
233 if (*ppSymmetricKey == NULL)
234 return false;
235 if (!(*ppSymmetricKey)->setKeyBits(secret))
236 {
237 delete *ppSymmetricKey;
238 *ppSymmetricKey = NULL;
239 return false;
240 }
241
242 return true;
243 }
244
245 unsigned long OSSLDH::getMinKeySize()
246 {
247 #ifdef WITH_FIPS
248 // OPENSSL_DH_FIPS_MIN_MODULUS_BITS is 1024
249 return 1024;
250 #else
251 return 512;
252 #endif
253 }
254
255 unsigned long OSSLDH::getMaxKeySize()
256 {
257 return OPENSSL_DH_MAX_MODULUS_BITS;
258 }
259
260 bool OSSLDH::generateParameters(AsymmetricParameters** ppParams, void* parameters /* = NULL */, RNG* /*rng = NULL*/)
261 {
262 if ((ppParams == NULL) || (parameters == NULL))
263 {
264 return false;
265 }
266
267 size_t bitLen = (size_t) parameters;
268
269 if (bitLen < getMinKeySize() || bitLen > getMaxKeySize())
270 {
271 ERROR_MSG("This DH key size is not supported");
272
273 return false;
274 }
275
276 DH* dh = DH_new();
277 if (dh == NULL)
278 {
279 ERROR_MSG("Failed to create DH object");
280
281 return false;
282 }
283
284 if (!DH_generate_parameters_ex(dh, bitLen, 2, NULL))
285 {
286 ERROR_MSG("Failed to generate %d bit DH parameters", bitLen);
287
288 DH_free(dh);
289
290 return false;
291 }
292
293 // Store the DH parameters
294 DHParameters* params = new DHParameters();
295
296 const BIGNUM* bn_p = NULL;
297 const BIGNUM* bn_g = NULL;
298
299 DH_get0_pqg(dh, &bn_p, NULL, &bn_g);
300 ByteString p = OSSL::bn2ByteString(bn_p); params->setP(p);
301 ByteString g = OSSL::bn2ByteString(bn_g); params->setG(g);
302
303 *ppParams = params;
304
305 DH_free(dh);
306
307 return true;
308 }
309
310 bool OSSLDH::reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData)
311 {
312 // Check input
313 if ((ppKeyPair == NULL) ||
314 (serialisedData.size() == 0))
315 {
316 return false;
317 }
318
319 ByteString dPub = ByteString::chainDeserialise(serialisedData);
320 ByteString dPriv = ByteString::chainDeserialise(serialisedData);
321
322 OSSLDHKeyPair* kp = new OSSLDHKeyPair();
323
324 bool rv = true;
325
326 if (!((DHPublicKey*) kp->getPublicKey())->deserialise(dPub))
327 {
328 rv = false;
329 }
330
331 if (!((DHPrivateKey*) kp->getPrivateKey())->deserialise(dPriv))
332 {
333 rv = false;
334 }
335
336 if (!rv)
337 {
338 delete kp;
339
340 return false;
341 }
342
343 *ppKeyPair = kp;
344
345 return true;
346 }
347
348 bool OSSLDH::reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData)
349 {
350 // Check input
351 if ((ppPublicKey == NULL) ||
352 (serialisedData.size() == 0))
353 {
354 return false;
355 }
356
357 OSSLDHPublicKey* pub = new OSSLDHPublicKey();
358
359 if (!pub->deserialise(serialisedData))
360 {
361 delete pub;
362
363 return false;
364 }
365
366 *ppPublicKey = pub;
367
368 return true;
369 }
370
371 bool OSSLDH::reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData)
372 {
373 // Check input
374 if ((ppPrivateKey == NULL) ||
375 (serialisedData.size() == 0))
376 {
377 return false;
378 }
379
380 OSSLDHPrivateKey* priv = new OSSLDHPrivateKey();
381
382 if (!priv->deserialise(serialisedData))
383 {
384 delete priv;
385
386 return false;
387 }
388
389 *ppPrivateKey = priv;
390
391 return true;
392 }
393
394 PublicKey* OSSLDH::newPublicKey()
395 {
396 return (PublicKey*) new OSSLDHPublicKey();
397 }
398
399 PrivateKey* OSSLDH::newPrivateKey()
400 {
401 return (PrivateKey*) new OSSLDHPrivateKey();
402 }
403
404 AsymmetricParameters* OSSLDH::newParameters()
405 {
406 return (AsymmetricParameters*) new DHParameters();
407 }
408
409 bool OSSLDH::reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData)
410 {
411 // Check input parameters
412 if ((ppParams == NULL) || (serialisedData.size() == 0))
413 {
414 return false;
415 }
416
417 DHParameters* params = new DHParameters();
418
419 if (!params->deserialise(serialisedData))
420 {
421 delete params;
422
423 return false;
424 }
425
426 *ppParams = params;
427
428 return true;
429 }
430