testDES.cpp

Go to the documentation of this file.
00001 /*
00002  * testDES.cpp
00003  *
00004  * Copyright (C) 2007  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * Quick test for symmetric encryption.
00031  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "crypto/crypto.h"      // always include our own header file first!
00035 
00036 #include <iostream>
00037 
00038 #include "common/wave_ex.h"
00039 #include "perf/perf.h"
00040 
00041 
00042 struct test_t {
00043         const char *    input;
00044         int             minSize;
00045 };
00046 
00047 
00048 static const test_t s_tests[] = {
00049         // input string                                 // min size
00050         { "test",                                       0 },
00051         { "test",                                       1 },
00052         { "test",                                       4 },
00053         { "test",                                       100 },
00054         { "test",                                       -5 },
00055         { "Hello World!",                               0 },
00056         { "Hello World!",                               8 },
00057         { "Hello World!",                               12 },
00058         { "Hello World!",                               -4 },
00059         { "Hello World!",                               25 },
00060         { "",                                           0 },
00061         { "",                                           -1 },
00062         { "",                                           1 },
00063         { "",                                           16 },
00064         { "abcdefghijklmnopqrstuvwxyz",                 0 },
00065         { "ABCDEFGHIJKLMNOPQRSTUVWXYZ",                 0 },
00066         { "01234567890",                                0 },
00067         { ":",                                          0 },
00068         { ":",                                          -1 },
00069         { ":",                                          5 },
00070 
00071         // must be last!
00072         { NULL, 0 }
00073 };
00074 
00075 
00076 
00077 ////////////////////////////////////////////////////////////////////////////////
00078 //
00079 //      static helper methods
00080 //
00081 ////////////////////////////////////////////////////////////////////////////////
00082 
00083 static void
00084 doTest
00085 (
00086 IN crypto::DESKey * key,
00087 IN const char * data,
00088 IN int minSize
00089 )
00090 {
00091         ASSERT(key, "null");
00092         ASSERT(data, "null");
00093 
00094         long bytes = strlen(data);
00095         DPRINTF("'%s': %ld bytes", data, bytes);
00096         DPRINTF("  Requesting min size of %d bytes", minSize);
00097 
00098         std::string enc = key->encrypt(data, minSize);
00099         DPRINTF("  Encrypted: '%s'", enc.c_str());
00100 
00101         std::string dec = key->decrypt(enc.c_str());
00102         DPRINTF("  Decrypted: '%s'", dec.c_str());
00103         if (data != dec) {
00104                 WAVE_EX(wex);
00105                 wex << "Decrypted string does not match input!";
00106         }
00107 }
00108 
00109 
00110 
00111 ////////////////////////////////////////////////////////////////////////////////
00112 //
00113 //      entry point
00114 //
00115 ////////////////////////////////////////////////////////////////////////////////
00116 
00117 int
00118 main
00119 (
00120 IN int argc,
00121 IN const char * argv[]
00122 )
00123 {
00124         int retval = 0;
00125         try {
00126                 smart_ptr<crypto::DESKey> key = crypto::DESKey::create();
00127                 if (!key) {
00128                         WAVE_EX(wex);
00129                         wex << "Failed to create DES key?";
00130                 }
00131 
00132                 for (const test_t * p = s_tests; p->input; ++p) {
00133                         doTest(key, p->input, p->minSize);
00134                 }
00135 
00136         } catch (std::exception& e) {
00137                 DPRINTF("Exception: %s", e.what());
00138                 retval = 1;
00139         } catch (...) {
00140                 DPRINTF("Unknown exception!");
00141                 retval = 2;
00142         }
00143         perf::dumpTimingSummary(std::cerr);
00144 
00145         return retval;
00146 }
00147