blob: 1850b86a0e7c12a6b6e4c38a71c5d7623ef409d6 [file] [log] [blame]
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001/*
Maulik Patel28659c42021-01-06 14:09:22 +00002 * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stddef.h>
9#include <stdint.h>
10
Antonio de Angelis04debbd2019-10-14 12:12:52 +010011#include "tfm_mbedcrypto_include.h"
12
Jamie Fox044bd8e2020-02-20 13:34:57 +000013/* Required for mbedtls_calloc in tfm_crypto_huk_derivation_input_bytes */
14#include "mbedtls/platform.h"
15
Antonio de Angelis04debbd2019-10-14 12:12:52 +010016#include "tfm_crypto_api.h"
17#include "tfm_crypto_defs.h"
Jamie Fox044bd8e2020-02-20 13:34:57 +000018#include "tfm_memory_utils.h"
19
Mingyang Sun8a19e7a2020-06-04 15:36:58 +080020#include "tfm_plat_crypto_keys.h"
Soby Mathewd8abdfd2020-10-14 10:28:01 +010021#include "tfm_crypto_private.h"
Jamie Fox044bd8e2020-02-20 13:34:57 +000022
Kevin Pengc6d74502020-03-04 16:55:37 +080023#ifdef TFM_PARTITION_TEST_PS
Jamie Fox044bd8e2020-02-20 13:34:57 +000024#include "psa_manifest/pid.h"
Kevin Pengc6d74502020-03-04 16:55:37 +080025#endif /* TFM_PARTITION_TEST_PS */
Jamie Fox044bd8e2020-02-20 13:34:57 +000026
27#ifndef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
28static psa_status_t tfm_crypto_huk_derivation_setup(
29 psa_key_derivation_operation_t *operation,
30 psa_algorithm_t alg)
31{
Summer Qin359167d2021-07-05 18:11:50 +080032 operation->MBEDTLS_PRIVATE(alg) = TFM_CRYPTO_ALG_HUK_DERIVATION;
Jamie Fox044bd8e2020-02-20 13:34:57 +000033 return PSA_SUCCESS;
34}
35
36static psa_status_t tfm_crypto_huk_derivation_input_bytes(
37 psa_key_derivation_operation_t *operation,
38 psa_key_derivation_step_t step,
39 const uint8_t *data,
40 size_t data_length)
41{
42 psa_status_t status;
43 int32_t partition_id;
Summer Qin359167d2021-07-05 18:11:50 +080044 psa_tls12_prf_key_derivation_t *tls12_prf;
Jamie Fox044bd8e2020-02-20 13:34:57 +000045
46 if (step != PSA_KEY_DERIVATION_INPUT_LABEL) {
47 return PSA_ERROR_INVALID_ARGUMENT;
48 }
49
50 /* Concatenate the caller's partition ID with the supplied label to prevent
51 * two different partitions from deriving the same key.
52 */
53 status = tfm_crypto_get_caller_id(&partition_id);
54 if (status != PSA_SUCCESS) {
55 return status;
56 }
57
Kevin Pengc6d74502020-03-04 16:55:37 +080058#ifdef TFM_PARTITION_TEST_PS
59 /* The PS tests run some operations under the wrong partition ID - this
Jamie Fox044bd8e2020-02-20 13:34:57 +000060 * causes the key derivation to change.
61 */
Kevin Pengc6d74502020-03-04 16:55:37 +080062 if (partition_id == TFM_SP_PS_TEST) {
63 partition_id = TFM_SP_PS;
Jamie Fox044bd8e2020-02-20 13:34:57 +000064 }
Kevin Pengc6d74502020-03-04 16:55:37 +080065#endif /* TFM_PARTITION_TEST_PS */
Jamie Fox044bd8e2020-02-20 13:34:57 +000066
67 /* Put the label in the tls12_prf ctx to make it available in the output key
68 * step.
69 */
Summer Qin359167d2021-07-05 18:11:50 +080070 tls12_prf = &(operation->MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(tls12_prf));
71 tls12_prf->MBEDTLS_PRIVATE(label) =
72 mbedtls_calloc(1, sizeof(partition_id) + data_length);
73 if (tls12_prf->MBEDTLS_PRIVATE(label) == NULL) {
Jamie Fox044bd8e2020-02-20 13:34:57 +000074 return PSA_ERROR_INSUFFICIENT_MEMORY;
75 }
Summer Qin359167d2021-07-05 18:11:50 +080076 (void)tfm_memcpy(tls12_prf->MBEDTLS_PRIVATE(label), &partition_id,
Jamie Fox044bd8e2020-02-20 13:34:57 +000077 sizeof(partition_id));
Summer Qin359167d2021-07-05 18:11:50 +080078 (void)tfm_memcpy(tls12_prf->MBEDTLS_PRIVATE(label) + sizeof(partition_id),
Jamie Fox044bd8e2020-02-20 13:34:57 +000079 data, data_length);
Summer Qin359167d2021-07-05 18:11:50 +080080 tls12_prf->MBEDTLS_PRIVATE(label_length) = sizeof(partition_id) +
81 data_length;
Jamie Fox044bd8e2020-02-20 13:34:57 +000082
83 return PSA_SUCCESS;
84}
85
86static psa_status_t tfm_crypto_huk_derivation_output_key(
87 const psa_key_attributes_t *attributes,
88 psa_key_derivation_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +000089 mbedtls_svc_key_id_t *key_id)
Jamie Fox044bd8e2020-02-20 13:34:57 +000090{
91 enum tfm_plat_err_t err;
92 size_t bytes = PSA_BITS_TO_BYTES(psa_get_key_bits(attributes));
Summer Qin359167d2021-07-05 18:11:50 +080093 psa_tls12_prf_key_derivation_t *tls12_prf =
94 &(operation->MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(tls12_prf));
Jamie Fox044bd8e2020-02-20 13:34:57 +000095
Summer Qin359167d2021-07-05 18:11:50 +080096 if (sizeof(tls12_prf->MBEDTLS_PRIVATE(output_block)) < bytes) {
Jamie Fox044bd8e2020-02-20 13:34:57 +000097 return PSA_ERROR_INSUFFICIENT_MEMORY;
98 }
99
100 /* Derive key material from the HUK and output it to the operation buffer */
Summer Qin359167d2021-07-05 18:11:50 +0800101 err = tfm_plat_get_huk_derived_key(tls12_prf->MBEDTLS_PRIVATE(label),
102 tls12_prf->MBEDTLS_PRIVATE(label_length),
Jamie Fox044bd8e2020-02-20 13:34:57 +0000103 NULL, 0,
Summer Qin359167d2021-07-05 18:11:50 +0800104 tls12_prf->MBEDTLS_PRIVATE(output_block),
Jamie Fox044bd8e2020-02-20 13:34:57 +0000105 bytes);
106 if (err != TFM_PLAT_ERR_SUCCESS) {
107 return PSA_ERROR_HARDWARE_FAILURE;
108 }
109
Summer Qin359167d2021-07-05 18:11:50 +0800110 return psa_import_key(attributes, tls12_prf->MBEDTLS_PRIVATE(output_block),
Maulik Patel28659c42021-01-06 14:09:22 +0000111 bytes, key_id);
Jamie Fox044bd8e2020-02-20 13:34:57 +0000112}
113
114static psa_status_t tfm_crypto_huk_derivation_abort(
115 psa_key_derivation_operation_t *operation)
116{
Summer Qin359167d2021-07-05 18:11:50 +0800117 psa_tls12_prf_key_derivation_t *tls12_prf =
118 &(operation->MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(tls12_prf));
119
120 if (tls12_prf->MBEDTLS_PRIVATE(label) != NULL) {
121 (void)tfm_memset(tls12_prf->MBEDTLS_PRIVATE(label), 0,
122 tls12_prf->MBEDTLS_PRIVATE(label_length));
123 mbedtls_free(tls12_prf->MBEDTLS_PRIVATE(label));
Jamie Fox044bd8e2020-02-20 13:34:57 +0000124 }
125
126 (void)tfm_memset(operation, 0, sizeof(*operation));
127
128 return PSA_SUCCESS;
129}
130#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100131
132/*!
133 * \defgroup public_psa Public functions, PSA
134 *
135 */
136
137/*!@{*/
138psa_status_t tfm_crypto_key_derivation_setup(psa_invec in_vec[],
139 size_t in_len,
140 psa_outvec out_vec[],
141 size_t out_len)
142{
143#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
144 return PSA_ERROR_NOT_SUPPORTED;
145#else
146 psa_status_t status = PSA_SUCCESS;
147 psa_key_derivation_operation_t *operation = NULL;
148
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100149 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100150
151 if ((out_vec[0].len != sizeof(uint32_t)) ||
152 (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100153 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100154 }
155 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
156 uint32_t handle = iov->op_handle;
157 uint32_t *handle_out = out_vec[0].base;
158 psa_algorithm_t alg = iov->alg;
159
160 /* Allocate the operation context in the secure world */
161 status = tfm_crypto_operation_alloc(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
162 &handle,
163 (void **)&operation);
164 if (status != PSA_SUCCESS) {
165 return status;
166 }
167
168 *handle_out = handle;
169
Jamie Fox044bd8e2020-02-20 13:34:57 +0000170 if (alg == TFM_CRYPTO_ALG_HUK_DERIVATION) {
171 status = tfm_crypto_huk_derivation_setup(operation, alg);
172 } else {
173 status = psa_key_derivation_setup(operation, alg);
174 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100175 if (status != PSA_SUCCESS) {
176 /* Release the operation context, ignore if the operation fails. */
177 (void)tfm_crypto_operation_release(handle_out);
178 return status;
179 }
180
181 return status;
182#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
183}
184
185psa_status_t tfm_crypto_key_derivation_get_capacity(psa_invec in_vec[],
186 size_t in_len,
187 psa_outvec out_vec[],
188 size_t out_len)
189{
190#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
191 return PSA_ERROR_NOT_SUPPORTED;
192#else
193 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100194
195 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100196
197 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
198 (out_vec[0].len != sizeof(size_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100199 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100200 }
201 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
202
203 uint32_t handle = iov->op_handle;
204 size_t *capacity = out_vec[0].base;
205 psa_key_derivation_operation_t *operation = NULL;
206
207 /* Look up the corresponding operation context */
208 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
209 handle,
210 (void **)&operation);
211 if (status != PSA_SUCCESS) {
212 *capacity = 0;
213 return status;
214 }
215
216 return psa_key_derivation_get_capacity(operation, capacity);
217#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
218}
219
220psa_status_t tfm_crypto_key_derivation_set_capacity(psa_invec in_vec[],
221 size_t in_len,
222 psa_outvec out_vec[],
223 size_t out_len)
224{
225#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
226 return PSA_ERROR_NOT_SUPPORTED;
227#else
228 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100229
230 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100231
232 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100233 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100234 }
235 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
236
237 uint32_t handle = iov->op_handle;
238 size_t capacity = iov->capacity;
239 psa_key_derivation_operation_t *operation = NULL;
240
241 /* Look up the corresponding operation context */
242 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
243 handle,
244 (void **)&operation);
245 if (status != PSA_SUCCESS) {
246 return status;
247 }
248
249 return psa_key_derivation_set_capacity(operation, capacity);
250#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
251}
252
253psa_status_t tfm_crypto_key_derivation_input_bytes(psa_invec in_vec[],
254 size_t in_len,
255 psa_outvec out_vec[],
256 size_t out_len)
257{
258#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
259 return PSA_ERROR_NOT_SUPPORTED;
260#else
261 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100262
263 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 0);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100264
265 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100266 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100267 }
268 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
269
270 uint32_t handle = iov->op_handle;
271 psa_key_derivation_step_t step = iov->step;
272 const uint8_t *data = in_vec[1].base;
273 size_t data_length = in_vec[1].len;
274 psa_key_derivation_operation_t *operation = NULL;
275
276 /* Look up the corresponding operation context */
277 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
278 handle,
279 (void **)&operation);
280 if (status != PSA_SUCCESS) {
281 return status;
282 }
283
Summer Qin359167d2021-07-05 18:11:50 +0800284 if (operation->MBEDTLS_PRIVATE(alg) == TFM_CRYPTO_ALG_HUK_DERIVATION) {
Jamie Fox044bd8e2020-02-20 13:34:57 +0000285 return tfm_crypto_huk_derivation_input_bytes(operation, step, data,
286 data_length);
287 } else {
288 return psa_key_derivation_input_bytes(operation, step, data,
289 data_length);
290 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100291#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
292}
293
294psa_status_t tfm_crypto_key_derivation_output_bytes(psa_invec in_vec[],
295 size_t in_len,
296 psa_outvec out_vec[],
297 size_t out_len)
298{
299#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
300 return PSA_ERROR_NOT_SUPPORTED;
301#else
302 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100303
304 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100305
306 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100307 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100308 }
309 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
310
311 uint32_t handle = iov->op_handle;
312 uint8_t *output = out_vec[0].base;
313 size_t output_length = out_vec[0].len;
314 psa_key_derivation_operation_t *operation = NULL;
315
316 /* Look up the corresponding operation context */
317 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
318 handle,
319 (void **)&operation);
320 if (status != PSA_SUCCESS) {
321 return status;
322 }
323
324 return psa_key_derivation_output_bytes(operation, output, output_length);
325#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
326}
327
328psa_status_t tfm_crypto_key_derivation_input_key(psa_invec in_vec[],
329 size_t in_len,
330 psa_outvec out_vec[],
331 size_t out_len)
332{
333#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
334 return PSA_ERROR_NOT_SUPPORTED;
335#else
336 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100337
338 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 0, 0);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100339
340 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100341 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100342 }
343 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100344 uint32_t handle = iov->op_handle;
Maulik Patel28659c42021-01-06 14:09:22 +0000345 psa_key_id_t key_id = iov->key_id;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100346 psa_key_derivation_step_t step = iov->step;
347 psa_key_derivation_operation_t *operation = NULL;
Maulik Patel28659c42021-01-06 14:09:22 +0000348 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100349
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100350 /* Look up the corresponding operation context */
351 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
352 handle,
353 (void **)&operation);
354 if (status != PSA_SUCCESS) {
355 return status;
356 }
357
Maulik Patel28659c42021-01-06 14:09:22 +0000358 status = tfm_crypto_encode_id_and_owner(key_id, &encoded_key);
359 if (status != PSA_SUCCESS) {
360 return status;
361 }
362
363 return psa_key_derivation_input_key(operation, step, encoded_key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100364#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
365}
366
367psa_status_t tfm_crypto_key_derivation_output_key(psa_invec in_vec[],
368 size_t in_len,
369 psa_outvec out_vec[],
370 size_t out_len)
371{
372#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
373 return PSA_ERROR_NOT_SUPPORTED;
374#else
375 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100376
377 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 2, 2, out_len, 1, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100378
379 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
Soby Mathewd7b79f22020-05-21 15:06:54 +0100380 (in_vec[1].len != sizeof(struct psa_client_key_attributes_s)) ||
Maulik Patel28659c42021-01-06 14:09:22 +0000381 (out_vec[0].len != sizeof(psa_key_id_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100382 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100383 }
384 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
385
386 uint32_t handle = iov->op_handle;
Soby Mathewd7b79f22020-05-21 15:06:54 +0100387 const struct psa_client_key_attributes_s *client_key_attr = in_vec[1].base;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100388 psa_key_derivation_operation_t *operation = NULL;
Maulik Patel28659c42021-01-06 14:09:22 +0000389 psa_key_id_t *key_handle = out_vec[0].base;
Jamie Fox98ab4412020-01-17 17:12:30 +0000390 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
391 int32_t partition_id;
Maulik Patel28659c42021-01-06 14:09:22 +0000392 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100393
394 /* Look up the corresponding operation context */
395 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
396 handle,
397 (void **)&operation);
398 if (status != PSA_SUCCESS) {
399 return status;
400 }
Jamie Fox99360e82020-02-20 16:00:09 +0000401
Jamie Fox98ab4412020-01-17 17:12:30 +0000402 status = tfm_crypto_get_caller_id(&partition_id);
403 if (status != PSA_SUCCESS) {
404 return status;
405 }
406
407 status = tfm_crypto_key_attributes_from_client(client_key_attr,
408 partition_id,
409 &key_attributes);
410 if (status != PSA_SUCCESS) {
411 return status;
412 }
413
Summer Qin359167d2021-07-05 18:11:50 +0800414 if (operation->MBEDTLS_PRIVATE(alg) == TFM_CRYPTO_ALG_HUK_DERIVATION) {
Jamie Fox98ab4412020-01-17 17:12:30 +0000415 status = tfm_crypto_huk_derivation_output_key(&key_attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000416 operation, &encoded_key);
Jamie Fox044bd8e2020-02-20 13:34:57 +0000417 } else {
Jamie Fox98ab4412020-01-17 17:12:30 +0000418 status = psa_key_derivation_output_key(&key_attributes, operation,
Maulik Patel28659c42021-01-06 14:09:22 +0000419 &encoded_key);
Jamie Fox044bd8e2020-02-20 13:34:57 +0000420 }
Maulik Patel28659c42021-01-06 14:09:22 +0000421
David Hu42e77b52021-07-24 21:14:30 +0800422 *key_handle = encoded_key.MBEDTLS_PRIVATE(key_id);
Jamie Fox99360e82020-02-20 16:00:09 +0000423
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100424 return status;
425#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
426}
427
428psa_status_t tfm_crypto_key_derivation_abort(psa_invec in_vec[],
429 size_t in_len,
430 psa_outvec out_vec[],
431 size_t out_len)
432{
433#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
434 return PSA_ERROR_NOT_SUPPORTED;
435#else
436 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100437
438 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 1, out_len, 1, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100439
440 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) ||
441 (out_vec[0].len != sizeof(uint32_t))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100442 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100443 }
444 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
445
446 uint32_t handle = iov->op_handle;
447 uint32_t *handle_out = out_vec[0].base;
448 psa_key_derivation_operation_t *operation = NULL;
449
450 /* Init the handle in the operation with the one passed from the iov */
451 *handle_out = iov->op_handle;
452
453 /* Look up the corresponding operation context */
454 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
455 handle,
456 (void **)&operation);
457 if (status != PSA_SUCCESS) {
458 /* Operation does not exist, so abort has no effect */
459 return PSA_SUCCESS;
460 }
461
462 *handle_out = handle;
463
Summer Qin359167d2021-07-05 18:11:50 +0800464 if (operation->MBEDTLS_PRIVATE(alg) == TFM_CRYPTO_ALG_HUK_DERIVATION) {
Jamie Fox044bd8e2020-02-20 13:34:57 +0000465 status = tfm_crypto_huk_derivation_abort(operation);
466 } else {
467 status = psa_key_derivation_abort(operation);
468 }
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100469 if (status != PSA_SUCCESS) {
470 /* Release the operation context, ignore if the operation fails. */
471 (void)tfm_crypto_operation_release(handle_out);
472 return status;
473 }
474
475 status = tfm_crypto_operation_release(handle_out);
476
477 return status;
478#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
479}
480
481psa_status_t tfm_crypto_key_derivation_key_agreement(psa_invec in_vec[],
482 size_t in_len,
483 psa_outvec out_vec[],
484 size_t out_len)
485{
486#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
487 return PSA_ERROR_NOT_SUPPORTED;
488#else
489 psa_status_t status;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100490
491 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 0);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100492
493 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100494 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100495 }
496 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
497
498 uint32_t handle = iov->op_handle;
Maulik Patel28659c42021-01-06 14:09:22 +0000499 psa_key_id_t private_key = iov->key_id;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100500 const uint8_t *peer_key = in_vec[1].base;
501 size_t peer_key_length = in_vec[1].len;
502 psa_key_derivation_operation_t *operation = NULL;
503 psa_key_derivation_step_t step = iov->step;
Maulik Patel28659c42021-01-06 14:09:22 +0000504 mbedtls_svc_key_id_t encoded_key;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100505
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100506 /* Look up the corresponding operation context */
507 status = tfm_crypto_operation_lookup(TFM_CRYPTO_KEY_DERIVATION_OPERATION,
508 handle,
509 (void **)&operation);
510 if (status != PSA_SUCCESS) {
511 return status;
512 }
513
Maulik Patel28659c42021-01-06 14:09:22 +0000514 status = tfm_crypto_encode_id_and_owner(private_key, &encoded_key);
515 if (status != PSA_SUCCESS) {
516 return status;
517 }
518
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519 return psa_key_derivation_key_agreement(operation, step,
Maulik Patel28659c42021-01-06 14:09:22 +0000520 encoded_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100521 peer_key,
522 peer_key_length);
523#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
524}
525
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100526psa_status_t tfm_crypto_raw_key_agreement(psa_invec in_vec[],
527 size_t in_len,
528 psa_outvec out_vec[],
529 size_t out_len)
530{
531#ifdef TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED
532 return PSA_ERROR_NOT_SUPPORTED;
533#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100534
535 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100536
537 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100538 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100539 }
540 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
541 uint8_t *output = out_vec[0].base;
542 size_t output_size = out_vec[0].len;
543 psa_algorithm_t alg = iov->alg;
Maulik Patel28659c42021-01-06 14:09:22 +0000544 psa_key_id_t private_key = iov->key_id;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100545 const uint8_t *peer_key = in_vec[1].base;
546 size_t peer_key_length = in_vec[1].len;
Maulik Patel28659c42021-01-06 14:09:22 +0000547 mbedtls_svc_key_id_t encoded_key;
David Hu42e77b52021-07-24 21:14:30 +0800548 psa_status_t status;
Maulik Patel28659c42021-01-06 14:09:22 +0000549
550 status = tfm_crypto_encode_id_and_owner(private_key, &encoded_key);
551 if (status != PSA_SUCCESS) {
552 return status;
553 }
554
555 return psa_raw_key_agreement(alg, encoded_key, peer_key, peer_key_length,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100556 output, output_size, &out_vec[0].len);
557#endif /* TFM_CRYPTO_KEY_DERIVATION_MODULE_DISABLED */
558}
559/*!@}*/