|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file cracen_sw_aead.h |
| 9 | + * |
| 10 | + * @defgroup cracen_sw_aead Software AEAD dispatcher |
| 11 | + * @{ |
| 12 | + * @brief Dispatcher layer for the software AEAD implementations. |
| 13 | + * |
| 14 | + * This module provides a common interface that dispatches to specific |
| 15 | + * software AEAD implementations (such as AES-CCM) as a workaround for |
| 16 | + * hardware limitations with multipart operations. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef CRACEN_SW_AEAD_H |
| 20 | +#define CRACEN_SW_AEAD_H |
| 21 | + |
| 22 | +#include <psa/crypto.h> |
| 23 | +#include <stdbool.h> |
| 24 | +#include <stdint.h> |
| 25 | +#include "cracen_psa_primitives.h" |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief Set up the software AEAD encryption operation. |
| 29 | + * |
| 30 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 31 | + * @param[in] attributes Key attributes. |
| 32 | + * @param[in] key_buffer Key material. |
| 33 | + * @param[in] key_buffer_size Size of the key material. |
| 34 | + * @param[in] alg AEAD algorithm to use. |
| 35 | + * |
| 36 | + * @retval PSA_SUCCESS Operation setup successfully. |
| 37 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 38 | + * @retval PSA_ERROR_INVALID_ARGUMENT Invalid key size. |
| 39 | + */ |
| 40 | +psa_status_t cracen_aead_encrypt_setup(cracen_aead_operation_t *operation, |
| 41 | + const psa_key_attributes_t *attributes, |
| 42 | + const uint8_t *key_buffer, size_t key_buffer_size, |
| 43 | + psa_algorithm_t alg); |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Set up the software AEAD decryption operation. |
| 47 | + * |
| 48 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 49 | + * @param[in] attributes Key attributes. |
| 50 | + * @param[in] key_buffer Key material. |
| 51 | + * @param[in] key_buffer_size Size of the key material. |
| 52 | + * @param[in] alg AEAD algorithm to use. |
| 53 | + * |
| 54 | + * @retval PSA_SUCCESS Operation setup successfully. |
| 55 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 56 | + * @retval PSA_ERROR_INVALID_ARGUMENT Invalid key size. |
| 57 | + */ |
| 58 | +psa_status_t cracen_aead_decrypt_setup(cracen_aead_operation_t *operation, |
| 59 | + const psa_key_attributes_t *attributes, |
| 60 | + const uint8_t *key_buffer, size_t key_buffer_size, |
| 61 | + psa_algorithm_t alg); |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Set nonce for the software AEAD operation. |
| 65 | + * |
| 66 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 67 | + * @param[in] nonce Nonce/IV value. |
| 68 | + * @param[in] nonce_length Length of the nonce. |
| 69 | + * |
| 70 | + * @retval PSA_SUCCESS Nonce set successfully. |
| 71 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 72 | + * @retval PSA_ERROR_INVALID_ARGUMENT Invalid nonce length. |
| 73 | + */ |
| 74 | +psa_status_t cracen_aead_set_nonce(cracen_aead_operation_t *operation, const uint8_t *nonce, |
| 75 | + size_t nonce_length); |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Set lengths for the software AEAD operation. |
| 79 | + * |
| 80 | + * Some AEAD algorithms (such as CCM) require lengths to be known up-front. |
| 81 | + * |
| 82 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 83 | + * @param[in] ad_length Length of additional data. |
| 84 | + * @param[in] plaintext_length Length of plaintext/ciphertext. |
| 85 | + * |
| 86 | + * @retval PSA_SUCCESS Lengths set successfully. |
| 87 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 88 | + */ |
| 89 | +psa_status_t cracen_aead_set_lengths(cracen_aead_operation_t *operation, size_t ad_length, |
| 90 | + size_t plaintext_length); |
| 91 | + |
| 92 | +/** |
| 93 | + * @brief Update the software AEAD operation with additional data. |
| 94 | + * |
| 95 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 96 | + * @param[in] input Additional data. |
| 97 | + * @param[in] input_length Length of additional data. |
| 98 | + * |
| 99 | + * @retval PSA_SUCCESS Additional data processed successfully. |
| 100 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 101 | + * @retval PSA_ERROR_BAD_STATE AD already finalized or operation not initialized. |
| 102 | + */ |
| 103 | +psa_status_t cracen_aead_update_ad(cracen_aead_operation_t *operation, const uint8_t *input, |
| 104 | + size_t input_length); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Update the software AEAD operation with plaintext/ciphertext. |
| 108 | + * |
| 109 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 110 | + * @param[in] input Input data (plaintext for encrypt, ciphertext for decrypt). |
| 111 | + * @param[in] input_length Length of input data. |
| 112 | + * @param[out] output Output buffer. |
| 113 | + * @param[in] output_size Size of output buffer. |
| 114 | + * @param[out] output_length Pointer to store actual output length. |
| 115 | + * |
| 116 | + * @retval PSA_SUCCESS Data processed successfully. |
| 117 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 118 | + * @retval PSA_ERROR_BUFFER_TOO_SMALL Output buffer too small. |
| 119 | + * @retval PSA_ERROR_BAD_STATE Operation not initialized. |
| 120 | + */ |
| 121 | +psa_status_t cracen_aead_update(cracen_aead_operation_t *operation, const uint8_t *input, |
| 122 | + size_t input_length, uint8_t *output, size_t output_size, |
| 123 | + size_t *output_length); |
| 124 | + |
| 125 | +/** |
| 126 | + * @brief Finish the software AEAD encryption and generate the tag. |
| 127 | + * |
| 128 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 129 | + * @param[out] ciphertext Final ciphertext output buffer. |
| 130 | + * @param[in] ciphertext_size Size of ciphertext buffer. |
| 131 | + * @param[out] ciphertext_length Pointer to store actual ciphertext length. |
| 132 | + * @param[out] tag Tag output buffer. |
| 133 | + * @param[in] tag_size Size of tag buffer. |
| 134 | + * @param[out] tag_length Pointer to store actual tag length. |
| 135 | + * |
| 136 | + * @retval PSA_SUCCESS Encryption finished successfully; tag generated. |
| 137 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 138 | + * @retval PSA_ERROR_BUFFER_TOO_SMALL Tag buffer too small. |
| 139 | + * @retval PSA_ERROR_BAD_STATE Operation not initialized. |
| 140 | + */ |
| 141 | +psa_status_t cracen_aead_finish(cracen_aead_operation_t *operation, uint8_t *ciphertext, |
| 142 | + size_t ciphertext_size, size_t *ciphertext_length, uint8_t *tag, |
| 143 | + size_t tag_size, size_t *tag_length); |
| 144 | + |
| 145 | +/** |
| 146 | + * @brief Finish the software AEAD decryption and verify the tag. |
| 147 | + * |
| 148 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 149 | + * @param[out] plaintext Final plaintext output buffer. |
| 150 | + * @param[in] plaintext_size Size of plaintext buffer. |
| 151 | + * @param[out] plaintext_length Pointer to store actual plaintext length. |
| 152 | + * @param[in] tag Tag to verify. |
| 153 | + * @param[in] tag_length Length of the tag. |
| 154 | + * |
| 155 | + * @retval PSA_SUCCESS Decryption finished successfully; tag verified. |
| 156 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 157 | + * @retval PSA_ERROR_INVALID_SIGNATURE Tag verification failed. |
| 158 | + * @retval PSA_ERROR_BAD_STATE Operation not initialized. |
| 159 | + */ |
| 160 | +psa_status_t cracen_aead_verify(cracen_aead_operation_t *operation, uint8_t *plaintext, |
| 161 | + size_t plaintext_size, size_t *plaintext_length, const uint8_t *tag, |
| 162 | + size_t tag_length); |
| 163 | + |
| 164 | +/** |
| 165 | + * @brief Abort the software AEAD operation. |
| 166 | + * |
| 167 | + * Clears the operation context and any sensitive data. |
| 168 | + * |
| 169 | + * @param[in,out] operation Pointer to the AEAD operation structure. |
| 170 | + * |
| 171 | + * @retval PSA_SUCCESS Operation aborted successfully. |
| 172 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 173 | + */ |
| 174 | +psa_status_t cracen_aead_abort(cracen_aead_operation_t *operation); |
| 175 | + |
| 176 | +/** |
| 177 | + * @brief Perform a single software AEAD encryption. |
| 178 | + * |
| 179 | + * @param[in] attributes Key attributes. |
| 180 | + * @param[in] key_buffer Key material. |
| 181 | + * @param[in] key_buffer_size Size of key material. |
| 182 | + * @param[in] alg AEAD algorithm. |
| 183 | + * @param[in] nonce Nonce/IV. |
| 184 | + * @param[in] nonce_length Length of nonce. |
| 185 | + * @param[in] additional_data Additional data. |
| 186 | + * @param[in] additional_data_length Length of additional data. |
| 187 | + * @param[in] plaintext Plaintext to encrypt. |
| 188 | + * @param[in] plaintext_length Length of plaintext. |
| 189 | + * @param[out] ciphertext Output buffer for ciphertext and tag. |
| 190 | + * @param[in] ciphertext_size Size of ciphertext buffer. |
| 191 | + * @param[out] ciphertext_length Pointer to store actual output length. |
| 192 | + * |
| 193 | + * @retval PSA_SUCCESS Encryption completed successfully. |
| 194 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 195 | + * @retval PSA_ERROR_BUFFER_TOO_SMALL Output buffer too small. |
| 196 | + * @retval PSA_ERROR_INVALID_ARGUMENT Invalid parameters. |
| 197 | + */ |
| 198 | +psa_status_t cracen_aead_encrypt(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 199 | + size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *nonce, |
| 200 | + size_t nonce_length, const uint8_t *additional_data, |
| 201 | + size_t additional_data_length, const uint8_t *plaintext, |
| 202 | + size_t plaintext_length, uint8_t *ciphertext, |
| 203 | + size_t ciphertext_size, size_t *ciphertext_length); |
| 204 | + |
| 205 | +/** |
| 206 | + * @brief Perform a single software AEAD decryption. |
| 207 | + * |
| 208 | + * @param[in] attributes Key attributes. |
| 209 | + * @param[in] key_buffer Key material. |
| 210 | + * @param[in] key_buffer_size Size of key material. |
| 211 | + * @param[in] alg AEAD algorithm. |
| 212 | + * @param[in] nonce Nonce/IV. |
| 213 | + * @param[in] nonce_length Length of nonce. |
| 214 | + * @param[in] additional_data Additional data. |
| 215 | + * @param[in] additional_data_length Length of additional data. |
| 216 | + * @param[in] ciphertext Ciphertext and tag to decrypt. |
| 217 | + * @param[in] ciphertext_length Length of ciphertext (including tag). |
| 218 | + * @param[out] plaintext Output buffer for plaintext. |
| 219 | + * @param[in] plaintext_size Size of plaintext buffer. |
| 220 | + * @param[out] plaintext_length Pointer to store actual plaintext length. |
| 221 | + * |
| 222 | + * @retval PSA_SUCCESS Decryption and verification completed successfully. |
| 223 | + * @retval PSA_ERROR_NOT_SUPPORTED Unsupported algorithm. |
| 224 | + * @retval PSA_ERROR_INVALID_SIGNATURE Tag verification failed. |
| 225 | + * @retval PSA_ERROR_BUFFER_TOO_SMALL Output buffer too small. |
| 226 | + * @retval PSA_ERROR_INVALID_ARGUMENT Invalid parameters. |
| 227 | + */ |
| 228 | +psa_status_t cracen_aead_decrypt(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, |
| 229 | + size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *nonce, |
| 230 | + size_t nonce_length, const uint8_t *additional_data, |
| 231 | + size_t additional_data_length, const uint8_t *ciphertext, |
| 232 | + size_t ciphertext_length, uint8_t *plaintext, |
| 233 | + size_t plaintext_size, size_t *plaintext_length); |
| 234 | + |
| 235 | +/** |
| 236 | + * @} |
| 237 | + */ |
| 238 | + |
| 239 | +#endif /* CRACEN_SW_AEAD_H */ |
0 commit comments