7 #ifndef CRYPTOPP_IMPORTS
8 #define CRYPTOPP_DEFAULT_NO_DLL
13 USING_NAMESPACE(CryptoPP)
20 : m_lineEnd(lineEnd) {Detach(attachment);}
22 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
25 throw BlockingInputOnly(
"LineBreakParser");
27 unsigned int i, last = 0;
28 for (i=0; i<length; i++)
30 if (begin[i] == m_lineEnd)
32 AttachedTransformation()->Put2(begin+last, i-last, GetAutoSignalPropagation(), blocking);
37 AttachedTransformation()->Put2(begin+last, i-last, 0, blocking);
39 if (messageEnd && GetAutoSignalPropagation())
41 AttachedTransformation()->MessageEnd(GetAutoSignalPropagation()-1, blocking);
42 AttachedTransformation()->MessageSeriesEnd(GetAutoSignalPropagation()-1, blocking);
52 class TestDataParser :
public Unflushable<FilterWithInputQueue>
55 enum DataType {OTHER, COUNT, KEY_T,
IV, INPUT, OUTPUT};
57 TestDataParser(std::string algorithm, std::string test, std::string mode,
unsigned int feedbackSize,
bool encrypt,
BufferedTransformation *attachment)
58 : m_algorithm(algorithm), m_test(test), m_mode(mode), m_feedbackSize(feedbackSize)
59 , m_firstLine(true), m_blankLineTransition(0)
63 m_typeToName[COUNT] =
"COUNT";
65 m_nameToType[
"COUNT"] = COUNT;
66 m_nameToType[
"KEY"] = KEY_T;
67 m_nameToType[
"KEYs"] = KEY_T;
68 m_nameToType[
"key"] = KEY_T;
69 m_nameToType[
"Key"] = KEY_T;
70 m_nameToType[
"IV"] =
IV;
71 m_nameToType[
"IV1"] =
IV;
72 m_nameToType[
"CV"] =
IV;
73 m_nameToType[
"CV1"] =
IV;
74 m_nameToType[
"IB"] =
IV;
75 m_nameToType[
"TEXT"] = INPUT;
76 m_nameToType[
"RESULT"] = OUTPUT;
77 m_nameToType[
"Msg"] = INPUT;
78 m_nameToType[
"Seed"] = INPUT;
79 m_nameToType[
"V"] = INPUT;
80 m_nameToType[
"DT"] =
IV;
83 if (m_algorithm ==
"DSA" || m_algorithm ==
"ECDSA")
87 else if (m_test ==
"KeyPair")
89 else if (m_test ==
"SigGen")
91 else if (m_test ==
"SigVer")
93 else if (m_test ==
"PQGGen")
95 else if (m_test ==
"PQGVer")
98 else if (m_algorithm ==
"HMAC")
100 else if (m_algorithm ==
"SHA")
101 m_trigger = (m_test ==
"MONTE") ?
"Seed" :
"Msg";
102 else if (m_algorithm ==
"RNG")
104 else if (m_algorithm ==
"RSA")
105 m_trigger = (m_test ==
"Ver") ?
"S" :
"Msg";
108 void SetEncrypt(
bool encrypt)
113 m_nameToType[
"PLAINTEXT"] = INPUT;
114 m_nameToType[
"CIPHERTEXT"] = OUTPUT;
115 m_nameToType[
"PT"] = INPUT;
116 m_nameToType[
"CT"] = OUTPUT;
120 m_nameToType[
"PLAINTEXT"] = OUTPUT;
121 m_nameToType[
"CIPHERTEXT"] = INPUT;
122 m_nameToType[
"PT"] = OUTPUT;
123 m_nameToType[
"CT"] = INPUT;
126 if (m_algorithm ==
"AES" || m_algorithm ==
"TDES")
130 m_trigger =
"PLAINTEXT";
131 m_typeToName[OUTPUT] =
"CIPHERTEXT";
135 m_trigger =
"CIPHERTEXT";
136 m_typeToName[OUTPUT] =
"PLAINTEXT";
143 void OutputData(std::string &output,
const std::string &key,
const std::string &data)
151 void OutputData(std::string &output,
const std::string &key,
int data)
153 OutputData(output, key, IntToString(data));
156 void OutputData(std::string &output,
const std::string &key,
const SecByteBlock &data)
164 void OutputData(std::string &output,
const std::string &key,
const Integer &data,
int size=-1)
168 OutputData(output, key, s);
171 void OutputData(std::string &output,
const std::string &key,
const PolynomialMod2 &data,
int size=-1)
175 OutputData(output, key, s);
178 void OutputData(std::string &output, DataType t,
const std::string &data)
180 if (m_algorithm ==
"SKIPJACK")
185 output = m_line + data +
"\n";
191 output += m_typeToName[t];
195 output += t == OUTPUT ?
"\n" :
" ";
198 else if (m_algorithm ==
"TDES" && t == KEY_T && m_typeToName[KEY_T].empty())
201 output += data.substr(0, 16);
202 output +=
"\nKEY2 = ";
203 output += data.size() > 16 ? data.substr(16, 16) : data.substr(0, 16);
204 output +=
"\nKEY3 = ";
205 output += data.size() > 32 ? data.substr(32, 16) : data.substr(0, 16);
210 output += m_typeToName[t];
217 void OutputData(std::string &output, DataType t,
int i)
219 OutputData(output, t, IntToString(i));
222 void OutputData(std::string &output, DataType t,
const SecByteBlock &data)
226 OutputData(output, t, hexData);
229 void OutputGivenData(std::string &output, DataType t,
bool optional =
false)
231 if (m_data.find(m_typeToName[t]) == m_data.end())
238 OutputData(output, t, m_data[m_typeToName[t]]);
244 if (!m_encrypt && (m_mode ==
"ECB" || m_mode ==
"CBC"))
245 return new typename T::Decryption;
247 return new typename T::Encryption;
254 return new typename T::Decryption(bt, iv, m_feedbackSize/8);
256 return new typename T::Encryption(bt, iv, m_feedbackSize/8);
261 assert(x.size() == y.size());
263 xorbuf(z, x, y, x.size());
268 unsigned int innerCount = (m_algorithm ==
"AES") ? 1000 : 10000;
269 int keySize = key.size(), blockSize = text[0].size();
271 for (
int k=0; k<keySize;)
273 int pos = innerCount * blockSize - keySize + k;
274 memcpy(x + k, text[pos / blockSize] + pos % blockSize, blockSize - pos % blockSize);
275 k += blockSize - pos % blockSize;
278 if (m_algorithm ==
"TDES" || m_algorithm ==
"DES")
280 for (
int i=0; i<keySize; i+=8)
282 xorbuf(key+i, x+keySize-8-i, 8);
287 xorbuf(key, x, keySize);
298 void EC_KeyPair(
string &output,
int n,
const OID &oid)
301 for (
int i=0; i<n; i++)
305 priv.Initialize(m_rng, params);
306 priv.MakePublicKey(pub);
308 OutputData(output,
"d ", priv.GetPrivateExponent());
309 OutputData(output,
"Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
310 OutputData(output,
"Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
315 void EC_SigGen(
string &output,
const OID &oid)
320 priv.Initialize(m_rng, params);
321 priv.MakePublicKey(pub);
326 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
328 OutputData(output,
"Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
329 OutputData(output,
"Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
330 OutputData(output,
"R ", R);
331 OutputData(output,
"S ", S);
335 void EC_SigVer(
string &output,
const OID &oid)
339 Integer r((m_data[
"R"]+
"h").c_str());
340 Integer s((m_data[
"S"]+
"h").c_str());
342 typename EC::FieldElement Qx(x, x.size());
343 typename EC::FieldElement Qy(y, y.size());
344 typename EC::Element Q(Qx, Qy);
348 pub.Initialize(params, Q);
352 r.Encode(sig, sig.size()/2);
353 s.Encode(sig+sig.size()/2, sig.size()/2);
356 filter.Put(sig, sig.size());
361 OutputData(output,
"Result ", b ?
"P" :
"F");
367 typename EC::FieldElement Qx(x, x.size());
368 typename EC::FieldElement Qy(y, y.size());
369 typename EC::Element Q(Qx, Qy);
373 pub.Initialize(params, Q);
377 template <
class H,
class Result>
378 Result * CreateRSA2(
const std::string &standard)
384 else if (standard ==
"P")
386 else if (standard ==
"1")
389 else if (
typeid(Result) ==
typeid(
PK_Signer))
393 else if (standard ==
"P")
395 else if (standard ==
"1")
402 template <
class Result>
403 Result * CreateRSA(
const std::string &standard,
const std::string &hash)
406 return CreateRSA2<SHA1, Result>(standard);
407 else if (hash ==
"224")
408 return CreateRSA2<SHA224, Result>(standard);
409 else if (hash ==
"256")
410 return CreateRSA2<SHA256, Result>(standard);
411 else if (hash ==
"384")
412 return CreateRSA2<SHA384, Result>(standard);
413 else if (hash ==
"512")
414 return CreateRSA2<SHA512, Result>(standard);
419 virtual void DoTest()
423 if (m_algorithm ==
"DSA")
425 if (m_test ==
"KeyPair")
428 int modLen = atol(m_bracketString.substr(6).c_str());
431 OutputData(output,
"P ", pqg.GetModulus());
432 OutputData(output,
"Q ", pqg.GetSubgroupOrder());
433 OutputData(output,
"G ", pqg.GetSubgroupGenerator());
435 int n = atol(m_data[
"N"].c_str());
436 for (
int i=0; i<n; i++)
439 priv.AccessKey().GenerateRandom(m_rng, pqg);
442 OutputData(output,
"X ", priv.GetKey().GetPrivateExponent());
443 OutputData(output,
"Y ", pub.GetKey().GetPublicElement());
444 AttachedTransformation()->Put((byte *)output.data(), output.size());
448 else if (m_test ==
"PQGGen")
450 int n = atol(m_data[
"N"].c_str());
451 for (
int i=0; i<n; i++)
459 m_rng.GenerateBlock(seed, seed.size());
461 while (!DSA::GeneratePrimes(seed, seed.size()*8, counter, p, 1024, q));
462 h.Randomize(m_rng, 2, p-2);
463 g = a_exp_b_mod_c(h, (p-1)/q, p);
465 OutputData(output,
"P ", p);
466 OutputData(output,
"Q ", q);
467 OutputData(output,
"G ", g);
468 OutputData(output,
"Seed ", seed);
469 OutputData(output,
"c ", counter);
470 OutputData(output,
"H ", h, p.
ByteCount());
471 AttachedTransformation()->Put((byte *)output.data(), output.size());
475 else if (m_test ==
"SigGen")
477 std::string &encodedKey = m_data[
"PrivKey"];
478 int modLen = atol(m_bracketString.substr(6).c_str());
481 if (!encodedKey.empty())
485 if (priv.GetGroupParameters().GetModulus().BitCount() != modLen)
489 if (encodedKey.empty())
491 priv.Initialize(m_rng, modLen);
494 OutputData(output,
"P ", priv.GetGroupParameters().GetModulus());
495 OutputData(output,
"Q ", priv.GetGroupParameters().GetSubgroupOrder());
496 OutputData(output,
"G ", priv.GetGroupParameters().GetSubgroupGenerator());
501 OutputData(output,
"Msg ", m_data[
"Msg"]);
502 OutputData(output,
"Y ", pub.GetKey().GetPublicElement());
506 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
507 OutputData(output,
"R ", R);
508 OutputData(output,
"S ", S);
509 AttachedTransformation()->Put((byte *)output.data(), output.size());
512 else if (m_test ==
"SigVer")
514 Integer p((m_data[
"P"] +
"h").c_str());
515 Integer q((m_data[
"Q"] +
"h").c_str());
516 Integer g((m_data[
"G"] +
"h").c_str());
517 Integer y((m_data[
"Y"] +
"h").c_str());
527 OutputData(output,
"Result ", b ?
"P" :
"F");
528 AttachedTransformation()->Put((byte *)output.data(), output.size());
531 else if (m_test ==
"PQGVer")
533 Integer p((m_data[
"P"] +
"h").c_str());
534 Integer q((m_data[
"Q"] +
"h").c_str());
535 Integer g((m_data[
"G"] +
"h").c_str());
536 Integer h((m_data[
"H"] +
"h").c_str());
537 int c = atol(m_data[
"c"].c_str());
542 bool result = DSA::GeneratePrimes(seed, seed.size()*8, c, p1, 1024, q1,
true);
543 result = result && (p1 == p && q1 == q);
544 result = result && g == a_exp_b_mod_c(h, (p-1)/q, p);
546 OutputData(output,
"Result ", result ?
"P" :
"F");
547 AttachedTransformation()->Put((byte *)output.data(), output.size());
554 if (m_algorithm ==
"ECDSA")
556 std::map<std::string, OID> name2oid;
557 name2oid[
"P-192"] = ASN1::secp192r1();
558 name2oid[
"P-224"] = ASN1::secp224r1();
559 name2oid[
"P-256"] = ASN1::secp256r1();
560 name2oid[
"P-384"] = ASN1::secp384r1();
561 name2oid[
"P-521"] = ASN1::secp521r1();
562 name2oid[
"K-163"] = ASN1::sect163k1();
563 name2oid[
"K-233"] = ASN1::sect233k1();
564 name2oid[
"K-283"] = ASN1::sect283k1();
565 name2oid[
"K-409"] = ASN1::sect409k1();
566 name2oid[
"K-571"] = ASN1::sect571k1();
567 name2oid[
"B-163"] = ASN1::sect163r2();
568 name2oid[
"B-233"] = ASN1::sect233r1();
569 name2oid[
"B-283"] = ASN1::sect283r1();
570 name2oid[
"B-409"] = ASN1::sect409r1();
571 name2oid[
"B-571"] = ASN1::sect571r1();
576 if (m_bracketString[0] ==
'P')
577 pass = EC_PKV<ECP>(m_rng, DecodeHex(m_data[
"Qx"]), DecodeHex(m_data[
"Qy"]), name2oid[m_bracketString]);
579 pass = EC_PKV<EC2N>(m_rng, DecodeHex(m_data[
"Qx"]), DecodeHex(m_data[
"Qy"]), name2oid[m_bracketString]);
581 OutputData(output,
"Result ", pass ?
"P" :
"F");
583 else if (m_test ==
"KeyPair")
585 if (m_bracketString[0] ==
'P')
586 EC_KeyPair<ECP>(output, atol(m_data[
"N"].c_str()), name2oid[m_bracketString]);
588 EC_KeyPair<EC2N>(output, atol(m_data[
"N"].c_str()), name2oid[m_bracketString]);
590 else if (m_test ==
"SigGen")
592 if (m_bracketString[0] ==
'P')
593 EC_SigGen<ECP>(output, name2oid[m_bracketString]);
595 EC_SigGen<EC2N>(output, name2oid[m_bracketString]);
597 else if (m_test ==
"SigVer")
599 if (m_bracketString[0] ==
'P')
600 EC_SigVer<ECP>(output, name2oid[m_bracketString]);
602 EC_SigVer<EC2N>(output, name2oid[m_bracketString]);
605 AttachedTransformation()->Put((byte *)output.data(), output.size());
610 if (m_algorithm ==
"RSA")
612 std::string shaAlg = m_data[
"SHAAlg"].substr(3);
616 Integer n((m_data[
"n"] +
"h").c_str());
617 Integer e((m_data[
"e"] +
"h").c_str());
619 pub.Initialize(n, e);
622 pV->AccessMaterial().AssignFrom(pub);
625 for (
unsigned int i=m_data[
"S"].size(); i<pV->SignatureLength()*2; i++)
632 OutputData(output,
"Result ", b ?
"P" :
"F");
636 assert(m_test ==
"Gen");
637 int modLen = atol(m_bracketString.substr(6).c_str());
638 std::string &encodedKey = m_data[
"PrivKey"];
641 if (!encodedKey.empty())
645 if (priv.GetModulus().
BitCount() != modLen)
649 if (encodedKey.empty())
651 priv.Initialize(m_rng, modLen);
654 OutputData(output,
"n ", priv.GetModulus());
655 OutputData(output,
"e ", priv.GetPublicExponent(), modLen/8);
659 pS->AccessMaterial().AssignFrom(priv);
663 OutputData(output,
"SHAAlg ", m_data[
"SHAAlg"]);
664 OutputData(output,
"Msg ", m_data[
"Msg"]);
665 OutputData(output,
"S ", sig);
668 AttachedTransformation()->Put((byte *)output.data(), output.size());
673 if (m_algorithm ==
"SHA")
679 else if (m_mode ==
"224")
681 else if (m_mode ==
"256")
683 else if (m_mode ==
"384")
685 else if (m_mode ==
"512")
688 if (m_test ==
"MONTE")
694 for (j=0; j<100; j++)
696 MD[0] = MD[1] = MD[2] = seed;
697 for (i=3; i<1003; i++)
704 OutputData(output,
"COUNT ", j);
705 OutputData(output,
"MD ", seed);
706 AttachedTransformation()->Put((byte *)output.data(), output.size());
714 int len = atol(m_data[
"Len"].c_str());
716 OutputData(output,
"MD ", tag);
717 AttachedTransformation()->Put((byte *)output.data(), output.size());
725 if (m_algorithm ==
"TDES")
727 if (!m_data[
"KEY1"].empty())
729 const std::string keys[3] = {m_data[
"KEY1"], m_data[
"KEY2"], m_data[
"KEY3"]};
732 for (
int i=0; i<3; i++)
733 hexDec.Put((byte *)keys[i].data(), keys[i].size());
735 if (keys[0] == keys[2])
737 if (keys[0] == keys[1])
747 if (m_algorithm ==
"RNG")
752 SecByteBlock seed(m_data2[INPUT]), dt(m_data2[IV]), r(8);
757 for (
int i=0; i<10000; i++)
765 OutputData(output,
"R ", r);
766 AttachedTransformation()->Put((byte *)output.data(), output.size());
771 if (m_algorithm ==
"HMAC")
775 if (m_bracketString ==
"L=20")
777 else if (m_bracketString ==
"L=28")
779 else if (m_bracketString ==
"L=32")
781 else if (m_bracketString ==
"L=48")
783 else if (m_bracketString ==
"L=64")
788 pMAC->
SetKey(key, key.size());
789 int Tlen = atol(m_data[
"Tlen"].c_str());
792 OutputData(output,
"Mac ", tag);
793 AttachedTransformation()->Put((byte *)output.data(), output.size());
799 if (m_algorithm ==
"DES")
800 pBT.reset(NewBT((
DES*)0));
801 else if (m_algorithm ==
"TDES")
804 pBT.reset(NewBT((
DES*)0));
805 else if (key.size() == 16)
810 else if (m_algorithm ==
"SKIPJACK")
812 else if (m_algorithm ==
"AES")
813 pBT.reset(NewBT((
AES*)0));
819 pBT->
SetKey(key.data(), key.size());
826 unsigned int K = m_feedbackSize;
830 else if (m_mode ==
"CBC")
832 else if (m_mode ==
"CFB")
834 else if (m_mode ==
"OFB")
839 bool encrypt = m_encrypt;
841 if (m_test ==
"MONTE")
845 int keySize = key.size();
848 std::vector<SecByteBlock> IB(10001), OB(10001), PT(10001), CT(10001), RESULT(10001), TXT(10001), CV(10001);
849 PT[0] = GetData(
"PLAINTEXT");
850 CT[0] = GetData(
"CIPHERTEXT");
852 TXT[0] = GetData(
"TEXT");
854 int outerCount = (m_algorithm ==
"AES") ? 100 : 400;
855 int innerCount = (m_algorithm ==
"AES") ? 1000 : 10000;
857 for (
int i=0; i<outerCount; i++)
859 pBT->
SetKey(KEY[i], keySize);
861 for (
int j=0; j<innerCount; j++)
868 CT[j].resize(blockSize);
875 PT[j].resize(blockSize);
880 else if (m_mode ==
"OFB")
882 OB[j].resize(blockSize);
884 Xor(RESULT[j], OB[j], TXT[j]);
888 else if (m_mode ==
"CBC")
892 Xor(IB[j], PT[j], CV[j]);
893 CT[j].resize(blockSize);
901 OB[j].resize(blockSize);
903 Xor(PT[j], OB[j], CV[j]);
908 else if (m_mode ==
"CFB")
912 OB[j].resize(blockSize);
914 AssignLeftMostBits(CT[j], OB[j], K);
915 Xor(CT[j], CT[j], PT[j]);
916 AssignLeftMostBits(PT[j+1], IB[j], K);
917 IB[j+1].resize(blockSize);
918 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
919 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
923 OB[j].resize(blockSize);
925 AssignLeftMostBits(PT[j], OB[j], K);
926 Xor(PT[j], PT[j], CT[j]);
927 IB[j+1].resize(blockSize);
928 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
929 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
930 AssignLeftMostBits(CT[j+1], OB[j], K);
937 OutputData(output, COUNT, IntToString(i));
938 OutputData(output, KEY_T, KEY[i]);
940 OutputData(output, IV, CV[0]);
941 if (m_mode ==
"OFB" || m_mode ==
"CFB")
942 OutputData(output, IV, IB[0]);
943 if (m_mode ==
"ECB" || m_mode ==
"CBC" || m_mode ==
"CFB")
947 OutputData(output, INPUT, PT[0]);
948 OutputData(output, OUTPUT, CT[innerCount-1]);
949 KEY[i+1] = UpdateKey(KEY[i], &CT[0]);
953 OutputData(output, INPUT, CT[0]);
954 OutputData(output, OUTPUT, PT[innerCount-1]);
955 KEY[i+1] = UpdateKey(KEY[i], &PT[0]);
957 PT[0] = PT[innerCount];
958 IB[0] = IB[innerCount];
959 CV[0] = CV[innerCount];
960 CT[0] = CT[innerCount];
962 else if (m_mode ==
"OFB")
964 OutputData(output, INPUT, TXT[0]);
965 OutputData(output, OUTPUT, RESULT[innerCount-1]);
966 KEY[i+1] = UpdateKey(KEY[i], &RESULT[0]);
967 Xor(TXT[0], TXT[0], IB[innerCount-1]);
968 IB[0] = OB[innerCount-1];
971 AttachedTransformation()->Put((byte *)output.data(), output.size());
975 else if (m_test ==
"MCT")
979 int keySize = key.size();
984 inputs[0] = m_data2[INPUT];
986 for (
int i=0; i<100; i++)
990 for (
int j=0; j<1000; j++)
992 outputs[j] = inputs[j];
994 if (K==8 && m_mode ==
"CFB")
997 inputs[j+1].
Assign(ivs[i]+j, 1);
999 inputs[j+1] = outputs[j-16];
1001 else if (m_mode ==
"ECB")
1002 inputs[j+1] = outputs[j];
1004 inputs[j+1] = ivs[i];
1006 inputs[j+1] = outputs[j-1];
1009 if (m_algorithm ==
"AES")
1010 OutputData(output, COUNT, m_count++);
1011 OutputData(output, KEY_T, KEY[i]);
1012 if (m_mode !=
"ECB")
1013 OutputData(output, IV, ivs[i]);
1014 OutputData(output, INPUT, inputs[0]);
1015 OutputData(output, OUTPUT, outputs[999]);
1017 AttachedTransformation()->Put((byte *)output.data(), output.size());
1020 KEY[i+1] = UpdateKey(KEY[i], outputs);
1021 ivs[i+1].
CleanNew(pCipher->IVSize());
1022 ivs[i+1] = UpdateKey(ivs[i+1], outputs);
1023 if (K==8 && m_mode ==
"CFB")
1024 inputs[0] = outputs[999-16];
1025 else if (m_mode ==
"ECB")
1026 inputs[0] = outputs[999];
1028 inputs[0] = outputs[998];
1033 assert(m_test ==
"KAT");
1038 StringSource(input.data(), input.size(),
true, pFilter.release());
1040 OutputGivenData(output, COUNT,
true);
1041 OutputData(output, KEY_T, key);
1042 OutputGivenData(output, IV,
true);
1043 OutputGivenData(output, INPUT);
1044 OutputData(output, OUTPUT, result);
1046 AttachedTransformation()->Put((byte *)output.data(), output.size());
1050 std::vector<std::string> Tokenize(
const std::string &line)
1052 std::vector<std::string> result;
1054 for (
unsigned int i=0; i<line.size(); i++)
1056 if (isalnum(line[i]) || line[i] ==
'^')
1058 else if (!s.empty())
1060 result.push_back(s);
1064 result.push_back(
"=");
1067 result.push_back(s);
1071 bool IsolatedMessageEnd(
bool blocking)
1074 throw BlockingInputOnly(
"TestDataParser");
1077 m_inQueue.TransferTo(
StringSink(m_line).Ref());
1079 if (m_line[0] ==
'#')
1082 bool copyLine =
false;
1084 if (m_line[0] ==
'[')
1086 m_bracketString = m_line.substr(1, m_line.size()-2);
1087 if (m_bracketString ==
"ENCRYPT")
1089 if (m_bracketString ==
"DECRYPT")
1094 if (m_line.substr(0, 2) ==
"H>")
1096 assert(m_test ==
"sha");
1097 m_bracketString = m_line.substr(2, m_line.size()-4);
1098 m_line = m_line.substr(0, 13) +
"Hashes<H";
1114 AttachedTransformation()->Put((byte *)m_line.data(), m_line.size(), blocking);
1118 std::vector<std::string> tokens = Tokenize(m_line);
1120 if (m_algorithm ==
"DSA" && m_test ==
"sha")
1122 for (
unsigned int i = 0; i < tokens.size(); i++)
1124 if (tokens[i] ==
"^")
1126 else if (tokens[i] !=
"")
1127 m_compactString.push_back(atol(tokens[i].c_str()));
1132 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"))))
1135 std::string output = m_line +
'\n';
1136 AttachedTransformation()->Put((byte *)output.data(), output.size());
1139 for (
unsigned int i = 0; i < tokens.size(); i++)
1141 if (m_firstLine && m_algorithm !=
"DSA")
1143 if (tokens[i] ==
"Encrypt" || tokens[i] ==
"OFB")
1145 else if (tokens[i] ==
"Decrypt")
1147 else if (tokens[i] ==
"Modes")
1152 if (tokens[i] !=
"=")
1158 const std::string &key = tokens[i-1];
1159 std::string &data = m_data[key];
1160 data = (tokens.size() > i+1) ? tokens[i+1] :
"";
1161 DataType t = m_nameToType[key];
1162 m_typeToName[t] = key;
1163 m_data2[t] = DecodeHex(data);
1165 if (key == m_trigger || (t == OUTPUT && !m_data2[INPUT].empty() && !isspace(m_line[0])))
1171 m_firstLine =
false;
1176 inline const SecByteBlock & GetData(
const std::string &key)
1178 return m_data2[m_nameToType[key]];
1188 std::string m_algorithm, m_test, m_mode, m_line, m_bracketString, m_trigger;
1189 unsigned int m_feedbackSize, m_blankLineTransition;
1190 bool m_encrypt, m_firstLine;
1192 typedef std::map<std::string, DataType> NameToTypeMap;
1193 NameToTypeMap m_nameToType;
1194 typedef std::map<DataType, std::string> TypeToNameMap;
1195 TypeToNameMap m_typeToName;
1197 typedef std::map<std::string, std::string> Map;
1199 typedef std::map<DataType, SecByteBlock> Map2;
1204 std::vector<unsigned int> m_compactString;
1207 int FIPS_140_AlgorithmTest(
int argc,
char **argv)
1212 std::string algorithm = argv[1];
1213 std::string pathname = argv[2];
1214 unsigned int i = pathname.find_last_of(
"\\/");
1215 std::string filename = pathname.substr(i == std::string::npos ? 0 : i+1);
1216 std::string dirname = pathname.substr(0, i);
1218 if (algorithm ==
"auto")
1220 string algTable[] = {
"AES",
"ECDSA",
"DSA",
"HMAC",
"RNG",
"RSA",
"TDES",
"SKIPJACK",
"SHA"};
1221 for (i=0; i<
sizeof(algTable)/
sizeof(algTable[0]); i++)
1223 if (dirname.find(algTable[i]) != std::string::npos)
1225 algorithm = algTable[i];
1234 if (algorithm ==
"SHA")
1235 mode = IntToString(atol(filename.substr(3, 3).c_str()));
1236 else if (algorithm ==
"RSA")
1237 mode = filename.substr(6, 1);
1238 else if (filename[0] ==
'S' || filename[0] ==
'T')
1239 mode = filename.substr(1, 3);
1241 mode = filename.substr(0, 3);
1242 for (i = 0; i<mode.size(); i++)
1243 mode[i] = toupper(mode[i]);
1244 unsigned int feedbackSize = mode ==
"CFB" ? atoi(filename.substr(filename.find_first_of(
"0123456789")).c_str()) : 0;
1246 if (algorithm ==
"DSA" || algorithm ==
"ECDSA")
1247 test = filename.substr(0, filename.size() - 4);
1248 else if (algorithm ==
"RSA")
1249 test = filename.substr(3, 3);
1250 else if (filename.find(
"Monte") != std::string::npos)
1252 else if (filename.find(
"MCT") != std::string::npos)
1256 bool encrypt = (filename.find(
"vrct") == std::string::npos);
1262 std::string outDir = argv[3];
1264 if (outDir ==
"auto")
1266 if (dirname.substr(dirname.size()-3) ==
"req")
1267 outDir = dirname.substr(0, dirname.size()-3) +
"resp";
1270 if (*outDir.rbegin() !=
'\\' && *outDir.rbegin() !=
'/')
1272 std::string outPathname = outDir + filename.substr(0, filename.size() - 3) +
"rsp";
1273 pSink =
new FileSink(outPathname.c_str(),
false);
1278 FileSource(pathname.c_str(),
true,
new LineBreakParser(
new TestDataParser(algorithm, test, mode, feedbackSize, encrypt, pSink)),
false);
1282 cout <<
"file: " << filename << endl;
1288 extern int (*AdhocTest)(
int argc,
char *argv[]);
1289 static int s_i = (AdhocTest = &FIPS_140_AlgorithmTest, 0);
base class for all exceptions thrown by Crypto++
implements the SHA-384 standard
const char * FeedbackSize()
int
void DEREncode(BufferedTransformation &bt) const
encode this object into a BufferedTransformation, using DER (Distinguished Encoding Rules) ...
Filter Wrapper for PK_Verifier.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
set or reset the key of this object
implements the SHA-256 standard
void CleanNew(size_type newSize)
change size and set contents to 0
static void CorrectKeyParityBits(byte *key)
correct DES key parity bits
virtual void GenerateBlock(byte *output, size_t size)
generate random array of bytes
RNG derived from ANSI X9.17 Appendix C.
void resize(size_type newSize)
change size and preserve contents
file-based implementation of Source interface
Converts given data to base 16.
interface for public-key signers
Decode base 16 data back to bytes.
a block of memory allocated using A
some error not belong to any of the above categories
Filter Wrapper for PK_Signer.
interface for random number generators
Append input to a string object.
virtual size_t DefaultKeyLength() const =0
returns default (recommended) key length in bytes */
string-based implementation of Source interface
virtual bool IsValidKeyLength(size_t n) const
returns whether n is a valid key length
Polynomial with Coefficients in GF(2)
interface for one direction (encryption or decryption) of a block cipher
unsigned int BitCount() const
number of significant bits = floor(log2(abs(*this))) + 1
Copy input to a memory buffer.
Filter Wrapper for HashTransformation.
CFB mode, external cipher.
implements the SHA-512 standard
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
check this object for errors
interface for one direction (encryption or decryption) of a stream cipher or cipher mode ...
void Encode(byte *output, size_t outputLen) const
encode in big-endian format
multiple precision integer and basic arithmetics
ECB mode, external cipher.
void Assign(const T *t, size_type len)
set contents and size
unsigned int MinEncodedSize() const
minimum number of bytes to encode this polynomial
RNG from ANSI X9.17 Appendix C, seeded using an OS provided RNG.
CBC mode, external cipher.
AES winner, announced on 10/2/2000
void BERDecode(BufferedTransformation &bt)
decode this object from a BufferedTransformation, using BER (Basic Encoding Rules) ...
string-based implementation of Store interface
Redirect input to another BufferedTransformation without owning it.
DSA group parameters, these are GF(p) group parameters that are allowed by the DSA standard...
OFB mode, external cipher.
Elliptic Curve Parameters.
void Encode(byte *output, size_t outputLen, Signedness=UNSIGNED) const
encode in big-endian format
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
interface for public-key signature verifiers
provides an implementation of BufferedTransformation's attachment interface
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
calls the above function with a NameValuePairs object that just specifies "KeySize" ...
size_t MinEncodedSize(Signedness=UNSIGNED) const
minimum number of bytes to encode this integer
file-based implementation of Sink interface
implements the SHA-224 standard
unsigned int ByteCount() const
number of significant bytes = ceiling(BitCount()/8)
A template implementing constructors for public key algorithm classes.