Skip to content

Commit 98e03e3

Browse files
committed
sys/psa_crypto: add aead aes ccm
Update sys/include/psa_crypto/psa/crypto_sizes.h Co-authored-by: mguetschow <[email protected]> Update sys/psa_crypto/include/psa_aead.h Co-authored-by: mguetschow <[email protected]> Update sys/psa_crypto/include/psa_aead.h Co-authored-by: mguetschow <[email protected]> Update sys/psa_crypto/psa_crypto.c Co-authored-by: mguetschow <[email protected]> Update sys/psa_crypto/psa_crypto.c Co-authored-by: mguetschow <[email protected]> Update sys/psa_crypto/psa_crypto_algorithm_dispatch.c Co-authored-by: mguetschow <[email protected]> Update sys/psa_crypto/psa_crypto_algorithm_dispatch.c Co-authored-by: mguetschow <[email protected]> Update sys/include/psa_crypto/psa/crypto_sizes.h
1 parent f9c3005 commit 98e03e3

File tree

11 files changed

+719
-62
lines changed

11 files changed

+719
-62
lines changed

sys/include/psa_crypto/psa/crypto_sizes.h

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

sys/psa_crypto/Makefile.dep

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,77 @@ ifneq (,$(filter psa_cipher_aes_192_cbc_backend_riot,$(USEMODULE)))
143143
USEMODULE += psa_riot_cipher_aes_192_cbc
144144
endif
145145

146+
# AEAD
147+
ifneq (,$(filter psa_aead,$(USEMODULE)))
148+
USEMODULE += psa_key_management
149+
endif
150+
151+
## AES-128-CCM
152+
ifneq (,$(filter psa_aead_aes_128_ccm,$(USEMODULE)))
153+
ifeq (,$(filter psa_aead_aes_128_ccm_custom_backend,$(USEMODULE)))
154+
FEATURES_OPTIONAL += periph_aead_aes_128_ccm
155+
include $(RIOTMAKE)/features_check.inc.mk
156+
# HACK: Due to kconfig migration, may cause problems
157+
ifneq (,$(filter periph_aead_aes_128_ccm,$(FEATURES_USED)))
158+
USEMODULE += psa_aead_aes_128_ccm_backend_periph
159+
else
160+
ifeq (, $(filter psa_aead_aes_128_ccm_backend_tinycrypt,$(USEMODULE)))
161+
USEMODULE += psa_aead_aes_128_ccm_backend_cifra
162+
endif
163+
endif
164+
endif
165+
endif
166+
ifneq (,$(filter psa_aead_aes_128_ccm_backend_periph,$(USEMODULE)))
167+
FEATURES_REQUIRED += periph_aead_aes_128_ccm
168+
endif
169+
ifneq (,$(filter psa_aead_aes_128_ccm_backend_tinycrypt,$(USEMODULE)))
170+
USEPKG += tinycrypt
171+
USEMODULE += psa_tinycrypt
172+
USEMODULE += psa_tinycrypt_aes_ccm
173+
endif
174+
175+
## AES-192-CCM
176+
ifneq (,$(filter psa_aead_aes_192_ccm,$(USEMODULE)))
177+
ifeq (,$(filter psa_aead_aes_192_ccm_custom_backend,$(USEMODULE)))
178+
FEATURES_OPTIONAL += periph_aead_aes_192_ccm
179+
include $(RIOTMAKE)/features_check.inc.mk
180+
# HACK: Due to kconfig migration, may cause problems
181+
ifneq (,$(filter periph_aead_aes_192_ccm,$(FEATURES_USED)))
182+
USEMODULE += psa_aead_aes_192_ccm_backend_periph
183+
else
184+
USEMODULE += psa_aead_aes_192_ccm_backend_cifra
185+
endif
186+
endif
187+
endif
188+
ifneq (,$(filter psa_aead_aes_192_ccm_backend_periph,$(USEMODULE)))
189+
FEATURES_REQUIRED += periph_aead_aes_192_ccm
190+
endif
191+
192+
## AES-256-CCM
193+
ifneq (,$(filter psa_aead_aes_256_ccm,$(USEMODULE)))
194+
ifeq (,$(filter psa_aead_aes_256_ccm_custom_backend,$(USEMODULE)))
195+
FEATURES_OPTIONAL += periph_aead_aes_256_ccm
196+
include $(RIOTMAKE)/features_check.inc.mk
197+
# HACK: Due to kconfig migration, may cause problems
198+
ifneq (,$(filter periph_aead_aes_256_ccm,$(FEATURES_USED)))
199+
USEMODULE += psa_aead_aes_256_ccm_backend_periph
200+
else
201+
USEMODULE += psa_aead_aes_256_ccm_backend_cifra
202+
endif
203+
endif
204+
endif
205+
ifneq (,$(filter psa_aead_aes_256_ccm_backend_periph,$(USEMODULE)))
206+
FEATURES_REQUIRED += periph_aead_aes_256_ccm
207+
endif
208+
209+
## Cifra supports all of them
210+
ifneq (,$(filter psa_aead_aes_%_ccm_backend_cifra,$(USEMODULE)))
211+
USEPKG += cifra
212+
USEMODULE += psa_cifra
213+
USEMODULE += psa_cifra_aes_ccm
214+
endif
215+
216+
146217
## ChaCha20
147218
ifneq (,$(filter psa_cipher_chacha20,$(USEMODULE)))
148219
ifeq (,$(filter psa_cipher_chacha20_custom_backend,$(USEMODULE)))

