| 1 |
/* |
| 2 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 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 |
BotanECDH.cpp |
| 29 |
|
| 30 |
Botan ECDH asymmetric algorithm implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#ifdef WITH_ECC |
| 35 |
#include "log.h" |
| 36 |
#include "BotanECDH.h" |
| 37 |
#include "BotanRNG.h" |
| 38 |
#include "CryptoFactory.h" |
| 39 |
#include "BotanCryptoFactory.h" |
| 40 |
#include "ECParameters.h" |
| 41 |
#include "BotanECDHKeyPair.h" |
| 42 |
#include "BotanUtil.h" |
| 43 |
#include <algorithm> |
| 44 |
#include <botan/dl_group.h> |
| 45 |
#include <botan/ecdh.h> |
| 46 |
#include <botan/pubkey.h> |
| 47 |
#include <botan/version.h> |
| 48 |
|
| 49 |
// Signing functions |
| 50 |
bool BotanECDH::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 BotanECDH::signUpdate(const ByteString& /*dataToSign*/) |
| 59 |
{ |
| 60 |
ERROR_MSG("ECDH does not support signing"); |
| 61 |
|
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
bool BotanECDH::signFinal(ByteString& /*signature*/) |
| 66 |
{ |
| 67 |
ERROR_MSG("ECDH does not support signing"); |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
// Verification functions |
| 73 |
bool BotanECDH::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 BotanECDH::verifyUpdate(const ByteString& /*originalData*/) |
| 82 |
{ |
| 83 |
ERROR_MSG("ECDH does not support verifying"); |
| 84 |
|
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
bool BotanECDH::verifyFinal(const ByteString& /*signature*/) |
| 89 |
{ |
| 90 |
ERROR_MSG("ECDH does not support verifying"); |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
// Encryption functions |
| 96 |
bool BotanECDH::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 BotanECDH::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 BotanECDH::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 |
Botan::ECDH_PrivateKey* eckp = NULL; |
| 134 |
try |
| 135 |
{ |
| 136 |
BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG(); |
| 137 |
eckp = new Botan::ECDH_PrivateKey(*rng->getRNG(), BotanUtil::byteString2ECGroup(params->getEC())); |
| 138 |
} |
| 139 |
catch (...) |
| 140 |
{ |
| 141 |
ERROR_MSG("ECDH key generation failed"); |
| 142 |
|
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
// Create an asymmetric key-pair object to return |
| 147 |
BotanECDHKeyPair* kp = new BotanECDHKeyPair(); |
| 148 |
|
| 149 |
((BotanECDHPublicKey*) kp->getPublicKey())->setFromBotan(eckp); |
| 150 |
((BotanECDHPrivateKey*) kp->getPrivateKey())->setFromBotan(eckp); |
| 151 |
|
| 152 |
*ppKeyPair = kp; |
| 153 |
|
| 154 |
// Release the key |
| 155 |
delete eckp; |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
bool BotanECDH::deriveKey(SymmetricKey **ppSymmetricKey, PublicKey* publicKey, PrivateKey* privateKey) |
| 161 |
{ |
| 162 |
// Check parameters |
| 163 |
if ((ppSymmetricKey == NULL) || |
| 164 |
(publicKey == NULL) || |
| 165 |
(privateKey == NULL)) |
| 166 |
{ |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
// Get keys |
| 171 |
Botan::ECDH_PublicKey* pub = ((BotanECDHPublicKey*) publicKey)->getBotanKey(); |
| 172 |
Botan::ECDH_PrivateKey* priv = ((BotanECDHPrivateKey*) privateKey)->getBotanKey(); |
| 173 |
if (pub == NULL || priv == NULL) |
| 174 |
{ |
| 175 |
ERROR_MSG("Failed to get Botan ECDH keys"); |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
// Derive the secret |
| 181 |
Botan::SymmetricKey sk; |
| 182 |
try |
| 183 |
{ |
| 184 |
BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG(); |
| 185 |
Botan::PK_Key_Agreement ka(*priv, *rng->getRNG(), "Raw"); |
| 186 |
sk = ka.derive_key(0, pub->public_value()); |
| 187 |
} |
| 188 |
catch (...) |
| 189 |
{ |
| 190 |
ERROR_MSG("Botan ECDH key agreement failed"); |
| 191 |
|
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
ByteString secret; |
| 196 |
|
| 197 |
// We compensate that Botan removes leading zeros |
| 198 |
int size = ((BotanECDHPublicKey *)publicKey)->getOrderLength(); |
| 199 |
int keySize = sk.length(); |
| 200 |
secret.wipe(size); |
| 201 |
memcpy(&secret[0] + size - keySize, sk.begin(), keySize); |
| 202 |
|
| 203 |
*ppSymmetricKey = new SymmetricKey(secret.size() * 8); |
| 204 |
if (*ppSymmetricKey == NULL) |
| 205 |
{ |
| 206 |
ERROR_MSG("Can't create ECDH secret"); |
| 207 |
|
| 208 |
return false; |
| 209 |
} |
| 210 |
if (!(*ppSymmetricKey)->setKeyBits(secret)) |
| 211 |
{ |
| 212 |
delete *ppSymmetricKey; |
| 213 |
*ppSymmetricKey = NULL; |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
unsigned long BotanECDH::getMinKeySize() |
| 221 |
{ |
| 222 |
// Smallest EC group is secp112r1 |
| 223 |
return 112; |
| 224 |
} |
| 225 |
|
| 226 |
unsigned long BotanECDH::getMaxKeySize() |
| 227 |
{ |
| 228 |
// Biggest EC group is secp521r1 |
| 229 |
return 521; |
| 230 |
} |
| 231 |
|
| 232 |
bool BotanECDH::reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData) |
| 233 |
{ |
| 234 |
// Check input |
| 235 |
if ((ppKeyPair == NULL) || |
| 236 |
(serialisedData.size() == 0)) |
| 237 |
{ |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
ByteString dPub = ByteString::chainDeserialise(serialisedData); |
| 242 |
ByteString dPriv = ByteString::chainDeserialise(serialisedData); |
| 243 |
|
| 244 |
BotanECDHKeyPair* kp = new BotanECDHKeyPair(); |
| 245 |
|
| 246 |
bool rv = true; |
| 247 |
|
| 248 |
if (!((ECPublicKey*) kp->getPublicKey())->deserialise(dPub)) |
| 249 |
{ |
| 250 |
rv = false; |
| 251 |
} |
| 252 |
|
| 253 |
if (!((ECPrivateKey*) kp->getPrivateKey())->deserialise(dPriv)) |
| 254 |
{ |
| 255 |
rv = false; |
| 256 |
} |
| 257 |
|
| 258 |
if (!rv) |
| 259 |
{ |
| 260 |
delete kp; |
| 261 |
|
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
*ppKeyPair = kp; |
| 266 |
|
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
bool BotanECDH::reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData) |
| 271 |
{ |
| 272 |
// Check input |
| 273 |
if ((ppPublicKey == NULL) || |
| 274 |
(serialisedData.size() == 0)) |
| 275 |
{ |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
BotanECDHPublicKey* pub = new BotanECDHPublicKey(); |
| 280 |
|
| 281 |
if (!pub->deserialise(serialisedData)) |
| 282 |
{ |
| 283 |
delete pub; |
| 284 |
|
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
*ppPublicKey = pub; |
| 289 |
|
| 290 |
return true; |
| 291 |
} |
| 292 |
|
| 293 |
bool BotanECDH::reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData) |
| 294 |
{ |
| 295 |
// Check input |
| 296 |
if ((ppPrivateKey == NULL) || |
| 297 |
(serialisedData.size() == 0)) |
| 298 |
{ |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
BotanECDHPrivateKey* priv = new BotanECDHPrivateKey(); |
| 303 |
|
| 304 |
if (!priv->deserialise(serialisedData)) |
| 305 |
{ |
| 306 |
delete priv; |
| 307 |
|
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
*ppPrivateKey = priv; |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
PublicKey* BotanECDH::newPublicKey() |
| 317 |
{ |
| 318 |
return (PublicKey*) new BotanECDHPublicKey(); |
| 319 |
} |
| 320 |
|
| 321 |
PrivateKey* BotanECDH::newPrivateKey() |
| 322 |
{ |
| 323 |
return (PrivateKey*) new BotanECDHPrivateKey(); |
| 324 |
} |
| 325 |
|
| 326 |
AsymmetricParameters* BotanECDH::newParameters() |
| 327 |
{ |
| 328 |
return (AsymmetricParameters*) new ECParameters(); |
| 329 |
} |
| 330 |
|
| 331 |
bool BotanECDH::reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData) |
| 332 |
{ |
| 333 |
// Check input parameters |
| 334 |
if ((ppParams == NULL) || (serialisedData.size() == 0)) |
| 335 |
{ |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
ECParameters* params = new ECParameters(); |
| 340 |
|
| 341 |
if (!params->deserialise(serialisedData)) |
| 342 |
{ |
| 343 |
delete params; |
| 344 |
|
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
*ppParams = params; |
| 349 |
|
| 350 |
return true; |
| 351 |
} |
| 352 |
#endif |