From d37b9a7e413abecec6f576ae6dd896066fa1445c Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 5 Feb 2015 12:55:48 +0000 Subject: [PATCH] Public key generation --- Makefile | 13 ++++++++----- crypto.cc | 19 +++++++++++++++++++ crypto.h | 7 +++++++ gen-keypair.cc | 38 ++++++++++++++++++++++++++++++++++++++ gen-pubkeypair.cc | 30 ------------------------------ 5 files changed, 72 insertions(+), 35 deletions(-) create mode 100644 crypto.cc create mode 100644 crypto.h create mode 100644 gen-keypair.cc delete mode 100644 gen-pubkeypair.cc diff --git a/Makefile b/Makefile index 9a3dddf..49e622f 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ -all: auth-server gen-pubkeypair +all: auth-server gen-keypair -auth-server: auth-server.cc - g++ -o auth-server auth-server.cc nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a +%.o: %.cc + g++ -c -o $@ $< -gen-pubkeypair: gen-pubkeypair.cc - g++ -o gen-pubkeypair gen-pubkeypair.cc nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a +auth-server: auth-server.o crypto.o + g++ -o auth-server auth-server.o crypto.o nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a + +gen-keypair: gen-keypair.o crypto.o + g++ -o gen-keypair gen-keypair.o crypto.o nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a diff --git a/crypto.cc b/crypto.cc new file mode 100644 index 0000000..be4d716 --- /dev/null +++ b/crypto.cc @@ -0,0 +1,19 @@ +#include "crypto.h" + +#include "nacl/build/instance1/include/amd64/crypto_box.h" + +std::string CryptoBase::BinToHex(const std::string& bin) { + static const char *hex = "0123456789ABCDEF"; + std::string ret; + ret.reserve(bin.length() * 2); + for (size_t i = 0; i < bin.length(); i++) { + const char c = bin[i]; + ret.push_back(hex[(c & 0xf0) >> 4]); + ret.push_back(hex[c & 0x0f]); + } + return ret; +} + +void CryptoBase::GenKeyPair(std::string* sk, std::string* pk) { + *pk = crypto_box_keypair(sk); +} diff --git a/crypto.h b/crypto.h new file mode 100644 index 0000000..4eb55c3 --- /dev/null +++ b/crypto.h @@ -0,0 +1,7 @@ +#include + +class CryptoBase { + public: + static std::string BinToHex(const std::string& bin); + static void GenKeyPair(std::string* sk, std::string* pk); +}; diff --git a/gen-keypair.cc b/gen-keypair.cc new file mode 100644 index 0000000..dc9a30f --- /dev/null +++ b/gen-keypair.cc @@ -0,0 +1,38 @@ +#include +#include +#include + +#include "crypto.h" + +int main(int argc, char *argv[]) { + if (argc < 3) { + std::cerr << "Usage: " << argv[0] << " secret_filename public_filename" << std::endl; + return 1; + } + + std::string pk; + std::string sk; + CryptoBase::GenKeyPair(&sk, &pk); + + { + std::fstream skf(argv[1], std::fstream::out); + if (skf.fail()) { + std::cerr << "Failed to open secret key file" << std::endl; + return 1; + } + skf << "# Secret key" << std::endl; + skf << CryptoBase::BinToHex(sk) << std::endl; + } + + { + std::fstream pkf(argv[2], std::fstream::out); + if (pkf.fail()) { + std::cerr << "Failed to open public key file" << std::endl; + return 1; + } + pkf << "# Public key" << std::endl; + pkf << CryptoBase::BinToHex(pk) << std::endl; + } + + return 0; +} diff --git a/gen-pubkeypair.cc b/gen-pubkeypair.cc deleted file mode 100644 index 4b9aa86..0000000 --- a/gen-pubkeypair.cc +++ /dev/null @@ -1,30 +0,0 @@ -#include - -#include "nacl/build/instance1/include/amd64/crypto_box.h" - -class CryptoBase { - public: - static const std::string BinToHex(const std::string& bin) { - static const char *hex = "0123456789ABCDEF"; - std::string ret; - ret.reserve(bin.length() * 2); - for (size_t i = 0; i < bin.length(); i++) { - const char c = bin[i]; - ret.push_back(hex[(c & 0xf0) >> 4]); - ret.push_back(hex[c & 0x0f]); - } - return ret; - } -}; - -int main() { - std::string pk; - std::string sk; - - pk = crypto_box_keypair(&sk); - - std::cout << "Secret: " << CryptoBase::BinToHex(sk) << std::endl; - std::cout << "Public: " << CryptoBase::BinToHex(pk) << std::endl; - - return 0; -}