void SignatureKnownAnswerTest(const char *key, const char *message, const char *signature, SCHEME *dummy = NULL) { typename SCHEME::Signer signer(StringSource(key, true, new HexDecoder).Ref()); typename SCHEME::Verifier verifier(signer); EqualityComparisonFilter comparison; StringSource(message, true, new SignerFilter(NullRNG(), signer, new ChannelSwitch(comparison, "0"))); StringSource(signature, true, new HexDecoder(new ChannelSwitch(comparison, "1"))); comparison.ChannelMessageSeriesEnd("0"); comparison.ChannelMessageSeriesEnd("1"); VerifierFilter verifierFilter(verifier, NULL, VerifierFilter::SIGNATURE_AT_BEGIN | VerifierFilter::THROW_EXCEPTION); StringSource(signature, true, new HexDecoder(new Redirector(verifierFilter, false))); StringSource(message, true, new Redirector(verifierFilter)); }
void MAC_KnownAnswerTest(const char *key, const char *message, const char *digest, MAC *dummy = NULL) { std::string decodedKey; StringSource(key, true, new HexDecoder(new StringSink(decodedKey))); MAC mac((const byte *)decodedKey.data(), decodedKey.size()); KnownAnswerTest(mac, message, digest); }
string RSADecryptString(const char *privFilename, const char *ciphertext) { FileSource privFile(privFilename, true, new HexDecoder); RSAES_OAEP_SHA_Decryptor priv(privFile); string result; StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result)))); return result; }
void KnownAnswerTest(RandomNumberGenerator &rng, const char *output) { EqualityComparisonFilter comparison; RandomNumberStore(rng, strlen(output)/2).TransferAllTo(comparison, "0"); StringSource(output, true, new HexDecoder(new ChannelSwitch(comparison, "1"))); comparison.ChannelMessageSeriesEnd("0"); comparison.ChannelMessageSeriesEnd("1"); }
void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransformation &target) { std::string s1 = GetRequiredDatum(data, name), s2; while (!s1.empty()) { while (s1[0] == ' ') s1 = s1.substr(1); int repeat = 1; if (s1[0] == 'r') { repeat = atoi(s1.c_str()+1); s1 = s1.substr(s1.find(' ')+1); } s2 = ""; // MSVC 6 doesn't have clear(); if (s1[0] == '\"') { s2 = s1.substr(1, s1.find('\"', 1)-1); s1 = s1.substr(s2.length() + 2); } else if (s1.substr(0, 2) == "0x") { StringSource(s1.substr(2, s1.find(' ')), true, new HexDecoder(new StringSink(s2))); s1 = s1.substr(STDMIN(s1.find(' '), s1.length())); } else { StringSource(s1.substr(0, s1.find(' ')), true, new HexDecoder(new StringSink(s2))); s1 = s1.substr(STDMIN(s1.find(' '), s1.length())); } ByteQueue q; while (repeat--) { q.Put((const byte *)s2.data(), s2.size()); if (q.MaxRetrievable() > 4*1024 || repeat == 0) q.TransferTo(target); } } }
void X917RNG_KnownAnswerTest( const char *key, const char *seed, const char *output, unsigned int deterministicTimeVector, CIPHER *dummy = NULL) { #ifdef OS_RNG_AVAILABLE std::string decodedKey, decodedSeed; StringSource(key, true, new HexDecoder(new StringSink(decodedKey))); StringSource(seed, true, new HexDecoder(new StringSink(decodedSeed))); AutoSeededX917RNG<CIPHER> rng; rng.Reseed((const byte *)decodedKey.data(), decodedKey.size(), (const byte *)decodedSeed.data(), deterministicTimeVector); KnownAnswerTest(rng, output); #else throw 0; #endif }
void SymmCipher::gcm_decrypt(const string *data, const byte *iv, unsigned ivlen, unsigned taglen, string *result) { aesgcm_d.Resynchronize(iv, ivlen); try { StringSource(*data, true, new AuthenticatedDecryptionFilter(aesgcm_d, new StringSink(*result), taglen)); } catch (HashVerificationFilter::HashVerificationFailed e) { result->clear(); LOG_err << "Failed AES-GCM decryption: " << e.GetWhat(); } }
/* * Description: use key to encrypt 'plainText', return the cipher * Input: * plainText: the string need to be encrypted * OutPUt: * return the cipher */ string MyAES::Encrypt(const string &plainText) { string cipher; CBC_Mode<AES>::Encryption aesEncryptor(key, key_length, iv); // AESEncryption aesEncryptor; //加密器 // aesEncryptor.SetKey( key, key_length ); //设定加密密钥 StringSource(plainText, true, new StreamTransformationFilter(aesEncryptor, new StringSink(cipher))); return cipher; }
/* * Description: use the same key to decrypt "cipher" and return the plainText * Input: * cipher: the string to be decrypted * Output: * return the recover */ string MyAES::Decrypt(const string & cipher) { string recover; CBC_Mode<AES>::Decryption aesDecryptor(key, key_length, iv); //AESDecryption aesDecryptor; //解密器 //aesDecryptor.SetKey( key, key_length ); //设定加密密钥 StringSource(cipher, true, new StreamTransformationFilter(aesDecryptor, new StringSink(recover))); return recover; }
void SymmCipher::ccm_decrypt(const string *data, const byte *iv, unsigned ivlen, unsigned taglen, string *result) { try { if (taglen == 16) { aesccm16_d.Resynchronize(iv, ivlen); aesccm16_d.SpecifyDataLengths(0, data->size() - taglen, 0); StringSource(*data, true, new AuthenticatedDecryptionFilter(aesccm16_d, new StringSink(*result))); } else if (taglen == 8) { aesccm8_d.Resynchronize(iv, ivlen); aesccm8_d.SpecifyDataLengths(0, data->size() - taglen, 0); StringSource(*data, true, new AuthenticatedDecryptionFilter(aesccm8_d, new StringSink(*result))); } } catch (HashVerificationFilter::HashVerificationFailed e) { result->clear(); LOG_err << "Failed AES-CCM decryption: " << e.GetWhat(); } }
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message) { FileSource pubFile(pubFilename, true, new HexDecoder); RSAES_OAEP_SHA_Encryptor pub(pubFile); RandomPool randPool; randPool.Put((byte *)seed, strlen(seed)); string result; StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result)))); return result; }
void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransformation &target) { std::string s1 = GetRequiredDatum(data, name), s2; int repeat = 1; if (s1[0] == 'r') { repeat = atoi(s1.c_str()+1); s1 = s1.substr(s1.find(' ')+1); } if (s1[0] == '\"') s2 = s1.substr(1, s1.find('\"', 1)-1); else if (s1.substr(0, 2) == "0x") StringSource(s1.substr(2), true, new HexDecoder(new StringSink(s2))); else StringSource(s1, true, new HexDecoder(new StringSink(s2))); while (repeat--) target.Put((const byte *)s2.data(), s2.size()); }
void EncryptionPairwiseConsistencyTest(const PK_Encryptor &encryptor, const PK_Decryptor &decryptor) { try { #ifdef OS_RNG_AVAILABLE DefaultAutoSeededRNG rng; #else RandomNumberGenerator &rng = NullRNG(); #endif const char *testMessage ="test message"; std::string ciphertext, decrypted; StringSource( testMessage, true, new PK_EncryptorFilter( rng, encryptor, new StringSink(ciphertext))); if (ciphertext == testMessage) throw 0; StringSource( ciphertext, true, new PK_DecryptorFilter( rng, decryptor, new StringSink(decrypted))); if (decrypted != testMessage) throw 0; } catch (...) { throw SelfTestFailure(encryptor.AlgorithmName() + ": pairwise consistency test failed"); } }
/*! * convert string hexadecimal format to cipher format. * @param str * @return string */ string AES::stringtoCipher(string str){ // generate encoded string encoded; encoded.clear(); StringSource(str, true, new HexDecoder( new StringSink(encoded) ) // HexEncoder ); // StringSource //cout << "#-- encoded text: " << encoded << endl; return encoded; }
void SignatureKnownAnswerTest(const char *key, const char *message, const char *signature, SCHEME *dummy = NULL) { #ifdef OS_RNG_AVAILABLE DefaultAutoSeededRNG rng; #else RandomNumberGenerator &rng = NullRNG(); #endif typename SCHEME::Signer signer(StringSource(key, true, new HexDecoder).Ref()); typename SCHEME::Verifier verifier(signer); EqualityComparisonFilter comparison; StringSource(message, true, new SignerFilter(rng, signer, new ChannelSwitch(comparison, "0"))); StringSource(signature, true, new HexDecoder(new ChannelSwitch(comparison, "1"))); comparison.ChannelMessageSeriesEnd("0"); comparison.ChannelMessageSeriesEnd("1"); VerifierFilter verifierFilter(verifier, NULL, VerifierFilter::SIGNATURE_AT_BEGIN | VerifierFilter::THROW_EXCEPTION); StringSource(signature, true, new HexDecoder(new Redirector(verifierFilter, Redirector::DATA_ONLY))); StringSource(message, true, new Redirector(verifierFilter)); }
QByteArray SHA256Algorithm::generateHash(const QByteArray& message) { std::string hash; try { StringSource(reinterpret_cast<const byte*>(message.data()), message.length(), true, new HashFilter(m_hash, new StringSink(hash))); } catch (const CryptoPP::Exception& e) { throw HashAlgorithmException(e.what()); } return QByteArray(hash.c_str(), hash.length()); }
bool TestHKDF(KeyDerivationFunction &kdf, const HKDF_TestTuple *testSet, unsigned int testSetSize) { bool pass = true; for (unsigned int i=0; i<testSetSize; i++) { const HKDF_TestTuple &tuple = testSet[i]; std::string secret, salt, info, expected; StringSource(tuple.hexSecret, true, new HexDecoder(new StringSink(secret))); StringSource(tuple.hexSalt ? tuple.hexSalt : "", true, new HexDecoder(new StringSink(salt))); StringSource(tuple.hexInfo ? tuple.hexInfo : "", true, new HexDecoder(new StringSink(info))); StringSource(tuple.hexExpected, true, new HexDecoder(new StringSink(expected))); SecByteBlock derived(expected.size()); unsigned int ret = kdf.DeriveKey(derived, derived.size(), reinterpret_cast<const unsigned char*>(secret.data()), secret.size(), (tuple.hexSalt ? reinterpret_cast<const unsigned char*>(salt.data()) : NULL), salt.size(), (tuple.hexInfo ? reinterpret_cast<const unsigned char*>(info.data()) : NULL), info.size()); bool fail = !VerifyBufsEqual(derived, reinterpret_cast<const unsigned char*>(expected.data()), derived.size()); pass = pass && (ret == tuple.len) && !fail; HexEncoder enc(new FileSink(std::cout)); std::cout << (fail ? "FAILED " : "passed "); std::cout << " " << tuple.hexSecret << " "; std::cout << (tuple.hexSalt ? (strlen(tuple.hexSalt) ? tuple.hexSalt : "<0-LEN SALT>") : "<NO SALT>"); std::cout << " "; std::cout << (tuple.hexInfo ? (strlen(tuple.hexInfo) ? tuple.hexInfo : "<0-LEN INFO>") : "<NO INFO>"); std::cout << " "; enc.Put(derived, derived.size()); std::cout << std::endl; } return pass; }
void HmacFile(const char *hexKey, const char *file) { member_ptr<MessageAuthenticationCode> mac; if (strcmp(hexKey, "selftest") == 0) { cerr << "Computing HMAC/SHA1 value for self test.\n"; mac.reset(NewIntegrityCheckingMAC()); } else { std::string decodedKey; StringSource(hexKey, true, new HexDecoder(new StringSink(decodedKey))); mac.reset(new HMAC<SHA1>((const byte *)decodedKey.data(), decodedKey.size())); } FileSource(file, true, new HashFilter(*mac, new HexEncoder(new FileSink(cout)))); }
QByteArray AESAlgorithmCBCMode::decrypt(const QByteArray& encrypted) { if (m_key.isEmpty() || m_iv.isEmpty()) { throw SymmetricAlgorithmException("Key has not been generated or set"); } std::string plain; try { StringSource(reinterpret_cast<const byte*>(encrypted.data()), encrypted.length(), true, new StreamTransformationFilter(m_aesDecryption, new StringSink(plain))); } catch (const CryptoPP::Exception& e) { throw SymmetricAlgorithmException(e.what()); } return QByteArray(plain.c_str(), plain.length()); }
void SignaturePairwiseConsistencyTest(const PK_Signer &signer, const PK_Verifier &verifier) { try { RandomPool rng; StringSource( "test message", true, new SignerFilter( rng, signer, new VerifierFilter(verifier, NULL, VerifierFilter::THROW_EXCEPTION), true)); } catch (...) { throw SelfTestFailure(signer.AlgorithmName() + ": pairwise consistency test failed"); } }
void SignaturePairwiseConsistencyTest(const PK_Signer &signer, const PK_Verifier &verifier) { try { #ifdef OS_RNG_AVAILABLE DefaultAutoSeededRNG rng; #else RandomNumberGenerator &rng = NullRNG(); #endif StringSource( "test message", true, new SignerFilter( rng, signer, new VerifierFilter(verifier, NULL, VerifierFilter::THROW_EXCEPTION), true)); } catch (...) { throw SelfTestFailure(signer.AlgorithmName() + ": pairwise consistency test failed"); } }
void SymmCipher::gcm_encrypt(const string *data, const byte *iv, unsigned ivlen, unsigned taglen, string *result) { aesgcm_e.Resynchronize(iv, ivlen); StringSource(*data, true, new AuthenticatedEncryptionFilter(aesgcm_e, new StringSink(*result), false, taglen)); }