Skip to content

Commit 08ebf6f

Browse files
committed
pkg/driver_cryptocell_310: add psa_crypto aes ccm
Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c Co-authored-by: mguetschow <[email protected]> Update pkg/driver_cryptocell_310/psa_cryptocell_310/aes_ccm.c
1 parent d004f6a commit 08ebf6f

File tree

9 files changed

+204
-0
lines changed

9 files changed

+204
-0
lines changed

cpu/nrf52/Makefile.features

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ifneq (,$(filter nrf52840xxaa,$(CPU_MODEL)))
2121
FEATURES_PROVIDED += periph_hmac_sha_256
2222
FEATURES_PROVIDED += periph_cipher_aes_128_cbc
2323
FEATURES_PROVIDED += periph_cipher_chacha20
24+
FEATURES_PROVIDED += periph_aead_aes_128_ccm
2425
FEATURES_PROVIDED += periph_ecc_p192r1
2526
FEATURES_PROVIDED += periph_ecc_p256r1
2627
FEATURES_PROVIDED += periph_ecc_ed25519

cpu/nrf52/periph/Makefile.dep

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ ifneq (,$(filter periph_cipher_chacha20,$(USEMODULE)))
4343
USEMODULE += psa_cryptocell_310_cipher_chacha20
4444
endif
4545

46+
ifneq (,$(filter periph_aead_aes_128_ccm,$(USEMODULE)))
47+
USEPKG += driver_cryptocell_310
48+
USEMODULE += psa_cryptocell_310_aes_ccm
49+
endif
50+
4651
ifneq (,$(filter periph_hmac_sha_256,$(USEMODULE)))
4752
USEPKG += driver_cryptocell_310
4853
USEMODULE += psa_cryptocell_310_hmac

features.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ groups:
866866
help: AES 128 CBC hardware acceleration present
867867
- name: periph_cipher_chacha20
868868
help: ChaCha20 hardware acceleration present
869+
- name: periph_aead_aes_128_ccm
870+
help: AES 128 CCM hardware acceleration present
869871
- name: periph_ecc_p192r1
870872
help: ECC P192R1 hardware acceleration peripheral present.
871873
- name: periph_ecc_p256r1

makefiles/features_existing.inc.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ FEATURES_EXISTING := \
139139
no_idle_thread \
140140
periph_adc \
141141
periph_adc_continuous \
142+
periph_aead_aes_128_ccm \
142143
periph_can \
143144
periph_cipher_aes_128_cbc \
144145
periph_cipher_chacha20 \

makefiles/features_modules.inc.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ USEMODULE += $(PERIPH_FEATURES)
1313
PERIPH_IGNORE_MODULES := \
1414
periph_cipher_aes_128_cbc \
1515
periph_cipher_chacha20 \
16+
periph_aead_aes_128_ccm \
1617
periph_clic \
1718
periph_common \
1819
periph_coretimer \

pkg/driver_cryptocell_310/Makefile.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ endif
1313
CFLAGS += -Wno-cast-align
1414

1515
PSEUDOMODULES += psa_cryptocell_310_aes_cbc
16+
PSEUDOMODULES += psa_cryptocell_310_aes_ccm
1617
PSEUDOMODULES += psa_cryptocell_310_aes_common
1718
PSEUDOMODULES += psa_cryptocell_310_cipher_chacha20
1819
PSEUDOMODULES += psa_cryptocell_310_ecc_common

pkg/driver_cryptocell_310/psa_cryptocell_310/Makefile.dep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ ifneq (,$(filter psa_cryptocell_310_aes_cbc,$(USEMODULE)))
2121
USEMODULE += psa_cryptocell_310_aes_common
2222
endif
2323

