t_ice_key.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_ice_key                                                      *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/byte_array.h>
00016 #include <basis/chaos.h>
00017 #include <basis/function.h>
00018 #include <basis/guards.h>
00019 #include <basis/istring.h>
00020 #include <crypto/ice_key.h>
00021 #include <loggers/file_logger.h>
00022 #include <opsystem/path_configuration.h>
00023 #include <data_struct/static_memory_gremlin.h>
00024 #include <textual/byte_format.h>
00025 
00026 HOOPLE_STARTUP_CODE;
00027 
00028 #define LOG(s) program_wide_logger().log(s)
00029 
00030 // define the specific keys that we use.
00031 byte TEST_KEY_1[] = {
00032   0x28, 0x94, 0x32, 0x99, 0xFE, 0x2B, 0x9F, 0x75,
00033   0x47, 0x26, 0xC8, 0x37, 0xED, 0x59, 0x8E, 0x1B
00034 };
00035 
00036 byte TEST_KEY_2[] = {
00037   0x92, 0x18, 0x2C, 0xF8, 0xCE, 0xEE, 0x34, 0xD0,
00038   0x91, 0x23, 0x85, 0x5C, 0xBB, 0x7A, 0x98, 0x85,
00039   0x12, 0x02, 0xF0, 0xE8, 0x14, 0x83, 0x9E, 0x3D,
00040   0x38, 0x23, 0xB0, 0x9D, 0x7D, 0x3D, 0x92, 0x94
00041 };
00042 
00043 #define PROGRAM "ice_key tester"
00044 
00045 const int TEST_ITERATIONS = 8;
00046 
00047 int main(int formal(argc), char *formal(argv)[])
00048 {
00049   SET_DEFAULT_COMBO_LOGGER;
00050 
00051   {
00052     // test group 1: test using a particular key.
00053     #define GROUP "test group 1: specific key"
00054 
00055     byte_array the_key(sizeof(TEST_KEY_1), TEST_KEY_1);
00056     ice_key encryptor(sizeof(TEST_KEY_1) / 8);
00057     encryptor.set(the_key);
00058 
00059     istring to_encrypt_1("I solely forged the permaderm butter arch, man.");
00060     byte_array tmp1(to_encrypt_1.length() + 1, (byte *)to_encrypt_1.s());
00061     byte_array encrypted1;
00062     encryptor.encrypt(tmp1, encrypted1);
00063     LOG("test 1: encoded form...");
00064     istring dumped_1a = byte_format::text_dump(encrypted1.observe(), encrypted1.length());
00065     LOG(dumped_1a);
00066 
00067     byte_array decoded1;
00068     encryptor.decrypt(encrypted1, decoded1);
00069     if (tmp1.length() != decoded1.length())
00070       deadly_error(PROGRAM, GROUP, "the length of the returned data "
00071           "is incorrect");
00072     istring got_back_1((char *)decoded1.observe());
00073     if (got_back_1 != to_encrypt_1) 
00074       deadly_error(PROGRAM, GROUP, "the decrypted data is erroneous");
00075     #undef GROUP
00076   }
00077 
00078   {
00079     // test group 2: using a longer key.
00080     #define GROUP "test group 2: longer key"
00081 
00082     byte_array the_key(sizeof(TEST_KEY_2), TEST_KEY_2);
00083     ice_key encryptor(sizeof(TEST_KEY_2) / 8);
00084     encryptor.set(the_key);
00085 
00086     istring to_encrypt_2("I solely forged the permaderm butter arch, man.");
00087     byte_array tmp2(to_encrypt_2.length() + 1, (byte *)to_encrypt_2.s());
00088     byte_array encrypted2;
00089     encryptor.encrypt(tmp2, encrypted2);
00090     LOG("test 2: encoded form...");
00091     istring dumped_2a = byte_format::text_dump(encrypted2.observe(), encrypted2.length());
00092     LOG(dumped_2a);
00093 
00094     byte_array decoded2;
00095     encryptor.decrypt(encrypted2, decoded2);
00096     if (tmp2.length() != decoded2.length())
00097       deadly_error(PROGRAM, GROUP, "the length of the returned data "
00098           "is incorrect");
00099     istring got_back_2((char *)decoded2.observe());
00100     if (got_back_2 != to_encrypt_2) 
00101       deadly_error(PROGRAM, GROUP, "the decrypted data is erroneous");
00102     #undef GROUP
00103   }
00104   {
00105     // test group 3: random keys and random encodings.
00106     #define GROUP "test group 3: random keys"
00107 
00108     chaos randomizer;
00109 
00110     for (int outer = 0; outer < TEST_ITERATIONS; outer++) {
00111       // pick a random key size.
00112       int key_size = randomizer.inclusive(1, 6);
00113       ice_key encryptor(key_size);
00114 
00115       // pick a random key.
00116       byte_array the_key(key_size * 8);
00117       int i;  // loop variable used in multiple loops.
00118       for (i = 0; i < the_key.length(); i++)
00119         the_key[i] = byte(randomizer.inclusive(0, 255));
00120 
00121       encryptor.set(the_key);
00122 
00123       // pick something to encode.
00124       byte_array to_encode(randomizer.inclusive(50, 53849));
00125       for (i = 0; i < to_encode.length(); i++)
00126         to_encode[i] = byte(randomizer.inclusive(0, 255));
00127 
00128       // now encode it.
00129       byte_array encoded_form;
00130       encryptor.encrypt(to_encode, encoded_form);
00131 
00132       // now, with little ado, decode it.
00133       byte_array decoded_form;
00134       encryptor.decrypt(encoded_form, decoded_form);
00135 
00136       if (to_encode.length() != decoded_form.length())
00137         deadly_error(PROGRAM, GROUP, "the length of the returned data "
00138             "is incorrect");
00139       for (i = 0; i < decoded_form.length(); i++)
00140         if (to_encode[i] != decoded_form[i]) 
00141           deadly_error(PROGRAM, GROUP, "the decrypted data is erroneous");
00142       #undef GROUP
00143     }
00144     
00145   }
00146 
00147   istring to_print("ice_key:: works for those functions tested.");
00148   guards::alert_message(to_print.s());
00149 
00150   return 0;
00151 }

Generated on Fri Oct 10 04:29:00 2008 for HOOPLE Libraries by  doxygen 1.5.1