1 #ifndef CRYPTOPP_DLL_ONLY
2 #define CRYPTOPP_DEFAULT_NO_DLL
7 USING_NAMESPACE(CryptoPP)
10 void FIPS140_SampleApplication()
12 if (!FIPS_140_2_ComplianceEnabled())
14 cerr <<
"FIPS 140-2 compliance was turned off at compile time.\n";
19 if (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)
21 cerr <<
"Automatic power-up self test failed.\n";
24 cout <<
"0. Automatic power-up self test passed.\n";
27 SimulatePowerUpSelfTestFailure();
34 cerr <<
"Use of AES failed to cause an exception after power-up self test error.\n";
39 cout <<
"1. Caught expected exception when simulating self test failure. Exception message follows: ";
40 cout << e.what() << endl;
45 if (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)
47 cerr <<
"Re-do power-up self test failed.\n";
50 cout <<
"2. Re-do power-up self test passed.\n";
53 const 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};
54 const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
55 const byte plaintext[] = {
56 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
57 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
58 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
63 encryption_DES_EDE3_CFB.SetKeyWithIV(key,
sizeof(key), iv);
64 encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);
67 decryption_DES_EDE3_CFB.SetKeyWithIV(key,
sizeof(key), iv);
68 decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);
70 if (memcmp(plaintext, decrypted, 24) != 0)
72 cerr <<
"DES-EDE3-CFB Encryption/decryption failed.\n";
75 cout <<
"3. DES-EDE3-CFB Encryption/decryption succeeded.\n";
78 const byte message[] = {
'a',
'b',
'c'};
79 const byte expectedDigest[] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
83 sha.Update(message, 3);
86 if (memcmp(digest, expectedDigest, 20) != 0)
88 cerr <<
"SHA-1 hash failed.\n";
91 cout <<
"4. SHA-1 hash succeeded.\n";
94 #ifdef OS_RNG_AVAILABLE
103 dsaPrivateKey.GenerateRandomWithKeySize(rng, 1024);
105 dsaPublicKey.AssignFrom(dsaPrivateKey);
106 if (!dsaPrivateKey.Validate(rng, 3) || !dsaPublicKey.
Validate(rng, 3))
108 cerr <<
"DSA key generation failed.\n";
111 cout <<
"5. DSA key generation succeeded.\n";
114 std::string encodedDsaPublicKey, encodedDsaPrivateKey;
116 dsaPrivateKey.DEREncode(
StringSink(encodedDsaPrivateKey).Ref());
120 decodedDsaPrivateKey.BERDecode(
StringStore(encodedDsaPrivateKey).Ref());
124 if (!decodedDsaPrivateKey.Validate(rng, 3) || !decodedDsaPublicKey.
Validate(rng, 3))
126 cerr <<
"DSA key encode/decode failed.\n";
129 cout <<
"6. DSA key encode/decode succeeded.\n";
134 assert(signer.SignatureLength() == 40);
135 signer.SignMessage(rng, message, 3, signature);
138 if (!verifier.VerifyMessage(message, 3, signature,
sizeof(signature)))
140 cerr <<
"DSA signature and verification failed.\n";
143 cout <<
"7. DSA signature and verification succeeded.\n";
148 if (verifier.VerifyMessage(message, 3, signature,
sizeof(signature)))
150 cerr <<
"DSA signature verification failed to detect bad signature.\n";
153 cout <<
"8. DSA signature verification successfully detected bad signature.\n";
159 encryption_DES_EDE3_ECB.SetKey(key, 5);
162 cerr <<
"DES-EDE3 implementation did not detect use of invalid key length.\n";
167 cout <<
"9. Caught expected exception when using invalid key length. Exception message follows: ";
168 cout << e.what() << endl;
171 cout <<
"\nFIPS 140-2 Sample Application completed normally.\n";
174 #ifdef CRYPTOPP_IMPORTS
176 static PNew s_pNew = NULL;
177 static PDelete s_pDelete = NULL;
179 extern "C" __declspec(dllexport) void __cdecl SetNewAndDeleteFromCryptoPP(PNew pNew, PDelete pDelete, PSetNewHandler pSetNewHandler)
185 void * __cdecl
operator new (
size_t size)
190 void __cdecl
operator delete (
void * p)
197 #ifdef CRYPTOPP_DLL_ONLY
201 FIPS140_SampleApplication();