crypto.h
Go to the documentation of this file.
1 /**
2  * @file crypto.h
3  * @brief General definitions for cryptographic algorithms
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _CRYPTO_H
30 #define _CRYPTO_H
31 
32 //Dependencies
33 #include "os_port.h"
34 #include "crypto_config.h"
35 #include "crypto_legacy.h"
36 #include "cpu_endian.h"
37 #include "error.h"
38 
39 
40 /*
41  * CycloneCrypto Open is licensed under GPL version 2. In particular:
42  *
43  * - If you link your program to CycloneCrypto Open, the result is a derivative
44  * work that can only be distributed under the same GPL license terms.
45  *
46  * - If additions or changes to CycloneCrypto Open are made, the result is a
47  * derivative work that can only be distributed under the same license terms.
48  *
49  * - The GPL license requires that you make the source code available to
50  * whoever you make the binary available to.
51  *
52  * - If you sell or distribute a hardware product that runs CycloneCrypto Open,
53  * the GPL license requires you to provide public and full access to all
54  * source code on a nondiscriminatory basis.
55  *
56  * If you fully understand and accept the terms of the GPL license, then edit
57  * the os_port_config.h header and add the following directive:
58  *
59  * #define GPL_LICENSE_TERMS_ACCEPTED
60  */
61 
62 #ifndef GPL_LICENSE_TERMS_ACCEPTED
63  #error Before compiling CycloneCrypto Open, you must accept the terms of the GPL license
64 #endif
65 
66 
67 //Multiple precision integer support
68 #ifndef MPI_SUPPORT
69  #define MPI_SUPPORT ENABLED
70 #elif (MPI_SUPPORT != ENABLED && MPI_SUPPORT != DISABLED)
71  #error MPI_SUPPORT parameter is not valid
72 #endif
73 
74 //Assembly optimizations for time-critical routines
75 #ifndef MPI_ASM_SUPPORT
76  #define MPI_ASM_SUPPORT DISABLED
77 #elif (MPI_ASM_SUPPORT != ENABLED && MPI_ASM_SUPPORT != DISABLED)
78  #error MPI_ASM_SUPPORT parameter is not valid
79 #endif
80 
81 //Base64 encoding support
82 #ifndef BASE64_SUPPORT
83  #define BASE64_SUPPORT ENABLED
84 #elif (BASE64_SUPPORT != ENABLED && BASE64_SUPPORT != DISABLED)
85  #error BASE64_SUPPORT parameter is not valid
86 #endif
87 
88 //MD2 hash support
89 #ifndef MD2_SUPPORT
90  #define MD2_SUPPORT ENABLED
91 #elif (MD2_SUPPORT != ENABLED && MD2_SUPPORT != DISABLED)
92  #error MD2_SUPPORT parameter is not valid
93 #endif
94 
95 //MD4 hash support
96 #ifndef MD4_SUPPORT
97  #define MD4_SUPPORT ENABLED
98 #elif (MD4_SUPPORT != ENABLED && MD4_SUPPORT != DISABLED)
99  #error MD4_SUPPORT parameter is not valid
100 #endif
101 
102 //MD5 hash support
103 #ifndef MD5_SUPPORT
104  #define MD5_SUPPORT ENABLED
105 #elif (MD5_SUPPORT != ENABLED && MD5_SUPPORT != DISABLED)
106  #error MD5_SUPPORT parameter is not valid
107 #endif
108 
109 //RIPEMD-128 hash support
110 #ifndef RIPEMD128_SUPPORT
111  #define RIPEMD128_SUPPORT ENABLED
112 #elif (RIPEMD128_SUPPORT != ENABLED && RIPEMD128_SUPPORT != DISABLED)
113  #error RIPEMD128_SUPPORT parameter is not valid
114 #endif
115 
116 //RIPEMD-160 hash support
117 #ifndef RIPEMD160_SUPPORT
118  #define RIPEMD160_SUPPORT ENABLED
119 #elif (RIPEMD160_SUPPORT != ENABLED && RIPEMD160_SUPPORT != DISABLED)
120  #error RIPEMD160_SUPPORT parameter is not valid
121 #endif
122 
123 //SHA-1 hash support
124 #ifndef SHA1_SUPPORT
125  #define SHA1_SUPPORT ENABLED
126 #elif (SHA1_SUPPORT != ENABLED && SHA1_SUPPORT != DISABLED)
127  #error SHA1_SUPPORT parameter is not valid
128 #endif
129 
130 //SHA-224 hash support
131 #ifndef SHA224_SUPPORT
132  #define SHA224_SUPPORT ENABLED
133 #elif (SHA224_SUPPORT != ENABLED && SHA224_SUPPORT != DISABLED)
134  #error SHA224_SUPPORT parameter is not valid
135 #endif
136 
137 //SHA-256 hash support
138 #ifndef SHA256_SUPPORT
139  #define SHA256_SUPPORT ENABLED
140 #elif (SHA256_SUPPORT != ENABLED && SHA256_SUPPORT != DISABLED)
141  #error SHA256_SUPPORT parameter is not valid
142 #endif
143 
144 //SHA-384 hash support
145 #ifndef SHA384_SUPPORT
146  #define SHA384_SUPPORT ENABLED
147 #elif (SHA384_SUPPORT != ENABLED && SHA384_SUPPORT != DISABLED)
148  #error SHA384_SUPPORT parameter is not valid
149 #endif
150 
151 //SHA-512 hash support
152 #ifndef SHA512_SUPPORT
153  #define SHA512_SUPPORT ENABLED
154 #elif (SHA512_SUPPORT != ENABLED && SHA512_SUPPORT != DISABLED)
155  #error SHA512_SUPPORT parameter is not valid
156 #endif
157 
158 //SHA-512/224 hash support
159 #ifndef SHA512_224_SUPPORT
160  #define SHA512_224_SUPPORT ENABLED
161 #elif (SHA512_224_SUPPORT != ENABLED && SHA512_224_SUPPORT != DISABLED)
162  #error SHA512_224_SUPPORT parameter is not valid
163 #endif
164 
165 //SHA-512/256 hash support
166 #ifndef SHA512_256_SUPPORT
167  #define SHA512_256_SUPPORT ENABLED
168 #elif (SHA512_256_SUPPORT != ENABLED && SHA512_256_SUPPORT != DISABLED)
169  #error SHA512_256_SUPPORT parameter is not valid
170 #endif
171 
172 //SHA3-224 hash support
173 #ifndef SHA3_224_SUPPORT
174  #define SHA3_224_SUPPORT DISABLED
175 #elif (SHA3_224_SUPPORT != ENABLED && SHA3_224_SUPPORT != DISABLED)
176  #error SHA3_224_SUPPORT parameter is not valid
177 #endif
178 
179 //SHA3-256 hash support
180 #ifndef SHA3_256_SUPPORT
181  #define SHA3_256_SUPPORT DISABLED
182 #elif (SHA3_256_SUPPORT != ENABLED && SHA3_256_SUPPORT != DISABLED)
183  #error SHA3_256_SUPPORT parameter is not valid
184 #endif
185 
186 //SHA3-384 hash support
187 #ifndef SHA3_384_SUPPORT
188  #define SHA3_384_SUPPORT DISABLED
189 #elif (SHA3_384_SUPPORT != ENABLED && SHA3_384_SUPPORT != DISABLED)
190  #error SHA3_384_SUPPORT parameter is not valid
191 #endif
192 
193 //SHA3-512 hash support
194 #ifndef SHA3_512_SUPPORT
195  #define SHA3_512_SUPPORT DISABLED
196 #elif (SHA3_512_SUPPORT != ENABLED && SHA3_512_SUPPORT != DISABLED)
197  #error SHA3_512_SUPPORT parameter is not valid
198 #endif
199 
200 //SHAKE128 support
201 #ifndef SHAKE128_SUPPORT
202  #define SHAKE128_SUPPORT DISABLED
203 #elif (SHAKE128_SUPPORT != ENABLED && SHAKE128_SUPPORT != DISABLED)
204  #error SHAKE128_SUPPORT parameter is not valid
205 #endif
206 
207 //SHAKE256 support
208 #ifndef SHAKE256_SUPPORT
209  #define SHAKE256_SUPPORT DISABLED
210 #elif (SHAKE256_SUPPORT != ENABLED && SHAKE256_SUPPORT != DISABLED)
211  #error SHAKE256_SUPPORT parameter is not valid
212 #endif
213 
214 //Keccak support
215 #ifndef KECCAK_SUPPORT
216  #define KECCAK_SUPPORT DISABLED
217 #elif (KECCAK_SUPPORT != ENABLED && KECCAK_SUPPORT != DISABLED)
218  #error KECCAK_SUPPORT parameter is not valid
219 #endif
220 
221 //BLAKE2b support
222 #ifndef BLAKE2B_SUPPORT
223  #define BLAKE2B_SUPPORT DISABLED
224 #elif (BLAKE2B_SUPPORT != ENABLED && BLAKE2B_SUPPORT != DISABLED)
225  #error BLAKE2B_SUPPORT parameter is not valid
226 #endif
227 
228 //BLAKE2b-160 hash support
229 #ifndef BLAKE2B160_SUPPORT
230  #define BLAKE2B160_SUPPORT DISABLED
231 #elif (BLAKE2B160_SUPPORT != ENABLED && BLAKE2B160_SUPPORT != DISABLED)
232  #error BLAKE2B160_SUPPORT parameter is not valid
233 #endif
234 
235 //BLAKE2b-256 hash support
236 #ifndef BLAKE2B256_SUPPORT
237  #define BLAKE2B256_SUPPORT DISABLED
238 #elif (BLAKE2B256_SUPPORT != ENABLED && BLAKE2B256_SUPPORT != DISABLED)
239  #error BLAKE2B256_SUPPORT parameter is not valid
240 #endif
241 
242 //BLAKE2b-384 hash support
243 #ifndef BLAKE2B384_SUPPORT
244  #define BLAKE2B384_SUPPORT DISABLED
245 #elif (BLAKE2B384_SUPPORT != ENABLED && BLAKE2B384_SUPPORT != DISABLED)
246  #error BLAKE2B384_SUPPORT parameter is not valid
247 #endif
248 
249 //BLAKE2b-512 hash support
250 #ifndef BLAKE2B512_SUPPORT
251  #define BLAKE2B512_SUPPORT DISABLED
252 #elif (BLAKE2B512_SUPPORT != ENABLED && BLAKE2B512_SUPPORT != DISABLED)
253  #error BLAKE2B512_SUPPORT parameter is not valid
254 #endif
255 
256 //BLAKE2s support
257 #ifndef BLAKE2S_SUPPORT
258  #define BLAKE2S_SUPPORT DISABLED
259 #elif (BLAKE2S_SUPPORT != ENABLED && BLAKE2S_SUPPORT != DISABLED)
260  #error BLAKE2S_SUPPORT parameter is not valid
261 #endif
262 
263 //BLAKE2s-128 hash support
264 #ifndef BLAKE2S128_SUPPORT
265  #define BLAKE2S128_SUPPORT DISABLED
266 #elif (BLAKE2S128_SUPPORT != ENABLED && BLAKE2S128_SUPPORT != DISABLED)
267  #error BLAKE2S128_SUPPORT parameter is not valid
268 #endif
269 
270 //BLAKE2s-160 hash support
271 #ifndef BLAKE2S160_SUPPORT
272  #define BLAKE2S160_SUPPORT DISABLED
273 #elif (BLAKE2S160_SUPPORT != ENABLED && BLAKE2S160_SUPPORT != DISABLED)
274  #error BLAKE2S160_SUPPORT parameter is not valid
275 #endif
276 
277 //BLAKE2s-224 hash support
278 #ifndef BLAKE2S224_SUPPORT
279  #define BLAKE2S224_SUPPORT DISABLED
280 #elif (BLAKE2S224_SUPPORT != ENABLED && BLAKE2S224_SUPPORT != DISABLED)
281  #error BLAKE2S224_SUPPORT parameter is not valid
282 #endif
283 
284 //BLAKE2s-256 hash support
285 #ifndef BLAKE2S256_SUPPORT
286  #define BLAKE2S256_SUPPORT DISABLED
287 #elif (BLAKE2S256_SUPPORT != ENABLED && BLAKE2S256_SUPPORT != DISABLED)
288  #error BLAKE2S256_SUPPORT parameter is not valid
289 #endif
290 
291 //Tiger hash support
292 #ifndef TIGER_SUPPORT
293  #define TIGER_SUPPORT ENABLED
294 #elif (TIGER_SUPPORT != ENABLED && TIGER_SUPPORT != DISABLED)
295  #error TIGER_SUPPORT parameter is not valid
296 #endif
297 
298 //Whirlpool hash support
299 #ifndef WHIRLPOOL_SUPPORT
300  #define WHIRLPOOL_SUPPORT ENABLED
301 #elif (WHIRLPOOL_SUPPORT != ENABLED && WHIRLPOOL_SUPPORT != DISABLED)
302  #error WHIRLPOOL_SUPPORT parameter is not valid
303 #endif
304 
305 //CMAC support
306 #ifndef CMAC_SUPPORT
307  #define CMAC_SUPPORT DISABLED
308 #elif (CMAC_SUPPORT != ENABLED && CMAC_SUPPORT != DISABLED)
309  #error CMAC_SUPPORT parameter is not valid
310 #endif
311 
312 //HMAC support
313 #ifndef HMAC_SUPPORT
314  #define HMAC_SUPPORT ENABLED
315 #elif (HMAC_SUPPORT != ENABLED && HMAC_SUPPORT != DISABLED)
316  #error HMAC_SUPPORT parameter is not valid
317 #endif
318 
319 //GMAC support
320 #ifndef GMAC_SUPPORT
321  #define GMAC_SUPPORT DISABLED
322 #elif (GMAC_SUPPORT != ENABLED && GMAC_SUPPORT != DISABLED)
323  #error GMAC_SUPPORT parameter is not valid
324 #endif
325 
326 //RC2 encryption support
327 #ifndef RC2_SUPPORT
328  #define RC2_SUPPORT DISABLED
329 #elif (RC2_SUPPORT != ENABLED && RC2_SUPPORT != DISABLED)
330  #error RC2_SUPPORT parameter is not valid
331 #endif
332 
333 //RC4 encryption support
334 #ifndef RC4_SUPPORT
335  #define RC4_SUPPORT ENABLED
336 #elif (RC4_SUPPORT != ENABLED && RC4_SUPPORT != DISABLED)
337  #error RC4_SUPPORT parameter is not valid
338 #endif
339 
340 //RC6 encryption support
341 #ifndef RC6_SUPPORT
342  #define RC6_SUPPORT ENABLED
343 #elif (RC6_SUPPORT != ENABLED && RC6_SUPPORT != DISABLED)
344  #error RC6_SUPPORT parameter is not valid
345 #endif
346 
347 //IDEA encryption support
348 #ifndef IDEA_SUPPORT
349  #define IDEA_SUPPORT ENABLED
350 #elif (IDEA_SUPPORT != ENABLED && IDEA_SUPPORT != DISABLED)
351  #error IDEA_SUPPORT parameter is not valid
352 #endif
353 
354 //DES encryption support
355 #ifndef DES_SUPPORT
356  #define DES_SUPPORT ENABLED
357 #elif (DES_SUPPORT != ENABLED && DES_SUPPORT != DISABLED)
358  #error DES_SUPPORT parameter is not valid
359 #endif
360 
361 //Triple DES encryption support
362 #ifndef DES3_SUPPORT
363  #define DES3_SUPPORT ENABLED
364 #elif (DES3_SUPPORT != ENABLED && DES3_SUPPORT != DISABLED)
365  #error DES3_SUPPORT parameter is not valid
366 #endif
367 
368 //AES encryption support
369 #ifndef AES_SUPPORT
370  #define AES_SUPPORT ENABLED
371 #elif (AES_SUPPORT != ENABLED && AES_SUPPORT != DISABLED)
372  #error AES_SUPPORT parameter is not valid
373 #endif
374 
375 //Camellia encryption support
376 #ifndef CAMELLIA_SUPPORT
377  #define CAMELLIA_SUPPORT ENABLED
378 #elif (CAMELLIA_SUPPORT != ENABLED && CAMELLIA_SUPPORT != DISABLED)
379  #error CAMELLIA_SUPPORT parameter is not valid
380 #endif
381 
382 //SEED encryption support
383 #ifndef SEED_SUPPORT
384  #define SEED_SUPPORT ENABLED
385 #elif (SEED_SUPPORT != ENABLED && SEED_SUPPORT != DISABLED)
386  #error SEED_SUPPORT parameter is not valid
387 #endif
388 
389 //ARIA encryption support
390 #ifndef ARIA_SUPPORT
391  #define ARIA_SUPPORT ENABLED
392 #elif (ARIA_SUPPORT != ENABLED && ARIA_SUPPORT != DISABLED)
393  #error ARIA_SUPPORT parameter is not valid
394 #endif
395 
396 //PRESENT encryption support
397 #ifndef PRESENT_SUPPORT
398  #define PRESENT_SUPPORT DISABLED
399 #elif (PRESENT_SUPPORT != ENABLED && PRESENT_SUPPORT != DISABLED)
400  #error PRESENT_SUPPORT parameter is not valid
401 #endif
402 
403 //ECB mode support
404 #ifndef ECB_SUPPORT
405  #define ECB_SUPPORT ENABLED
406 #elif (ECB_SUPPORT != ENABLED && ECB_SUPPORT != DISABLED)
407  #error ECB_SUPPORT parameter is not valid
408 #endif
409 
410 //CBC mode support
411 #ifndef CBC_SUPPORT
412  #define CBC_SUPPORT ENABLED
413 #elif (CBC_SUPPORT != ENABLED && CBC_SUPPORT != DISABLED)
414  #error CBC_SUPPORT parameter is not valid
415 #endif
416 
417 //CFB mode support
418 #ifndef CFB_SUPPORT
419  #define CFB_SUPPORT ENABLED
420 #elif (CFB_SUPPORT != ENABLED && CFB_SUPPORT != DISABLED)
421  #error CFB_SUPPORT parameter is not valid
422 #endif
423 
424 //OFB mode support
425 #ifndef OFB_SUPPORT
426  #define OFB_SUPPORT ENABLED
427 #elif (OFB_SUPPORT != ENABLED && OFB_SUPPORT != DISABLED)
428  #error OFB_SUPPORT parameter is not valid
429 #endif
430 
431 //CTR mode support
432 #ifndef CTR_SUPPORT
433  #define CTR_SUPPORT ENABLED
434 #elif (CTR_SUPPORT != ENABLED && CTR_SUPPORT != DISABLED)
435  #error CTR_SUPPORT parameter is not valid
436 #endif
437 
438 //XTS mode support
439 #ifndef XTS_SUPPORT
440  #define XTS_SUPPORT ENABLED
441 #elif (XTS_SUPPORT != ENABLED && XTS_SUPPORT != DISABLED)
442  #error XTS_SUPPORT parameter is not valid
443 #endif
444 
445 //CCM mode support
446 #ifndef CCM_SUPPORT
447  #define CCM_SUPPORT ENABLED
448 #elif (CCM_SUPPORT != ENABLED && CCM_SUPPORT != DISABLED)
449  #error CCM_SUPPORT parameter is not valid
450 #endif
451 
452 //GCM mode support
453 #ifndef GCM_SUPPORT
454  #define GCM_SUPPORT ENABLED
455 #elif (GCM_SUPPORT != ENABLED && GCM_SUPPORT != DISABLED)
456  #error GCM_SUPPORT parameter is not valid
457 #endif
458 
459 //Chacha support
460 #ifndef CHACHA_SUPPORT
461  #define CHACHA_SUPPORT DISABLED
462 #elif (CHACHA_SUPPORT != ENABLED && CHACHA_SUPPORT != DISABLED)
463  #error CHACHA_SUPPORT parameter is not valid
464 #endif
465 
466 //Poly1305 support
467 #ifndef POLY1305_SUPPORT
468  #define POLY1305_SUPPORT DISABLED
469 #elif (POLY1305_SUPPORT != ENABLED && POLY1305_SUPPORT != DISABLED)
470  #error POLY1305_SUPPORT parameter is not valid
471 #endif
472 
473 //Chacha20Poly1305 support
474 #ifndef CHACHA20_POLY1305_SUPPORT
475  #define CHACHA20_POLY1305_SUPPORT DISABLED
476 #elif (CHACHA20_POLY1305_SUPPORT != ENABLED && CHACHA20_POLY1305_SUPPORT != DISABLED)
477  #error CHACHA20_POLY1305_SUPPORT parameter is not valid
478 #endif
479 
480 //Diffie-Hellman support
481 #ifndef DH_SUPPORT
482  #define DH_SUPPORT ENABLED
483 #elif (DH_SUPPORT != ENABLED && DH_SUPPORT != DISABLED)
484  #error DH_SUPPORT parameter is not valid
485 #endif
486 
487 //RSA support
488 #ifndef RSA_SUPPORT
489  #define RSA_SUPPORT ENABLED
490 #elif (RSA_SUPPORT != ENABLED && RSA_SUPPORT != DISABLED)
491  #error RSA_SUPPORT parameter is not valid
492 #endif
493 
494 //DSA support
495 #ifndef DSA_SUPPORT
496  #define DSA_SUPPORT ENABLED
497 #elif (DSA_SUPPORT != ENABLED && DSA_SUPPORT != DISABLED)
498  #error DSA_SUPPORT parameter is not valid
499 #endif
500 
501 //Elliptic curve cryptography support
502 #ifndef EC_SUPPORT
503  #define EC_SUPPORT ENABLED
504 #elif (EC_SUPPORT != ENABLED && EC_SUPPORT != DISABLED)
505  #error EC_SUPPORT parameter is not valid
506 #endif
507 
508 //ECDH support
509 #ifndef ECDH_SUPPORT
510  #define ECDH_SUPPORT ENABLED
511 #elif (ECDH_SUPPORT != ENABLED && ECDH_SUPPORT != DISABLED)
512  #error ECDH_SUPPORT parameter is not valid
513 #endif
514 
515 //ECDSA support
516 #ifndef ECDSA_SUPPORT
517  #define ECDSA_SUPPORT ENABLED
518 #elif (ECDSA_SUPPORT != ENABLED && ECDSA_SUPPORT != DISABLED)
519  #error ECDSA_SUPPORT parameter is not valid
520 #endif
521 
522 //HKDF support
523 #ifndef HKDF_SUPPORT
524  #define HKDF_SUPPORT DISABLED
525 #elif (HKDF_SUPPORT != ENABLED && HKDF_SUPPORT != DISABLED)
526  #error HKDF_SUPPORT parameter is not valid
527 #endif
528 
529 //PKCS #5 support
530 #ifndef PKCS5_SUPPORT
531  #define PKCS5_SUPPORT ENABLED
532 #elif (PKCS5_SUPPORT != ENABLED && PKCS5_SUPPORT != DISABLED)
533  #error PKCS5_SUPPORT parameter is not valid
534 #endif
535 
536 //Yarrow PRNG support
537 #ifndef YARROW_SUPPORT
538  #define YARROW_SUPPORT ENABLED
539 #elif (YARROW_SUPPORT != ENABLED && YARROW_SUPPORT != DISABLED)
540  #error YARROW_SUPPORT parameter is not valid
541 #endif
542 
543 //Object identifier support
544 #ifndef OID_SUPPORT
545  #define OID_SUPPORT ENABLED
546 #elif (OID_SUPPORT != ENABLED && OID_SUPPORT != DISABLED)
547  #error OID_SUPPORT parameter is not valid
548 #endif
549 
550 //ASN.1 syntax support
551 #ifndef ASN1_SUPPORT
552  #define ASN1_SUPPORT ENABLED
553 #elif (ASN1_SUPPORT != ENABLED && ASN1_SUPPORT != DISABLED)
554  #error ASN1_SUPPORT parameter is not valid
555 #endif
556 
557 //PEM file support
558 #ifndef PEM_SUPPORT
559  #define PEM_SUPPORT ENABLED
560 #elif (PEM_SUPPORT != ENABLED && PEM_SUPPORT != DISABLED)
561  #error PEM_SUPPORT parameter is not valid
562 #endif
563 
564 //X.509 certificate support
565 #ifndef X509_SUPPORT
566  #define X509_SUPPORT ENABLED
567 #elif (X509_SUPPORT != ENABLED && X509_SUPPORT != DISABLED)
568  #error X509_SUPPORT parameter is not valid
569 #endif
570 
571 //Allocate memory block
572 #ifndef cryptoAllocMem
573  #define cryptoAllocMem(size) osAllocMem(size)
574 #endif
575 
576 //Deallocate memory block
577 #ifndef cryptoFreeMem
578  #define cryptoFreeMem(p) osFreeMem(p)
579 #endif
580 
581 //Fill block of memory
582 #ifndef cryptoMemset
583  #include <string.h>
584  #define cryptoMemset(p, value, length) (void) memset(p, value, length)
585 #endif
586 
587 //Copy block of memory
588 #ifndef cryptoMemcpy
589  #include <string.h>
590  #define cryptoMemcpy(dest, src, length) (void) memcpy(dest, src, length)
591 #endif
592 
593 //Move block of memory
594 #ifndef cryptoMemmove
595  #include <string.h>
596  #define cryptoMemmove(dest, src, length) (void) memmove(dest, src, length)
597 #endif
598 
599 //Compare two blocks of memory
600 #ifndef cryptoMemcmp
601  #include <string.h>
602  #define cryptoMemcmp(p1, p2, length) memcmp(p1, p2, length)
603 #endif
604 
605 //Get string length
606 #ifndef cryptoStrlen
607  #include <string.h>
608  #define cryptoStrlen(s) strlen(s)
609 #endif
610 
611 //Copy string
612 #ifndef cryptoStrcpy
613  #include <string.h>
614  #define cryptoStrcpy(s1, s2) (void) strcpy(s1, s2)
615 #endif
616 
617 //Copy characters from string
618 #ifndef cryptoStrncpy
619  #include <string.h>
620  #define cryptoStrncpy(s1, s2, length) (void) strncpy(s1, s2, length)
621 #endif
622 
623 //Convert a character to lowercase
624 #ifndef cryptoTolower
625  #include <ctype.h>
626  #define cryptoTolower(c) tolower((uint8_t) (c))
627 #endif
628 
629 //Check if a character is a decimal digit
630 #ifndef cryptoIsdigit
631  #include <ctype.h>
632  #define cryptoIsdigit(c) isdigit((uint8_t) (c))
633 #endif
634 
635 //Maximum context size (hash algorithms)
636 #if (SHA3_512_SUPPORT == ENABLED)
637  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha3_512Context)
638 #elif (SHA3_384_SUPPORT == ENABLED)
639  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha3_384Context)
640 #elif (SHA3_256_SUPPORT == ENABLED)
641  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha3_256Context)
642 #elif (SHA3_224_SUPPORT == ENABLED)
643  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha3_224Context)
644 #elif (WHIRLPOOL_SUPPORT == ENABLED)
645  #define MAX_HASH_CONTEXT_SIZE sizeof(WhirlpoolContext)
646 #elif (BLAKE2B512_SUPPORT == ENABLED)
647  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2b512Context)
648 #elif (BLAKE2B384_SUPPORT == ENABLED)
649  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2b384Context)
650 #elif (BLAKE2B256_SUPPORT == ENABLED)
651  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2b256Context)
652 #elif (BLAKE2B160_SUPPORT == ENABLED)
653  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2b160Context)
654 #elif (SHA512_SUPPORT == ENABLED)
655  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha512Context)
656 #elif (SHA384_SUPPORT == ENABLED)
657  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha384Context)
658 #elif (SHA512_256_SUPPORT == ENABLED)
659  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha512_256Context)
660 #elif (SHA512_224_SUPPORT == ENABLED)
661  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha512_224Context)
662 #elif (BLAKE2S256_SUPPORT == ENABLED)
663  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2s256Context)
664 #elif (BLAKE2S224_SUPPORT == ENABLED)
665  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2s224Context)
666 #elif (BLAKE2S160_SUPPORT == ENABLED)
667  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2s160Context)
668 #elif (BLAKE2S128_SUPPORT == ENABLED)
669  #define MAX_HASH_CONTEXT_SIZE sizeof(Blake2s128Context)
670 #elif (SHA256_SUPPORT == ENABLED)
671  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha256Context)
672 #elif (SHA224_SUPPORT == ENABLED)
673  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha224Context)
674 #elif (TIGER_SUPPORT == ENABLED)
675  #define MAX_HASH_CONTEXT_SIZE sizeof(TigerContext)
676 #elif (SHA1_SUPPORT == ENABLED)
677  #define MAX_HASH_CONTEXT_SIZE sizeof(Sha1Context)
678 #elif (RIPEMD160_SUPPORT == ENABLED)
679  #define MAX_HASH_CONTEXT_SIZE sizeof(Ripemd160Context)
680 #elif (RIPEMD128_SUPPORT == ENABLED)
681  #define MAX_HASH_CONTEXT_SIZE sizeof(Ripemd128Context)
682 #elif (MD5_SUPPORT == ENABLED)
683  #define MAX_HASH_CONTEXT_SIZE sizeof(Md5Context)
684 #elif (MD4_SUPPORT == ENABLED)
685  #define MAX_HASH_CONTEXT_SIZE sizeof(Md4Context)
686 #elif (MD2_SUPPORT == ENABLED)
687  #define MAX_HASH_CONTEXT_SIZE sizeof(Md2Context)
688 #endif
689 
690 //Maximum block size (hash algorithms)
691 #if (SHA3_224_SUPPORT == ENABLED)
692  #define MAX_HASH_BLOCK_SIZE SHA3_224_BLOCK_SIZE
693 #elif (SHA3_256_SUPPORT == ENABLED)
694  #define MAX_HASH_BLOCK_SIZE SHA3_256_BLOCK_SIZE
695 #elif (BLAKE2B512_SUPPORT == ENABLED)
696  #define MAX_HASH_BLOCK_SIZE BLAKE2B512_BLOCK_SIZE
697 #elif (BLAKE2B384_SUPPORT == ENABLED)
698  #define MAX_HASH_BLOCK_SIZE BLAKE2B384_BLOCK_SIZE
699 #elif (BLAKE2B256_SUPPORT == ENABLED)
700  #define MAX_HASH_BLOCK_SIZE BLAKE2B256_BLOCK_SIZE
701 #elif (BLAKE2B160_SUPPORT == ENABLED)
702  #define MAX_HASH_BLOCK_SIZE BLAKE2B160_BLOCK_SIZE
703 #elif (SHA512_SUPPORT == ENABLED)
704  #define MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
705 #elif (SHA384_SUPPORT == ENABLED)
706  #define MAX_HASH_BLOCK_SIZE SHA384_BLOCK_SIZE
707 #elif (SHA512_256_SUPPORT == ENABLED)
708  #define MAX_HASH_BLOCK_SIZE SHA512_256_BLOCK_SIZE
709 #elif (SHA512_224_SUPPORT == ENABLED)
710  #define MAX_HASH_BLOCK_SIZE SHA512_224_BLOCK_SIZE
711 #elif (SHA3_384_SUPPORT == ENABLED)
712  #define MAX_HASH_BLOCK_SIZE SHA3_384_BLOCK_SIZE
713 #elif (SHA3_512_SUPPORT == ENABLED)
714  #define MAX_HASH_BLOCK_SIZE SHA3_512_BLOCK_SIZE
715 #elif (BLAKE2S256_SUPPORT == ENABLED)
716  #define MAX_HASH_BLOCK_SIZE BLAKE2S256_BLOCK_SIZE
717 #elif (BLAKE2S224_SUPPORT == ENABLED)
718  #define MAX_HASH_BLOCK_SIZE BLAKE2S224_BLOCK_SIZE
719 #elif (BLAKE2S160_SUPPORT == ENABLED)
720  #define MAX_HASH_BLOCK_SIZE BLAKE2S160_BLOCK_SIZE
721 #elif (BLAKE2S128_SUPPORT == ENABLED)
722  #define MAX_HASH_BLOCK_SIZE BLAKE2S128_BLOCK_SIZE
723 #elif (SHA256_SUPPORT == ENABLED)
724  #define MAX_HASH_BLOCK_SIZE SHA256_BLOCK_SIZE
725 #elif (SHA224_SUPPORT == ENABLED)
726  #define MAX_HASH_BLOCK_SIZE SHA224_BLOCK_SIZE
727 #elif (SHA1_SUPPORT == ENABLED)
728  #define MAX_HASH_BLOCK_SIZE SHA1_BLOCK_SIZE
729 #elif (WHIRLPOOL_SUPPORT == ENABLED)
730  #define MAX_HASH_BLOCK_SIZE WHIRLPOOL_BLOCK_SIZE
731 #elif (TIGER_SUPPORT == ENABLED)
732  #define MAX_HASH_BLOCK_SIZE TIGER_BLOCK_SIZE
733 #elif (RIPEMD160_SUPPORT == ENABLED)
734  #define MAX_HASH_BLOCK_SIZE RIPEMD160_BLOCK_SIZE
735 #elif (RIPEMD128_SUPPORT == ENABLED)
736  #define MAX_HASH_BLOCK_SIZE RIPEMD128_BLOCK_SIZE
737 #elif (MD5_SUPPORT == ENABLED)
738  #define MAX_HASH_BLOCK_SIZE MD5_BLOCK_SIZE
739 #elif (MD4_SUPPORT == ENABLED)
740  #define MAX_HASH_BLOCK_SIZE MD4_BLOCK_SIZE
741 #elif (MD2_SUPPORT == ENABLED)
742  #define MAX_HASH_BLOCK_SIZE MD2_BLOCK_SIZE
743 #endif
744 
745 //Maximum digest size (hash algorithms)
746 #if (WHIRLPOOL_SUPPORT == ENABLED)
747  #define MAX_HASH_DIGEST_SIZE WHIRLPOOL_DIGEST_SIZE
748 #elif (BLAKE2B512_SUPPORT == ENABLED)
749  #define MAX_HASH_DIGEST_SIZE BLAKE2B512_DIGEST_SIZE
750 #elif (SHA3_512_SUPPORT == ENABLED)
751  #define MAX_HASH_DIGEST_SIZE SHA3_512_DIGEST_SIZE
752 #elif (SHA512_SUPPORT == ENABLED)
753  #define MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE
754 #elif (BLAKE2B384_SUPPORT == ENABLED)
755  #define MAX_HASH_DIGEST_SIZE BLAKE2B384_DIGEST_SIZE
756 #elif (SHA3_384_SUPPORT == ENABLED)
757  #define MAX_HASH_DIGEST_SIZE SHA3_384_DIGEST_SIZE
758 #elif (SHA384_SUPPORT == ENABLED)
759  #define MAX_HASH_DIGEST_SIZE SHA384_DIGEST_SIZE
760 #elif (BLAKE2B256_SUPPORT == ENABLED)
761  #define MAX_HASH_DIGEST_SIZE BLAKE2B256_DIGEST_SIZE
762 #elif (BLAKE2S256_SUPPORT == ENABLED)
763  #define MAX_HASH_DIGEST_SIZE BLAKE2S256_DIGEST_SIZE
764 #elif (SHA3_256_SUPPORT == ENABLED)
765  #define MAX_HASH_DIGEST_SIZE SHA3_256_DIGEST_SIZE
766 #elif (SHA512_256_SUPPORT == ENABLED)
767  #define MAX_HASH_DIGEST_SIZE SHA512_256_DIGEST_SIZE
768 #elif (SHA256_SUPPORT == ENABLED)
769  #define MAX_HASH_DIGEST_SIZE SHA256_DIGEST_SIZE
770 #elif (BLAKE2S224_SUPPORT == ENABLED)
771  #define MAX_HASH_DIGEST_SIZE BLAKE2S224_DIGEST_SIZE
772 #elif (SHA3_224_SUPPORT == ENABLED)
773  #define MAX_HASH_DIGEST_SIZE SHA3_224_DIGEST_SIZE
774 #elif (SHA512_224_SUPPORT == ENABLED)
775  #define MAX_HASH_DIGEST_SIZE SHA512_224_DIGEST_SIZE
776 #elif (SHA224_SUPPORT == ENABLED)
777  #define MAX_HASH_DIGEST_SIZE SHA224_DIGEST_SIZE
778 #elif (TIGER_SUPPORT == ENABLED)
779  #define MAX_HASH_DIGEST_SIZE TIGER_DIGEST_SIZE
780 #elif (BLAKE2B160_SUPPORT == ENABLED)
781  #define MAX_HASH_DIGEST_SIZE BLAKE2B160_DIGEST_SIZE
782 #elif (BLAKE2S160_SUPPORT == ENABLED)
783  #define MAX_HASH_DIGEST_SIZE BLAKE2S160_DIGEST_SIZE
784 #elif (SHA1_SUPPORT == ENABLED)
785  #define MAX_HASH_DIGEST_SIZE SHA1_DIGEST_SIZE
786 #elif (RIPEMD160_SUPPORT == ENABLED)
787  #define MAX_HASH_DIGEST_SIZE RIPEMD160_DIGEST_SIZE
788 #elif (BLAKE2S128_SUPPORT == ENABLED)
789  #define MAX_HASH_DIGEST_SIZE BLAKE2S128_DIGEST_SIZE
790 #elif (RIPEMD128_SUPPORT == ENABLED)
791  #define MAX_HASH_DIGEST_SIZE RIPEMD128_DIGEST_SIZE
792 #elif (MD5_SUPPORT == ENABLED)
793  #define MAX_HASH_DIGEST_SIZE MD5_DIGEST_SIZE
794 #elif (MD4_SUPPORT == ENABLED)
795  #define MAX_HASH_DIGEST_SIZE MD4_DIGEST_SIZE
796 #elif (MD2_SUPPORT == ENABLED)
797  #define MAX_HASH_DIGEST_SIZE MD2_DIGEST_SIZE
798 #endif
799 
800 //Maximum context size (block cipher algorithms)
801 #if (ARIA_SUPPORT == ENABLED)
802  #define MAX_CIPHER_CONTEXT_SIZE sizeof(AriaContext)
803 #elif (AES_SUPPORT == ENABLED)
804  #define MAX_CIPHER_CONTEXT_SIZE sizeof(AesContext)
805 #elif (DES3_SUPPORT == ENABLED)
806  #define MAX_CIPHER_CONTEXT_SIZE sizeof(Des3Context)
807 #elif (CAMELLIA_SUPPORT == ENABLED)
808  #define MAX_CIPHER_CONTEXT_SIZE sizeof(CamelliaContext)
809 #elif (PRESENT_SUPPORT == ENABLED)
810  #define MAX_CIPHER_CONTEXT_SIZE sizeof(PresentContext)
811 #elif (RC6_SUPPORT == ENABLED)
812  #define MAX_CIPHER_CONTEXT_SIZE sizeof(Rc6Context)
813 #elif (IDEA_SUPPORT == ENABLED)
814  #define MAX_CIPHER_CONTEXT_SIZE sizeof(IdeaContext)
815 #elif (DES_SUPPORT == ENABLED)
816  #define MAX_CIPHER_CONTEXT_SIZE sizeof(DesContext)
817 #elif (SEED_SUPPORT == ENABLED)
818  #define MAX_CIPHER_CONTEXT_SIZE sizeof(SeedContext)
819 #elif (RC2_SUPPORT == ENABLED)
820  #define MAX_CIPHER_CONTEXT_SIZE sizeof(Rc2Context)
821 #endif
822 
823 //Maximum block size (block cipher algorithms)
824 #if (CAMELLIA_SUPPORT == ENABLED)
825  #define MAX_CIPHER_BLOCK_SIZE CAMELLIA_BLOCK_SIZE
826 #elif (AES_SUPPORT == ENABLED)
827  #define MAX_CIPHER_BLOCK_SIZE AES_BLOCK_SIZE
828 #elif (ARIA_SUPPORT == ENABLED)
829  #define MAX_CIPHER_BLOCK_SIZE ARIA_BLOCK_SIZE
830 #elif (SEED_SUPPORT == ENABLED)
831  #define MAX_CIPHER_BLOCK_SIZE SEED_BLOCK_SIZE
832 #elif (RC6_SUPPORT == ENABLED)
833  #define MAX_CIPHER_BLOCK_SIZE RC6_BLOCK_SIZE
834 #elif (DES3_SUPPORT == ENABLED)
835  #define MAX_CIPHER_BLOCK_SIZE DES3_BLOCK_SIZE
836 #elif (DES_SUPPORT == ENABLED)
837  #define MAX_CIPHER_BLOCK_SIZE DES_BLOCK_SIZE
838 #elif (IDEA_SUPPORT == ENABLED)
839  #define MAX_CIPHER_BLOCK_SIZE IDEA_BLOCK_SIZE
840 #elif (PRESENT_SUPPORT == ENABLED)
841  #define MAX_CIPHER_BLOCK_SIZE PRESENT_BLOCK_SIZE
842 #elif (RC2_SUPPORT == ENABLED)
843  #define MAX_CIPHER_BLOCK_SIZE RC2_BLOCK_SIZE
844 #endif
845 
846 //Rotate left operation
847 #define ROL8(a, n) (((a) << (n)) | ((a) >> (8 - (n))))
848 #define ROL16(a, n) (((a) << (n)) | ((a) >> (16 - (n))))
849 #define ROL32(a, n) (((a) << (n)) | ((a) >> (32 - (n))))
850 #define ROL64(a, n) (((a) << (n)) | ((a) >> (64 - (n))))
851 
852 //Rotate right operation
853 #define ROR8(a, n) (((a) >> (n)) | ((a) << (8 - (n))))
854 #define ROR16(a, n) (((a) >> (n)) | ((a) << (16 - (n))))
855 #define ROR32(a, n) (((a) >> (n)) | ((a) << (32 - (n))))
856 #define ROR64(a, n) (((a) >> (n)) | ((a) << (64 - (n))))
857 
858 //Shift left operation
859 #define SHL8(a, n) ((a) << (n))
860 #define SHL16(a, n) ((a) << (n))
861 #define SHL32(a, n) ((a) << (n))
862 #define SHL64(a, n) ((a) << (n))
863 
864 //Shift right operation
865 #define SHR8(a, n) ((a) >> (n))
866 #define SHR16(a, n) ((a) >> (n))
867 #define SHR32(a, n) ((a) >> (n))
868 #define SHR64(a, n) ((a) >> (n))
869 
870 //Micellaneous macros
871 #define _U8(x) ((uint8_t) (x))
872 #define _U16(x) ((uint16_t) (x))
873 #define _U32(x) ((uint32_t) (x))
874 
875 //Test if a 8-bit integer is zero
876 #define CRYPTO_TEST_Z_8(a) \
877  _U8((_U8((_U8(a) | (~_U8(a) + 1U))) >> 7U) ^ 1U)
878 
879 //Test if a 8-bit integer is nonzero
880 #define CRYPTO_TEST_NZ_8(a) \
881  _U8(_U8((_U8(a) | (~_U8(a) + 1U))) >> 7U)
882 
883 //Test if two 8-bit integers are equal
884 #define CRYPTO_TEST_EQ_8(a, b) \
885  _U8((_U8(((_U8(a) ^ _U8(b)) | (~(_U8(a) ^ _U8(b)) + 1U))) >> 7U) ^ 1U)
886 
887 //Test if two 8-bit integers are not equal
888 #define CRYPTO_TEST_NEQ_8(a, b) \
889  _U8(_U8(((_U8(a) ^ _U8(b)) | (~(_U8(a) ^ _U8(b)) + 1U))) >> 7U)
890 
891 //Test if a 8-bit integer is lower than another 8-bit integer
892 #define CRYPTO_TEST_LT_8(a, b) \
893  _U8(_U8((((_U8(a) - _U8(b)) ^ _U8(b)) | (_U8(a) ^ _U8(b))) ^ _U8(a)) >> 7U)
894 
895 //Test if a 8-bit integer is lower or equal than another 8-bit integer
896 #define CRYPTO_TEST_LTE_8(a, b) \
897  _U8((_U8((((_U8(b) - _U8(a)) ^ _U8(a)) | (_U8(a) ^ _U8(b))) ^ _U8(b)) >> 7U) ^ 1U)
898 
899 //Test if a 8-bit integer is greater than another 8-bit integer
900 #define CRYPTO_TEST_GT_8(a, b) \
901  _U8(_U8((((_U8(b) - _U8(a)) ^ _U8(a)) | (_U8(a) ^ _U8(b))) ^ _U8(b)) >> 7U)
902 
903 //Test if a 8-bit integer is greater or equal than another 8-bit integer
904 #define CRYPTO_TEST_GTE_8(a, b) \
905  _U8((_U8((((_U8(a) - _U8(b)) ^ _U8(b)) | (_U8(a) ^ _U8(b))) ^ _U8(a)) >> 7U) ^ 1U)
906 
907 //Select between two 8-bit integers
908 #define CRYPTO_SELECT_8(a, b, c) \
909  _U8((_U8(a) & (_U8(c) - 1U)) | (_U8(b) & ~(_U8(c) - 1U)))
910 
911 //Test if a 16-bit integer is zero
912 #define CRYPTO_TEST_Z_16(a) \
913  _U16((_U16((_U16(a) | (~_U16(a) + 1U))) >> 15U) ^ 1U)
914 
915 //Test if a 16-bit integer is nonzero
916 #define CRYPTO_TEST_NZ_16(a) \
917  _U16(_U16((_U16(a) | (~_U16(a) + 1U))) >> 15U)
918 
919 //Test if two 16-bit integers are equal
920 #define CRYPTO_TEST_EQ_16(a, b) \
921  _U16((_U16(((_U16(a) ^ _U16(b)) | (~(_U16(a) ^ _U16(b)) + 1U))) >> 15U) ^ 1U)
922 
923 //Test if two 16-bit integers are not equal
924 #define CRYPTO_TEST_NEQ_16(a, b) \
925  _U16(_U16(((_U16(a) ^ _U16(b)) | (~(_U16(a) ^ _U16(b)) + 1U))) >> 15U)
926 
927 //Test if a 16-bit integer is lower than another 16-bit integer
928 #define CRYPTO_TEST_LT_16(a, b) \
929  _U16(_U16((((_U16(a) - _U16(b)) ^ _U16(b)) | (_U16(a) ^ _U16(b))) ^ _U16(a)) >> 15U)
930 
931 //Test if a 16-bit integer is lower or equal than another 16-bit integer
932 #define CRYPTO_TEST_LTE_16(a, b) \
933  _U16((_U16((((_U16(b) - _U16(a)) ^ _U16(a)) | (_U16(a) ^ _U16(b))) ^ _U16(b)) >> 15U) ^ 1U)
934 
935 //Test if a 16-bit integer is greater than another 16-bit integer
936 #define CRYPTO_TEST_GT_16(a, b) \
937  _U16(_U16((((_U16(b) - _U16(a)) ^ _U16(a)) | (_U16(a) ^ _U16(b))) ^ _U16(b)) >> 15U)
938 
939 //Test if a 16-bit integer is greater or equal than another 16-bit integer
940 #define CRYPTO_TEST_GTE_16(a, b) \
941  _U16((_U16((((_U16(a) - _U16(b)) ^ _U16(b)) | (_U16(a) ^ _U16(b))) ^ _U16(a)) >> 15U) ^ 1U)
942 
943 //Select between two 16-bit integers
944 #define CRYPTO_SELECT_16(a, b, c) \
945  _U16((_U16(a) & (_U16(c) - 1U)) | (_U16(b) & ~(_U16(c) - 1U)))
946 
947 //Test if a 32-bit integer is zero
948 #define CRYPTO_TEST_Z_32(a) \
949  _U32((_U32((_U32(a) | (~_U32(a) + 1U))) >> 31U) ^ 1U)
950 
951 //Test if a 32-bit integer is nonzero
952 #define CRYPTO_TEST_NZ_32(a) \
953  _U32(_U32((_U32(a) | (~_U32(a) + 1U))) >> 31U)
954 
955 //Test if two 32-bit integers are equal
956 #define CRYPTO_TEST_EQ_32(a, b) \
957  _U32((_U32(((_U32(a) ^ _U32(b)) | (~(_U32(a) ^ _U32(b)) + 1U))) >> 31U) ^ 1U)
958 
959 //Test if two 32-bit integers are not equal
960 #define CRYPTO_TEST_NEQ_32(a, b) \
961  _U32(_U32(((_U32(a) ^ _U32(b)) | (~(_U32(a) ^ _U32(b)) + 1U))) >> 31U)
962 
963 //Test if a 32-bit integer is lower than another 32-bit integer
964 #define CRYPTO_TEST_LT_32(a, b) \
965  _U32(_U32((((_U32(a) - _U32(b)) ^ _U32(b)) | (_U32(a) ^ _U32(b))) ^ _U32(a)) >> 31U)
966 
967 //Test if a 32-bit integer is lower or equal than another 32-bit integer
968 #define CRYPTO_TEST_LTE_32(a, b) \
969  _U32((_U32((((_U32(b) - _U32(a)) ^ _U32(a)) | (_U32(a) ^ _U32(b))) ^ _U32(b)) >> 31U) ^ 1U)
970 
971 //Test if a 32-bit integer is greater than another 32-bit integer
972 #define CRYPTO_TEST_GT_32(a, b) \
973  _U32(_U32((((_U32(b) - _U32(a)) ^ _U32(a)) | (_U32(a) ^ _U32(b))) ^ _U32(b)) >> 31U)
974 
975 //Test if a 32-bit integer is greater or equal than another 32-bit integer
976 #define CRYPTO_TEST_GTE_32(a, b) \
977  _U32((_U32((((_U32(a) - _U32(b)) ^ _U32(b)) | (_U32(a) ^ _U32(b))) ^ _U32(a)) >> 31U) ^ 1U)
978 
979 //Select between two 32-bit integers
980 #define CRYPTO_SELECT_32(a, b, c) \
981  _U32((_U32(a) & (_U32(c) - 1U)) | (_U32(b) & ~(_U32(c) - 1U)))
982 
983 //C++ guard
984 #ifdef __cplusplus
985  extern "C" {
986 #endif
987 
988 
989 /**
990  * @brief Encryption algorithm type
991  **/
992 
993 typedef enum
994 {
998 
999 
1000 /**
1001  * @brief Cipher operation modes
1002  **/
1003 
1004 typedef enum
1005 {
1016 } CipherMode;
1017 
1018 
1019 //Common API for hash algorithms
1020 typedef error_t (*HashAlgoCompute)(const void *data, size_t length, uint8_t *digest);
1021 typedef void (*HashAlgoInit)(void *context);
1022 typedef void (*HashAlgoUpdate)(void *context, const void *data, size_t length);
1023 typedef void (*HashAlgoFinal)(void *context, uint8_t *digest);
1024 
1025 //Common API for encryption algorithms
1026 typedef error_t (*CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen);
1027 typedef void (*CipherAlgoEncryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length);
1028 typedef void (*CipherAlgoDecryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length);
1029 typedef void (*CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output);
1030 typedef void (*CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output);
1031 
1032 //Common API for pseudo-random number generators
1033 typedef error_t (*PrngAlgoInit)(void *context);
1034 typedef void (*PrngAlgoRelease)(void *context);
1035 typedef error_t (*PrngAlgoSeed)(void *context, const uint8_t *input, size_t length);
1036 typedef error_t (*PrngAlgoAddEntropy)(void *context, uint_t source, const uint8_t *input, size_t length, size_t entropy);
1037 typedef error_t (*PrngAlgoRead)(void *context, uint8_t *output, size_t length);
1038 
1039 
1040 /**
1041  * @brief Generic hash algorithm context
1042  **/
1043 
1044 typedef struct
1045 {
1046  uint8_t digest[1];
1047 } HashContext;
1048 
1049 
1050 /**
1051  * @brief Common interface for hash algorithms
1052  **/
1053 
1054 typedef struct
1055 {
1056  const char_t *name;
1057  const uint8_t *oid;
1058  size_t oidSize;
1059  size_t contextSize;
1060  size_t blockSize;
1061  size_t digestSize;
1066 } HashAlgo;
1067 
1068 
1069 /**
1070  * @brief Common interface for encryption algorithms
1071  **/
1072 
1073 typedef struct
1074 {
1075  const char_t *name;
1076  size_t contextSize;
1078  size_t blockSize;
1084 } CipherAlgo;
1085 
1086 
1087 /**
1088  * @brief Common interface for pseudo-random number generators
1089  **/
1090 
1091 typedef struct
1092 {
1093  const char_t *name;
1094  size_t contextSize;
1100 } PrngAlgo;
1101 
1102 //C++ guard
1103 #ifdef __cplusplus
1104  }
1105 #endif
1106 
1107 #endif
Legacy definitions.
size_t blockSize
Definition: crypto.h:1060
char char_t
Definition: compiler_port.h:41
size_t contextSize
Definition: crypto.h:1076
size_t contextSize
Definition: crypto.h:1094
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1020
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1030
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1082
HashAlgoInit init
Definition: crypto.h:1063
Common interface for encryption algorithms.
Definition: crypto.h:1073
const char_t * name
Definition: crypto.h:1056
HashAlgoCompute compute
Definition: crypto.h:1062
const char_t * name
Definition: crypto.h:1093
error_t(* PrngAlgoSeed)(void *context, const uint8_t *input, size_t length)
Definition: crypto.h:1035
error_t(* PrngAlgoInit)(void *context)
Definition: crypto.h:1033
Generic hash algorithm context.
Definition: crypto.h:1044
CipherAlgoDecryptBlock decryptBlock
Definition: crypto.h:1083
CipherAlgoDecryptStream decryptStream
Definition: crypto.h:1081
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1026
size_t blockSize
Definition: crypto.h:1078
Error codes description.
CipherAlgoInit init
Definition: crypto.h:1079
void(* PrngAlgoRelease)(void *context)
Definition: crypto.h:1034
const char_t * name
Definition: crypto.h:1075
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1021
size_t oidSize
Definition: crypto.h:1058
PrngAlgoAddEntropy addEntropy
Definition: crypto.h:1098
RTOS abstraction layer.
size_t contextSize
Definition: crypto.h:1059
PrngAlgoRelease release
Definition: crypto.h:1096
PrngAlgoSeed seed
Definition: crypto.h:1097
PrngAlgoInit init
Definition: crypto.h:1095
Byte order conversion.
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1022
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1029
CipherAlgoEncryptStream encryptStream
Definition: crypto.h:1080
void(* CipherAlgoEncryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length)
Definition: crypto.h:1027
uint8_t data[]
Definition: dtls_misc.h:167
Common interface for pseudo-random number generators.
Definition: crypto.h:1091
void(* CipherAlgoDecryptStream)(void *context, const uint8_t *input, uint8_t *output, size_t length)
Definition: crypto.h:1028
CipherAlgoType
Encryption algorithm type.
Definition: crypto.h:993
error_t(* PrngAlgoAddEntropy)(void *context, uint_t source, const uint8_t *input, size_t length, size_t entropy)
Definition: crypto.h:1036
CipherAlgoType type
Definition: crypto.h:1077
size_t digestSize
Definition: crypto.h:1061
Common interface for hash algorithms.
Definition: crypto.h:1054
uint8_t length
Definition: dtls_misc.h:140
const uint8_t * oid
Definition: crypto.h:1057
CipherMode
Cipher operation modes.
Definition: crypto.h:1004
error_t(* PrngAlgoRead)(void *context, uint8_t *output, size_t length)
Definition: crypto.h:1037
HashAlgoUpdate update
Definition: crypto.h:1064
PrngAlgoRead read
Definition: crypto.h:1099
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1023