Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto_sizes.h |
| 3 | * |
| 4 | * \brief PSA cryptography module: Mbed TLS buffer size macros |
| 5 | * |
| 6 | * This file contains the definitions of macros that are useful to |
| 7 | * compute buffer sizes. The signatures and semantics of these macros |
| 8 | * are standardized, but the definitions are not, because they depend on |
| 9 | * the available algorithms and, in some cases, on permitted tolerances |
| 10 | * on buffer sizes. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame^] | 11 | * |
| 12 | * Macros that compute sizes whose values do not depend on the |
| 13 | * implementation are in crypto.h. |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 14 | */ |
| 15 | /* |
| 16 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
| 30 | * |
| 31 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 32 | */ |
| 33 | |
| 34 | #ifndef PSA_CRYPTO_SIZES_H |
| 35 | #define PSA_CRYPTO_SIZES_H |
| 36 | |
| 37 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 38 | * in each of its header files. */ |
| 39 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 40 | #include "../mbedtls/config.h" |
| 41 | #else |
| 42 | #include MBEDTLS_CONFIG_FILE |
| 43 | #endif |
| 44 | |
| 45 | #if defined(MBEDTLS_SHA512_C) |
| 46 | #define PSA_HASH_MAX_SIZE 64 |
| 47 | #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128 |
| 48 | #else |
| 49 | #define PSA_HASH_MAX_SIZE 32 |
| 50 | #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64 |
| 51 | #endif |
| 52 | |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame^] | 53 | |
| 54 | /** The size of the output of psa_mac_finish(), in bytes. |
| 55 | * |
| 56 | * This is also the MAC size that psa_mac_verify() expects. |
| 57 | * |
| 58 | * \param key_type The type of the MAC key. |
| 59 | * \param key_bits The size of the MAC key in bits. |
| 60 | * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that |
| 61 | * #PSA_ALG_IS_MAC(alg) is true). |
| 62 | * |
| 63 | * \return The MAC size for the specified algorithm with |
| 64 | * the specified key parameters. |
| 65 | * \return 0 if the MAC algorithm is not recognized. |
| 66 | * \return Either 0 or the correct size for a MAC algorithm that |
| 67 | * the implementation recognizes, but does not support. |
| 68 | * \return Unspecified if the key parameters are not consistent |
| 69 | * with the algorithm. |
| 70 | */ |
| 71 | #define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \ |
| 72 | (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \ |
| 73 | PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \ |
| 74 | 0) |
| 75 | |
| 76 | /** The maximum size of the output of psa_aead_encrypt(), in bytes. |
| 77 | * |
| 78 | * If the size of the ciphertext buffer is at least this large, it is |
| 79 | * guaranteed that psa_aead_encrypt() will not fail due to an |
| 80 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 81 | * the ciphertext may be smaller. |
| 82 | * |
| 83 | * \param alg An AEAD algorithm |
| 84 | * (\c PSA_ALG_XXX value such that |
| 85 | * #PSA_ALG_IS_AEAD(alg) is true). |
| 86 | * \param plaintext_length Size of the plaintext in bytes. |
| 87 | * |
| 88 | * \return The AEAD ciphertext size for the specified |
| 89 | * algorithm. |
| 90 | * If the AEAD algorithm is not recognized, return 0. |
| 91 | * An implementation may return either 0 or a |
| 92 | * correct size for an AEAD algorithm that it |
| 93 | * recognizes, but does not support. |
| 94 | */ |
| 95 | #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \ |
| 96 | (PSA_AEAD_TAG_SIZE(alg) != 0 ? \ |
| 97 | (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \ |
| 98 | 0) |
| 99 | |
| 100 | /** The maximum size of the output of psa_aead_decrypt(), in bytes. |
| 101 | * |
| 102 | * If the size of the plaintext buffer is at least this large, it is |
| 103 | * guaranteed that psa_aead_decrypt() will not fail due to an |
| 104 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 105 | * the plaintext may be smaller. |
| 106 | * |
| 107 | * \param alg An AEAD algorithm |
| 108 | * (\c PSA_ALG_XXX value such that |
| 109 | * #PSA_ALG_IS_AEAD(alg) is true). |
| 110 | * \param ciphertext_length Size of the plaintext in bytes. |
| 111 | * |
| 112 | * \return The AEAD ciphertext size for the specified |
| 113 | * algorithm. |
| 114 | * If the AEAD algorithm is not recognized, return 0. |
| 115 | * An implementation may return either 0 or a |
| 116 | * correct size for an AEAD algorithm that it |
| 117 | * recognizes, but does not support. |
| 118 | */ |
| 119 | #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \ |
| 120 | (PSA_AEAD_TAG_SIZE(alg) != 0 ? \ |
| 121 | (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \ |
| 122 | 0) |
| 123 | |
| 124 | /** Safe signature buffer size for psa_asymmetric_sign(). |
| 125 | * |
| 126 | * This macro returns a safe buffer size for a signature using a key |
| 127 | * of the specified type and size, with the specified algorithm. |
| 128 | * Note that the actual size of the signature may be smaller |
| 129 | * (some algorithms produce a variable-size signature). |
| 130 | * |
| 131 | * \warning This function may call its arguments multiple times or |
| 132 | * zero times, so you should not pass arguments that contain |
| 133 | * side effects. |
| 134 | * |
| 135 | * \param key_type An asymmetric key type (this may indifferently be a |
| 136 | * key pair type or a public key type). |
| 137 | * \param key_bits The size of the key in bits. |
| 138 | * \param alg The signature algorithm. |
| 139 | * |
| 140 | * \return If the parameters are valid and supported, return |
| 141 | * a buffer size in bytes that guarantees that |
| 142 | * psa_asymmetric_sign() will not fail with |
| 143 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 144 | * If the parameters are a valid combination that is not supported |
| 145 | * by the implementation, this macro either shall return either a |
| 146 | * sensible size or 0. |
| 147 | * If the parameters are not valid, the |
| 148 | * return value is unspecified. |
| 149 | * |
| 150 | */ |
| 151 | #define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 152 | (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 153 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ |
| 154 | ((void)alg, 0)) |
| 155 | |
| 156 | #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 157 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 158 | ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 159 | 0) |
| 160 | #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 161 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 162 | PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ |
| 163 | 0) |
| 164 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 165 | #endif /* PSA_CRYPTO_SIZES_H */ |