1#ifndef TENDUKE_CRYPTO_ABSTRACTCIPHERS_H
2#define TENDUKE_CRYPTO_ABSTRACTCIPHERS_H
5#include "CryptoException.h"
6#include "../utl/BinaryView.h"
25 std::unique_ptr<::tenduke::utl::BinaryData>
decrypt(
26 const ::tenduke::utl::DataSpan &ciphertext,
27 const ::tenduke::utl::DataSpan &encryptionKey
32 ciphertext.size_bytes(),
34 encryptionKey.size_bytes()
42 const ::tenduke::utl::DataSpan &ciphertext,
43 const ::tenduke::utl::DataSpan &encryptionKey
46 auto plaintext =
decrypt(ciphertext, encryptionKey);
47 return std::string(
reinterpret_cast<char *
>(plaintext->getData()), plaintext->getLength());
53 std::unique_ptr<::tenduke::utl::BinaryData>
encrypt(
54 const ::tenduke::utl::DataSpan &plaintext,
55 const ::tenduke::utl::DataSpan &encryptionKey
60 plaintext.size_bytes(),
62 encryptionKey.size_bytes()
69 std::unique_ptr<::tenduke::utl::BinaryData>
encrypt(
70 const std::string &plaintext,
71 const ::tenduke::utl::DataSpan &encryptionKey
91 const unsigned char *ciphertext,
92 std::size_t ciphertextLength,
93 const unsigned char *encryptionKey,
94 std::size_t encryptionKeyLength
101 const unsigned char *plaintext,
102 std::size_t plaintextLength,
103 const unsigned char *encryptionKey,
104 std::size_t encryptionKeyLength
112 virtual void writeHeader(
unsigned char *buffer, std::uint16_t version)
const
115 buffer[0] =
static_cast<unsigned char>((
BLOB_MAGIC >> 24) & 0xFF);
116 buffer[1] =
static_cast<unsigned char>((
BLOB_MAGIC >> 16) & 0xFF);
117 buffer[2] =
static_cast<unsigned char>((
BLOB_MAGIC >> 8) & 0xFF);
118 buffer[3] =
static_cast<unsigned char>(
BLOB_MAGIC & 0xFF);
121 buffer[4] =
static_cast<unsigned char>((version >> 8) & 0xFF);
122 buffer[5] =
static_cast<unsigned char>(version & 0xFF);
133 const unsigned char *buffer,
134 std::size_t bufferLength
138 throw ::tenduke::crypto::CryptoException(
139 "cipher_invalid_ciphertext",
140 "Ciphertext too short to contain header"
145 std::uint32_t magic = (
static_cast<std::uint32_t
>(buffer[0]) << 24) |
146 (
static_cast<std::uint32_t
>(buffer[1]) << 16) |
147 (
static_cast<std::uint32_t
>(buffer[2]) << 8) |
148 static_cast<std::uint32_t
>(buffer[3]);
151 throw ::tenduke::crypto::CryptoException(
152 "cipher_invalid_ciphertext",
153 "Invalid blob magic value"
158 std::uint16_t version = (
static_cast<std::uint16_t
>(buffer[4]) << 8) |
159 static_cast<std::uint16_t
>(buffer[5]);
Abstract base class for cipher implementations.
Definition AbstractCiphers.h:23
static constexpr std::uint32_t BLOB_MAGIC
Magic value for encrypted blob header.
Definition AbstractCiphers.h:82
std::string decryptString(const ::tenduke::utl::DataSpan &ciphertext, const ::tenduke::utl::DataSpan &encryptionKey) const override
Decrypts given data using the system default cipher (AES256 GCM) to a string.This method expects the ...
Definition AbstractCiphers.h:41
virtual std::unique_ptr<::tenduke::utl::BinaryData > encryptBytes(const unsigned char *plaintext, std::size_t plaintextLength, const unsigned char *encryptionKey, std::size_t encryptionKeyLength) const =0
Encrypts the data.
virtual std::uint16_t readAndValidateHeader(const unsigned char *buffer, std::size_t bufferLength) const
Reads and validates the blob header.
Definition AbstractCiphers.h:132
std::unique_ptr<::tenduke::utl::BinaryData > encrypt(const std::string &plaintext, const ::tenduke::utl::DataSpan &encryptionKey) const override
Encrypts given data using the system default cipher (AES256 GCM).The output consists of magic (4 byte...
Definition AbstractCiphers.h:69
std::unique_ptr<::tenduke::utl::BinaryData > decrypt(const ::tenduke::utl::DataSpan &ciphertext, const ::tenduke::utl::DataSpan &encryptionKey) const override
Decrypts given data using the system default cipher (AES256 GCM).
Definition AbstractCiphers.h:25
virtual std::unique_ptr<::tenduke::utl::BinaryData > decryptBytes(const unsigned char *ciphertext, std::size_t ciphertextLength, const unsigned char *encryptionKey, std::size_t encryptionKeyLength) const =0
Decrypts the data.
std::unique_ptr<::tenduke::utl::BinaryData > encrypt(const ::tenduke::utl::DataSpan &plaintext, const ::tenduke::utl::DataSpan &encryptionKey) const override
Encrypts given data using the system default cipher (AES256 GCM).The output consists of magic (4 byte...
Definition AbstractCiphers.h:53
static constexpr std::size_t BLOB_HEADER_SIZE
Header size: magic (4 bytes) + version (2 bytes).
Definition AbstractCiphers.h:85
virtual void writeHeader(unsigned char *buffer, std::uint16_t version) const
Writes the blob header (magic and version) in network byte order.
Definition AbstractCiphers.h:112
A "service locator" for cipher-related operations.
Definition Ciphers.h:18
Cryptography services.
Definition AbstractCiphers.h:12
BinaryView make_view(const unsigned char *data, const std::size_t size)
A helper function to create a tenduke::utl::BinaryView from a const pointer.
Definition BinaryView.h:91
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4