blob: 9f43eb4ae37be20ba612bbf496ba097c1d721947 [file] [log] [blame]
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stddef.h>
9#include <stdint.h>
10
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010011#include "tfm_mbedcrypto_include.h"
12
13#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 Angelis25e2b2d2019-04-25 14:49:50 +010016
17/*!
18 * \defgroup public_psa Public functions, PSA
19 *
20 */
21
22/*!@{*/
Antonio de Angelis04debbd2019-10-14 12:12:52 +010023psa_status_t tfm_crypto_sign_hash(psa_invec in_vec[],
24 size_t in_len,
25 psa_outvec out_vec[],
26 size_t out_len)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010027{
Kevin Peng96f802e2019-12-26 16:10:25 +080028#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010029 return PSA_ERROR_NOT_SUPPORTED;
30#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +010031 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010032
33 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010034 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010035 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010036
Soby Mathewd8abdfd2020-10-14 10:28:01 +010037 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010038 psa_key_handle_t handle = iov->key_handle;
39 psa_algorithm_t alg = iov->alg;
40 const uint8_t *hash = in_vec[1].base;
41 size_t hash_length = in_vec[1].len;
42 uint8_t *signature = out_vec[0].base;
43 size_t signature_size = out_vec[0].len;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010044 psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL);
45
46 if (status != PSA_SUCCESS) {
47 return status;
48 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010049
Antonio de Angelis04debbd2019-10-14 12:12:52 +010050 return psa_sign_hash(handle, alg, hash, hash_length,
51 signature, signature_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +010052#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010053}
54
Antonio de Angelis04debbd2019-10-14 12:12:52 +010055psa_status_t tfm_crypto_verify_hash(psa_invec in_vec[],
56 size_t in_len,
57 psa_outvec out_vec[],
58 size_t out_len)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010059{
Kevin Peng96f802e2019-12-26 16:10:25 +080060#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010061 return PSA_ERROR_NOT_SUPPORTED;
62#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +010063 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010064
65 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +010066 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010067 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +010068
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010069 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
70
71 psa_key_handle_t handle = iov->key_handle;
72 psa_algorithm_t alg = iov->alg;
73 const uint8_t *hash = in_vec[1].base;
74 size_t hash_length = in_vec[1].len;
75 const uint8_t *signature = in_vec[2].base;
76 size_t signature_length = in_vec[2].len;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010077 psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL);
78
79 if (status != PSA_SUCCESS) {
80 return status;
81 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010082
Antonio de Angelis04debbd2019-10-14 12:12:52 +010083 return psa_verify_hash(handle, alg, hash, hash_length,
84 signature, signature_length);
Antonio de Angelis7740b382019-07-16 10:59:25 +010085#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010086}
87
88psa_status_t tfm_crypto_asymmetric_encrypt(psa_invec in_vec[],
89 size_t in_len,
90 psa_outvec out_vec[],
91 size_t out_len)
92{
Kevin Peng96f802e2019-12-26 16:10:25 +080093#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010094 return PSA_ERROR_NOT_SUPPORTED;
95#else
Jamie Fox707caf72019-05-29 15:14:18 +010096 psa_status_t status;
97
Soby Mathewd8abdfd2020-10-14 10:28:01 +010098 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010099
100 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100101 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100102 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100103
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100104 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100105 psa_key_handle_t handle = iov->key_handle;
106 psa_algorithm_t alg = iov->alg;
107 const uint8_t *input = in_vec[1].base;
108 size_t input_length = in_vec[1].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100109 const uint8_t *salt = in_vec[2].base;
110 size_t salt_length = in_vec[2].len;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100111 uint8_t *output = out_vec[0].base;
112 size_t output_size = out_vec[0].len;
Jamie Fox707caf72019-05-29 15:14:18 +0100113 psa_key_type_t type;
114 size_t key_bits;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100115 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100116
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100117 status = tfm_crypto_check_handle_owner(handle, NULL);
118 if (status != PSA_SUCCESS) {
119 return status;
120 }
121
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100122 status = psa_get_key_attributes(handle, &key_attributes);
Jamie Fox707caf72019-05-29 15:14:18 +0100123 if (status != PSA_SUCCESS) {
124 return status;
125 }
126
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100127 key_bits = psa_get_key_bits(&key_attributes);
128 type = psa_get_key_type(&key_attributes);
129
130 psa_reset_key_attributes(&key_attributes);
131
Jamie Fox707caf72019-05-29 15:14:18 +0100132 /* Check that the output buffer is large enough */
133 if (output_size < PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(type, key_bits, alg)) {
134 return PSA_ERROR_BUFFER_TOO_SMALL;
135 }
136
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100137 return psa_asymmetric_encrypt(handle, alg, input, input_length,
138 salt, salt_length,
139 output, output_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100140#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100141}
142
143psa_status_t tfm_crypto_asymmetric_decrypt(psa_invec in_vec[],
144 size_t in_len,
145 psa_outvec out_vec[],
146 size_t out_len)
147{
Kevin Peng96f802e2019-12-26 16:10:25 +0800148#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100149 return PSA_ERROR_NOT_SUPPORTED;
150#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100151
152 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100153
154 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Soby Mathewc6e89362020-10-19 16:55:16 +0100155 return PSA_ERROR_PROGRAMMER_ERROR;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100156 }
157 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
158
159 psa_key_handle_t handle = iov->key_handle;
160 psa_algorithm_t alg = iov->alg;
161 const uint8_t *input = in_vec[1].base;
162 size_t input_length = in_vec[1].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100163 const uint8_t *salt = in_vec[2].base;
164 size_t salt_length = in_vec[2].len;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100165 uint8_t *output = out_vec[0].base;
166 size_t output_size = out_vec[0].len;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100167 psa_status_t status;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100168
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100169 status = tfm_crypto_check_handle_owner(handle, NULL);
170 if (status != PSA_SUCCESS) {
171 return status;
172 }
173
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100174 return psa_asymmetric_decrypt(handle, alg, input, input_length,
175 salt, salt_length,
176 output, output_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100177#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100178}
179/*!@}*/