crypto.cc: API cleanup and add generate() / to_string() methods

This commit is contained in:
Eelco Dolstra 2021-01-06 17:04:46 +01:00
parent 146af4ee9b
commit 555152ffe8
3 changed files with 45 additions and 29 deletions

View file

@ -8,15 +8,15 @@
namespace nix { namespace nix {
static std::pair<std::string, std::string> split(const string & s) static std::pair<std::string_view, std::string_view> split(std::string_view s)
{ {
size_t colon = s.find(':'); size_t colon = s.find(':');
if (colon == std::string::npos || colon == 0) if (colon == std::string::npos || colon == 0)
return {"", ""}; return {"", ""};
return {std::string(s, 0, colon), std::string(s, colon + 1)}; return {s.substr(0, colon), s.substr(colon + 1)};
} }
Key::Key(const string & s) Key::Key(std::string_view s)
{ {
auto ss = split(s); auto ss = split(s);
@ -29,7 +29,12 @@ Key::Key(const string & s)
key = base64Decode(key); key = base64Decode(key);
} }
SecretKey::SecretKey(const string & s) std::string Key::to_string() const
{
return name + ":" + base64Encode(key);
}
SecretKey::SecretKey(std::string_view s)
: Key(s) : Key(s)
{ {
#if HAVE_SODIUM #if HAVE_SODIUM
@ -45,7 +50,7 @@ SecretKey::SecretKey(const string & s)
} }
#endif #endif
std::string SecretKey::signDetached(const std::string & data) const std::string SecretKey::signDetached(std::string_view data) const
{ {
#if HAVE_SODIUM #if HAVE_SODIUM
unsigned char sig[crypto_sign_BYTES]; unsigned char sig[crypto_sign_BYTES];
@ -69,7 +74,21 @@ PublicKey SecretKey::toPublicKey() const
#endif #endif
} }
PublicKey::PublicKey(const string & s) SecretKey SecretKey::generate(std::string_view name)
{
#if HAVE_SODIUM
unsigned char pk[crypto_sign_PUBLICKEYBYTES];
unsigned char sk[crypto_sign_SECRETKEYBYTES];
if (crypto_sign_keypair(pk, sk) != 0)
throw Error("key generation failed");
return SecretKey(name, std::string((char *) sk, crypto_sign_SECRETKEYBYTES));
#else
noSodium();
#endif
}
PublicKey::PublicKey(std::string_view s)
: Key(s) : Key(s)
{ {
#if HAVE_SODIUM #if HAVE_SODIUM
@ -84,7 +103,7 @@ bool verifyDetached(const std::string & data, const std::string & sig,
#if HAVE_SODIUM #if HAVE_SODIUM
auto ss = split(sig); auto ss = split(sig);
auto key = publicKeys.find(ss.first); auto key = publicKeys.find(std::string(ss.first));
if (key == publicKeys.end()) return false; if (key == publicKeys.end()) return false;
auto sig2 = base64Decode(ss.second); auto sig2 = base64Decode(ss.second);

View file

@ -13,32 +13,40 @@ struct Key
/* Construct Key from a string in the format /* Construct Key from a string in the format
<name>:<key-in-base64>. */ <name>:<key-in-base64>. */
Key(const std::string & s); Key(std::string_view s);
std::string to_string() const;
protected: protected:
Key(const std::string & name, const std::string & key) Key(std::string_view name, std::string && key)
: name(name), key(key) { } : name(name), key(std::move(key)) { }
}; };
struct PublicKey; struct PublicKey;
struct SecretKey : Key struct SecretKey : Key
{ {
SecretKey(const std::string & s); SecretKey(std::string_view s);
/* Return a detached signature of the given string. */ /* Return a detached signature of the given string. */
std::string signDetached(const std::string & s) const; std::string signDetached(std::string_view s) const;
PublicKey toPublicKey() const; PublicKey toPublicKey() const;
static SecretKey generate(std::string_view name);
private:
SecretKey(std::string_view name, std::string && key)
: Key(name, std::move(key)) { }
}; };
struct PublicKey : Key struct PublicKey : Key
{ {
PublicKey(const std::string & data); PublicKey(std::string_view data);
private: private:
PublicKey(const std::string & name, const std::string & key) PublicKey(std::string_view name, std::string && key)
: Key(name, key) { } : Key(name, std::move(key)) { }
friend struct SecretKey; friend struct SecretKey;
}; };

View file

@ -19,10 +19,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#if HAVE_SODIUM
#include <sodium.h>
#endif
namespace nix_store { namespace nix_store {
@ -980,18 +976,11 @@ static void opGenerateBinaryCacheKey(Strings opFlags, Strings opArgs)
string secretKeyFile = *i++; string secretKeyFile = *i++;
string publicKeyFile = *i++; string publicKeyFile = *i++;
#if HAVE_SODIUM auto secretKey = SecretKey::generate(keyName);
unsigned char pk[crypto_sign_PUBLICKEYBYTES];
unsigned char sk[crypto_sign_SECRETKEYBYTES];
if (crypto_sign_keypair(pk, sk) != 0)
throw Error("key generation failed");
writeFile(publicKeyFile, keyName + ":" + base64Encode(string((char *) pk, crypto_sign_PUBLICKEYBYTES))); writeFile(publicKeyFile, secretKey.toPublicKey().to_string());
umask(0077); umask(0077);
writeFile(secretKeyFile, keyName + ":" + base64Encode(string((char *) sk, crypto_sign_SECRETKEYBYTES))); writeFile(secretKeyFile, secretKey.to_string());
#else
throw Error("Nix was not compiled with libsodium, required for signed binary cache support");
#endif
} }