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

# test.cpp

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