Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Cryptography.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Common/CompilerMacrosExport.h"
6#ifndef SC_EXPORT_LIBRARY_CRYPTOGRAPHY
7#define SC_EXPORT_LIBRARY_CRYPTOGRAPHY 0
8#endif
9#define SC_CRYPTOGRAPHY_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_CRYPTOGRAPHY)
10
11#include "../Common/CompilerMacrosLifetimeBound.h"
12#include "../Common/CompilerMove.h"
13#include "../Common/OpaqueObject.h"
14#include "../Common/Result.h"
15#include "../Common/Span.h"
16
17namespace SC
18{
21
24
32struct SC_CRYPTOGRAPHY_EXPORT Cryptography
33{
35 enum class Backend : uint8_t
36 {
37 Native,
38 OpenSSL,
39 };
40
41 struct Features
42 {
43 Backend backend = Backend::Native;
44
45 bool secureRandom = false;
46 bool aes128Gcm = false;
47 bool aes256Gcm = false;
48 bool aes128CbcPkcs7 = false;
49 bool aes256CbcPkcs7 = false;
50 bool hmacSha256 = false;
51 bool hmacSha384 = false;
52 bool hkdfSha256 = false;
53 bool hkdfSha384 = false;
54
57 size_t maximumAeadAssociatedDataSize = 0;
58 };
59
60 enum class HashType : uint8_t
61 {
62 SHA256,
63 SHA384,
64 };
65
66 enum class AeadType : uint8_t
67 {
68 AES128GCM,
69 AES256GCM,
70 };
71
72 enum class CipherType : uint8_t
73 {
74 AES128CBCPKCS7,
75 AES256CBCPKCS7,
76 };
77
78 struct MacResult
79 {
80 uint8_t bytes[48] = {0};
81 size_t size = 0;
82
83 Span<const uint8_t> toBytesSpan() const SC_LANGUAGE_LIFETIME_BOUND { return {bytes, size}; }
84 };
85
87 static Result queryFeatures(Features& outFeatures);
88
90 static Result queryFeatures(Backend backend, Features& outFeatures);
91
92 struct SC_CRYPTOGRAPHY_EXPORT Random
93 {
95 static Result fill(Span<uint8_t> output);
96 };
97
98 struct SC_CRYPTOGRAPHY_EXPORT Aead
99 {
100 private:
101 struct Internal;
102 struct InternalDefinition
103 {
104 static constexpr int Windows = 4600;
105 static constexpr int Apple = 128;
106 static constexpr int Linux = 96;
107 static constexpr int Default = Linux;
108
109 static constexpr size_t Alignment = alignof(void*);
110
111 using Object = Internal;
112 };
113
114 public:
115 using InternalOpaque = OpaqueObject<InternalDefinition>;
116
117 private:
118 InternalOpaque internal;
119
120 public:
124 explicit Aead(Backend backend);
125 ~Aead();
126 Aead(const Aead&) = delete;
127 Aead(Aead&&) = delete;
128 Aead& operator=(const Aead&) = delete;
129 Aead& operator=(Aead&&) = delete;
130
133 Result init(AeadType type, Span<const uint8_t> key);
134
146 Result seal(Span<const uint8_t> nonce, Span<const uint8_t> aad, Span<const uint8_t> plaintext,
147 Span<uint8_t> ciphertext, Span<uint8_t> tag, size_t& bytesWritten);
148
159 Result open(Span<const uint8_t> nonce, Span<const uint8_t> aad, Span<const uint8_t> ciphertext,
160 Span<const uint8_t> tag, Span<uint8_t> plaintext, size_t& bytesWritten);
161
162 private:
163 friend decltype(internal);
164 };
165
166 struct SC_CRYPTOGRAPHY_EXPORT Cipher
167 {
168 enum class Operation : uint8_t
169 {
170 Encrypt,
171 Decrypt,
172 };
173
174 private:
175 struct Internal;
176 struct InternalDefinition
177 {
178 static constexpr int Windows = 4600;
179 static constexpr int Apple = 512;
180 static constexpr int Linux = 128;
181 static constexpr int Default = Linux;
182
183 static constexpr size_t Alignment = alignof(void*);
184
185 using Object = Internal;
186 };
187
188 public:
189 using InternalOpaque = OpaqueObject<InternalDefinition>;
190
191 private:
192 InternalOpaque internal;
193
194 public:
198 explicit Cipher(Backend backend);
199 ~Cipher();
200 Cipher(const Cipher&) = delete;
201 Cipher(Cipher&&) = delete;
202 Cipher& operator=(const Cipher&) = delete;
203 Cipher& operator=(Cipher&&) = delete;
204
209 Result start(CipherType type, Operation operation, Span<const uint8_t> key, Span<const uint8_t> iv);
210
215 Result update(Span<const uint8_t> input, Span<uint8_t> output, size_t& bytesWritten);
216
220 Result finish(Span<uint8_t> output, size_t& bytesWritten);
221
224 void reset();
225
226 private:
227 friend decltype(internal);
228 };
229
230 struct SC_CRYPTOGRAPHY_EXPORT Hmac
231 {
232 private:
233 struct Internal;
234 struct InternalDefinition
235 {
236 static constexpr int Windows = 2600;
237 static constexpr int Apple = 512;
238 static constexpr int Linux = 64;
239 static constexpr int Default = Linux;
240
241 static constexpr size_t Alignment = alignof(void*);
242
243 using Object = Internal;
244 };
245
246 public:
247 using InternalOpaque = OpaqueObject<InternalDefinition>;
248
249 private:
250 InternalOpaque internal;
251
252 public:
256 explicit Hmac(Backend backend);
257 ~Hmac();
258 Hmac(const Hmac&) = delete;
259 Hmac(Hmac&&) = delete;
260 Hmac& operator=(const Hmac&) = delete;
261 Hmac& operator=(Hmac&&) = delete;
262
266 Result setType(HashType type);
267
270 Result setKey(Span<const uint8_t> key);
271
273 Result add(Span<const uint8_t> data);
274
278 Result getMac(MacResult& result);
279
282 void reset();
283
284 private:
285 friend decltype(internal);
286 };
287
288 struct SC_CRYPTOGRAPHY_EXPORT Hkdf
289 {
293 static Result derive(HashType type, Span<const uint8_t> salt, Span<const uint8_t> ikm, Span<const uint8_t> info,
294 Span<uint8_t> output);
295
297 static Result derive(Backend backend, HashType type, Span<const uint8_t> salt, Span<const uint8_t> ikm,
298 Span<const uint8_t> info, Span<uint8_t> output);
299 };
300};
301
303} // namespace SC
Definition Cryptography.h:99
Aead(Backend backend)
Construct a session using an explicitly selected backend.
Result open(Span< const uint8_t > nonce, Span< const uint8_t > aad, Span< const uint8_t > ciphertext, Span< const uint8_t > tag, Span< uint8_t > plaintext, size_t &bytesWritten)
Decrypt a single message using AEAD.
Result init(AeadType type, Span< const uint8_t > key)
Initialize an AEAD context with a single key.
Aead()
Construct a session using the default native backend.
Result seal(Span< const uint8_t > nonce, Span< const uint8_t > aad, Span< const uint8_t > plaintext, Span< uint8_t > ciphertext, Span< uint8_t > tag, size_t &bytesWritten)
Encrypt a single message using AEAD.
Definition Cryptography.h:167
Cipher()
Construct a session using the default native backend.
Result start(CipherType type, Operation operation, Span< const uint8_t > key, Span< const uint8_t > iv)
Start a legacy AES-CBC PKCS#7 operation.
void reset()
Discard the current operation and clear library-owned session state.
Result finish(Span< uint8_t > output, size_t &bytesWritten)
Finalize the operation and flush the remaining bytes / padding.
Cipher(Backend backend)
Construct a session using an explicitly selected backend.
Result update(Span< const uint8_t > input, Span< uint8_t > output, size_t &bytesWritten)
Process the next chunk of bytes.
Definition Cryptography.h:42
Definition Cryptography.h:289
static Result derive(HashType type, Span< const uint8_t > salt, Span< const uint8_t > ikm, Span< const uint8_t > info, Span< uint8_t > output)
Derive output keying material with RFC5869 HKDF built on top of the native HMAC primitive.
static Result derive(Backend backend, HashType type, Span< const uint8_t > salt, Span< const uint8_t > ikm, Span< const uint8_t > info, Span< uint8_t > output)
Derive output keying material using HMAC from a specific backend.
Definition Cryptography.h:231
Hmac()
Construct a session using the default native backend.
Result setKey(Span< const uint8_t > key)
Set the HMAC key and reset the current running MAC state.
void reset()
Discard the current HMAC computation and clear library-owned session state.
Result setType(HashType type)
Select which hash family to use for HMAC.
Hmac(Backend backend)
Construct a session using an explicitly selected backend.
Result add(Span< const uint8_t > data)
Add more message bytes to the running HMAC computation.
Result getMac(MacResult &result)
Finalize the current HMAC computation.
Definition Cryptography.h:79
Definition Cryptography.h:93
static Result fill(Span< uint8_t > output)
Fill output with cryptographically secure random bytes.
Platform-backed symmetric cryptography primitives.
Definition Cryptography.h:33
Backend
Selects the provider used by a cryptographic session.
Definition Cryptography.h:36
static Result queryFeatures(Features &outFeatures)
Query which primitives are available on the default native backend.
static Result queryFeatures(Backend backend, Features &outFeatures)
Query which primitives are available on a specific backend.