| 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 |
ECDSATests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the ECDSA class |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <utility> |
| 35 |
#include <vector> |
| 36 |
#include <cppunit/extensions/HelperMacros.h> |
| 37 |
#include "ECDSATests.h" |
| 38 |
#include "CryptoFactory.h" |
| 39 |
#include "RNG.h" |
| 40 |
#include "AsymmetricKeyPair.h" |
| 41 |
#include "AsymmetricAlgorithm.h" |
| 42 |
#ifdef WITH_ECC |
| 43 |
#include "ECParameters.h" |
| 44 |
#include "ECPublicKey.h" |
| 45 |
#include "ECPrivateKey.h" |
| 46 |
|
| 47 |
CPPUNIT_TEST_SUITE_REGISTRATION(ECDSATests); |
| 48 |
|
| 49 |
void ECDSATests::setUp() |
| 50 |
{ |
| 51 |
ecdsa = NULL; |
| 52 |
|
| 53 |
ecdsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::ECDSA); |
| 54 |
|
| 55 |
// Check the ECDSA object |
| 56 |
CPPUNIT_ASSERT(ecdsa != NULL); |
| 57 |
} |
| 58 |
|
| 59 |
void ECDSATests::tearDown() |
| 60 |
{ |
| 61 |
if (ecdsa != NULL) |
| 62 |
{ |
| 63 |
CryptoFactory::i()->recycleAsymmetricAlgorithm(ecdsa); |
| 64 |
} |
| 65 |
|
| 66 |
fflush(stdout); |
| 67 |
} |
| 68 |
|
| 69 |
void ECDSATests::testKeyGeneration() |
| 70 |
{ |
| 71 |
AsymmetricKeyPair* kp; |
| 72 |
|
| 73 |
// Curves to test |
| 74 |
std::vector<ByteString> curves; |
| 75 |
// Add X9.62 prime256v1 |
| 76 |
curves.push_back(ByteString("06082a8648ce3d030107")); |
| 77 |
// Add secp384r1 |
| 78 |
curves.push_back(ByteString("06052b81040022")); |
| 79 |
// Add secp521r1 |
| 80 |
curves.push_back(ByteString("06052b81040023")); |
| 81 |
|
| 82 |
for (std::vector<ByteString>::iterator c = curves.begin(); c != curves.end(); c++) |
| 83 |
{ |
| 84 |
// Set domain parameters |
| 85 |
ECParameters* p = new ECParameters; |
| 86 |
p->setEC(*c); |
| 87 |
|
| 88 |
// Generate key-pair |
| 89 |
CPPUNIT_ASSERT(ecdsa->generateKeyPair(&kp, p)); |
| 90 |
|
| 91 |
ECPublicKey* pub = (ECPublicKey*) kp->getPublicKey(); |
| 92 |
ECPrivateKey* priv = (ECPrivateKey*) kp->getPrivateKey(); |
| 93 |
|
| 94 |
CPPUNIT_ASSERT(pub->getEC() == *c); |
| 95 |
CPPUNIT_ASSERT(priv->getEC() == *c); |
| 96 |
|
| 97 |
ecdsa->recycleParameters(p); |
| 98 |
ecdsa->recycleKeyPair(kp); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void ECDSATests::testSerialisation() |
| 103 |
{ |
| 104 |
// Get prime256v1 domain parameters |
| 105 |
ECParameters* p = new ECParameters; |
| 106 |
p->setEC(ByteString("06082a8648ce3d030107")); |
| 107 |
|
| 108 |
// Serialise the parameters |
| 109 |
ByteString serialisedParams = p->serialise(); |
| 110 |
|
| 111 |
// Deserialise the parameters |
| 112 |
AsymmetricParameters* dEC; |
| 113 |
|
| 114 |
CPPUNIT_ASSERT(ecdsa->reconstructParameters(&dEC, serialisedParams)); |
| 115 |
|
| 116 |
CPPUNIT_ASSERT(dEC->areOfType(ECParameters::type)); |
| 117 |
|
| 118 |
ECParameters* ddEC = (ECParameters*) dEC; |
| 119 |
|
| 120 |
CPPUNIT_ASSERT(p->getEC() == ddEC->getEC()); |
| 121 |
|
| 122 |
// Generate a key-pair |
| 123 |
AsymmetricKeyPair* kp; |
| 124 |
|
| 125 |
CPPUNIT_ASSERT(ecdsa->generateKeyPair(&kp, dEC)); |
| 126 |
|
| 127 |
// Serialise the key-pair |
| 128 |
ByteString serialisedKP = kp->serialise(); |
| 129 |
|
| 130 |
// Deserialise the key-pair |
| 131 |
AsymmetricKeyPair* dKP; |
| 132 |
|
| 133 |
CPPUNIT_ASSERT(ecdsa->reconstructKeyPair(&dKP, serialisedKP)); |
| 134 |
|
| 135 |
// Check the deserialised key-pair |
| 136 |
ECPrivateKey* privKey = (ECPrivateKey*) kp->getPrivateKey(); |
| 137 |
ECPublicKey* pubKey = (ECPublicKey*) kp->getPublicKey(); |
| 138 |
|
| 139 |
ECPrivateKey* dPrivKey = (ECPrivateKey*) dKP->getPrivateKey(); |
| 140 |
ECPublicKey* dPubKey = (ECPublicKey*) dKP->getPublicKey(); |
| 141 |
|
| 142 |
CPPUNIT_ASSERT(privKey->getEC() == dPrivKey->getEC()); |
| 143 |
CPPUNIT_ASSERT(privKey->getD() == dPrivKey->getD()); |
| 144 |
|
| 145 |
CPPUNIT_ASSERT(pubKey->getEC() == dPubKey->getEC()); |
| 146 |
CPPUNIT_ASSERT(pubKey->getQ() == dPubKey->getQ()); |
| 147 |
|
| 148 |
ecdsa->recycleParameters(p); |
| 149 |
ecdsa->recycleParameters(dEC); |
| 150 |
ecdsa->recycleKeyPair(kp); |
| 151 |
ecdsa->recycleKeyPair(dKP); |
| 152 |
} |
| 153 |
|
| 154 |
void ECDSATests::testPKCS8() |
| 155 |
{ |
| 156 |
// Get prime256v1 domain parameters |
| 157 |
ECParameters* p = new ECParameters; |
| 158 |
p->setEC(ByteString("06082a8648ce3d030107")); |
| 159 |
|
| 160 |
// Generate a key-pair |
| 161 |
AsymmetricKeyPair* kp; |
| 162 |
|
| 163 |
CPPUNIT_ASSERT(ecdsa->generateKeyPair(&kp, p)); |
| 164 |
CPPUNIT_ASSERT(kp != NULL); |
| 165 |
|
| 166 |
ECPrivateKey* priv = (ECPrivateKey*) kp->getPrivateKey(); |
| 167 |
CPPUNIT_ASSERT(priv != NULL); |
| 168 |
|
| 169 |
// Encode and decode the private key |
| 170 |
ByteString pkcs8 = priv->PKCS8Encode(); |
| 171 |
CPPUNIT_ASSERT(pkcs8.size() != 0); |
| 172 |
|
| 173 |
ECPrivateKey* dPriv = (ECPrivateKey*) ecdsa->newPrivateKey(); |
| 174 |
CPPUNIT_ASSERT(dPriv != NULL); |
| 175 |
|
| 176 |
CPPUNIT_ASSERT(dPriv->PKCS8Decode(pkcs8)); |
| 177 |
|
| 178 |
CPPUNIT_ASSERT(priv->getEC() == dPriv->getEC()); |
| 179 |
CPPUNIT_ASSERT(priv->getD() == dPriv->getD()); |
| 180 |
|
| 181 |
ecdsa->recycleParameters(p); |
| 182 |
ecdsa->recycleKeyPair(kp); |
| 183 |
ecdsa->recyclePrivateKey(dPriv); |
| 184 |
} |
| 185 |
|
| 186 |
void ECDSATests::testSigningVerifying() |
| 187 |
{ |
| 188 |
AsymmetricKeyPair* kp; |
| 189 |
ECParameters *p; |
| 190 |
|
| 191 |
// Curves/Hashes to test |
| 192 |
std::vector<std::pair<ByteString, HashAlgo::Type> > totest; |
| 193 |
// Add X9.62 prime256v1 |
| 194 |
totest.push_back(std::make_pair(ByteString("06082a8648ce3d030107"), HashAlgo::SHA256)); |
| 195 |
// Add secp384r1 |
| 196 |
totest.push_back(std::make_pair(ByteString("06052b81040022"), HashAlgo::SHA384)); |
| 197 |
// Add secp521r1 |
| 198 |
totest.push_back(std::make_pair(ByteString("06052b81040023"), HashAlgo::SHA384)); |
| 199 |
|
| 200 |
for (std::vector<std::pair<ByteString, HashAlgo::Type> >::iterator k = totest.begin(); k != totest.end(); k++) |
| 201 |
{ |
| 202 |
// Get parameters |
| 203 |
p = new ECParameters; |
| 204 |
CPPUNIT_ASSERT(p != NULL); |
| 205 |
p->setEC(k->first); |
| 206 |
HashAlgorithm *hash; |
| 207 |
hash = CryptoFactory::i()->getHashAlgorithm(k->second); |
| 208 |
CPPUNIT_ASSERT(hash != NULL); |
| 209 |
|
| 210 |
// Generate key-pair |
| 211 |
CPPUNIT_ASSERT(ecdsa->generateKeyPair(&kp, p)); |
| 212 |
|
| 213 |
// Generate some data to sign |
| 214 |
ByteString dataToSign; |
| 215 |
|
| 216 |
RNG* rng = CryptoFactory::i()->getRNG(); |
| 217 |
CPPUNIT_ASSERT(rng != NULL); |
| 218 |
|
| 219 |
CPPUNIT_ASSERT(rng->generateRandom(dataToSign, 567)); |
| 220 |
|
| 221 |
// Sign the data |
| 222 |
CPPUNIT_ASSERT(hash->hashInit()); |
| 223 |
CPPUNIT_ASSERT(hash->hashUpdate(dataToSign)); |
| 224 |
ByteString hResult; |
| 225 |
CPPUNIT_ASSERT(hash->hashFinal(hResult)); |
| 226 |
ByteString sig; |
| 227 |
CPPUNIT_ASSERT(ecdsa->sign(kp->getPrivateKey(), hResult, sig, AsymMech::ECDSA)); |
| 228 |
|
| 229 |
// And verify it |
| 230 |
CPPUNIT_ASSERT(ecdsa->verify(kp->getPublicKey(), hResult, sig, AsymMech::ECDSA)); |
| 231 |
|
| 232 |
ecdsa->recycleKeyPair(kp); |
| 233 |
ecdsa->recycleParameters(p); |
| 234 |
CryptoFactory::i()->recycleHashAlgorithm(hash); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void ECDSATests::testSignVerifyKnownVector() |
| 239 |
{ |
| 240 |
ECPublicKey* pubKey1 = (ECPublicKey*) ecdsa->newPublicKey(); |
| 241 |
ECPublicKey* pubKey2 = (ECPublicKey*) ecdsa->newPublicKey(); |
| 242 |
ECPrivateKey* privKey1 = (ECPrivateKey*) ecdsa->newPrivateKey(); |
| 243 |
ECPrivateKey* privKey2 = (ECPrivateKey*) ecdsa->newPrivateKey(); |
| 244 |
HashAlgorithm* hash1 = CryptoFactory::i()->getHashAlgorithm(HashAlgo::SHA256); |
| 245 |
HashAlgorithm* hash2 = CryptoFactory::i()->getHashAlgorithm(HashAlgo::SHA384); |
| 246 |
|
| 247 |
// Reconstruct public and private key #1 |
| 248 |
ByteString ec1 = "06082a8648ce3d030107"; // X9.62 prime256v1 |
| 249 |
ByteString d1 = "dc51d3866a15bacde33d96f992fca99da7e6ef0934e7097559c27f1614c88a7f"; |
| 250 |
// add 04 (ASN_String) <len+1> 04 (UNCOMPRESSED) in front! |
| 251 |
ByteString q1 = "0441042442a5cc0ecd015fa3ca31dc8e2bbc70bf42d60cbca20085e0822cb04235e9706fc98bd7e50211a4a27102fa3549df79ebcb4bf246b80945cddfe7d509bbfd7d"; |
| 252 |
|
| 253 |
pubKey1->setEC(ec1); |
| 254 |
pubKey1->setQ(q1); |
| 255 |
privKey1->setEC(ec1); |
| 256 |
privKey1->setD(d1); |
| 257 |
CPPUNIT_ASSERT(hash1 != NULL); |
| 258 |
|
| 259 |
// Test with key #1 |
| 260 |
ByteString data1 = "616263"; // "abc" |
| 261 |
ByteString goodSignature1 = "cb28e0999b9c7715fd0a80d8e47a77079716cbbf917dd72e97566ea1c066957c86fa3bb4e26cad5bf90b7f81899256ce7594bb1ea0c89212748bff3b3d5b0315"; |
| 262 |
ByteString badSignature1 = "cb28e0999b9c7715fd0a80d8e47a77079716cbbf917dd72e97566ea1c066957c86fa3bb4e26cad5bf90b7f81899256ce7594bb1ea0c89212748bff3b3d5b0316"; |
| 263 |
|
| 264 |
// Reconstruct public and private key #2 |
| 265 |
ByteString ec2 = "06052b81040022"; // secp384r1 |
| 266 |
ByteString d2 = "0beb646634ba87735d77ae4809a0ebea865535de4c1e1dcb692e84708e81a5af62e528c38b2a81b35309668d73524d9f"; |
| 267 |
// add 04 (ASN_String) <len+1> 04 (UNCOMPRESSED) in front! |
| 268 |
ByteString q2 = "04610496281bf8dd5e0525ca049c048d345d3082968d10fedf5c5aca0c64e6465a97ea5ce10c9dfec21797415710721f437922447688ba94708eb6e2e4d59f6ab6d7edff9301d249fe49c33096655f5d502fad3d383b91c5e7edaa2b714cc99d5743ca"; |
| 269 |
|
| 270 |
pubKey2->setEC(ec2); |
| 271 |
pubKey2->setQ(q2); |
| 272 |
privKey2->setEC(ec2); |
| 273 |
privKey2->setD(d2); |
| 274 |
CPPUNIT_ASSERT(hash2 != NULL); |
| 275 |
|
| 276 |
// Test with key #2 |
| 277 |
ByteString data2 = "616263"; // "abc" |
| 278 |
ByteString goodSignature2 = "fb017b914e29149432d8bac29a514640b46f53ddab2c69948084e2930f1c8f7e08e07c9c63f2d21a07dcb56a6af56eb3b263a1305e057f984d38726a1b46874109f417bca112674c528262a40a629af1cbb9f516ce0fa7d2ff630863a00e8b9f"; |
| 279 |
ByteString badSignature2 = "fb017b914e29149432d8bac29a514640b46f53ddab2c69948084e2930f1c8f7e08e07c9c63f2d21a07dcb56a6af56eb3b263a1305e057f984d38726a1b46874109f417bca112674c528262a40a629af1cbb9f516ce0fa7d2ff630863a00e8b9e"; |
| 280 |
|
| 281 |
CPPUNIT_ASSERT(hash1->hashInit()); |
| 282 |
CPPUNIT_ASSERT(hash1->hashUpdate(data1)); |
| 283 |
ByteString hResult1; |
| 284 |
CPPUNIT_ASSERT(hash1->hashFinal(hResult1)); |
| 285 |
CPPUNIT_ASSERT(ecdsa->verify(pubKey1, hResult1, goodSignature1, AsymMech::ECDSA)); |
| 286 |
CPPUNIT_ASSERT(!ecdsa->verify(pubKey1, hResult1, badSignature1, AsymMech::ECDSA)); |
| 287 |
CPPUNIT_ASSERT(hash2->hashInit()); |
| 288 |
CPPUNIT_ASSERT(hash2->hashUpdate(data2)); |
| 289 |
ByteString hResult2; |
| 290 |
CPPUNIT_ASSERT(hash2->hashFinal(hResult2)); |
| 291 |
CPPUNIT_ASSERT(ecdsa->verify(pubKey2, hResult2, goodSignature2, AsymMech::ECDSA)); |
| 292 |
CPPUNIT_ASSERT(!ecdsa->verify(pubKey2, hResult2, badSignature2, AsymMech::ECDSA)); |
| 293 |
|
| 294 |
ecdsa->recyclePublicKey(pubKey1); |
| 295 |
ecdsa->recyclePublicKey(pubKey2); |
| 296 |
ecdsa->recyclePrivateKey(privKey1); |
| 297 |
ecdsa->recyclePrivateKey(privKey2); |
| 298 |
CryptoFactory::i()->recycleHashAlgorithm(hash1); |
| 299 |
CryptoFactory::i()->recycleHashAlgorithm(hash2); |
| 300 |
} |
| 301 |
#endif |