24+
ifneq (,$(filter psa_cryptocell_310_aes_ccm,$(USEMODULE)))
25+
USEMODULE += psa_cryptocell_310_aes_common
26+
endif
27+
2428
ifneq (,$(filter psa_cryptocell_310_ecc_p192,$(USEMODULE)))
2529
USEMODULE += psa_cryptocell_310_ecc_common
2630
endif
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (C) 2025 TU Dresden
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+
9+
/**
10+
* @ingroup sys_psa_crypto pkg_driver_cryptocell_310
11+
* @{
12+
*
13+
* @brief Glue code translating between PSA Crypto and the PSA Cryptocell 310 APIs
14+
*
15+
* @author Lukas Luger <[email protected]>
16+
*
17+
* @}
18+
*/
19+
20+
#include "psa/crypto.h"
21+
#include "crys_aesccm.h"
22+
#include "crys_aesccm_error.h"
23+
#include "psa_error.h"
24+
#include "cryptocell_310_util.h"
25+
26+
#define ENABLE_DEBUG 0
27+
#include "debug.h"
28+
29+
psa_status_t psa_aead_aes_128_ccm_encrypt(const psa_key_attributes_t *attributes,
30+
uint8_t *key_buffer, size_t key_buffer_length,
31+
uint8_t tag_length, const uint8_t *nonce,
32+
size_t nonce_length, const uint8_t *additional_data,
33+
size_t additional_data_length, const uint8_t *plaintext,
34+
size_t plaintext_length, uint8_t *ciphertext,
35+
size_t ciphertext_size, size_t *ciphertext_length)
36+
{
37+
(void)attributes;
38+
(void)key_buffer_length;
39+
/* This should already have been checked by PSA. */
40+
assert(ciphertext_size >= plaintext_length + tag_length);
41+
42+
if (!cryptocell_310_data_within_ram(nonce) ||
43+
!cryptocell_310_data_within_ram(key_buffer) ||
44+
!cryptocell_310_data_within_ram(additional_data) ||
45+
!cryptocell_310_data_within_ram(plaintext)) {
46+
DEBUG("%s : cryptocell_310 data required to be in RAM.\n", __FILE__);
47+
return PSA_ERROR_DATA_INVALID;
48+
}
49+
50+
uint8_t tag[PSA_AES_CCM_TAG_MAX_SIZE];
51+
52+
CRYSError_t ret = CC_AESCCM(SASI_AES_ENCRYPT, (uint8_t *)key_buffer, CRYS_AES_Key128BitSize,
53+
(uint8_t *)nonce, nonce_length, (uint8_t *)additional_data,
54+
additional_data_length, (uint8_t *)plaintext, plaintext_length,
55+
ciphertext, tag_length, tag, CRYS_AESCCM_MODE_CCM);
56+
57+
if (ret != CRYS_OK) {
58+
DEBUG("%s : cryptocell_310 failed to encrypt with %s.\n", __FILE__,
59+
cryptocell310_status_to_humanly_readable(ret));
60+
return CRYS_to_psa_error(ret);
61+
}
62+
63+
memcpy(&ciphertext[plaintext_length], tag, tag_length);
64+
65+
*ciphertext_length = plaintext_length + tag_length;
66+
67+
return PSA_SUCCESS;
68+
}
69+
70+
psa_status_t psa_aead_aes_128_ccm_decrypt(const psa_key_attributes_t *attributes,
71+
uint8_t *key_buffer, size_t key_buffer_length,
72+
uint8_t tag_length, const uint8_t *nonce,
73+
size_t nonce_length, const uint8_t *additional_data,
74+
size_t additional_data_length, const uint8_t *ciphertext,
75+
size_t ciphertext_length, uint8_t *plaintext,
76+
size_t plaintext_size, size_t *plaintext_length)
77+
{
78+
(void)attributes;
79+
(void)key_buffer_length;
80+
/* This should already have been checked by PSA. */
81+
assert(plaintext_size >= ciphertext_length - tag_length);
82+
83+
if (!cryptocell_310_data_within_ram(nonce) ||
84+
!cryptocell_310_data_within_ram(key_buffer) ||
85+
!cryptocell_310_data_within_ram(additional_data) ||
86+
!cryptocell_310_data_within_ram(ciphertext)) {
87+
DEBUG("%s : cryptocell_310 data required to be in RAM.\n", __FILE__);
88+
return PSA_ERROR_DATA_INVALID;
89+
}
90+
91+
uint8_t tag[PSA_AES_CCM_TAG_MAX_SIZE];
92+
memcpy(tag, &ciphertext[plaintext_size], tag_length);
93+
94+
CRYSError_t ret = CC_AESCCM(SASI_AES_DECRYPT, (uint8_t *)key_buffer, CRYS_AES_Key128BitSize,
95+
(uint8_t *)nonce, nonce_length, (uint8_t *)additional_data,
96+
additional_data_length, (uint8_t *)ciphertext,
97+
ciphertext_length - tag_length, plaintext, tag_length,
98+
tag, CRYS_AESCCM_MODE_CCM);
99+
100+
if (ret != CRYS_OK) {
101+
DEBUG("%s : cryptocell_310 failed to decrypt with %s.\n", __FILE__,
102+
cryptocell310_status_to_humanly_readable(ret));
103+
return CRYS_to_psa_error(ret);
104+
}
105+
106+
*plaintext_length = ciphertext_length - tag_length;
107+
108+
return PSA_SUCCESS;
109+
}

