| 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 |
ECDHTests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the ECDH class |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <vector> |
| 35 |
#include <cppunit/extensions/HelperMacros.h> |
| 36 |
#include "ECDHTests.h" |
| 37 |
#include "CryptoFactory.h" |
| 38 |
#include "RNG.h" |
| 39 |
#include "AsymmetricKeyPair.h" |
| 40 |
#include "AsymmetricAlgorithm.h" |
| 41 |
#ifdef WITH_ECC |
| 42 |
#include "ECParameters.h" |
| 43 |
#include "ECPublicKey.h" |
| 44 |
#include "ECPrivateKey.h" |
| 45 |
|
| 46 |
CPPUNIT_TEST_SUITE_REGISTRATION(ECDHTests); |
| 47 |
|
| 48 |
void ECDHTests::setUp() |
| 49 |
{ |
| 50 |
ecdh = NULL; |
| 51 |
|
| 52 |
ecdh = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::ECDH); |
| 53 |
|
| 54 |
// Check the ECDH object |
| 55 |
CPPUNIT_ASSERT(ecdh != NULL); |
| 56 |
} |
| 57 |
|
| 58 |
void ECDHTests::tearDown() |
| 59 |
{ |
| 60 |
if (ecdh != NULL) |
| 61 |
{ |
| 62 |
CryptoFactory::i()->recycleAsymmetricAlgorithm(ecdh); |
| 63 |
} |
| 64 |
|
| 65 |
fflush(stdout); |
| 66 |
} |
| 67 |
|
| 68 |
void ECDHTests::testKeyGeneration() |
| 69 |
{ |
| 70 |
AsymmetricKeyPair* kp; |
| 71 |
|
| 72 |
// Curves to test |
| 73 |
std::vector<ByteString> curves; |
| 74 |
// Add X9.62 prime256v1 |
| 75 |
curves.push_back(ByteString("06082a8648ce3d030107")); |
| 76 |
// Add secp384r1 |
| 77 |
curves.push_back(ByteString("06052b81040022")); |
| 78 |
|
| 79 |
for (std::vector<ByteString>::iterator c = curves.begin(); c != curves.end(); c++) |
| 80 |
{ |
| 81 |
// Set domain parameters |
| 82 |
ECParameters* p = new ECParameters; |
| 83 |
p->setEC(*c); |
| 84 |
|
| 85 |
// Generate key-pair |
| 86 |
CPPUNIT_ASSERT(ecdh->generateKeyPair(&kp, p)); |
| 87 |
|
| 88 |
ECPublicKey* pub = (ECPublicKey*) kp->getPublicKey(); |
| 89 |
ECPrivateKey* priv = (ECPrivateKey*) kp->getPrivateKey(); |
| 90 |
|
| 91 |
CPPUNIT_ASSERT(pub->getEC() == *c); |
| 92 |
CPPUNIT_ASSERT(priv->getEC() == *c); |
| 93 |
|
| 94 |
ecdh->recycleParameters(p); |
| 95 |
ecdh->recycleKeyPair(kp); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void ECDHTests::testSerialisation() |
| 100 |
{ |
| 101 |
// Get prime256v1 domain parameters |
| 102 |
ECParameters* p = new ECParameters; |
| 103 |
p->setEC(ByteString("06082a8648ce3d030107")); |
| 104 |
|
| 105 |
// Serialise the parameters |
| 106 |
ByteString serialisedParams = p->serialise(); |
| 107 |
|
| 108 |
// Deserialise the parameters |
| 109 |
AsymmetricParameters* dEC; |
| 110 |
|
| 111 |
CPPUNIT_ASSERT(ecdh->reconstructParameters(&dEC, serialisedParams)); |
| 112 |
|
| 113 |
CPPUNIT_ASSERT(dEC->areOfType(ECParameters::type)); |
| 114 |
|
| 115 |
ECParameters* ddEC = (ECParameters*) dEC; |
| 116 |
|
| 117 |
CPPUNIT_ASSERT(p->getEC() == ddEC->getEC()); |
| 118 |
|
| 119 |
// Generate a key-pair |
| 120 |
AsymmetricKeyPair* kp; |
| 121 |
|
| 122 |
CPPUNIT_ASSERT(ecdh->generateKeyPair(&kp, dEC)); |
| 123 |
|
| 124 |
// Serialise the key-pair |
| 125 |
ByteString serialisedKP = kp->serialise(); |
| 126 |
|
| 127 |
// Deserialise the key-pair |
| 128 |
AsymmetricKeyPair* dKP; |
| 129 |
|
| 130 |
CPPUNIT_ASSERT(ecdh->reconstructKeyPair(&dKP, serialisedKP)); |
| 131 |
|
| 132 |
// Check the deserialised key-pair |
| 133 |
ECPrivateKey* privKey = (ECPrivateKey*) kp->getPrivateKey(); |
| 134 |
ECPublicKey* pubKey = (ECPublicKey*) kp->getPublicKey(); |
| 135 |
|
| 136 |
ECPrivateKey* dPrivKey = (ECPrivateKey*) dKP->getPrivateKey(); |
| 137 |
ECPublicKey* dPubKey = (ECPublicKey*) dKP->getPublicKey(); |
| 138 |
|
| 139 |
CPPUNIT_ASSERT(privKey->getEC() == dPrivKey->getEC()); |
| 140 |
CPPUNIT_ASSERT(privKey->getD() == dPrivKey->getD()); |
| 141 |
|
| 142 |
CPPUNIT_ASSERT(pubKey->getEC() == dPubKey->getEC()); |
| 143 |
CPPUNIT_ASSERT(pubKey->getQ() == dPubKey->getQ()); |
| 144 |
|
| 145 |
ecdh->recycleParameters(p); |
| 146 |
ecdh->recycleParameters(dEC); |
| 147 |
ecdh->recycleKeyPair(kp); |
| 148 |
ecdh->recycleKeyPair(dKP); |
| 149 |
} |
| 150 |
|
| 151 |
void ECDHTests::testPKCS8() |
| 152 |
{ |
| 153 |
// Get prime256v1 domain parameters |
| 154 |
ECParameters* p = new ECParameters; |
| 155 |
p->setEC(ByteString("06082a8648ce3d030107")); |
| 156 |
|
| 157 |
// Generate a key-pair |
| 158 |
AsymmetricKeyPair* kp; |
| 159 |
|
| 160 |
CPPUNIT_ASSERT(ecdh->generateKeyPair(&kp, p)); |
| 161 |
CPPUNIT_ASSERT(kp != NULL); |
| 162 |
|
| 163 |
ECPrivateKey* priv = (ECPrivateKey*) kp->getPrivateKey(); |
| 164 |
CPPUNIT_ASSERT(priv != NULL); |
| 165 |
|
| 166 |
// Encode and decode the private key |
| 167 |
ByteString pkcs8 = priv->PKCS8Encode(); |
| 168 |
CPPUNIT_ASSERT(pkcs8.size() != 0); |
| 169 |
|
| 170 |
ECPrivateKey* dPriv = (ECPrivateKey*) ecdh->newPrivateKey(); |
| 171 |
CPPUNIT_ASSERT(dPriv != NULL); |
| 172 |
|
| 173 |
CPPUNIT_ASSERT(dPriv->PKCS8Decode(pkcs8)); |
| 174 |
|
| 175 |
CPPUNIT_ASSERT(priv->getEC() == dPriv->getEC()); |
| 176 |
CPPUNIT_ASSERT(priv->getD() == dPriv->getD()); |
| 177 |
|
| 178 |
ecdh->recycleParameters(p); |
| 179 |
ecdh->recycleKeyPair(kp); |
| 180 |
ecdh->recyclePrivateKey(dPriv); |
| 181 |
} |
| 182 |
|
| 183 |
void ECDHTests::testDerivation() |
| 184 |
{ |
| 185 |
AsymmetricKeyPair* kpa; |
| 186 |
AsymmetricKeyPair* kpb; |
| 187 |
ECParameters* p; |
| 188 |
|
| 189 |
// Curves to test |
| 190 |
std::vector<ByteString> curves; |
| 191 |
// Add X9.62 prime256v1 |
| 192 |
curves.push_back(ByteString("06082a8648ce3d030107")); |
| 193 |
// Add secp384r1 |
| 194 |
curves.push_back(ByteString("06052b81040022")); |
| 195 |
|
| 196 |
for (std::vector<ByteString>::iterator c = curves.begin(); c != curves.end(); c++) |
| 197 |
{ |
| 198 |
// Get parameters |
| 199 |
p = new ECParameters; |
| 200 |
CPPUNIT_ASSERT(p != NULL); |
| 201 |
p->setEC(*c); |
| 202 |
|
| 203 |
// Generate key-pairs |
| 204 |
CPPUNIT_ASSERT(ecdh->generateKeyPair(&kpa, p)); |
| 205 |
CPPUNIT_ASSERT(ecdh->generateKeyPair(&kpb, p)); |
| 206 |
|
| 207 |
// Derive secrets |
| 208 |
SymmetricKey* sa; |
| 209 |
CPPUNIT_ASSERT(ecdh->deriveKey(&sa, kpb->getPublicKey(), kpa->getPrivateKey())); |
| 210 |
SymmetricKey* sb; |
| 211 |
CPPUNIT_ASSERT(ecdh->deriveKey(&sb, kpa->getPublicKey(), kpb->getPrivateKey())); |
| 212 |
|
| 213 |
// Must be the same |
| 214 |
CPPUNIT_ASSERT(sa->getKeyBits() == sb->getKeyBits()); |
| 215 |
|
| 216 |
// Clean up |
| 217 |
ecdh->recycleSymmetricKey(sa); |
| 218 |
ecdh->recycleSymmetricKey(sb); |
| 219 |
ecdh->recycleKeyPair(kpa); |
| 220 |
ecdh->recycleKeyPair(kpb); |
| 221 |
ecdh->recycleParameters(p); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void ECDHTests::testDeriveKnownVector() |
| 226 |
{ |
| 227 |
ECPublicKey* pubKeya = (ECPublicKey*) ecdh->newPublicKey(); |
| 228 |
ECPublicKey* pubKeyb = (ECPublicKey*) ecdh->newPublicKey(); |
| 229 |
ECPrivateKey* privKeya = (ECPrivateKey*) ecdh->newPrivateKey(); |
| 230 |
ECPrivateKey* privKeyb = (ECPrivateKey*) ecdh->newPrivateKey(); |
| 231 |
|
| 232 |
// Reconstruct public and private key for Alice |
| 233 |
ByteString ec = "06082a8648ce3d030107"; // X9.62 prime256v1 |
| 234 |
ByteString da = "c88f01f510d9ac3f70a292daa2316de544e9aab8afe84049c62a9c57862d1433"; |
| 235 |
// add 04 (ASN_String) <len+1> 04 (UNCOMPRESSED) in front! |
| 236 |
ByteString qa = "044104dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3"; |
| 237 |
|
| 238 |
pubKeya->setEC(ec); |
| 239 |
pubKeya->setQ(qa); |
| 240 |
privKeya->setEC(ec); |
| 241 |
privKeya->setD(da); |
| 242 |
|
| 243 |
// Reconstruct public and private key for Bob |
| 244 |
ByteString db = "c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53"; |
| 245 |
ByteString qb = "044104d12dfb5289c8d4f81208b70270398c342296970a0bccb74c736fc7554494bf6356fbf3ca366cc23e8157854c13c58d6aac23f046ada30f8353e74f33039872ab"; |
| 246 |
|
| 247 |
pubKeyb->setEC(ec); |
| 248 |
pubKeyb->setQ(qb); |
| 249 |
privKeyb->setEC(ec); |
| 250 |
privKeyb->setD(db); |
| 251 |
|
| 252 |
// Test |
| 253 |
ByteString expected = "d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"; |
| 254 |
SymmetricKey* sa; |
| 255 |
CPPUNIT_ASSERT(ecdh->deriveKey(&sa, pubKeya, privKeyb)); |
| 256 |
CPPUNIT_ASSERT(sa->getKeyBits() == expected); |
| 257 |
SymmetricKey* sb; |
| 258 |
CPPUNIT_ASSERT(ecdh->deriveKey(&sb, pubKeyb, privKeya)); |
| 259 |
CPPUNIT_ASSERT(sb->getKeyBits() == expected); |
| 260 |
|
| 261 |
ecdh->recyclePublicKey(pubKeya); |
| 262 |
ecdh->recyclePublicKey(pubKeyb); |
| 263 |
ecdh->recyclePrivateKey(privKeya); |
| 264 |
ecdh->recyclePrivateKey(privKeyb); |
| 265 |
ecdh->recycleSymmetricKey(sa); |
| 266 |
ecdh->recycleSymmetricKey(sb); |
| 267 |
} |
| 268 |
#endif |