All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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_MAC_HMAC_SHA_256))
61#define CONFIG_PSA_MAX_KEY_SIZE 64
62#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
63 IS_USED(MODULE_PSA_ASYMMETRIC_ECC_ED25519) || \
64 IS_USED(MODULE_PSA_CIPHER_AES_256_CBC) || \
65 IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256) || \
66 IS_USED(MODULE_PSA_CIPHER_CHACHA20))
67#define CONFIG_PSA_MAX_KEY_SIZE 32
68#elif (IS_USED(MODULE_PSA_CIPHER_AES_192_CBC) || \
69 IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1))
70#define CONFIG_PSA_MAX_KEY_SIZE 24
71#elif (IS_USED(MODULE_PSA_CIPHER_AES_128_CBC)) || \
72 (IS_USED(MODULE_PSA_CIPHER_AES_128_ECB))
73#define CONFIG_PSA_MAX_KEY_SIZE 16
74#else
75#define CONFIG_PSA_MAX_KEY_SIZE 0
76#endif
77
84#ifndef CONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT
85#define CONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT 0
86#endif
87
94#ifndef CONFIG_PSA_SINGLE_KEY_COUNT
95#define CONFIG_PSA_SINGLE_KEY_COUNT 0
96#endif
97
104#ifndef CONFIG_PSA_PROTECTED_KEY_COUNT
105#if (IS_USED(MODULE_PSA_SECURE_ELEMENT))
106#define CONFIG_PSA_PROTECTED_KEY_COUNT 5
107#else
108#define CONFIG_PSA_PROTECTED_KEY_COUNT 0
109#endif
110#endif
111
124#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
125/* implementation-defined value */
126
145#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
146/* implementation-defined value */
147
160#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
161/* implementation-defined value */
162
181#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
182/* implementation-defined value */
183
193#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE /* implementation-defined value */
194
213#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
214/* implementation-defined value */
215
235#define PSA_AEAD_NONCE_LENGTH(key_type, alg) /* implementation-defined value */
236
246#define PSA_AEAD_NONCE_MAX_SIZE /* implementation-defined value */
247
267#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
268/* implementation-defined value */
269
279#define PSA_AEAD_TAG_MAX_SIZE /* implementation-defined value */
280
292#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
293/* implementation-defined value */
294
313#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
314/* implementation-defined value */
315
327#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE /* implementation-defined value */
328
346#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
347/* implementation-defined value */
348
354#define PSA_HASH_MAX_SIZE (64)
355
361#if (IS_USED(MODULE_PSA_HASH_SHA3_256))
362#define PSA_HASH_MAX_BLOCK_SIZE 136
363#elif (IS_USED(MODULE_PSA_HASH_SHA_512) || \
364 IS_USED(MODULE_PSA_HASH_SHA_384) || \
365 IS_USED(MODULE_PSA_HASH_SHA_512_224) || \
366 IS_USED(MODULE_PSA_HASH_SHA_512_256))
367#define PSA_HASH_MAX_BLOCK_SIZE 128
368#elif (IS_USED(MODULE_PSA_HASH_SHA3_384))
369#define PSA_HASH_MAX_BLOCK_SIZE 104
370#elif (IS_USED(MODULE_PSA_HASH_SHA3_512))
371#define PSA_HASH_MAX_BLOCK_SIZE 72
372#elif (IS_USED(MODULE_PSA_HASH_MD5) || \
373 IS_USED(MODULE_PSA_HASH_SHA_1) || \
374 IS_USED(MODULE_PSA_HASH_SHA_224) || \
375 IS_USED(MODULE_PSA_HASH_SHA_256))
376#define PSA_HASH_MAX_BLOCK_SIZE 64
377#else
378#define PSA_HASH_MAX_BLOCK_SIZE 0
379#endif
380
397#define PSA_HASH_BLOCK_LENGTH(alg) \
398 ( \
399 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
400 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 64 : \
401 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
402 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
403 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
404 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
405 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
406 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
407 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
408 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
409 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
410 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
411 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
412 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
413 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
414 0)
415
430#define PSA_HASH_LENGTH(alg) \
431 ( \
432 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
433 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
434 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
435 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
436 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
437 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
438 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
439 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
440 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
441 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
442 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
443 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
444 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
445 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
446 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
447 0)
448
471#define PSA_MAC_LENGTH(key_type, key_bits, alg) \
472 ((PSA_ALG_IS_HMAC(alg)) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
473 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
474 ((void)(key_type), (void)(key_bits), 0))
475
486#if (IS_USED(MODULE_PSA_MAC_HMAC_SHA_512) || \
487 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_512))
488#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_512)) /* 64 */
489#elif (IS_USED(MODULE_PSA_MAC_HMAC_SHA_384) || \
490 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_384))
491#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_384)) /* 48 */
492#elif (IS_USED(MODULE_PSA_MAC_HMAC_SHA_256) || \
493 IS_USED(MODULE_PSA_MAC_HMAC_SHA_512_256) || \
494 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_256))
495#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_256)) /* 32 */
496#elif (IS_USED(MODULE_PSA_MAC_HMAC_SHA_224) || \
497 IS_USED(MODULE_PSA_MAC_HMAC_SHA_512_224) || \
498 IS_USED(MODULE_PSA_MAC_HMAC_SHA3_224))
499#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA3_224)) /* 28 */
500#elif (IS_USED(MODULE_PSA_MAC_HMAC_RIPEMD160) || \
501 IS_USED(MODULE_PSA_MAC_HMAC_SHA_1))
502#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_SHA_1)) /* 20 */
503#elif (IS_USED(MODULE_PSA_MAC_HMAC_MD2) || \
504 IS_USED(MODULE_PSA_MAC_HMAC_MD4) || \
505 IS_USED(MODULE_PSA_MAC_HMAC_MD5))
506#define PSA_MAC_MAX_SIZE (PSA_HASH_LENGTH(PSA_ALG_MD5)) /* 16 */
507#else
508#define PSA_MAC_MAX_SIZE 0
509#endif
510
523#define PSA_BLOCK_CIPHER_BLOCK_LENGTH(type) \
524 (1u << (((type) >> 8) & 7))
525
531#define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE /* implementation-defined value */
532
544#define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
545 (input_length)
546
565#define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
566 (input_length - PSA_CIPHER_IV_LENGTH(key_type, alg))
567
579#define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
580 (PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(PSA_KEY_TYPE_AES, PSA_ALG_CBC_NO_PADDING, input_length))
581
600#define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
601 (input_length + PSA_CIPHER_IV_LENGTH(key_type, alg))
602
612#define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE /* implementation-defined value */
613
631#define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
632/* implementation-defined value */
633
660#define PSA_CIPHER_IV_LENGTH(key_type, alg) \
661 ((PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
662 ((alg) == PSA_ALG_CBC_NO_PADDING)) ? 16 : \
663 (key_type == PSA_KEY_TYPE_CHACHA20) ? 12 : 0)
664
674#define PSA_CIPHER_IV_MAX_SIZE /* implementation-defined value */
675
688#define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
689/* implementation-defined value */
690
710#define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
711/* implementation-defined value */
712
720#define PSA_HASH_SUSPEND_ALGORITHM_FIELD_LENGTH ((size_t)4)
721
736#define PSA_HASH_SUSPEND_HASH_STATE_FIELD_LENGTH(alg) \
737/* specification-defined value */
738
753#define PSA_HASH_SUSPEND_INPUT_LENGTH_FIELD_LENGTH(alg) \
754/* specification-defined value */
755
765#define PSA_HASH_SUSPEND_OUTPUT_MAX_SIZE /* implementation-defined value */
766
791#define PSA_HASH_SUSPEND_OUTPUT_SIZE(alg) /* specification-defined value */
792
802#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
803/* implementation-defined value */
804
824#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
825/* implementation-defined value */
826
836#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE /* implementation-defined value */
837
857#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
858/* implementation-defined value */
859
870#define PSA_KEY_EXPORT_ECC_KEY_MAX_SIZE(key_type, key_bits) \
871 (size_t)\
872 (PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_ECC_FAMILY_TWISTED_EDWARDS ? 32 : \
873 (PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_ECC_FAMILY_SECP_R1 ? \
874 PSA_BITS_TO_BYTES(key_bits) : \
875 0))
876
913#define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
914 (PSA_KEY_TYPE_IS_PUBLIC_KEY(key_type) ? \
915 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) : \
916 (PSA_KEY_TYPE_IS_ECC(key_type) ? \
917 PSA_KEY_EXPORT_ECC_KEY_MAX_SIZE(key_type, key_bits) : \
918 0))
919
926#define PSA_ECC_KEY_SIZE_IS_VALID(type, bits) \
927 (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_TWISTED_EDWARDS ? \
928 (bits == 255) : \
929 (PSA_KEY_TYPE_ECC_GET_FAMILY(type) == PSA_ECC_FAMILY_SECP_R1 ? \
930 (bits == 128 || \
931 bits == 192 || \
932 bits == 224 || \
933 bits == 256 || \
934 bits == 384) : \
935 0))
936
940#define PSA_MAX_PRIV_KEY_SIZE (PSA_BYTES_TO_BITS(CONFIG_PSA_MAX_KEY_SIZE))
941
951#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
952 IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256))
953#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
954 (PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_ECC_FAMILY_SECT_R1, 256))
955#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_ED25519))
956#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
957 (PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255))
958#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1))
959#define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
960 (PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_ECC_FAMILY_SECT_R1, 192))
961#else
962#define PSA_EXPORT_KEY_PAIR_MAX_SIZE 0
963#endif
964
977#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_type, key_bits) \
978 (PSA_KEY_TYPE_ECC_GET_FAMILY(key_type) == PSA_ECC_FAMILY_TWISTED_EDWARDS ? 32 : \
979 ((size_t)(2 * PSA_BITS_TO_BYTES(key_bits) + 1)))
980
1021#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
1022 (PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_type, key_bits) : \
1023 0)
1024
1035#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
1036 IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256))
1037#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1038 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_SECT_R1, 256))
1039#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1))
1040#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1041 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_SECT_R1, 192))
1042#elif (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_ED25519))
1043#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
1044 (PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255))
1045#else
1046#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE 0
1047#endif
1048
1054#define PSA_MAX_PRIV_KEY_BUFFER_SIZE (PSA_BITS_TO_BYTES(PSA_MAX_PRIV_KEY_SIZE))
1055
1059#define PSA_MAX_ASYMMETRIC_KEYPAIR_SIZE (PSA_BITS_TO_BYTES(PSA_MAX_PRIV_KEY_SIZE) + \
1060 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1061
1065#if IS_USED(MODULE_PSA_ASYMMETRIC)
1066#define PSA_MAX_KEY_DATA_SIZE (PSA_EXPORT_PUBLIC_KEY_MAX_SIZE)
1067#else
1068#define PSA_MAX_KEY_DATA_SIZE (CONFIG_PSA_MAX_KEY_SIZE)
1069#endif
1070
1074#define PSA_MAX_UNSTRUCTURED_KEY_SIZE (CONFIG_PSA_MAX_KEY_SIZE)
1075
1085#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1086 ((size_t)(PSA_BITS_TO_BYTES(curve_bits) * 2))
1087
1106#define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
1107 (PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
1108 ((void)alg, 0))
1109
1110#ifdef __cplusplus
1111}
1112#endif
1113
1114#endif /* PSA_CRYPTO_PSA_CRYPTO_SIZES_H */
Value definitions for PSA Crypto.
Common macros and compiler attributes/pragmas configuration.