pkg/driver_cryptocell_310/psa_cryptocell_310/error_conversion.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
#include "psa_error.h"
22+
#include "crys_aesccm_error.h"
2223

2324
psa_status_t CRYS_to_psa_error(CRYSError_t error)
2425
{
@@ -144,7 +145,34 @@ psa_status_t CRYS_to_psa_error(CRYSError_t error)
144145
case CRYS_ECMONT_PKI_ERROR:
145146
case CRYS_ECMONT_IS_NOT_SUPPORTED:
146147
case CRYS_ECEDW_IS_NOT_SUPPORTED:
148+
case CRYS_AESCCM_INVALID_USER_CONTEXT_POINTER_ERROR:
149+
case CRYS_AESCCM_ILLEGAL_KEY_SIZE_ERROR:
150+
case CRYS_AESCCM_INVALID_KEY_POINTER_ERROR:
151+
case CRYS_AESCCM_INVALID_ENCRYPT_MODE_ERROR:
152+
case CRYS_AESCCM_USER_CONTEXT_CORRUPTED_ERROR:
153+
case CRYS_AESCCM_DATA_IN_POINTER_INVALID_ERROR:
154+
case CRYS_AESCCM_DATA_OUT_POINTER_INVALID_ERROR:
155+
case CRYS_AESCCM_DATA_IN_SIZE_ILLEGAL:
156+
case CRYS_AESCCM_DATA_OUT_DATA_IN_OVERLAP_ERROR:
157+
case CRYS_AESCCM_DATA_OUT_SIZE_INVALID_ERROR:
158+
case CRYS_AESCCM_ADDITIONAL_BLOCK_NOT_PERMITTED_ERROR:
159+
case CRYS_AESCCM_ILLEGAL_DMA_BUFF_TYPE_ERROR:
160+
case CRYS_AESCCM_ILLEGAL_PARAMETER_SIZE_ERROR:
161+
case CRYS_AESCCM_ILLEGAL_PARAMETER_PTR_ERROR:
162+
case CRYS_AESCCM_ILLEGAL_DATA_TYPE_ERROR:
163+
case CRYS_AESCCM_LAST_BLOCK_NOT_PERMITTED_ERROR:
164+
case CRYS_AESCCM_ILLEGAL_PARAMETER_ERROR:
165+
case CRYS_AESCCM_NOT_ALL_ADATA_WAS_PROCESSED_ERROR:
166+
case CRYS_AESCCM_NOT_ALL_DATA_WAS_PROCESSED_ERROR:
167+
case CRYS_AESCCM_ADATA_WAS_PROCESSED_ERROR:
168+
case CRYS_AESCCM_ILLEGAL_NONCE_SIZE_ERROR:
169+
case CRYS_AESCCM_ILLEGAL_TAG_SIZE_ERROR:
170+
case CRYS_AESCCM_CTX_SIZES_ERROR:
171+
case CRYS_AESCCM_ILLEGAL_PARAMS_ERROR:
172+
case CRYS_AESCCM_IS_NOT_SUPPORTED:
147173
return PSA_ERROR_INVALID_ARGUMENT;
174+
case CRYS_AESCCM_CCM_MAC_INVALID_ERROR:
175+
return PSA_ERROR_INVALID_SIGNATURE;
148176
default:
149177
return PSA_ERROR_GENERIC_ERROR;
150178
}
@@ -486,6 +514,58 @@ const char *cryptocell310_status_to_humanly_readable(uint32_t status)
486514
return "CRYS_CHACHA_INVALID_USER_CONTEXT_POINTER_ERROR";
487515
case CRYS_CHACHA_IS_NOT_SUPPORTED:
488516
return "CRYS_CHACHA_IS_NOT_SUPPORTED";
517+
case CRYS_AESCCM_INVALID_USER_CONTEXT_POINTER_ERROR:
518+
return "CRYS_AESCCM_INVALID_USER_CONTEXT_POINTER_ERROR";
519+
case CRYS_AESCCM_ILLEGAL_KEY_SIZE_ERROR:
520+
return "CRYS_AESCCM_ILLEGAL_KEY_SIZE_ERROR";
521+
case CRYS_AESCCM_INVALID_KEY_POINTER_ERROR:
522+
return "CRYS_AESCCM_INVALID_KEY_POINTER_ERROR";
523+
case CRYS_AESCCM_INVALID_ENCRYPT_MODE_ERROR:
524+
return "CRYS_AESCCM_INVALID_ENCRYPT_MODE_ERROR";
525+
case CRYS_AESCCM_USER_CONTEXT_CORRUPTED_ERROR:
526+
return "CRYS_AESCCM_USER_CONTEXT_CORRUPTED_ERROR";
527+
case CRYS_AESCCM_DATA_IN_POINTER_INVALID_ERROR:
528+
return "CRYS_AESCCM_DATA_IN_POINTER_INVALID_ERROR";
529+
case CRYS_AESCCM_DATA_OUT_POINTER_INVALID_ERROR:
530+
return "CRYS_AESCCM_DATA_OUT_POINTER_INVALID_ERROR";
531+
case CRYS_AESCCM_DATA_IN_SIZE_ILLEGAL:
532+
return "CRYS_AESCCM_DATA_IN_SIZE_ILLEGAL";
533+
case CRYS_AESCCM_DATA_OUT_DATA_IN_OVERLAP_ERROR:
534+
return "CRYS_AESCCM_DATA_OUT_DATA_IN_OVERLAP_ERROR";
535+
case CRYS_AESCCM_DATA_OUT_SIZE_INVALID_ERROR:
536+
return "CRYS_AESCCM_DATA_OUT_SIZE_INVALID_ERROR";
537+
case CRYS_AESCCM_ADDITIONAL_BLOCK_NOT_PERMITTED_ERROR:
538+
return "CRYS_AESCCM_ADDITIONAL_BLOCK_NOT_PERMITTED_ERROR";
539+
case CRYS_AESCCM_ILLEGAL_DMA_BUFF_TYPE_ERROR:
540+
return "CRYS_AESCCM_ILLEGAL_DMA_BUFF_TYPE_ERROR";
541+
case CRYS_AESCCM_ILLEGAL_PARAMETER_SIZE_ERROR:
542+
return "CRYS_AESCCM_ILLEGAL_PARAMETER_SIZE_ERROR";
543+
case CRYS_AESCCM_ILLEGAL_PARAMETER_PTR_ERROR:
544+
return "CRYS_AESCCM_ILLEGAL_PARAMETER_PTR_ERROR";
545+
case CRYS_AESCCM_ILLEGAL_DATA_TYPE_ERROR:
546+
return "CRYS_AESCCM_ILLEGAL_DATA_TYPE_ERROR";
547+
case CRYS_AESCCM_LAST_BLOCK_NOT_PERMITTED_ERROR:
548+
return "CRYS_AESCCM_LAST_BLOCK_NOT_PERMITTED_ERROR";
549+
case CRYS_AESCCM_ILLEGAL_PARAMETER_ERROR:
550+
return "CRYS_AESCCM_ILLEGAL_PARAMETER_ERROR";
551+
case CRYS_AESCCM_NOT_ALL_ADATA_WAS_PROCESSED_ERROR:
552+
return "CRYS_AESCCM_NOT_ALL_ADATA_WAS_PROCESSED_ERROR";
553+
case CRYS_AESCCM_NOT_ALL_DATA_WAS_PROCESSED_ERROR:
554+
return "CRYS_AESCCM_NOT_ALL_DATA_WAS_PROCESSED_ERROR";
555+
case CRYS_AESCCM_ADATA_WAS_PROCESSED_ERROR:
556+
return "CRYS_AESCCM_ADATA_WAS_PROCESSED_ERROR";
557+
case CRYS_AESCCM_ILLEGAL_NONCE_SIZE_ERROR:
558+
return "CRYS_AESCCM_ILLEGAL_NONCE_SIZE_ERROR";
559+
case CRYS_AESCCM_ILLEGAL_TAG_SIZE_ERROR:
560+
return "CRYS_AESCCM_ILLEGAL_TAG_SIZE_ERROR";
561+
case CRYS_AESCCM_CTX_SIZES_ERROR:
562+
return "CRYS_AESCCM_CTX_SIZES_ERROR";
563+
case CRYS_AESCCM_ILLEGAL_PARAMS_ERROR:
564+
return "CRYS_AESCCM_ILLEGAL_PARAMS_ERROR";
565+
case CRYS_AESCCM_IS_NOT_SUPPORTED:
566+
return "CRYS_AESCCM_IS_NOT_SUPPORTED";
567+
case CRYS_AESCCM_CCM_MAC_INVALID_ERROR:
568+
return "CRYS_AESCCM_CCM_MAC_INVALID_ERROR";
489569
default:
490570
return "Error value not recognized";
491571
}

0 commit comments

Comments
 (0)