Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/dlltest.cpp
2 views
#ifndef CRYPTOPP_DLL_ONLY1# define CRYPTOPP_DEFAULT_NO_DLL2#endif34#include "dll.h"5#include "cryptlib.h"6#include "filters.h"7#include "pkcspad.h"89#if CRYPTOPP_MSC_VERSION10# pragma warning(disable: 4505 4355)11#endif1213USING_NAMESPACE(CryptoPP)1415void FIPS140_SampleApplication()16{17if (!FIPS_140_2_ComplianceEnabled())18{19std::cerr << "FIPS 140-2 compliance was turned off at compile time.\n";20abort();21}2223// check self test status24if (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)25{26std::cerr << "Automatic power-up self test failed.\n";27abort();28}29std::cout << "0. Automatic power-up self test passed.\n";3031// simulate a power-up self test error32SimulatePowerUpSelfTestFailure();33try34{35// trying to use a crypto algorithm after power-up self test error will result in an exception36AES::Encryption aes;3738// should not be here39std::cerr << "Use of AES failed to cause an exception after power-up self test error.\n";40abort();41}42catch (SelfTestFailure &e)43{44std::cout << "1. Caught expected exception when simulating self test failure. Exception message follows: ";45std::cout << e.what() << std::endl;46}4748// clear the self test error state and redo power-up self test49DoDllPowerUpSelfTest();50if (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)51{52std::cerr << "Re-do power-up self test failed.\n";53abort();54}55std::cout << "2. Re-do power-up self test passed.\n";5657// encrypt and decrypt58const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};59const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};60const byte plaintext[] = { // "Now is the time for all " without tailing 0610x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,620x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,630x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};64byte ciphertext[24];65byte decrypted[24];6667CFB_FIPS_Mode<DES_EDE3>::Encryption encryption_DES_EDE3_CFB;68encryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);69encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);7071CFB_FIPS_Mode<DES_EDE3>::Decryption decryption_DES_EDE3_CFB;72decryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);73decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);7475if (std::memcmp(plaintext, decrypted, 24) != 0)76{77std::cerr << "DES-EDE3-CFB Encryption/decryption failed.\n";78abort();79}80std::cout << "3. DES-EDE3-CFB Encryption/decryption succeeded.\n";8182// hash83const byte message[] = {'a', 'b', 'c'};84const byte expectedDigest[] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};85byte digest[20];8687SHA1 sha;88sha.Update(message, 3);89sha.Final(digest);9091if (std::memcmp(digest, expectedDigest, 20) != 0)92{93std::cerr << "SHA-1 hash failed.\n";94abort();95}96std::cout << "4. SHA-1 hash succeeded.\n";9798// create auto-seeded X9.17 RNG object, if available99#ifdef OS_RNG_AVAILABLE100AutoSeededX917RNG<AES> rng;101#else102// this is used to allow this function to compile on platforms that don't have auto-seeded RNGs103RandomNumberGenerator &rng(NullRNG());104#endif105106// generate DSA key107DSA::PrivateKey dsaPrivateKey;108dsaPrivateKey.GenerateRandomWithKeySize(rng, 1024);109DSA::PublicKey dsaPublicKey;110dsaPublicKey.AssignFrom(dsaPrivateKey);111if (!dsaPrivateKey.Validate(rng, 3) || !dsaPublicKey.Validate(rng, 3))112{113std::cerr << "DSA key generation failed.\n";114abort();115}116std::cout << "5. DSA key generation succeeded.\n";117118// encode DSA key119std::string encodedDsaPublicKey, encodedDsaPrivateKey;120dsaPublicKey.DEREncode(StringSink(encodedDsaPublicKey).Ref());121dsaPrivateKey.DEREncode(StringSink(encodedDsaPrivateKey).Ref());122123// decode DSA key124DSA::PrivateKey decodedDsaPrivateKey;125decodedDsaPrivateKey.BERDecode(StringStore(encodedDsaPrivateKey).Ref());126DSA::PublicKey decodedDsaPublicKey;127decodedDsaPublicKey.BERDecode(StringStore(encodedDsaPublicKey).Ref());128129if (!decodedDsaPrivateKey.Validate(rng, 3) || !decodedDsaPublicKey.Validate(rng, 3))130{131std::cerr << "DSA key encode/decode failed.\n";132abort();133}134std::cout << "6. DSA key encode/decode succeeded.\n";135136// sign and verify137byte signature[40];138DSA::Signer signer(dsaPrivateKey);139CRYPTOPP_ASSERT(signer.SignatureLength() == 40);140signer.SignMessage(rng, message, 3, signature);141142DSA::Verifier verifier(dsaPublicKey);143if (!verifier.VerifyMessage(message, 3, signature, sizeof(signature)))144{145std::cerr << "DSA signature and verification failed.\n";146abort();147}148std::cout << "7. DSA signature and verification succeeded.\n";149150151// try to verify an invalid signature152signature[0] ^= 1;153if (verifier.VerifyMessage(message, 3, signature, sizeof(signature)))154{155std::cerr << "DSA signature verification failed to detect bad signature.\n";156abort();157}158std::cout << "8. DSA signature verification successfully detected bad signature.\n";159160// try to use an invalid key length161try162{163ECB_Mode<DES_EDE3>::Encryption encryption_DES_EDE3_ECB;164encryption_DES_EDE3_ECB.SetKey(key, 5);165166// should not be here167std::cerr << "DES-EDE3 implementation did not detect use of invalid key length.\n";168abort();169}170catch (InvalidArgument &e)171{172std::cout << "9. Caught expected exception when using invalid key length. Exception message follows: ";173std::cout << e.what() << std::endl;174}175176std::cout << "\nFIPS 140-2 Sample Application completed normally.\n";177}178179#ifdef CRYPTOPP_IMPORTS180181static PNew s_pNew = NULLPTR;182static PDelete s_pDelete = NULLPTR;183184extern "C" __declspec(dllexport) void __cdecl SetNewAndDeleteFromCryptoPP(PNew pNew, PDelete pDelete, PSetNewHandler pSetNewHandler)185{186(void)(pSetNewHandler);187s_pNew = pNew;188s_pDelete = pDelete;189}190191void * __cdecl operator new (size_t size)192{193return s_pNew(size);194}195196void __cdecl operator delete (void * p)197{198s_pDelete(p);199}200201#endif202203204