blob: d4aee11e44d7291dc472f6c599f3c7a1f74ed614 [file] [log] [blame]
Antonio de Angelis3a480992018-11-07 11:53:28 +00001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
Antonio de Angelis3a480992018-11-07 11:53:28 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Jamie Fox0e54ebc2019-04-09 14:21:04 +01008#include <stddef.h>
9#include <stdint.h>
Antonio de Angelis4743e672019-04-11 11:38:48 +010010
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
Antonio de Angelis3a480992018-11-07 11:53:28 +000012
Jamie Fox0e54ebc2019-04-09 14:21:04 +010013#include "tfm_crypto_api.h"
14#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010015#include "tfm_crypto_private.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000016
Antonio de Angelis3a480992018-11-07 11:53:28 +000017/*!
18 * \defgroup public_psa Public functions, PSA
19 *
20 */
21
22/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000023psa_status_t tfm_crypto_aead_encrypt(psa_invec in_vec[],
24 size_t in_len,
25 psa_outvec out_vec[],
26 size_t out_len)
Antonio de Angelis3a480992018-11-07 11:53:28 +000027{
Kevin Peng96f802e2019-12-26 16:10:25 +080028#ifdef TFM_CRYPTO_AEAD_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010029 return PSA_ERROR_NOT_SUPPORTED;
30#else
Antonio de Angelis3a480992018-11-07 11:53:28 +000031 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis3a480992018-11-07 11:53:28 +000032
Soby Mathewd8abdfd2020-10-14 10:28:01 +010033 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000034
Antonio de Angelis4743e672019-04-11 11:38:48 +010035 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010036 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000037 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +010038
Antonio de Angelis4743e672019-04-11 11:38:48 +010039 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
40 const struct tfm_crypto_aead_pack_input *aead_pack_input = &iov->aead_in;
Maulik Patel28659c42021-01-06 14:09:22 +000041 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +010042 psa_algorithm_t alg = iov->alg;
43 const uint8_t *nonce = aead_pack_input->nonce;
44 size_t nonce_length = aead_pack_input->nonce_length;
45 const uint8_t *plaintext = in_vec[1].base;
46 size_t plaintext_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000047 uint8_t *ciphertext = out_vec[0].base;
48 size_t ciphertext_size = out_vec[0].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +010049 const uint8_t *additional_data = in_vec[2].base;
50 size_t additional_data_length = in_vec[2].len;
Maulik Patel28659c42021-01-06 14:09:22 +000051 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis4743e672019-04-11 11:38:48 +010052
Soby Mathewd8abdfd2020-10-14 10:28:01 +010053 /* Initialise ciphertext_length to zero. */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000054 out_vec[0].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +000055
Maulik Patel28659c42021-01-06 14:09:22 +000056 status = tfm_crypto_check_handle_owner(key_id, NULL);
57 if (status != PSA_SUCCESS) {
58 return status;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010059 }
60
Maulik Patel28659c42021-01-06 14:09:22 +000061 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
62 if (status != PSA_SUCCESS) {
63 return status;
64 }
65
66 return psa_aead_encrypt(encoded_key, alg, nonce, nonce_length,
67 additional_data, additional_data_length,
68 plaintext, plaintext_length,
69 ciphertext, ciphertext_size, &out_vec[0].len);
Antonio de Angelis7740b382019-07-16 10:59:25 +010070#endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */
Antonio de Angelis3a480992018-11-07 11:53:28 +000071}
72
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000073psa_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 Angelis3a480992018-11-07 11:53:28 +000077{
Kevin Peng96f802e2019-12-26 16:10:25 +080078#ifdef TFM_CRYPTO_AEAD_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010079 return PSA_ERROR_NOT_SUPPORTED;
80#else
Antonio de Angelis3a480992018-11-07 11:53:28 +000081 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis3a480992018-11-07 11:53:28 +000082
Soby Mathewd8abdfd2020-10-14 10:28:01 +010083 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000084
Antonio de Angelis4743e672019-04-11 11:38:48 +010085 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010086 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis4743e672019-04-11 11:38:48 +010087 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +010088
Antonio de Angelis4743e672019-04-11 11:38:48 +010089 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;
Maulik Patel28659c42021-01-06 14:09:22 +000091 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis4743e672019-04-11 11:38:48 +010092 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 Angelisab85ccd2019-03-25 15:14:29 +000097 uint8_t *plaintext = out_vec[0].base;
98 size_t plaintext_size = out_vec[0].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +010099 const uint8_t *additional_data = in_vec[2].base;
100 size_t additional_data_length = in_vec[2].len;
Maulik Patel28659c42021-01-06 14:09:22 +0000101 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100102
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100103 /* Initialise plaintext_length to zero. */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000104 out_vec[0].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000105
Maulik Patel28659c42021-01-06 14:09:22 +0000106 status = tfm_crypto_check_handle_owner(key_id, NULL);
107 if (status != PSA_SUCCESS) {
108 return status;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100109 }
110
Maulik Patel28659c42021-01-06 14:09:22 +0000111 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
112 if (status != PSA_SUCCESS) {
113 return status;
114 }
115
116 return psa_aead_decrypt(encoded_key, alg, nonce, nonce_length,
117 additional_data, additional_data_length,
118 ciphertext, ciphertext_length,
119 plaintext, plaintext_size, &out_vec[0].len);
Antonio de Angelis7740b382019-07-16 10:59:25 +0100120#endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */
Antonio de Angelis3a480992018-11-07 11:53:28 +0000121}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100122
123psa_status_t tfm_crypto_aead_encrypt_setup(psa_invec in_vec[],
124 size_t in_len,
125 psa_outvec out_vec[],
126 size_t out_len)
127{
128 /* FixMe: To be implemented */
129 return PSA_ERROR_NOT_SUPPORTED;
130}
131
132psa_status_t tfm_crypto_aead_decrypt_setup(psa_invec in_vec[],
133 size_t in_len,
134 psa_outvec out_vec[],
135 size_t out_len)
136{
137 /* FixMe: To be implemented */
138 return PSA_ERROR_NOT_SUPPORTED;
139}
140
141psa_status_t tfm_crypto_aead_abort(psa_invec in_vec[],
142 size_t in_len,
143 psa_outvec out_vec[],
144 size_t out_len)
145{
146 /* FixMe: To be implemented */
147 return PSA_ERROR_NOT_SUPPORTED;
148}
149
150psa_status_t tfm_crypto_aead_finish(psa_invec in_vec[],
151 size_t in_len,
152 psa_outvec out_vec[],
153 size_t out_len)
154{
155 /* FixMe: To be implemented */
156 return PSA_ERROR_NOT_SUPPORTED;
157}
158
159psa_status_t tfm_crypto_aead_generate_nonce(psa_invec in_vec[],
160 size_t in_len,
161 psa_outvec out_vec[],
162 size_t out_len)
163{
164 /* FixMe: To be implemented */
165 return PSA_ERROR_NOT_SUPPORTED;
166}
167
168psa_status_t tfm_crypto_aead_set_nonce(psa_invec in_vec[],
169 size_t in_len,
170 psa_outvec out_vec[],
171 size_t out_len)
172{
173 /* FixMe: To be implemented */
174 return PSA_ERROR_NOT_SUPPORTED;
175}
176
177psa_status_t tfm_crypto_aead_set_lengths(psa_invec in_vec[],
178 size_t in_len,
179 psa_outvec out_vec[],
180 size_t out_len)
181{
182 /* FixMe: To be implemented */
183 return PSA_ERROR_NOT_SUPPORTED;
184}
185
186psa_status_t tfm_crypto_aead_update(psa_invec in_vec[],
187 size_t in_len,
188 psa_outvec out_vec[],
189 size_t out_len)
190{
191 /* FixMe: To be implemented */
192 return PSA_ERROR_NOT_SUPPORTED;
193}
194
195psa_status_t tfm_crypto_aead_update_ad(psa_invec in_vec[],
196 size_t in_len,
197 psa_outvec out_vec[],
198 size_t out_len)
199{
200 /* FixMe: To be implemented */
201 return PSA_ERROR_NOT_SUPPORTED;
202}
203
204psa_status_t tfm_crypto_aead_verify(psa_invec in_vec[],
205 size_t in_len,
206 psa_outvec out_vec[],
207 size_t out_len)
208{
209 /* FixMe: To be implemented */
210 return PSA_ERROR_NOT_SUPPORTED;
211}
Antonio de Angelis3a480992018-11-07 11:53:28 +0000212/*!@}*/