blob: ebb522fa354e38430949fbcbd4f6aff03f9ebd6a [file] [log] [blame]
Antonio de Angelis3a480992018-11-07 11:53:28 +00001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2019-2020, 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
Summer Qin4b1d03b2019-07-02 14:56:08 +080011/* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter
Antonio de Angelis4743e672019-04-11 11:38:48 +010012 * 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 Fox0e54ebc2019-04-09 14:21:04 +010016#include "tfm_mbedcrypto_include.h"
Antonio de Angelis3a480992018-11-07 11:53:28 +000017
Jamie Fox0e54ebc2019-04-09 14:21:04 +010018#include "tfm_crypto_api.h"
19#include "tfm_crypto_defs.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010020#include "tfm_crypto_private.h"
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000021
Antonio de Angelis3a480992018-11-07 11:53:28 +000022/*!
23 * \defgroup public_psa Public functions, PSA
24 *
25 */
26
27/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000028psa_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 Angelis3a480992018-11-07 11:53:28 +000032{
Kevin Peng96f802e2019-12-26 16:10:25 +080033#ifdef TFM_CRYPTO_AEAD_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010034 return PSA_ERROR_NOT_SUPPORTED;
35#else
Antonio de Angelis3a480992018-11-07 11:53:28 +000036 psa_status_t status = PSA_SUCCESS;
Antonio de Angelis3a480992018-11-07 11:53:28 +000037
Soby Mathewd8abdfd2020-10-14 10:28:01 +010038 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000039
Antonio de Angelis4743e672019-04-11 11:38:48 +010040 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080041 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000042 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +010043
Antonio de Angelis4743e672019-04-11 11:38:48 +010044 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 Fox0e54ebc2019-04-09 14:21:04 +010046 psa_key_handle_t key_handle = iov->key_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010047 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 Angelisab85ccd2019-03-25 15:14:29 +000052 uint8_t *ciphertext = out_vec[0].base;
53 size_t ciphertext_size = out_vec[0].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +010054 const uint8_t *additional_data = in_vec[2].base;
55 size_t additional_data_length = in_vec[2].len;
Antonio de Angelis4743e672019-04-11 11:38:48 +010056
Soby Mathewd8abdfd2020-10-14 10:28:01 +010057 /* Initialise ciphertext_length to zero. */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000058 out_vec[0].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +000059
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010060 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 Angelisab85ccd2019-03-25 15:14:29 +000069 return status;
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))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080086 return PSA_ERROR_CONNECTION_REFUSED;
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;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010091 psa_key_handle_t key_handle = iov->key_handle;
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;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100101
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100102 /* Initialise plaintext_length to zero. */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000103 out_vec[0].len = 0;
Jamie Fox82b87ca2018-12-11 16:41:11 +0000104
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100105 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 Angelisab85ccd2019-03-25 15:14:29 +0000114 return status;
Antonio de Angelis7740b382019-07-16 10:59:25 +0100115#endif /* TFM_CRYPTO_AEAD_MODULE_DISABLED */
Antonio de Angelis3a480992018-11-07 11:53:28 +0000116}
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100117
118psa_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
127psa_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
136psa_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
145psa_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
154psa_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
163psa_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
172psa_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
181psa_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
190psa_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
199psa_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 Angelis3a480992018-11-07 11:53:28 +0000207/*!@}*/