blob: cd58b11f7d032fc8b1323642b44988601171f981 [file] [log] [blame]
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +01001/*
2 * Copyright (c) 2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stddef.h>
9#include <stdint.h>
10
11/* 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 */
16#include "tfm_mbedcrypto_include.h"
17
18#include "tfm_crypto_api.h"
19#include "tfm_crypto_defs.h"
20
21/*!
22 * \defgroup public_psa Public functions, PSA
23 *
24 */
25
26/*!@{*/
27psa_status_t tfm_crypto_asymmetric_sign(psa_invec in_vec[],
28 size_t in_len,
29 psa_outvec out_vec[],
30 size_t out_len)
31{
32 if ((in_len != 2) || (out_len != 1)) {
33 return PSA_CONNECTION_REFUSED;
34 }
35
36 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
37 return PSA_CONNECTION_REFUSED;
38 }
39 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
40
41 psa_key_handle_t handle = iov->key_handle;
42 psa_algorithm_t alg = iov->alg;
43 const uint8_t *hash = in_vec[1].base;
44 size_t hash_length = in_vec[1].len;
45 uint8_t *signature = out_vec[0].base;
46 size_t signature_size = out_vec[0].len;
47
48 return psa_asymmetric_sign(handle, alg, hash, hash_length,
49 signature, signature_size, &(out_vec[0].len));
50}
51
52psa_status_t tfm_crypto_asymmetric_verify(psa_invec in_vec[],
53 size_t in_len,
54 psa_outvec out_vec[],
55 size_t out_len)
56{
57 if ((in_len != 3) || (out_len != 0)) {
58 return PSA_CONNECTION_REFUSED;
59 }
60
61 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
62 return PSA_CONNECTION_REFUSED;
63 }
64 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
65
66 psa_key_handle_t handle = iov->key_handle;
67 psa_algorithm_t alg = iov->alg;
68 const uint8_t *hash = in_vec[1].base;
69 size_t hash_length = in_vec[1].len;
70 const uint8_t *signature = in_vec[2].base;
71 size_t signature_length = in_vec[2].len;
72
73 return psa_asymmetric_verify(handle, alg, hash, hash_length,
74 signature, signature_length);
75}
76
77psa_status_t tfm_crypto_asymmetric_encrypt(psa_invec in_vec[],
78 size_t in_len,
79 psa_outvec out_vec[],
80 size_t out_len)
81{
Jamie Fox707caf72019-05-29 15:14:18 +010082 psa_status_t status;
83
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +010084 if (!((in_len == 2) || (in_len == 3)) || (out_len != 1)) {
85 return PSA_CONNECTION_REFUSED;
86 }
87
88 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
89 return PSA_CONNECTION_REFUSED;
90 }
91 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
92
93 psa_key_handle_t handle = iov->key_handle;
94 psa_algorithm_t alg = iov->alg;
95 const uint8_t *input = in_vec[1].base;
96 size_t input_length = in_vec[1].len;
97 const uint8_t *salt = NULL;
98 size_t salt_length = 0;
99 uint8_t *output = out_vec[0].base;
100 size_t output_size = out_vec[0].len;
Jamie Fox707caf72019-05-29 15:14:18 +0100101 psa_key_type_t type;
102 size_t key_bits;
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100103
104 if (in_len == 3) {
105 salt = in_vec[2].base;
106 salt_length = in_vec[2].len;
107 }
108
Jamie Fox707caf72019-05-29 15:14:18 +0100109 status = psa_get_key_information(handle, &type, &key_bits);
110 if (status != PSA_SUCCESS) {
111 return status;
112 }
113
114 /* Check that the output buffer is large enough */
115 if (output_size < PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(type, key_bits, alg)) {
116 return PSA_ERROR_BUFFER_TOO_SMALL;
117 }
118
Antonio de Angelis25e2b2d2019-04-25 14:49:50 +0100119 return psa_asymmetric_encrypt(handle, alg, input, input_length,
120 salt, salt_length,
121 output, output_size, &(out_vec[0].len));
122}
123
124psa_status_t tfm_crypto_asymmetric_decrypt(psa_invec in_vec[],
125 size_t in_len,
126 psa_outvec out_vec[],
127 size_t out_len)
128{
129 if (!((in_len == 2) || (in_len == 3)) || (out_len != 1)) {
130 return PSA_CONNECTION_REFUSED;
131 }
132
133 if ((in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec))) {
134 return PSA_CONNECTION_REFUSED;
135 }
136 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
137
138 psa_key_handle_t handle = iov->key_handle;
139 psa_algorithm_t alg = iov->alg;
140 const uint8_t *input = in_vec[1].base;
141 size_t input_length = in_vec[1].len;
142 const uint8_t *salt = NULL;
143 size_t salt_length = 0;
144 uint8_t *output = out_vec[0].base;
145 size_t output_size = out_vec[0].len;
146
147 if (in_len == 3) {
148 salt = in_vec[2].base;
149 salt_length = in_vec[2].len;
150 }
151
152 return psa_asymmetric_decrypt(handle, alg, input, input_length,
153 salt, salt_length,
154 output, output_size, &(out_vec[0].len));
155}
156/*!@}*/