WIP update

Implemented AES.
Implemented AES-CBC.
Implemented HKDF.
Implemented HMAC.
Implemented PKCS7.
Implemented Fernet.
Added extensive logging and successfully tested ability to encrypt and
decrypt a packet.
This commit is contained in:
attermann committed 2023-10-08 23:59:43 -06:00
1 parent 4b062aa485
commit f9e3a22911
28 files changed
+1487 -328

No files matched your search

+86 -109
View File
@@ -3,8 +3,10 @@
#include "Reticulum.h"
#include "Packet.h"
#include "Log.h"
#include "Cryptography/Hashes.h"
#include "Cryptography/X25519.h"
#include "Cryptography/HKDF.h"
#include "Cryptography/Fernet.h"
#include "Cryptography/Random.h"
#include <string.h>
@@ -21,18 +23,22 @@ Identity::Identity(bool create_keys) : _object(new Object()) {
void Identity::createKeys() {
assert(_object);
// CRYPTO: create encryption private keys
_object->_prv = Cryptography::X25519PrivateKey::generate();
_object->_prv_bytes = _object->_prv->private_bytes();
debug("Identity::createKeys: prv bytes: " + _object->_prv_bytes.toHex());
_object->_sig_prv = Cryptography::Ed25519PrivateKey::generate();
_object->_sig_prv_bytes = _object->_sig_prv->private_bytes();
debug("Identity::createKeys: sig prv bytes: " + _object->_sig_prv_bytes.toHex());
// CRYPTO: create encryption public keys
_object->_pub = _object->_prv->public_key();
_object->_pub_bytes = _object->_pub->public_bytes();
debug("Identity::createKeys: pub bytes: " + _object->_pub_bytes.toHex());
// CRYPTO: create signature private keys
_object->_sig_prv = Cryptography::Ed25519PrivateKey::generate();
_object->_sig_prv_bytes = _object->_sig_prv->private_bytes();
debug("Identity::createKeys: sig prv bytes: " + _object->_sig_prv_bytes.toHex());
// CRYPTO: create signature public keys
_object->_sig_pub = _object->_sig_prv->public_key();
_object->_sig_pub_bytes = _object->_sig_pub->public_bytes();
debug("Identity::createKeys: sig pub bytes: " + _object->_sig_pub_bytes.toHex());
@@ -42,43 +48,6 @@ void Identity::createKeys() {
verbose("Identity keys created for " + _object->_hash.toHex());
}
/*
:returns: The public key as *bytes*
*/
Bytes Identity::get_public_key() {
assert(_object);
return _object->_pub_bytes + _object->_sig_pub_bytes;
}
void Identity::update_hashes() {
assert(_object);
_object->_hash = truncated_hash(get_public_key());
debug("Identity::update_hashes: hash: " + _object->_hash.toHex());
_object->_hexhash = _object->_hash.toHex();
debug("Identity::update_hashes: hexhash: " + _object->_hexhash);
};
/*
Get a SHA-256 hash of passed data.
:param data: Data to be hashed as *bytes*.
:returns: SHA-256 hash as *bytes*
*/
/*static*/ Bytes Identity::full_hash(const Bytes &data) {
return Cryptography::sha256(data);
}
/*
Get a truncated SHA-256 hash of passed data.
:param data: Data to be hashed as *bytes*.
:returns: Truncated SHA-256 hash as *bytes*
*/
/*static*/ Bytes Identity::truncated_hash(const Bytes &data) {
return full_hash(data).right(TRUNCATED_HASHLENGTH/8);
}
/*
Encrypts information for the identity.
@@ -89,31 +58,34 @@ Encrypts information for the identity.
*/
Bytes Identity::encrypt(const Bytes &plaintext) {
assert(_object);
if (_object->_pub) {
Cryptography::X25519PrivateKey::Ptr ephemeral_key = Cryptography::X25519PrivateKey::generate();
Bytes ephemeral_pub_bytes = ephemeral_key->public_key()->public_bytes();
/*
Bytes shared_key = ephemeral_key->exchange(_object->_pub);
Bytes derived_key = RNS.Cryptography.hkdf(
length=32,
derive_from=shared_key,
salt=get_salt(),
context=get_context(),
)
fernet = Fernet(derived_key)
ciphertext = fernet.encrypt(plaintext)
return ephemeral_pub_bytes + ciphertext;
*/
// MOCK
return Bytes::NONE;
}
else {
debug("Identity::encrypt: encrypting data...");
if (!_object->_pub) {
throw std::runtime_error("Encryption failed because identity does not hold a public key");
}
Cryptography::X25519PrivateKey::Ptr ephemeral_key = Cryptography::X25519PrivateKey::generate();
Bytes ephemeral_pub_bytes = ephemeral_key->public_key()->public_bytes();
debug("Identity::encrypt: ephemeral public key: " + ephemeral_pub_bytes.toHex());
// CRYPTO: create shared key for key exchange using own public key
//shared_key = ephemeral_key.exchange(self.pub)
Bytes shared_key = ephemeral_key->exchange(_object->_pub_bytes);
debug("Identity::encrypt: shared key: " + shared_key.toHex());
Bytes derived_key = Cryptography::hkdf(
32,
shared_key,
get_salt(),
get_context()
);
debug("Identity::encrypt: derived key: " + derived_key.toHex());
Cryptography::Fernet fernet(derived_key);
debug("Identity::encrypt: Fernet encrypting data of length " + std::to_string(plaintext.size()));
extreme("Identity::encrypt: plaintext: " + plaintext.toHex());
Bytes ciphertext = fernet.encrypt(plaintext);
extreme("Identity::encrypt: ciphertext: " + ciphertext.toHex());
return ephemeral_pub_bytes + ciphertext;
}
@@ -126,42 +98,49 @@ Decrypts information for the identity.
*/
Bytes Identity::decrypt(const Bytes &ciphertext_token) {
assert(_object);
if (_object->_prv) {
if (ciphertext_token.size() > Identity::KEYSIZE/8/2) {
Bytes plaintext;
try {
Bytes peer_pub_bytes = ciphertext_token.right(Identity::KEYSIZE/8/2);
Cryptography::X25519PublicKey::Ptr peer_pub = Cryptography::X25519PublicKey::from_public_bytes(peer_pub_bytes);
/*
Bytes shared_key = _object->_prv->exchange(peer_pub);
Bytes derived_key = RNS.Cryptography.hkdf(
length=32,
derive_from=shared_key,
salt=get_salt(),
context=get_context(),
)
fernet = Fernet(derived_key)
ciphertext = ciphertext_token[Identity.KEYSIZE//8//2:]
plaintext = fernet.decrypt(ciphertext)
*/
}
catch (std::exception &e) {
debug("Decryption by " + _object->_hash.toHex() + " failed: " + e.what());
}
return plaintext;
}
else {
debug("Decryption failed because the token size was invalid.");
return Bytes::NONE;
}
}
else {
debug("Identity::decrypt: decrypting data...");
if (!_object->_prv) {
throw std::runtime_error("Decryption failed because identity does not hold a private key");
}
if (ciphertext_token.size() <= Identity::KEYSIZE/8/2) {
debug("Decryption failed because the token size " + std::to_string(ciphertext_token.size()) + " was invalid.");
return Bytes::NONE;
}
Bytes plaintext;
try {
//peer_pub_bytes = ciphertext_token[:Identity.KEYSIZE//8//2]
Bytes peer_pub_bytes = ciphertext_token.left(Identity::KEYSIZE/8/2);
//peer_pub = X25519PublicKey.from_public_bytes(peer_pub_bytes)
//Cryptography::X25519PublicKey::Ptr peer_pub = Cryptography::X25519PublicKey::from_public_bytes(peer_pub_bytes);
debug("Identity::decrypt: peer public key: " + peer_pub_bytes.toHex());
// CRYPTO: create shared key for key exchange using peer public key
//shared_key = _object->_prv->exchange(peer_pub);
Bytes shared_key = _object->_prv->exchange(peer_pub_bytes);
debug("Identity::decrypt: shared key: " + shared_key.toHex());
Bytes derived_key = Cryptography::hkdf(
32,
shared_key,
get_salt(),
get_context()
);
debug("Identity::decrypt: derived key: " + derived_key.toHex());
Cryptography::Fernet fernet(derived_key);
//ciphertext = ciphertext_token[Identity.KEYSIZE//8//2:]
Bytes ciphertext(ciphertext_token.mid(Identity::KEYSIZE/8/2));
debug("Identity::decrypt: Fernet decrypting data of length " + std::to_string(ciphertext.size()));
extreme("Identity::decrypt: ciphertext: " + ciphertext.toHex());
plaintext = fernet.decrypt(ciphertext);
extreme("Identity::decrypt: plaintext: " + plaintext.toHex());
debug("Identity::decrypt: Fernet decrypted data of length " + std::to_string(plaintext.size()));
}
catch (std::exception &e) {
debug("Decryption by " + toString() + " failed: " + e.what());
}
return plaintext;
}
/*
@@ -173,18 +152,16 @@ Signs information by the identity.
*/
Bytes Identity::sign(const Bytes &message) {
assert(_object);
if (_object->_sig_prv) {
try {
return _object->_sig_prv->sign(message);
}
catch (std::exception &e) {
error("The identity " + toString() + " could not sign the requested message. The contained exception was: " + e.what());
throw e;
}
}
else {
if (!_object->_sig_prv) {
throw std::runtime_error("Signing failed because identity does not hold a private key");
}
try {
return _object->_sig_prv->sign(message);
}
catch (std::exception &e) {
error("The identity " + toString() + " could not sign the requested message. The contained exception was: " + e.what());
throw e;
}
}
/*