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 | |
| 24 | #include <psa/crypto.h> |
| 25 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 26 | /** |
| 27 | * \brief Set the key for a multipart symmetric encryption operation. |
| 28 | * |
| 29 | * \note The signature of this function is that of a PSA driver |
| 30 | * cipher_encrypt_setup entry point. This function behaves as a |
| 31 | * cipher_encrypt_setup entry point as defined in the PSA driver |
| 32 | * interface specification for transparent drivers. |
| 33 | * |
| 34 | * \param[in,out] operation The operation object to set up. It has been |
| 35 | * initialized as per the documentation for |
| 36 | * #psa_cipher_operation_t and not yet in use. |
| 37 | * \param[in] attributes The attributes of the key to use for the |
| 38 | * operation. |
| 39 | * \param[in] key_buffer The buffer containing the key context. |
| 40 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 41 | * \param[in] alg The cipher algorithm to compute |
| 42 | * (\c PSA_ALG_XXX value such that |
| 43 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 44 | * |
| 45 | * \retval #PSA_SUCCESS |
| 46 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 47 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 48 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 49 | */ |
| 50 | psa_status_t mbedtls_psa_cipher_encrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 51 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 52 | const psa_key_attributes_t *attributes, |
| 53 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 54 | psa_algorithm_t alg ); |
| 55 | |
| 56 | /** |
| 57 | * \brief Set the key for a multipart symmetric decryption operation. |
| 58 | * |
| 59 | * \note The signature of this function is that of a PSA driver |
| 60 | * cipher_decrypt_setup entry point. This function behaves as a |
| 61 | * cipher_decrypt_setup entry point as defined in the PSA driver |
| 62 | * interface specification for transparent drivers. |
| 63 | * |
| 64 | * \param[in,out] operation The operation object to set up. It has been |
| 65 | * initialized as per the documentation for |
| 66 | * #psa_cipher_operation_t and not yet in use. |
| 67 | * \param[in] attributes The attributes of the key to use for the |
| 68 | * operation. |
| 69 | * \param[in] key_buffer The buffer containing the key context. |
| 70 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 71 | * \param[in] alg The cipher algorithm to compute |
| 72 | * (\c PSA_ALG_XXX value such that |
| 73 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 74 | * |
| 75 | * \retval #PSA_SUCCESS |
| 76 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 77 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 78 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 79 | */ |
| 80 | psa_status_t mbedtls_psa_cipher_decrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 81 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 82 | const psa_key_attributes_t *attributes, |
| 83 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 84 | psa_algorithm_t alg ); |
| 85 | |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 86 | /** Generate an IV for a symmetric encryption operation. |
| 87 | * |
| 88 | * This function generates a random IV (initialization vector), nonce |
| 89 | * or initial counter value for the encryption operation as appropriate |
| 90 | * for the chosen algorithm, key type and key size. |
| 91 | * |
| 92 | * \note The signature of this function is that of a PSA driver |
| 93 | * cipher_generate_iv entry point. This function behaves as a |
| 94 | * cipher_generate_iv entry point as defined in the PSA driver |
| 95 | * interface specification for transparent drivers. |
| 96 | * |
| 97 | * \param[in,out] operation Active cipher operation. |
| 98 | * \param[out] iv Buffer where the generated IV is to be written. |
| 99 | * \param[in] iv_size Size of the \p iv buffer in bytes. |
| 100 | * \param[out] iv_length On success, the number of bytes of the |
| 101 | * generated IV. |
| 102 | * |
| 103 | * \retval #PSA_SUCCESS |
| 104 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 105 | * The size of the \p iv buffer is too small. |
| 106 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 107 | */ |
| 108 | psa_status_t mbedtls_psa_cipher_generate_iv( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 109 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 110 | uint8_t *iv, size_t iv_size, size_t *iv_length ); |
| 111 | |
| 112 | /** Set the IV for a symmetric encryption or decryption operation. |
| 113 | * |
| 114 | * This function sets the IV (initialization vector), nonce |
| 115 | * or initial counter value for the encryption or decryption operation. |
| 116 | * |
| 117 | * \note The signature of this function is that of a PSA driver |
| 118 | * cipher_set_iv entry point. This function behaves as a |
| 119 | * cipher_set_iv entry point as defined in the PSA driver |
| 120 | * interface specification for transparent drivers. |
| 121 | * |
| 122 | * \param[in,out] operation Active cipher operation. |
| 123 | * \param[in] iv Buffer containing the IV to use. |
| 124 | * \param[in] iv_length Size of the IV in bytes. |
| 125 | * |
| 126 | * \retval #PSA_SUCCESS |
| 127 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 128 | * The size of \p iv is not acceptable for the chosen algorithm, |
| 129 | * or the chosen algorithm does not use an IV. |
| 130 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 131 | */ |
| 132 | psa_status_t mbedtls_psa_cipher_set_iv( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 133 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 134 | const uint8_t *iv, size_t iv_length ); |
| 135 | |
| 136 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 137 | * |
| 138 | * \note The signature of this function is that of a PSA driver |
| 139 | * cipher_update entry point. This function behaves as a |
| 140 | * cipher_update entry point as defined in the PSA driver |
| 141 | * interface specification for transparent drivers. |
| 142 | * |
| 143 | * \param[in,out] operation Active cipher operation. |
| 144 | * \param[in] input Buffer containing the message fragment to |
| 145 | * encrypt or decrypt. |
| 146 | * \param[in] input_length Size of the \p input buffer in bytes. |
| 147 | * \param[out] output Buffer where the output is to be written. |
| 148 | * \param[in] output_size Size of the \p output buffer in bytes. |
| 149 | * \param[out] output_length On success, the number of bytes |
| 150 | * that make up the returned output. |
| 151 | * |
| 152 | * \retval #PSA_SUCCESS |
| 153 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 154 | * The size of the \p output buffer is too small. |
| 155 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 156 | */ |
| 157 | psa_status_t mbedtls_psa_cipher_update( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 158 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 159 | const uint8_t *input, size_t input_length, |
| 160 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 161 | |
| 162 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 163 | * |
| 164 | * \note The signature of this function is that of a PSA driver |
| 165 | * cipher_finish entry point. This function behaves as a |
| 166 | * cipher_finish entry point as defined in the PSA driver |
| 167 | * interface specification for transparent drivers. |
| 168 | * |
| 169 | * \param[in,out] operation Active cipher operation. |
| 170 | * \param[out] output Buffer where the output is to be written. |
| 171 | * \param[in] output_size Size of the \p output buffer in bytes. |
| 172 | * \param[out] output_length On success, the number of bytes |
| 173 | * that make up the returned output. |
| 174 | * |
| 175 | * \retval #PSA_SUCCESS |
| 176 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 177 | * The total input size passed to this operation is not valid for |
| 178 | * this particular algorithm. For example, the algorithm is a based |
| 179 | * on block cipher and requires a whole number of blocks, but the |
| 180 | * total input size is not a multiple of the block size. |
| 181 | * \retval #PSA_ERROR_INVALID_PADDING |
| 182 | * This is a decryption operation for an algorithm that includes |
| 183 | * padding, and the ciphertext does not contain valid padding. |
| 184 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 185 | * The size of the \p output buffer is too small. |
| 186 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 187 | */ |
| 188 | psa_status_t mbedtls_psa_cipher_finish( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 189 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 190 | uint8_t *output, size_t output_size, size_t *output_length ); |
| 191 | |
| 192 | /** Abort a cipher operation. |
| 193 | * |
| 194 | * Aborting an operation frees all associated resources except for the |
| 195 | * \p operation structure itself. Once aborted, the operation object |
| 196 | * can be reused for another operation. |
| 197 | * |
| 198 | * \note The signature of this function is that of a PSA driver |
| 199 | * cipher_abort entry point. This function behaves as a |
| 200 | * cipher_abort entry point as defined in the PSA driver |
| 201 | * interface specification for transparent drivers. |
| 202 | * |
| 203 | * \param[in,out] operation Initialized cipher operation. |
| 204 | * |
| 205 | * \retval #PSA_SUCCESS |
| 206 | */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame^] | 207 | 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] | 208 | |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 209 | #endif /* PSA_CRYPTO_CIPHER_H */ |