7 #ifndef CRYPTOPP_IMPORTS
8 #define CRYPTOPP_DEFAULT_NO_DLL
20 class LineBreakParser :
public AutoSignaling<Bufferless<Filter> >
24 : m_lineEnd(lineEnd) {Detach(attachment);}
26 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
29 throw BlockingInputOnly(
"LineBreakParser");
31 unsigned int i, last = 0;
32 for (i=0; i<length; i++)
34 if (begin[i] == m_lineEnd)
36 AttachedTransformation()->Put2(begin+last, i-last, GetAutoSignalPropagation(), blocking);
41 AttachedTransformation()->Put2(begin+last, i-last, 0, blocking);
43 if (messageEnd && GetAutoSignalPropagation())
45 AttachedTransformation()->MessageEnd(GetAutoSignalPropagation()-1, blocking);
46 AttachedTransformation()->MessageSeriesEnd(GetAutoSignalPropagation()-1, blocking);
56 class TestDataParser :
public Unflushable<FilterWithInputQueue>
59 enum DataType {OTHER, COUNT, KEY_T, IV, INPUT, OUTPUT};
61 TestDataParser(std::string algorithm, std::string test, std::string mode,
unsigned int feedbackSize,
bool encrypt,
BufferedTransformation *attachment)
62 : m_algorithm(algorithm), m_test(test), m_mode(mode), m_feedbackSize(feedbackSize)
63 , m_firstLine(true), m_blankLineTransition(0)
67 m_typeToName[COUNT] =
"COUNT";
69 m_nameToType[
"COUNT"] = COUNT;
70 m_nameToType[
"KEY"] = KEY_T;
71 m_nameToType[
"KEYs"] = KEY_T;
72 m_nameToType[
"key"] = KEY_T;
73 m_nameToType[
"Key"] = KEY_T;
74 m_nameToType[
"IV"] = IV;
75 m_nameToType[
"IV1"] = IV;
76 m_nameToType[
"CV"] = IV;
77 m_nameToType[
"CV1"] = IV;
78 m_nameToType[
"IB"] = IV;
79 m_nameToType[
"TEXT"] = INPUT;
80 m_nameToType[
"RESULT"] = OUTPUT;
81 m_nameToType[
"Msg"] = INPUT;
82 m_nameToType[
"Seed"] = INPUT;
83 m_nameToType[
"V"] = INPUT;
84 m_nameToType[
"DT"] = IV;
87 if (m_algorithm ==
"DSA" || m_algorithm ==
"ECDSA")
91 else if (m_test ==
"KeyPair")
93 else if (m_test ==
"SigGen")
95 else if (m_test ==
"SigVer")
97 else if (m_test ==
"PQGGen")
99 else if (m_test ==
"PQGVer")
102 else if (m_algorithm ==
"HMAC")
104 else if (m_algorithm ==
"SHA")
105 m_trigger = (m_test ==
"MONTE") ?
"Seed" :
"Msg";
106 else if (m_algorithm ==
"RNG")
108 else if (m_algorithm ==
"RSA")
109 m_trigger = (m_test ==
"Ver") ?
"S" :
"Msg";
112 void SetEncrypt(
bool encrypt)
117 m_nameToType[
"PLAINTEXT"] = INPUT;
118 m_nameToType[
"CIPHERTEXT"] = OUTPUT;
119 m_nameToType[
"PT"] = INPUT;
120 m_nameToType[
"CT"] = OUTPUT;
124 m_nameToType[
"PLAINTEXT"] = OUTPUT;
125 m_nameToType[
"CIPHERTEXT"] = INPUT;
126 m_nameToType[
"PT"] = OUTPUT;
127 m_nameToType[
"CT"] = INPUT;
130 if (m_algorithm ==
"AES" || m_algorithm ==
"TDES")
134 m_trigger =
"PLAINTEXT";
135 m_typeToName[OUTPUT] =
"CIPHERTEXT";
139 m_trigger =
"CIPHERTEXT";
140 m_typeToName[OUTPUT] =
"PLAINTEXT";
147 void OutputData(std::string &output,
const std::string &key,
const std::string &data)
155 void OutputData(std::string &output,
const std::string &key,
int data)
160 void OutputData(std::string &output,
const std::string &key,
const SecByteBlock &data)
168 void OutputData(std::string &output,
const std::string &key,
const Integer &data,
int size=-1)
172 OutputData(output, key, s);
175 void OutputData(std::string &output,
const std::string &key,
const PolynomialMod2 &data,
int size=-1)
179 OutputData(output, key, s);
182 void OutputData(std::string &output, DataType t,
const std::string &data)
184 if (m_algorithm ==
"SKIPJACK")
189 output = m_line + data +
"\n";
195 output += m_typeToName[t];
199 output += t == OUTPUT ?
"\n" :
" ";
202 else if (m_algorithm ==
"TDES" && t == KEY_T && m_typeToName[KEY_T].empty())
205 output += data.substr(0, 16);
206 output +=
"\nKEY2 = ";
207 output += data.size() > 16 ? data.substr(16, 16) : data.substr(0, 16);
208 output +=
"\nKEY3 = ";
209 output += data.size() > 32 ? data.substr(32, 16) : data.substr(0, 16);
214 output += m_typeToName[t];
221 void OutputData(std::string &output, DataType t,
int i)
226 void OutputData(std::string &output, DataType t,
const SecByteBlock &data)
230 OutputData(output, t, hexData);
233 void OutputGivenData(std::string &output, DataType t,
bool optional =
false)
235 if (m_data.find(m_typeToName[t]) == m_data.end())
242 OutputData(output, t, m_data[m_typeToName[t]]);
248 if (!m_encrypt && (m_mode ==
"ECB" || m_mode ==
"CBC"))
249 return new typename T::Decryption;
251 return new typename T::Encryption;
258 return new typename T::Decryption(bt, iv, m_feedbackSize/8);
260 return new typename T::Encryption(bt, iv, m_feedbackSize/8);
267 xorbuf(z, x, y, x.size());
272 unsigned int innerCount = (m_algorithm ==
"AES") ? 1000 : 10000;
273 int keySize = key.
size(), blockSize = text[0].
size();
275 for (
int k=0; k<keySize;)
277 int pos = innerCount * blockSize - keySize + k;
278 memcpy(x + k, text[pos / blockSize] + pos % blockSize, blockSize - pos % blockSize);
279 k += blockSize - pos % blockSize;
282 if (m_algorithm ==
"TDES" || m_algorithm ==
"DES")
284 for (
int i=0; i<keySize; i+=8)
286 xorbuf(key+i, x+keySize-8-i, 8);
302 void EC_KeyPair(
string &output,
int n,
const OID &oid)
305 for (
int i=0; i<n; i++)
309 priv.Initialize(m_rng, params);
310 priv.MakePublicKey(pub);
312 OutputData(output,
"d ", priv.GetPrivateExponent());
313 OutputData(output,
"Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
314 OutputData(output,
"Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
319 void EC_SigGen(
string &output,
const OID &oid)
324 priv.Initialize(m_rng, params);
325 priv.MakePublicKey(pub);
330 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
332 OutputData(output,
"Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
333 OutputData(output,
"Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
334 OutputData(output,
"R ", R);
335 OutputData(output,
"S ", S);
339 void EC_SigVer(
string &output,
const OID &oid)
343 Integer r((m_data[
"R"]+
"h").c_str());
344 Integer s((m_data[
"S"]+
"h").c_str());
346 typename EC::FieldElement Qx(x, x.size());
347 typename EC::FieldElement Qy(y, y.
size());
348 typename EC::Element Q(Qx, Qy);
352 pub.Initialize(params, Q);
356 r.Encode(sig, sig.size()/2);
357 s.Encode(sig+sig.size()/2, sig.size()/2);
360 filter.Put(sig, sig.size());
365 OutputData(output,
"Result ", b ?
"P" :
"F");
371 typename EC::FieldElement Qx(x, x.size());
372 typename EC::FieldElement Qy(y, y.
size());
373 typename EC::Element Q(Qx, Qy);
377 pub.Initialize(params, Q);
378 return pub.Validate(rng, 3);
381 template <
class H,
class Result>
382 Result * CreateRSA2(
const std::string &standard)
388 else if (standard ==
"P")
390 else if (standard ==
"1")
393 else if (
typeid(Result) ==
typeid(
PK_Signer))
397 else if (standard ==
"P")
399 else if (standard ==
"1")
406 template <
class Result>
407 Result * CreateRSA(
const std::string &standard,
const std::string &hash)
410 return CreateRSA2<SHA1, Result>(standard);
411 else if (hash ==
"224")
412 return CreateRSA2<SHA224, Result>(standard);
413 else if (hash ==
"256")
414 return CreateRSA2<SHA256, Result>(standard);
415 else if (hash ==
"384")
416 return CreateRSA2<SHA384, Result>(standard);
417 else if (hash ==
"512")
418 return CreateRSA2<SHA512, Result>(standard);
423 virtual void DoTest()
427 if (m_algorithm ==
"DSA")
429 if (m_test ==
"KeyPair")
432 int modLen = atol(m_bracketString.substr(6).c_str());
435 OutputData(output,
"P ", pqg.GetModulus());
436 OutputData(output,
"Q ", pqg.GetSubgroupOrder());
439 int n = atol(m_data[
"N"].c_str());
440 for (
int i=0; i<n; i++)
443 priv.AccessKey().GenerateRandom(m_rng, pqg);
444 DSA::Verifier pub(priv);
446 OutputData(output,
"X ", priv.GetKey().GetPrivateExponent());
447 OutputData(output,
"Y ", pub.GetKey().GetPublicElement());
448 AttachedTransformation()->Put((
byte *)output.data(), output.size());
452 else if (m_test ==
"PQGGen")
454 int n = atol(m_data[
"N"].c_str());
455 for (
int i=0; i<n; i++)
463 m_rng.GenerateBlock(seed, seed.size());
465 while (!DSA::GeneratePrimes(seed, seed.size()*8, counter, p, 1024, q));
466 h.Randomize(m_rng, 2, p-2);
467 g = a_exp_b_mod_c(h, (p-1)/q, p);
469 OutputData(output,
"P ", p);
470 OutputData(output,
"Q ", q);
471 OutputData(output,
"G ", g);
472 OutputData(output,
"Seed ", seed);
473 OutputData(output,
"c ", counter);
474 OutputData(output,
"H ", h, p.
ByteCount());
475 AttachedTransformation()->Put((
byte *)output.data(), output.size());
479 else if (m_test ==
"SigGen")
481 std::string &encodedKey = m_data[
"PrivKey"];
482 int modLen = atol(m_bracketString.substr(6).c_str());
483 DSA::PrivateKey priv;
485 if (!encodedKey.empty())
489 if (priv.GetGroupParameters().GetModulus().BitCount() != modLen)
493 if (encodedKey.empty())
495 priv.Initialize(m_rng, modLen);
498 OutputData(output,
"P ", priv.GetGroupParameters().GetModulus());
499 OutputData(output,
"Q ", priv.GetGroupParameters().GetSubgroupOrder());
500 OutputData(output,
"G ", priv.GetGroupParameters().GetSubgroupGenerator());
503 DSA::Signer signer(priv);
504 DSA::Verifier pub(signer);
505 OutputData(output,
"Msg ", m_data[
"Msg"]);
506 OutputData(output,
"Y ", pub.GetKey().GetPublicElement());
510 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
511 OutputData(output,
"R ", R);
512 OutputData(output,
"S ", S);
513 AttachedTransformation()->Put((
byte *)output.data(), output.size());
516 else if (m_test ==
"SigVer")
518 Integer p((m_data[
"P"] +
"h").c_str());
519 Integer q((m_data[
"Q"] +
"h").c_str());
520 Integer g((m_data[
"G"] +
"h").c_str());
521 Integer y((m_data[
"Y"] +
"h").c_str());
522 DSA::Verifier verifier(p, q, g, y);
531 OutputData(output,
"Result ", b ?
"P" :
"F");
532 AttachedTransformation()->Put((
byte *)output.data(), output.size());
535 else if (m_test ==
"PQGVer")
537 Integer p((m_data[
"P"] +
"h").c_str());
538 Integer q((m_data[
"Q"] +
"h").c_str());
539 Integer g((m_data[
"G"] +
"h").c_str());
540 Integer h((m_data[
"H"] +
"h").c_str());
541 int c = atol(m_data[
"c"].c_str());
546 bool result = DSA::GeneratePrimes(seed, seed.size()*8, c, p1, 1024, q1,
true);
547 result = result && (p1 == p && q1 == q);
548 result = result && g == a_exp_b_mod_c(h, (p-1)/q, p);
550 OutputData(output,
"Result ", result ?
"P" :
"F");
551 AttachedTransformation()->Put((
byte *)output.data(), output.size());
558 if (m_algorithm ==
"ECDSA")
560 std::map<std::string, OID> name2oid;
561 name2oid[
"P-192"] = ASN1::secp192r1();
562 name2oid[
"P-224"] = ASN1::secp224r1();
563 name2oid[
"P-256"] = ASN1::secp256r1();
564 name2oid[
"P-384"] = ASN1::secp384r1();
565 name2oid[
"P-521"] = ASN1::secp521r1();
566 name2oid[
"K-163"] = ASN1::sect163k1();
567 name2oid[
"K-233"] = ASN1::sect233k1();
568 name2oid[
"K-283"] = ASN1::sect283k1();
569 name2oid[
"K-409"] = ASN1::sect409k1();
570 name2oid[
"K-571"] = ASN1::sect571k1();
571 name2oid[
"B-163"] = ASN1::sect163r2();
572 name2oid[
"B-233"] = ASN1::sect233r1();
573 name2oid[
"B-283"] = ASN1::sect283r1();
574 name2oid[
"B-409"] = ASN1::sect409r1();
575 name2oid[
"B-571"] = ASN1::sect571r1();
580 if (m_bracketString[0] ==
'P')
581 pass = EC_PKV<ECP>(m_rng, DecodeHex(m_data[
"Qx"]), DecodeHex(m_data[
"Qy"]), name2oid[m_bracketString]);
583 pass = EC_PKV<EC2N>(m_rng, DecodeHex(m_data[
"Qx"]), DecodeHex(m_data[
"Qy"]), name2oid[m_bracketString]);
585 OutputData(output,
"Result ", pass ?
"P" :
"F");
587 else if (m_test ==
"KeyPair")
589 if (m_bracketString[0] ==
'P')
590 EC_KeyPair<ECP>(output, atol(m_data[
"N"].c_str()), name2oid[m_bracketString]);
592 EC_KeyPair<EC2N>(output, atol(m_data[
"N"].c_str()), name2oid[m_bracketString]);
594 else if (m_test ==
"SigGen")
596 if (m_bracketString[0] ==
'P')
597 EC_SigGen<ECP>(output, name2oid[m_bracketString]);
599 EC_SigGen<EC2N>(output, name2oid[m_bracketString]);
601 else if (m_test ==
"SigVer")
603 if (m_bracketString[0] ==
'P')
604 EC_SigVer<ECP>(output, name2oid[m_bracketString]);
606 EC_SigVer<EC2N>(output, name2oid[m_bracketString]);
609 AttachedTransformation()->Put((
byte *)output.data(), output.size());
614 if (m_algorithm ==
"RSA")
616 std::string shaAlg = m_data[
"SHAAlg"].substr(3);
620 Integer n((m_data[
"n"] +
"h").c_str());
621 Integer e((m_data[
"e"] +
"h").c_str());
626 pV->AccessMaterial().AssignFrom(pub);
629 for (
unsigned int i=m_data[
"S"].size(); i<pV->SignatureLength()*2; i++)
636 OutputData(output,
"Result ", b ?
"P" :
"F");
641 int modLen = atol(m_bracketString.substr(6).c_str());
642 std::string &encodedKey = m_data[
"PrivKey"];
645 if (!encodedKey.empty())
649 if (priv.GetModulus().
BitCount() != modLen)
653 if (encodedKey.empty())
658 OutputData(output,
"n ", priv.GetModulus());
659 OutputData(output,
"e ", priv.GetPublicExponent(), modLen/8);
663 pS->AccessMaterial().AssignFrom(priv);
667 OutputData(output,
"SHAAlg ", m_data[
"SHAAlg"]);
668 OutputData(output,
"Msg ", m_data[
"Msg"]);
669 OutputData(output,
"S ", sig);
672 AttachedTransformation()->Put((
byte *)output.data(), output.size());
677 if (m_algorithm ==
"SHA")
683 else if (m_mode ==
"224")
685 else if (m_mode ==
"256")
687 else if (m_mode ==
"384")
689 else if (m_mode ==
"512")
692 if (m_test ==
"MONTE")
698 for (j=0; j<100; j++)
700 MD[0] = MD[1] = MD[2] = seed;
701 for (i=3; i<1003; i++)
708 OutputData(output,
"COUNT ", j);
709 OutputData(output,
"MD ", seed);
710 AttachedTransformation()->Put((
byte *)output.data(), output.size());
718 int len = atol(m_data[
"Len"].c_str());
720 OutputData(output,
"MD ", tag);
721 AttachedTransformation()->Put((
byte *)output.data(), output.size());
729 if (m_algorithm ==
"TDES")
731 if (!m_data[
"KEY1"].empty())
733 const std::string keys[3] = {m_data[
"KEY1"], m_data[
"KEY2"], m_data[
"KEY3"]};
736 for (
int i=0; i<3; i++)
737 hexDec.Put((
byte *)keys[i].data(), keys[i].size());
739 if (keys[0] == keys[2])
741 if (keys[0] == keys[1])
751 if (m_algorithm ==
"RNG")
756 SecByteBlock seed(m_data2[INPUT]), dt(m_data2[IV]), r(8);
761 for (
int i=0; i<10000; i++)
769 OutputData(output,
"R ", r);
770 AttachedTransformation()->Put((
byte *)output.data(), output.size());
775 if (m_algorithm ==
"HMAC")
779 if (m_bracketString ==
"L=20")
781 else if (m_bracketString ==
"L=28")
783 else if (m_bracketString ==
"L=32")
785 else if (m_bracketString ==
"L=48")
787 else if (m_bracketString ==
"L=64")
793 int Tlen = atol(m_data[
"Tlen"].c_str());
796 OutputData(output,
"Mac ", tag);
797 AttachedTransformation()->Put((
byte *)output.data(), output.size());
803 if (m_algorithm ==
"DES")
804 pBT.reset(NewBT((
DES*)0));
805 else if (m_algorithm ==
"TDES")
808 pBT.reset(NewBT((
DES*)0));
809 else if (key.
size() == 16)
814 else if (m_algorithm ==
"SKIPJACK")
816 else if (m_algorithm ==
"AES")
817 pBT.reset(NewBT((
AES*)0));
830 unsigned int K = m_feedbackSize;
834 else if (m_mode ==
"CBC")
836 else if (m_mode ==
"CFB")
838 else if (m_mode ==
"OFB")
843 bool encrypt = m_encrypt;
845 if (m_test ==
"MONTE")
849 int keySize = key.
size();
852 std::vector<SecByteBlock> IB(10001), OB(10001), PT(10001), CT(10001), RESULT(10001), TXT(10001), CV(10001);
853 PT[0] = GetData(
"PLAINTEXT");
854 CT[0] = GetData(
"CIPHERTEXT");
856 TXT[0] = GetData(
"TEXT");
858 int outerCount = (m_algorithm ==
"AES") ? 100 : 400;
859 int innerCount = (m_algorithm ==
"AES") ? 1000 : 10000;
861 for (
int i=0; i<outerCount; i++)
863 pBT->
SetKey(KEY[i], keySize);
865 for (
int j=0; j<innerCount; j++)
872 CT[j].resize(blockSize);
879 PT[j].resize(blockSize);
884 else if (m_mode ==
"OFB")
886 OB[j].resize(blockSize);
888 Xor(RESULT[j], OB[j], TXT[j]);
892 else if (m_mode ==
"CBC")
896 Xor(IB[j], PT[j], CV[j]);
897 CT[j].resize(blockSize);
905 OB[j].resize(blockSize);
907 Xor(PT[j], OB[j], CV[j]);
912 else if (m_mode ==
"CFB")
916 OB[j].resize(blockSize);
918 AssignLeftMostBits(CT[j], OB[j], K);
919 Xor(CT[j], CT[j], PT[j]);
920 AssignLeftMostBits(PT[j+1], IB[j], K);
921 IB[j+1].resize(blockSize);
922 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
923 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
927 OB[j].resize(blockSize);
929 AssignLeftMostBits(PT[j], OB[j], K);
930 Xor(PT[j], PT[j], CT[j]);
931 IB[j+1].resize(blockSize);
932 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
933 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
934 AssignLeftMostBits(CT[j+1], OB[j], K);
942 OutputData(output, KEY_T, KEY[i]);
944 OutputData(output, IV, CV[0]);
945 if (m_mode ==
"OFB" || m_mode ==
"CFB")
946 OutputData(output, IV, IB[0]);
947 if (m_mode ==
"ECB" || m_mode ==
"CBC" || m_mode ==
"CFB")
951 OutputData(output, INPUT, PT[0]);
952 OutputData(output, OUTPUT, CT[innerCount-1]);
953 KEY[i+1] = UpdateKey(KEY[i], &CT[0]);
957 OutputData(output, INPUT, CT[0]);
958 OutputData(output, OUTPUT, PT[innerCount-1]);
959 KEY[i+1] = UpdateKey(KEY[i], &PT[0]);
961 PT[0] = PT[innerCount];
962 IB[0] = IB[innerCount];
963 CV[0] = CV[innerCount];
964 CT[0] = CT[innerCount];
966 else if (m_mode ==
"OFB")
968 OutputData(output, INPUT, TXT[0]);
969 OutputData(output, OUTPUT, RESULT[innerCount-1]);
970 KEY[i+1] = UpdateKey(KEY[i], &RESULT[0]);
971 Xor(TXT[0], TXT[0], IB[innerCount-1]);
972 IB[0] = OB[innerCount-1];
975 AttachedTransformation()->Put((
byte *)output.data(), output.size());
979 else if (m_test ==
"MCT")
983 int keySize = key.
size();
988 inputs[0] = m_data2[INPUT];
990 for (
int i=0; i<100; i++)
992 pCipher->
SetKey(KEY[i], keySize,
MakeParameters(Name::IV(), (
const byte *)ivs[i])(Name::FeedbackSize(), (
int)K/8,
false));
994 for (
int j=0; j<1000; j++)
996 outputs[j] = inputs[j];
998 if (K==8 && m_mode ==
"CFB")
1001 inputs[j+1].
Assign(ivs[i]+j, 1);
1003 inputs[j+1] = outputs[j-16];
1005 else if (m_mode ==
"ECB")
1006 inputs[j+1] = outputs[j];
1008 inputs[j+1] = ivs[i];
1010 inputs[j+1] = outputs[j-1];
1013 if (m_algorithm ==
"AES")
1014 OutputData(output, COUNT, m_count++);
1015 OutputData(output, KEY_T, KEY[i]);
1016 if (m_mode !=
"ECB")
1017 OutputData(output, IV, ivs[i]);
1018 OutputData(output, INPUT, inputs[0]);
1019 OutputData(output, OUTPUT, outputs[999]);
1021 AttachedTransformation()->Put((
byte *)output.data(), output.size());
1024 KEY[i+1] = UpdateKey(KEY[i], outputs);
1026 ivs[i+1] = UpdateKey(ivs[i+1], outputs);
1027 if (K==8 && m_mode ==
"CFB")
1028 inputs[0] = outputs[999-16];
1029 else if (m_mode ==
"ECB")
1030 inputs[0] = outputs[999];
1032 inputs[0] = outputs[998];
1044 OutputGivenData(output, COUNT,
true);
1045 OutputData(output, KEY_T, key);
1046 OutputGivenData(output, IV,
true);
1047 OutputGivenData(output, INPUT);
1048 OutputData(output, OUTPUT, result);
1050 AttachedTransformation()->Put((
byte *)output.data(), output.size());
1054 std::vector<std::string> Tokenize(
const std::string &line)
1056 std::vector<std::string> result;
1058 for (
unsigned int i=0; i<line.size(); i++)
1060 if (isalnum(line[i]) || line[i] ==
'^')
1062 else if (!s.empty())
1064 result.push_back(s);
1068 result.push_back(
"=");
1071 result.push_back(s);
1075 bool IsolatedMessageEnd(
bool blocking)
1078 throw BlockingInputOnly(
"TestDataParser");
1081 m_inQueue.TransferTo(
StringSink(m_line).Ref());
1083 if (m_line[0] ==
'#')
1086 bool copyLine =
false;
1088 if (m_line[0] ==
'[')
1090 m_bracketString = m_line.substr(1, m_line.size()-2);
1091 if (m_bracketString ==
"ENCRYPT")
1093 if (m_bracketString ==
"DECRYPT")
1098 if (m_line.substr(0, 2) ==
"H>")
1101 m_bracketString = m_line.substr(2, m_line.size()-4);
1102 m_line = m_line.substr(0, 13) +
"Hashes<H";
1118 AttachedTransformation()->Put((
byte *)m_line.data(), m_line.size(), blocking);
1122 std::vector<std::string> tokens = Tokenize(m_line);
1124 if (m_algorithm ==
"DSA" && m_test ==
"sha")
1126 for (
unsigned int i = 0; i < tokens.size(); i++)
1128 if (tokens[i] ==
"^")
1130 else if (tokens[i] !=
"")
1131 m_compactString.push_back(atol(tokens[i].c_str()));
1136 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"))))
1139 std::string output = m_line +
'\n';
1140 AttachedTransformation()->Put((
byte *)output.data(), output.size());
1143 for (
unsigned int i = 0; i < tokens.size(); i++)
1145 if (m_firstLine && m_algorithm !=
"DSA")
1147 if (tokens[i] ==
"Encrypt" || tokens[i] ==
"OFB")
1149 else if (tokens[i] ==
"Decrypt")
1151 else if (tokens[i] ==
"Modes")
1156 if (tokens[i] !=
"=")
1162 const std::string &key = tokens[i-1];
1163 std::string &data = m_data[key];
1164 data = (tokens.size() > i+1) ? tokens[i+1] :
"";
1165 DataType t = m_nameToType[key];
1166 m_typeToName[t] = key;
1167 m_data2[t] = DecodeHex(data);
1169 if (key == m_trigger || (t == OUTPUT && !m_data2[INPUT].empty() && !isspace(m_line[0])))
1175 m_firstLine =
false;
1180 inline const SecByteBlock & GetData(
const std::string &key)
1182 return m_data2[m_nameToType[key]];
1192 std::string m_algorithm, m_test, m_mode, m_line, m_bracketString, m_trigger;
1193 unsigned int m_feedbackSize, m_blankLineTransition;
1194 bool m_encrypt, m_firstLine;
1196 typedef std::map<std::string, DataType> NameToTypeMap;
1197 NameToTypeMap m_nameToType;
1198 typedef std::map<DataType, std::string> TypeToNameMap;
1199 TypeToNameMap m_typeToName;
1201 typedef std::map<std::string, std::string> Map;
1203 typedef std::map<DataType, SecByteBlock> Map2;
1208 std::vector<unsigned int> m_compactString;
1211 int FIPS_140_AlgorithmTest(
int argc,
char **argv)
1216 std::string algorithm = argv[1];
1217 std::string pathname = argv[2];
1218 unsigned int i = pathname.find_last_of(
"\\/");
1219 std::string filename = pathname.substr(i == std::string::npos ? 0 : i+1);
1220 std::string dirname = pathname.substr(0, i);
1222 if (algorithm ==
"auto")
1224 string algTable[] = {
"AES",
"ECDSA",
"DSA",
"HMAC",
"RNG",
"RSA",
"TDES",
"SKIPJACK",
"SHA"};
1225 for (i=0; i<
sizeof(algTable)/
sizeof(algTable[0]); i++)
1227 if (dirname.find(algTable[i]) != std::string::npos)
1229 algorithm = algTable[i];
1238 if (algorithm ==
"SHA")
1239 mode =
IntToString(atol(filename.substr(3, 3).c_str()));
1240 else if (algorithm ==
"RSA")
1241 mode = filename.substr(6, 1);
1242 else if (filename[0] ==
'S' || filename[0] ==
'T')
1243 mode = filename.substr(1, 3);
1245 mode = filename.substr(0, 3);
1246 for (i = 0; i<mode.size(); i++)
1247 mode[i] = toupper(mode[i]);
1248 unsigned int feedbackSize = mode ==
"CFB" ? atoi(filename.substr(filename.find_first_of(
"0123456789")).c_str()) : 0;
1250 if (algorithm ==
"DSA" || algorithm ==
"ECDSA")
1251 test = filename.substr(0, filename.size() - 4);
1252 else if (algorithm ==
"RSA")
1253 test = filename.substr(3, 3);
1254 else if (filename.find(
"Monte") != std::string::npos)
1256 else if (filename.find(
"MCT") != std::string::npos)
1260 bool encrypt = (filename.find(
"vrct") == std::string::npos);
1266 std::string outDir = argv[3];
1268 if (outDir ==
"auto")
1270 if (dirname.substr(dirname.size()-3) ==
"req")
1271 outDir = dirname.substr(0, dirname.size()-3) +
"resp";
1274 if (*outDir.rbegin() !=
'\\' && *outDir.rbegin() !=
'/')
1276 std::string outPathname = outDir + filename.substr(0, filename.size() - 3) +
"rsp";
1277 pSink =
new FileSink(outPathname.c_str(),
false);
1282 FileSource(pathname.c_str(),
true,
new LineBreakParser(
new TestDataParser(algorithm, test, mode, feedbackSize, encrypt, pSink)),
false);
1286 cout <<
"file: " << filename << endl;
1292 extern int (*AdhocTest)(
int argc,
char *argv[]);
1293 static int s_i = (AdhocTest = &FIPS_140_AlgorithmTest, 0);