NingSun | 0c89b3c | 2018-02-08 08:34:03 -0800 | [diff] [blame] | 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 | DHTests.cpp |
| 29 | |
| 30 | Contains test cases to test the DH class |
| 31 | *****************************************************************************/ |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <vector> |
| 35 | #include <cppunit/extensions/HelperMacros.h> |
| 36 | #include "DHTests.h" |
| 37 | #include "CryptoFactory.h" |
| 38 | #include "RNG.h" |
| 39 | #include "AsymmetricKeyPair.h" |
| 40 | #include "AsymmetricAlgorithm.h" |
| 41 | #include "DHParameters.h" |
| 42 | #include "DHPublicKey.h" |
| 43 | #include "DHPrivateKey.h" |
| 44 | |
| 45 | CPPUNIT_TEST_SUITE_REGISTRATION(DHTests); |
| 46 | |
| 47 | void DHTests::setUp() |
| 48 | { |
| 49 | dh = NULL; |
| 50 | |
| 51 | dh = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::DH); |
| 52 | |
| 53 | // Check the DH object |
| 54 | CPPUNIT_ASSERT(dh != NULL); |
| 55 | } |
| 56 | |
| 57 | void DHTests::tearDown() |
| 58 | { |
| 59 | if (dh != NULL) |
| 60 | { |
| 61 | CryptoFactory::i()->recycleAsymmetricAlgorithm(dh); |
| 62 | } |
| 63 | |
| 64 | fflush(stdout); |
| 65 | } |
| 66 | |
| 67 | void DHTests::testKeyGeneration() |
| 68 | { |
| 69 | AsymmetricKeyPair* kp; |
| 70 | |
| 71 | // Key sizes to test |
| 72 | std::vector<size_t> keySizes; |
| 73 | keySizes.push_back(1024); |
| 74 | |
| 75 | for (std::vector<size_t>::iterator k = keySizes.begin(); k != keySizes.end(); k++) |
| 76 | { |
| 77 | // Generate parameters |
| 78 | DHParameters* p; |
| 79 | AsymmetricParameters** ap = (AsymmetricParameters**) &p; |
| 80 | |
| 81 | CPPUNIT_ASSERT(dh->generateParameters(ap, (void*) *k)); |
| 82 | |
| 83 | // Generate key-pair |
| 84 | CPPUNIT_ASSERT(dh->generateKeyPair(&kp, p)); |
| 85 | |
| 86 | DHPublicKey* pub = (DHPublicKey*) kp->getPublicKey(); |
| 87 | DHPrivateKey* priv = (DHPrivateKey*) kp->getPrivateKey(); |
| 88 | |
| 89 | CPPUNIT_ASSERT(pub->getBitLength() == *k); |
| 90 | CPPUNIT_ASSERT(priv->getBitLength() == *k); |
| 91 | |
| 92 | dh->recycleKeyPair(kp); |
| 93 | |
| 94 | // Generate key-pair with a fixed private value length |
| 95 | p->setXBitLength(128); |
| 96 | CPPUNIT_ASSERT(dh->generateKeyPair(&kp, p)); |
| 97 | |
| 98 | priv = (DHPrivateKey*) kp->getPrivateKey(); |
| 99 | |
| 100 | CPPUNIT_ASSERT(priv->getX().bits() == 128); |
| 101 | |
| 102 | dh->recycleParameters(p); |
| 103 | dh->recycleKeyPair(kp); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void DHTests::testSerialisation() |
| 108 | { |
| 109 | // Generate 1024-bit parameters for testing |
| 110 | DHParameters* p; |
| 111 | AsymmetricParameters** ap = (AsymmetricParameters**) &p; |
| 112 | |
| 113 | CPPUNIT_ASSERT(dh->generateParameters(ap, (void*) 1024)); |
| 114 | |
| 115 | // Set a fixed private value length |
| 116 | p->setXBitLength(128); |
| 117 | |
| 118 | // Serialise the parameters |
| 119 | ByteString serialisedParams = p->serialise(); |
| 120 | |
| 121 | // Deserialise the parameters |
| 122 | AsymmetricParameters* dP; |
| 123 | |
| 124 | CPPUNIT_ASSERT(dh->reconstructParameters(&dP, serialisedParams)); |
| 125 | |
| 126 | CPPUNIT_ASSERT(dP->areOfType(DHParameters::type)); |
| 127 | |
| 128 | DHParameters* ddP = (DHParameters*) dP; |
| 129 | |
| 130 | CPPUNIT_ASSERT(p->getP() == ddP->getP()); |
| 131 | CPPUNIT_ASSERT(p->getG() == ddP->getG()); |
| 132 | CPPUNIT_ASSERT(p->getXBitLength() == ddP->getXBitLength()); |
| 133 | |
| 134 | // Generate a key-pair |
| 135 | AsymmetricKeyPair* kp; |
| 136 | |
| 137 | CPPUNIT_ASSERT(dh->generateKeyPair(&kp, dP)); |
| 138 | |
| 139 | // Serialise the key-pair |
| 140 | ByteString serialisedKP = kp->serialise(); |
| 141 | |
| 142 | // Deserialise the key-pair |
| 143 | AsymmetricKeyPair* dKP; |
| 144 | |
| 145 | CPPUNIT_ASSERT(dh->reconstructKeyPair(&dKP, serialisedKP)); |
| 146 | |
| 147 | // Check the deserialised key-pair |
| 148 | DHPrivateKey* privKey = (DHPrivateKey*) kp->getPrivateKey(); |
| 149 | DHPublicKey* pubKey = (DHPublicKey*) kp->getPublicKey(); |
| 150 | |
| 151 | DHPrivateKey* dPrivKey = (DHPrivateKey*) dKP->getPrivateKey(); |
| 152 | DHPublicKey* dPubKey = (DHPublicKey*) dKP->getPublicKey(); |
| 153 | |
| 154 | CPPUNIT_ASSERT(privKey->getP() == dPrivKey->getP()); |
| 155 | CPPUNIT_ASSERT(privKey->getG() == dPrivKey->getG()); |
| 156 | CPPUNIT_ASSERT(privKey->getX() == dPrivKey->getX()); |
| 157 | |
| 158 | CPPUNIT_ASSERT(pubKey->getP() == dPubKey->getP()); |
| 159 | CPPUNIT_ASSERT(pubKey->getG() == dPubKey->getG()); |
| 160 | CPPUNIT_ASSERT(pubKey->getY() == dPubKey->getY()); |
| 161 | |
| 162 | dh->recycleParameters(p); |
| 163 | dh->recycleParameters(dP); |
| 164 | dh->recycleKeyPair(kp); |
| 165 | dh->recycleKeyPair(dKP); |
| 166 | } |
| 167 | |
| 168 | void DHTests::testPKCS8() |
| 169 | { |
| 170 | // Generate 1024-bit parameters for testing |
| 171 | AsymmetricParameters* p; |
| 172 | |
| 173 | CPPUNIT_ASSERT(dh->generateParameters(&p, (void*) 1024)); |
| 174 | |
| 175 | // Generate a key-pair |
| 176 | AsymmetricKeyPair* kp; |
| 177 | |
| 178 | CPPUNIT_ASSERT(dh->generateKeyPair(&kp, p)); |
| 179 | CPPUNIT_ASSERT(kp != NULL); |
| 180 | |
| 181 | DHPrivateKey* priv = (DHPrivateKey*) kp->getPrivateKey(); |
| 182 | CPPUNIT_ASSERT(priv != NULL); |
| 183 | |
| 184 | // Encode and decode the private key |
| 185 | ByteString pkcs8 = priv->PKCS8Encode(); |
| 186 | CPPUNIT_ASSERT(pkcs8.size() != 0); |
| 187 | |
| 188 | DHPrivateKey* dPriv = (DHPrivateKey*) dh->newPrivateKey(); |
| 189 | CPPUNIT_ASSERT(dPriv != NULL); |
| 190 | |
| 191 | CPPUNIT_ASSERT(dPriv->PKCS8Decode(pkcs8)); |
| 192 | |
| 193 | |
| 194 | CPPUNIT_ASSERT(priv->getP() == dPriv->getP()); |
| 195 | CPPUNIT_ASSERT(priv->getG() == dPriv->getG()); |
| 196 | CPPUNIT_ASSERT(priv->getX() == dPriv->getX()); |
| 197 | |
| 198 | dh->recycleParameters(p); |
| 199 | dh->recycleKeyPair(kp); |
| 200 | dh->recyclePrivateKey(dPriv); |
| 201 | } |
| 202 | |
| 203 | void DHTests::testDerivation() |
| 204 | { |
| 205 | AsymmetricKeyPair* kpa; |
| 206 | AsymmetricKeyPair* kpb; |
| 207 | |
| 208 | // Key sizes to test |
| 209 | std::vector<size_t> keySizes; |
| 210 | keySizes.push_back(1024); |
| 211 | |
| 212 | for (std::vector<size_t>::iterator k = keySizes.begin(); k != keySizes.end(); k++) |
| 213 | { |
| 214 | // Generate parameters |
| 215 | AsymmetricParameters* p; |
| 216 | |
| 217 | CPPUNIT_ASSERT(dh->generateParameters(&p, (void*) *k)); |
| 218 | |
| 219 | // Generate key-pairs |
| 220 | CPPUNIT_ASSERT(dh->generateKeyPair(&kpa, p)); |
| 221 | CPPUNIT_ASSERT(dh->generateKeyPair(&kpb, p)); |
| 222 | |
| 223 | // Derive secrets |
| 224 | SymmetricKey* sa; |
| 225 | CPPUNIT_ASSERT(dh->deriveKey(&sa, kpb->getPublicKey(), kpa->getPrivateKey())); |
| 226 | SymmetricKey* sb; |
| 227 | CPPUNIT_ASSERT(dh->deriveKey(&sb, kpa->getPublicKey(), kpb->getPrivateKey())); |
| 228 | |
| 229 | // Must be the same |
| 230 | CPPUNIT_ASSERT(sa->getKeyBits() == sb->getKeyBits()); |
| 231 | |
| 232 | // Clean up |
| 233 | dh->recycleSymmetricKey(sa); |
| 234 | dh->recycleSymmetricKey(sb); |
| 235 | dh->recycleKeyPair(kpa); |
| 236 | dh->recycleKeyPair(kpb); |
| 237 | dh->recycleParameters(p); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void DHTests::testDeriveKnownVector() |
| 242 | { |
| 243 | // TODO |
| 244 | } |
| 245 | |