1 #ifndef CRYPTOPP_DLL_ONLY
2 # define CRYPTOPP_DEFAULT_NO_DLL
12 void FIPS140_SampleApplication()
16 cerr <<
"FIPS 140-2 compliance was turned off at compile time.\n";
23 cerr <<
"Automatic power-up self test failed.\n";
26 cout <<
"0. Automatic power-up self test passed.\n";
36 cerr <<
"Use of AES failed to cause an exception after power-up self test error.\n";
41 cout <<
"1. Caught expected exception when simulating self test failure. Exception message follows: ";
42 cout << e.what() << endl;
49 cerr <<
"Re-do power-up self test failed.\n";
52 cout <<
"2. Re-do power-up self test passed.\n";
55 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};
56 const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
57 const byte plaintext[] = {
58 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
59 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
60 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
65 encryption_DES_EDE3_CFB.SetKeyWithIV(key,
sizeof(key), iv);
66 encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);
69 decryption_DES_EDE3_CFB.SetKeyWithIV(key,
sizeof(key), iv);
70 decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);
72 if (memcmp(plaintext, decrypted, 24) != 0)
74 cerr <<
"DES-EDE3-CFB Encryption/decryption failed.\n";
77 cout <<
"3. DES-EDE3-CFB Encryption/decryption succeeded.\n";
80 const byte message[] = {
'a',
'b',
'c'};
81 const byte expectedDigest[] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
85 sha.Update(message, 3);
88 if (memcmp(digest, expectedDigest, 20) != 0)
90 cerr <<
"SHA-1 hash failed.\n";
93 cout <<
"4. SHA-1 hash succeeded.\n";
96 #ifdef OS_RNG_AVAILABLE
104 DSA::PrivateKey dsaPrivateKey;
105 dsaPrivateKey.GenerateRandomWithKeySize(rng, 1024);
106 DSA::PublicKey dsaPublicKey;
107 dsaPublicKey.AssignFrom(dsaPrivateKey);
108 if (!dsaPrivateKey.Validate(rng, 3) || !dsaPublicKey.Validate(rng, 3))
110 cerr <<
"DSA key generation failed.\n";
113 cout <<
"5. DSA key generation succeeded.\n";
116 std::string encodedDsaPublicKey, encodedDsaPrivateKey;
117 dsaPublicKey.DEREncode(
StringSink(encodedDsaPublicKey).Ref());
118 dsaPrivateKey.DEREncode(
StringSink(encodedDsaPrivateKey).Ref());
121 DSA::PrivateKey decodedDsaPrivateKey;
122 decodedDsaPrivateKey.BERDecode(
StringStore(encodedDsaPrivateKey).Ref());
123 DSA::PublicKey decodedDsaPublicKey;
124 decodedDsaPublicKey.BERDecode(
StringStore(encodedDsaPublicKey).Ref());
126 if (!decodedDsaPrivateKey.Validate(rng, 3) || !decodedDsaPublicKey.Validate(rng, 3))
128 cerr <<
"DSA key encode/decode failed.\n";
131 cout <<
"6. DSA key encode/decode succeeded.\n";
135 DSA::Signer signer(dsaPrivateKey);
136 assert(signer.SignatureLength() == 40);
137 signer.SignMessage(rng, message, 3, signature);
139 DSA::Verifier verifier(dsaPublicKey);
140 if (!verifier.VerifyMessage(message, 3, signature,
sizeof(signature)))
142 cerr <<
"DSA signature and verification failed.\n";
145 cout <<
"7. DSA signature and verification succeeded.\n";
150 if (verifier.VerifyMessage(message, 3, signature,
sizeof(signature)))
152 cerr <<
"DSA signature verification failed to detect bad signature.\n";
155 cout <<
"8. DSA signature verification successfully detected bad signature.\n";
161 encryption_DES_EDE3_ECB.SetKey(key, 5);
164 cerr <<
"DES-EDE3 implementation did not detect use of invalid key length.\n";
169 cout <<
"9. Caught expected exception when using invalid key length. Exception message follows: ";
170 cout << e.what() << endl;
173 cout <<
"\nFIPS 140-2 Sample Application completed normally.\n";
176 #ifdef CRYPTOPP_IMPORTS
178 static PNew s_pNew = NULL;
179 static PDelete s_pDelete = NULL;
181 extern "C" __declspec(dllexport)
void __cdecl SetNewAndDeleteFromCryptoPP(PNew pNew, PDelete pDelete, PSetNewHandler pSetNewHandler)
187 void * __cdecl
operator new (
size_t size)
192 void __cdecl
operator delete (
void * p)
199 #ifdef CRYPTOPP_DLL_ONLY
203 FIPS140_SampleApplication();