Debian Codegraph @

src/lib/crypto/OSSLGOST.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 OSSLGOST.cpp
29
30 OpenSSL GOST R 34.10-2001 asymmetric algorithm implementation
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_GOST
35 #include "log.h"
36 #include "OSSLGOST.h"
37 #include "OSSLCryptoFactory.h"
38 #include "ECParameters.h"
39 #include "OSSLGOSTKeyPair.h"
40 #include "OSSLGOSTPrivateKey.h"
41 #include "OSSLGOSTPublicKey.h"
42 #include "OSSLComp.h"
43 #include <algorithm>
44 #include <openssl/ecdsa.h>
45 #include <openssl/pem.h>
46 #include <openssl/err.h>
47 #include <string.h>
48
49 // Destructor
50 OSSLGOST::~OSSLGOST()
51 {
52 EVP_MD_CTX_free(curCTX);
53 }
54
55 // Signing functions
56 bool OSSLGOST::sign(PrivateKey* privateKey, const ByteString& dataToSign,
57 ByteString& signature, const AsymMech::Type mechanism,
58 const void* param /* = NULL */, const size_t paramLen /* = 0 */)
59 {
60 if (mechanism == AsymMech::GOST)
61 {
62 // Separate implementation for GOST signing without hash computation
63
64 // Check if the private key is the right type
65 if (!privateKey->isOfType(OSSLGOSTPrivateKey::type))
66 {
67 ERROR_MSG("Invalid key type supplied");
68
69 return false;
70 }
71
72 // In case of raw GOST, the length of the input data must be 32 bytes
73 if (dataToSign.size() != 32)
74 {
75 ERROR_MSG("Size of data to sign is not 32 bytes");
76
77 return false;
78 }
79
80 // Perform the signature operation
81 OSSLGOSTPrivateKey* osslKey = (OSSLGOSTPrivateKey*) privateKey;
82 EVP_PKEY* pkey = osslKey->getOSSLKey();
83 size_t outLen;
84
85 if (pkey == NULL)
86 {
87 ERROR_MSG("Could not get the OpenSSL private key");
88
89 return false;
90 }
91
92 signature.resize(EVP_PKEY_size(pkey));
93 outLen = signature.size();
94
95 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey,NULL);
96 if (ctx == NULL)
97 {
98 ERROR_MSG("EVP_PKEY_CTX_new failed");
99 return false;
100 }
101
102 if (EVP_PKEY_sign_init(ctx) <= 0)
103 {
104 ERROR_MSG("EVP_PKEY_sign_init failed");
105 EVP_PKEY_CTX_free(ctx);
106 return false;
107 }
108
109 if (EVP_PKEY_sign(ctx, &signature[0], &outLen, dataToSign.const_byte_str(), dataToSign.size()) <= 0)
110 {
111 ERROR_MSG("An error occurred while performing a signature");
112 EVP_PKEY_CTX_free(ctx);
113 return false;
114 }
115
116 signature.resize(outLen);
117 EVP_PKEY_CTX_free(ctx);
118
119 return true;
120 }
121 else
122 {
123 // Call default implementation
124 return AsymmetricAlgorithm::sign(privateKey, dataToSign, signature, mechanism, param, paramLen);
125 }
126 }
127
128 bool OSSLGOST::signInit(PrivateKey* privateKey, const AsymMech::Type mechanism,
129 const void* param /* = NULL */, const size_t paramLen /* = 0 */)
130 {
131 if (!AsymmetricAlgorithm::signInit(privateKey, mechanism, param, paramLen))
132 {
133 return false;
134 }
135
136 // Check if the private key is the right type
137 if (!privateKey->isOfType(OSSLGOSTPrivateKey::type))
138 {
139 ERROR_MSG("Invalid key type supplied");
140
141 ByteString dummy;
142 AsymmetricAlgorithm::signFinal(dummy);
143
144 return false;
145 }
146
147 if (mechanism != AsymMech::GOST_GOST)
148 {
149 ERROR_MSG("Invalid mechanism supplied (%i)", mechanism);
150
151 ByteString dummy;
152 AsymmetricAlgorithm::signFinal(dummy);
153
154 return false;
155 }
156
157 curCTX = EVP_MD_CTX_new();
158 if (curCTX == NULL)
159 {
160 ERROR_MSG("Failed to allocate space for EVP_MD_CTX");
161
162 ByteString dummy;
163 AsymmetricAlgorithm::signFinal(dummy);
164
165 return false;
166 }
167
168 const EVP_MD* md = OSSLCryptoFactory::i()->EVP_GOST_34_11;
169 if (!EVP_DigestInit_ex(curCTX, md, NULL))
170 {
171 ERROR_MSG("EVP_DigestInit_ex failed");
172
173 EVP_MD_CTX_free(curCTX);
174 curCTX = NULL;
175
176 ByteString dummy;
177 AsymmetricAlgorithm::signFinal(dummy);
178
179 return false;
180 }
181
182 return true;
183 }
184
185 bool OSSLGOST::signUpdate(const ByteString& dataToSign)
186 {
187 if (!AsymmetricAlgorithm::signUpdate(dataToSign))
188 {
189 return false;
190 }
191
192 if (!EVP_DigestUpdate(curCTX, dataToSign.const_byte_str(), dataToSign.size()))
193 {
194 ERROR_MSG("EVP_DigestUpdate failed");
195
196 EVP_MD_CTX_free(curCTX);
197 curCTX = NULL;
198
199 ByteString dummy;
200 AsymmetricAlgorithm::signFinal(dummy);
201
202 return false;
203 }
204
205 return true;
206 }
207
208 bool OSSLGOST::signFinal(ByteString& signature)
209 {
210 // Save necessary state before calling super class signFinal
211 OSSLGOSTPrivateKey* pk = (OSSLGOSTPrivateKey*) currentPrivateKey;
212
213 if (!AsymmetricAlgorithm::signFinal(signature))
214 {
215 return false;
216 }
217
218 // Perform the signature operation
219 EVP_PKEY* pkey = pk->getOSSLKey();
220 unsigned int outLen;
221
222 if (pkey == NULL)
223 {
224 ERROR_MSG("Could not get the OpenSSL private key");
225
226 EVP_MD_CTX_free(curCTX);
227 curCTX = NULL;
228
229 return false;
230 }
231
232 signature.resize(EVP_PKEY_size(pkey));
233 outLen = signature.size();
234 if (!EVP_SignFinal(curCTX, &signature[0], &outLen, pkey))
235 {
236 ERROR_MSG("EVP_SignFinal failed");
237
238 EVP_MD_CTX_free(curCTX);
239 curCTX = NULL;
240
241 return false;
242 }
243
244 signature.resize(outLen);
245
246 EVP_MD_CTX_free(curCTX);
247 curCTX = NULL;
248
249 return true;
250 }
251
252 // Verification functions
253 bool OSSLGOST::verify(PublicKey* publicKey, const ByteString& originalData,
254 const ByteString& signature, const AsymMech::Type mechanism,
255 const void* param /* = NULL */, const size_t paramLen /* = 0 */)
256 {
257 if (mechanism == AsymMech::GOST)
258 {
259 // Separate implementation for GOST verification without hash computation
260
261 // Check if the private key is the right type
262 if (!publicKey->isOfType(OSSLGOSTPublicKey::type))
263 {
264 ERROR_MSG("Invalid key type supplied");
265
266 return false;
267 }
268
269 // Perform the verification operation
270 OSSLGOSTPublicKey* osslKey = (OSSLGOSTPublicKey*) publicKey;
271 EVP_PKEY* pkey = osslKey->getOSSLKey();
272
273 if (pkey == NULL)
274 {
275 ERROR_MSG("Could not get the OpenSSL public key");
276
277 return false;
278 }
279
280 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey,NULL);
281 if (ctx == NULL)
282 {
283 ERROR_MSG("EVP_PKEY_CTX_new failed");
284 return false;
285 }
286
287 if (EVP_PKEY_verify_init(ctx) <= 0)
288 {
289 ERROR_MSG("EVP_PKEY_verify_init failed");
290 EVP_PKEY_CTX_free(ctx);
291 return false;
292 }
293
294 int ret = EVP_PKEY_verify(ctx, signature.const_byte_str(), signature.size(), originalData.const_byte_str(), originalData.size());
295 EVP_PKEY_CTX_free(ctx);
296 if (ret != 1)
297 {
298 if (ret < 0)
299 ERROR_MSG("GOST verify failed (0x%08X)", ERR_get_error());
300
301 return false;
302 }
303 return true;
304 }
305 else
306 {
307 // Call the generic function
308 return AsymmetricAlgorithm::verify(publicKey, originalData, signature, mechanism, param, paramLen);
309 }
310 }
311
312 bool OSSLGOST::verifyInit(PublicKey* publicKey, const AsymMech::Type mechanism,
313 const void* param /* = NULL */, const size_t paramLen /* = 0 */)
314 {
315 if (!AsymmetricAlgorithm::verifyInit(publicKey, mechanism, param, paramLen))
316 {
317 return false;
318 }
319
320 // Check if the public key is the right type
321 if (!publicKey->isOfType(OSSLGOSTPublicKey::type))
322 {
323 ERROR_MSG("Invalid key type supplied");
324
325 ByteString dummy;
326 AsymmetricAlgorithm::verifyFinal(dummy);
327
328 return false;
329 }
330
331 if (mechanism != AsymMech::GOST_GOST)
332 {
333 ERROR_MSG("Invalid mechanism supplied (%i)", mechanism);
334
335 ByteString dummy;
336 AsymmetricAlgorithm::verifyFinal(dummy);
337
338 return false;
339 }
340
341 curCTX = EVP_MD_CTX_new();
342 if (curCTX == NULL)
343 {
344 ERROR_MSG("Failed to allocate space for EVP_MD_CTX");
345
346 ByteString dummy;
347 AsymmetricAlgorithm::verifyFinal(dummy);
348
349 return false;
350 }
351
352 const EVP_MD* md = OSSLCryptoFactory::i()->EVP_GOST_34_11;
353 if (!EVP_DigestInit_ex(curCTX, md, NULL))
354 {
355 ERROR_MSG("EVP_DigestInit_ex failed");
356
357 EVP_MD_CTX_free(curCTX);
358 curCTX = NULL;
359
360 ByteString dummy;
361 AsymmetricAlgorithm::verifyFinal(dummy);
362
363 return false;
364 }
365
366 return true;
367 }
368
369 bool OSSLGOST::verifyUpdate(const ByteString& originalData)
370 {
371 if (!AsymmetricAlgorithm::verifyUpdate(originalData))
372 {
373 return false;
374 }
375
376 if (!EVP_DigestUpdate(curCTX, originalData.const_byte_str(), originalData.size()))
377 {
378 ERROR_MSG("EVP_DigestUpdate failed");
379
380 EVP_MD_CTX_free(curCTX);
381 curCTX = NULL;
382
383 ByteString dummy;
384 AsymmetricAlgorithm::verifyFinal(dummy);
385
386 return false;
387 }
388
389 return true;
390 }
391
392 bool OSSLGOST::verifyFinal(const ByteString& signature)
393 {
394 // Save necessary state before calling super class verifyFinal
395 OSSLGOSTPublicKey* pk = (OSSLGOSTPublicKey*) currentPublicKey;
396
397 if (!AsymmetricAlgorithm::verifyFinal(signature))
398 {
399 return false;
400 }
401
402 // Perform the verify operation
403 EVP_PKEY *pkey = pk->getOSSLKey();
404 int ret;
405
406 if (pkey == NULL)
407 {
408 ERROR_MSG("Could not get the OpenSSL public key");
409
410 EVP_MD_CTX_free(curCTX);
411 curCTX = NULL;
412
413 return false;
414 }
415
416 ret = EVP_VerifyFinal(curCTX, signature.const_byte_str(), signature.size(), pkey);
417 EVP_MD_CTX_free(curCTX);
418 curCTX = NULL;
419 if (ret != 1)
420 {
421 if (ret < 0)
422 ERROR_MSG("GOST verify failed (0x%08X)", ERR_get_error());
423
424 return false;
425 }
426 return true;
427 }
428
429 // Encryption functions
430 bool OSSLGOST::encrypt(PublicKey* /*publicKey*/, const ByteString& /*data*/,
431 ByteString& /*encryptedData*/, const AsymMech::Type /*padding*/)
432 {
433 ERROR_MSG("GOST does not support encryption");
434
435 return false;
436 }
437
438 // Decryption functions
439 bool OSSLGOST::decrypt(PrivateKey* /*privateKey*/, const ByteString& /*encryptedData*/,
440 ByteString& /*data*/, const AsymMech::Type /*padding*/)
441 {
442 ERROR_MSG("GOST does not support decryption");
443
444 return false;
445 }
446
447 // Key factory
448 bool OSSLGOST::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */)
449 {
450 // Check parameters
451 if ((ppKeyPair == NULL) ||
452 (parameters == NULL))
453 {
454 return false;
455 }
456
457 if (!parameters->areOfType(ECParameters::type))
458 {
459 ERROR_MSG("Invalid parameters supplied for GOST key generation");
460
461 return false;
462 }
463
464 ECParameters* params = (ECParameters*) parameters;
465 ByteString paramA = "06072a850302022301";
466 if (params->getEC() != paramA)
467 {
468 ERROR_MSG("unsupported parameters");
469
470 return false;
471 }
472
473 // Generate the key-pair
474 EVP_PKEY_CTX* ctx = NULL;
475 EVP_PKEY* pkey = NULL;
476 OSSLGOSTKeyPair* kp;
477
478 ctx = EVP_PKEY_CTX_new_id(NID_id_GostR3410_2001, NULL);
479 if (ctx == NULL)
480 {
481 ERROR_MSG("EVP_PKEY_CTX_new_id failed");
482
483 goto err;
484 }
485 if (EVP_PKEY_keygen_init(ctx) <= 0)
486 {
487 ERROR_MSG("EVP_PKEY_keygen_init failed");
488
489 goto err;
490 }
491 if (EVP_PKEY_CTX_ctrl_str(ctx, "paramset", "A") <= 0)
492 {
493 ERROR_MSG("EVP_PKEY_CTX_ctrl_str failed");
494
495 goto err;
496 }
497 if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
498 {
499 ERROR_MSG("EVP_PKEY_keygen failed");
500
501 goto err;
502 }
503 EVP_PKEY_CTX_free(ctx);
504 ctx = NULL;
505
506 // Create an asymmetric key-pair object to return
507 kp = new OSSLGOSTKeyPair();
508
509 ((OSSLGOSTPublicKey*) kp->getPublicKey())->setFromOSSL(pkey);
510 ((OSSLGOSTPrivateKey*) kp->getPrivateKey())->setFromOSSL(pkey);
511
512 *ppKeyPair = kp;
513
514 // Release the key
515 EVP_PKEY_free(pkey);
516
517 return true;
518
519 err:
520 if (ctx != NULL)
521 EVP_PKEY_CTX_free(ctx);
522 if (pkey != NULL)
523 EVP_PKEY_free(pkey);
524
525 return false;
526 }
527
528 unsigned long OSSLGOST::getMinKeySize()
529 {
530 return 0;
531 }
532
533 unsigned long OSSLGOST::getMaxKeySize()
534 {
535 return 0;
536 }
537
538 bool OSSLGOST::reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData)
539 {
540 // Check input
541 if ((ppKeyPair == NULL) ||
542 (serialisedData.size() == 0))
543 {
544 return false;
545 }
546
547 ByteString dPub = ByteString::chainDeserialise(serialisedData);
548 ByteString dPriv = ByteString::chainDeserialise(serialisedData);
549
550 OSSLGOSTKeyPair* kp = new OSSLGOSTKeyPair();
551
552 bool rv = true;
553
554 if (!((OSSLGOSTPublicKey*) kp->getPublicKey())->deserialise(dPub))
555 {
556 rv = false;
557 }
558
559 if (!((OSSLGOSTPrivateKey*) kp->getPrivateKey())->deserialise(dPriv))
560 {
561 rv = false;
562 }
563
564 if (!rv)
565 {
566 delete kp;
567
568 return false;
569 }
570
571 *ppKeyPair = kp;
572
573 return true;
574 }
575
576 bool OSSLGOST::reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData)
577 {
578 // Check input
579 if ((ppPublicKey == NULL) ||
580 (serialisedData.size() == 0))
581 {
582 return false;
583 }
584
585 OSSLGOSTPublicKey* pub = new OSSLGOSTPublicKey();
586
587 if (!pub->deserialise(serialisedData))
588 {
589 delete pub;
590
591 return false;
592 }
593
594 *ppPublicKey = pub;
595
596 return true;
597 }
598
599 bool OSSLGOST::reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData)
600 {
601 // Check input
602 if ((ppPrivateKey == NULL) ||
603 (serialisedData.size() == 0))
604 {
605 return false;
606 }
607
608 OSSLGOSTPrivateKey* priv = new OSSLGOSTPrivateKey();
609
610 if (!priv->deserialise(serialisedData))
611 {
612 delete priv;
613
614 return false;
615 }
616
617 *ppPrivateKey = priv;
618
619 return true;
620 }
621
622 PublicKey* OSSLGOST::newPublicKey()
623 {
624 return (PublicKey*) new OSSLGOSTPublicKey();
625 }
626
627 PrivateKey* OSSLGOST::newPrivateKey()
628 {
629 return (PrivateKey*) new OSSLGOSTPrivateKey();
630 }
631
632 AsymmetricParameters* OSSLGOST::newParameters()
633 {
634 return (AsymmetricParameters*) new ECParameters();
635 }
636
637 bool OSSLGOST::reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData)
638 {
639 // Check input parameters
640 if ((ppParams == NULL) || (serialisedData.size() == 0))
641 {
642 return false;
643 }
644
645 ECParameters* params = new ECParameters();
646
647 if (!params->deserialise(serialisedData))
648 {
649 delete params;
650
651 return false;
652 }
653
654 *ppParams = params;
655
656 return true;
657 }
658 #endif