00001
00002
00003
00004
00005
00006 #if 0
00007 #ifndef CRYPTOPP_IMPORTS
00008 #define CRYPTOPP_DEFAULT_NO_DLL
00009 #endif
00010
00011 #include "dll.h"
00012 #include "cryptlib.h"
00013 #include "smartptr.h"
00014 #include "filters.h"
00015 #include "oids.h"
00016
00017 USING_NAMESPACE(CryptoPP)
00018 USING_NAMESPACE(std)
00019
00020 class LineBreakParser : public AutoSignaling<Bufferless<Filter> >
00021 {
00022 public:
00023 LineBreakParser(BufferedTransformation *attachment=NULL, byte lineEnd='\n')
00024 : m_lineEnd(lineEnd) {Detach(attachment);}
00025
00026 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00027 {
00028 if (!blocking)
00029 throw BlockingInputOnly("LineBreakParser");
00030
00031 unsigned int i, last = 0;
00032 for (i=0; i<length; i++)
00033 {
00034 if (begin[i] == m_lineEnd)
00035 {
00036 AttachedTransformation()->Put2(begin+last, i-last, GetAutoSignalPropagation(), blocking);
00037 last = i+1;
00038 }
00039 }
00040 if (last != i)
00041 AttachedTransformation()->Put2(begin+last, i-last, 0, blocking);
00042
00043 if (messageEnd && GetAutoSignalPropagation())
00044 {
00045 AttachedTransformation()->MessageEnd(GetAutoSignalPropagation()-1, blocking);
00046 AttachedTransformation()->MessageSeriesEnd(GetAutoSignalPropagation()-1, blocking);
00047 }
00048
00049 return 0;
00050 }
00051
00052 private:
00053 byte m_lineEnd;
00054 };
00055
00056 class TestDataParser : public Unflushable<FilterWithInputQueue>
00057 {
00058 public:
00059 enum DataType {OTHER, COUNT, KEY_T, IV, INPUT, OUTPUT};
00060
00061 TestDataParser(std::string algorithm, std::string test, std::string mode, unsigned int feedbackSize, bool encrypt, BufferedTransformation *attachment)
00062 : m_algorithm(algorithm), m_test(test), m_mode(mode), m_feedbackSize(feedbackSize)
00063 , m_firstLine(true), m_blankLineTransition(0)
00064 {
00065 Detach(attachment);
00066
00067 m_typeToName[COUNT] = "COUNT";
00068
00069 m_nameToType["COUNT"] = COUNT;
00070 m_nameToType["KEY"] = KEY_T;
00071 m_nameToType["KEYs"] = KEY_T;
00072 m_nameToType["key"] = KEY_T;
00073 m_nameToType["Key"] = KEY_T;
00074 m_nameToType["IV"] = IV;
00075 m_nameToType["IV1"] = IV;
00076 m_nameToType["CV"] = IV;
00077 m_nameToType["CV1"] = IV;
00078 m_nameToType["IB"] = IV;
00079 m_nameToType["TEXT"] = INPUT;
00080 m_nameToType["RESULT"] = OUTPUT;
00081 m_nameToType["Msg"] = INPUT;
00082 m_nameToType["Seed"] = INPUT;
00083 m_nameToType["V"] = INPUT;
00084 m_nameToType["DT"] = IV;
00085 SetEncrypt(encrypt);
00086
00087 if (m_algorithm == "DSA" || m_algorithm == "ECDSA")
00088 {
00089 if (m_test == "PKV")
00090 m_trigger = "Qy";
00091 else if (m_test == "KeyPair")
00092 m_trigger = "N";
00093 else if (m_test == "SigGen")
00094 m_trigger = "Msg";
00095 else if (m_test == "SigVer")
00096 m_trigger = "S";
00097 else if (m_test == "PQGGen")
00098 m_trigger = "N";
00099 else if (m_test == "PQGVer")
00100 m_trigger = "H";
00101 }
00102 else if (m_algorithm == "HMAC")
00103 m_trigger = "Msg";
00104 else if (m_algorithm == "SHA")
00105 m_trigger = (m_test == "MONTE") ? "Seed" : "Msg";
00106 else if (m_algorithm == "RNG")
00107 m_trigger = "V";
00108 else if (m_algorithm == "RSA")
00109 m_trigger = (m_test == "Ver") ? "S" : "Msg";
00110 }
00111
00112 void SetEncrypt(bool encrypt)
00113 {
00114 m_encrypt = encrypt;
00115 if (encrypt)
00116 {
00117 m_nameToType["PLAINTEXT"] = INPUT;
00118 m_nameToType["CIPHERTEXT"] = OUTPUT;
00119 m_nameToType["PT"] = INPUT;
00120 m_nameToType["CT"] = OUTPUT;
00121 }
00122 else
00123 {
00124 m_nameToType["PLAINTEXT"] = OUTPUT;
00125 m_nameToType["CIPHERTEXT"] = INPUT;
00126 m_nameToType["PT"] = OUTPUT;
00127 m_nameToType["CT"] = INPUT;
00128 }
00129
00130 if (m_algorithm == "AES" || m_algorithm == "TDES")
00131 {
00132 if (encrypt)
00133 {
00134 m_trigger = "PLAINTEXT";
00135 m_typeToName[OUTPUT] = "CIPHERTEXT";
00136 }
00137 else
00138 {
00139 m_trigger = "CIPHERTEXT";
00140 m_typeToName[OUTPUT] = "PLAINTEXT";
00141 }
00142 m_count = 0;
00143 }
00144 }
00145
00146 protected:
00147 void OutputData(std::string &output, const std::string &key, const std::string &data)
00148 {
00149 output += key;
00150 output += "= ";
00151 output += data;
00152 output += "\n";
00153 }
00154
00155 void OutputData(std::string &output, const std::string &key, int data)
00156 {
00157 OutputData(output, key, IntToString(data));
00158 }
00159
00160 void OutputData(std::string &output, const std::string &key, const SecByteBlock &data)
00161 {
00162 output += key;
00163 output += "= ";
00164 HexEncoder(new StringSink(output), false).Put(data, data.size());
00165 output += "\n";
00166 }
00167
00168 void OutputData(std::string &output, const std::string &key, const Integer &data, int size=-1)
00169 {
00170 SecByteBlock s(size < 0 ? data.MinEncodedSize() : size);
00171 data.Encode(s, s.size());
00172 OutputData(output, key, s);
00173 }
00174
00175 void OutputData(std::string &output, const std::string &key, const PolynomialMod2 &data, int size=-1)
00176 {
00177 SecByteBlock s(size < 0 ? data.MinEncodedSize() : size);
00178 data.Encode(s, s.size());
00179 OutputData(output, key, s);
00180 }
00181
00182 void OutputData(std::string &output, DataType t, const std::string &data)
00183 {
00184 if (m_algorithm == "SKIPJACK")
00185 {
00186 if (m_test == "KAT")
00187 {
00188 if (t == OUTPUT)
00189 output = m_line + data + "\n";
00190 }
00191 else
00192 {
00193 if (t != COUNT)
00194 {
00195 output += m_typeToName[t];
00196 output += "=";
00197 }
00198 output += data;
00199 output += t == OUTPUT ? "\n" : " ";
00200 }
00201 }
00202 else if (m_algorithm == "TDES" && t == KEY_T && m_typeToName[KEY_T].empty())
00203 {
00204 output += "KEY1 = ";
00205 output += data.substr(0, 16);
00206 output += "\nKEY2 = ";
00207 output += data.size() > 16 ? data.substr(16, 16) : data.substr(0, 16);
00208 output += "\nKEY3 = ";
00209 output += data.size() > 32 ? data.substr(32, 16) : data.substr(0, 16);
00210 output += "\n";
00211 }
00212 else
00213 {
00214 output += m_typeToName[t];
00215 output += " = ";
00216 output += data;
00217 output += "\n";
00218 }
00219 }
00220
00221 void OutputData(std::string &output, DataType t, int i)
00222 {
00223 OutputData(output, t, IntToString(i));
00224 }
00225
00226 void OutputData(std::string &output, DataType t, const SecByteBlock &data)
00227 {
00228 std::string hexData;
00229 StringSource(data.begin(), data.size(), true, new HexEncoder(new StringSink(hexData), false));
00230 OutputData(output, t, hexData);
00231 }
00232
00233 void OutputGivenData(std::string &output, DataType t, bool optional = false)
00234 {
00235 if (m_data.find(m_typeToName[t]) == m_data.end())
00236 {
00237 if (optional)
00238 return;
00239 throw Exception(Exception::OTHER_ERROR, "TestDataParser: key not found: " + m_typeToName[t]);
00240 }
00241
00242 OutputData(output, t, m_data[m_typeToName[t]]);
00243 }
00244
00245 template <class T>
00246 BlockCipher * NewBT(T *)
00247 {
00248 if (!m_encrypt && (m_mode == "ECB" || m_mode == "CBC"))
00249 return new typename T::Decryption;
00250 else
00251 return new typename T::Encryption;
00252 }
00253
00254 template <class T>
00255 SymmetricCipher * NewMode(T *, BlockCipher &bt, const byte *iv)
00256 {
00257 if (!m_encrypt)
00258 return new typename T::Decryption(bt, iv, m_feedbackSize/8);
00259 else
00260 return new typename T::Encryption(bt, iv, m_feedbackSize/8);
00261 }
00262
00263 static inline void Xor(SecByteBlock &z, const SecByteBlock &x, const SecByteBlock &y)
00264 {
00265 assert(x.size() == y.size());
00266 z.resize(x.size());
00267 xorbuf(z, x, y, x.size());
00268 }
00269
00270 SecByteBlock UpdateKey(SecByteBlock key, const SecByteBlock *text)
00271 {
00272 unsigned int innerCount = (m_algorithm == "AES") ? 1000 : 10000;
00273 int keySize = key.size(), blockSize = text[0].size();
00274 SecByteBlock x(keySize);
00275 for (int k=0; k<keySize;)
00276 {
00277 int pos = innerCount * blockSize - keySize + k;
00278 memcpy(x + k, text[pos / blockSize] + pos % blockSize, blockSize - pos % blockSize);
00279 k += blockSize - pos % blockSize;
00280 }
00281
00282 if (m_algorithm == "TDES" || m_algorithm == "DES")
00283 {
00284 for (int i=0; i<keySize; i+=8)
00285 {
00286 xorbuf(key+i, x+keySize-8-i, 8);
00287 DES::CorrectKeyParityBits(key+i);
00288 }
00289 }
00290 else
00291 xorbuf(key, x, keySize);
00292
00293 return key;
00294 }
00295
00296 static inline void AssignLeftMostBits(SecByteBlock &z, const SecByteBlock &x, unsigned int K)
00297 {
00298 z.Assign(x, K/8);
00299 }
00300
00301 template <class EC>
00302 void EC_KeyPair(string &output, int n, const OID &oid)
00303 {
00304 DL_GroupParameters_EC<EC> params(oid);
00305 for (int i=0; i<n; i++)
00306 {
00307 DL_PrivateKey_EC<EC> priv;
00308 DL_PublicKey_EC<EC> pub;
00309 priv.Initialize(m_rng, params);
00310 priv.MakePublicKey(pub);
00311
00312 OutputData(output, "d ", priv.GetPrivateExponent());
00313 OutputData(output, "Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
00314 OutputData(output, "Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
00315 }
00316 }
00317
00318 template <class EC>
00319 void EC_SigGen(string &output, const OID &oid)
00320 {
00321 DL_GroupParameters_EC<EC> params(oid);
00322 typename ECDSA<EC, SHA1>::PrivateKey priv;
00323 typename ECDSA<EC, SHA1>::PublicKey pub;
00324 priv.Initialize(m_rng, params);
00325 priv.MakePublicKey(pub);
00326
00327 typename ECDSA<EC, SHA1>::Signer signer(priv);
00328 SecByteBlock sig(signer.SignatureLength());
00329 StringSource(m_data["Msg"], true, new HexDecoder(new SignerFilter(m_rng, signer, new ArraySink(sig, sig.size()))));
00330 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
00331
00332 OutputData(output, "Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
00333 OutputData(output, "Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
00334 OutputData(output, "R ", R);
00335 OutputData(output, "S ", S);
00336 }
00337
00338 template <class EC>
00339 void EC_SigVer(string &output, const OID &oid)
00340 {
00341 SecByteBlock x(DecodeHex(m_data["Qx"]));
00342 SecByteBlock y(DecodeHex(m_data["Qy"]));
00343 Integer r((m_data["R"]+"h").c_str());
00344 Integer s((m_data["S"]+"h").c_str());
00345
00346 typename EC::FieldElement Qx(x, x.size());
00347 typename EC::FieldElement Qy(y, y.size());
00348 typename EC::Element Q(Qx, Qy);
00349
00350 DL_GroupParameters_EC<EC> params(oid);
00351 typename ECDSA<EC, SHA1>::PublicKey pub;
00352 pub.Initialize(params, Q);
00353 typename ECDSA<EC, SHA1>::Verifier verifier(pub);
00354
00355 SecByteBlock sig(verifier.SignatureLength());
00356 r.Encode(sig, sig.size()/2);
00357 s.Encode(sig+sig.size()/2, sig.size()/2);
00358
00359 SignatureVerificationFilter filter(verifier);
00360 filter.Put(sig, sig.size());
00361 StringSource(m_data["Msg"], true, new HexDecoder(new Redirector(filter, Redirector::DATA_ONLY)));
00362 filter.MessageEnd();
00363 byte b;
00364 filter.Get(b);
00365 OutputData(output, "Result ", b ? "P" : "F");
00366 }
00367
00368 template <class EC>
00369 static bool EC_PKV(RandomNumberGenerator &rng, const SecByteBlock &x, const SecByteBlock &y, const OID &oid)
00370 {
00371 typename EC::FieldElement Qx(x, x.size());
00372 typename EC::FieldElement Qy(y, y.size());
00373 typename EC::Element Q(Qx, Qy);
00374
00375 DL_GroupParameters_EC<EC> params(oid);
00376 typename ECDSA<EC, SHA1>::PublicKey pub;
00377 pub.Initialize(params, Q);
00378 return pub.Validate(rng, 3);
00379 }
00380
00381 template <class H, class Result>
00382 Result * CreateRSA2(const std::string &standard)
00383 {
00384 if (typeid(Result) == typeid(PK_Verifier))
00385 {
00386 if (standard == "R")
00387 return (Result *) new typename RSASS_ISO<H>::Verifier;
00388 else if (standard == "P")
00389 return (Result *) new typename RSASS<PSS, H>::Verifier;
00390 else if (standard == "1")
00391 return (Result *) new typename RSASS<PKCS1v15, H>::Verifier;
00392 }
00393 else if (typeid(Result) == typeid(PK_Signer))
00394 {
00395 if (standard == "R")
00396 return (Result *) new typename RSASS_ISO<H>::Signer;
00397 else if (standard == "P")
00398 return (Result *) new typename RSASS<PSS, H>::Signer;
00399 else if (standard == "1")
00400 return (Result *) new typename RSASS<PKCS1v15, H>::Signer;
00401 }
00402
00403 return NULL;
00404 }
00405
00406 template <class Result>
00407 Result * CreateRSA(const std::string &standard, const std::string &hash)
00408 {
00409 if (hash == "1")
00410 return CreateRSA2<SHA1, Result>(standard);
00411 else if (hash == "224")
00412 return CreateRSA2<SHA224, Result>(standard);
00413 else if (hash == "256")
00414 return CreateRSA2<SHA256, Result>(standard);
00415 else if (hash == "384")
00416 return CreateRSA2<SHA384, Result>(standard);
00417 else if (hash == "512")
00418 return CreateRSA2<SHA512, Result>(standard);
00419 else
00420 return NULL;
00421 }
00422
00423 virtual void DoTest()
00424 {
00425 std::string output;
00426
00427 if (m_algorithm == "DSA")
00428 {
00429 if (m_test == "KeyPair")
00430 {
00431 DL_GroupParameters_DSA pqg;
00432 int modLen = atol(m_bracketString.substr(6).c_str());
00433 pqg.GenerateRandomWithKeySize(m_rng, modLen);
00434
00435 OutputData(output, "P ", pqg.GetModulus());
00436 OutputData(output, "Q ", pqg.GetSubgroupOrder());
00437 OutputData(output, "G ", pqg.GetSubgroupGenerator());
00438
00439 int n = atol(m_data["N"].c_str());
00440 for (int i=0; i<n; i++)
00441 {
00442 DSA::Signer priv;
00443 priv.AccessKey().GenerateRandom(m_rng, pqg);
00444 DSA::Verifier pub(priv);
00445
00446 OutputData(output, "X ", priv.GetKey().GetPrivateExponent());
00447 OutputData(output, "Y ", pub.GetKey().GetPublicElement());
00448 AttachedTransformation()->Put((byte *)output.data(), output.size());
00449 output.resize(0);
00450 }
00451 }
00452 else if (m_test == "PQGGen")
00453 {
00454 int n = atol(m_data["N"].c_str());
00455 for (int i=0; i<n; i++)
00456 {
00457 Integer p, q, h, g;
00458 int counter;
00459
00460 SecByteBlock seed(SHA::DIGESTSIZE);
00461 do
00462 {
00463 m_rng.GenerateBlock(seed, seed.size());
00464 }
00465 while (!DSA::GeneratePrimes(seed, seed.size()*8, counter, p, 1024, q));
00466 h.Randomize(m_rng, 2, p-2);
00467 g = a_exp_b_mod_c(h, (p-1)/q, p);
00468
00469 OutputData(output, "P ", p);
00470 OutputData(output, "Q ", q);
00471 OutputData(output, "G ", g);
00472 OutputData(output, "Seed ", seed);
00473 OutputData(output, "c ", counter);
00474 OutputData(output, "H ", h, p.ByteCount());
00475 AttachedTransformation()->Put((byte *)output.data(), output.size());
00476 output.resize(0);
00477 }
00478 }
00479 else if (m_test == "SigGen")
00480 {
00481 std::string &encodedKey = m_data["PrivKey"];
00482 int modLen = atol(m_bracketString.substr(6).c_str());
00483 DSA::PrivateKey priv;
00484
00485 if (!encodedKey.empty())
00486 {
00487 StringStore s(encodedKey);
00488 priv.BERDecode(s);
00489 if (priv.GetGroupParameters().GetModulus().BitCount() != modLen)
00490 encodedKey.clear();
00491 }
00492
00493 if (encodedKey.empty())
00494 {
00495 priv.Initialize(m_rng, modLen);
00496 StringSink s(encodedKey);
00497 priv.DEREncode(s);
00498 OutputData(output, "P ", priv.GetGroupParameters().GetModulus());
00499 OutputData(output, "Q ", priv.GetGroupParameters().GetSubgroupOrder());
00500 OutputData(output, "G ", priv.GetGroupParameters().GetSubgroupGenerator());
00501 }
00502
00503 DSA::Signer signer(priv);
00504 DSA::Verifier pub(signer);
00505 OutputData(output, "Msg ", m_data["Msg"]);
00506 OutputData(output, "Y ", pub.GetKey().GetPublicElement());
00507
00508 SecByteBlock sig(signer.SignatureLength());
00509 StringSource(m_data["Msg"], true, new HexDecoder(new SignerFilter(m_rng, signer, new ArraySink(sig, sig.size()))));
00510 SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
00511 OutputData(output, "R ", R);
00512 OutputData(output, "S ", S);
00513 AttachedTransformation()->Put((byte *)output.data(), output.size());
00514 output.resize(0);
00515 }
00516 else if (m_test == "SigVer")
00517 {
00518 Integer p((m_data["P"] + "h").c_str());
00519 Integer q((m_data["Q"] + "h").c_str());
00520 Integer g((m_data["G"] + "h").c_str());
00521 Integer y((m_data["Y"] + "h").c_str());
00522 DSA::Verifier verifier(p, q, g, y);
00523
00524 HexDecoder filter(new SignatureVerificationFilter(verifier));
00525 StringSource(m_data["R"], true, new Redirector(filter, Redirector::DATA_ONLY));
00526 StringSource(m_data["S"], true, new Redirector(filter, Redirector::DATA_ONLY));
00527 StringSource(m_data["Msg"], true, new Redirector(filter, Redirector::DATA_ONLY));
00528 filter.MessageEnd();
00529 byte b;
00530 filter.Get(b);
00531 OutputData(output, "Result ", b ? "P" : "F");
00532 AttachedTransformation()->Put((byte *)output.data(), output.size());
00533 output.resize(0);
00534 }
00535 else if (m_test == "PQGVer")
00536 {
00537 Integer p((m_data["P"] + "h").c_str());
00538 Integer q((m_data["Q"] + "h").c_str());
00539 Integer g((m_data["G"] + "h").c_str());
00540 Integer h((m_data["H"] + "h").c_str());
00541 int c = atol(m_data["c"].c_str());
00542 SecByteBlock seed(m_data["Seed"].size()/2);
00543 StringSource(m_data["Seed"], true, new HexDecoder(new ArraySink(seed, seed.size())));
00544
00545 Integer p1, q1;
00546 bool result = DSA::GeneratePrimes(seed, seed.size()*8, c, p1, 1024, q1, true);
00547 result = result && (p1 == p && q1 == q);
00548 result = result && g == a_exp_b_mod_c(h, (p-1)/q, p);
00549
00550 OutputData(output, "Result ", result ? "P" : "F");
00551 AttachedTransformation()->Put((byte *)output.data(), output.size());
00552 output.resize(0);
00553 }
00554
00555 return;
00556 }
00557
00558 if (m_algorithm == "ECDSA")
00559 {
00560 std::map<std::string, OID> name2oid;
00561 name2oid["P-192"] = ASN1::secp192r1();
00562 name2oid["P-224"] = ASN1::secp224r1();
00563 name2oid["P-256"] = ASN1::secp256r1();
00564 name2oid["P-384"] = ASN1::secp384r1();
00565 name2oid["P-521"] = ASN1::secp521r1();
00566 name2oid["K-163"] = ASN1::sect163k1();
00567 name2oid["K-233"] = ASN1::sect233k1();
00568 name2oid["K-283"] = ASN1::sect283k1();
00569 name2oid["K-409"] = ASN1::sect409k1();
00570 name2oid["K-571"] = ASN1::sect571k1();
00571 name2oid["B-163"] = ASN1::sect163r2();
00572 name2oid["B-233"] = ASN1::sect233r1();
00573 name2oid["B-283"] = ASN1::sect283r1();
00574 name2oid["B-409"] = ASN1::sect409r1();
00575 name2oid["B-571"] = ASN1::sect571r1();
00576
00577 if (m_test == "PKV")
00578 {
00579 bool pass;
00580 if (m_bracketString[0] == 'P')
00581 pass = EC_PKV<ECP>(m_rng, DecodeHex(m_data["Qx"]), DecodeHex(m_data["Qy"]), name2oid[m_bracketString]);
00582 else
00583 pass = EC_PKV<EC2N>(m_rng, DecodeHex(m_data["Qx"]), DecodeHex(m_data["Qy"]), name2oid[m_bracketString]);
00584
00585 OutputData(output, "Result ", pass ? "P" : "F");
00586 }
00587 else if (m_test == "KeyPair")
00588 {
00589 if (m_bracketString[0] == 'P')
00590 EC_KeyPair<ECP>(output, atol(m_data["N"].c_str()), name2oid[m_bracketString]);
00591 else
00592 EC_KeyPair<EC2N>(output, atol(m_data["N"].c_str()), name2oid[m_bracketString]);
00593 }
00594 else if (m_test == "SigGen")
00595 {
00596 if (m_bracketString[0] == 'P')
00597 EC_SigGen<ECP>(output, name2oid[m_bracketString]);
00598 else
00599 EC_SigGen<EC2N>(output, name2oid[m_bracketString]);
00600 }
00601 else if (m_test == "SigVer")
00602 {
00603 if (m_bracketString[0] == 'P')
00604 EC_SigVer<ECP>(output, name2oid[m_bracketString]);
00605 else
00606 EC_SigVer<EC2N>(output, name2oid[m_bracketString]);
00607 }
00608
00609 AttachedTransformation()->Put((byte *)output.data(), output.size());
00610 output.resize(0);
00611 return;
00612 }
00613
00614 if (m_algorithm == "RSA")
00615 {
00616 std::string shaAlg = m_data["SHAAlg"].substr(3);
00617
00618 if (m_test == "Ver")
00619 {
00620 Integer n((m_data["n"] + "h").c_str());
00621 Integer e((m_data["e"] + "h").c_str());
00622 RSA::PublicKey pub;
00623 pub.Initialize(n, e);
00624
00625 member_ptr<PK_Verifier> pV(CreateRSA<PK_Verifier>(m_mode, shaAlg));
00626 pV->AccessMaterial().AssignFrom(pub);
00627
00628 HexDecoder filter(new SignatureVerificationFilter(*pV));
00629 for (unsigned int i=m_data["S"].size(); i<pV->SignatureLength()*2; i++)
00630 filter.Put('0');
00631 StringSource(m_data["S"], true, new Redirector(filter, Redirector::DATA_ONLY));
00632 StringSource(m_data["Msg"], true, new Redirector(filter, Redirector::DATA_ONLY));
00633 filter.MessageEnd();
00634 byte b;
00635 filter.Get(b);
00636 OutputData(output, "Result ", b ? "P" : "F");
00637 }
00638 else
00639 {
00640 assert(m_test == "Gen");
00641 int modLen = atol(m_bracketString.substr(6).c_str());
00642 std::string &encodedKey = m_data["PrivKey"];
00643 RSA::PrivateKey priv;
00644
00645 if (!encodedKey.empty())
00646 {
00647 StringStore s(encodedKey);
00648 priv.BERDecode(s);
00649 if (priv.GetModulus().BitCount() != modLen)
00650 encodedKey.clear();
00651 }
00652
00653 if (encodedKey.empty())
00654 {
00655 priv.Initialize(m_rng, modLen);
00656 StringSink s(encodedKey);
00657 priv.DEREncode(s);
00658 OutputData(output, "n ", priv.GetModulus());
00659 OutputData(output, "e ", priv.GetPublicExponent(), modLen/8);
00660 }
00661
00662 member_ptr<PK_Signer> pS(CreateRSA<PK_Signer>(m_mode, shaAlg));
00663 pS->AccessMaterial().AssignFrom(priv);
00664
00665 SecByteBlock sig(pS->SignatureLength());
00666 StringSource(m_data["Msg"], true, new HexDecoder(new SignerFilter(m_rng, *pS, new ArraySink(sig, sig.size()))));
00667 OutputData(output, "SHAAlg ", m_data["SHAAlg"]);
00668 OutputData(output, "Msg ", m_data["Msg"]);
00669 OutputData(output, "S ", sig);
00670 }
00671
00672 AttachedTransformation()->Put((byte *)output.data(), output.size());
00673 output.resize(0);
00674 return;
00675 }
00676
00677 if (m_algorithm == "SHA")
00678 {
00679 member_ptr<HashFunction> pHF;
00680
00681 if (m_mode == "1")
00682 pHF.reset(new SHA1);
00683 else if (m_mode == "224")
00684 pHF.reset(new SHA224);
00685 else if (m_mode == "256")
00686 pHF.reset(new SHA256);
00687 else if (m_mode == "384")
00688 pHF.reset(new SHA384);
00689 else if (m_mode == "512")
00690 pHF.reset(new SHA512);
00691
00692 if (m_test == "MONTE")
00693 {
00694 SecByteBlock seed = m_data2[INPUT];
00695 SecByteBlock MD[1003];
00696 int i,j;
00697
00698 for (j=0; j<100; j++)
00699 {
00700 MD[0] = MD[1] = MD[2] = seed;
00701 for (i=3; i<1003; i++)
00702 {
00703 SecByteBlock Mi = MD[i-3] + MD[i-2] + MD[i-1];
00704 MD[i].resize(pHF->DigestSize());
00705 pHF->CalculateDigest(MD[i], Mi, Mi.size());
00706 }
00707 seed = MD[1002];
00708 OutputData(output, "COUNT ", j);
00709 OutputData(output, "MD ", seed);
00710 AttachedTransformation()->Put((byte *)output.data(), output.size());
00711 output.resize(0);
00712 }
00713 }
00714 else
00715 {
00716 SecByteBlock tag(pHF->DigestSize());
00717 SecByteBlock &msg(m_data2[INPUT]);
00718 int len = atol(m_data["Len"].c_str());
00719 StringSource(msg.begin(), len/8, true, new HashFilter(*pHF, new ArraySink(tag, tag.size())));
00720 OutputData(output, "MD ", tag);
00721 AttachedTransformation()->Put((byte *)output.data(), output.size());
00722 output.resize(0);
00723 }
00724 return;
00725 }
00726
00727 SecByteBlock &key = m_data2[KEY_T];
00728
00729 if (m_algorithm == "TDES")
00730 {
00731 if (!m_data["KEY1"].empty())
00732 {
00733 const std::string keys[3] = {m_data["KEY1"], m_data["KEY2"], m_data["KEY3"]};
00734 key.resize(24);
00735 HexDecoder hexDec(new ArraySink(key, key.size()));
00736 for (int i=0; i<3; i++)
00737 hexDec.Put((byte *)keys[i].data(), keys[i].size());
00738
00739 if (keys[0] == keys[2])
00740 {
00741 if (keys[0] == keys[1])
00742 key.resize(8);
00743 else
00744 key.resize(16);
00745 }
00746 else
00747 key.resize(24);
00748 }
00749 }
00750
00751 if (m_algorithm == "RNG")
00752 {
00753 key.resize(24);
00754 StringSource(m_data["Key1"] + m_data["Key2"] + m_data["Key3"], true, new HexDecoder(new ArraySink(key, key.size())));
00755
00756 SecByteBlock seed(m_data2[INPUT]), dt(m_data2[IV]), r(8);
00757 X917RNG rng(new DES_EDE3::Encryption(key, key.size()), seed, dt);
00758
00759 if (m_test == "MCT")
00760 {
00761 for (int i=0; i<10000; i++)
00762 rng.GenerateBlock(r, r.size());
00763 }
00764 else
00765 {
00766 rng.GenerateBlock(r, r.size());
00767 }
00768
00769 OutputData(output, "R ", r);
00770 AttachedTransformation()->Put((byte *)output.data(), output.size());
00771 output.resize(0);
00772 return;
00773 }
00774
00775 if (m_algorithm == "HMAC")
00776 {
00777 member_ptr<MessageAuthenticationCode> pMAC;
00778
00779 if (m_bracketString == "L=20")
00780 pMAC.reset(new HMAC<SHA1>);
00781 else if (m_bracketString == "L=28")
00782 pMAC.reset(new HMAC<SHA224>);
00783 else if (m_bracketString == "L=32")
00784 pMAC.reset(new HMAC<SHA256>);
00785 else if (m_bracketString == "L=48")
00786 pMAC.reset(new HMAC<SHA384>);
00787 else if (m_bracketString == "L=64")
00788 pMAC.reset(new HMAC<SHA512>);
00789 else
00790 throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected HMAC bracket string: " + m_bracketString);
00791
00792 pMAC->SetKey(key, key.size());
00793 int Tlen = atol(m_data["Tlen"].c_str());
00794 SecByteBlock tag(Tlen);
00795 StringSource(m_data["Msg"], true, new HexDecoder(new HashFilter(*pMAC, new ArraySink(tag, Tlen), false, Tlen)));
00796 OutputData(output, "Mac ", tag);
00797 AttachedTransformation()->Put((byte *)output.data(), output.size());
00798 output.resize(0);
00799 return;
00800 }
00801
00802 member_ptr<BlockCipher> pBT;
00803 if (m_algorithm == "DES")
00804 pBT.reset(NewBT((DES*)0));
00805 else if (m_algorithm == "TDES")
00806 {
00807 if (key.size() == 8)
00808 pBT.reset(NewBT((DES*)0));
00809 else if (key.size() == 16)
00810 pBT.reset(NewBT((DES_EDE2*)0));
00811 else
00812 pBT.reset(NewBT((DES_EDE3*)0));
00813 }
00814 else if (m_algorithm == "SKIPJACK")
00815 pBT.reset(NewBT((SKIPJACK*)0));
00816 else if (m_algorithm == "AES")
00817 pBT.reset(NewBT((AES*)0));
00818 else
00819 throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected algorithm: " + m_algorithm);
00820
00821 if (!pBT->IsValidKeyLength(key.size()))
00822 key.CleanNew(pBT->DefaultKeyLength());
00823 pBT->SetKey(key.data(), key.size());
00824
00825 SecByteBlock &iv = m_data2[IV];
00826 if (iv.empty())
00827 iv.CleanNew(pBT->BlockSize());
00828
00829 member_ptr<SymmetricCipher> pCipher;
00830 unsigned int K = m_feedbackSize;
00831
00832 if (m_mode == "ECB")
00833 pCipher.reset(NewMode((ECB_Mode_ExternalCipher*)0, *pBT, iv));
00834 else if (m_mode == "CBC")
00835 pCipher.reset(NewMode((CBC_Mode_ExternalCipher*)0, *pBT, iv));
00836 else if (m_mode == "CFB")
00837 pCipher.reset(NewMode((CFB_Mode_ExternalCipher*)0, *pBT, iv));
00838 else if (m_mode == "OFB")
00839 pCipher.reset(NewMode((OFB_Mode_ExternalCipher*)0, *pBT, iv));
00840 else
00841 throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected mode: " + m_mode);
00842
00843 bool encrypt = m_encrypt;
00844
00845 if (m_test == "MONTE")
00846 {
00847 SecByteBlock KEY[401];
00848 KEY[0] = key;
00849 int keySize = key.size();
00850 int blockSize = pBT->BlockSize();
00851
00852 std::vector<SecByteBlock> IB(10001), OB(10001), PT(10001), CT(10001), RESULT(10001), TXT(10001), CV(10001);
00853 PT[0] = GetData("PLAINTEXT");
00854 CT[0] = GetData("CIPHERTEXT");
00855 CV[0] = IB[0] = iv;
00856 TXT[0] = GetData("TEXT");
00857
00858 int outerCount = (m_algorithm == "AES") ? 100 : 400;
00859 int innerCount = (m_algorithm == "AES") ? 1000 : 10000;
00860
00861 for (int i=0; i<outerCount; i++)
00862 {
00863 pBT->SetKey(KEY[i], keySize);
00864
00865 for (int j=0; j<innerCount; j++)
00866 {
00867 if (m_mode == "ECB")
00868 {
00869 if (encrypt)
00870 {
00871 IB[j] = PT[j];
00872 CT[j].resize(blockSize);
00873 pBT->ProcessBlock(IB[j], CT[j]);
00874 PT[j+1] = CT[j];
00875 }
00876 else
00877 {
00878 IB[j] = CT[j];
00879 PT[j].resize(blockSize);
00880 pBT->ProcessBlock(IB[j], PT[j]);
00881 CT[j+1] = PT[j];
00882 }
00883 }
00884 else if (m_mode == "OFB")
00885 {
00886 OB[j].resize(blockSize);
00887 pBT->ProcessBlock(IB[j], OB[j]);
00888 Xor(RESULT[j], OB[j], TXT[j]);
00889 TXT[j+1] = IB[j];
00890 IB[j+1] = OB[j];
00891 }
00892 else if (m_mode == "CBC")
00893 {
00894 if (encrypt)
00895 {
00896 Xor(IB[j], PT[j], CV[j]);
00897 CT[j].resize(blockSize);
00898 pBT->ProcessBlock(IB[j], CT[j]);
00899 PT[j+1] = CV[j];
00900 CV[j+1] = CT[j];
00901 }
00902 else
00903 {
00904 IB[j] = CT[j];
00905 OB[j].resize(blockSize);
00906 pBT->ProcessBlock(IB[j], OB[j]);
00907 Xor(PT[j], OB[j], CV[j]);
00908 CV[j+1] = CT[j];
00909 CT[j+1] = PT[j];
00910 }
00911 }
00912 else if (m_mode == "CFB")
00913 {
00914 if (encrypt)
00915 {
00916 OB[j].resize(blockSize);
00917 pBT->ProcessBlock(IB[j], OB[j]);
00918 AssignLeftMostBits(CT[j], OB[j], K);
00919 Xor(CT[j], CT[j], PT[j]);
00920 AssignLeftMostBits(PT[j+1], IB[j], K);
00921 IB[j+1].resize(blockSize);
00922 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
00923 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
00924 }
00925 else
00926 {
00927 OB[j].resize(blockSize);
00928 pBT->ProcessBlock(IB[j], OB[j]);
00929 AssignLeftMostBits(PT[j], OB[j], K);
00930 Xor(PT[j], PT[j], CT[j]);
00931 IB[j+1].resize(blockSize);
00932 memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
00933 memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
00934 AssignLeftMostBits(CT[j+1], OB[j], K);
00935 }
00936 }
00937 else
00938 throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected mode: " + m_mode);
00939 }
00940
00941 OutputData(output, COUNT, IntToString(i));
00942 OutputData(output, KEY_T, KEY[i]);
00943 if (m_mode == "CBC")
00944 OutputData(output, IV, CV[0]);
00945 if (m_mode == "OFB" || m_mode == "CFB")
00946 OutputData(output, IV, IB[0]);
00947 if (m_mode == "ECB" || m_mode == "CBC" || m_mode == "CFB")
00948 {
00949 if (encrypt)
00950 {
00951 OutputData(output, INPUT, PT[0]);
00952 OutputData(output, OUTPUT, CT[innerCount-1]);
00953 KEY[i+1] = UpdateKey(KEY[i], &CT[0]);
00954 }
00955 else
00956 {
00957 OutputData(output, INPUT, CT[0]);
00958 OutputData(output, OUTPUT, PT[innerCount-1]);
00959 KEY[i+1] = UpdateKey(KEY[i], &PT[0]);
00960 }
00961 PT[0] = PT[innerCount];
00962 IB[0] = IB[innerCount];
00963 CV[0] = CV[innerCount];
00964 CT[0] = CT[innerCount];
00965 }
00966 else if (m_mode == "OFB")
00967 {
00968 OutputData(output, INPUT, TXT[0]);
00969 OutputData(output, OUTPUT, RESULT[innerCount-1]);
00970 KEY[i+1] = UpdateKey(KEY[i], &RESULT[0]);
00971 Xor(TXT[0], TXT[0], IB[innerCount-1]);
00972 IB[0] = OB[innerCount-1];
00973 }
00974 output += "\n";
00975 AttachedTransformation()->Put((byte *)output.data(), output.size());
00976 output.resize(0);
00977 }
00978 }
00979 else if (m_test == "MCT")
00980 {
00981 SecByteBlock KEY[101];
00982 KEY[0] = key;
00983 int keySize = key.size();
00984 int blockSize = pBT->BlockSize();
00985
00986 SecByteBlock ivs[101], inputs[1001], outputs[1001];
00987 ivs[0] = iv;
00988 inputs[0] = m_data2[INPUT];
00989
00990 for (int i=0; i<100; i++)
00991 {
00992 pCipher->SetKey(KEY[i], keySize, MakeParameters(Name::IV(), (const byte *)ivs[i])(Name::FeedbackSize(), (int)K/8, false));
00993
00994 for (int j=0; j<1000; j++)
00995 {
00996 outputs[j] = inputs[j];
00997 pCipher->ProcessString(outputs[j], outputs[j].size());
00998 if (K==8 && m_mode == "CFB")
00999 {
01000 if (j<16)
01001 inputs[j+1].Assign(ivs[i]+j, 1);
01002 else
01003 inputs[j+1] = outputs[j-16];
01004 }
01005 else if (m_mode == "ECB")
01006 inputs[j+1] = outputs[j];
01007 else if (j == 0)
01008 inputs[j+1] = ivs[i];
01009 else
01010 inputs[j+1] = outputs[j-1];
01011 }
01012
01013 if (m_algorithm == "AES")
01014 OutputData(output, COUNT, m_count++);
01015 OutputData(output, KEY_T, KEY[i]);
01016 if (m_mode != "ECB")
01017 OutputData(output, IV, ivs[i]);
01018 OutputData(output, INPUT, inputs[0]);
01019 OutputData(output, OUTPUT, outputs[999]);
01020 output += "\n";
01021 AttachedTransformation()->Put((byte *)output.data(), output.size());
01022 output.resize(0);
01023
01024 KEY[i+1] = UpdateKey(KEY[i], outputs);
01025 ivs[i+1].CleanNew(pCipher->IVSize());
01026 ivs[i+1] = UpdateKey(ivs[i+1], outputs);
01027 if (K==8 && m_mode == "CFB")
01028 inputs[0] = outputs[999-16];
01029 else if (m_mode == "ECB")
01030 inputs[0] = outputs[999];
01031 else
01032 inputs[0] = outputs[998];
01033 }
01034 }
01035 else
01036 {
01037 assert(m_test == "KAT");
01038
01039 SecByteBlock &input = m_data2[INPUT];
01040 SecByteBlock result(input.size());
01041 member_ptr<Filter> pFilter(new StreamTransformationFilter(*pCipher, new ArraySink(result, result.size()), StreamTransformationFilter::NO_PADDING));
01042 StringSource(input.data(), input.size(), true, pFilter.release());
01043
01044 OutputGivenData(output, COUNT, true);
01045 OutputData(output, KEY_T, key);
01046 OutputGivenData(output, IV, true);
01047 OutputGivenData(output, INPUT);
01048 OutputData(output, OUTPUT, result);
01049 output += "\n";
01050 AttachedTransformation()->Put((byte *)output.data(), output.size());
01051 }
01052 }
01053
01054 std::vector<std::string> Tokenize(const std::string &line)
01055 {
01056 std::vector<std::string> result;
01057 std::string s;
01058 for (unsigned int i=0; i<line.size(); i++)
01059 {
01060 if (isalnum(line[i]) || line[i] == '^')
01061 s += line[i];
01062 else if (!s.empty())
01063 {
01064 result.push_back(s);
01065 s = "";
01066 }
01067 if (line[i] == '=')
01068 result.push_back("=");
01069 }
01070 if (!s.empty())
01071 result.push_back(s);
01072 return result;
01073 }
01074
01075 bool IsolatedMessageEnd(bool blocking)
01076 {
01077 if (!blocking)
01078 throw BlockingInputOnly("TestDataParser");
01079
01080 m_line.resize(0);
01081 m_inQueue.TransferTo(StringSink(m_line).Ref());
01082
01083 if (m_line[0] == '#')
01084 return false;
01085
01086 bool copyLine = false;
01087
01088 if (m_line[0] == '[')
01089 {
01090 m_bracketString = m_line.substr(1, m_line.size()-2);
01091 if (m_bracketString == "ENCRYPT")
01092 SetEncrypt(true);
01093 if (m_bracketString == "DECRYPT")
01094 SetEncrypt(false);
01095 copyLine = true;
01096 }
01097
01098 if (m_line.substr(0, 2) == "H>")
01099 {
01100 assert(m_test == "sha");
01101 m_bracketString = m_line.substr(2, m_line.size()-4);
01102 m_line = m_line.substr(0, 13) + "Hashes<H";
01103 copyLine = true;
01104 }
01105
01106 if (m_line == "D>")
01107 copyLine = true;
01108
01109 if (m_line == "<D")
01110 {
01111 m_line += "\n";
01112 copyLine = true;
01113 }
01114
01115 if (copyLine)
01116 {
01117 m_line += '\n';
01118 AttachedTransformation()->Put((byte *)m_line.data(), m_line.size(), blocking);
01119 return false;
01120 }
01121
01122 std::vector<std::string> tokens = Tokenize(m_line);
01123
01124 if (m_algorithm == "DSA" && m_test == "sha")
01125 {
01126 for (unsigned int i = 0; i < tokens.size(); i++)
01127 {
01128 if (tokens[i] == "^")
01129 DoTest();
01130 else if (tokens[i] != "")
01131 m_compactString.push_back(atol(tokens[i].c_str()));
01132 }
01133 }
01134 else
01135 {
01136 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"))))
01137 {
01138
01139 std::string output = m_line + '\n';
01140 AttachedTransformation()->Put((byte *)output.data(), output.size());
01141 }
01142
01143 for (unsigned int i = 0; i < tokens.size(); i++)
01144 {
01145 if (m_firstLine && m_algorithm != "DSA")
01146 {
01147 if (tokens[i] == "Encrypt" || tokens[i] == "OFB")
01148 SetEncrypt(true);
01149 else if (tokens[i] == "Decrypt")
01150 SetEncrypt(false);
01151 else if (tokens[i] == "Modes")
01152 m_test = "MONTE";
01153 }
01154 else
01155 {
01156 if (tokens[i] != "=")
01157 continue;
01158
01159 if (i == 0)
01160 throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected data: " + m_line);
01161
01162 const std::string &key = tokens[i-1];
01163 std::string &data = m_data[key];
01164 data = (tokens.size() > i+1) ? tokens[i+1] : "";
01165 DataType t = m_nameToType[key];
01166 m_typeToName[t] = key;
01167 m_data2[t] = DecodeHex(data);
01168
01169 if (key == m_trigger || (t == OUTPUT && !m_data2[INPUT].empty() && !isspace(m_line[0])))
01170 DoTest();
01171 }
01172 }
01173 }
01174
01175 m_firstLine = false;
01176
01177 return false;
01178 }
01179
01180 inline const SecByteBlock & GetData(const std::string &key)
01181 {
01182 return m_data2[m_nameToType[key]];
01183 }
01184
01185 static SecByteBlock DecodeHex(const std::string &data)
01186 {
01187 SecByteBlock data2(data.size() / 2);
01188 StringSource(data, true, new HexDecoder(new ArraySink(data2, data2.size())));
01189 return data2;
01190 }
01191
01192 std::string m_algorithm, m_test, m_mode, m_line, m_bracketString, m_trigger;
01193 unsigned int m_feedbackSize, m_blankLineTransition;
01194 bool m_encrypt, m_firstLine;
01195
01196 typedef std::map<std::string, DataType> NameToTypeMap;
01197 NameToTypeMap m_nameToType;
01198 typedef std::map<DataType, std::string> TypeToNameMap;
01199 TypeToNameMap m_typeToName;
01200
01201 typedef std::map<std::string, std::string> Map;
01202 Map m_data;
01203 typedef std::map<DataType, SecByteBlock> Map2;
01204 Map2 m_data2;
01205 int m_count;
01206
01207 AutoSeededX917RNG<AES> m_rng;
01208 std::vector<unsigned int> m_compactString;
01209 };
01210
01211 int FIPS_140_AlgorithmTest(int argc, char **argv)
01212 {
01213 argc--;
01214 argv++;
01215
01216 std::string algorithm = argv[1];
01217 std::string pathname = argv[2];
01218 unsigned int i = pathname.find_last_of("\\/");
01219 std::string filename = pathname.substr(i == std::string::npos ? 0 : i+1);
01220 std::string dirname = pathname.substr(0, i);
01221
01222 if (algorithm == "auto")
01223 {
01224 string algTable[] = {"AES", "ECDSA", "DSA", "HMAC", "RNG", "RSA", "TDES", "SKIPJACK", "SHA"};
01225 for (i=0; i<sizeof(algTable)/sizeof(algTable[0]); i++)
01226 {
01227 if (dirname.find(algTable[i]) != std::string::npos)
01228 {
01229 algorithm = algTable[i];
01230 break;
01231 }
01232 }
01233 }
01234
01235 try
01236 {
01237 std::string mode;
01238 if (algorithm == "SHA")
01239 mode = IntToString(atol(filename.substr(3, 3).c_str()));
01240 else if (algorithm == "RSA")
01241 mode = filename.substr(6, 1);
01242 else if (filename[0] == 'S' || filename[0] == 'T')
01243 mode = filename.substr(1, 3);
01244 else
01245 mode = filename.substr(0, 3);
01246 for (i = 0; i<mode.size(); i++)
01247 mode[i] = toupper(mode[i]);
01248 unsigned int feedbackSize = mode == "CFB" ? atoi(filename.substr(filename.find_first_of("0123456789")).c_str()) : 0;
01249 std::string test;
01250 if (algorithm == "DSA" || algorithm == "ECDSA")
01251 test = filename.substr(0, filename.size() - 4);
01252 else if (algorithm == "RSA")
01253 test = filename.substr(3, 3);
01254 else if (filename.find("Monte") != std::string::npos)
01255 test = "MONTE";
01256 else if (filename.find("MCT") != std::string::npos)
01257 test = "MCT";
01258 else
01259 test = "KAT";
01260 bool encrypt = (filename.find("vrct") == std::string::npos);
01261
01262 BufferedTransformation *pSink = NULL;
01263
01264 if (argc > 3)
01265 {
01266 std::string outDir = argv[3];
01267
01268 if (outDir == "auto")
01269 {
01270 if (dirname.substr(dirname.size()-3) == "req")
01271 outDir = dirname.substr(0, dirname.size()-3) + "resp";
01272 }
01273
01274 if (*outDir.rbegin() != '\\' && *outDir.rbegin() != '/')
01275 outDir += '/';
01276 std::string outPathname = outDir + filename.substr(0, filename.size() - 3) + "rsp";
01277 pSink = new FileSink(outPathname.c_str(), false);
01278 }
01279 else
01280 pSink = new FileSink(cout);
01281
01282 FileSource(pathname.c_str(), true, new LineBreakParser(new TestDataParser(algorithm, test, mode, feedbackSize, encrypt, pSink)), false);
01283 }
01284 catch (...)
01285 {
01286 cout << "file: " << filename << endl;
01287 throw;
01288 }
01289 return 0;
01290 }
01291
01292 extern int (*AdhocTest)(int argc, char *argv[]);
01293 static int s_i = (AdhocTest = &FIPS_140_AlgorithmTest, 0);
01294 #endif