5#include "../Common/CompilerMacrosExport.h"
6#ifndef SC_EXPORT_LIBRARY_CRYPTOGRAPHY
7#define SC_EXPORT_LIBRARY_CRYPTOGRAPHY 0
9#define SC_CRYPTOGRAPHY_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_CRYPTOGRAPHY)
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"
43 Backend backend = Backend::Native;
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;
57 size_t maximumAeadAssociatedDataSize = 0;
60 enum class HashType : uint8_t
66 enum class AeadType : uint8_t
72 enum class CipherType : uint8_t
80 uint8_t bytes[48] = {0};
83 Span<const uint8_t> toBytesSpan()
const SC_LANGUAGE_LIFETIME_BOUND {
return {bytes, size}; }
95 static Result
fill(Span<uint8_t> output);
98 struct SC_CRYPTOGRAPHY_EXPORT
Aead
102 struct InternalDefinition
104 static constexpr int Windows = 4600;
105 static constexpr int Apple = 128;
106 static constexpr int Linux = 96;
107 static constexpr int Default = Linux;
109 static constexpr size_t Alignment =
alignof(
void*);
111 using Object = Internal;
115 using InternalOpaque = OpaqueObject<InternalDefinition>;
118 InternalOpaque internal;
128 Aead& operator=(
const Aead&) =
delete;
133 Result
init(AeadType type, Span<const uint8_t> key);
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);
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);
163 friend decltype(internal);
168 enum class Operation : uint8_t
176 struct InternalDefinition
178 static constexpr int Windows = 4600;
179 static constexpr int Apple = 512;
180 static constexpr int Linux = 128;
181 static constexpr int Default = Linux;
183 static constexpr size_t Alignment =
alignof(
void*);
185 using Object = Internal;
189 using InternalOpaque = OpaqueObject<InternalDefinition>;
192 InternalOpaque internal;
209 Result
start(CipherType type, Operation operation, Span<const uint8_t> key, Span<const uint8_t> iv);
215 Result
update(Span<const uint8_t> input, Span<uint8_t> output,
size_t& bytesWritten);
220 Result
finish(Span<uint8_t> output,
size_t& bytesWritten);
227 friend decltype(internal);
230 struct SC_CRYPTOGRAPHY_EXPORT
Hmac
234 struct InternalDefinition
236 static constexpr int Windows = 2600;
237 static constexpr int Apple = 512;
238 static constexpr int Linux = 64;
239 static constexpr int Default = Linux;
241 static constexpr size_t Alignment =
alignof(
void*);
243 using Object = Internal;
247 using InternalOpaque = OpaqueObject<InternalDefinition>;
250 InternalOpaque internal;
260 Hmac& operator=(
const Hmac&) =
delete;
273 Result
add(Span<const uint8_t> data);
285 friend decltype(internal);
288 struct SC_CRYPTOGRAPHY_EXPORT
Hkdf
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);
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);
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.