|
| 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_tinycrypt |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @brief Glue code translating between PSA Crypto and the tinycrypt APIs |
| 14 | + * |
| 15 | + * @author Lukas Luger <[email protected]> |
| 16 | + * |
| 17 | + * @} |
| 18 | + */ |
| 19 | + |
| 20 | +#include "psa/crypto.h" |
| 21 | +#include "tinycrypt/aes.h" |
| 22 | +#include "tinycrypt/ccm_mode.h" |
| 23 | +#include <assert.h> |
| 24 | + |
| 25 | +psa_status_t psa_aead_aes_128_ccm_encrypt(const psa_key_attributes_t *attributes, |
| 26 | + uint8_t *key_buffer, size_t key_buffer_length, |
| 27 | + uint8_t tag_length, const uint8_t *nonce, |
| 28 | + size_t nonce_length, const uint8_t *additional_data, |
| 29 | + size_t additional_data_length, const uint8_t *plaintext, |
| 30 | + size_t plaintext_length, uint8_t *ciphertext, |
| 31 | + size_t ciphertext_size, size_t *ciphertext_length) |
| 32 | +{ |
| 33 | + (void)attributes; |
| 34 | + /* This should already have been checked by PSA. */ |
| 35 | + assert(ciphertext_size >= plaintext_length + tag_length); |
| 36 | + |
| 37 | + int ret; |
| 38 | + struct tc_ccm_mode_struct c; |
| 39 | + struct tc_aes_key_sched_struct sched; |
| 40 | + /* tinycrypt only supports a nonce_length of 13 */ |
| 41 | + if (nonce_length != 13 || additional_data_length >= TC_CCM_AAD_MAX_BYTES || |
| 42 | + plaintext_length >= TC_CCM_PAYLOAD_MAX_BYTES) { |
| 43 | + return PSA_ERROR_NOT_SUPPORTED; |
| 44 | + } |
| 45 | + |
| 46 | + tc_aes128_set_encrypt_key(&sched, key_buffer); |
| 47 | + |
| 48 | + ret = tc_ccm_config(&c, &sched, (uint8_t *)nonce, nonce_length, tag_length); |
| 49 | + if (ret != 1) { |
| 50 | + return PSA_ERROR_GENERIC_ERROR; |
| 51 | + } |
| 52 | + |
| 53 | + ret = tc_ccm_generation_encryption(ciphertext, ciphertext_size, additional_data, |
| 54 | + additional_data_length, plaintext, |
| 55 | + plaintext_length, &c); |
| 56 | + if (ret != 1) { |
| 57 | + return PSA_ERROR_GENERIC_ERROR; |
| 58 | + } |
| 59 | + |
| 60 | + *ciphertext_length = ciphertext_size; |
| 61 | + |
| 62 | + return PSA_SUCCESS; |
| 63 | +} |
| 64 | + |
| 65 | +psa_status_t psa_aead_aes_128_ccm_decrypt(const psa_key_attributes_t *attributes, |
| 66 | + uint8_t *key_buffer, size_t key_buffer_length, |
| 67 | + uint8_t tag_length, const uint8_t *nonce, |
| 68 | + size_t nonce_length, const uint8_t *additional_data, |
| 69 | + size_t additional_data_length, const uint8_t *ciphertext, |
| 70 | + size_t ciphertext_length, uint8_t *plaintext, |
| 71 | + size_t plaintext_size, size_t *plaintext_length) |
| 72 | +{ |
| 73 | + (void)attributes; |
| 74 | + /* This should already have been checked by PSA. */ |
| 75 | + assert(plaintext_size >= ciphertext_length - tag_length); |
| 76 | + |
| 77 | + int ret; |
| 78 | + |
| 79 | + struct tc_ccm_mode_struct c; |
| 80 | + struct tc_aes_key_sched_struct sched; |
| 81 | + /* tinycrypt only supports a nonce_length of 13 */ |
| 82 | + if (nonce_length != 13 || additional_data_length >= TC_CCM_AAD_MAX_BYTES || |
| 83 | + ciphertext_length >= TC_CCM_PAYLOAD_MAX_BYTES) { |
| 84 | + return PSA_ERROR_NOT_SUPPORTED; |
| 85 | + } |
| 86 | + |
| 87 | + tc_aes128_set_decrypt_key(&sched, key_buffer); |
| 88 | + |
| 89 | + ret = tc_ccm_config(&c, &sched, (uint8_t *)nonce, nonce_length, tag_length); |
| 90 | + if (ret != 1) { |
| 91 | + return PSA_ERROR_GENERIC_ERROR; |
| 92 | + } |
| 93 | + |
| 94 | + ret = tc_ccm_decryption_verification(plaintext, plaintext_size, additional_data, |
| 95 | + additional_data_length, ciphertext, |
| 96 | + ciphertext_length, &c); |
| 97 | + if (ret != 1) { |
| 98 | + return PSA_ERROR_INVALID_SIGNATURE; |
| 99 | + } |
| 100 | + |
| 101 | + *plaintext_length = plaintext_size; |
| 102 | + |
| 103 | + return PSA_SUCCESS; |
| 104 | +} |
0 commit comments