Solaris Software
Loading...
Searching...
No Matches
encryption.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3 Name : encryption.h
4 Author : polfosol
5 Version : 12
6 Copyright : copyright © 2022 - polfosol
7 Description : μAES ™ is a minimalist all-in-one library for AES encryption
8 Modified : Renamed file and public API symbols to SPP_EXTERNAL_ENCRYPTION_*
9 ==============================================================================
10 */
11
12#ifndef SPP_EXTERNAL_ENCRYPTION_H_
13#define SPP_EXTERNAL_ENCRYPTION_H_
14
15/**----------------------------------------------------------------------------
16You can use different AES algorithms by changing this macro. Default is AES-128
17 -----------------------------------------------------------------------------*/
18#define AES___ 128 /* or 256 (or 192; not standardized in some modes) */
19
20/**----------------------------------------------------------------------------
21AES block-cipher modes of operation. The following modes can be enabled/disabled
22 by setting their corresponding macros to TRUE (1) or FALSE (0).
23 -----------------------------------------------------------------------------*/
24#define BLOCKCIPHERS 1
25#define AEAD_MODES 1 /* authenticated encryption with associated data. */
26
27#if BLOCKCIPHERS
28#define ECB 1 /* electronic code-book (NIST SP 800-38A) */
29#define CBC 1 /* cipher block chaining (NIST SP 800-38A) */
30#define CFB 1 /* cipher feedback (NIST SP 800-38A) */
31#define OFB 1 /* output feedback (NIST SP 800-38A) */
32#define CTR 1 /* counter-block (NIST SP 800-38A) */
33#define XEX 1 /* xor-encrypt-xor (NIST SP 800-38E) */
34#define KWA 1 /* key wrap with authentication (NIST SP 800-38F) */
35#define FPE 1 /* format-preserving encryption (NIST SP 800-38G) */
36#endif
37
38#if AEAD_MODES
39#define CMAC 1 /* message authentication code (NIST SP 800-38B) */
40
41#if CTR
42#define CCM 1 /* counter with CBC-MAC (RFC-3610/NIST SP 800-38C) */
43#define GCM 1 /* Galois/counter mode with GMAC (NIST SP 800-38D) */
44#define EAX 1 /* encrypt-authenticate-translate (ANSI C12.22) */
45#define SIV 1 /* synthetic initialization vector (RFC-5297) */
46#define GCM_SIV 1 /* nonce misuse-resistant AES-GCM (RFC-8452) */
47#endif
48
49#if XEX
50#define OCB 1 /* offset codebook mode with PMAC (RFC-7253) */
51#endif
52
53#define POLY1305 1 /* poly1305-AES mac (https://cr.yp.to/mac.html) */
54#endif
55
56#if CBC
57#define CTS 1 /* ciphertext stealing (CS3: unconditional swap) */
58#endif
59
60#if XEX
61#define XTS 1 /* XEX tweaked-codebook with ciphertext stealing */
62#endif
63
64#if CTR
65#define CTR_NA 1 /* pure counter mode, with no authentication */
66#endif
67
68#if EAX
69#define EAXP 0 /* EAX-prime, as specified by IEEE Std 1703 */
70#endif
71
72#define WTF ! (BLOCKCIPHERS | AEAD_MODES)
73#define MICRO_RJNDL WTF /* none of above; just rijndael API. dude.., why? */
74
75/**----------------------------------------------------------------------------
76Parameters and MACROs; refer to the bottom of this document for more info:
77 -----------------------------------------------------------------------------*/
78
79#if ECB | (CBC && !CTS) | (XEX && !XTS)
80#define AES_PADDING 0 /* standard values: (1) PKCS#7 (2) ISO/IEC7816-4 */
81#endif
82
83#if ECB | CBC | XEX | KWA | MICRO_RJNDL
84#define DECRYPTION 1 /* rijndael decryption is NOT required otherwise. */
85#endif
86
87#if FPE
88#define CUSTOM_ALPHABET 0 /* if disabled, use default alphabet (digits 0..9) */
89#define FF_X 1 /* algorithm type: (1) for FF1, or (3) for FF3-1 */
90#endif
91
93{
94#if FF_X == 3
95 FF3_TWEAK_LEN = 7, /* fixed for FF3-1. (LEN=8 in old version of FF3) */
96#endif
97
98#if CTR_NA
99 CTR_START_VALUE = 1, /* Recommended by the RFC-3686 */
100 CTR_IV_LENGTH = 12, /* use the last 32 bits of block for counting */
101#define PRESET_COUNTER 0 /* enable this macro if CTR_IV_LENGTH == 16 */
102#endif
103
104#if CCM
105 CCM_NONCE_LEN = 11, /* for 32-bit count (since one byte is reserved) */
106 CCM_TAG_LEN = 16, /* must be EVEN. at most 16 bytes in all modes */
107#endif
108#if GCM
109 GCM_NONCE_LEN = 12, /* Recommended; but other values are supported. */
111#endif
112#if GCM_SIV
113 SIVGCM_NONCE_LEN = 12, /* the only valid value recommended by RFC-8452 */
114 SIVGCM_TAG_LEN = 16, /* the only valid value recommended by RFC-8452 */
115#endif
116#if OCB
117 OCB_NONCE_LEN = 12, /* Recommended; must be positive and less than 16 */
119#endif
120#if EAX && !EAXP
121 EAX_NONCE_LEN = 16, /* no specified limit; can be arbitrarily large */
122 EAX_TAG_LEN = 16, /* more info at the bottom of this document. */
123#endif
124
125#if AES___ == 256 || AES___ == 192
127#else
128 AES_KEYLENGTH = 16 /* default value (or fallback for invalid AES___) */
129#endif
130};
131
132/**----------------------------------------------------------------------------
133Since <stdint.h> is not a part of ANSI-C, we may need a 'trick' to use uint8_t
134 -----------------------------------------------------------------------------*/
135#include <string.h>
136#include <limits.h>
137#ifdef LLONG_MAX /* which means compiler conforms to C99 standard. */
138#include <stdint.h>
139#elif CHAR_BIT == 8
140typedef unsigned char uint8_t;
141#if INT_MAX > 200000L
142typedef int int32_t;
143#else
144typedef long int32_t;
145#endif
146#else
147#error "YOUR SYSTEM/COMPILER NEITHER SUPPORTS <cstdint> NOR 8-BIT CHARACTERS!!"
148#endif
149
150#ifdef __SDCC /* compiler is SDCC (small-device C compiler) */
151#define SDCC_REENT __reentrant
152#else
153#define SDCC_REENT
154#endif
155
156#ifdef __cplusplus
157extern "C" {
158#endif
159
160/**----------------------------------------------------------------------------
161Encryption/decryption of a single block with Rijndael
162 -----------------------------------------------------------------------------*/
163#if MICRO_RJNDL
164void SPP_EXTERNAL_ENCRYPTION_cipher( const uint8_t* key, /* encryption/decryption key */
165 const char mode, /* encrypt: 'E', decrypt: 'D' */
166 const uint8_t x[16], /* input array (or input block) */
167 uint8_t y[16] ); /* output block */
168#endif
169
170/**----------------------------------------------------------------------------
171Main functions for ECB-AES block ciphering
172 -----------------------------------------------------------------------------*/
173#if ECB
174void SPP_EXTERNAL_ENCRYPTION_ecbEncrypt( const uint8_t* key, /* encryption key */
175 const void* pntxt, /* plaintext input */
176 const size_t ptextLen, /* length of plaintext */
177 void* crtxt ); /* ciphertext result */
178
179char SPP_EXTERNAL_ENCRYPTION_ecbDecrypt( const uint8_t* key, /* decryption key */
180 const void* crtxt, /* ciphertext input */
181 const size_t crtxtLen, /* length of ciphertext */
182 void* pntxt ); /* plaintext result */
183#endif /* ECB */
184
185/**----------------------------------------------------------------------------
186Main functions for CBC-AES block ciphering
187 -----------------------------------------------------------------------------*/
188#if CBC
189char SPP_EXTERNAL_ENCRYPTION_cbcEncrypt( const uint8_t* key, /* encryption key */
190 const uint8_t iVec[16], /* initialization vector */
191 const void* pntxt, /* plaintext input */
192 const size_t ptextLen, /* length of plaintext */
193 void* crtxt ); /* ciphertext result */
194
195char SPP_EXTERNAL_ENCRYPTION_cbcDecrypt( const uint8_t* key, /* decryption key */
196 const uint8_t iVec[16], /* initialization vector */
197 const void* crtxt, /* ciphertext input */
198 const size_t crtxtLen, /* length of ciphertext */
199 void* pntxt ); /* plaintext result */
200#endif /* CBC */
201
202/**----------------------------------------------------------------------------
203Main functions for CFB-AES block ciphering
204 -----------------------------------------------------------------------------*/
205#if CFB
206void SPP_EXTERNAL_ENCRYPTION_cfbEncrypt( const uint8_t* key, /* encryption key */
207 const uint8_t iVec[16], /* initialization vector */
208 const void* pntxt, /* plaintext input */
209 const size_t ptextLen, /* length of plaintext */
210 void* crtxt ); /* ciphertext result */
211
212void SPP_EXTERNAL_ENCRYPTION_cfbDecrypt( const uint8_t* key, /* decryption key */
213 const uint8_t iVec[16], /* initialization vector */
214 const void* crtxt, /* ciphertext input */
215 const size_t crtxtLen, /* length of ciphertext */
216 void* pntxt ); /* plaintext result */
217#endif /* CFB */
218
219/**----------------------------------------------------------------------------
220Main functions for OFB-AES block ciphering
221 -----------------------------------------------------------------------------*/
222#if OFB
223void SPP_EXTERNAL_ENCRYPTION_ofbEncrypt( const uint8_t* key, /* encryption key */
224 const uint8_t iVec[16], /* initialization vector */
225 const void* pntxt, /* plaintext input */
226 const size_t ptextLen, /* length of plaintext */
227 void* crtxt ); /* ciphertext result */
228
229void SPP_EXTERNAL_ENCRYPTION_ofbDecrypt( const uint8_t* key, /* decryption key */
230 const uint8_t iVec[16], /* initialization vector */
231 const void* crtxt, /* ciphertext input */
232 const size_t crtxtLen, /* length of ciphertext */
233 void* pntxt ); /* plaintext result */
234#endif /* OFB */
235
236/**----------------------------------------------------------------------------
237Main functions for XTS-AES block ciphering
238 -----------------------------------------------------------------------------*/
239#if XTS
240char SPP_EXTERNAL_ENCRYPTION_xtsEncrypt( const uint8_t* keys, /* encryption key pair */
241 const uint8_t* tweak, /* tweak value (unit/sector ID) */
242 const void* pntxt, /* plaintext input */
243 const size_t ptextLen, /* length of plaintext */
244 void* crtxt ); /* ciphertext result */
245
246char SPP_EXTERNAL_ENCRYPTION_xtsDecrypt( const uint8_t* keys, /* decryption key pair */
247 const uint8_t* tweak, /* tweak value (unit/sector ID) */
248 const void* crtxt, /* ciphertext input */
249 const size_t crtxtLen, /* length of ciphertext */
250 void* pntxt ); /* plaintext result */
251#endif /* XTS */
252
253/**----------------------------------------------------------------------------
254Main functions for CTR-AES block ciphering
255 -----------------------------------------------------------------------------*/
256#if CTR_NA
257void SPP_EXTERNAL_ENCRYPTION_ctrEncrypt( const uint8_t* key, /* encryption key */
258 const uint8_t* iv, /* initialization vector/ nonce */
259 const void* pntxt, /* plaintext input */
260 const size_t ptextLen, /* length of plaintext */
261 void* crtxt ); /* ciphertext result */
262
263void SPP_EXTERNAL_ENCRYPTION_ctrDecrypt( const uint8_t* key, /* decryption key */
264 const uint8_t* iv, /* initialization vector/ nonce */
265 const void* crtxt, /* ciphertext input */
266 const size_t crtxtLen, /* length of ciphertext */
267 void* pntxt ); /* plaintext result */
268#endif /* CTR */
269
270/**----------------------------------------------------------------------------
271Main functions for SIV-AES block ciphering
272 -----------------------------------------------------------------------------*/
273#if SIV
274void SPP_EXTERNAL_ENCRYPTION_sivEncrypt( const uint8_t* keys, /* encryption key pair */
275 const void* aData, /* added authentication data */
276 const size_t aDataLen, /* length of AAD (auth. data) */
277 const void* pntxt, /* plaintext input */
278 const size_t ptextLen, /* length of plaintext */
279 uint8_t iv[16], /* synthesized initial-vector */
280 void* crtxt ); /* ciphertext result */
281
282char SPP_EXTERNAL_ENCRYPTION_sivDecrypt( const uint8_t* keys, /* decryption key pair */
283 const uint8_t iv[16], /* provided initial-vector */
284 const void* aData, /* added authentication data */
285 const size_t aDataLen, /* length of AAD (auth. data) */
286 const void* crtxt, /* ciphertext input */
287 const size_t crtxtLen, /* length of ciphertext */
288 void* pntxt ); /* plaintext result */
289#endif /* SIV */
290
291/**----------------------------------------------------------------------------
292Main functions for GCM-AES block ciphering
293 -----------------------------------------------------------------------------*/
294#if GCM
295void SPP_EXTERNAL_ENCRYPTION_gcmEncrypt( const uint8_t* key, /* encryption key */
296 const uint8_t* nonce, /* nonce or IV (init. vector) */
297 const void* aData, /* added authentication data */
298 const size_t aDataLen, /* length of AAD (auth. data) */
299 const void* pntxt, /* plaintext input */
300 const size_t ptextLen, /* length of plaintext */
301 void* crtxt ); /* ciphertext + auth. tag */
302
303char SPP_EXTERNAL_ENCRYPTION_gcmDecrypt( const uint8_t* key, /* decryption key */
304 const uint8_t* nonce, /* nonce or IV (init. vector) */
305 const void* aData, /* added authentication data */
306 const size_t aDataLen, /* length of AAD (auth. data) */
307 const void* crtxt, /* ciphertext + appended tag */
308 const size_t crtxtLen, /* length of ciphertext */
309 void* pntxt ); /* plaintext result */
310#endif /* GCM */
311
312/**----------------------------------------------------------------------------
313Main functions for CCM-AES block ciphering
314 -----------------------------------------------------------------------------*/
315#if CCM
316void SPP_EXTERNAL_ENCRYPTION_ccmEncrypt( const uint8_t* key, /* encryption key */
317 const uint8_t* nonce, /* nonce or IV (init. vector) */
318 const void* aData, /* added authentication data */
319 const size_t aDataLen, /* length of AAD (auth. data) */
320 const void* pntxt, /* plaintext input */
321 const size_t ptextLen, /* length of plaintext */
322 void* crtxt ); /* ciphertext + auth. tag */
323
324char SPP_EXTERNAL_ENCRYPTION_ccmDecrypt( const uint8_t* key, /* decryption key */
325 const uint8_t* nonce, /* nonce or IV (init. vector) */
326 const void* aData, /* added authentication data */
327 const size_t aDataLen, /* length of AAD (auth. data) */
328 const void* crtxt, /* ciphertext + appended tag */
329 const size_t crtxtLen, /* length of ciphertext */
330 void* pntxt ); /* plaintext result */
331#endif /* CCM */
332
333/**----------------------------------------------------------------------------
334Main functions for OCB-AES block ciphering
335 -----------------------------------------------------------------------------*/
336#if OCB
337void SPP_EXTERNAL_ENCRYPTION_ocbEncrypt( const uint8_t* key, /* encryption key */
338 const uint8_t* nonce, /* nonce or IV (init. vector) */
339 const void* aData, /* added authentication data */
340 const size_t aDataLen, /* length of AAD (auth. data) */
341 const void* pntxt, /* plaintext input */
342 const size_t ptextLen, /* length of plaintext */
343 void* crtxt ); /* ciphertext + auth. tag */
344
345char SPP_EXTERNAL_ENCRYPTION_ocbDecrypt( const uint8_t* key, /* decryption key */
346 const uint8_t* nonce, /* nonce or IV (init. vector) */
347 const void* aData, /* added authentication data */
348 const size_t aDataLen, /* length of AAD (auth. data) */
349 const void* crtxt, /* ciphertext + appended tag */
350 const size_t crtxtLen, /* length of ciphertext */
351 void* pntxt ); /* plaintext result */
352#endif /* OCB */
353
354/**----------------------------------------------------------------------------
355Main functions for EAX-AES mode; more info at the bottom of this document.
356 -----------------------------------------------------------------------------*/
357#if EAX
358void SPP_EXTERNAL_ENCRYPTION_eaxEncrypt( const uint8_t* key, /* encryption key */
359 const uint8_t* nonce, /* arbitrary-size nonce array */
360#if EAXP
361 const size_t nonceLen, /* length of provided nonce */
362#else
363 const void* aData, /* added authentication data */
364 const size_t aDataLen, /* length of AAD (auth. data) */
365#endif
366 const void* pntxt, /* plaintext input */
367 const size_t ptextLen, /* length of plaintext */
368 void* crtxt ); /* ciphertext+ appended tag/mac */
369
370char SPP_EXTERNAL_ENCRYPTION_eaxDecrypt( const uint8_t* key, /* decryption key */
371 const uint8_t* nonce, /* arbitrary-size nonce array */
372#if EAXP
373 const size_t nonceLen, /* length of provided nonce */
374#else
375 const void* aData, /* added authentication data */
376 const size_t aDataLen, /* length of AAD (auth. data) */
377#endif
378 const void* crtxt, /* ciphertext+ appended tag/mac */
379 const size_t crtxtLen, /* length of ciphertext */
380 void* pntxt ); /* plaintext result */
381#endif /* EAX */
382
383/**----------------------------------------------------------------------------
384Main functions for GCM-SIV-AES block ciphering
385 -----------------------------------------------------------------------------*/
386#if GCM_SIV
387void SPP_EXTERNAL_ENCRYPTION_gcmSivEncrypt( const uint8_t* key, /* encryption key */
388 const uint8_t* nonce, /* provided 96-bit nonce */
389 const void* aData, /* added authentication data */
390 const size_t aDataLen, /* length of AAD (auth. data) */
391 const void* pntxt, /* plaintext input */
392 const size_t ptextLen, /* length of plaintext */
393 void* crtxt ); /* ciphertext + auth. tag */
394
395char SPP_EXTERNAL_ENCRYPTION_gcmSivDecrypt( const uint8_t* key, /* decryption key */
396 const uint8_t* nonce, /* provided 96-bit nonce */
397 const void* aData, /* added authentication data */
398 const size_t aDataLen, /* length of AAD (auth. data) */
399 const void* crtxt, /* ciphertext + appended tag */
400 const size_t crtxtLen, /* length of ciphertext */
401 void* pntxt ); /* plaintext result */
402#endif /* GCM-SIV */
403
404/**----------------------------------------------------------------------------
405Main functions for AES key-wrapping
406 -----------------------------------------------------------------------------*/
407#if KWA
408char SPP_EXTERNAL_ENCRYPTION_keyWrap( const uint8_t* kek, /* key encryption key */
409 const void* secret, /* the secret to be wrapped */
410 const size_t secretLen, /* length of plaintext secret */
411 void* wrapped ); /* key-wrapped output */
412
413char SPP_EXTERNAL_ENCRYPTION_keyUnwrap( const uint8_t* kek, /* key encryption key */
414 const void* wrapped, /* key-wrapped secret */
415 const size_t wrapLen, /* length of wrapped secret */
416 void* secret ); /* the unwrapped key result */
417#endif /* KWA */
418
419/**----------------------------------------------------------------------------
420Main functions for FPE-AES; more info at the bottom of this page.
421 -----------------------------------------------------------------------------*/
422#if FPE
423char SPP_EXTERNAL_ENCRYPTION_fpeEncrypt( const uint8_t* key, /* encryption key */
424 const uint8_t* tweak, /* tweak bytes */
425#if FF_X != 3
426 const size_t tweakLen, /* length of tweak array */
427#endif
428 const void* pntxt, /* input plaintext string */
429 const size_t ptextLen, /* length of plaintext string */
430 void* crtxt ); /* ciphertext result */
431
432char SPP_EXTERNAL_ENCRYPTION_fpeDecrypt( const uint8_t* key, /* decryption key */
433 const uint8_t* tweak, /* tweak bytes */
434#if FF_X != 3
435 const size_t tweakLen, /* length of tweak array */
436#endif
437 const void* crtxt, /* input ciphertext string */
438 const size_t crtxtLen, /* length of ciphertext string */
439 void* pntxt ); /* plaintext result */
440#endif /* FPE */
441
442/**----------------------------------------------------------------------------
443Main function for Poly1305-AES message authentication code
444 -----------------------------------------------------------------------------*/
445#if POLY1305
446void SPP_EXTERNAL_ENCRYPTION_poly1305( const uint8_t* keys, /* encryption/mixing key pair */
447 const uint8_t nonce[16], /* the 128-bit nonce */
448 const void* data, /* input data buffer */
449 const size_t dataSize, /* length of data in bytes */
450 uint8_t mac[16] ); /* poly1305-AES mac of data */
451#endif
452
453/**----------------------------------------------------------------------------
454Main function for AES Cipher-based Message Authentication Code
455 -----------------------------------------------------------------------------*/
456#if CMAC
457void SPP_EXTERNAL_ENCRYPTION_cmac( const uint8_t* key, /* encryption or cipher key */
458 const void* data, /* input data buffer */
459 const size_t dataSize, /* length of data in bytes */
460 uint8_t mac[16] ); /* CMAC of input data */
461#endif
462
463#ifdef __cplusplus
464}
465#endif
466
467/**----------------------------------------------------------------------------
468Define some error codes here
469 -----------------------------------------------------------------------------*/
480
481#endif /* header guard */
482
483/*----------------------------------------------------------------------------*\
484¦ Notes and remarks about the above-defined macros ¦
485 ------------------------------------------------------------------------------
486
487* The main difference between the standard AES methods is in their key-expansion
488 process. So for example, AES-128-GCM and AES-256-GCM are pretty much similar
489 except for their key size and a minor change in the KeyExpansion function.
490
491* In EBC/CBC/XEX modes, the size of input must be a multiple of block-size.
492 Otherwise it needs to be padded. The simplest (default) way of padding is to
493 fill the rest of block by zeros. Two standard methods for padding are PKCS#7
494 and ISO/IEC 7816-4, which can be enabled by the AES_PADDING macro.
495
496* The FPE mode has two distinct NIST-approved algorithms, namely FF1 and FF3-1.
497 Use the FF_X macro to change the encryption method, which is FF1 by default.
498 The early version of FF3 required 8-byte tweaks. But this turned out to have
499 vulnerabilities and so it was reduced to 7 bytes in the FF3-1.
500
501 The input and output strings of FPE functions must be consisted of a fixed
502 set of characters called "the alphabet". Here, the default alphabet is the
503 set of digits {'0'...'9'}. If you want to use a different alphabet, set the
504 CUSTOM_ALPHABET macro and refer to the <micro_fpe.h> header. This header is
505 required only when a custom alphabet has to be defined, and contains some
506 illustrative examples and clear guidelines on how to do so.
507
508* Many reference texts may use the terms "nonce" and "initialization vector"
509 interchangeably, but technically they are not the same. Sometimes nonce is
510 a part of the I.V, which itself can either be a full block or a partial one.
511 In CBC, CFB and OFB modes, the provided IV must be a full block. In pure CTR
512 (CTR_NA) mode, the IV can either be a full block, or a 96 bit one —which is
513 also called nonce. In the latter case, counting begins at CTR_START_VALUE.
514
515* In AEAD modes, the size of nonce and tag are considered as constant parameters
516 and changing them might affect the results. The GCM and EAX modes support
517 arbitrary sizes for nonce. In CCM, the nonce length may vary from 7 to 13
518 bytes. Also the tag size is an EVEN number between 4..16. In OCB, the nonce
519 size is 1..15 and the tag is 0..16 bytes. Note that the "calculated" tag-
520 size is always 16 bytes which is then truncated to the desired values. So in
521 encryption functions, the provided buffer for tag must be 16 bytes long.
522
523* In most functions, as you may notice, first the entire input data is copied
524 to the output and then the encryption process is carried out on its buffer.
525 This is a very useful feature especially when the memory is limited, as you
526 can perform "in-place encryption" on the input data and there is no need to
527 allocate a separate buffer for the output. But please note that the `memcpy`
528 function has undefined behavior if its source and destination are the same.
529 So in such cases, you can simply delete the memcpy(...); line.
530
531* For the EAX mode of operation, the IEEE-1703 standard defines EAX' which is a
532 modified version that combines AAD and nonce. Also the tag size is fixed on
533 4 bytes. So EAX-prime functions don't need to take additional authentication
534 data and its size as separate parameters. It has been proven that EAX' has
535 serious vulnerabilities and its usage is not recommended.
536
537* In SIV mode, multiple separate units of authentication headers can be provided
538 for the nonce synthesis. Here we assume that only one unit of AAD (aData) is
539 sufficient, which is practically true.
540
541* The key wrapping mode is also denoted by KW. In this mode, the input secret is
542 divided into 64bit blocks. Number of blocks is at least 2, and it is assumed
543 that no padding is required. For padding, the KWP mode must be used which is
544 easily implementable but left as an exercise! In the NIST SP800-38F document
545 you may find mentions of TKW which is based on 3DES and irrelevant here.
546
547* Here is a technical tip for the keen minds who have managed to read this far:
548 Excessive use of macro definitions in code is generally not a good practice,
549 especially in large projects. As it may possibly cause some name-conflicts
550 and errors for macro redefinition. So you can either delete the unnecessary
551 macros and clean up the code, or undef macros at the end of the source file
552 (i.e. write: #ifdef MACRO \ #undef MACRO \ #endif). Or replace their names
553 with some unique ones that surely won't be used elsewhere. For example,
554 rename the CTR macro above to MICRO_AES_CTR_MODE or something like that.
555
556* Let me explain three extra options that are defined in the source file. If the
557 length of the input cipher/plain text is 'always' less than 4KB, you can
558 enable the SMALL_CIPHER macro to save a few bytes in the compiled code. This
559 assumption is likely to be valid for some embedded systems and small-scale
560 applications. Furthermore, enabling the AVOID_FUNCTION_CALL macro may have a
561 positive effect on the speed while increasing the size of compiled code.
562 Nonetheless, it is also possible to get different results sometimes.
563
564 The INCREASE_SECURITY macro —as its name suggests, is dealing with security
565 considerations. For example, since the RoundKey is declared as static array
566 it might get exposed to some attacks. By enabling this macro, round-keys are
567 wiped out at the end of ciphering operations. However, please keep in mind
568 that this is NOT A GUARANTEE against side-channel attacks.
569
570*/
void SPP_EXTERNAL_ENCRYPTION_ctrDecrypt(const uint8_t *key, const uint8_t *iv, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using CTR-AES block-cipher method
Definition encryption.c:989
void SPP_EXTERNAL_ENCRYPTION_ecbEncrypt(const uint8_t *key, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using ECB-AES block-cipher method
Definition encryption.c:639
char SPP_EXTERNAL_ENCRYPTION_cbcEncrypt(const uint8_t *key, const block_t iVec, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using CBC-AES block-cipher method
Definition encryption.c:700
void SPP_EXTERNAL_ENCRYPTION_ctrEncrypt(const uint8_t *key, const uint8_t *iv, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using CTR-AES block-cipher method
Definition encryption.c:965
void SPP_EXTERNAL_ENCRYPTION_poly1305(const uint8_t *keys, const block_t nonce, const void *data, const size_t dataSize, block_t mac)
derive the Poly1305-AES mac of message using a nonce and key pair.
void SPP_EXTERNAL_ENCRYPTION_ofbDecrypt(const uint8_t *key, const block_t iVec, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using OFB-AES block-cipher method
Definition encryption.c:892
char SPP_EXTERNAL_ENCRYPTION_gcmSivDecrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using SIV-GCM-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_cfbEncrypt(const uint8_t *key, const block_t iVec, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using CFB-AES block-cipher method
Definition encryption.c:830
char SPP_EXTERNAL_ENCRYPTION_keyWrap(const uint8_t *kek, const void *secret, const size_t secretLen, void *wrapped)
encrypt the input secret with key-wrapping method
void SPP_EXTERNAL_ENCRYPTION_ofbEncrypt(const uint8_t *key, const block_t iVec, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using OFB-AES block-cipher method
Definition encryption.c:864
char SPP_EXTERNAL_ENCRYPTION_keyUnwrap(const uint8_t *kek, const void *wrapped, const size_t wrapLen, void *secret)
unwrap/decrypt a wrapped secret according to RFC-3394
void SPP_EXTERNAL_ENCRYPTION_gcmEncrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using GCM-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_ocbEncrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input stream using OCB-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_sivEncrypt(const uint8_t *keys, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, block_t iv, void *crtxt)
encrypt the input plaintext using SIV-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_cbcDecrypt(const uint8_t *key, const block_t iVec, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using CBC-AES block-cipher method
Definition encryption.c:749
char SPP_EXTERNAL_ENCRYPTION_eaxDecrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using EAX-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_cfbDecrypt(const uint8_t *key, const block_t iVec, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using CFB-AES block-cipher method
Definition encryption.c:844
char SPP_EXTERNAL_ENCRYPTION_sivDecrypt(const uint8_t *keys, const block_t iv, const void *aData, const size_t aDataLen, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using SIV-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_gcmSivEncrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using SIV-GCM-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_ccmEncrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using CCM-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_gcmDecrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using GCM-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_xtsEncrypt(const uint8_t *keys, const uint8_t *tweak, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using XTS-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_fpeDecrypt(const uint8_t *key, const TWEAK_PARAMS, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt a ciphertext string using FPE-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_ocbDecrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input stream using OCB-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_xtsDecrypt(const uint8_t *keys, const uint8_t *tweak, const void *crtxt, const size_t crtxtLen, void *pntxt)
encrypt the input ciphertext using XTS-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_ecbDecrypt(const uint8_t *key, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using ECB-AES block-cipher method
Definition encryption.c:666
void SPP_EXTERNAL_ENCRYPTION_eaxEncrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using EAX-AES block-cipher method
void SPP_EXTERNAL_ENCRYPTION_cmac(const uint8_t *key, const void *data, const size_t dataSize, block_t mac)
derive the AES-CMAC of input data using an encryption key
char SPP_EXTERNAL_ENCRYPTION_ccmDecrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *crtxt, const size_t crtxtLen, void *pntxt)
decrypt the input ciphertext using CCM-AES block-cipher method
char SPP_EXTERNAL_ENCRYPTION_fpeEncrypt(const uint8_t *key, const TWEAK_PARAMS, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input string using FPE-AES block-cipher method
#define AES___
Definition encryption.h:18
#define FF_X
Definition encryption.h:89
constant_parameters_of_modes
Definition encryption.h:93
@ SIVGCM_NONCE_LEN
Definition encryption.h:113
@ SIVGCM_TAG_LEN
Definition encryption.h:114
@ CTR_IV_LENGTH
Definition encryption.h:100
@ EAX_NONCE_LEN
Definition encryption.h:121
@ AES_KEYLENGTH
Definition encryption.h:128
@ GCM_TAG_LEN
Definition encryption.h:110
@ CCM_NONCE_LEN
Definition encryption.h:105
@ OCB_NONCE_LEN
Definition encryption.h:117
@ EAX_TAG_LEN
Definition encryption.h:122
@ OCB_TAG_LEN
Definition encryption.h:118
@ CCM_TAG_LEN
Definition encryption.h:106
@ CTR_START_VALUE
Definition encryption.h:99
@ GCM_NONCE_LEN
Definition encryption.h:109
#define EAXP
Definition encryption.h:69
function_result_codes
Definition encryption.h:471
@ M_DATAFORMAT_ERROR
Definition encryption.h:476
@ M_AUTHENTICATION_ERROR
Definition encryption.h:474
@ M_MEMORY_ERROR
Definition encryption.h:477
@ M_ENCRYPTION_ERROR
Definition encryption.h:472
@ M_DATALENGTH_ERROR
Definition encryption.h:475
@ M_SUCCESS
Definition encryption.h:478
@ M_DECRYPTION_ERROR
Definition encryption.h:473