sys/psa_crypto/Makefile.include

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,43 @@ ifneq (,$(filter psa_cipher_chacha20,$(USEMODULE)))
9797
endif
9898
endif
9999

100+
## AEAD
101+
PSEUDOMODULES += psa_aead
102+
PSEUDOMODULES += psa_aead_aes_128_ccm
103+
PSEUDOMODULES += psa_aead_aes_128_ccm_backend_periph
104+
PSEUDOMODULES += psa_aead_aes_128_ccm_backend_cifra
105+
PSEUDOMODULES += psa_aead_aes_128_ccm_backend_tinycrypt
106+
PSEUDOMODULES += psa_aead_aes_128_ccm_custom_backend
107+
108+
# check that one and only one backend has been selected
109+
ifneq (,$(filter psa_aead_aes_128_ccm,$(USEMODULE)))
110+
ifneq (1,$(call backends,psa_aead_aes_128_ccm))
111+
$(error "One (and only one) backend should be selected for psa_aead_aes_128_ccm")
112+
endif
113+
endif
114+
115+
PSEUDOMODULES += psa_aead_aes_192_ccm
116+
PSEUDOMODULES += psa_aead_aes_192_ccm_backend_cifra
117+
PSEUDOMODULES += psa_aead_aes_192_ccm_custom_backend
118+
119+
# check that one and only one backend has been selected
120+
ifneq (,$(filter psa_aead_aes_192_ccm,$(USEMODULE)))
121+
ifneq (1,$(call backends,psa_aead_aes_192_ccm))
122+
$(error "One (and only one) backend should be selected for psa_aead_aes_192_ccm")
123+
endif
124+
endif
125+
126+
PSEUDOMODULES += psa_aead_aes_256_ccm
127+
PSEUDOMODULES += psa_aead_aes_256_ccm_backend_cifra
128+
PSEUDOMODULES += psa_aead_aes_256_ccm_custom_backend
129+
130+
# check that one and only one backend has been selected
131+
ifneq (,$(filter psa_aead_aes_256_ccm,$(USEMODULE)))
132+
ifneq (1,$(call backends,psa_aead_aes_256_ccm))
133+
$(error "One (and only one) backend should be selected for psa_aead_aes_256_ccm")
134+
endif
135+
endif
136+
100137
## Hash
101138
PSEUDOMODULES += psa_hash
102139
PSEUDOMODULES += psa_hash_md5

sys/psa_crypto/doc.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ names in uppercase and add the prefix `CONFIG_MODULE_` to all of them.
269269
- psa_asymmetric_ecc_ed25519_custom_backend
270270
- psa_asymmetric_ecc_ed25519_backend_c25519
271271

272+
### AEAD
273+
- Base: psa_aead
274+
275+
#### AES CCM
276+
- psa_aead_aes_128_ccm
277+
- psa_aead_aes_128_ccm_backend_periph
278+
- psa_aead_aes_128_ccm_backend_cifra
279+
- psa_aead_aes_128_ccm_backend_tinycrypt
280+
281+
@note Be aware that the tinycrypt only allows a nonce size of 13.
282+
283+
- psa_aead_aes_128_ccm_custom_backend
284+
- psa_aead_aes_192_ccm
285+
- psa_aead_aes_192_ccm_backend_cifra
286+
- psa_aead_aes_192_ccm_custom_backend
287+
- psa_aead_aes_256_ccm
288+
- psa_aead_aes_256_ccm_backend_cifra
289+
- psa_aead_aes_256_ccm_custom_backend
290+
272291
### Ciphers
273292
- Base: psa_cipher
274293

0 commit comments

Comments
 (0)