diff options
author | lloyd <[email protected]> | 2014-01-10 00:08:13 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-01-10 00:08:13 +0000 |
commit | 57789bdfc55061002b2727d0b32587612829a37c (patch) | |
tree | 99f36631b4ec50c5187a1b0a7c256b99182373ad /src/cmd/dsa_ver.cpp | |
parent | 94968c917407a63d888fd3eb4d02491f60de6ebc (diff) |
Split up test vectors into per-algo files and app into botan-test for
the tests and botan for everything else.
Diffstat (limited to 'src/cmd/dsa_ver.cpp')
-rw-r--r-- | src/cmd/dsa_ver.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/cmd/dsa_ver.cpp b/src/cmd/dsa_ver.cpp new file mode 100644 index 000000000..a5d0ca271 --- /dev/null +++ b/src/cmd/dsa_ver.cpp @@ -0,0 +1,84 @@ +#include "apps.h" +#include <iostream> +#include <iomanip> +#include <fstream> +#include <cstdlib> +#include <string> +#include <memory> + +#include <botan/pubkey.h> +#include <botan/dsa.h> +#include <botan/b64_filt.h> +using namespace Botan; + +namespace { + +secure_vector<byte> b64_decode(const std::string& in) + { + Pipe pipe(new Base64_Decoder); + pipe.process_msg(in); + return pipe.read_all(); + } + +} + +int dsa_verify_main(int argc, char* argv[]) + { + if(argc != 4) + { + std::cout << "Usage: " << argv[0] + << " keyfile messagefile sigfile" << std::endl; + return 1; + } + + + try { + std::ifstream message(argv[2], std::ios::binary); + if(!message) + { + std::cout << "Couldn't read the message file." << std::endl; + return 1; + } + + std::ifstream sigfile(argv[3]); + if(!sigfile) + { + std::cout << "Couldn't read the signature file." << std::endl; + return 1; + } + + std::string sigstr; + getline(sigfile, sigstr); + + std::auto_ptr<X509_PublicKey> key(X509::load_key(argv[1])); + DSA_PublicKey* dsakey = dynamic_cast<DSA_PublicKey*>(key.get()); + + if(!dsakey) + { + std::cout << "The loaded key is not a DSA key!\n"; + return 1; + } + + secure_vector<byte> sig = b64_decode(sigstr); + + PK_Verifier ver(*dsakey, "EMSA1(SHA-1)"); + + DataSource_Stream in(message); + byte buf[4096] = { 0 }; + while(size_t got = in.read(buf, sizeof(buf))) + ver.update(buf, got); + + const bool ok = ver.check_signature(sig); + + if(ok) + std::cout << "Signature verified\n"; + else + std::cout << "Signature did NOT verify\n"; + } + catch(std::exception& e) + { + std::cout << "Exception caught: " << e.what() << std::endl; + return 1; + } + return 0; + } |