• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestRSA.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/Debug.h>
00018 #include <spl/Log.h>
00019 #include <spl/math/RSA.h>
00020 
00021 #ifdef DEBUG
00022 
00023 static void _TestRSA1()
00024 {
00025         RSA rsa(32);
00026 
00027         const byte *txt = (const byte *)"No is the time";
00028         Array<byte> pt(txt, (int)strlen((const char *)txt));
00029         rsa.PadInputBuffer(pt);
00030 
00031         RefCountPtr<Array<byte> > enc = rsa.EncryptBinary(pt);
00032         RefCountPtr<Array<byte> > dec = rsa.DecryptBinary(*enc);
00033         UNIT_ASSERT("bin comp", pt.Equals(*dec));
00034         String str((const char *)dec->Data(), dec->Length());
00035         UNIT_ASSERT("str", str.Equals((const char *)txt));
00036 
00037         DEBUG_CLEAR_MEM_CHECK_POINTS();
00038         rsa.CheckMem();
00039         pt.CheckMem();
00040         enc.CheckMem();
00041         dec.CheckMem();
00042         str.CheckMem();
00043         DEBUG_DUMP_MEM_LEAKS();
00044         UNIT_ASSERT_MEM_NOTED("RSA 1.0");
00045                 
00046         Log::SWriteOkFail( "RSA test" );
00047 }
00048 
00049 static void _TestRSA384Bits()
00050 {
00051         RSA rsa(128, 1);
00052 
00053         const byte *txt = (const byte *)"Now is the time for all good monkeys to come to the aid of their party.";
00054         Array<byte> pt(txt, (int)strlen((const char *)txt));
00055         rsa.PadInputBuffer(pt);
00056 
00057         RefCountPtr<Array<byte> > enc = rsa.EncryptBinary(pt);
00058         RefCountPtr<Array<byte> > dec = rsa.DecryptBinary(*enc);
00059         UNIT_ASSERT("bin comp", pt.Equals(*dec));
00060         String str((const char *)dec->Data(), dec->Length());
00061         UNIT_ASSERT("str", str.Equals((const char *)txt));
00062 
00063         DEBUG_CLEAR_MEM_CHECK_POINTS();
00064         rsa.CheckMem();
00065         pt.CheckMem();
00066         enc.CheckMem();
00067         dec.CheckMem();
00068         str.CheckMem();
00069         DEBUG_DUMP_MEM_LEAKS();
00070         UNIT_ASSERT_MEM_NOTED("RSA 1.0");
00071                 
00072         Log::SWriteOkFail( "RSA 128 bit test" );
00073 }
00074 
00075 static void _TestRSAFizz()
00076 {
00077         const byte *txt = (const byte *)"Now is the time$~ ";
00078         Array<byte> pt(txt, (int)strlen((const char *)txt));
00079 
00080         for( int x = 0; x < 32; x++)
00081         {
00082                 RSA rsa(32);
00083 
00084                 RefCountPtr<Array<byte> > enc = rsa.EncryptBinary(pt);
00085                 RefCountPtr<Array<byte> > dec = rsa.DecryptBinary(*enc);
00086                 UNIT_ASSERT("bin comp", pt.Equals(*dec));
00087                 String str((const char *)dec->Data(), dec->Length());
00088                 UNIT_ASSERT("str", str.Equals((const char *)txt));
00089         }
00090 
00091         DEBUG_CLEAR_MEM_CHECK_POINTS();
00092         pt.CheckMem();
00093         DEBUG_DUMP_MEM_LEAKS();
00094         UNIT_ASSERT_MEM_NOTED("RSA 1.0");
00095                 
00096         Log::SWriteOkFail( "RSA fizzing" );
00097 }
00098 
00099 void _TestRSA()
00100 {
00101         _TestRSA1();
00102         DEBUG_CLEAR_MEM_CHECK_POINTS();
00103         DEBUG_DUMP_MEM_LEAKS();
00104 
00105         _TestRSA384Bits();
00106         DEBUG_CLEAR_MEM_CHECK_POINTS();
00107         DEBUG_DUMP_MEM_LEAKS();
00108 
00109         _TestRSAFizz();
00110         DEBUG_CLEAR_MEM_CHECK_POINTS();
00111         DEBUG_DUMP_MEM_LEAKS();
00112 }
00113 
00114 #endif