blob: f70d804b0fae5cffcbf1a605bb4fd525a710be99 [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA ECP layer on top of Mbed TLS crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
27#include "psa_crypto_ecp.h"
Ronald Crone5ca3d82020-11-26 16:36:16 +010028#include "psa_crypto_random_impl.h"
Manuel Pégourié-Gonnardd82a9ed2022-07-18 15:21:37 +020029#include "hash_info.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030
31#include <stdlib.h>
32#include <string.h>
33#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010034
Ronald Cron072722c2020-12-09 16:36:19 +010035#include <mbedtls/ecdsa.h>
Aditya Deshpande3f1606a2022-11-04 16:55:57 +000036#include <mbedtls/ecdh.h>
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include <mbedtls/ecp.h>
38#include <mbedtls/error.h>
39
Ronald Cron0266cfe2021-03-13 18:50:11 +010040#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
41 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
42 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
43 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
Ronald Cronb5399a82020-12-10 09:35:33 +010044 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010045psa_status_t mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010046 psa_key_type_t type, size_t curve_bits,
47 const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_ecp_keypair **p_ecp)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010049{
50 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
51 psa_status_t status;
52 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010053 size_t curve_bytes = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +010054 int explicit_bits = (curve_bits != 0);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
57 PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010058 /* A Weierstrass public key is represented as:
59 * - The byte 0x04;
60 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
61 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
62 * So its data length is 2m+1 where m is the curve size in bits.
63 */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if ((data_length & 1) == 0) {
65 return PSA_ERROR_INVALID_ARGUMENT;
66 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010067 curve_bytes = data_length / 2;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010068
69 /* Montgomery public keys are represented in compressed format, meaning
Gilles Peskined88ccae2021-02-08 18:39:18 +010070 * their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010071
72 /* Private keys are represented in uncompressed private random integer
Gilles Peskined88ccae2021-02-08 18:39:18 +010073 * format, meaning their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010074 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (explicit_bits) {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010077 /* With an explicit bit-size, the data must have the matching length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) {
79 return PSA_ERROR_INVALID_ARGUMENT;
80 }
81 } else {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010082 /* We need to infer the bit-size from the data. Since the only
83 * information we have is the length in bytes, the value of curve_bits
84 * at this stage is rounded up to the nearest multiple of 8. */
Gilles Peskine449bd832023-01-11 14:50:10 +010085 curve_bits = PSA_BYTES_TO_BITS(curve_bytes);
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010086 }
87
Ronald Cron00b7bfc2020-11-25 15:25:26 +010088 /* Allocate and initialize a key representation. */
Gilles Peskine449bd832023-01-11 14:50:10 +010089 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
90 if (ecp == NULL) {
91 return PSA_ERROR_INSUFFICIENT_MEMORY;
92 }
93 mbedtls_ecp_keypair_init(ecp);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010094
95 /* Load the group. */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 grp_id = mbedtls_ecc_group_of_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type),
97 curve_bits, !explicit_bits);
98 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010099 /* We can't distinguish between a nonsensical family/size combination
100 * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a
101 * well-regarded curve that Mbed TLS just doesn't know about (which
102 * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how
103 * curves that Mbed TLS knows about but for which support is disabled
104 * at build time, return NOT_SUPPORTED. */
105 status = PSA_ERROR_NOT_SUPPORTED;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100106 goto exit;
107 }
108
109 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_ecp_group_load(&ecp->grp, grp_id));
111 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100114
115 /* Load the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100117 /* Load the public value. */
118 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q,
120 data,
121 data_length));
122 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100125
126 /* Check that the point is on the curve. */
127 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q));
129 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 }
132 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100133 /* Load and validate the secret value. */
134 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_ecp_read_key(ecp->grp.id,
136 ecp,
137 data,
138 data_length));
139 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100142 }
143
144 *p_ecp = ecp;
145exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (status != PSA_SUCCESS) {
147 mbedtls_ecp_keypair_free(ecp);
148 mbedtls_free(ecp);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100149 }
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100152}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100153#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
154 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
155 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
156 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
Ronald Cronb5399a82020-12-10 09:35:33 +0100157 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100158
Ronald Cron0266cfe2021-03-13 18:50:11 +0100159#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
160 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crond6ec3032020-11-27 18:54:57 +0100161
Ronald Cron0266cfe2021-03-13 18:50:11 +0100162psa_status_t mbedtls_psa_ecp_import_key(
Ronald Crond6ec3032020-11-27 18:54:57 +0100163 const psa_key_attributes_t *attributes,
164 const uint8_t *data, size_t data_length,
165 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 size_t *key_buffer_length, size_t *bits)
Ronald Crond6ec3032020-11-27 18:54:57 +0100167{
168 psa_status_t status;
169 mbedtls_ecp_keypair *ecp = NULL;
170
171 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
173 attributes->core.bits,
174 data,
175 data_length,
176 &ecp);
177 if (status != PSA_SUCCESS) {
Ronald Crond6ec3032020-11-27 18:54:57 +0100178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 }
Ronald Crond6ec3032020-11-27 18:54:57 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) ==
182 PSA_ECC_FAMILY_MONTGOMERY) {
Ronald Crond6ec3032020-11-27 18:54:57 +0100183 *bits = ecp->grp.nbits + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 } else {
Ronald Crond6ec3032020-11-27 18:54:57 +0100185 *bits = ecp->grp.nbits;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 }
Ronald Crond6ec3032020-11-27 18:54:57 +0100187
188 /* Re-export the data to PSA export format. There is currently no support
189 * for other input formats then the export format, so this is a 1-1
190 * copy operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 status = mbedtls_psa_ecp_export_key(attributes->core.type,
192 ecp,
193 key_buffer,
194 key_buffer_size,
195 key_buffer_length);
Ronald Crond6ec3032020-11-27 18:54:57 +0100196exit:
197 /* Always free the PK object (will also free contained ECP context) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_ecp_keypair_free(ecp);
199 mbedtls_free(ecp);
Ronald Crond6ec3032020-11-27 18:54:57 +0100200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return status;
Ronald Crond6ec3032020-11-27 18:54:57 +0100202}
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
205 mbedtls_ecp_keypair *ecp,
206 uint8_t *data,
207 size_t data_size,
208 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100209{
210 psa_status_t status;
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100213 /* Check whether the public part is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (mbedtls_ecp_is_zero(&ecp->Q)) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100215 /* Calculate the public key */
216 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
218 mbedtls_psa_get_random,
219 MBEDTLS_PSA_RANDOM_STATE));
220 if (status != PSA_SUCCESS) {
221 return status;
222 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100223 }
224
225 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q,
227 MBEDTLS_ECP_PF_UNCOMPRESSED,
228 data_length,
229 data,
230 data_size));
231 if (status != PSA_SUCCESS) {
232 memset(data, 0, data_size);
233 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return status;
236 } else {
237 if (data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits)) {
238 return PSA_ERROR_BUFFER_TOO_SMALL;
239 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100240
241 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_ecp_write_key(ecp,
243 data,
244 PSA_BITS_TO_BYTES(ecp->grp.nbits)));
245 if (status == PSA_SUCCESS) {
246 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
247 } else {
248 memset(data, 0, data_size);
249 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100252 }
253}
254
Ronald Cron0266cfe2021-03-13 18:50:11 +0100255psa_status_t mbedtls_psa_ecp_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100256 const psa_key_attributes_t *attributes,
257 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100259{
260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
261 mbedtls_ecp_keypair *ecp = NULL;
262
263 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100264 attributes->core.type, attributes->core.bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 key_buffer, key_buffer_size, &ecp);
266 if (status != PSA_SUCCESS) {
267 return status;
268 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100269
270 status = mbedtls_psa_ecp_export_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
272 PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)),
273 ecp, data, data_size, data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_ecp_keypair_free(ecp);
276 mbedtls_free(ecp);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100279}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100280#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
281 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100282
Ronald Cron0266cfe2021-03-13 18:50:11 +0100283#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
284psa_status_t mbedtls_psa_ecp_generate_key(
Ronald Cron7023db52020-11-20 18:17:42 +0100285 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron7023db52020-11-20 18:17:42 +0100287{
288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
290
291 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 attributes->core.type);
Ronald Cron7023db52020-11-20 18:17:42 +0100293 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 mbedtls_ecc_group_of_psa(curve, attributes->core.bits, 0);
Ronald Cron7023db52020-11-20 18:17:42 +0100295
296 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 mbedtls_ecp_curve_info_from_grp_id(grp_id);
Ronald Cron7023db52020-11-20 18:17:42 +0100298 mbedtls_ecp_keypair ecp;
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (attributes->domain_parameters_size != 0) {
301 return PSA_ERROR_NOT_SUPPORTED;
302 }
Ronald Cron7023db52020-11-20 18:17:42 +0100303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) {
305 return PSA_ERROR_NOT_SUPPORTED;
306 }
Ronald Cron7023db52020-11-20 18:17:42 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 mbedtls_ecp_keypair_init(&ecp);
309 ret = mbedtls_ecp_gen_key(grp_id, &ecp,
310 mbedtls_psa_get_random,
311 MBEDTLS_PSA_RANDOM_STATE);
312 if (ret != 0) {
313 mbedtls_ecp_keypair_free(&ecp);
314 return mbedtls_to_psa_error(ret);
Ronald Cron7023db52020-11-20 18:17:42 +0100315 }
316
317 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 mbedtls_ecp_write_key(&ecp, key_buffer, key_buffer_size));
Ronald Cron7023db52020-11-20 18:17:42 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ecp_keypair_free(&ecp);
Ronald Cron7023db52020-11-20 18:17:42 +0100321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (status == PSA_SUCCESS) {
Ronald Cron7023db52020-11-20 18:17:42 +0100323 *key_buffer_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 }
Ronald Cron7023db52020-11-20 18:17:42 +0100325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return status;
Ronald Cron7023db52020-11-20 18:17:42 +0100327}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100328#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron7023db52020-11-20 18:17:42 +0100329
Ronald Cron072722c2020-12-09 16:36:19 +0100330/****************************************************************/
331/* ECDSA sign/verify */
332/****************************************************************/
333
Ronald Cron0266cfe2021-03-13 18:50:11 +0100334#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
335 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
336psa_status_t mbedtls_psa_ecdsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100337 const psa_key_attributes_t *attributes,
338 const uint8_t *key_buffer, size_t key_buffer_size,
339 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron072722c2020-12-09 16:36:19 +0100341{
342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
343 mbedtls_ecp_keypair *ecp = NULL;
344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
345 size_t curve_bytes;
346 mbedtls_mpi r, s;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
349 attributes->core.bits,
350 key_buffer,
351 key_buffer_size,
352 &ecp);
353 if (status != PSA_SUCCESS) {
354 return status;
355 }
Ronald Cron072722c2020-12-09 16:36:19 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
358 mbedtls_mpi_init(&r);
359 mbedtls_mpi_init(&s);
Ronald Cron072722c2020-12-09 16:36:19 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (signature_size < 2 * curve_bytes) {
Ronald Cron072722c2020-12-09 16:36:19 +0100362 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
363 goto cleanup;
364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100367#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
369 mbedtls_md_type_t md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
370 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext(
371 &ecp->grp, &r, &s,
372 &ecp->d, hash,
373 hash_length, md_alg,
374 mbedtls_psa_get_random,
375 MBEDTLS_PSA_RANDOM_STATE));
Ronald Cron9103d492021-03-04 11:26:03 +0100376#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
378 goto cleanup;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100379#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 } else {
Ronald Cron072722c2020-12-09 16:36:19 +0100381 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d,
383 hash, hash_length,
384 mbedtls_psa_get_random,
385 MBEDTLS_PSA_RANDOM_STATE));
Ronald Cron072722c2020-12-09 16:36:19 +0100386 }
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r,
389 signature,
390 curve_bytes));
391 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s,
392 signature + curve_bytes,
393 curve_bytes));
Ronald Cron072722c2020-12-09 16:36:19 +0100394cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_mpi_free(&r);
396 mbedtls_mpi_free(&s);
397 if (ret == 0) {
Ronald Cron072722c2020-12-09 16:36:19 +0100398 *signature_length = 2 * curve_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 }
Ronald Cron072722c2020-12-09 16:36:19 +0100400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_ecp_keypair_free(ecp);
402 mbedtls_free(ecp);
Ronald Cron072722c2020-12-09 16:36:19 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return mbedtls_to_psa_error(ret);
Ronald Cron072722c2020-12-09 16:36:19 +0100405}
406
Paul Elliott2c9843f2023-02-15 17:32:42 +0000407psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp)
Paul Elliotteefe4722023-02-06 15:59:09 +0000408{
409 int ret = 0;
410
411 /* Check whether the public part is loaded. If not, load it. */
412 if (mbedtls_ecp_is_zero(&ecp->Q)) {
413 ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q,
414 &ecp->d, &ecp->grp.G,
415 mbedtls_psa_get_random,
416 MBEDTLS_PSA_RANDOM_STATE);
417 }
418
Paul Elliott2c9843f2023-02-15 17:32:42 +0000419 return mbedtls_to_psa_error(ret);
Paul Elliotteefe4722023-02-06 15:59:09 +0000420}
421
Ronald Cron0266cfe2021-03-13 18:50:11 +0100422psa_status_t mbedtls_psa_ecdsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100423 const psa_key_attributes_t *attributes,
424 const uint8_t *key_buffer, size_t key_buffer_size,
425 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 const uint8_t *signature, size_t signature_length)
Ronald Cron072722c2020-12-09 16:36:19 +0100427{
428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
429 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron072722c2020-12-09 16:36:19 +0100430 size_t curve_bytes;
431 mbedtls_mpi r, s;
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 (void) alg;
Ronald Cron072722c2020-12-09 16:36:19 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
436 attributes->core.bits,
437 key_buffer,
438 key_buffer_size,
439 &ecp);
440 if (status != PSA_SUCCESS) {
441 return status;
442 }
Ronald Cron072722c2020-12-09 16:36:19 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
445 mbedtls_mpi_init(&r);
446 mbedtls_mpi_init(&s);
Ronald Cron072722c2020-12-09 16:36:19 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (signature_length != 2 * curve_bytes) {
Paul Elliott2c9843f2023-02-15 17:32:42 +0000449 status = PSA_ERROR_INVALID_SIGNATURE;
Ronald Cron072722c2020-12-09 16:36:19 +0100450 goto cleanup;
451 }
452
Paul Elliott2c9843f2023-02-15 17:32:42 +0000453 status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r,
454 signature,
455 curve_bytes));
456 if (status != PSA_SUCCESS) {
457 goto cleanup;
458 }
Ronald Cron072722c2020-12-09 16:36:19 +0100459
Paul Elliott2c9843f2023-02-15 17:32:42 +0000460 status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s,
461 signature + curve_bytes,
462 curve_bytes));
463 if (status != PSA_SUCCESS) {
464 goto cleanup;
465 }
Paul Elliotteefe4722023-02-06 15:59:09 +0000466
Paul Elliott2c9843f2023-02-15 17:32:42 +0000467 status = mbedtls_psa_ecp_load_public_part(ecp);
468 if (status != PSA_SUCCESS) {
469 goto cleanup;
470 }
Ronald Cron072722c2020-12-09 16:36:19 +0100471
Paul Elliott2c9843f2023-02-15 17:32:42 +0000472 status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash,
473 hash_length, &ecp->Q,
474 &r, &s));
Ronald Cron072722c2020-12-09 16:36:19 +0100475cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_mpi_free(&r);
477 mbedtls_mpi_free(&s);
478 mbedtls_ecp_keypair_free(ecp);
479 mbedtls_free(ecp);
Ronald Cron072722c2020-12-09 16:36:19 +0100480
Paul Elliott2c9843f2023-02-15 17:32:42 +0000481 return status;
Ronald Cron072722c2020-12-09 16:36:19 +0100482}
483
Ronald Cron0266cfe2021-03-13 18:50:11 +0100484#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Ronald Cronb5399a82020-12-10 09:35:33 +0100485 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
486
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000487/****************************************************************/
488/* ECDH Key Agreement */
489/****************************************************************/
Aditya Deshpande5567c662022-11-07 10:43:29 +0000490
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000491#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Aditya Deshpande5567c662022-11-07 10:43:29 +0000492psa_status_t mbedtls_psa_key_agreement_ecdh(
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000493 const psa_key_attributes_t *attributes,
494 const uint8_t *key_buffer, size_t key_buffer_size,
495 psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length,
496 uint8_t *shared_secret, size_t shared_secret_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 size_t *shared_secret_length)
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000498{
Aditya Deshpandeb6bc7522022-11-29 16:53:29 +0000499 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->core.type) ||
501 !PSA_ALG_IS_ECDH(alg)) {
502 return PSA_ERROR_INVALID_ARGUMENT;
503 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000504 mbedtls_ecp_keypair *ecp = NULL;
Aditya Deshpandeb6bc7522022-11-29 16:53:29 +0000505 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 attributes->core.type,
507 attributes->core.bits,
508 key_buffer,
509 key_buffer_size,
510 &ecp);
511 if (status != PSA_SUCCESS) {
512 return status;
513 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000514 mbedtls_ecp_keypair *their_key = NULL;
515 mbedtls_ecdh_context ecdh;
516 size_t bits = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ecp->grp.id, &bits);
518 mbedtls_ecdh_init(&ecdh);
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000519
520 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
522 bits,
523 peer_key,
524 peer_key_length,
525 &their_key);
526 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000527 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000529
530 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
532 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000533 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000535 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_ecdh_get_params(&ecdh, ecp, MBEDTLS_ECDH_OURS));
537 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000538 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000540
541 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 mbedtls_ecdh_calc_secret(&ecdh,
543 shared_secret_length,
544 shared_secret, shared_secret_size,
545 mbedtls_psa_get_random,
546 MBEDTLS_PSA_RANDOM_STATE));
547 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000548 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 }
550 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000551 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000553exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (status != PSA_SUCCESS) {
555 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
556 }
557 mbedtls_ecdh_free(&ecdh);
558 mbedtls_ecp_keypair_free(their_key);
559 mbedtls_free(their_key);
560 mbedtls_ecp_keypair_free(ecp);
561 mbedtls_free(ecp);
562 return status;
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000563}
564#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
565
566
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100567#endif /* MBEDTLS_PSA_CRYPTO_C */