Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

test.cpp

00001 // test.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #define CRYPTOPP_DEFAULT_NO_DLL
00004 #include "dll.h"
00005 #include "md5.h"
00006 #include "ripemd.h"
00007 #include "rng.h"
00008 #include "gzip.h"
00009 #include "default.h"
00010 #include "randpool.h"
00011 #include "ida.h"
00012 #include "base64.h"
00013 #include "socketft.h"
00014 #include "wait.h"
00015 #include "factory.h"
00016 
00017 #include "validate.h"
00018 #include "bench.h"
00019 
00020 #include <iostream>
00021 #include <time.h>
00022 
00023 #ifdef CRYPTOPP_WIN32_AVAILABLE
00024 #include <windows.h>
00025 #endif
00026 
00027 #if defined(USE_BERKELEY_STYLE_SOCKETS) && !defined(macintosh)
00028 #include <netinet/in.h>
00029 #include <netinet/tcp.h>
00030 #endif
00031 
00032 #if (_MSC_VER >= 1000)
00033 #include <crtdbg.h>             // for the debug heap
00034 #endif
00035 
00036 #if defined(__MWERKS__) && defined(macintosh)
00037 #include <console.h>
00038 #endif
00039 
00040 USING_NAMESPACE(CryptoPP)
00041 USING_NAMESPACE(std)
00042 
00043 const int MAX_PHRASE_LENGTH=250;
00044 
00045 void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);
00046 string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);
00047 string RSADecryptString(const char *privFilename, const char *ciphertext);
00048 void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename);
00049 bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename);
00050 
00051 void DigestFile(const char *file);
00052 void HmacFile(const char *hexKey, const char *file);
00053 
00054 void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile);
00055 
00056 string EncryptString(const char *plaintext, const char *passPhrase);
00057 string DecryptString(const char *ciphertext, const char *passPhrase);
00058 
00059 void EncryptFile(const char *in, const char *out, const char *passPhrase);
00060 void DecryptFile(const char *in, const char *out, const char *passPhrase);
00061 
00062 void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed);
00063 void SecretRecoverFile(int threshold, const char *outFilename, char *const *inFilenames);
00064 
00065 void InformationDisperseFile(int threshold, int nShares, const char *filename);
00066 void InformationRecoverFile(int threshold, const char *outFilename, char *const *inFilenames);
00067 
00068 void GzipFile(const char *in, const char *out, int deflate_level);
00069 void GunzipFile(const char *in, const char *out);
00070 
00071 void Base64Encode(const char *infile, const char *outfile);
00072 void Base64Decode(const char *infile, const char *outfile);
00073 void HexEncode(const char *infile, const char *outfile);
00074 void HexDecode(const char *infile, const char *outfile);
00075 
00076 void ForwardTcpPort(const char *sourcePort, const char *destinationHost, const char *destinationPort);
00077 
00078 void FIPS140_SampleApplication();
00079 void FIPS140_GenerateRandomFiles();
00080 
00081 bool Validate(int, bool, const char *);
00082 
00083 int (*AdhocTest)(int argc, char *argv[]) = NULL;
00084 
00085 #ifdef __BCPLUSPLUS__
00086 int cmain(int argc, char *argv[])
00087 #else
00088 int main(int argc, char *argv[])
00089 #endif
00090 {
00091 #ifdef _CRTDBG_LEAK_CHECK_DF
00092         // Turn on leak-checking
00093         int tempflag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
00094         tempflag |= _CRTDBG_LEAK_CHECK_DF;
00095         _CrtSetDbgFlag( tempflag );
00096 #endif
00097 
00098 #if defined(__MWERKS__) && defined(macintosh)
00099         argc = ccommand(&argv);
00100 #endif
00101 
00102         try
00103         {
00104                 std::string command, executableName, macFilename;
00105 
00106                 if (argc < 2)
00107                         command = 'h';
00108                 else
00109                         command = argv[1];
00110 
00111                 if (command == "g")
00112                 {
00113                         char seed[1024], privFilename[128], pubFilename[128];
00114                         unsigned int keyLength;
00115 
00116                         cout << "Key length in bits: ";
00117                         cin >> keyLength;
00118 
00119                         cout << "\nSave private key to file: ";
00120                         cin >> privFilename;
00121 
00122                         cout << "\nSave public key to file: ";
00123                         cin >> pubFilename;
00124 
00125                         cout << "\nRandom Seed: ";
00126                         ws(cin);
00127                         cin.getline(seed, 1024);
00128 
00129                         GenerateRSAKey(keyLength, privFilename, pubFilename, seed);
00130                 }
00131                 else if (command == "rs")
00132                         RSASignFile(argv[2], argv[3], argv[4]);
00133                 else if (command == "rv")
00134                 {
00135                         bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]);
00136                         cout << (verified ? "valid signature" : "invalid signature") << endl;
00137                 }
00138                 else if (command == "r")
00139                 {
00140                         char privFilename[128], pubFilename[128];
00141                         char seed[1024], message[1024];
00142 
00143                         cout << "Private key file: ";
00144                         cin >> privFilename;
00145 
00146                         cout << "\nPublic key file: ";
00147                         cin >> pubFilename;
00148 
00149                         cout << "\nRandom Seed: ";
00150                         ws(cin);
00151                         cin.getline(seed, 1024);
00152 
00153                         cout << "\nMessage: ";
00154                         cin.getline(message, 1024);
00155 
00156                         string ciphertext = RSAEncryptString(pubFilename, seed, message);
00157                         cout << "\nCiphertext: " << ciphertext << endl;
00158 
00159                         string decrypted = RSADecryptString(privFilename, ciphertext.c_str());
00160                         cout << "\nDecrypted: " << decrypted << endl;
00161                 }
00162                 else if (command == "mt")
00163                 {
00164                         MaurerRandomnessTest mt;
00165                         FileStore fs(argv[2]);
00166                         fs.TransferAllTo(mt);
00167                         cout << "Maurer Test Value: " << mt.GetTestValue() << endl;
00168                 }
00169 #ifdef CRYPTOPP_WIN32_AVAILABLE
00170                 else if (command == "mac_dll")
00171                 {
00172                         HMODULE hModule = LoadLibrary(argv[2]);
00173                         PGetPowerUpSelfTestStatus pGetPowerUpSelfTestStatus = (PGetPowerUpSelfTestStatus)GetProcAddress(hModule, "?GetPowerUpSelfTestStatus@CryptoPP@@YA?AW4PowerUpSelfTestStatus@1@XZ");
00174                         PGetActualMacAndLocation pGetActualMacAndLocation = (PGetActualMacAndLocation)GetProcAddress(hModule, 
00175                                 sizeof(byte *)==4 ? "?GetActualMacAndLocation@CryptoPP@@YAPBEAAI0@Z" : "?GetActualMacAndLocation@CryptoPP@@YAPEBEAEAI0@Z");
00176 
00177                         PowerUpSelfTestStatus status = pGetPowerUpSelfTestStatus();
00178                         if (status == POWER_UP_SELF_TEST_PASSED)
00179                         {
00180                                 cout << "Crypto++ DLL MAC is valid. Nothing to do.\n";
00181                                 return 0;
00182                         }
00183 
00184                         unsigned int macSize, macFileLocation;
00185                         const byte *pMac = pGetActualMacAndLocation(macSize, macFileLocation);
00186                         
00187                         if (macFileLocation == 0)
00188                         {
00189                                 cerr << "Could not find MAC location in Crypto++ DLL.\n";
00190                                 return 1;
00191                         }
00192                         else
00193                         {
00194                                 SecByteBlock mac(pMac, macSize);        // copy MAC before freeing the DLL
00195                                 BOOL r = FreeLibrary(hModule);
00196                                 cout << "Placing MAC in file " << argv[2] << ", location " << macFileLocation << ".\n";
00197                                 std::ofstream dllFile(argv[2], ios::in | ios::out | ios::binary);
00198                                 dllFile.seekp(macFileLocation);
00199                                 dllFile.write((const char *)mac.data(), macSize);
00200                                 if (!dllFile.good())
00201                                 {
00202                                         cerr << "Error writing file.\n";
00203                                         return 1;
00204                                 }
00205                         }
00206                 }
00207 #endif
00208                 else if (command == "m")
00209                         DigestFile(argv[2]);
00210                 else if (command == "tv")
00211                         return !RunTestDataFile(argv[2]);
00212                 else if (command == "t")
00213                 {
00214                         // VC60 workaround: use char array instead of std::string to workaround MSVC's getline bug
00215                         char passPhrase[MAX_PHRASE_LENGTH], plaintext[1024];
00216 
00217                         cout << "Passphrase: ";
00218                         cin.getline(passPhrase, MAX_PHRASE_LENGTH);
00219 
00220                         cout << "\nPlaintext: ";
00221                         cin.getline(plaintext, 1024);
00222 
00223                         string ciphertext = EncryptString(plaintext, passPhrase);
00224                         cout << "\nCiphertext: " << ciphertext << endl;
00225 
00226                         string decrypted = DecryptString(ciphertext.c_str(), passPhrase);
00227                         cout << "\nDecrypted: " << decrypted << endl;
00228 
00229                         return 0;
00230                 }
00231                 else if (command == "e64")
00232                         Base64Encode(argv[2], argv[3]);
00233                 else if (command == "d64")
00234                         Base64Decode(argv[2], argv[3]);
00235                 else if (command == "e16")
00236                         HexEncode(argv[2], argv[3]);
00237                 else if (command == "d16")
00238                         HexDecode(argv[2], argv[3]);
00239                 else if (command == "e" || command == "d")
00240                 {
00241                         char passPhrase[MAX_PHRASE_LENGTH];
00242                         cout << "Passphrase: ";
00243                         cin.getline(passPhrase, MAX_PHRASE_LENGTH);
00244                         if (command == "e")
00245                                 EncryptFile(argv[2], argv[3], passPhrase);
00246                         else
00247                                 DecryptFile(argv[2], argv[3], passPhrase);
00248                 }
00249                 else if (command == "ss")
00250                 {
00251                         char seed[1024];
00252                         cout << "\nRandom Seed: ";
00253                         ws(cin);
00254                         cin.getline(seed, 1024);
00255                         SecretShareFile(atoi(argv[2]), atoi(argv[3]), argv[4], seed);
00256                 }
00257                 else if (command == "sr")
00258                         SecretRecoverFile(argc-3, argv[2], argv+3);
00259                 else if (command == "id")
00260                         InformationDisperseFile(atoi(argv[2]), atoi(argv[3]), argv[4]);
00261                 else if (command == "ir")
00262                         InformationRecoverFile(argc-3, argv[2], argv+3);
00263                 else if (command == "v")
00264                         return !Validate(argc>2 ? atoi(argv[2]) : 0, argv[1][1] == 'v', argc>3 ? argv[3] : NULL);
00265                 else if (command == "b")
00266                         BenchmarkAll(argc<3 ? 1 : atof(argv[2]));
00267                 else if (command == "b2")
00268                         BenchmarkAll2(argc<3 ? 1 : atof(argv[2]));
00269                 else if (command == "z")
00270                         GzipFile(argv[3], argv[4], argv[2][0]-'0');
00271                 else if (command == "u")
00272                         GunzipFile(argv[2], argv[3]);
00273                 else if (command == "fips")
00274                         FIPS140_SampleApplication();
00275                 else if (command == "fips-rand")
00276                         FIPS140_GenerateRandomFiles();
00277                 else if (command == "ft")
00278                         ForwardTcpPort(argv[2], argv[3], argv[4]);
00279                 else if (command == "a")
00280                 {
00281                         if (AdhocTest)
00282                                 return (*AdhocTest)(argc, argv);
00283                         else
00284                         {
00285                                 cerr << "AdhocTest not defined.\n";
00286                                 return 1;
00287                         }
00288                 }
00289                 else if (command == "hmac")
00290                         HmacFile(argv[2], argv[3]);
00291                 else if (command == "ae")
00292                         AES_CTR_Encrypt(argv[2], argv[3], argv[4], argv[5]);
00293                 else if (command == "h")
00294                 {
00295                         FileSource usage("usage.dat", true, new FileSink(cout));
00296                         return 1;
00297                 }
00298                 else
00299                 {
00300                         cerr << "Unrecognized command. Run \"cryptest h\" to obtain usage information.\n";
00301                         return 1;
00302                 }
00303                 return 0;
00304         }
00305         catch(CryptoPP::Exception &e)
00306         {
00307                 cout << "\nCryptoPP::Exception caught: " << e.what() << endl;
00308                 return -1;
00309         }
00310         catch(std::exception &e)
00311         {
00312                 cout << "\nstd::exception caught: " << e.what() << endl;
00313                 return -2;
00314         }
00315 }
00316 
00317 void FIPS140_GenerateRandomFiles()
00318 {
00319 #ifdef OS_RNG_AVAILABLE
00320         AutoSeededX917RNG<DES_EDE3> rng;
00321         RandomNumberStore store(rng, ULONG_MAX);
00322 
00323         for (unsigned int i=0; i<100000; i++)
00324                 store.TransferTo(FileSink((IntToString(i) + ".rnd").c_str()).Ref(), 20000);
00325 #else
00326         cout << "OS provided RNG not available.\n";
00327         exit(-1);
00328 #endif
00329 }
00330 
00331 SecByteBlock HexDecodeString(const char *hex)
00332 {
00333         StringSource ss(hex, true, new HexDecoder);
00334         SecByteBlock result((size_t)ss.MaxRetrievable());
00335         ss.Get(result, result.size());
00336         return result;
00337 }
00338 
00339 RandomPool & GlobalRNG()
00340 {
00341         static RandomPool randomPool;
00342         return randomPool;
00343 }
00344 
00345 void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)
00346 {
00347         RandomPool randPool;
00348         randPool.Put((byte *)seed, strlen(seed));
00349 
00350         RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
00351         HexEncoder privFile(new FileSink(privFilename));
00352         priv.DEREncode(privFile);
00353         privFile.MessageEnd();
00354 
00355         RSAES_OAEP_SHA_Encryptor pub(priv);
00356         HexEncoder pubFile(new FileSink(pubFilename));
00357         pub.DEREncode(pubFile);
00358         pubFile.MessageEnd();
00359 }
00360 
00361 string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)
00362 {
00363         FileSource pubFile(pubFilename, true, new HexDecoder);
00364         RSAES_OAEP_SHA_Encryptor pub(pubFile);
00365 
00366         RandomPool randPool;
00367         randPool.Put((byte *)seed, strlen(seed));
00368 
00369         string result;
00370         StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));
00371         return result;
00372 }
00373 
00374 string RSADecryptString(const char *privFilename, const char *ciphertext)
00375 {
00376         FileSource privFile(privFilename, true, new HexDecoder);
00377         RSAES_OAEP_SHA_Decryptor priv(privFile);
00378 
00379         string result;
00380         StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
00381         return result;
00382 }
00383 
00384 void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename)
00385 {
00386         FileSource privFile(privFilename, true, new HexDecoder);
00387         RSASS<PKCS1v15, SHA>::Signer priv(privFile);
00388         FileSource f(messageFilename, true, new SignerFilter(GlobalRNG(), priv, new HexEncoder(new FileSink(signatureFilename))));
00389 }
00390 
00391 bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename)
00392 {
00393         FileSource pubFile(pubFilename, true, new HexDecoder);
00394         RSASS<PKCS1v15, SHA>::Verifier pub(pubFile);
00395 
00396         FileSource signatureFile(signatureFilename, true, new HexDecoder);
00397         if (signatureFile.MaxRetrievable() != pub.SignatureLength())
00398                 return false;
00399         SecByteBlock signature(pub.SignatureLength());
00400         signatureFile.Get(signature, signature.size());
00401 
00402         VerifierFilter *verifierFilter = new VerifierFilter(pub);
00403         verifierFilter->Put(signature, pub.SignatureLength());
00404         FileSource f(messageFilename, true, verifierFilter);
00405 
00406         return verifierFilter->GetLastResult();
00407 }
00408 
00409 void DigestFile(const char *filename)
00410 {
00411         MD5 md5;
00412         SHA sha;
00413         RIPEMD160 ripemd;
00414         SHA256 sha256;
00415         HashFilter md5Filter(md5), shaFilter(sha), ripemdFilter(ripemd), sha256Filter(sha256);
00416 
00417         auto_ptr<ChannelSwitch> channelSwitch(new ChannelSwitch);
00418         channelSwitch->AddDefaultRoute(md5Filter);
00419         channelSwitch->AddDefaultRoute(shaFilter);
00420         channelSwitch->AddDefaultRoute(ripemdFilter);
00421         channelSwitch->AddDefaultRoute(sha256Filter);
00422         FileSource(filename, true, channelSwitch.release());
00423 
00424         HexEncoder encoder(new FileSink(cout), false);
00425         cout << "\nMD5: ";
00426         md5Filter.TransferTo(encoder);
00427         cout << "\nSHA-1: ";
00428         shaFilter.TransferTo(encoder);
00429         cout << "\nRIPEMD-160: ";
00430         ripemdFilter.TransferTo(encoder);
00431         cout << "\nSHA-256: ";
00432         sha256Filter.TransferTo(encoder);
00433 }
00434 
00435 void HmacFile(const char *hexKey, const char *file)
00436 {
00437         member_ptr<MessageAuthenticationCode> mac;
00438         if (strcmp(hexKey, "selftest") == 0)
00439         {
00440                 cerr << "Computing HMAC/SHA1 value for self test.\n";
00441                 mac.reset(NewIntegrityCheckingMAC());
00442         }
00443         else
00444         {
00445                 std::string decodedKey;
00446                 StringSource(hexKey, true, new HexDecoder(new StringSink(decodedKey)));
00447                 mac.reset(new HMAC<SHA1>((const byte *)decodedKey.data(), decodedKey.size()));
00448         }
00449         FileSource(file, true, new HashFilter(*mac, new HexEncoder(new FileSink(cout))));
00450 }
00451 
00452 void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
00453 {
00454         SecByteBlock key = HexDecodeString(hexKey);
00455         SecByteBlock iv = HexDecodeString(hexIV);
00456         CTR_Mode<AES>::Encryption aes(key, key.size(), iv);
00457         FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
00458 }
00459 
00460 string EncryptString(const char *instr, const char *passPhrase)
00461 {
00462         string outstr;
00463 
00464         DefaultEncryptorWithMAC encryptor(passPhrase, new HexEncoder(new StringSink(outstr)));
00465         encryptor.Put((byte *)instr, strlen(instr));
00466         encryptor.MessageEnd();
00467 
00468         return outstr;
00469 }
00470 
00471 string DecryptString(const char *instr, const char *passPhrase)
00472 {
00473         string outstr;
00474 
00475         HexDecoder decryptor(new DefaultDecryptorWithMAC(passPhrase, new StringSink(outstr)));
00476         decryptor.Put((byte *)instr, strlen(instr));
00477         decryptor.MessageEnd();
00478 
00479         return outstr;
00480 }
00481 
00482 void EncryptFile(const char *in, const char *out, const char *passPhrase)
00483 {
00484         FileSource f(in, true, new DefaultEncryptorWithMAC(passPhrase, new FileSink(out)));
00485 }
00486 
00487 void DecryptFile(const char *in, const char *out, const char *passPhrase)
00488 {
00489         FileSource f(in, true, new DefaultDecryptorWithMAC(passPhrase, new FileSink(out)));
00490 }
00491 
00492 void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed)
00493 {
00494         assert(nShares<=1000);
00495 
00496         RandomPool rng;
00497         rng.Put((byte *)seed, strlen(seed));
00498 
00499         ChannelSwitch *channelSwitch;
00500         FileSource source(filename, false, new SecretSharing(rng, threshold, nShares, channelSwitch = new ChannelSwitch));
00501 
00502         vector_member_ptrs<FileSink> fileSinks(nShares);
00503         string channel;
00504         for (int i=0; i<nShares; i++)
00505         {
00506                 char extension[5] = ".000";
00507                 extension[1]='0'+byte(i/100);
00508                 extension[2]='0'+byte((i/10)%10);
00509                 extension[3]='0'+byte(i%10);
00510                 fileSinks[i].reset(new FileSink((string(filename)+extension).c_str()));
00511 
00512                 channel = WordToString<word32>(i);
00513                 fileSinks[i]->Put((byte *)channel.data(), 4);
00514                 channelSwitch->AddRoute(channel, *fileSinks[i], BufferedTransformation::NULL_CHANNEL);
00515         }
00516 
00517         source.PumpAll();
00518 }
00519 
00520 void SecretRecoverFile(int threshold, const char *outFilename, char *const *inFilenames)
00521 {
00522         assert(threshold<=1000);
00523 
00524         SecretRecovery recovery(threshold, new FileSink(outFilename));
00525 
00526         vector_member_ptrs<FileSource> fileSources(threshold);
00527         SecByteBlock channel(4);
00528         int i;
00529         for (i=0; i<threshold; i++)
00530         {
00531                 fileSources[i].reset(new FileSource(inFilenames[i], false));
00532                 fileSources[i]->Pump(4);
00533                 fileSources[i]->Get(channel, 4);
00534                 fileSources[i]->Attach(new ChannelSwitch(recovery, string((char *)channel.begin(), 4)));
00535         }
00536 
00537         while (fileSources[0]->Pump(256))
00538                 for (i=1; i<threshold; i++)
00539                         fileSources[i]->Pump(256);
00540 
00541         for (i=0; i<threshold; i++)
00542                 fileSources[i]->PumpAll();
00543 }
00544 
00545 void InformationDisperseFile(int threshold, int nShares, const char *filename)
00546 {
00547         assert(nShares<=1000);
00548 
00549         ChannelSwitch *channelSwitch;
00550         FileSource source(filename, false, new InformationDispersal(threshold, nShares, channelSwitch = new ChannelSwitch));
00551 
00552         vector_member_ptrs<FileSink> fileSinks(nShares);
00553         string channel;
00554         for (int i=0; i<nShares; i++)
00555         {
00556                 char extension[5] = ".000";
00557                 extension[1]='0'+byte(i/100);
00558                 extension[2]='0'+byte((i/10)%10);
00559                 extension[3]='0'+byte(i%10);
00560                 fileSinks[i].reset(new FileSink((string(filename)+extension).c_str()));
00561 
00562                 channel = WordToString<word32>(i);
00563                 fileSinks[i]->Put((byte *)channel.data(), 4);
00564                 channelSwitch->AddRoute(channel, *fileSinks[i], BufferedTransformation::NULL_CHANNEL);
00565         }
00566 
00567         source.PumpAll();
00568 }
00569 
00570 void InformationRecoverFile(int threshold, const char *outFilename, char *const *inFilenames)
00571 {
00572         assert(threshold<=1000);
00573 
00574         InformationRecovery recovery(threshold, new FileSink(outFilename));
00575 
00576         vector_member_ptrs<FileSource> fileSources(threshold);
00577         SecByteBlock channel(4);
00578         int i;
00579         for (i=0; i<threshold; i++)
00580         {
00581                 fileSources[i].reset(new FileSource(inFilenames[i], false));
00582                 fileSources[i]->Pump(4);
00583                 fileSources[i]->Get(channel, 4);
00584                 fileSources[i]->Attach(new ChannelSwitch(recovery, string((char *)channel.begin(), 4)));
00585         }
00586 
00587         while (fileSources[0]->Pump(256))
00588                 for (i=1; i<threshold; i++)
00589                         fileSources[i]->Pump(256);
00590 
00591         for (i=0; i<threshold; i++)
00592                 fileSources[i]->PumpAll();
00593 }
00594 
00595 void GzipFile(const char *in, const char *out, int deflate_level)
00596 {
00597 //      FileSource(in, true, new Gzip(new FileSink(out), deflate_level));
00598 
00599         // use a filter graph to compare decompressed data with original
00600         //
00601         // Source ----> Gzip ------> Sink
00602         //    \           |
00603         //          \       Gunzip
00604         //                \       |
00605         //                  \     v
00606         //                    > ComparisonFilter 
00607                            
00608         EqualityComparisonFilter comparison;
00609 
00610         Gunzip gunzip(new ChannelSwitch(comparison, "0"));
00611         gunzip.SetAutoSignalPropagation(0);
00612 
00613         FileSink sink(out);
00614 
00615         ChannelSwitch *cs;
00616         Gzip gzip(cs = new ChannelSwitch(sink), deflate_level);
00617         cs->AddDefaultRoute(gunzip);
00618 
00619         cs = new ChannelSwitch(gzip);
00620         cs->AddDefaultRoute(comparison, "1");
00621         FileSource source(in, true, cs);
00622 
00623         comparison.ChannelMessageSeriesEnd("0");
00624         comparison.ChannelMessageSeriesEnd("1");
00625 }
00626 
00627 void GunzipFile(const char *in, const char *out)
00628 {
00629         FileSource(in, true, new Gunzip(new FileSink(out)));
00630 }
00631 
00632 void Base64Encode(const char *in, const char *out)
00633 {
00634         FileSource(in, true, new Base64Encoder(new FileSink(out)));
00635 }
00636 
00637 void Base64Decode(const char *in, const char *out)
00638 {
00639         FileSource(in, true, new Base64Decoder(new FileSink(out)));
00640 }
00641 
00642 void HexEncode(const char *in, const char *out)
00643 {
00644         FileSource(in, true, new HexEncoder(new FileSink(out)));
00645 }
00646 
00647 void HexDecode(const char *in, const char *out)
00648 {
00649         FileSource(in, true, new HexDecoder(new FileSink(out)));
00650 }
00651 
00652 void ForwardTcpPort(const char *sourcePortName, const char *destinationHost, const char *destinationPortName)
00653 {
00654 #ifdef SOCKETS_AVAILABLE
00655         SocketsInitializer sockInit;
00656 
00657         Socket sockListen, sockSource, sockDestination;
00658 
00659         int sourcePort = Socket::PortNameToNumber(sourcePortName);
00660         int destinationPort = Socket::PortNameToNumber(destinationPortName);
00661 
00662         sockListen.Create();
00663         sockListen.Bind(sourcePort);
00664         setsockopt(sockListen, IPPROTO_TCP, TCP_NODELAY, "\x01", 1);
00665 
00666         cout << "Listing on port " << sourcePort << ".\n";
00667         sockListen.Listen();
00668 
00669         sockListen.Accept(sockSource);
00670         cout << "Connection accepted on port " << sourcePort << ".\n";
00671         sockListen.CloseSocket();
00672 
00673         cout << "Making connection to " << destinationHost << ", port " << destinationPort << ".\n";
00674         sockDestination.Create();
00675         sockDestination.Connect(destinationHost, destinationPort);
00676 
00677         cout << "Connection made to " << destinationHost << ", starting to forward.\n";
00678 
00679         SocketSource out(sockSource, false, new SocketSink(sockDestination));
00680         SocketSource in(sockDestination, false, new SocketSink(sockSource));
00681 
00682         WaitObjectContainer waitObjects;
00683 
00684         while (!(in.SourceExhausted() && out.SourceExhausted()))
00685         {
00686                 waitObjects.Clear();
00687 
00688                 out.GetWaitObjects(waitObjects);
00689                 in.GetWaitObjects(waitObjects);
00690 
00691                 waitObjects.Wait(INFINITE_TIME);
00692 
00693                 if (!out.SourceExhausted())
00694                 {
00695                         cout << "o" << flush;
00696                         out.PumpAll2(false);
00697                         if (out.SourceExhausted())
00698                                 cout << "EOF received on source socket.\n";
00699                 }
00700 
00701                 if (!in.SourceExhausted())
00702                 {
00703                         cout << "i" << flush;
00704                         in.PumpAll2(false);
00705                         if (in.SourceExhausted())
00706                                 cout << "EOF received on destination socket.\n";
00707                 }
00708         }
00709 #else
00710         cout << "Socket support was not enabled at compile time.\n";
00711         exit(-1);
00712 #endif
00713 }
00714 
00715 bool Validate(int alg, bool thorough, const char *seed)
00716 {
00717         bool result;
00718 
00719         std::string timeSeed;
00720         if (!seed)
00721         {
00722                 timeSeed = IntToString(time(NULL));
00723                 seed = timeSeed.c_str();
00724         }
00725 
00726         cout << "Using seed: " << seed << endl << endl;
00727         GlobalRNG().Put((const byte *)seed, strlen(seed));
00728 
00729         switch (alg)
00730         {
00731         case 1: result = TestSettings(); break;
00732         case 2: result = TestOS_RNG(); break;
00733         case 3: result = ValidateMD5(); break;
00734         case 4: result = ValidateSHA(); break;
00735         case 5: result = ValidateDES(); break;
00736         case 6: result = ValidateIDEA(); break;
00737         case 7: result = ValidateARC4(); break;
00738         case 8: result = ValidateRC5(); break;
00739         case 9: result = ValidateBlowfish(); break;
00740 //      case 10: result = ValidateDiamond2(); break;
00741         case 11: result = ValidateThreeWay(); break;
00742         case 12: result = ValidateBBS(); break;
00743         case 13: result = ValidateDH(); break;
00744         case 14: result = ValidateRSA(); break;
00745         case 15: result = ValidateElGamal(); break;
00746         case 16: result = ValidateDSA(thorough); break;
00747         case 17: result = ValidateHAVAL(); break;
00748         case 18: result = ValidateSAFER(); break;
00749         case 19: result = ValidateLUC(); break;
00750         case 20: result = ValidateRabin(); break;
00751 //      case 21: result = ValidateBlumGoldwasser(); break;
00752         case 22: result = ValidateECP(); break;
00753         case 23: result = ValidateEC2N(); break;
00754         case 24: result = ValidateMD5MAC(); break;
00755         case 25: result = ValidateGOST(); break;
00756         case 26: result = ValidateTiger(); break;
00757         case 27: result = ValidateRIPEMD(); break;
00758         case 28: result = ValidateHMAC(); break;
00759         case 29: result = ValidateXMACC(); break;
00760         case 30: result = ValidateSHARK(); break;
00761         case 32: result = ValidateLUC_DH(); break;
00762         case 33: result = ValidateLUC_DL(); break;
00763         case 34: result = ValidateSEAL(); break;
00764         case 35: result = ValidateCAST(); break;
00765         case 36: result = ValidateSquare(); break;
00766         case 37: result = ValidateRC2(); break;
00767         case 38: result = ValidateRC6(); break;
00768         case 39: result = ValidateMARS(); break;
00769         case 40: result = ValidateRW(); break;
00770         case 41: result = ValidateMD2(); break;
00771         case 42: result = ValidateNR(); break;
00772         case 43: result = ValidateMQV(); break;
00773         case 44: result = ValidateRijndael(); break;
00774         case 45: result = ValidateTwofish(); break;
00775         case 46: result = ValidateSerpent(); break;
00776         case 47: result = ValidateCipherModes(); break;
00777         case 48: result = ValidateCRC32(); break;
00778         case 49: result = ValidateECDSA(); break;
00779         case 50: result = ValidateXTR_DH(); break;
00780         case 51: result = ValidateSKIPJACK(); break;
00781         case 52: result = ValidateSHA2(); break;
00782         case 53: result = ValidatePanama(); break;
00783         case 54: result = ValidateAdler32(); break;
00784         case 55: result = ValidateMD4(); break;
00785         case 56: result = ValidatePBKDF(); break;
00786         case 57: result = ValidateESIGN(); break;
00787         case 58: result = ValidateDLIES(); break;
00788         case 59: result = ValidateBaseCode(); break;
00789         case 60: result = ValidateSHACAL2(); break;
00790         case 61: result = ValidateCamellia(); break;
00791         case 62: result = ValidateWhirlpool(); break;
00792         case 63: result = ValidateTTMAC(); break;
00793         default: result = ValidateAll(thorough); break;
00794         }
00795 
00796         time_t endTime = time(NULL);
00797         cout << "\nTest ended at " << asctime(localtime(&endTime));
00798         cout << "Seed used was: " << seed << endl;
00799 
00800         return result;
00801 }

Generated on Tue Aug 16 08:38:43 2005 for Crypto++ by  doxygen 1.3.9.1