10Duke Enterprise C++ SDK
Loading...
Searching...
No Matches
AbstractCiphers.h
1#ifndef TENDUKE_CRYPTO_ABSTRACTCIPHERS_H
2#define TENDUKE_CRYPTO_ABSTRACTCIPHERS_H
3
4#include "Ciphers.h"
5#include "CryptoException.h"
6#include "../utl/BinaryView.h"
7#include <cstddef>
8#include <cstdint>
9#include <memory>
10#include <string>
11
12namespace tenduke { namespace crypto {
13
22class AbstractCiphers : public virtual Ciphers
23{
24public:
25 std::unique_ptr<::tenduke::utl::BinaryData> decrypt(
26 const ::tenduke::utl::DataSpan &ciphertext,
27 const ::tenduke::utl::DataSpan &encryptionKey
28 ) const override
29 {
30 return decryptBytes(
31 ciphertext.data(),
32 ciphertext.size_bytes(),
33 encryptionKey.data(),
34 encryptionKey.size_bytes()
35 );
36 }
37
41 std::string decryptString(
42 const ::tenduke::utl::DataSpan &ciphertext,
43 const ::tenduke::utl::DataSpan &encryptionKey
44 ) const override
45 {
46 auto plaintext = decrypt(ciphertext, encryptionKey);
47 return std::string(reinterpret_cast<char *>(plaintext->getData()), plaintext->getLength());
48 }
49
53 std::unique_ptr<::tenduke::utl::BinaryData> encrypt(
54 const ::tenduke::utl::DataSpan &plaintext,
55 const ::tenduke::utl::DataSpan &encryptionKey
56 ) const override
57 {
58 return encryptBytes(
59 plaintext.data(),
60 plaintext.size_bytes(),
61 encryptionKey.data(),
62 encryptionKey.size_bytes()
63 );
64 }
65
69 std::unique_ptr<::tenduke::utl::BinaryData> encrypt(
70 const std::string &plaintext,
71 const ::tenduke::utl::DataSpan &encryptionKey
72 ) const override
73 {
74 return encrypt(
76 encryptionKey
77 );
78 }
79
80protected:
82 static constexpr std::uint32_t BLOB_MAGIC = 0x44554B45; // "DUKE" in network byte order (big-endian)
83
85 static constexpr std::size_t BLOB_HEADER_SIZE = 6;
86
90 virtual std::unique_ptr<::tenduke::utl::BinaryData> decryptBytes(
91 const unsigned char *ciphertext,
92 std::size_t ciphertextLength,
93 const unsigned char *encryptionKey,
94 std::size_t encryptionKeyLength
95 ) const = 0;
96
100 virtual std::unique_ptr<::tenduke::utl::BinaryData> encryptBytes(
101 const unsigned char *plaintext,
102 std::size_t plaintextLength,
103 const unsigned char *encryptionKey,
104 std::size_t encryptionKeyLength
105 ) const = 0;
106
112 virtual void writeHeader(unsigned char *buffer, std::uint16_t version) const
113 {
114 // Write magic in network byte order (big-endian)
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);
119
120 // Write version in network byte order (big-endian)
121 buffer[4] = static_cast<unsigned char>((version >> 8) & 0xFF);
122 buffer[5] = static_cast<unsigned char>(version & 0xFF);
123 }
124
132 virtual std::uint16_t readAndValidateHeader(
133 const unsigned char *buffer,
134 std::size_t bufferLength
135 ) const
136 {
137 if (bufferLength < BLOB_HEADER_SIZE) {
138 throw ::tenduke::crypto::CryptoException(
139 "cipher_invalid_ciphertext",
140 "Ciphertext too short to contain header"
141 );
142 }
143
144 // Read magic in network byte order (big-endian)
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]);
149
150 if (magic != BLOB_MAGIC) {
151 throw ::tenduke::crypto::CryptoException(
152 "cipher_invalid_ciphertext",
153 "Invalid blob magic value"
154 );
155 }
156
157 // Read version in network byte order (big-endian)
158 std::uint16_t version = (static_cast<std::uint16_t>(buffer[4]) << 8) |
159 static_cast<std::uint16_t>(buffer[5]);
160
161 return version;
162 }
163};
164
165}}
166
167#endif //TENDUKE_CRYPTO_ABSTRACTCIPHERS_H
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