From 6fc935389225f2200182f666e3fca7daf3622010 Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 18 Sep 2008 13:49:25 +0000 Subject: Add testers for the NIST CAVS PQGGen and SigGen tests (part of FIPS-140 DSA test suite) --- doc/examples/pqg_gen.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++ doc/examples/sig_gen.cpp | 89 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 doc/examples/pqg_gen.cpp create mode 100644 doc/examples/sig_gen.cpp (limited to 'doc') diff --git a/doc/examples/pqg_gen.cpp b/doc/examples/pqg_gen.cpp new file mode 100644 index 000000000..96852a81c --- /dev/null +++ b/doc/examples/pqg_gen.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +using namespace Botan; + +bool check(std::map); + +int main() + { + try { + LibraryInitializer init("use_engines"); + + std::ifstream in("PQGGen.rsp"); + if(!in) + throw Exception("Can't open response file"); + + std::map inputs; + + while(in.good()) + { + std::string line; + std::getline(in, line); + + if(line == "" || line[0] == '[' || line[0] == '#') + continue; + + std::vector name_and_val = split_on(line, '='); + + if(name_and_val.size() != 2) + throw Decoding_Error("Unexpected input: " + line); + + name_and_val[0].erase(name_and_val[0].size()-1); + name_and_val[1].erase(0, 1); + + std::string name = name_and_val[0], value = name_and_val[1]; + + inputs[name] = value; + + if(name == "H") + { + bool result = check(inputs); + std::cout << "." << std::flush; + if(result == false) + { + std::cout << " Check failed\n"; + + std::map::const_iterator i; + + for(i = inputs.begin(); i != inputs.end(); i++) + std::cout << i->first << " = " << i->second << "\n"; + + std::cout << "\n"; + } + + inputs.clear(); + } + } + } + catch(std::exception& e) + { + std::cout << e.what() << std::endl; + return 1; + } + return 0; + } + +bool check(std::map inputs) + { + BigInt p("0x"+inputs["P"]), + q("0x"+inputs["Q"]), + g("0x"+inputs["G"]), + h("0x"+inputs["H"]); + + if(h < 1 || h >= p-1) return false; + + u32bit c = to_u32bit(inputs["c"]); + + Pipe pipe(new Hex_Decoder); + pipe.process_msg(inputs["Seed"]); + SecureVector seed = pipe.read_all(); + + BigInt our_p, our_q; + + bool found = generate_dsa_primes(our_p, our_q, seed, seed.size(), + p.bits(), 0); + + if(!found) /* bad seed */ + return false; + + if(our_p != p) return false; + if(our_q != q) return false; + + BigInt our_g = power_mod(h, (p-1)/q, p); + + if(our_g != g) return false; + + return true; + } diff --git a/doc/examples/sig_gen.cpp b/doc/examples/sig_gen.cpp new file mode 100644 index 000000000..69739047d --- /dev/null +++ b/doc/examples/sig_gen.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +using namespace Botan; + +bool check(std::map); + +int main() + { + try { + LibraryInitializer init; + + std::ifstream in("SigGen.rsp"); + if(!in) + throw Exception("Can't open response file"); + + std::map inputs; + + while(in.good()) + { + std::string line; + std::getline(in, line); + + if(line == "" || line[0] == '[' || line[0] == '#') + continue; + + std::vector name_and_val = split_on(line, '='); + + if(name_and_val.size() != 2) + throw Decoding_Error("Unexpected input: " + line); + + name_and_val[0].erase(name_and_val[0].size()-1); + name_and_val[1].erase(0, 1); + + std::string name = name_and_val[0], value = name_and_val[1]; + + inputs[name] = value; + + if(name == "S") + { + bool result = check(inputs); + if(result == false) + { + std::cout << " Check failed\n"; + + std::map::const_iterator i; + + for(i = inputs.begin(); i != inputs.end(); i++) + std::cout << i->first << " = " << i->second << "\n"; + } + inputs["Msg"] = inputs["R"] = inputs["S"] = ""; + } + } + } + catch(std::exception& e) + { + std::cout << e.what() << std::endl; + return 1; + } + return 0; + } + +bool check(std::map inputs) + { + BigInt p("0x"+inputs["P"]), + q("0x"+inputs["Q"]), + g("0x"+inputs["G"]), + y("0x"+inputs["Y"]); + + DSA_PublicKey key(DL_Group(p, q, g), y); + + Pipe pipe(new Hex_Decoder); + + pipe.process_msg(inputs["Msg"]); + pipe.start_msg(); + pipe.write(inputs["R"]); + pipe.write(inputs["S"] ); + pipe.end_msg(); + + std::auto_ptr verify(get_pk_verifier(key, "EMSA1(SHA-1)")); + + return verify->verify_message(pipe.read_all(0), pipe.read_all(1)); + } -- cgit v1.2.3