Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA cipher driver entry points |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #ifndef PSA_CRYPTO_CIPHER_H |
| 22 | #define PSA_CRYPTO_CIPHER_H |
| 23 | |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 24 | #include <mbedtls/cipher.h> |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 25 | #include <psa/crypto.h> |
| 26 | |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 27 | /** Get Mbed TLS cipher information given the cipher algorithm PSA identifier |
| 28 | * as well as the PSA type and size of the key to be used with the cipher |
| 29 | * algorithm. |
| 30 | * |
| 31 | * \param alg PSA cipher algorithm identifier |
| 32 | * \param key_type PSA key type |
| 33 | * \param key_bits Size of the key in bits |
| 34 | * \param[out] cipher_id Mbed TLS cipher algorithm identifier |
| 35 | * |
| 36 | * \return The Mbed TLS cipher information of the cipher algorithm. |
| 37 | * \c NULL if the PSA cipher algorithm is not supported. |
| 38 | */ |
| 39 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
| 40 | psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits, |
| 41 | mbedtls_cipher_id_t *cipher_id ); |
| 42 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 43 | /** |
| 44 | * \brief Set the key for a multipart symmetric encryption operation. |
| 45 | * |
| 46 | * \note The signature of this function is that of a PSA driver |
| 47 | * cipher_encrypt_setup entry point. This function behaves as a |
| 48 | * cipher_encrypt_setup entry point as defined in the PSA driver |
| 49 | * interface specification for transparent drivers. |
| 50 | * |
| 51 | * \param[in,out] operation The operation object to set up. It has been |
| 52 | * initialized as per the documentation for |
| 53 | * #psa_cipher_operation_t and not yet in use. |
| 54 | * \param[in] attributes The attributes of the key to use for the |
| 55 | * operation. |
| 56 | * \param[in] key_buffer The buffer containing the key context. |
| 57 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 58 | * \param[in] alg The cipher algorithm to compute |
| 59 | * (\c PSA_ALG_XXX value such that |
| 60 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 61 | * |
| 62 | * \retval #PSA_SUCCESS |
| 63 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 64 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 65 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 66 | */ |
| 67 | psa_status_t mbedtls_psa_cipher_encrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 68 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 69 | const psa_key_attributes_t *attributes, |
| 70 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 71 | psa_algorithm_t alg ); |
| 72 | |
| 73 | /** |
| 74 | * \brief Set the key for a multipart symmetric decryption operation. |
| 75 | * |
| 76 | * \note The signature of this function is that of a PSA driver |
| 77 | * cipher_decrypt_setup entry point. This function behaves as a |
| 78 | * cipher_decrypt_setup entry point as defined in the PSA driver |
| 79 | * interface specification for transparent drivers. |
| 80 | * |
| 81 | * \param[in,out] operation The operation object to set up. It has been |
| 82 | * initialized as per the documentation for |
| 83 | * #psa_cipher_operation_t and not yet in use. |
| 84 | * \param[in] attributes The attributes of the key to use for the |
| 85 | * operation. |
| 86 | * \param[in] key_buffer The buffer containing the key context. |
| 87 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 88 | * \param[in] alg The cipher algorithm to compute |
| 89 | * (\c PSA_ALG_XXX value such that |
| 90 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 91 | * |
| 92 | * \retval #PSA_SUCCESS |
| 93 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 94 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 95 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 96 | */ |
| 97 | psa_status_t mbedtls_psa_cipher_decrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 98 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 99 | const psa_key_attributes_t *attributes, |
| 100 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 101 | psa_algorithm_t alg ); |
| 102 | |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 103 | /** Generate an IV for a symmetric encryption operation. |
| 104 | * |
| 105 | * This function generates a random IV (initialization vector), nonce |
| 106 | * or initial counter value for the encryption operation as appropriate |
| 107 | * for the chosen algorithm, key type and key size. |
| 108 | * |
| 109 | * \note The signature of this function is that of a PSA driver |
| 110 | * cipher_generate_iv entry point. This function behaves as a |
| 111 | * cipher_generate_iv entry point as defined in the PSA driver |
| 112 | * interface specification for transparent drivers. |
| 113 | * |
| 114 | * \param[in,out] operation Active cipher operation. |
| 115 | * \param[out] iv Buffer where the generated IV is to be written. |
| 116 | * \param[in] iv_size Size of the \p iv buffer in bytes. |
| 117 | * \param[out] iv_length On success, the number of bytes of the |
| 118 | * generated IV. |
| 119 | * |
| 120 | * \retval #PSA_SUCCESS |
| 121 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 122 | * The size of the \p iv buffer is too small. |
| 123 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 124 | */ |
| 125 | psa_status_t mbedtls_psa_cipher_generate_iv( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 126 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 127 | uint8_t *iv, size_t iv_size, size_t *iv_length ); |
| 128 | |
| 129 | /** Set the IV for a symmetric encryption or decryption operation. |
| 130 | * |
| 131 | * This function sets the IV (initialization vector), nonce |
| 132 | * or initial counter value for the encryption or decryption operation. |
| 133 | * |
| 134 | * \note The signature of this function is that of a PSA driver |
| 135 | * cipher_set_iv entry point. This function behaves as a |
| 136 | * cipher_set_iv entry point as defined in the PSA driver |
| 137 | * interface specification for transparent drivers. |
| 138 | * |
| 139 | * \param[in,out] operation Active cipher operation. |
| 140 | * \param[in] iv Buffer containing the IV to use. |
Ronald Cron | a0d6817 | 2021-03-26 10:15:08 +0100 | [diff] [blame^] | 141 | * \param[in] iv_length Size of the IV in bytes. It is guaranteed by |
| 142 | * the core to be less or equal to |
| 143 | * PSA_CIPHER_IV_MAX_SIZE. |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 144 | * |
| 145 | * \retval #PSA_SUCCESS |
| 146 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 147 | * The size of \p iv is not acceptable for the chosen algorithm, |
| 148 | * or the chosen algorithm does not use an IV. |
| 149 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 150 | */ |
| 151 | psa_status_t mbedtls_psa_cipher_set_iv( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 152 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 153 | const uint8_t *iv, size_t iv_length ); |
| 154 | |
| 155 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 156 | * |
| 157 | * \note The signature of this function is that of a PSA driver |
| 158 | * cipher_update entry point. This function behaves as a |
| 159 | * cipher_update entry point as defined in the PSA driver |
| 160 | * interface specification for transparent drivers. |
| 161 | * |
| 162 | * \param[in,out] operation Active cipher operation. |
| 163 | * \param[in] input Buffer containing the message fragment to |
| 164 | * encrypt or decrypt. |
| 165 | * \param[in] input_length Size of the \p input buffer in bytes. |
| 166 | * \param[out] output Buffer where the output is to be written. |
| 167 | * \param[in] output_size Size of the \p output buffer in bytes. |
| 168 | * \param[out] output_length On success, the number of bytes |
| 169 | * that make up the returned output. |
| 170 | * |
| 171 | * \retval #PSA_SUCCESS |
| 172 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 173 | * The size of the \p output buffer is too small. |
| 174 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 175 | */ |
| 176 | psa_status_t mbedtls_psa_cipher_update( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 177 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 178 | const uint8_t *input, size_t input_length, |
| 179 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 180 | |
| 181 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 182 | * |
| 183 | * \note The signature of this function is that of a PSA driver |
| 184 | * cipher_finish entry point. This function behaves as a |
| 185 | * cipher_finish entry point as defined in the PSA driver |
| 186 | * interface specification for transparent drivers. |
| 187 | * |
| 188 | * \param[in,out] operation Active cipher operation. |
| 189 | * \param[out] output Buffer where the output is to be written. |
| 190 | * \param[in] output_size Size of the \p output buffer in bytes. |
| 191 | * \param[out] output_length On success, the number of bytes |
| 192 | * that make up the returned output. |
| 193 | * |
| 194 | * \retval #PSA_SUCCESS |
| 195 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 196 | * The total input size passed to this operation is not valid for |
| 197 | * this particular algorithm. For example, the algorithm is a based |
| 198 | * on block cipher and requires a whole number of blocks, but the |
| 199 | * total input size is not a multiple of the block size. |
| 200 | * \retval #PSA_ERROR_INVALID_PADDING |
| 201 | * This is a decryption operation for an algorithm that includes |
| 202 | * padding, and the ciphertext does not contain valid padding. |
| 203 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 204 | * The size of the \p output buffer is too small. |
| 205 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 206 | */ |
| 207 | psa_status_t mbedtls_psa_cipher_finish( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 208 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 209 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 210 | |
| 211 | /** Abort a cipher operation. |
| 212 | * |
| 213 | * Aborting an operation frees all associated resources except for the |
| 214 | * \p operation structure itself. Once aborted, the operation object |
| 215 | * can be reused for another operation. |
| 216 | * |
| 217 | * \note The signature of this function is that of a PSA driver |
| 218 | * cipher_abort entry point. This function behaves as a |
| 219 | * cipher_abort entry point as defined in the PSA driver |
| 220 | * interface specification for transparent drivers. |
| 221 | * |
| 222 | * \param[in,out] operation Initialized cipher operation. |
| 223 | * |
| 224 | * \retval #PSA_SUCCESS |
| 225 | */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 226 | psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 227 | |
Ronald Cron | 3522e32 | 2021-03-12 11:08:49 +0100 | [diff] [blame] | 228 | /* |
| 229 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 230 | */ |
| 231 | |
| 232 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 233 | psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup( |
| 234 | mbedtls_psa_cipher_operation_t *operation, |
| 235 | const psa_key_attributes_t *attributes, |
| 236 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 237 | psa_algorithm_t alg ); |
| 238 | |
| 239 | psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup( |
| 240 | mbedtls_psa_cipher_operation_t *operation, |
| 241 | const psa_key_attributes_t *attributes, |
| 242 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 243 | psa_algorithm_t alg ); |
| 244 | |
| 245 | psa_status_t mbedtls_transparent_test_driver_cipher_generate_iv( |
| 246 | mbedtls_psa_cipher_operation_t *operation, |
| 247 | uint8_t *iv, size_t iv_size, size_t *iv_length ); |
| 248 | |
| 249 | psa_status_t mbedtls_transparent_test_driver_cipher_set_iv( |
| 250 | mbedtls_psa_cipher_operation_t *operation, |
| 251 | const uint8_t *iv, size_t iv_length ); |
| 252 | |
| 253 | psa_status_t mbedtls_transparent_test_driver_cipher_update( |
| 254 | mbedtls_psa_cipher_operation_t *operation, |
| 255 | const uint8_t *input, size_t input_length, |
| 256 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 257 | |
| 258 | psa_status_t mbedtls_transparent_test_driver_cipher_finish( |
| 259 | mbedtls_psa_cipher_operation_t *operation, |
| 260 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 261 | |
| 262 | psa_status_t mbedtls_transparent_test_driver_cipher_abort( |
| 263 | mbedtls_psa_cipher_operation_t *operation ); |
| 264 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 265 | |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 266 | #endif /* PSA_CRYPTO_CIPHER_H */ |