crypto_test.cpp

00001 #include <iostream> 00002 #include "gm/cryptography.h" 00003 00004 using namespace std; 00005 using namespace GNUMessenger; 00006 00007 int failures = 0; 00008 00009 int testHashes() { 00010 00011 string input; 00012 unsigned int len; 00013 // MD5 validate 00014 input = "abc"; 00015 00016 byte * result = CryptoManager::hash(input, len, CryptDefines::MD_5); 00017 byte benchmark[16] = { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 00018 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 }; 00019 00020 cout << "Testing MD5: "; 00021 00022 if (memcmp(result, benchmark, 16) == 0) 00023 cout << "Passed.\n"; 00024 else { 00025 cout << "Failed!\n"; 00026 failures++; 00027 } 00028 00029 delete [] result; 00030 00031 return 0; 00032 } 00033 00034 int testRNG() 00035 { 00036 const unsigned int inSize = 10000; 00037 unsigned int result = CryptoManager::testRNG(inSize); 00038 00039 cout << "Testing OS RNG: " << inSize << " reduced to " << result; 00040 00041 if (result < inSize) { 00042 cout << " Failed!\n"; 00043 failures++; 00044 } else 00045 cout << " Passed.\n"; 00046 00047 } 00048 00049 int testBlockCipher() 00050 { 00051 00052 const byte key[] = { 00053 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }; 00054 const byte iv[] = { 00055 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef }; 00056 00057 const byte plain[] = { // "Now is the time for all " without tailing 0 00058 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74, 00059 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20, 00060 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20 }; 00061 00062 const byte decryptionIV[] = { 00063 0x4D, 0xD0, 0xAC, 0x8F, 0x47, 0xCF, 0x79, 0xCE }; 00064 const byte encrypted[] = { 00065 0x12, 0x34, 0x56 }; 00066 00067 unsigned int resultLen; 00068 byte * result = CryptoManager::encrypt(SecByteBlock(plain, sizeof(plain)), 00069 SecByteBlock(key, sizeof(key)), 00070 resultLen, 0, CryptDefines::_DES); 00071 00072 if (memcmp(result, encrypted, 3) == 0) 00073 cout << "Passed.\n"; 00074 else { 00075 cout << "Failed: " 00076 << CryptoManager::encode(SecByteBlock(result, resultLen)) 00077 << endl; 00078 failures++; 00079 } 00080 delete [] result; 00081 00082 } 00083 00084 int testEncode() 00085 { 00086 cout << "Testing HEX Encode: "; 00087 00088 byte block[16] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 00089 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x10 }; 00090 00091 string encoded = CryptoManager::encode(SecByteBlock(block, 16)); 00092 00093 cout << encoded; 00094 if (encoded == "1112131415161718191A1B1C1D1E1F10") 00095 cout << " Passed.\n"; 00096 else { 00097 cout << " Failed.\n"; 00098 failures++; 00099 } 00100 return 0; 00101 00102 } 00103 00104 int main() { 00105 00106 cout << "Starting crypto manager testing" << endl; 00107 00108 testHashes(); 00109 testEncode(); 00110 testRNG(); 00111 00112 return failures; 00113 }

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8