7#ifndef CRYPTOPP_IMPORTS
8#define CRYPTOPP_DEFAULT_NO_DLL
19class LineBreakParser :
public AutoSignaling<Bufferless<Filter> >
23 : m_lineEnd(lineEnd) {Detach(attachment);}
25 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
28 throw BlockingInputOnly(
"LineBreakParser");
30 unsigned int i, last = 0;
31 for (i=0; i<length; i++)
33 if (begin[i] == m_lineEnd)
40 AttachedTransformation()->Put2(begin+last, i-last, 0, blocking);
55class TestDataParser :
public Unflushable<FilterWithInputQueue>
58 enum DataType {OTHER, COUNT, KEY_T,
IV, INPUT, OUTPUT};
60 TestDataParser(std::string algorithm, std::string test, std::string mode,
unsigned int feedbackSize,
bool encrypt,
BufferedTransformation *attachment)
61 : m_algorithm(algorithm), m_test(test), m_mode(mode), m_feedbackSize(feedbackSize)
62 , m_firstLine(true), m_blankLineTransition(0)
66 m_typeToName[COUNT] =
"COUNT";
68 m_nameToType[
"COUNT"] = COUNT;
69 m_nameToType[
"KEY"] = KEY_T;
70 m_nameToType[
"KEYs"] = KEY_T;
71 m_nameToType[
"key"] = KEY_T;
72 m_nameToType[
"Key"] = KEY_T;
73 m_nameToType[
"IV"] =
IV;
74 m_nameToType[
"IV1"] =
IV;
75 m_nameToType[
"CV"] =
IV;
76 m_nameToType[
"CV1"] =
IV;
77 m_nameToType[
"IB"] =
IV;
78 m_nameToType[
"TEXT"] = INPUT;
79 m_nameToType[
"RESULT"] = OUTPUT;
80 m_nameToType[
"Msg"] = INPUT;
81 m_nameToType[
"Seed"] = INPUT;
82 m_nameToType[
"V"] = INPUT;
83 m_nameToType[
"DT"] =
IV;
86 if (m_algorithm ==
"DSA" || m_algorithm ==
"ECDSA")
90 else if (m_test ==
"KeyPair")
92 else if (m_test ==
"SigGen")
94 else if (m_test ==
"SigVer")
96 else if (m_test ==
"PQGGen")
98 else if (m_test ==
"PQGVer")
101 else if (m_algorithm ==
"HMAC")
103 else if (m_algorithm ==
"SHA")
104 m_trigger = (m_test ==
"MONTE") ?
"Seed" :
"Msg";
105 else if (m_algorithm ==
"RNG")
107 else if (m_algorithm ==
"RSA")
108 m_trigger = (m_test ==
"Ver") ?
"S" :
"Msg";
111 void SetEncrypt(
bool encrypt)
116 m_nameToType[
"PLAINTEXT"] = INPUT;
117 m_nameToType[
"CIPHERTEXT"] = OUTPUT;
118 m_nameToType[
"PT"] = INPUT;
119 m_nameToType[
"CT"] = OUTPUT;
123 m_nameToType[
"PLAINTEXT"] = OUTPUT;
124 m_nameToType[
"CIPHERTEXT"] = INPUT;
125 m_nameToType[
"PT"] = OUTPUT;
126 m_nameToType[
"CT"] = INPUT;
129 if (m_algorithm ==
"AES" || m_algorithm ==
"TDES")
133 m_trigger =
"PLAINTEXT";
134 m_typeToName[OUTPUT] =
"CIPHERTEXT";
138 m_trigger =
"CIPHERTEXT";
139 m_typeToName[OUTPUT] =
"PLAINTEXT";
146 void OutputData(std::string &output,
const std::string &key,
const std::string &data)
154 void OutputData(std::string &output,
const std::string &key,
int data)
159 void OutputData(std::string &output,
const std::string &key,
const SecByteBlock &data)
167 void OutputData(std::string &output,
const std::string &key,
const Integer &data,
int size=-1)
171 OutputData(output, key, s);
174 void OutputData(std::string &output,
const std::string &key,
const PolynomialMod2 &data,
int size=-1)
178 OutputData(output, key, s);
181 void OutputData(std::string &output, DataType t,
const std::string &data)
183 if (m_algorithm ==
"SKIPJACK")
188 output = m_line + data +
"\n";
194 output += m_typeToName[t];
198 output += t == OUTPUT ?
"\n" :
" ";
201 else if (m_algorithm ==
"TDES" && t == KEY_T && m_typeToName[KEY_T].empty())
204 output += data.substr(0, 16);
205 output +=
"\nKEY2 = ";
206 output += data.size() > 16 ? data.substr(16, 16) : data.substr(0, 16);
207 output +=
"\nKEY3 = ";
208 output += data.size() > 32 ? data.substr(32, 16) : data.substr(0, 16);
213 output += m_typeToName[t];
220 void OutputData(std::string &output, DataType t,
int i)
225 void OutputData(std::string &output, DataType t,
const SecByteBlock &data)
229 OutputData(output, t, hexData);
232 void OutputGivenData(std::string &output, DataType t,
bool optional =
false)
234 if (m_data.find(m_typeToName[t]) == m_data.end())
241 OutputData(output, t, m_data[m_typeToName[t]]);
247 if (!m_encrypt && (m_mode ==
"ECB" || m_mode ==
"CBC"))
248 return new typename T::Decryption;
250 return new typename T::Encryption;
257 return new typename T::Decryption(bt, iv, m_feedbackSize/8);
259 return new typename T::Encryption(bt, iv, m_feedbackSize/8);
266 xorbuf(z, x, y, x.size());
271 unsigned int innerCount = (m_algorithm ==
"AES") ? 1000 : 10000;
272 int keySize = key.
size(), blockSize = text[0].
size();
274 for (
int k=0; k<keySize;)
276 int pos = innerCount * blockSize - keySize + k;
277 memcpy(x + k, text[pos / blockSize] + pos % blockSize, blockSize - pos % blockSize);
278 k += blockSize - pos % blockSize;
281 if (m_algorithm ==
"TDES" || m_algorithm ==
"DES")
283 for (
int i=0; i<keySize; i+=8)
285 xorbuf(key+i, x+keySize-8-i, 8);
301 void EC_KeyPair(
string &output,
int n,
const OID &oid)
304 for (
int i=0; i<n; i++)
312 OutputData(output,
"Qx ", pub.
GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
313 OutputData(output,
"Qy ", pub.
GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
318 void EC_SigGen(
string &output,
const OID &oid)
323 priv.Initialize(m_rng, params);
324 priv.MakePublicKey(pub);
329 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
331 OutputData(output,
"Qx ", pub.
GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
332 OutputData(output,
"Qy ", pub.
GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
333 OutputData(output,
"R ", R);
334 OutputData(output,
"S ", S);
338 void EC_SigVer(
string &output,
const OID &oid)
342 Integer r((m_data[
"R"]+
"h").c_str());
343 Integer s((m_data[
"S"]+
"h").c_str());
345 typename EC::FieldElement Qx(x, x.size());
346 typename EC::FieldElement Qy(y, y.
size());
347 typename EC::Element Q(Qx, Qy);
355 r.Encode(sig, sig.size()/2);
356 s.Encode(sig+sig.size()/2, sig.size()/2);
359 filter.Put(sig, sig.size());
364 OutputData(output,
"Result ", b ?
"P" :
"F");
370 typename EC::FieldElement Qx(x, x.size());
371 typename EC::FieldElement Qy(y, y.
size());
372 typename EC::Element Q(Qx, Qy);
380 template <
class H,
class Result>
381 Result * CreateRSA2(
const std::string &standard)
387 else if (standard ==
"P")
389 else if (standard ==
"1")
392 else if (
typeid(Result) ==
typeid(
PK_Signer))
396 else if (standard ==
"P")
398 else if (standard ==
"1")
405 template <
class Result>
406 Result * CreateRSA(
const std::string &standard,
const std::string &hash)
409 return CreateRSA2<SHA1, Result>(standard);
410 else if (hash ==
"224")
411 return CreateRSA2<SHA224, Result>(standard);
412 else if (hash ==
"256")
413 return CreateRSA2<SHA256, Result>(standard);
414 else if (hash ==
"384")
415 return CreateRSA2<SHA384, Result>(standard);
416 else if (hash ==
"512")
417 return CreateRSA2<SHA512, Result>(standard);
422 virtual void DoTest()
426 if (m_algorithm ==
"DSA")
428 if (m_test ==
"KeyPair")
431 int modLen = atol(m_bracketString.substr(6).c_str());
438 int n = atol(m_data[
"N"].c_str());
439 for (
int i=0; i<n; i++)
442 priv.AccessKey().GenerateRandom(m_rng, pqg);
445 OutputData(output,
"X ", priv.GetKey().GetPrivateExponent());
447 AttachedTransformation()->Put((
byte *)output.data(), output.size());
451 else if (m_test ==
"PQGGen")
453 int n = atol(m_data[
"N"].c_str());
454 for (
int i=0; i<n; i++)
462 m_rng.GenerateBlock(seed, seed.size());
464 while (!DSA::GeneratePrimes(seed, seed.size()*8, counter, p, 1024, q));
466 g = a_exp_b_mod_c(h, (p-1)/q, p);
468 OutputData(output,
"P ", p);
469 OutputData(output,
"Q ", q);
470 OutputData(output,
"G ", g);
471 OutputData(output,
"Seed ", seed);
472 OutputData(output,
"c ", counter);
473 OutputData(output,
"H ", h, p.
ByteCount());
474 AttachedTransformation()->Put((
byte *)output.data(), output.size());
478 else if (m_test ==
"SigGen")
480 std::string &encodedKey = m_data[
"PrivKey"];
481 int modLen = atol(m_bracketString.substr(6).c_str());
484 if (!encodedKey.empty())
488 if (priv.GetGroupParameters().GetModulus().BitCount() != modLen)
492 if (encodedKey.empty())
494 priv.Initialize(m_rng, modLen);
497 OutputData(output,
"P ", priv.GetGroupParameters().GetModulus());
498 OutputData(output,
"Q ", priv.GetGroupParameters().GetSubgroupOrder());
499 OutputData(output,
"G ", priv.GetGroupParameters().GetSubgroupGenerator());
504 OutputData(output,
"Msg ", m_data[
"Msg"]);
509 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
510 OutputData(output,
"R ", R);
511 OutputData(output,
"S ", S);
512 AttachedTransformation()->Put((
byte *)output.data(), output.size());
515 else if (m_test ==
"SigVer")
517 Integer p((m_data[
"P"] +
"h").c_str());
518 Integer q((m_data[
"Q"] +
"h").c_str());
519 Integer g((m_data[
"G"] +
"h").c_str());
520 Integer y((m_data[
"Y"] +
"h").c_str());
530 OutputData(output,
"Result ", b ?
"P" :
"F");
531 AttachedTransformation()->Put((
byte *)output.data(), output.size());
534 else if (m_test ==
"PQGVer")
536 Integer p((m_data[
"P"] +
"h").c_str());
537 Integer q((m_data[
"Q"] +
"h").c_str());
538 Integer g((m_data[
"G"] +
"h").c_str());
539 Integer h((m_data[
"H"] +
"h").c_str());
540 int c = atol(m_data[
"c"].c_str());
545 bool result = DSA::GeneratePrimes(seed, seed.size()*8, c, p1, 1024, q1,
true);
546 result = result && (p1 == p && q1 == q);
547 result = result && g == a_exp_b_mod_c(h, (p-1)/q, p);
549 OutputData(output,
"Result ", result ?
"P" :
"F");
550 AttachedTransformation()->Put((
byte *)output.data(), output.size());
557 if (m_algorithm ==
"ECDSA")
559 std::map<std::string, OID> name2oid;
560 name2oid[
"P-192"] = ASN1::secp192r1();
561 name2oid[
"P-224"] = ASN1::secp224r1();
562 name2oid[
"P-256"] = ASN1::secp256r1();
563 name2oid[
"P-384"] = ASN1::secp384r1();
564 name2oid[
"P-521"] = ASN1::secp521r1();
565 name2oid[
"K-163"] = ASN1::sect163k1();
566 name2oid[
"K-233"] = ASN1::sect233k1();
567 name2oid[
"K-283"] = ASN1::sect283k1();
568 name2oid[
"K-409"] = ASN1::sect409k1();
569 name2oid[
"K-571"] = ASN1::sect571k1();
570 name2oid[
"B-163"] = ASN1::sect163r2();
571 name2oid[
"B-233"] = ASN1::sect233r1();
572 name2oid[
"B-283"] = ASN1::sect283r1();
573 name2oid[
"B-409"] = ASN1::sect409r1();
574 name2oid[
"B-571"] = ASN1::sect571r1();
579 if (m_bracketString[0] ==
'P')
580 pass = EC_PKV<ECP>(m_rng, DecodeHex(m_data[
"Qx"]), DecodeHex(m_data[
"Qy"]), name2oid[m_bracketString]);
582 pass = EC_PKV<EC2N>(m_rng, DecodeHex(m_data[
"Qx"]), DecodeHex(m_data[
"Qy"]), name2oid[m_bracketString]);
584 OutputData(output,
"Result ", pass ?
"P" :
"F");
586 else if (m_test ==
"KeyPair")
588 if (m_bracketString[0] ==
'P')
589 EC_KeyPair<ECP>(output, atol(m_data[
"N"].c_str()), name2oid[m_bracketString]);
591 EC_KeyPair<EC2N>(output, atol(m_data[
"N"].c_str()), name2oid[m_bracketString]);
593 else if (m_test ==
"SigGen")
595 if (m_bracketString[0] ==
'P')
596 EC_SigGen<ECP>(output, name2oid[m_bracketString]);
598 EC_SigGen<EC2N>(output, name2oid[m_bracketString]);
600 else if (m_test ==
"SigVer")
602 if (m_bracketString[0] ==
'P')
603 EC_SigVer<ECP>(output, name2oid[m_bracketString]);
605 EC_SigVer<EC2N>(output, name2oid[m_bracketString]);
608 AttachedTransformation()->Put((
byte *)output.data(), output.size());
613 if (m_algorithm ==
"RSA")
615 std::string shaAlg = m_data[
"SHAAlg"].substr(3);
619 Integer n((m_data[
"n"] +
"h").c_str());
620 Integer e((m_data[
"e"] +
"h").c_str());
625 pV->AccessMaterial().AssignFrom(pub);
628 for (
unsigned int i=m_data[
"S"].size(); i<pV->SignatureLength()*2; i++)
635 OutputData(output,
"Result ", b ?
"P" :
"F");
640 int modLen = atol(m_bracketString.substr(6).c_str());
641 std::string &encodedKey = m_data[
"PrivKey"];
644 if (!encodedKey.empty())
648 if (priv.GetModulus().
BitCount() != modLen)
652 if (encodedKey.empty())
657 OutputData(output,
"n ", priv.GetModulus());
658 OutputData(output,
"e ", priv.GetPublicExponent(), modLen/8);
662 pS->AccessMaterial().AssignFrom(priv);
666 OutputData(output,
"SHAAlg ", m_data[
"SHAAlg"]);
667 OutputData(output,
"Msg ", m_data[
"Msg"]);
668 OutputData(output,
"S ", sig);
671 AttachedTransformation()->Put((
byte *)output.data(), output.size());
676 if (m_algorithm ==
"SHA")
682 else if (m_mode ==
"224")
684 else if (m_mode ==
"256")
686 else if (m_mode ==
"384")
688 else if (m_mode ==
"512")
691 if (m_test ==
"MONTE")
697 for (j=0; j<100; j++)
699 MD[0] = MD[1] = MD[2] = seed;
700 for (i=3; i<1003; i++)
703 MD[i].
resize(pHF->DigestSize());
704 pHF->CalculateDigest(MD[i], Mi, Mi.
size());
707 OutputData(output,
"COUNT ", j);
708 OutputData(output,
"MD ", seed);
709 AttachedTransformation()->Put((
byte *)output.data(), output.size());
717 int len = atol(m_data[
"Len"].c_str());
719 OutputData(output,
"MD ", tag);
720 AttachedTransformation()->Put((
byte *)output.data(), output.size());
728 if (m_algorithm ==
"TDES")
730 if (!m_data[
"KEY1"].empty())
732 const std::string keys[3] = {m_data[
"KEY1"], m_data[
"KEY2"], m_data[
"KEY3"]};
735 for (
int i=0; i<3; i++)
736 hexDec.Put((
byte *)keys[i].data(), keys[i].size());
738 if (keys[0] == keys[2])
740 if (keys[0] == keys[1])
750 if (m_algorithm ==
"RNG")
760 for (
int i=0; i<10000; i++)
768 OutputData(output,
"R ", r);
769 AttachedTransformation()->Put((
byte *)output.data(), output.size());
774 if (m_algorithm ==
"HMAC")
778 if (m_bracketString ==
"L=20")
780 else if (m_bracketString ==
"L=28")
782 else if (m_bracketString ==
"L=32")
784 else if (m_bracketString ==
"L=48")
786 else if (m_bracketString ==
"L=64")
791 pMAC->SetKey(key, key.
size());
792 int Tlen = atol(m_data[
"Tlen"].c_str());
795 OutputData(output,
"Mac ", tag);
796 AttachedTransformation()->Put((
byte *)output.data(), output.size());
802 if (m_algorithm ==
"DES")
803 pBT.reset(NewBT((
DES*)0));
804 else if (m_algorithm ==
"TDES")
807 pBT.reset(NewBT((
DES*)0));
808 else if (key.
size() == 16)
813 else if (m_algorithm ==
"SKIPJACK")
815 else if (m_algorithm ==
"AES")
816 pBT.reset(NewBT((
AES*)0));
829 unsigned int K = m_feedbackSize;
833 else if (m_mode ==
"CBC")
835 else if (m_mode ==
"CFB")
837 else if (m_mode ==
"OFB")
842 bool encrypt = m_encrypt;
844 if (m_test ==
"MONTE")
848 int keySize = key.
size();
851 std::vector<SecByteBlock> IB(10001), OB(10001), PT(10001), CT(10001), RESULT(10001), TXT(10001), CV(10001);
852 PT[0] = GetData(
"PLAINTEXT");
853 CT[0] = GetData(
"CIPHERTEXT");
855 TXT[0] = GetData(
"TEXT");
857 int outerCount = (m_algorithm ==
"AES") ? 100 : 400;
858 int innerCount = (m_algorithm ==
"AES") ? 1000 : 10000;
860 for (
int i=0; i<outerCount; i++)
862 pBT->
SetKey(KEY[i], keySize);
864 for (
int j=0; j<innerCount; j++)
871 CT[j].resize(blockSize);
878 PT[j].resize(blockSize);
883 else if (m_mode ==
"OFB")
885 OB[j].resize(blockSize);
887 Xor(RESULT[j], OB[j], TXT[j]);
891 else if (m_mode ==
"CBC")
895 Xor(IB[j], PT[j], CV[j]);
896 CT[j].resize(blockSize);
904 OB[j].resize(blockSize);
906 Xor(PT[j], OB[j], CV[j]);
911 else if (m_mode ==
"CFB")
915 OB[j].resize(blockSize);
917 AssignLeftMostBits(CT[j], OB[j], K);
918 Xor(CT[j], CT[j], PT[j]);
919 AssignLeftMostBits(PT[j+1], IB[j], K);
920 IB[j+1].resize(blockSize);
921 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
922 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
926 OB[j].resize(blockSize);
928 AssignLeftMostBits(PT[j], OB[j], K);
929 Xor(PT[j], PT[j], CT[j]);
930 IB[j+1].resize(blockSize);
931 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
932 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
933 AssignLeftMostBits(CT[j+1], OB[j], K);
941 OutputData(output, KEY_T, KEY[i]);
943 OutputData(output,
IV, CV[0]);
944 if (m_mode ==
"OFB" || m_mode ==
"CFB")
945 OutputData(output,
IV, IB[0]);
946 if (m_mode ==
"ECB" || m_mode ==
"CBC" || m_mode ==
"CFB")
950 OutputData(output, INPUT, PT[0]);
951 OutputData(output, OUTPUT, CT[innerCount-1]);
952 KEY[i+1] = UpdateKey(KEY[i], &CT[0]);
956 OutputData(output, INPUT, CT[0]);
957 OutputData(output, OUTPUT, PT[innerCount-1]);
958 KEY[i+1] = UpdateKey(KEY[i], &PT[0]);
960 PT[0] = PT[innerCount];
961 IB[0] = IB[innerCount];
962 CV[0] = CV[innerCount];
963 CT[0] = CT[innerCount];
965 else if (m_mode ==
"OFB")
967 OutputData(output, INPUT, TXT[0]);
968 OutputData(output, OUTPUT, RESULT[innerCount-1]);
969 KEY[i+1] = UpdateKey(KEY[i], &RESULT[0]);
970 Xor(TXT[0], TXT[0], IB[innerCount-1]);
971 IB[0] = OB[innerCount-1];
974 AttachedTransformation()->Put((
byte *)output.data(), output.size());
978 else if (m_test ==
"MCT")
982 int keySize = key.
size();
987 inputs[0] = m_data2[INPUT];
989 for (
int i=0; i<100; i++)
993 for (
int j=0; j<1000; j++)
995 outputs[j] = inputs[j];
996 pCipher->ProcessString(outputs[j], outputs[j].size());
997 if (K==8 && m_mode ==
"CFB")
1000 inputs[j+1].
Assign(ivs[i]+j, 1);
1002 inputs[j+1] = outputs[j-16];
1004 else if (m_mode ==
"ECB")
1005 inputs[j+1] = outputs[j];
1007 inputs[j+1] = ivs[i];
1009 inputs[j+1] = outputs[j-1];
1012 if (m_algorithm ==
"AES")
1013 OutputData(output, COUNT, m_count++);
1014 OutputData(output, KEY_T, KEY[i]);
1015 if (m_mode !=
"ECB")
1016 OutputData(output,
IV, ivs[i]);
1017 OutputData(output, INPUT, inputs[0]);
1018 OutputData(output, OUTPUT, outputs[999]);
1020 AttachedTransformation()->Put((
byte *)output.data(), output.size());
1023 KEY[i+1] = UpdateKey(KEY[i], outputs);
1024 ivs[i+1].
CleanNew(pCipher->IVSize());
1025 ivs[i+1] = UpdateKey(ivs[i+1], outputs);
1026 if (K==8 && m_mode ==
"CFB")
1027 inputs[0] = outputs[999-16];
1028 else if (m_mode ==
"ECB")
1029 inputs[0] = outputs[999];
1031 inputs[0] = outputs[998];
1043 OutputGivenData(output, COUNT,
true);
1044 OutputData(output, KEY_T, key);
1045 OutputGivenData(output,
IV,
true);
1046 OutputGivenData(output, INPUT);
1047 OutputData(output, OUTPUT, result);
1049 AttachedTransformation()->Put((
byte *)output.data(), output.size());
1053 std::vector<std::string> Tokenize(
const std::string &line)
1055 std::vector<std::string> result;
1057 for (
unsigned int i=0; i<line.size(); i++)
1059 if (isalnum(line[i]) || line[i] ==
'^')
1061 else if (!s.empty())
1063 result.push_back(s);
1067 result.push_back(
"=");
1070 result.push_back(s);
1074 bool IsolatedMessageEnd(
bool blocking)
1077 throw BlockingInputOnly(
"TestDataParser");
1080 m_inQueue.TransferTo(
StringSink(m_line).Ref());
1082 if (m_line[0] ==
'#')
1085 bool copyLine =
false;
1087 if (m_line[0] ==
'[')
1089 m_bracketString = m_line.substr(1, m_line.size()-2);
1090 if (m_bracketString ==
"ENCRYPT")
1092 if (m_bracketString ==
"DECRYPT")
1097 if (m_line.substr(0, 2) ==
"H>")
1100 m_bracketString = m_line.substr(2, m_line.size()-4);
1101 m_line = m_line.substr(0, 13) +
"Hashes<H";
1117 AttachedTransformation()->Put((
byte *)m_line.data(), m_line.size(), blocking);
1121 std::vector<std::string> tokens = Tokenize(m_line);
1123 if (m_algorithm ==
"DSA" && m_test ==
"sha")
1125 for (
unsigned int i = 0; i < tokens.size(); i++)
1127 if (tokens[i] ==
"^")
1129 else if (tokens[i] !=
"")
1130 m_compactString.push_back(atol(tokens[i].c_str()));
1135 if (!m_line.empty() && ((m_algorithm ==
"RSA" && m_test !=
"Gen") || m_algorithm ==
"RNG" || m_algorithm ==
"HMAC" || m_algorithm ==
"SHA" || (m_algorithm ==
"ECDSA" && m_test !=
"KeyPair") || (m_algorithm ==
"DSA" && (m_test ==
"PQGVer" || m_test ==
"SigVer"))))
1138 std::string output = m_line +
'\n';
1139 AttachedTransformation()->Put((
byte *)output.data(), output.size());
1142 for (
unsigned int i = 0; i < tokens.size(); i++)
1144 if (m_firstLine && m_algorithm !=
"DSA")
1146 if (tokens[i] ==
"Encrypt" || tokens[i] ==
"OFB")
1148 else if (tokens[i] ==
"Decrypt")
1150 else if (tokens[i] ==
"Modes")
1155 if (tokens[i] !=
"=")
1161 const std::string &key = tokens[i-1];
1162 std::string &data = m_data[key];
1163 data = (tokens.size() > i+1) ? tokens[i+1] :
"";
1164 DataType t = m_nameToType[key];
1165 m_typeToName[t] = key;
1166 m_data2[t] = DecodeHex(data);
1168 if (key == m_trigger || (t == OUTPUT && !m_data2[INPUT].empty() && !isspace(m_line[0])))
1174 m_firstLine =
false;
1179 inline const SecByteBlock & GetData(
const std::string &key)
1181 return m_data2[m_nameToType[key]];
1191 std::string m_algorithm, m_test, m_mode, m_line, m_bracketString, m_trigger;
1192 unsigned int m_feedbackSize, m_blankLineTransition;
1193 bool m_encrypt, m_firstLine;
1195 typedef std::map<std::string, DataType> NameToTypeMap;
1196 NameToTypeMap m_nameToType;
1197 typedef std::map<DataType, std::string> TypeToNameMap;
1198 TypeToNameMap m_typeToName;
1200 typedef std::map<std::string, std::string> Map;
1202 typedef std::map<DataType, SecByteBlock> Map2;
1207 std::vector<unsigned int> m_compactString;
1210int FIPS_140_AlgorithmTest(
int argc,
char **argv)
1215 std::string algorithm = argv[1];
1216 std::string pathname = argv[2];
1217 unsigned int i = pathname.find_last_of(
"\\/");
1218 std::string filename = pathname.substr(i == std::string::npos ? 0 : i+1);
1219 std::string dirname = pathname.substr(0, i);
1221 if (algorithm ==
"auto")
1223 string algTable[] = {
"AES",
"ECDSA",
"DSA",
"HMAC",
"RNG",
"RSA",
"TDES",
"SKIPJACK",
"SHA"};
1224 for (i=0; i<
sizeof(algTable)/
sizeof(algTable[0]); i++)
1226 if (dirname.find(algTable[i]) != std::string::npos)
1228 algorithm = algTable[i];
1237 if (algorithm ==
"SHA")
1238 mode =
IntToString(atol(filename.substr(3, 3).c_str()));
1239 else if (algorithm ==
"RSA")
1240 mode = filename.substr(6, 1);
1241 else if (filename[0] ==
'S' || filename[0] ==
'T')
1242 mode = filename.substr(1, 3);
1244 mode = filename.substr(0, 3);
1245 for (i = 0; i<mode.size(); i++)
1246 mode[i] = toupper(mode[i]);
1247 unsigned int feedbackSize = mode ==
"CFB" ? atoi(filename.substr(filename.find_first_of(
"0123456789")).c_str()) : 0;
1249 if (algorithm ==
"DSA" || algorithm ==
"ECDSA")
1250 test = filename.substr(0, filename.size() - 4);
1251 else if (algorithm ==
"RSA")
1252 test = filename.substr(3, 3);
1253 else if (filename.find(
"Monte") != std::string::npos)
1255 else if (filename.find(
"MCT") != std::string::npos)
1259 bool encrypt = (filename.find(
"vrct") == std::string::npos);
1265 std::string outDir = argv[3];
1267 if (outDir ==
"auto")
1269 if (dirname.substr(dirname.size()-3) ==
"req")
1270 outDir = dirname.substr(0, dirname.size()-3) +
"resp";
1273 if (*outDir.rbegin() !=
'\\' && *outDir.rbegin() !=
'/')
1275 std::string outPathname = outDir + filename.substr(0, filename.size() - 3) +
"rsp";
1276 pSink =
new FileSink(outPathname.c_str(),
false);
1281 FileSource(pathname.c_str(),
true,
new LineBreakParser(
new TestDataParser(algorithm, test, mode, feedbackSize, encrypt, pSink)),
false);
1285 cout <<
"file: " << filename << endl;
1291extern int (*AdhocTest)(
int argc,
char *argv[]);
1292static int s_i = (AdhocTest = &FIPS_140_AlgorithmTest, 0);
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
AES block cipher (Rijndael)
Copy input to a memory buffer.
Automatically Seeded X9.17 RNG.
Provides auto signaling support.
int GetAutoSignalPropagation() const
Retrieve automatic signal propagation value.
Provides class member functions to key a block cipher.
Interface for one direction (encryption or decryption) of a block cipher.
2-key TripleDES block cipher
3-key TripleDES block cipher
static void CorrectKeyParityBits(byte *key)
correct DES key parity bits
Elliptic Curve Parameters.
const Integer & GetSubgroupOrder() const
Retrieves the subgroup order.
const Integer & GetModulus() const
Retrieve the modulus for the group.
virtual const Element & GetSubgroupGenerator() const
Retrieves the subgroup generator.
Elliptic Curve Discrete Log (DL) private key.
void Initialize(const DL_GroupParameters_EC< EC > ¶ms, const Integer &x)
Initialize an EC Private Key using {GP,x}.
void MakePublicKey(DL_PublicKey< T > &pub) const
Initializes a public key from this key.
const Integer & GetPrivateExponent() const
Retrieves the private exponent.
Elliptic Curve Discrete Log (DL) public key.
void Initialize(const DL_GroupParameters_EC< EC > ¶ms, const Element &Q)
Initialize an EC Public Key using {GP,Q}.
virtual const Element & GetPublicElement() const
Retrieves the public element.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Implementation of Store interface.
Implementation of Store interface.
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Filter wrapper for HashTransformation.
Decode base 16 data back to bytes.
Converts given data to base 16.
Multiple precision integer with arithmetic operations.
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
size_t MinEncodedSize(Signedness sign=UNSIGNED) const
Minimum number of bytes to encode this integer.
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
RSA trapdoor function using the private key.
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e=17)
Create a RSA private key.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Template implementing constructors for public key algorithm classes.
Interface for public-key signers.
Interface for public-key signature verifiers.
Polynomial with Coefficients in GF(2)
unsigned int MinEncodedSize() const
minimum number of bytes to encode this polynomial
void Encode(byte *output, size_t outputLen) const
encode in big-endian format
RSA trapdoor function using the public key.
void Initialize(const Integer &n, const Integer &e)
Initialize a RSA public key.
Interface for random number generators.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Redirect input to another BufferedTransformation without owning it.
@ DATA_ONLY
Pass data only.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void CleanNew(size_type newSize)
Change size without preserving contents.
A::pointer data()
Provides a pointer to the first element in the memory block.
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
bool empty() const
Determines if the SecBlock is empty.
size_type size() const
Provides the count of elements in the SecBlock.
void resize(size_type newSize)
Change size and preserve contents.
Filter wrapper for PK_Verifier.
Filter wrapper for PK_Signer.
virtual bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
virtual size_t DefaultKeyLength() const =0
Returns default key length.
Append input to a string object.
String-based implementation of the Source interface.
String-based implementation of Store interface.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Base class for unflushable filters.
Pointer that overloads operator ->
Abstract base classes that provide a uniform interface to this library.
Functions and definitions required for building the FIPS-140 DLL on Windows.
Implementation of BufferedTransformation's attachment interface.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
CRYPTOPP_DLL void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Crypto++ library namespace.
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
const char * FeedbackSize()
int
ASN.1 object identifiers for algorithms and schemes.
Classes for automatic resource management.
@ NO_PADDING
No padding added to a block.
CBC mode, external cipher.
CFB mode, external cipher.
ECB mode, external cipher.
OFB mode, external cipher.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.