Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
CryptographyError.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Common/Result.h"
6
7namespace SC
8{
11
13enum class CryptographyError : uint32_t
14{
15 OperationUnsupported = 1,
16 BackendUnavailable,
17 InvalidAeadType,
18 InvalidCipherType,
19 InvalidHashType,
20 InvalidCipherOperation,
21 InvalidKeySize,
22 InvalidNonceSize,
23 InvalidInitializationVectorSize,
24 InvalidTagSize,
25 SizeLimitExceeded,
26 OutputCapacityExceeded,
27 BufferOverlap,
28 SessionNotInitialized,
29 KeyNotSet,
30 InvalidCiphertext,
31 AuthenticationFailed,
32 RandomGenerationFailed,
33 BackendInitializationFailed,
34 BackendConfigurationFailed,
35 BackendOperationFailed,
36 InternalCapacityExceeded,
37 UnexpectedOutputSize,
38 InvalidBlockInputSize,
39};
40
42enum class CryptographyErrorDetail : uint16_t
43{
44 None = 0,
45 FillRandom,
46 InitializeAead,
47 SealAead,
48 OpenAead,
49 StartCipher,
50 UpdateCipher,
51 FinishCipher,
52 SetHmacType,
53 SetHmacKey,
54 AddHmacData,
55 FinalizeHmac,
56 DeriveHkdf,
57 ValidateAeadKey,
58 ValidateAeadNonce,
59 ValidateAeadTag,
60 ValidateAeadMessageSize,
61 ValidateAeadOutput,
62 ValidateAeadPartialOverlap,
63 ValidateAeadSealCiphertextOverlap,
64 ValidateAeadSealTagOverlap,
65 ValidateAeadOpenPlaintextOverlap,
66 ValidateCipherKey,
67 ValidateCipherInitializationVector,
68 ValidateCipherOutput,
69 ValidateCipherOverlap,
70 ValidateCipherBlockInput,
71 ValidateCipherFinalBlock,
72 ValidateCipherPadding,
73 ValidateHkdfOutput,
74 AppleCommonCryptoRandomGenerate,
75 AppleCommonCryptoAeadCreate,
76 AppleCommonCryptoAeadEncrypt,
77 AppleCommonCryptoCipherCreate,
78 AppleCommonCryptoCipherUpdate,
79 WindowsBCryptRandomGenerate,
80 WindowsBCryptAeadOpenAlgorithm,
81 WindowsBCryptAeadSetGcmMode,
82 WindowsBCryptAeadQueryObjectLength,
83 WindowsBCryptAeadKeyObjectCapacity,
84 WindowsBCryptAeadGenerateKey,
85 WindowsBCryptAeadEncrypt,
86 WindowsBCryptAeadDecrypt,
87 WindowsBCryptCipherOpenAlgorithm,
88 WindowsBCryptCipherSetCbcMode,
89 WindowsBCryptCipherQueryObjectLength,
90 WindowsBCryptCipherKeyObjectCapacity,
91 WindowsBCryptCipherGenerateKey,
92 WindowsBCryptCipherEncrypt,
93 WindowsBCryptCipherDecrypt,
94 WindowsBCryptHmacOpenAlgorithm,
95 WindowsBCryptHmacQueryObjectLength,
96 WindowsBCryptHmacObjectCapacity,
97 WindowsBCryptHmacCreate,
98 WindowsBCryptHmacUpdate,
99 WindowsBCryptHmacFinalize,
100 LinuxGetRandom,
101 LinuxAFAlgCreateAlgorithmSocket,
102 LinuxAFAlgBindAlgorithmSocket,
103 LinuxAFAlgAcceptOperationSocket,
104 LinuxAFAlgSetKey,
105 LinuxAFAlgSetAeadAuthenticationSize,
106 LinuxAFAlgAeadSealSend,
107 LinuxAFAlgAeadSealReceive,
108 LinuxAFAlgAeadOpenSend,
109 LinuxAFAlgAeadOpenReceive,
110 LinuxAFAlgCipherSend,
111 LinuxAFAlgCipherReceive,
112 LinuxAFAlgHmacSend,
113 LinuxAFAlgHmacReceive,
114 OpenSSL3RuntimeUnavailable,
115 OpenSSL3AeadFetchCipher,
116 OpenSSL3AeadCreateContext,
117 OpenSSL3AeadInitialize,
118 OpenSSL3AeadAssociatedDataUpdate,
119 OpenSSL3AeadPayloadUpdate,
120 OpenSSL3AeadSealFinalize,
121 OpenSSL3AeadGetAuthenticationTag,
122 OpenSSL3AeadSetAuthenticationTag,
123 OpenSSL3AeadOpenFinalize,
124 OpenSSL3CipherFetch,
125 OpenSSL3CipherCreateContext,
126 OpenSSL3CipherInitialize,
127 OpenSSL3CipherSetPadding,
128 OpenSSL3CipherUpdate,
129 OpenSSL3HmacFetch,
130 OpenSSL3HmacCreateContext,
131 OpenSSL3HmacInitialize,
132 OpenSSL3HmacUpdate,
133 OpenSSL3HmacFinalize,
134};
135
137enum class CryptographyErrorContextKind : uint16_t
138{
139 None = 0,
140 AppleCommonCryptoStatus,
141 WindowsNtStatus,
142 PosixErrno,
143 ExpectedBytes,
144 RequiredBytes,
145 MaximumBytes,
146 ActualBytes,
147};
148
151{
153 {
154 };
156 {
157 };
159 {
160 };
162 {
163 };
165 {
166 };
168 {
169 };
170
171 int32_t appleCommonCryptoStatus;
172 uint32_t windowsNtStatus;
173 int32_t posixErrno;
174 uint32_t expectedBytes;
175 uint32_t requiredBytes;
176 uint32_t maximumBytes;
177 uint32_t actualBytes;
178
179 constexpr CryptographyErrorContext(uint32_t value = 0) : windowsNtStatus(value) {}
180 constexpr CryptographyErrorContext(AppleCommonCryptoStatus, int32_t value) : appleCommonCryptoStatus(value) {}
181 constexpr CryptographyErrorContext(PosixErrno, int32_t value) : posixErrno(value) {}
182 constexpr CryptographyErrorContext(ExpectedBytes, uint32_t value) : expectedBytes(value) {}
183 constexpr CryptographyErrorContext(RequiredBytes, uint32_t value) : requiredBytes(value) {}
184 constexpr CryptographyErrorContext(MaximumBytes, uint32_t value) : maximumBytes(value) {}
185 constexpr CryptographyErrorContext(ActualBytes, uint32_t value) : actualBytes(value) {}
186};
187
189static constexpr ResultCategory CryptographyResultCategory = ResultCategory(9);
190
194struct [[nodiscard]] ResultCryptography
195{
196 Result result;
197 CryptographyErrorDetail detail = CryptographyErrorDetail::None;
198 CryptographyErrorContextKind contextKind = CryptographyErrorContextKind::None;
199 CryptographyErrorContext context = {};
200
201 constexpr ResultCryptography() : result(true) {}
202 explicit constexpr ResultCryptography(bool valid) : result(valid) {}
204 CryptographyErrorDetail detail = CryptographyErrorDetail::None)
205 : result(Result::Error(CryptographyResultCategory, error)), detail(detail)
206 {}
209 : result(Result::Error(CryptographyResultCategory, error)), detail(detail), contextKind(contextKind),
210 context(context)
211 {}
212 constexpr ResultCryptography(Result result) : result(result) {}
213
214 template <typename ResultLike>
215 constexpr ResultCryptography(const ResultLike& other) : result(other.toResult())
216 {}
217
218 static constexpr ResultCryptography withAppleCommonCryptoStatus(CryptographyError error,
219 CryptographyErrorDetail detail, int32_t status)
220 {
221 return {error, detail, CryptographyErrorContextKind::AppleCommonCryptoStatus,
223 }
224
225 static constexpr ResultCryptography withWindowsNtStatus(CryptographyError error, CryptographyErrorDetail detail,
226 uint32_t status)
227 {
228 return {error, detail, CryptographyErrorContextKind::WindowsNtStatus, CryptographyErrorContext(status)};
229 }
230
231 static constexpr ResultCryptography withPosixErrno(CryptographyError error, CryptographyErrorDetail detail,
232 int32_t errorNumber)
233 {
234 return {error, detail, CryptographyErrorContextKind::PosixErrno,
236 }
237
238 static constexpr ResultCryptography withExpectedBytes(CryptographyError error, CryptographyErrorDetail detail,
239 uint32_t expectedBytes)
240 {
241 return {error, detail, CryptographyErrorContextKind::ExpectedBytes,
243 }
244
245 static constexpr ResultCryptography withRequiredBytes(CryptographyError error, CryptographyErrorDetail detail,
246 uint32_t requiredBytes)
247 {
248 return {error, detail, CryptographyErrorContextKind::RequiredBytes,
250 }
251
252 static constexpr ResultCryptography withMaximumBytes(CryptographyError error, CryptographyErrorDetail detail,
253 uint32_t maximumBytes)
254 {
255 return {error, detail, CryptographyErrorContextKind::MaximumBytes,
257 }
258
259 static constexpr ResultCryptography withActualBytes(CryptographyError error, CryptographyErrorDetail detail,
260 uint32_t actualBytes)
261 {
262 return {error, detail, CryptographyErrorContextKind::ActualBytes,
264 }
265
266 explicit constexpr operator bool() const { return static_cast<bool>(result); }
267 constexpr operator Result() const { return result; }
268 constexpr Result toResult() const { return result; }
269 constexpr bool isError(CryptographyError error) const { return result.isError(CryptographyResultCategory, error); }
270};
271
273} // namespace SC
CryptographyErrorDetail
Stable operation or backend stage retained for a Cryptography failure.
Definition CryptographyError.h:43
CryptographyErrorContextKind
Kind of the scalar diagnostic context stored in ResultCryptography.
Definition CryptographyError.h:138
CryptographyError
Stable portable failures returned by the Cryptography library.
Definition CryptographyError.h:14
Definition CryptographyError.h:168
Definition CryptographyError.h:159
Definition CryptographyError.h:165
Definition CryptographyError.h:156
Definition CryptographyError.h:162
Cryptography result retaining a stable operation detail and one scalar diagnostic context.
Definition CryptographyError.h:195
A typed scalar payload for a Cryptography error.
Definition CryptographyError.h:151