diff --git a/Makefile b/Makefile index 49e622f..c639b17 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: auth-server gen-keypair +all: auth-server gen-key gen-keypair %.o: %.cc g++ -c -o $@ $< @@ -6,5 +6,8 @@ all: auth-server gen-keypair 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-key: gen-key.o crypto.o + g++ -o gen-key gen-key.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 index be4d716..a0dc488 100644 --- a/crypto.cc +++ b/crypto.cc @@ -1,6 +1,8 @@ #include "crypto.h" #include "nacl/build/instance1/include/amd64/crypto_box.h" +#include "nacl/build/instance1/include/amd64/crypto_secretbox.h" +#include "nacl/build/instance1/include/amd64/randombytes.h" std::string CryptoBase::BinToHex(const std::string& bin) { static const char *hex = "0123456789ABCDEF"; @@ -14,6 +16,12 @@ std::string CryptoBase::BinToHex(const std::string& bin) { return ret; } -void CryptoBase::GenKeyPair(std::string* sk, std::string* pk) { - *pk = crypto_box_keypair(sk); +void CryptoBase::GenKey(std::string* key) { + char buf[crypto_secretbox_KEYBYTES]; + randombytes((unsigned char *)buf, crypto_secretbox_KEYBYTES); + *key = buf; +} + +void CryptoBase::GenKeyPair(std::string* secret_key, std::string* public_key) { + *public_key = crypto_box_keypair(secret_key); } diff --git a/crypto.h b/crypto.h index 4eb55c3..6df3b98 100644 --- a/crypto.h +++ b/crypto.h @@ -3,5 +3,6 @@ class CryptoBase { public: static std::string BinToHex(const std::string& bin); - static void GenKeyPair(std::string* sk, std::string* pk); + static void GenKey(std::string* key); + static void GenKeyPair(std::string* secret_key, std::string* public_key); }; diff --git a/gen-key.cc b/gen-key.cc new file mode 100644 index 0000000..95c0bf8 --- /dev/null +++ b/gen-key.cc @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "crypto.h" + +int main(int argc, char *argv[]) { + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " key_filename" << std::endl; + return 1; + } + + std::string key; + CryptoBase::GenKey(&key); + + { + std::fstream key_file(argv[1], std::fstream::out); + if (key_file.fail()) { + std::cerr << "Failed to open key file" << std::endl; + return 1; + } + key_file << "# Shared secret key" << std::endl; + key_file << CryptoBase::BinToHex(key) << std::endl; + } + + return 0; +}