Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 10 | |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 11 | /* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 12 | * integrity checks but this will have to be revised |
| 13 | * when the full set of error codes mandated by PSA FF |
| 14 | * is available. |
| 15 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 16 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 17 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 18 | #include "tfm_crypto_api.h" |
| 19 | #include "tfm_crypto_defs.h" |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 20 | #include "tfm_crypto_private.h" |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 21 | |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 22 | /*! |
| 23 | * \defgroup public_psa Public functions, PSA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 28 | psa_status_t tfm_crypto_aead_encrypt(psa_invec in_vec[], |
| 29 | size_t in_len, |
| 30 | psa_outvec out_vec[], |
| 31 | size_t out_len) |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 32 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 33 | #ifdef TFM_CRYPTO_AEAD_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 34 | return PSA_ERROR_NOT_SUPPORTED; |
| 35 | #else |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 36 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 37 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 38 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 39 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 40 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 41 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 42 | } |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 43 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 44 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 45 | const struct tfm_crypto_aead_pack_input *aead_pack_input = &iov->aead_in; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 46 | psa_key_handle_t key_handle = iov->key_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 47 | psa_algorithm_t alg = iov->alg; |
| 48 | const uint8_t *nonce = aead_pack_input->nonce; |
| 49 | size_t nonce_length = aead_pack_input->nonce_length; |
| 50 | const uint8_t *plaintext = in_vec[1].base; |
| 51 | size_t plaintext_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 52 | uint8_t *ciphertext = out_vec[0].base; |
| 53 | size_t ciphertext_size = out_vec[0].len; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 54 | const uint8_t *additional_data = in_vec[2].base; |
| 55 | size_t additional_data_length = in_vec[2].len; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 56 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 57 | /* Initialise ciphertext_length to zero. */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 58 | out_vec[0].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 59 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 60 | status = tfm_crypto_check_handle_owner(key_handle, NULL); |
| 61 | if (status == PSA_SUCCESS) { |
| 62 | |
| 63 | status = psa_aead_encrypt(key_handle, alg, nonce, nonce_length, |
| 64 | additional_data, additional_data_length, |
| 65 | plaintext, plaintext_length, |
| 66 | ciphertext, ciphertext_size, &out_vec[0].len); |
| 67 | } |
| 68 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 69 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 70 | #endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */ |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 73 | psa_status_t tfm_crypto_aead_decrypt(psa_invec in_vec[], |
| 74 | size_t in_len, |
| 75 | psa_outvec out_vec[], |
| 76 | size_t out_len) |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 77 | { |
Kevin Peng | 96f802e | 2019-12-26 16:10:25 +0800 | [diff] [blame] | 78 | #ifdef TFM_CRYPTO_AEAD_MODULE_DISABLED |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 79 | return PSA_ERROR_NOT_SUPPORTED; |
| 80 | #else |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 81 | psa_status_t status = PSA_SUCCESS; |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 82 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 83 | CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 84 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 85 | if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) { |
Summer Qin | 4b1d03b | 2019-07-02 14:56:08 +0800 | [diff] [blame] | 86 | return PSA_ERROR_CONNECTION_REFUSED; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 87 | } |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 88 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 89 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 90 | const struct tfm_crypto_aead_pack_input *aead_pack_input = &iov->aead_in; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 91 | psa_key_handle_t key_handle = iov->key_handle; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 92 | psa_algorithm_t alg = iov->alg; |
| 93 | const uint8_t *nonce = aead_pack_input->nonce; |
| 94 | size_t nonce_length = aead_pack_input->nonce_length; |
| 95 | const uint8_t *ciphertext = in_vec[1].base; |
| 96 | size_t ciphertext_length = in_vec[1].len; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 97 | uint8_t *plaintext = out_vec[0].base; |
| 98 | size_t plaintext_size = out_vec[0].len; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 99 | const uint8_t *additional_data = in_vec[2].base; |
| 100 | size_t additional_data_length = in_vec[2].len; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 101 | |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame^] | 102 | /* Initialise plaintext_length to zero. */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 103 | out_vec[0].len = 0; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 104 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 105 | status = tfm_crypto_check_handle_owner(key_handle, NULL); |
| 106 | if (status == PSA_SUCCESS) { |
| 107 | |
| 108 | status = psa_aead_decrypt(key_handle, alg, nonce, nonce_length, |
| 109 | additional_data, additional_data_length, |
| 110 | ciphertext, ciphertext_length, |
| 111 | plaintext, plaintext_size, &out_vec[0].len); |
| 112 | } |
| 113 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 114 | return status; |
Antonio de Angelis | 7740b38 | 2019-07-16 10:59:25 +0100 | [diff] [blame] | 115 | #endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */ |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 116 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 117 | |
| 118 | psa_status_t tfm_crypto_aead_encrypt_setup(psa_invec in_vec[], |
| 119 | size_t in_len, |
| 120 | psa_outvec out_vec[], |
| 121 | size_t out_len) |
| 122 | { |
| 123 | /* FixMe: To be implemented */ |
| 124 | return PSA_ERROR_NOT_SUPPORTED; |
| 125 | } |
| 126 | |
| 127 | psa_status_t tfm_crypto_aead_decrypt_setup(psa_invec in_vec[], |
| 128 | size_t in_len, |
| 129 | psa_outvec out_vec[], |
| 130 | size_t out_len) |
| 131 | { |
| 132 | /* FixMe: To be implemented */ |
| 133 | return PSA_ERROR_NOT_SUPPORTED; |
| 134 | } |
| 135 | |
| 136 | psa_status_t tfm_crypto_aead_abort(psa_invec in_vec[], |
| 137 | size_t in_len, |
| 138 | psa_outvec out_vec[], |
| 139 | size_t out_len) |
| 140 | { |
| 141 | /* FixMe: To be implemented */ |
| 142 | return PSA_ERROR_NOT_SUPPORTED; |
| 143 | } |
| 144 | |
| 145 | psa_status_t tfm_crypto_aead_finish(psa_invec in_vec[], |
| 146 | size_t in_len, |
| 147 | psa_outvec out_vec[], |
| 148 | size_t out_len) |
| 149 | { |
| 150 | /* FixMe: To be implemented */ |
| 151 | return PSA_ERROR_NOT_SUPPORTED; |
| 152 | } |
| 153 | |
| 154 | psa_status_t tfm_crypto_aead_generate_nonce(psa_invec in_vec[], |
| 155 | size_t in_len, |
| 156 | psa_outvec out_vec[], |
| 157 | size_t out_len) |
| 158 | { |
| 159 | /* FixMe: To be implemented */ |
| 160 | return PSA_ERROR_NOT_SUPPORTED; |
| 161 | } |
| 162 | |
| 163 | psa_status_t tfm_crypto_aead_set_nonce(psa_invec in_vec[], |
| 164 | size_t in_len, |
| 165 | psa_outvec out_vec[], |
| 166 | size_t out_len) |
| 167 | { |
| 168 | /* FixMe: To be implemented */ |
| 169 | return PSA_ERROR_NOT_SUPPORTED; |
| 170 | } |
| 171 | |
| 172 | psa_status_t tfm_crypto_aead_set_lengths(psa_invec in_vec[], |
| 173 | size_t in_len, |
| 174 | psa_outvec out_vec[], |
| 175 | size_t out_len) |
| 176 | { |
| 177 | /* FixMe: To be implemented */ |
| 178 | return PSA_ERROR_NOT_SUPPORTED; |
| 179 | } |
| 180 | |
| 181 | psa_status_t tfm_crypto_aead_update(psa_invec in_vec[], |
| 182 | size_t in_len, |
| 183 | psa_outvec out_vec[], |
| 184 | size_t out_len) |
| 185 | { |
| 186 | /* FixMe: To be implemented */ |
| 187 | return PSA_ERROR_NOT_SUPPORTED; |
| 188 | } |
| 189 | |
| 190 | psa_status_t tfm_crypto_aead_update_ad(psa_invec in_vec[], |
| 191 | size_t in_len, |
| 192 | psa_outvec out_vec[], |
| 193 | size_t out_len) |
| 194 | { |
| 195 | /* FixMe: To be implemented */ |
| 196 | return PSA_ERROR_NOT_SUPPORTED; |
| 197 | } |
| 198 | |
| 199 | psa_status_t tfm_crypto_aead_verify(psa_invec in_vec[], |
| 200 | size_t in_len, |
| 201 | psa_outvec out_vec[], |
| 202 | size_t out_len) |
| 203 | { |
| 204 | /* FixMe: To be implemented */ |
| 205 | return PSA_ERROR_NOT_SUPPORTED; |
| 206 | } |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 207 | /*!@}*/ |