| 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 |
OSSLECDSA.cpp |
| 29 |
|
| 30 |
OpenSSL ECDSA asymmetric algorithm implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#ifdef WITH_ECC |
| 35 |
#include "log.h" |
| 36 |
#include "OSSLECDSA.h" |
| 37 |
#include "CryptoFactory.h" |
| 38 |
#include "ECParameters.h" |
| 39 |
#include "OSSLECKeyPair.h" |
| 40 |
#include "OSSLComp.h" |
| 41 |
#include "OSSLUtil.h" |
| 42 |
#include <algorithm> |
| 43 |
#include <openssl/ecdsa.h> |
| 44 |
#include <openssl/pem.h> |
| 45 |
#include <openssl/err.h> |
| 46 |
#ifdef WITH_FIPS |
| 47 |
#include <openssl/fips.h> |
| 48 |
#endif |
| 49 |
#include <string.h> |
| 50 |
|
| 51 |
// Signing functions |
| 52 |
bool OSSLECDSA::sign(PrivateKey* privateKey, const ByteString& dataToSign, |
| 53 |
ByteString& signature, const AsymMech::Type mechanism, |
| 54 |
const void* /* param = NULL */, const size_t /* paramLen = 0 */) |
| 55 |
{ |
| 56 |
if (mechanism != AsymMech::ECDSA) |
| 57 |
{ |
| 58 |
ERROR_MSG("Invalid mechanism supplied (%i)", mechanism); |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
// Check if the private key is the right type |
| 63 |
if (!privateKey->isOfType(OSSLECPrivateKey::type)) |
| 64 |
{ |
| 65 |
ERROR_MSG("Invalid key type supplied"); |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
OSSLECPrivateKey* pk = (OSSLECPrivateKey*) privateKey; |
| 71 |
EC_KEY* eckey = pk->getOSSLKey(); |
| 72 |
|
| 73 |
if (eckey == NULL) |
| 74 |
{ |
| 75 |
ERROR_MSG("Could not get the OpenSSL private key"); |
| 76 |
|
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
// Use the OpenSSL implementation and not any engine |
| 81 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 82 |
|
| 83 |
#ifdef WITH_FIPS |
| 84 |
if (FIPS_mode()) |
| 85 |
ECDSA_set_method(eckey, FIPS_ecdsa_openssl()); |
| 86 |
else |
| 87 |
ECDSA_set_method(eckey, ECDSA_OpenSSL()); |
| 88 |
#else |
| 89 |
ECDSA_set_method(eckey, ECDSA_OpenSSL()); |
| 90 |
#endif |
| 91 |
|
| 92 |
#else |
| 93 |
EC_KEY_set_method(eckey, EC_KEY_OpenSSL()); |
| 94 |
#endif |
| 95 |
|
| 96 |
// Perform the signature operation |
| 97 |
size_t len = pk->getOrderLength(); |
| 98 |
if (len == 0) |
| 99 |
{ |
| 100 |
ERROR_MSG("Could not get the order length"); |
| 101 |
return false; |
| 102 |
} |
| 103 |
signature.resize(2 * len); |
| 104 |
memset(&signature[0], 0, 2 * len); |
| 105 |
ECDSA_SIG *sig = ECDSA_do_sign(dataToSign.const_byte_str(), dataToSign.size(), eckey); |
| 106 |
if (sig == NULL) |
| 107 |
{ |
| 108 |
ERROR_MSG("ECDSA sign failed (0x%08X)", ERR_get_error()); |
| 109 |
return false; |
| 110 |
} |
| 111 |
// Store the 2 values with padding |
| 112 |
const BIGNUM* bn_r = NULL; |
| 113 |
const BIGNUM* bn_s = NULL; |
| 114 |
ECDSA_SIG_get0(sig, &bn_r, &bn_s); |
| 115 |
BN_bn2bin(bn_r, &signature[len - BN_num_bytes(bn_r)]); |
| 116 |
BN_bn2bin(bn_s, &signature[2 * len - BN_num_bytes(bn_s)]); |
| 117 |
ECDSA_SIG_free(sig); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
bool OSSLECDSA::signInit(PrivateKey* /*privateKey*/, const AsymMech::Type /*mechanism*/, |
| 122 |
const void* /* param = NULL */, const size_t /* paramLen = 0 */) |
| 123 |
{ |
| 124 |
ERROR_MSG("ECDSA does not support multi part signing"); |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
bool OSSLECDSA::signUpdate(const ByteString& /*dataToSign*/) |
| 130 |
{ |
| 131 |
ERROR_MSG("ECDSA does not support multi part signing"); |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
bool OSSLECDSA::signFinal(ByteString& /*signature*/) |
| 137 |
{ |
| 138 |
ERROR_MSG("ECDSA does not support multi part signing"); |
| 139 |
|
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
// Verification functions |
| 144 |
bool OSSLECDSA::verify(PublicKey* publicKey, const ByteString& originalData, |
| 145 |
const ByteString& signature, const AsymMech::Type mechanism, |
| 146 |
const void* /* param = NULL */, const size_t /* paramLen = 0 */) |
| 147 |
{ |
| 148 |
if (mechanism != AsymMech::ECDSA) |
| 149 |
{ |
| 150 |
ERROR_MSG("Invalid mechanism supplied (%i)", mechanism); |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
// Check if the private key is the right type |
| 155 |
if (!publicKey->isOfType(OSSLECPublicKey::type)) |
| 156 |
{ |
| 157 |
ERROR_MSG("Invalid key type supplied"); |
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
OSSLECPublicKey* pk = (OSSLECPublicKey*) publicKey; |
| 163 |
EC_KEY* eckey = pk->getOSSLKey(); |
| 164 |
|
| 165 |
if (eckey == NULL) |
| 166 |
{ |
| 167 |
ERROR_MSG("Could not get the OpenSSL public key"); |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
// Use the OpenSSL implementation and not any engine |
| 173 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 174 |
|
| 175 |
#ifdef WITH_FIPS |
| 176 |
if (FIPS_mode()) |
| 177 |
ECDSA_set_method(eckey, FIPS_ecdsa_openssl()); |
| 178 |
else |
| 179 |
ECDSA_set_method(eckey, ECDSA_OpenSSL()); |
| 180 |
#else |
| 181 |
ECDSA_set_method(eckey, ECDSA_OpenSSL()); |
| 182 |
#endif |
| 183 |
|
| 184 |
#else |
| 185 |
EC_KEY_set_method(eckey, EC_KEY_OpenSSL()); |
| 186 |
#endif |
| 187 |
|
| 188 |
// Perform the verify operation |
| 189 |
size_t len = pk->getOrderLength(); |
| 190 |
if (len == 0) |
| 191 |
{ |
| 192 |
ERROR_MSG("Could not get the order length"); |
| 193 |
return false; |
| 194 |
} |
| 195 |
if (signature.size() != 2 * len) |
| 196 |
{ |
| 197 |
ERROR_MSG("Invalid buffer length"); |
| 198 |
return false; |
| 199 |
} |
| 200 |
ECDSA_SIG* sig = ECDSA_SIG_new(); |
| 201 |
if (sig == NULL) |
| 202 |
{ |
| 203 |
ERROR_MSG("Could not create an ECDSA_SIG object"); |
| 204 |
return false; |
| 205 |
} |
| 206 |
const unsigned char *s = signature.const_byte_str(); |
| 207 |
BIGNUM* bn_r = BN_bin2bn(s, len, NULL); |
| 208 |
BIGNUM* bn_s = BN_bin2bn(s + len, len, NULL); |
| 209 |
if (bn_r == NULL || bn_s == NULL || |
| 210 |
!ECDSA_SIG_set0(sig, bn_r, bn_s)) |
| 211 |
{ |
| 212 |
ERROR_MSG("Could not add data to the ECDSA_SIG object"); |
| 213 |
ECDSA_SIG_free(sig); |
| 214 |
return false; |
| 215 |
} |
| 216 |
int ret = ECDSA_do_verify(originalData.const_byte_str(), originalData.size(), sig, eckey); |
| 217 |
if (ret != 1) |
| 218 |
{ |
| 219 |
if (ret < 0) |
| 220 |
ERROR_MSG("ECDSA verify failed (0x%08X)", ERR_get_error()); |
| 221 |
|
| 222 |
ECDSA_SIG_free(sig); |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
ECDSA_SIG_free(sig); |
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
bool OSSLECDSA::verifyInit(PublicKey* /*publicKey*/, const AsymMech::Type /*mechanism*/, |
| 231 |
const void* /* param = NULL */, const size_t /* paramLen = 0 */) |
| 232 |
{ |
| 233 |
ERROR_MSG("ECDSA does not support multi part verifying"); |
| 234 |
|
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
bool OSSLECDSA::verifyUpdate(const ByteString& /*originalData*/) |
| 239 |
{ |
| 240 |
ERROR_MSG("ECDSA does not support multi part verifying"); |
| 241 |
|
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
bool OSSLECDSA::verifyFinal(const ByteString& /*signature*/) |
| 246 |
{ |
| 247 |
ERROR_MSG("ECDSA does not support multi part verifying"); |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
// Encryption functions |
| 253 |
bool OSSLECDSA::encrypt(PublicKey* /*publicKey*/, const ByteString& /*data*/, |
| 254 |
ByteString& /*encryptedData*/, const AsymMech::Type /*padding*/) |
| 255 |
{ |
| 256 |
ERROR_MSG("ECDSA does not support encryption"); |
| 257 |
|
| 258 |
return false; |
| 259 |
} |
| 260 |
|
| 261 |
// Decryption functions |
| 262 |
bool OSSLECDSA::decrypt(PrivateKey* /*privateKey*/, const ByteString& /*encryptedData*/, |
| 263 |
ByteString& /*data*/, const AsymMech::Type /*padding*/) |
| 264 |
{ |
| 265 |
ERROR_MSG("ECDSA does not support decryption"); |
| 266 |
|
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
// Key factory |
| 271 |
bool OSSLECDSA::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */) |
| 272 |
{ |
| 273 |
// Check parameters |
| 274 |
if ((ppKeyPair == NULL) || |
| 275 |
(parameters == NULL)) |
| 276 |
{ |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
if (!parameters->areOfType(ECParameters::type)) |
| 281 |
{ |
| 282 |
ERROR_MSG("Invalid parameters supplied for ECDSA key generation"); |
| 283 |
|
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
ECParameters* params = (ECParameters*) parameters; |
| 288 |
|
| 289 |
// Generate the key-pair |
| 290 |
EC_KEY* eckey = EC_KEY_new(); |
| 291 |
if (eckey == NULL) |
| 292 |
{ |
| 293 |
ERROR_MSG("Failed to instantiate OpenSSL ECDSA object"); |
| 294 |
|
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
EC_GROUP* grp = OSSL::byteString2grp(params->getEC()); |
| 299 |
EC_KEY_set_group(eckey, grp); |
| 300 |
EC_GROUP_free(grp); |
| 301 |
|
| 302 |
if (!EC_KEY_generate_key(eckey)) |
| 303 |
{ |
| 304 |
ERROR_MSG("ECDSA key generation failed (0x%08X)", ERR_get_error()); |
| 305 |
|
| 306 |
EC_KEY_free(eckey); |
| 307 |
|
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
// Create an asymmetric key-pair object to return |
| 312 |
OSSLECKeyPair* kp = new OSSLECKeyPair(); |
| 313 |
|
| 314 |
((OSSLECPublicKey*) kp->getPublicKey())->setFromOSSL(eckey); |
| 315 |
((OSSLECPrivateKey*) kp->getPrivateKey())->setFromOSSL(eckey); |
| 316 |
|
| 317 |
*ppKeyPair = kp; |
| 318 |
|
| 319 |
// Release the key |
| 320 |
EC_KEY_free(eckey); |
| 321 |
|
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
unsigned long OSSLECDSA::getMinKeySize() |
| 326 |
{ |
| 327 |
// Smallest EC group is secp112r1 |
| 328 |
return 112; |
| 329 |
} |
| 330 |
|
| 331 |
unsigned long OSSLECDSA::getMaxKeySize() |
| 332 |
{ |
| 333 |
// Biggest EC group is secp521r1 |
| 334 |
return 521; |
| 335 |
} |
| 336 |
|
| 337 |
bool OSSLECDSA::reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData) |
| 338 |
{ |
| 339 |
// Check input |
| 340 |
if ((ppKeyPair == NULL) || |
| 341 |
(serialisedData.size() == 0)) |
| 342 |
{ |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
ByteString dPub = ByteString::chainDeserialise(serialisedData); |
| 347 |
ByteString dPriv = ByteString::chainDeserialise(serialisedData); |
| 348 |
|
| 349 |
OSSLECKeyPair* kp = new OSSLECKeyPair(); |
| 350 |
|
| 351 |
bool rv = true; |
| 352 |
|
| 353 |
if (!((ECPublicKey*) kp->getPublicKey())->deserialise(dPub)) |
| 354 |
{ |
| 355 |
rv = false; |
| 356 |
} |
| 357 |
|
| 358 |
if (!((ECPrivateKey*) kp->getPrivateKey())->deserialise(dPriv)) |
| 359 |
{ |
| 360 |
rv = false; |
| 361 |
} |
| 362 |
|
| 363 |
if (!rv) |
| 364 |
{ |
| 365 |
delete kp; |
| 366 |
|
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
*ppKeyPair = kp; |
| 371 |
|
| 372 |
return true; |
| 373 |
} |
| 374 |
|
| 375 |
bool OSSLECDSA::reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData) |
| 376 |
{ |
| 377 |
// Check input |
| 378 |
if ((ppPublicKey == NULL) || |
| 379 |
(serialisedData.size() == 0)) |
| 380 |
{ |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
OSSLECPublicKey* pub = new OSSLECPublicKey(); |
| 385 |
|
| 386 |
if (!pub->deserialise(serialisedData)) |
| 387 |
{ |
| 388 |
delete pub; |
| 389 |
|
| 390 |
return false; |
| 391 |
} |
| 392 |
|
| 393 |
*ppPublicKey = pub; |
| 394 |
|
| 395 |
return true; |
| 396 |
} |
| 397 |
|
| 398 |
bool OSSLECDSA::reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData) |
| 399 |
{ |
| 400 |
// Check input |
| 401 |
if ((ppPrivateKey == NULL) || |
| 402 |
(serialisedData.size() == 0)) |
| 403 |
{ |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
OSSLECPrivateKey* priv = new OSSLECPrivateKey(); |
| 408 |
|
| 409 |
if (!priv->deserialise(serialisedData)) |
| 410 |
{ |
| 411 |
delete priv; |
| 412 |
|
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
*ppPrivateKey = priv; |
| 417 |
|
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
PublicKey* OSSLECDSA::newPublicKey() |
| 422 |
{ |
| 423 |
return (PublicKey*) new OSSLECPublicKey(); |
| 424 |
} |
| 425 |
|
| 426 |
PrivateKey* OSSLECDSA::newPrivateKey() |
| 427 |
{ |
| 428 |
return (PrivateKey*) new OSSLECPrivateKey(); |
| 429 |
} |
| 430 |
|
| 431 |
AsymmetricParameters* OSSLECDSA::newParameters() |
| 432 |
{ |
| 433 |
return (AsymmetricParameters*) new ECParameters(); |
| 434 |
} |
| 435 |
|
| 436 |
bool OSSLECDSA::reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData) |
| 437 |
{ |
| 438 |
// Check input parameters |
| 439 |
if ((ppParams == NULL) || (serialisedData.size() == 0)) |
| 440 |
{ |
| 441 |
return false; |
| 442 |
} |
| 443 |
|
| 444 |
ECParameters* params = new ECParameters(); |
| 445 |
|
| 446 |
if (!params->deserialise(serialisedData)) |
| 447 |
{ |
| 448 |
delete params; |
| 449 |
|
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
*ppParams = params; |
| 454 |
|
| 455 |
return true; |
| 456 |
} |
| 457 |
#endif |