// Key factory bool BotanDH::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */) { // Check parameters if ((ppKeyPair == NULL) || (parameters == NULL)) { return false; } if (!parameters->areOfType(DHParameters::type)) { ERROR_MSG("Invalid parameters supplied for DH key generation"); return false; } DHParameters* params = (DHParameters*) parameters; // Generate the key-pair BotanDH_PrivateKey* dh = NULL; try { BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG(); // PKCS#3: 2^(l-1) <= x < 2^l Botan::BigInt x; if (params->getXBitLength() > 0) { x.randomize(*rng->getRNG(), params->getXBitLength()); } dh = new BotanDH_PrivateKey(*rng->getRNG(), Botan::DL_Group(BotanUtil::byteString2bigInt(params->getP()), BotanUtil::byteString2bigInt(params->getG())), x); } catch (std::exception& e) { ERROR_MSG("DH key generation failed with %s", e.what()); return false; } // Create an asymmetric key-pair object to return BotanDHKeyPair* kp = new BotanDHKeyPair(); ((BotanDHPublicKey*) kp->getPublicKey())->setFromBotan(dh); ((BotanDHPrivateKey*) kp->getPrivateKey())->setFromBotan(dh); *ppKeyPair = kp; // Release the key delete dh; return true; }
void DHTests::testSerialisation() { // Generate 1024-bit parameters for testing DHParameters* p; AsymmetricParameters** ap = (AsymmetricParameters**) &p; //CPPUNIT_ASSERT(dh->generateParameters(ap, (void*) 1024)); // changed for 512-bit for speed... #ifndef WITH_BOTAN CPPUNIT_ASSERT(dh->generateParameters(ap, (void*) 1024)); #else CPPUNIT_ASSERT(dh->generateParameters(ap, (void*) 512)); #endif // Set a fixed private value length p->setXBitLength(128); // Serialise the parameters ByteString serialisedParams = p->serialise(); // Deserialise the parameters AsymmetricParameters* dP; CPPUNIT_ASSERT(dh->reconstructParameters(&dP, serialisedParams)); CPPUNIT_ASSERT(dP->areOfType(DHParameters::type)); DHParameters* ddP = (DHParameters*) dP; CPPUNIT_ASSERT(p->getP() == ddP->getP()); CPPUNIT_ASSERT(p->getG() == ddP->getG()); CPPUNIT_ASSERT(p->getXBitLength() == ddP->getXBitLength()); // Generate a key-pair AsymmetricKeyPair* kp; CPPUNIT_ASSERT(dh->generateKeyPair(&kp, dP)); // Serialise the key-pair ByteString serialisedKP = kp->serialise(); // Deserialise the key-pair AsymmetricKeyPair* dKP; CPPUNIT_ASSERT(dh->reconstructKeyPair(&dKP, serialisedKP)); // Check the deserialised key-pair DHPrivateKey* privKey = (DHPrivateKey*) kp->getPrivateKey(); DHPublicKey* pubKey = (DHPublicKey*) kp->getPublicKey(); DHPrivateKey* dPrivKey = (DHPrivateKey*) dKP->getPrivateKey(); DHPublicKey* dPubKey = (DHPublicKey*) dKP->getPublicKey(); CPPUNIT_ASSERT(privKey->getP() == dPrivKey->getP()); CPPUNIT_ASSERT(privKey->getG() == dPrivKey->getG()); CPPUNIT_ASSERT(privKey->getX() == dPrivKey->getX()); CPPUNIT_ASSERT(pubKey->getP() == dPubKey->getP()); CPPUNIT_ASSERT(pubKey->getG() == dPubKey->getG()); CPPUNIT_ASSERT(pubKey->getY() == dPubKey->getY()); dh->recycleParameters(p); dh->recycleParameters(dP); dh->recycleKeyPair(kp); dh->recycleKeyPair(dKP); }