bool ElGamalValidate() { cout << "\nElGamal validation suite running...\n\n"; bool pass = true; { FileSource fc("elgc2048.dat", true, new HexDecoder); ElGamalDecryptor privC(fc); ElGamalEncryptor pubC(privC); privC.Precompute(); ByteQueue queue; privC.SavePrecomputation(queue); pubC.LoadPrecomputation(queue); pass = CryptoSystemValidate(privC, pubC) && pass; } { LC_RNG rng(4780); cout << "Generating new encryption key..." << endl; ElGamalDecryptor privC(rng, 128); ElGamalEncryptor pubC(privC); pass = CryptoSystemValidate(privC, pubC) && pass; } return pass; }
bool EC2NValidate() { cout << "\nEC2N validation suite running...\n\n"; LC_RNG rng(5667); ECDecryptor<EC2N> cpriv(rng, ASN1::sect193r1()); ECEncryptor<EC2N> cpub(cpriv); ByteQueue bq; cpriv.DEREncode(bq); cpub.SetEncodeAsOID(true); cpub.DEREncode(bq); ECSigner<EC2N, SHA> spriv(bq); ECVerifier<EC2N, SHA> spub(bq); ECDHC<EC2N> ecdhc(ASN1::sect193r1()); ECMQVC<EC2N> ecmqvc(ASN1::sect193r1()); spriv.Precompute(); ByteQueue queue; spriv.SavePrecomputation(queue); spub.LoadPrecomputation(queue); bool pass = SignatureValidate(spriv, spub); pass = CryptoSystemValidate(cpriv, cpub) && pass; pass = SimpleKeyAgreementValidate(ecdhc) && pass; pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass; cout << "Turning on point compression..." << endl; cpriv.SetPointCompression(true); cpub.SetPointCompression(true); ecdhc.SetPointCompression(true); ecmqvc.SetPointCompression(true); pass = CryptoSystemValidate(cpriv, cpub) && pass; pass = SimpleKeyAgreementValidate(ecdhc) && pass; pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass; #if 0 // TODO: turn this back on when I make EC2N faster for pentanomial basis cout << "Testing SEC 2 recommended curves..." << endl; OID oid; while (!(oid = ECParameters<EC2N>::GetNextRecommendedParametersOID(oid)).m_values.empty()) { ECParameters<EC2N> params(oid); bool fail = !params.ValidateParameters(rng); cout << (fail ? "FAILED" : "passed") << " " << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl; pass = pass && !fail; } #endif return pass; }
bool ValidateEC2N() { cout << "\nEC2N validation suite running...\n\n"; ECIES<EC2N>::Decryptor cpriv(GlobalRNG(), ASN1::sect193r1()); ECIES<EC2N>::Encryptor cpub(cpriv); ByteQueue bq; cpriv.DEREncode(bq); cpub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true); cpub.DEREncode(bq); ECDSA<EC2N, SHA>::Signer spriv(bq); ECDSA<EC2N, SHA>::Verifier spub(bq); ECDH<EC2N>::Domain ecdhc(ASN1::sect193r1()); ECMQV<EC2N>::Domain ecmqvc(ASN1::sect193r1()); spriv.AccessKey().Precompute(); ByteQueue queue; spriv.AccessKey().SavePrecomputation(queue); spriv.AccessKey().LoadPrecomputation(queue); bool pass = SignatureValidate(spriv, spub); pass = CryptoSystemValidate(cpriv, cpub) && pass; pass = SimpleKeyAgreementValidate(ecdhc) && pass; pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass; cout << "Turning on point compression..." << endl; cpriv.AccessKey().AccessGroupParameters().SetPointCompression(true); cpub.AccessKey().AccessGroupParameters().SetPointCompression(true); ecdhc.AccessGroupParameters().SetPointCompression(true); ecmqvc.AccessGroupParameters().SetPointCompression(true); pass = CryptoSystemValidate(cpriv, cpub) && pass; pass = SimpleKeyAgreementValidate(ecdhc) && pass; pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass; #if 0 // TODO: turn this back on when I make EC2N faster for pentanomial basis cout << "Testing SEC 2 recommended curves..." << endl; OID oid; while (!(oid = DL_GroupParameters_EC<EC2N>::GetNextRecommendedParametersOID(oid)).m_values.empty()) { DL_GroupParameters_EC<EC2N> params(oid); bool fail = !params.Validate(GlobalRNG(), 2); cout << (fail ? "FAILED" : "passed") << " " << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl; pass = pass && !fail; } #endif return pass; }
bool ValidateECP() { std::cout << "\nECP validation suite running...\n\n"; ECIES<ECP>::Decryptor cpriv(GlobalRNG(), ASN1::secp192r1()); ECIES<ECP>::Encryptor cpub(cpriv); ByteQueue bq; cpriv.GetKey().DEREncode(bq); cpub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true); cpub.GetKey().DEREncode(bq); ECDSA<ECP, SHA>::Signer spriv(bq); ECDSA<ECP, SHA>::Verifier spub(bq); ECDH<ECP>::Domain ecdhc(ASN1::secp192r1()); ECMQV<ECP>::Domain ecmqvc(ASN1::secp192r1()); spriv.AccessKey().Precompute(); ByteQueue queue; spriv.AccessKey().SavePrecomputation(queue); spriv.AccessKey().LoadPrecomputation(queue); bool pass = SignatureValidate(spriv, spub); cpub.AccessKey().Precompute(); cpriv.AccessKey().Precompute(); pass = CryptoSystemValidate(cpriv, cpub) && pass; pass = SimpleKeyAgreementValidate(ecdhc) && pass; pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass; std::cout << "Turning on point compression..." << std::endl; cpriv.AccessKey().AccessGroupParameters().SetPointCompression(true); cpub.AccessKey().AccessGroupParameters().SetPointCompression(true); ecdhc.AccessGroupParameters().SetPointCompression(true); ecmqvc.AccessGroupParameters().SetPointCompression(true); pass = CryptoSystemValidate(cpriv, cpub) && pass; pass = SimpleKeyAgreementValidate(ecdhc) && pass; pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass; std::cout << "Testing SEC 2, NIST, and Brainpool recommended curves..." << std::endl; OID oid; while (!(oid = DL_GroupParameters_EC<ECP>::GetNextRecommendedParametersOID(oid)).m_values.empty()) { DL_GroupParameters_EC<ECP> params(oid); bool fail = !params.Validate(GlobalRNG(), 2); std::cout << (fail ? "FAILED" : "passed") << " " << std::dec << params.GetCurve().GetField().MaxElementBitLength() << " bits" << std::endl; pass = pass && !fail; } return pass; }
bool BlumGoldwasserValidate() { cout << "\nBlumGoldwasser validation suite running...\n\n"; FileSource f("blum512.dat", true, new HexDecoder); BlumGoldwasserPrivateKey priv(f); BlumGoldwasserPublicKey pub(priv); return CryptoSystemValidate(priv, pub); }
bool ValidateDLIES() { cout << "\nDLIES validation suite running...\n\n"; bool pass = true; { FileSource fc("TestData/dlie1024.dat", true, new HexDecoder); DLIES<>::Decryptor privC(fc); DLIES<>::Encryptor pubC(privC); pass = CryptoSystemValidate(privC, pubC) && pass; } { cout << "Generating new encryption key..." << endl; DLIES<>::GroupParameters gp; gp.GenerateRandomWithKeySize(GlobalRNG(), 128); DLIES<>::Decryptor decryptor; decryptor.AccessKey().GenerateRandom(GlobalRNG(), gp); DLIES<>::Encryptor encryptor(decryptor); pass = CryptoSystemValidate(decryptor, encryptor) && pass; } return pass; }
bool ValidateElGamal() { cout << "\nElGamal validation suite running...\n\n"; bool pass = true; { FileSource fc("TestData/elgc1024.dat", true, new HexDecoder); ElGamalDecryptor privC(fc); ElGamalEncryptor pubC(privC); privC.AccessKey().Precompute(); ByteQueue queue; privC.AccessKey().SavePrecomputation(queue); privC.AccessKey().LoadPrecomputation(queue); pass = CryptoSystemValidate(privC, pubC) && pass; } return pass; }
bool ValidateRabin() { cout << "\nRabin validation suite running...\n\n"; bool pass=true; { FileSource f("TestData/rabi1024.dat", true, new HexDecoder); RabinSS<PSSR, SHA>::Signer priv(f); RabinSS<PSSR, SHA>::Verifier pub(priv); pass = SignatureValidate(priv, pub) && pass; } { RabinES<OAEP<SHA> >::Decryptor priv(GlobalRNG(), 512); RabinES<OAEP<SHA> >::Encryptor pub(priv); pass = CryptoSystemValidate(priv, pub) && pass; } return pass; }
bool ValidateLUC_DL() { cout << "\nLUC-HMP validation suite running...\n\n"; FileSource f("TestData/lucs512.dat", true, new HexDecoder); LUC_HMP<SHA>::Signer privS(f); LUC_HMP<SHA>::Verifier pubS(privS); bool pass = SignatureValidate(privS, pubS); cout << "\nLUC-IES validation suite running...\n\n"; FileSource fc("TestData/lucc512.dat", true, new HexDecoder); LUC_IES<>::Decryptor privC(fc); LUC_IES<>::Encryptor pubC(privC); pass = CryptoSystemValidate(privC, pubC) && pass; return pass; }
bool ValidateLUC() { cout << "\nLUC validation suite running...\n\n"; bool pass=true; { FileSource f("TestData/luc1024.dat", true, new HexDecoder); LUCSSA_PKCS1v15_SHA_Signer priv(f); LUCSSA_PKCS1v15_SHA_Verifier pub(priv); pass = SignatureValidate(priv, pub) && pass; } { LUCES_OAEP_SHA_Decryptor priv(GlobalRNG(), 512); LUCES_OAEP_SHA_Encryptor pub(priv); pass = CryptoSystemValidate(priv, pub) && pass; } return pass; }
bool LUCELGValidate() { cout << "\nLUCELG validation suite running...\n\n"; FileSource f("lucs512.dat", true, new HexDecoder); LUCELG_Signer<SHA> privS(f); LUCELG_Verifier<SHA> pubS(privS); bool pass = SignatureValidate(privS, pubS); FileSource fc("lucc512.dat", true, new HexDecoder); LUCELG_Decryptor privC(fc); LUCELG_Encryptor pubC(privC); pass = CryptoSystemValidate(privC, pubC) && pass; return pass; }
bool RabinValidate() { cout << "\nRabin validation suite running...\n\n"; bool pass=true; { FileSource f("rabi512.dat", true, new HexDecoder); RabinSignerWith(SHA) priv(f); RabinVerifierWith(SHA) pub(priv); pass = SignatureValidate(priv, pub) && pass; } { FileSource f("rabi512.dat", true, new HexDecoder); RabinDecryptor priv(f); RabinEncryptor pub(priv); pass = CryptoSystemValidate(priv, pub) && pass; } return pass; }
bool LUCValidate() { cout << "\nLUC validation suite running...\n\n"; bool pass=true; { FileSource f("luc512.dat", true, new HexDecoder); LUCSSA_PKCS1v15_SHA_Signer priv(f); LUCSSA_PKCS1v15_SHA_Verifier pub(priv); pass = SignatureValidate(priv, pub) && pass; } { FileSource f("luc512.dat", true, new HexDecoder); LUCES_OAEP_SHA_Decryptor priv(f); LUCES_OAEP_SHA_Encryptor pub(priv); pass = CryptoSystemValidate(priv, pub) && pass; } return pass; }
bool ValidateRSA() { cout << "\nRSA validation suite running...\n\n"; byte out[100], outPlain[100]; bool pass = true, fail; { static const char plain[] = "Everyone gets Friday off."; byte *signature = (byte *) "\x05\xfa\x6a\x81\x2f\xc7\xdf\x8b\xf4\xf2\x54\x25\x09\xe0\x3e\x84" "\x6e\x11\xb9\xc6\x20\xbe\x20\x09\xef\xb4\x40\xef\xbc\xc6\x69\x21" "\x69\x94\xac\x04\xf3\x41\xb5\x7d\x05\x20\x2d\x42\x8f\xb2\xa2\x7b" "\x5c\x77\xdf\xd9\xb1\x5b\xfc\x3d\x55\x93\x53\x50\x34\x10\xc1\xe1"; FileSource keys("TestData/rsa512a.dat", true, new HexDecoder); Weak::RSASSA_PKCS1v15_MD2_Signer rsaPriv(keys); Weak::RSASSA_PKCS1v15_MD2_Verifier rsaPub(rsaPriv); size_t signatureLength = rsaPriv.SignMessage(GlobalRNG(), (byte *)plain, strlen(plain), out); fail = !VerifyBufsEqual(signature, out, 64); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "signature check against test vector\n"; fail = !rsaPub.VerifyMessage((byte *)plain, strlen(plain), out, signatureLength); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "verification check against test vector\n"; out[10]++; fail = rsaPub.VerifyMessage((byte *)plain, strlen(plain), out, signatureLength); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "invalid signature verification\n"; } { FileSource keys("TestData/rsa1024.dat", true, new HexDecoder); RSAES_PKCS1v15_Decryptor rsaPriv(keys); RSAES_PKCS1v15_Encryptor rsaPub(rsaPriv); pass = CryptoSystemValidate(rsaPriv, rsaPub) && pass; } { RSAES<OAEP<SHA> >::Decryptor rsaPriv(GlobalRNG(), 512); RSAES<OAEP<SHA> >::Encryptor rsaPub(rsaPriv); pass = CryptoSystemValidate(rsaPriv, rsaPub) && pass; } { byte *plain = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; byte *encrypted = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a" "\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4" "\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52" "\x62\x51"; byte *oaepSeed = (byte *) "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2" "\xf0\x6c\xb5\x8f"; ByteQueue bq; bq.Put(oaepSeed, 20); FixedRNG rng(bq); FileSource privFile("TestData/rsa400pv.dat", true, new HexDecoder); FileSource pubFile("TestData/rsa400pb.dat", true, new HexDecoder); RSAES_OAEP_SHA_Decryptor rsaPriv; rsaPriv.AccessKey().BERDecodePrivateKey(privFile, false, 0); RSAES_OAEP_SHA_Encryptor rsaPub(pubFile); memset(out, 0, 50); memset(outPlain, 0, 8); rsaPub.Encrypt(rng, plain, 8, out); DecodingResult result = rsaPriv.FixedLengthDecrypt(GlobalRNG(), encrypted, outPlain); fail = !result.isValidCoding || (result.messageLength!=8) || !VerifyBufsEqual(out, encrypted, 50) || !VerifyBufsEqual(plain, outPlain, 8); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "PKCS 2.0 encryption and decryption\n"; } return pass; }
bool RSAValidate() { cout << "\nRSA validation suite running...\n\n"; byte out[100], outPlain[100]; unsigned int outLen; bool pass = true, fail; try { { char *plain = "Everyone gets Friday off."; byte *signature = (byte *) "\x05\xfa\x6a\x81\x2f\xc7\xdf\x8b\xf4\xf2\x54\x25\x09\xe0\x3e\x84" "\x6e\x11\xb9\xc6\x20\xbe\x20\x09\xef\xb4\x40\xef\xbc\xc6\x69\x21" "\x69\x94\xac\x04\xf3\x41\xb5\x7d\x05\x20\x2d\x42\x8f\xb2\xa2\x7b" "\x5c\x77\xdf\xd9\xb1\x5b\xfc\x3d\x55\x93\x53\x50\x34\x10\xc1\xe1"; LC_RNG rng(765); FileSource keys("rsa512a.dat", true, new HexDecoder); RSASSA_PKCS1v15_MD2_Signer rsaPriv(keys); RSASSA_PKCS1v15_MD2_Verifier rsaPub(rsaPriv); rsaPriv.SignMessage(rng, (byte *)plain, strlen(plain), out); fail = memcmp(signature, out, 64) != 0; pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "signature check against test vector\n"; fail = !rsaPub.VerifyMessage((byte *)plain, strlen(plain), out); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "verification check against test vector\n"; out[10]++; fail = rsaPub.VerifyMessage((byte *)plain, strlen(plain), out); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "invalid signature verification\n"; } { FileSource keys("rsa512.dat", true, new HexDecoder); RSAES_PKCS1v15_Decryptor rsaPriv(keys); RSAES_PKCS1v15_Encryptor rsaPub(rsaPriv); pass = CryptoSystemValidate(rsaPriv, rsaPub) && pass; } { byte *plain = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; byte *encrypted = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a" "\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4" "\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52" "\x62\x51"; byte *oaepSeed = (byte *) "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2" "\xf0\x6c\xb5\x8f"; ByteQueue bq; bq.Put(oaepSeed, 20); FixedRNG rng(bq); FileSource privFile("rsa400pv.dat", true, new HexDecoder); FileSource pubFile("rsa400pb.dat", true, new HexDecoder); RSAES_OAEP_SHA_Decryptor rsaPriv(privFile); RSAES_OAEP_SHA_Encryptor rsaPub(pubFile); memset(out, 0, 50); memset(outPlain, 0, 8); rsaPub.Encrypt(rng, plain, 8, out); outLen = rsaPriv.Decrypt(encrypted, outPlain); fail = (outLen!=8) || memcmp(out, encrypted, 50) || memcmp(plain, outPlain, 8); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "PKCS 2.0 encryption and decryption\n"; } } catch (BERDecodeErr) { cout << "FAILED Error decoding RSA key\n"; pass = false; } return pass; }