-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83ab3a9
commit 8c70060
Showing
4 changed files
with
136 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "chacha_secret_box.hpp" | ||
|
||
#include <cstring> // std::memcpy | ||
|
||
#include <util/crypto.hpp> | ||
#include <defs/assert.hpp> | ||
#include <defs/minimal_geode.hpp> | ||
|
||
using namespace util::data; | ||
|
||
ChaChaSecretBox::ChaChaSecretBox(bytevector key) { | ||
CRYPTO_REQUIRE(key.size() == KEY_LEN, "provided key is too long or too short for ChaChaSecretBox") | ||
|
||
this->key = reinterpret_cast<byte*>(sodium_malloc( | ||
KEY_LEN | ||
)); | ||
|
||
CRYPTO_REQUIRE(this->key != nullptr, "sodium_malloc returned nullptr") | ||
|
||
std::memcpy(this->key, key.data(), KEY_LEN); | ||
} | ||
|
||
ChaChaSecretBox ChaChaSecretBox::withPassword(const std::string_view pw) { | ||
auto key = util::crypto::simpleHash(pw); | ||
return ChaChaSecretBox(key); | ||
} | ||
|
||
ChaChaSecretBox::~ChaChaSecretBox() { | ||
if (this->key) { | ||
sodium_free(this->key); | ||
} | ||
} | ||
|
||
constexpr size_t ChaChaSecretBox::nonceLength() { | ||
return NONCE_LEN; | ||
} | ||
|
||
constexpr size_t ChaChaSecretBox::macLength() { | ||
return MAC_LEN; | ||
} | ||
|
||
size_t ChaChaSecretBox::encryptInto(const byte* src, byte* dest, size_t size) { | ||
byte nonce[NONCE_LEN]; | ||
util::crypto::secureRandom(nonce, NONCE_LEN); | ||
|
||
byte* mac = dest + NONCE_LEN; | ||
byte* ciphertext = mac + MAC_LEN; | ||
|
||
CRYPTO_ERR_CHECK(crypto_secretbox_xchacha20poly1305_detached(ciphertext, mac, src, size, nonce, key), "crypto_secretbox_xchacha20poly1305_detached failed") | ||
|
||
// prepend the nonce | ||
std::memcpy(dest, nonce, NONCE_LEN); | ||
|
||
return size + PREFIX_LEN; | ||
} | ||
|
||
size_t ChaChaSecretBox::decryptInto(const byte* src, byte* dest, size_t size) { | ||
CRYPTO_REQUIRE(size >= PREFIX_LEN, "message is too short") | ||
|
||
size_t plaintextLength = size - PREFIX_LEN; | ||
|
||
const byte* nonce = src; | ||
const byte* mac = src + NONCE_LEN; | ||
const byte* ciphertext = mac + MAC_LEN; | ||
|
||
CRYPTO_ERR_CHECK(crypto_secretbox_xchacha20poly1305_open_detached(dest, ciphertext, mac, plaintextLength, nonce, key), "crypto_secretbox_xchacha20poly1305_open_detached failed") | ||
|
||
return plaintextLength; | ||
} | ||
|
||
void ChaChaSecretBox::setKey(const util::data::bytevector& src) { | ||
GLOBED_REQUIRE(src.size() == crypto_secretbox_KEYBYTES, "key size is too small or too big for ChaChaSecretBox") | ||
setKey(src.data()); | ||
} | ||
|
||
void ChaChaSecretBox::setKey(const util::data::byte* src) { | ||
std::memcpy(this->key, src, crypto_secretbox_KEYBYTES); | ||
} | ||
|
||
void ChaChaSecretBox::setPassword(const std::string_view pw) { | ||
auto key = util::crypto::simpleHash(pw); | ||
setKey(key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
#include "base_box.hpp" | ||
|
||
#include <sodium.h> | ||
|
||
/* | ||
* ChaChaSecretBox - SecretBox with prefix chacha algo | ||
* | ||
* Algorithm - XChaCha2020Poly1305 | ||
* Tag implementation - prefix | ||
*/ | ||
|
||
class ChaChaSecretBox final : public BaseCryptoBox { | ||
public: | ||
static const size_t NONCE_LEN = crypto_secretbox_xchacha20poly1305_NONCEBYTES; | ||
static const size_t MAC_LEN = crypto_secretbox_xchacha20poly1305_MACBYTES; | ||
static const size_t KEY_LEN = crypto_secretbox_xchacha20poly1305_KEYBYTES; | ||
static const size_t PREFIX_LEN = NONCE_LEN + MAC_LEN; | ||
|
||
ChaChaSecretBox(util::data::bytevector key); | ||
ChaChaSecretBox(const ChaChaSecretBox&) = delete; | ||
ChaChaSecretBox& operator=(const ChaChaSecretBox&) = delete; | ||
~ChaChaSecretBox(); | ||
|
||
static ChaChaSecretBox withPassword(const std::string_view pw); | ||
|
||
constexpr size_t nonceLength() override; | ||
constexpr size_t macLength() override; | ||
using BaseCryptoBox::prefixLength; | ||
|
||
size_t encryptInto(const util::data::byte* src, util::data::byte* dest, size_t size) override; | ||
size_t decryptInto(const util::data::byte* src, util::data::byte* dest, size_t size) override; | ||
|
||
void setKey(const util::data::bytevector& src); | ||
void setKey(const util::data::byte* src); | ||
// hashes the password and initializes the secret key with the hash | ||
void setPassword(const std::string_view pw); | ||
|
||
private: | ||
util::data::byte* key = nullptr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters