blob: c9218fee337d6eff683b6ea3b444453c37aa18ec [file] [log] [blame]
Louis Mayencourt7a36f782018-09-24 14:00:57 +01001/*
2 * Copyright (c) 2019, Arm Limited. All rights reserved.
3 *
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>
Louis Mayencourt7a36f782018-09-24 14:00:57 +010010
Antonio de Angelis4743e672019-04-11 11:38:48 +010011/* FixMe: Use PSA_CONNECTION_REFUSED when performing parameter
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 Fox0e54ebc2019-04-09 14:21:04 +010016#include "tfm_mbedcrypto_include.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010017
Jamie Fox0e54ebc2019-04-09 14:21:04 +010018#include "tfm_crypto_api.h"
19#include "tfm_crypto_defs.h"
Louis Mayencourt7a36f782018-09-24 14:00:57 +010020
21/*!
22 * \defgroup public_psa Public functions, PSA
23 *
24 */
25
26/*!@{*/
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000027psa_status_t tfm_crypto_mac_sign_setup(psa_invec in_vec[],
28 size_t in_len,
29 psa_outvec out_vec[],
30 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +010031{
Antonio de Angelis4743e672019-04-11 11:38:48 +010032 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010033 psa_mac_operation_t *operation = NULL;
34
Antonio de Angelis4743e672019-04-11 11:38:48 +010035 if ((in_len != 1) || (out_len != 1)) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000036 return PSA_CONNECTION_REFUSED;
37 }
38
Antonio de Angelis4743e672019-04-11 11:38:48 +010039 if ((out_vec[0].len != sizeof(uint32_t)) ||
40 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000041 return PSA_CONNECTION_REFUSED;
42 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010043 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010044 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010045 uint32_t *handle_out = out_vec[0].base;
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;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000048
Jamie Fox0e54ebc2019-04-09 14:21:04 +010049 /* Init the handle in the operation with the one passed from the iov */
50 *handle_out = iov->op_handle;
51
52 /* Allocate the operation context in the secure world */
53 status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION,
54 &handle,
55 (void **)&operation);
56 if (status != PSA_SUCCESS) {
57 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +010058 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +010059
60 *handle_out = handle;
61
62 status = psa_mac_sign_setup(operation, key_handle, alg);
63 if (status != PSA_SUCCESS) {
64 /* Release the operation context, ignore if the operation fails. */
65 (void)tfm_crypto_operation_release(handle_out);
66 return status;
67 }
68
69 return PSA_SUCCESS;
Louis Mayencourt7a36f782018-09-24 14:00:57 +010070}
71
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000072psa_status_t tfm_crypto_mac_verify_setup(psa_invec in_vec[],
73 size_t in_len,
74 psa_outvec out_vec[],
75 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +010076{
Antonio de Angelis4743e672019-04-11 11:38:48 +010077 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010078 psa_mac_operation_t *operation = NULL;
79
Antonio de Angelis4743e672019-04-11 11:38:48 +010080 if ((in_len != 1) || (out_len != 1)) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000081 return PSA_CONNECTION_REFUSED;
82 }
83
Antonio de Angelis4743e672019-04-11 11:38:48 +010084 if ((out_vec[0].len != sizeof(uint32_t)) ||
85 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000086 return PSA_CONNECTION_REFUSED;
87 }
Antonio de Angelis4743e672019-04-11 11:38:48 +010088 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +010089 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +010090 uint32_t *handle_out = out_vec[0].base;
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;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +000093
Jamie Fox0e54ebc2019-04-09 14:21:04 +010094 /* Init the handle in the operation with the one passed from the iov */
95 *handle_out = iov->op_handle;
96
97 /* Allocate the operation context in the secure world */
98 status = tfm_crypto_operation_alloc(TFM_CRYPTO_MAC_OPERATION,
99 &handle,
100 (void **)&operation);
101 if (status != PSA_SUCCESS) {
102 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100103 }
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100104
105 *handle_out = handle;
106
107 status = psa_mac_verify_setup(operation, key_handle, alg);
108 if (status != PSA_SUCCESS) {
109 /* Release the operation context, ignore if the operation fails. */
110 (void)tfm_crypto_operation_release(handle_out);
111 return status;
112 }
113
114 return PSA_SUCCESS;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100115}
116
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000117psa_status_t tfm_crypto_mac_update(psa_invec in_vec[],
118 size_t in_len,
119 psa_outvec out_vec[],
120 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100121{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000122 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100123 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100124
Antonio de Angelis4743e672019-04-11 11:38:48 +0100125 if ((in_len != 2) || (out_len != 1)) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000126 return PSA_CONNECTION_REFUSED;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100127 }
128
Antonio de Angelis4743e672019-04-11 11:38:48 +0100129 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
130 (out_vec[0].len != sizeof(uint32_t))) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000131 return PSA_CONNECTION_REFUSED;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100132 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100133 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100134 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100135 uint32_t *handle_out = out_vec[0].base;
136 const uint8_t *input = in_vec[1].base;
137 size_t input_length = in_vec[1].len;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100138
Antonio de Angelis4743e672019-04-11 11:38:48 +0100139 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100140 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000141
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100142 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000143 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100144 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100145 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000146 if (status != PSA_SUCCESS) {
147 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100148 }
149
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100150 status = psa_mac_update(operation, input, input_length);
151 if (status != PSA_SUCCESS) {
152 /* Release the operation context, ignore if the operation fails. */
153 (void)tfm_crypto_operation_release(handle_out);
154 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100155 }
156
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000157 return PSA_SUCCESS;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100158}
159
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000160psa_status_t tfm_crypto_mac_sign_finish(psa_invec in_vec[],
161 size_t in_len,
162 psa_outvec out_vec[],
163 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100164{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000165 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100166 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100167
Antonio de Angelis4743e672019-04-11 11:38:48 +0100168 if ((in_len != 1) || (out_len != 2)) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000169 return PSA_CONNECTION_REFUSED;
170 }
171
Antonio de Angelis4743e672019-04-11 11:38:48 +0100172 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
173 (out_vec[0].len != sizeof(uint32_t))) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000174 return PSA_CONNECTION_REFUSED;
175 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100176 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100177 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100178 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000179 uint8_t *mac = out_vec[1].base;
180 size_t mac_size = out_vec[1].len;
181
Antonio de Angelis4743e672019-04-11 11:38:48 +0100182 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100183 *handle_out = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100184
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000185 /* Initialise mac_length to zero */
186 out_vec[1].len = 0;
187
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100188 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000189 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100190 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100191 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000192 if (status != PSA_SUCCESS) {
193 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100194 }
195
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100196 status = psa_mac_sign_finish(operation, mac, mac_size, &out_vec[1].len);
197 if (status != PSA_SUCCESS) {
198 /* Release the operation context, ignore if the operation fails. */
199 (void)tfm_crypto_operation_release(handle_out);
200 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100201 }
202
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100203 status = tfm_crypto_operation_release(handle_out);
204
Antonio de Angelis4743e672019-04-11 11:38:48 +0100205 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100206}
207
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000208psa_status_t tfm_crypto_mac_verify_finish(psa_invec in_vec[],
209 size_t in_len,
210 psa_outvec out_vec[],
211 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100212{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000213 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100214 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100215
Antonio de Angelis4743e672019-04-11 11:38:48 +0100216 if ((in_len != 2) || (out_len != 1)) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000217 return PSA_CONNECTION_REFUSED;
218 }
219
Antonio de Angelis4743e672019-04-11 11:38:48 +0100220 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
221 (out_vec[0].len != sizeof(uint32_t))) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000222 return PSA_CONNECTION_REFUSED;
223 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100224 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100225 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100226 uint32_t *handle_out = out_vec[0].base;
227 const uint8_t *mac = in_vec[1].base;
228 size_t mac_length = in_vec[1].len;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000229
Antonio de Angelis4743e672019-04-11 11:38:48 +0100230 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100231 *handle_out = iov->op_handle;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100232
233 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000234 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100235 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100236 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000237 if (status != PSA_SUCCESS) {
238 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100239 }
240
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100241 status = psa_mac_verify_finish(operation, mac, mac_length);
242 if (status != PSA_SUCCESS) {
243 /* Release the operation context, ignore if the operation fails. */
244 (void)tfm_crypto_operation_release(handle_out);
245 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100246 }
247
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100248 status = tfm_crypto_operation_release(handle_out);
249
250 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100251}
252
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000253psa_status_t tfm_crypto_mac_abort(psa_invec in_vec[],
254 size_t in_len,
255 psa_outvec out_vec[],
256 size_t out_len)
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100257{
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000258 psa_status_t status = PSA_SUCCESS;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100259 psa_mac_operation_t *operation = NULL;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100260
Antonio de Angelis4743e672019-04-11 11:38:48 +0100261 if ((in_len != 1) || (out_len != 1)) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000262 return PSA_CONNECTION_REFUSED;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100263 }
264
Antonio de Angelis4743e672019-04-11 11:38:48 +0100265 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
266 (out_vec[0].len != sizeof(uint32_t))) {
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000267 return PSA_CONNECTION_REFUSED;
268 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100269 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100270 uint32_t handle = iov->op_handle;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100271 uint32_t *handle_out = out_vec[0].base;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000272
Antonio de Angelis4743e672019-04-11 11:38:48 +0100273 /* Init the handle in the operation with the one passed from the iov */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100274 *handle_out = iov->op_handle;
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000275
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100276 /* Look up the corresponding operation context */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000277 status = tfm_crypto_operation_lookup(TFM_CRYPTO_MAC_OPERATION,
Antonio de Angelis4743e672019-04-11 11:38:48 +0100278 handle,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100279 (void **)&operation);
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000280 if (status != PSA_SUCCESS) {
281 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100282 }
283
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100284 status = psa_mac_abort(operation);
285
286 if (status != PSA_SUCCESS) {
287 /* Release the operation context, ignore if the operation fails. */
288 (void)tfm_crypto_operation_release(handle_out);
289 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100290 }
291
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100292 status = tfm_crypto_operation_release(handle_out);
293
Antonio de Angelis4743e672019-04-11 11:38:48 +0100294 return status;
Louis Mayencourt7a36f782018-09-24 14:00:57 +0100295}
296/*!@}*/