summaryrefslogtreecommitdiff
path: root/validat2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'validat2.cpp')
-rw-r--r--validat2.cpp128
1 files changed, 122 insertions, 6 deletions
diff --git a/validat2.cpp b/validat2.cpp
index 496eaaa7..2faeaa51 100644
--- a/validat2.cpp
+++ b/validat2.cpp
@@ -24,6 +24,7 @@
#include "rw.h"
#include "eccrypto.h"
#include "integer.h"
+#include "gf2n.h"
#include "ecp.h"
#include "ec2n.h"
#include "asn.h"
@@ -35,6 +36,7 @@
#include "smartptr.h"
#include <iostream>
+#include <sstream>
#include <iomanip>
#include "validate.h"
@@ -275,7 +277,7 @@ bool ValidateRSA()
{
const char *plain = "Everyone gets Friday off.";
- byte *signature = (byte *)
+ static const byte signature[] =
"\x05\xfa\x6a\x81\x2f\xc7\xdf\x8b\xf4\xf2\x54\x25\x09\xe0\x3e\x84"
"\x6e\x11\xb9\xc6\x20\xbe\x20\x09\xef\xb4\x40\xef\xbc\xc6\x69\x21"
"\x69\x94\xac\x04\xf3\x41\xb5\x7d\x05\x20\x2d\x42\x8f\xb2\xa2\x7b"
@@ -321,12 +323,12 @@ bool ValidateRSA()
{
byte *plain = (byte *)
"\x54\x85\x9b\x34\x2c\x49\xea\x2a";
- byte *encrypted = (byte *)
+ static const byte encrypted[] =
"\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a"
"\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4"
"\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52"
"\x62\x51";
- byte *oaepSeed = (byte *)
+ static const byte oaepSeed[] =
"\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2"
"\xf0\x6c\xb5\x8f";
ByteQueue bq;
@@ -549,6 +551,120 @@ bool ValidateBlumGoldwasser()
}
*/
+#if !defined(NDEBUG) && !defined(CRYPTOPP_IMPORTS)
+// Issue 64: "PolynomialMod2::operator<<=", http://github.com/weidai11/cryptopp/issues/64
+bool TestPolynomialMod2()
+{
+ bool pass1 = true, pass2 = true, pass3 = true;
+
+ cout << "\nTesting PolynomialMod2 bit operations...\n\n";
+
+ static const unsigned int start = 0;
+ static const unsigned int stop = 4 * WORD_BITS + 1;
+
+ for (unsigned int i=start; i < stop; i++)
+ {
+ PolynomialMod2 p(1);
+ p <<= i;
+
+ Integer n(Integer::One());
+ n <<= i;
+
+ std::ostringstream oss1;
+ oss1 << p;
+
+ std::string str1, str2;
+
+ // str1 needs the commas removed used for grouping
+ str1 = oss1.str();
+ str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());
+
+ // str1 needs the trailing 'b' removed
+ str1.erase(str1.end() - 1);
+
+ // str2 is fine as-is
+ str2 = IntToString(n, 2);
+
+ pass1 &= (str1 == str2);
+ }
+
+ for (unsigned int i=start; i < stop; i++)
+ {
+ const word w(SIZE_MAX);
+
+ PolynomialMod2 p(w);
+ p <<= i;
+
+ Integer n(Integer::POSITIVE, static_cast<lword>(w));
+ n <<= i;
+
+ std::ostringstream oss1;
+ oss1 << p;
+
+ std::string str1, str2;
+
+ // str1 needs the commas removed used for grouping
+ str1 = oss1.str();
+ str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());
+
+ // str1 needs the trailing 'b' removed
+ str1.erase(str1.end() - 1);
+
+ // str2 is fine as-is
+ str2 = IntToString(n, 2);
+
+ pass2 &= (str1 == str2);
+ }
+
+ RandomNumberGenerator& prng = GlobalRNG();
+ for (unsigned int i=start; i < stop; i++)
+ {
+ word w; // Cast to lword due to Visual Studio
+ prng.GenerateBlock((byte*)&w, sizeof(w));
+
+ PolynomialMod2 p(w);
+ p <<= i;
+
+ Integer n(Integer::POSITIVE, static_cast<lword>(w));
+ n <<= i;
+
+ std::ostringstream oss1;
+ oss1 << p;
+
+ std::string str1, str2;
+
+ // str1 needs the commas removed used for grouping
+ str1 = oss1.str();
+ str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());
+
+ // str1 needs the trailing 'b' removed
+ str1.erase(str1.end() - 1);
+
+ // str2 is fine as-is
+ str2 = IntToString(n, 2);
+
+ if (str1 != str2)
+ {
+ cout << " Oops..." << "\n";
+ cout << " random: " << std::hex << n << std::dec << "\n";
+ cout << " str1: " << str1 << "\n";
+ cout << " str2: " << str2 << "\n";
+ }
+
+ pass3 &= (str1 == str2);
+ }
+
+ cout << (!pass1 ? "FAILED" : "passed") << " " << "1 shifted over range [0," << 2 * WORD_BITS + 1 << "]" << "\n";
+ cout << (!pass2 ? "FAILED" : "passed") << " " << "0x" << hex << word(SIZE_MAX) << " shifted over range [0," << dec << 2 * WORD_BITS + 1 << "]" << "\n";
+ cout << (!pass3 ? "FAILED" : "passed") << " " << "random values shifted over range [0," << dec << 2 * WORD_BITS + 1 << "]" << "\n";
+
+ if (!(pass1 && pass2 && pass3))
+ cout.flush();
+
+ return pass1 && pass2 && pass3;
+}
+#endif
+
bool ValidateECP()
{
cout << "\nECP validation suite running...\n\n";
@@ -668,7 +784,7 @@ bool ValidateECDSA()
Integer h("A9993E364706816ABA3E25717850C26C9CD0D89DH");
Integer k("3eeace72b4919d991738d521879f787cb590aff8189d2b69H");
- byte sig[]="\x03\x8e\x5a\x11\xfb\x55\xe4\xc6\x54\x71\xdc\xd4\x99\x84\x52\xb1\xe0\x2d\x8a\xf7\x09\x9b\xb9\x30"
+ static const byte sig[]="\x03\x8e\x5a\x11\xfb\x55\xe4\xc6\x54\x71\xdc\xd4\x99\x84\x52\xb1\xe0\x2d\x8a\xf7\x09\x9b\xb9\x30"
"\x0c\x9a\x08\xc3\x44\x68\xc2\x44\xb4\xe5\xd6\xb2\x1b\x3c\x68\x36\x28\x07\x41\x60\x20\x32\x8b\x6e";
Integer r(sig, 24);
Integer s(sig+24, 24);
@@ -703,8 +819,8 @@ bool ValidateESIGN()
bool pass = true, fail;
- const char *plain = "test";
- const byte *signature = (byte *)
+ static const char plain[] = "test";
+ static const byte signature[] =
"\xA3\xE3\x20\x65\xDE\xDA\xE7\xEC\x05\xC1\xBF\xCD\x25\x79\x7D\x99\xCD\xD5\x73\x9D\x9D\xF3\xA4\xAA\x9A\xA4\x5A\xC8\x23\x3D\x0D\x37\xFE\xBC\x76\x3F\xF1\x84\xF6\x59"
"\x14\x91\x4F\x0C\x34\x1B\xAE\x9A\x5C\x2E\x2E\x38\x08\x78\x77\xCB\xDC\x3C\x7E\xA0\x34\x44\x5B\x0F\x67\xD9\x35\x2A\x79\x47\x1A\x52\x37\x71\xDB\x12\x67\xC1\xB6\xC6"
"\x66\x73\xB3\x40\x2E\xD6\xF2\x1A\x84\x0A\xB6\x7B\x0F\xEB\x8B\x88\xAB\x33\xDD\xE4\x83\x21\x90\x63\x2D\x51\x2A\xB1\x6F\xAB\xA7\x5C\xFD\x77\x99\xF2\xE1\xEF\x67\x1A"