@@ -62,13 +62,16 @@ extern "C" {
6262#elif (IS_USED (MODULE_PSA_ASYMMETRIC_ECC_P256R1 ) || \
6363 IS_USED (MODULE_PSA_ASYMMETRIC_ECC_ED25519 ) || \
6464 IS_USED (MODULE_PSA_CIPHER_AES_256_CBC ) || \
65+ IS_USED (MODULE_PSA_AEAD_AES_256_CCM ) || \
6566 IS_USED (MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256 ) || \
6667 IS_USED (MODULE_PSA_CIPHER_CHACHA20 ))
6768#define CONFIG_PSA_MAX_KEY_SIZE 32
6869#elif (IS_USED (MODULE_PSA_CIPHER_AES_192_CBC ) || \
70+ IS_USED (MODULE_PSA_AEAD_AES_192_CCM ) || \
6971 IS_USED (MODULE_PSA_ASYMMETRIC_ECC_P192R1 ))
7072#define CONFIG_PSA_MAX_KEY_SIZE 24
7173#elif (IS_USED (MODULE_PSA_CIPHER_AES_128_CBC )) || \
74+ (IS_USED (MODULE_PSA_AEAD_AES_128_CCM )) || \
7275 (IS_USED (MODULE_PSA_CIPHER_AES_128_ECB ))
7376#define CONFIG_PSA_MAX_KEY_SIZE 16
7477#else
@@ -124,6 +127,52 @@ extern "C" {
124127#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE (ciphertext_length ) \
125128/* implementation-defined value */
126129
130+ /**
131+ * @brief The length of a tag for an AEAD algorithm, in bytes.
132+ *
133+ * @details This is the size of the tag output from @ref psa_aead_finish().
134+ * If the size of the tag buffer is at least this large, it is guaranteed that
135+ * @ref psa_aead_finish() will not fail due to an insufficient tag buffer size.
136+ *
137+ * See also @ref PSA_AEAD_TAG_MAX_SIZE.
138+ *
139+ * @param key_type The type of the AEAD key.
140+ * @param key_bits The size of the AEAD key in bits.
141+ * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that
142+ * @ref PSA_ALG_IS_AEAD(@p alg) is true.
143+ *
144+ * @return The tag length for the specified algorithm and key.
145+ * 0 if the AEAD algorithm does not have an identified tag that can be distinguished from
146+ * the rest of the ciphertext.
147+ * 0 if the AEAD algorithm is not recognized or not supported.
148+ */
149+ #define PSA_AEAD_TAG_LENGTH (key_type , key_bits , alg ) \
150+ (PSA_ALG_IS_AEAD(alg) ? \
151+ (((alg) & 0x003f0000) >> 16) : \
152+ ((void) (key_type), (void) (key_bits), 0))
153+
154+ /**
155+ * @brief A sufficient buffer size for storing the tag output by @ref psa_aead_finish(),
156+ * for any of the supported key types and AEAD algorithms.
157+ *
158+ * @details If the size of the tag buffer is at least this large, it is guaranteed that
159+ * @ref psa_aead_finish() will not fail due to an insufficient buffer size.
160+ *
161+ * See also @ref PSA_AEAD_TAG_LENGTH().
162+ */
163+ #define PSA_AEAD_TAG_MAX_SIZE (16)
164+
165+ /**
166+ * @brief A sufficient buffer size for storing the tag output by @ref psa_aead_finish(),
167+ * for AES key types and CCM algorithms.
168+ *
169+ * @details If the size of the tag buffer is at least this large, it is guaranteed that
170+ * @ref psa_aead_finish() will not fail due to an insufficient buffer size.
171+ *
172+ * See also @ref PSA_AEAD_TAG_LENGTH().
173+ */
174+ #define PSA_AES_CCM_TAG_MAX_SIZE (16)
175+
127176/**
128177 * @brief A sufficient plaintext buffer size for @ref psa_aead_decrypt(), in bytes.
129178 *
@@ -143,7 +192,9 @@ extern "C" {
143192 * are incompatible.
144193 */
145194#define PSA_AEAD_DECRYPT_OUTPUT_SIZE (key_type , alg , ciphertext_length ) \
146- /* implementation-defined value */
195+ (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
196+ ((ciphertext_length) > PSA_AEAD_TAG_LENGTH(key_type, 0, alg)) ? \
197+ (ciphertext_length) - PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : 0)
147198
148199/**
149200 * @brief A sufficient ciphertext buffer size for @ref psa_aead_encrypt(),
@@ -158,7 +209,7 @@ extern "C" {
158209 * @param plaintext_length Size of the plaintext in bytes.
159210 */
160211#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE (plaintext_length ) \
161- /* implementation-defined value */
212+ ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
162213
163214/**
164215 * @brief A sufficient ciphertext buffer size for @ref psa_aead_encrypt(), in bytes.
@@ -179,7 +230,8 @@ extern "C" {
179230 * are incompatible.
180231 */
181232#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE (key_type , alg , plaintext_length ) \
182- /* implementation-defined value */
233+ (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
234+ (plaintext_length) + PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : 0)
183235
184236/**
185237 * @brief A sufficient ciphertext buffer size for @ref psa_aead_finish(),
@@ -232,7 +284,13 @@ extern "C" {
232284 * 0 if the key type or AEAD algorithm is not recognized, not supported or the parameters
233285 * are incompatible.
234286 */
235- #define PSA_AEAD_NONCE_LENGTH (key_type , alg ) /* implementation-defined value */
287+ #define PSA_AEAD_NONCE_LENGTH (key_type , alg ) \
288+ ((PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 && \
289+ ((PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM) || \
290+ (PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM))) || \
291+ (key_type == PSA_KEY_TYPE_CHACHA20 && \
292+ PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CHACHA20_POLY1305) ? \
293+ 12 : 0)
236294
237295/**
238296 * @brief A sufficient buffer size for storing the nonce generated by
@@ -243,40 +301,7 @@ extern "C" {
243301 *
244302 * See also @ref PSA_AEAD_NONCE_LENGTH().
245303 */
246- #define PSA_AEAD_NONCE_MAX_SIZE /* implementation-defined value */
247-
248- /**
249- * @brief The length of a tag for an AEAD algorithm, in bytes.
250- *
251- * @details This is the size of the tag output from @ref psa_aead_finish().
252- * If the size of the tag buffer is at least this large, it is guaranteed that
253- * @ref psa_aead_finish() will not fail due to an insufficient tag buffer size.
254- *
255- * See also @ref PSA_AEAD_TAG_MAX_SIZE.
256- *
257- * @param key_type The type of the AEAD key.
258- * @param key_bits The size of the AEAD key in bits.
259- * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that
260- * @ref PSA_ALG_IS_AEAD(@p alg) is true.
261- *
262- * @return The tag length for the specified algorithm and key.
263- * 0 if the AEAD algorithm does not have an identified tag that can be distinguished from
264- * the rest of the ciphertext.
265- * 0 if the AEAD algorithm is not recognized or not supported.
266- */
267- #define PSA_AEAD_TAG_LENGTH (key_type , key_bits , alg ) \
268- /* implementation-defined value */
269-
270- /**
271- * @brief A sufficient buffer size for storing the tag output by @ref psa_aead_finish(),
272- * for any of the supported key types and AEAD algorithms.
273- *
274- * @details If the size of the tag buffer is at least this large, it is guaranteed that
275- * @ref psa_aead_finish() will not fail due to an insufficient buffer size.
276- *
277- * See also @ref PSA_AEAD_TAG_LENGTH().
278- */
279- #define PSA_AEAD_TAG_MAX_SIZE /* implementation-defined value */
304+ #define PSA_AEAD_NONCE_MAX_SIZE (13)
280305
281306/**
282307 * @brief A sufficient output buffer size for @ref psa_aead_update(), for any of the supported key
0 commit comments