blob: b0017464d459245452d364840cb3b30f2c77ecc6 [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
Summer Qin4b1d03b2019-07-02 14:56:08 +080011/* FixMe: Use PSA_ERROR_CONNECTION_REFUSED when performing parameter
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +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 */
16#include "tfm_mbedcrypto_include.h"
17
18#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 Angelis25e2b2d2019-04-25 14:49:50 +010021
22/*!
23 * \defgroup public_psa Public functions, PSA
24 *
25 */
26
27/*!@{*/
Antonio de Angelis04debbd2019-10-14 12:12:52 +010028psa_status_t tfm_crypto_sign_hash(psa_invec in_vec[],
29 size_t in_len,
30 psa_outvec out_vec[],
31 size_t out_len)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010032{
Kevin Peng96f802e2019-12-26 16:10:25 +080033#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010034 return PSA_ERROR_NOT_SUPPORTED;
35#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +010036 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 2, out_len, 0, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010037
38 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080039 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010040 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010041
Soby Mathewd8abdfd2020-10-14 10:28:01 +010042 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010043 psa_key_handle_t handle = iov->key_handle;
44 psa_algorithm_t alg = iov->alg;
45 const uint8_t *hash = in_vec[1].base;
46 size_t hash_length = in_vec[1].len;
47 uint8_t *signature = out_vec[0].base;
48 size_t signature_size = out_vec[0].len;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010049 psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL);
50
51 if (status != PSA_SUCCESS) {
52 return status;
53 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010054
Antonio de Angelis04debbd2019-10-14 12:12:52 +010055 return psa_sign_hash(handle, alg, hash, hash_length,
56 signature, signature_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +010057#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010058}
59
Antonio de Angelis04debbd2019-10-14 12:12:52 +010060psa_status_t tfm_crypto_verify_hash(psa_invec in_vec[],
61 size_t in_len,
62 psa_outvec out_vec[],
63 size_t out_len)
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010064{
Kevin Peng96f802e2019-12-26 16:10:25 +080065#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010066 return PSA_ERROR_NOT_SUPPORTED;
67#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +010068 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 0);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010069
70 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +080071 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010072 }
Soby Mathewd8abdfd2020-10-14 10:28:01 +010073
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010074 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
75
76 psa_key_handle_t handle = iov->key_handle;
77 psa_algorithm_t alg = iov->alg;
78 const uint8_t *hash = in_vec[1].base;
79 size_t hash_length = in_vec[1].len;
80 const uint8_t *signature = in_vec[2].base;
81 size_t signature_length = in_vec[2].len;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010082 psa_status_t status = tfm_crypto_check_handle_owner(handle, NULL);
83
84 if (status != PSA_SUCCESS) {
85 return status;
86 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010087
Antonio de Angelis04debbd2019-10-14 12:12:52 +010088 return psa_verify_hash(handle, alg, hash, hash_length,
89 signature, signature_length);
Antonio de Angelis7740b382019-07-16 10:59:25 +010090#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010091}
92
93psa_status_t tfm_crypto_asymmetric_encrypt(psa_invec in_vec[],
94 size_t in_len,
95 psa_outvec out_vec[],
96 size_t out_len)
97{
Kevin Peng96f802e2019-12-26 16:10:25 +080098#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +010099 return PSA_ERROR_NOT_SUPPORTED;
100#else
Jamie Fox707caf72019-05-29 15:14:18 +0100101 psa_status_t status;
102
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100103 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100104
105 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800106 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100107 }
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100108
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100109 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100110 psa_key_handle_t handle = iov->key_handle;
111 psa_algorithm_t alg = iov->alg;
112 const uint8_t *input = in_vec[1].base;
113 size_t input_length = in_vec[1].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100114 const uint8_t *salt = in_vec[2].base;
115 size_t salt_length = in_vec[2].len;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100116 uint8_t *output = out_vec[0].base;
117 size_t output_size = out_vec[0].len;
Jamie Fox707caf72019-05-29 15:14:18 +0100118 psa_key_type_t type;
119 size_t key_bits;
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100120 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100121
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100122 status = tfm_crypto_check_handle_owner(handle, NULL);
123 if (status != PSA_SUCCESS) {
124 return status;
125 }
126
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100127 status = psa_get_key_attributes(handle, &key_attributes);
Jamie Fox707caf72019-05-29 15:14:18 +0100128 if (status != PSA_SUCCESS) {
129 return status;
130 }
131
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100132 key_bits = psa_get_key_bits(&key_attributes);
133 type = psa_get_key_type(&key_attributes);
134
135 psa_reset_key_attributes(&key_attributes);
136
Jamie Fox707caf72019-05-29 15:14:18 +0100137 /* Check that the output buffer is large enough */
138 if (output_size < PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(type, key_bits, alg)) {
139 return PSA_ERROR_BUFFER_TOO_SMALL;
140 }
141
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100142 return psa_asymmetric_encrypt(handle, alg, input, input_length,
143 salt, salt_length,
144 output, output_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100145#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100146}
147
148psa_status_t tfm_crypto_asymmetric_decrypt(psa_invec in_vec[],
149 size_t in_len,
150 psa_outvec out_vec[],
151 size_t out_len)
152{
Kevin Peng96f802e2019-12-26 16:10:25 +0800153#ifdef TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED
Antonio de Angelis7740b382019-07-16 10:59:25 +0100154 return PSA_ERROR_NOT_SUPPORTED;
155#else
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100156
157 CRYPTO_IN_OUT_LEN_VALIDATE(in_len, 1, 3, out_len, 0, 1);
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100158
159 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
Summer Qin4b1d03b2019-07-02 14:56:08 +0800160 return PSA_ERROR_CONNECTION_REFUSED;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100161 }
162 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
163
164 psa_key_handle_t handle = iov->key_handle;
165 psa_algorithm_t alg = iov->alg;
166 const uint8_t *input = in_vec[1].base;
167 size_t input_length = in_vec[1].len;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100168 const uint8_t *salt = in_vec[2].base;
169 size_t salt_length = in_vec[2].len;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100170 uint8_t *output = out_vec[0].base;
171 size_t output_size = out_vec[0].len;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100172 psa_status_t status;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100173
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100174 status = tfm_crypto_check_handle_owner(handle, NULL);
175 if (status != PSA_SUCCESS) {
176 return status;
177 }
178
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100179 return psa_asymmetric_decrypt(handle, alg, input, input_length,
180 salt, salt_length,
181 output, output_size, &(out_vec[0].len));
Antonio de Angelis7740b382019-07-16 10:59:25 +0100182#endif /* TFM_CRYPTO_ASYMMETRIC_MODULE_DISABLED */
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100183}
184/*!@}*/