Пример #1
0
void DHTests::testKeyGeneration()
{
	AsymmetricKeyPair* kp;

	// Key sizes to test
	std::vector<size_t> keySizes;
#ifdef WITH_FIPS
	keySizes.push_back(1024);
#else
	keySizes.push_back(512);
	//keySizes.push_back(768);
	//keySizes.push_back(1024);
#endif

	for (std::vector<size_t>::iterator k = keySizes.begin(); k != keySizes.end(); k++)
	{
		// Generate parameters
		DHParameters* p;
		AsymmetricParameters** ap = (AsymmetricParameters**) &p;

		CPPUNIT_ASSERT(dh->generateParameters(ap, (void*) *k));

		// Generate key-pair
		CPPUNIT_ASSERT(dh->generateKeyPair(&kp, p));

		DHPublicKey* pub = (DHPublicKey*) kp->getPublicKey();
		DHPrivateKey* priv = (DHPrivateKey*) kp->getPrivateKey();

		CPPUNIT_ASSERT(pub->getBitLength() == *k);
		CPPUNIT_ASSERT(priv->getBitLength() == *k);

		dh->recycleKeyPair(kp);

		// Generate key-pair with a fixed private value length
		p->setXBitLength(128);
		CPPUNIT_ASSERT(dh->generateKeyPair(&kp, p));

		priv = (DHPrivateKey*) kp->getPrivateKey();

		CPPUNIT_ASSERT(priv->getX().bits() == 128);

		dh->recycleParameters(p);
		dh->recycleKeyPair(kp);
	}
}
Пример #2
0
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);
}