Loading...
Searching...
No Matches
crypto_sizes.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 HAW Hamburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
25#ifndef PSA_CRYPTO_PSA_CRYPTO_SIZES_H
26#define PSA_CRYPTO_PSA_CRYPTO_SIZES_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include "kernel_defines.h"
33#include "crypto_values.h"
34
42#define PSA_BITS_TO_BYTES(bits) (size_t)(((bits) + 7) / 8)
43
51#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
52
60#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
61 IS_USED(MODULE_PSA_ASYMMETRIC_ECC_ED25519) || \
62 IS_USED(MODULE_PSA_CIPHER_AES_256_CBC) || \
63 IS_USED(MODULE_PSA_MAC_HMAC_SHA_256) || \
64 IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256) || \
65 IS_USED(MODULE_PSA_CIPHER_CHACHA20))
66#define CONFIG_PSA_MAX_KEY_SIZE 32
67#elif (IS_USED(MODULE_PSA_CIPHER_AES_192_CBC) || \
68 IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1))
69#define CONFIG_PSA_MAX_KEY_SIZE 24
70#elif (IS_USED(MODULE_PSA_CIPHER_AES_128_CBC)) || \
71 (IS_USED(MODULE_PSA_CIPHER_AES_128_ECB))
72#define CONFIG_PSA_MAX_KEY_SIZE 16
73#else
74#define CONFIG_PSA_MAX_KEY_SIZE 0
75#endif
76
83#ifndef CONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT
84#define CONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT 0
85#endif
86
93#ifndef CONFIG_PSA_SINGLE_KEY_COUNT
94#define CONFIG_PSA_SINGLE_KEY_COUNT 0
95#endif
96
103#ifndef CONFIG_PSA_PROTECTED_KEY_COUNT
104#if (IS_USED(MODULE_PSA_SECURE_ELEMENT))
105#define CONFIG_PSA_PROTECTED_KEY_COUNT 5
106#else
107#define CONFIG_PSA_PROTECTED_KEY_COUNT 0
108#endif
109#endif
110
123#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
124/* implementation-defined value */
125
144#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
145/* implementation-defined value */
146
159#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
160/* implementation-defined value */
161
180#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
181/* implementation-defined value */
182
192#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE /* implementation-defined value */
193
212#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
213/* implementation-defined value */
214
234#define PSA_AEAD_NONCE_LENGTH(key_type, alg) /* implementation-defined value */
235
245#define PSA_AEAD_NONCE_MAX_SIZE /* implementation-defined value */
246
266#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
267/* implementation-defined value */
268
278#define PSA_AEAD_TAG_MAX_SIZE /* implementation-defined value */
279
291#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
292/* implementation-defined value */
293
312#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
313/* implementation-defined value */
314
326#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE /* implementation-defined value */
327
345#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
346/* implementation-defined value */
347
353#define PSA_HASH_MAX_SIZE (64)
354
371#define PSA_HASH_BLOCK_LENGTH(alg) \
372 ( \
373 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
374 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 64 : \
375 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
376 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
377 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
378 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
379 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
380 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
381 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
382 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
383 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
384 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
385 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
386 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
387 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
388 0)
389
404#define PSA_HASH_LENGTH(alg) \
405 ( \
406 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
407 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
408 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
409 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
410 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
411 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
412 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
413 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
414 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
415 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
416 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
417 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
418 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
419 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
420 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
421 0)
422
445#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
446 ((PSA_ALG_IS_HMAC(alg)) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
447 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
448 ((void)(key_type), (void)(key_bits), 0))
449
460#if (IS_USED(MODULE_PSA_MAC_HMAC_SHA_512) || \
461 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_512))
462#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_512)) /* 64 */
463#elif (IS_USED(MODULE_PSA_MAC_HMAC_SHA_384) || \
464 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_384))
465#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_384)) /* 48 */
466#elif (IS_USED(MODULE_PSA_MAC_HMAC_SHA_256) || \
467 IS_USED(MODULE_PSA_MAC_HMAC_SHA_512_256) || \
468 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_256))
469#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_256)) /* 32 */
470#elif (IS_USED(MODULE_PSA_MAC_HMAC_SHA_224) || \
471 IS_USED(MODULE_PSA_MAC_HMAC_SHA_512_224) || \
472 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_224))
473#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_224)) /* 28 */
474#elif (IS_USED(MODULE_PSA_MAC_HMAC_RIPEMD160) || \
475 IS_USED(MODULE_PSA_MAC_HMAC_SHA_1))
476#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA_1)) /* 20 */
477#elif (IS_USED(MODULE_PSA_MAC_HMAC_MD2) || \
478 IS_USED(MODULE_PSA_MAC_HMAC_MD4) || \
479 IS_USED(MODULE_PSA_MAC_HMAC_MD5))
480#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_MD5)) /* 16 */
481#else
482#define PSA_MAC_MAX_SIZE 0
483#endif
484
497#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
498 (1u << (((type) >> 8) & 7))
499
505#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE /* implementation-defined value */
506
518#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
519 (input_length)
520
539#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
540 (input_length - PSA_CIPHER_IV_LENGTH(key_type, alg))
541
553#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
554 (PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(PSA_KEY_TYPE_AES, PSA_ALG_CBC_NO_PADDING, input_length))
555
574#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
575 (input_length + PSA_CIPHER_IV_LENGTH(key_type, alg))
576
586#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE /* implementation-defined value */
587
605#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
606/* implementation-defined value */
607
634#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
635 ((PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
636 ((alg) == PSA_ALG_CBC_NO_PADDING)) ? 16 : \
637 (key_type == PSA_KEY_TYPE_CHACHA20) ? 12 : 0)
638
648#define PSA_CIPHER_IV_MAX_SIZE /* implementation-defined value */
649
662#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
663/* implementation-defined value */
664
684#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
685/* implementation-defined value */
686
694#define PSA_HASH_SUSPEND_ALGORITHM_FIELD_LENGTH ((size_t)4)
695
710#define PSA_HASH_SUSPEND_HASH_STATE_FIELD_LENGTH(alg) \
711/* specification-defined value */
712
727#define PSA_HASH_SUSPEND_INPUT_LENGTH_FIELD_LENGTH(alg) \
728/* specification-defined value */
729
739#define PSA_HASH_SUSPEND_OUTPUT_MAX_SIZE /* implementation-defined value */
740
765#define PSA_HASH_SUSPEND_OUTPUT_SIZE(alg) /* specification-defined value */
766
776#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
777/* implementation-defined value */
778
798#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
799/* implementation-defined value */
800
810#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE /* implementation-defined value */
811
831#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
832/* implementation-defined value */
833
844#define PSA_KEY_EXPORT_ECC_KEY_MAX_SIZE(key_type, key_bits) \
845 (size_t)\
846 (PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_ECC_FAMILY_TWISTED_EDWARDS ? 32 : \
847 (PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_ECC_FAMILY_SECP_R1 ? PSA_BITS_TO_BYTES(key_bits) : \
848 0))
849
886#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
887 (PSA_KEY_TYPE_IS_PUBLIC_KEY(key_type) ? PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) : \
888 (PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_KEY_MAX_SIZE(key_type, key_bits) : \
889 0))
890
897#define PSA_ECC_KEY_SIZE_IS_VALID(type, bits) \
898 (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_TWISTED_EDWARDS ? \
899 (bits == 255) : \
900 (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_SECP_R1 ? \
901 (bits == 128 || \
902 bits == 192 || \
903 bits == 224 || \
904 bits == 256 || \
905 bits == 384) : \
906 0))
907
911#define PSA_MAX_PRIV_KEY_SIZE (PSA_BYTES_TO_BITS(CONFIG_PSA_MAX_KEY_SIZE))
912
922#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
923 IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256))
924#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
925 (PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_ECC_FAMILY_SECT_R1, 256))
926#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_ED25519))
927#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
928 (PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255))
929#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1))
930#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
931 (PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_ECC_FAMILY_SECT_R1, 192))
932#else
933#define PSA_EXPORT_KEY_PAIR_MAX_SIZE 0
934#endif
935
948#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_type, key_bits) \
949 (PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_ECC_FAMILY_TWISTED_EDWARDS ? 32 : \
950 ((size_t)(2 * PSA_BITS_TO_BYTES(key_bits) + 1)))
951
992#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
993 (PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_type, key_bits) : \
994 0)
995
1006#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
1007 IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256))
1008#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1009 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_SECT_R1, 256))
1010#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1))
1011#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1012 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_SECT_R1, 192))
1013#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_ED25519))
1014#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1015 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255))
1016#else
1017#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 0
1018#endif
1019
1025#define PSA_MAX_PRIV_KEY_BUFFER_SIZE (PSA_BITS_TO_BYTES(PSA_MAX_PRIV_KEY_SIZE))
1026
1030#define PSA_MAX_ASYMMETRIC_KEYPAIR_SIZE (PSA_BITS_TO_BYTES(PSA_MAX_PRIV_KEY_SIZE) + \
1031 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1032
1036#if IS_USED(MODULE_PSA_ASYMMETRIC)
1037#define PSA_MAX_KEY_DATA_SIZE (PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1038#else
1039#define PSA_MAX_KEY_DATA_SIZE (CONFIG_PSA_MAX_KEY_SIZE)
1040#endif
1041
1045#define PSA_MAX_UNSTRUCTURED_KEY_SIZE (CONFIG_PSA_MAX_KEY_SIZE)
1046
1056#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1057 ((size_t)(PSA_BITS_TO_BYTES(curve_bits) * 2))
1058
1077#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
1078 (PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
1079 ((void)alg, 0))
1080
1081#ifdef __cplusplus
1082}
1083#endif
1084
1085#endif /* PSA_CRYPTO_PSA_CRYPTO_SIZES_H */
Value definitions for PSA Crypto.
Common macros and compiler attributes/pragmas configuration.