blob: 5c77865046a86a5d593b7104bb0f9efd8286d301 [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é-Gonnard2d6d9932023-03-28 11:38:08 +020029#include "md_psa.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
Valerio Setti27c501a2023-06-27 16:58:52 +020040#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) || \
41 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
42 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010043 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
44 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
45 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
Ronald Cronb5399a82020-12-10 09:35:33 +010046 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010047psa_status_t mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010048 psa_key_type_t type, size_t curve_bits,
49 const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_ecp_keypair **p_ecp)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010051{
52 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
53 psa_status_t status;
54 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010055 size_t curve_bytes = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +010056 int explicit_bits = (curve_bits != 0);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
59 PSA_KEY_TYPE_ECC_GET_FAMILY(type) != PSA_ECC_FAMILY_MONTGOMERY) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010060 /* A Weierstrass public key is represented as:
61 * - The byte 0x04;
62 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
63 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
64 * So its data length is 2m+1 where m is the curve size in bits.
65 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if ((data_length & 1) == 0) {
67 return PSA_ERROR_INVALID_ARGUMENT;
68 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010069 curve_bytes = data_length / 2;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010070
71 /* Montgomery public keys are represented in compressed format, meaning
Gilles Peskined88ccae2021-02-08 18:39:18 +010072 * their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010073
74 /* Private keys are represented in uncompressed private random integer
Gilles Peskined88ccae2021-02-08 18:39:18 +010075 * format, meaning their curve_bytes is equal to the amount of input. */
Ronald Cron00b7bfc2020-11-25 15:25:26 +010076 }
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (explicit_bits) {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010079 /* With an explicit bit-size, the data must have the matching length. */
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (curve_bytes != PSA_BITS_TO_BYTES(curve_bits)) {
81 return PSA_ERROR_INVALID_ARGUMENT;
82 }
83 } else {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010084 /* We need to infer the bit-size from the data. Since the only
85 * information we have is the length in bytes, the value of curve_bits
86 * at this stage is rounded up to the nearest multiple of 8. */
Gilles Peskine449bd832023-01-11 14:50:10 +010087 curve_bits = PSA_BYTES_TO_BITS(curve_bytes);
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010088 }
89
Ronald Cron00b7bfc2020-11-25 15:25:26 +010090 /* Allocate and initialize a key representation. */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
92 if (ecp == NULL) {
93 return PSA_ERROR_INSUFFICIENT_MEMORY;
94 }
95 mbedtls_ecp_keypair_init(ecp);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010096
97 /* Load the group. */
Gilles Peskine449bd832023-01-11 14:50:10 +010098 grp_id = mbedtls_ecc_group_of_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(type),
99 curve_bits, !explicit_bits);
100 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100101 /* We can't distinguish between a nonsensical family/size combination
102 * (which would warrant PSA_ERROR_INVALID_ARGUMENT) and a
103 * well-regarded curve that Mbed TLS just doesn't know about (which
104 * would warrant PSA_ERROR_NOT_SUPPORTED). For uniformity with how
105 * curves that Mbed TLS knows about but for which support is disabled
106 * at build time, return NOT_SUPPORTED. */
107 status = PSA_ERROR_NOT_SUPPORTED;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100108 goto exit;
109 }
110
111 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_ecp_group_load(&ecp->grp, grp_id));
113 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100116
117 /* Load the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100119 /* Load the public value. */
120 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_ecp_point_read_binary(&ecp->grp, &ecp->Q,
122 data,
123 data_length));
124 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100127
128 /* Check that the point is on the curve. */
129 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_ecp_check_pubkey(&ecp->grp, &ecp->Q));
131 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 }
134 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100135 /* Load and validate the secret value. */
136 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_ecp_read_key(ecp->grp.id,
138 ecp,
139 data,
140 data_length));
141 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100144 }
145
146 *p_ecp = ecp;
147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (status != PSA_SUCCESS) {
149 mbedtls_ecp_keypair_free(ecp);
150 mbedtls_free(ecp);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100151 }
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100154}
Valerio Setti27c501a2023-06-27 16:58:52 +0200155#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_BASIC) ||
156 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
157 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100158 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
159 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
160 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
Ronald Cronb5399a82020-12-10 09:35:33 +0100161 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100162
Valerio Setti27c501a2023-06-27 16:58:52 +0200163#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
164 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100165 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crond6ec3032020-11-27 18:54:57 +0100166
Ronald Cron0266cfe2021-03-13 18:50:11 +0100167psa_status_t mbedtls_psa_ecp_import_key(
Ronald Crond6ec3032020-11-27 18:54:57 +0100168 const psa_key_attributes_t *attributes,
169 const uint8_t *data, size_t data_length,
170 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 size_t *key_buffer_length, size_t *bits)
Ronald Crond6ec3032020-11-27 18:54:57 +0100172{
173 psa_status_t status;
174 mbedtls_ecp_keypair *ecp = NULL;
175
176 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
178 attributes->core.bits,
179 data,
180 data_length,
181 &ecp);
182 if (status != PSA_SUCCESS) {
Ronald Crond6ec3032020-11-27 18:54:57 +0100183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
Ronald Crond6ec3032020-11-27 18:54:57 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type) ==
187 PSA_ECC_FAMILY_MONTGOMERY) {
Ronald Crond6ec3032020-11-27 18:54:57 +0100188 *bits = ecp->grp.nbits + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 } else {
Ronald Crond6ec3032020-11-27 18:54:57 +0100190 *bits = ecp->grp.nbits;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 }
Ronald Crond6ec3032020-11-27 18:54:57 +0100192
193 /* Re-export the data to PSA export format. There is currently no support
194 * for other input formats then the export format, so this is a 1-1
195 * copy operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 status = mbedtls_psa_ecp_export_key(attributes->core.type,
197 ecp,
198 key_buffer,
199 key_buffer_size,
200 key_buffer_length);
Ronald Crond6ec3032020-11-27 18:54:57 +0100201exit:
202 /* Always free the PK object (will also free contained ECP context) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_ecp_keypair_free(ecp);
204 mbedtls_free(ecp);
Ronald Crond6ec3032020-11-27 18:54:57 +0100205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return status;
Ronald Crond6ec3032020-11-27 18:54:57 +0100207}
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
210 mbedtls_ecp_keypair *ecp,
211 uint8_t *data,
212 size_t data_size,
213 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214{
215 psa_status_t status;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100218 /* Check whether the public part is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (mbedtls_ecp_is_zero(&ecp->Q)) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100220 /* Calculate the public key */
221 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
223 mbedtls_psa_get_random,
224 MBEDTLS_PSA_RANDOM_STATE));
225 if (status != PSA_SUCCESS) {
226 return status;
227 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100228 }
229
230 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_ecp_point_write_binary(&ecp->grp, &ecp->Q,
232 MBEDTLS_ECP_PF_UNCOMPRESSED,
233 data_length,
234 data,
235 data_size));
236 if (status != PSA_SUCCESS) {
237 memset(data, 0, data_size);
238 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return status;
241 } else {
242 if (data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits)) {
243 return PSA_ERROR_BUFFER_TOO_SMALL;
244 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100245
246 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 mbedtls_ecp_write_key(ecp,
248 data,
249 PSA_BITS_TO_BYTES(ecp->grp.nbits)));
250 if (status == PSA_SUCCESS) {
251 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
252 } else {
253 memset(data, 0, data_size);
254 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100257 }
258}
259
Ronald Cron0266cfe2021-03-13 18:50:11 +0100260psa_status_t mbedtls_psa_ecp_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100261 const psa_key_attributes_t *attributes,
262 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100264{
265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
266 mbedtls_ecp_keypair *ecp = NULL;
267
268 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100269 attributes->core.type, attributes->core.bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 key_buffer, key_buffer_size, &ecp);
271 if (status != PSA_SUCCESS) {
272 return status;
273 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100274
275 status = mbedtls_psa_ecp_export_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
277 PSA_KEY_TYPE_ECC_GET_FAMILY(attributes->core.type)),
278 ecp, data, data_size, data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 mbedtls_ecp_keypair_free(ecp);
281 mbedtls_free(ecp);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100284}
Valerio Setti27c501a2023-06-27 16:58:52 +0200285#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
286 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100287 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100288
Valerio Setti4c0174d2023-06-26 10:05:50 +0200289#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100290psa_status_t mbedtls_psa_ecp_generate_key(
Ronald Cron7023db52020-11-20 18:17:42 +0100291 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron7023db52020-11-20 18:17:42 +0100293{
294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
296
297 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 attributes->core.type);
Ronald Cron7023db52020-11-20 18:17:42 +0100299 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 mbedtls_ecc_group_of_psa(curve, attributes->core.bits, 0);
Ronald Cron7023db52020-11-20 18:17:42 +0100301
302 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_ecp_curve_info_from_grp_id(grp_id);
Ronald Cron7023db52020-11-20 18:17:42 +0100304 mbedtls_ecp_keypair ecp;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (attributes->domain_parameters_size != 0) {
307 return PSA_ERROR_NOT_SUPPORTED;
308 }
Ronald Cron7023db52020-11-20 18:17:42 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL) {
311 return PSA_ERROR_NOT_SUPPORTED;
312 }
Ronald Cron7023db52020-11-20 18:17:42 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_ecp_keypair_init(&ecp);
315 ret = mbedtls_ecp_gen_key(grp_id, &ecp,
316 mbedtls_psa_get_random,
317 MBEDTLS_PSA_RANDOM_STATE);
318 if (ret != 0) {
319 mbedtls_ecp_keypair_free(&ecp);
320 return mbedtls_to_psa_error(ret);
Ronald Cron7023db52020-11-20 18:17:42 +0100321 }
322
323 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_ecp_write_key(&ecp, key_buffer, key_buffer_size));
Ronald Cron7023db52020-11-20 18:17:42 +0100325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_ecp_keypair_free(&ecp);
Ronald Cron7023db52020-11-20 18:17:42 +0100327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (status == PSA_SUCCESS) {
Ronald Cron7023db52020-11-20 18:17:42 +0100329 *key_buffer_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Ronald Cron7023db52020-11-20 18:17:42 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return status;
Ronald Cron7023db52020-11-20 18:17:42 +0100333}
Valerio Setti4c0174d2023-06-26 10:05:50 +0200334#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE */
Ronald Cron7023db52020-11-20 18:17:42 +0100335
Ronald Cron072722c2020-12-09 16:36:19 +0100336/****************************************************************/
337/* ECDSA sign/verify */
338/****************************************************************/
339
Ronald Cron0266cfe2021-03-13 18:50:11 +0100340#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
341 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
342psa_status_t mbedtls_psa_ecdsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100343 const psa_key_attributes_t *attributes,
344 const uint8_t *key_buffer, size_t key_buffer_size,
345 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron072722c2020-12-09 16:36:19 +0100347{
348 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
349 mbedtls_ecp_keypair *ecp = NULL;
350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
351 size_t curve_bytes;
352 mbedtls_mpi r, s;
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
355 attributes->core.bits,
356 key_buffer,
357 key_buffer_size,
358 &ecp);
359 if (status != PSA_SUCCESS) {
360 return status;
361 }
Ronald Cron072722c2020-12-09 16:36:19 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
364 mbedtls_mpi_init(&r);
365 mbedtls_mpi_init(&s);
Ronald Cron072722c2020-12-09 16:36:19 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (signature_size < 2 * curve_bytes) {
Ronald Cron072722c2020-12-09 16:36:19 +0100368 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
369 goto cleanup;
370 }
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(alg)) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100373#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200375 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_ext(
377 &ecp->grp, &r, &s,
378 &ecp->d, hash,
379 hash_length, md_alg,
380 mbedtls_psa_get_random,
381 MBEDTLS_PSA_RANDOM_STATE));
Ronald Cron9103d492021-03-04 11:26:03 +0100382#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
384 goto cleanup;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100385#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 } else {
Ronald Cron072722c2020-12-09 16:36:19 +0100387 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ecp->grp, &r, &s, &ecp->d,
389 hash, hash_length,
390 mbedtls_psa_get_random,
391 MBEDTLS_PSA_RANDOM_STATE));
Ronald Cron072722c2020-12-09 16:36:19 +0100392 }
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&r,
395 signature,
396 curve_bytes));
397 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&s,
398 signature + curve_bytes,
399 curve_bytes));
Ronald Cron072722c2020-12-09 16:36:19 +0100400cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_mpi_free(&r);
402 mbedtls_mpi_free(&s);
403 if (ret == 0) {
Ronald Cron072722c2020-12-09 16:36:19 +0100404 *signature_length = 2 * curve_bytes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 }
Ronald Cron072722c2020-12-09 16:36:19 +0100406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 mbedtls_ecp_keypair_free(ecp);
408 mbedtls_free(ecp);
Ronald Cron072722c2020-12-09 16:36:19 +0100409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return mbedtls_to_psa_error(ret);
Ronald Cron072722c2020-12-09 16:36:19 +0100411}
412
Paul Elliott2c9843f2023-02-15 17:32:42 +0000413psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp)
Paul Elliotteefe4722023-02-06 15:59:09 +0000414{
415 int ret = 0;
416
417 /* Check whether the public part is loaded. If not, load it. */
418 if (mbedtls_ecp_is_zero(&ecp->Q)) {
419 ret = mbedtls_ecp_mul(&ecp->grp, &ecp->Q,
420 &ecp->d, &ecp->grp.G,
421 mbedtls_psa_get_random,
422 MBEDTLS_PSA_RANDOM_STATE);
423 }
424
Paul Elliott2c9843f2023-02-15 17:32:42 +0000425 return mbedtls_to_psa_error(ret);
Paul Elliotteefe4722023-02-06 15:59:09 +0000426}
427
Ronald Cron0266cfe2021-03-13 18:50:11 +0100428psa_status_t mbedtls_psa_ecdsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +0100429 const psa_key_attributes_t *attributes,
430 const uint8_t *key_buffer, size_t key_buffer_size,
431 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 const uint8_t *signature, size_t signature_length)
Ronald Cron072722c2020-12-09 16:36:19 +0100433{
434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
435 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron072722c2020-12-09 16:36:19 +0100436 size_t curve_bytes;
437 mbedtls_mpi r, s;
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 (void) alg;
Ronald Cron072722c2020-12-09 16:36:19 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
442 attributes->core.bits,
443 key_buffer,
444 key_buffer_size,
445 &ecp);
446 if (status != PSA_SUCCESS) {
447 return status;
448 }
Ronald Cron072722c2020-12-09 16:36:19 +0100449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 curve_bytes = PSA_BITS_TO_BYTES(ecp->grp.pbits);
451 mbedtls_mpi_init(&r);
452 mbedtls_mpi_init(&s);
Ronald Cron072722c2020-12-09 16:36:19 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (signature_length != 2 * curve_bytes) {
Paul Elliott2c9843f2023-02-15 17:32:42 +0000455 status = PSA_ERROR_INVALID_SIGNATURE;
Ronald Cron072722c2020-12-09 16:36:19 +0100456 goto cleanup;
457 }
458
Paul Elliott2c9843f2023-02-15 17:32:42 +0000459 status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&r,
460 signature,
461 curve_bytes));
462 if (status != PSA_SUCCESS) {
463 goto cleanup;
464 }
Ronald Cron072722c2020-12-09 16:36:19 +0100465
Paul Elliott2c9843f2023-02-15 17:32:42 +0000466 status = mbedtls_to_psa_error(mbedtls_mpi_read_binary(&s,
467 signature + curve_bytes,
468 curve_bytes));
469 if (status != PSA_SUCCESS) {
470 goto cleanup;
471 }
Paul Elliotteefe4722023-02-06 15:59:09 +0000472
Paul Elliott2c9843f2023-02-15 17:32:42 +0000473 status = mbedtls_psa_ecp_load_public_part(ecp);
474 if (status != PSA_SUCCESS) {
475 goto cleanup;
476 }
Ronald Cron072722c2020-12-09 16:36:19 +0100477
Paul Elliott2c9843f2023-02-15 17:32:42 +0000478 status = mbedtls_to_psa_error(mbedtls_ecdsa_verify(&ecp->grp, hash,
479 hash_length, &ecp->Q,
480 &r, &s));
Ronald Cron072722c2020-12-09 16:36:19 +0100481cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_mpi_free(&r);
483 mbedtls_mpi_free(&s);
484 mbedtls_ecp_keypair_free(ecp);
485 mbedtls_free(ecp);
Ronald Cron072722c2020-12-09 16:36:19 +0100486
Paul Elliott2c9843f2023-02-15 17:32:42 +0000487 return status;
Ronald Cron072722c2020-12-09 16:36:19 +0100488}
489
Ronald Cron0266cfe2021-03-13 18:50:11 +0100490#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Ronald Cronb5399a82020-12-10 09:35:33 +0100491 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
492
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000493/****************************************************************/
494/* ECDH Key Agreement */
495/****************************************************************/
Aditya Deshpande5567c662022-11-07 10:43:29 +0000496
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000497#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Aditya Deshpande5567c662022-11-07 10:43:29 +0000498psa_status_t mbedtls_psa_key_agreement_ecdh(
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000499 const psa_key_attributes_t *attributes,
500 const uint8_t *key_buffer, size_t key_buffer_size,
501 psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length,
502 uint8_t *shared_secret, size_t shared_secret_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 size_t *shared_secret_length)
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000504{
Aditya Deshpandeb6bc7522022-11-29 16:53:29 +0000505 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->core.type) ||
507 !PSA_ALG_IS_ECDH(alg)) {
508 return PSA_ERROR_INVALID_ARGUMENT;
509 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000510 mbedtls_ecp_keypair *ecp = NULL;
Aditya Deshpandeb6bc7522022-11-29 16:53:29 +0000511 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 attributes->core.type,
513 attributes->core.bits,
514 key_buffer,
515 key_buffer_size,
516 &ecp);
517 if (status != PSA_SUCCESS) {
518 return status;
519 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000520 mbedtls_ecp_keypair *their_key = NULL;
521 mbedtls_ecdh_context ecdh;
522 size_t bits = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ecp->grp.id, &bits);
524 mbedtls_ecdh_init(&ecdh);
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000525
526 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
528 bits,
529 peer_key,
530 peer_key_length,
531 &their_key);
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
536 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
538 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000541 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 mbedtls_ecdh_get_params(&ecdh, ecp, MBEDTLS_ECDH_OURS));
543 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000544 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000546
547 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_ecdh_calc_secret(&ecdh,
549 shared_secret_length,
550 shared_secret, shared_secret_size,
551 mbedtls_psa_get_random,
552 MBEDTLS_PSA_RANDOM_STATE));
553 if (status != PSA_SUCCESS) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000554 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 }
556 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000557 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 }
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000559exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (status != PSA_SUCCESS) {
561 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
562 }
563 mbedtls_ecdh_free(&ecdh);
564 mbedtls_ecp_keypair_free(their_key);
565 mbedtls_free(their_key);
566 mbedtls_ecp_keypair_free(ecp);
567 mbedtls_free(ecp);
568 return status;
Aditya Deshpande3f1606a2022-11-04 16:55:57 +0000569}
570#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
571
572
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100573#endif /* MBEDTLS_PSA_CRYPTO_C */