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/Span.h"
15#include "CryptographyError.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}; }
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;
147 Span<uint8_t> ciphertext, Span<uint8_t> tag,
size_t& bytesWritten);
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;
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;
285 friend decltype(internal);
288 struct SC_CRYPTOGRAPHY_EXPORT
Hkdf
294 Span<const uint8_t> info, Span<uint8_t> output);
298 Span<const uint8_t> ikm, Span<const uint8_t> info, Span<uint8_t> output);
Definition Cryptography.h:99
ResultCryptography init(AeadType type, Span< const uint8_t > key)
Initialize an AEAD context with a single key.
ResultCryptography 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.
ResultCryptography 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.
Aead(Backend backend)
Construct a session using an explicitly selected backend.
Aead()
Construct a session using the default native backend.
Definition Cryptography.h:167
ResultCryptography finish(Span< uint8_t > output, size_t &bytesWritten)
Finalize the operation and flush the remaining bytes / padding.
Cipher()
Construct a session using the default native backend.
void reset()
Discard the current operation and clear library-owned session state.
Cipher(Backend backend)
Construct a session using an explicitly selected backend.
ResultCryptography update(Span< const uint8_t > input, Span< uint8_t > output, size_t &bytesWritten)
Process the next chunk of bytes.
ResultCryptography start(CipherType type, Operation operation, Span< const uint8_t > key, Span< const uint8_t > iv)
Start a legacy AES-CBC PKCS#7 operation.
Definition Cryptography.h:42
Definition Cryptography.h:289
static ResultCryptography 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.
static ResultCryptography 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.
Definition Cryptography.h:231
Hmac()
Construct a session using the default native backend.
ResultCryptography getMac(MacResult &result)
Finalize the current HMAC computation.
ResultCryptography setType(HashType type)
Select which hash family to use for HMAC.
void reset()
Discard the current HMAC computation and clear library-owned session state.
Hmac(Backend backend)
Construct a session using an explicitly selected backend.
ResultCryptography add(Span< const uint8_t > data)
Add more message bytes to the running HMAC computation.
ResultCryptography setKey(Span< const uint8_t > key)
Set the HMAC key and reset the current running MAC state.
Definition Cryptography.h:79
Definition Cryptography.h:93
static ResultCryptography 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 ResultCryptography queryFeatures(Features &outFeatures)
Query which primitives are available on the default native backend.
static ResultCryptography queryFeatures(Backend backend, Features &outFeatures)
Query which primitives are available on a specific backend.
Cryptography result retaining a stable operation detail and one scalar diagnostic context.
Definition CryptographyError.h:195