Add reusable strict JSON, rate limiting, and sealed secrets
This commit is contained in:
10 files changed
+778
-40
No files matched your search
@@ -175,4 +175,76 @@ namespace Void::Security
|
||||
static_cast<unsigned long long>(password.size())) == 0;
|
||||
}
|
||||
|
||||
Void::Result::Result<SecretKey> random_secret_key()
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
|
||||
static_assert(secret_key_bytes == crypto_secretbox_KEYBYTES);
|
||||
SecretKey key{};
|
||||
crypto_secretbox_keygen(key.data());
|
||||
return key;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<unsigned char>>
|
||||
seal_secret(std::span<const unsigned char> plaintext,
|
||||
const SecretKey& key)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
static_assert(secret_nonce_bytes == crypto_secretbox_NONCEBYTES);
|
||||
static_assert(secret_authentication_bytes == crypto_secretbox_MACBYTES);
|
||||
if (plaintext.size() > std::numeric_limits<std::size_t>::max() -
|
||||
secret_nonce_bytes -
|
||||
secret_authentication_bytes) {
|
||||
return Void::Result::fail(
|
||||
"Security::seal_secret: plaintext is too large");
|
||||
}
|
||||
|
||||
std::vector<unsigned char> envelope(secret_nonce_bytes +
|
||||
secret_authentication_bytes +
|
||||
plaintext.size());
|
||||
auto* nonce = envelope.data();
|
||||
auto* ciphertext = nonce + secret_nonce_bytes;
|
||||
randombytes_buf(nonce, secret_nonce_bytes);
|
||||
if (crypto_secretbox_easy(
|
||||
ciphertext, plaintext.data(),
|
||||
static_cast<unsigned long long>(plaintext.size()), nonce,
|
||||
key.data()) != 0) {
|
||||
return Void::Result::fail(
|
||||
"Security::seal_secret: encryption failed");
|
||||
}
|
||||
return envelope;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<unsigned char>>
|
||||
open_secret(std::span<const unsigned char> envelope,
|
||||
const SecretKey& key)
|
||||
{
|
||||
if (const auto initialized = ensure_sodium(); !initialized) {
|
||||
return Void::Result::fail(std::move(initialized).error());
|
||||
}
|
||||
if (envelope.size() <
|
||||
secret_nonce_bytes + secret_authentication_bytes) {
|
||||
return Void::Result::fail(
|
||||
"Security::open_secret: envelope is truncated");
|
||||
}
|
||||
|
||||
const auto* nonce = envelope.data();
|
||||
const auto* ciphertext = nonce + secret_nonce_bytes;
|
||||
const auto ciphertext_size = envelope.size() - secret_nonce_bytes;
|
||||
std::vector<unsigned char> plaintext(ciphertext_size -
|
||||
secret_authentication_bytes);
|
||||
if (crypto_secretbox_open_easy(
|
||||
plaintext.data(), ciphertext,
|
||||
static_cast<unsigned long long>(ciphertext_size), nonce,
|
||||
key.data()) != 0) {
|
||||
return Void::Result::fail(
|
||||
"Security::open_secret: authentication failed");
|
||||
}
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
} // namespace Void::Security
|
||||
Reference in new issue
Block a user