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