blob: f577fccdbba77f453d82e8444eef3a04f70cba75 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020023#include "mbedtls/pk_internal.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#endif
38
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/asn1write.h"
41#endif
42
Andres Amaya Garciae32df082017-10-25 09:37:04 +010043#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010045#endif
46
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010047#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040048#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010049#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040050#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#endif
52
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020054
Andres AG72849872017-01-19 11:24:33 +000055#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010056#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020060{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 return type == MBEDTLS_PK_RSA ||
62 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020063}
64
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065static size_t rsa_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020066{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
68 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020069}
70
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
72 const unsigned char *hash, size_t hash_len,
73 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020074{
Janos Follath24eed8d2019-11-22 13:21:35 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
77 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020078
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010079#if SIZE_MAX > UINT_MAX
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
81 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
82 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010083#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +000084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if (sig_len < rsa_len) {
86 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
87 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, NULL, NULL,
90 MBEDTLS_RSA_PUBLIC, md_alg,
91 (unsigned int) hash_len, hash, sig)) != 0) {
92 return ret;
93 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020094
Gilles Peskine5114d3e2018-03-30 07:12:15 +020095 /* The buffer contains a valid signature followed by extra data.
96 * We have a special error code for that so that so that callers can
97 * use mbedtls_pk_verify() to check "Does the buffer start with a
98 * valid signature?" and not just "Does the buffer contain a valid
99 * signature?". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 if (sig_len > rsa_len) {
101 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
102 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200105}
106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
108 const unsigned char *hash, size_t hash_len,
109 unsigned char *sig, size_t *sig_len,
110 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200111{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100113
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100114#if SIZE_MAX > UINT_MAX
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
116 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
117 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100118#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 *sig_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
123 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200124}
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126static int rsa_decrypt_wrap(void *ctx,
127 const unsigned char *input, size_t ilen,
128 unsigned char *output, size_t *olen, size_t osize,
129 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200130{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if (ilen != mbedtls_rsa_get_len(rsa)) {
134 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
135 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
138 MBEDTLS_RSA_PRIVATE, olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200139}
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141static int rsa_encrypt_wrap(void *ctx,
142 const unsigned char *input, size_t ilen,
143 unsigned char *output, size_t *olen, size_t osize,
144 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200145{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
147 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (*olen > osize) {
150 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
151 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
154 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200155}
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157static int rsa_check_pair_wrap(const void *pub, const void *prv)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100158{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
160 (const mbedtls_rsa_context *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100161}
162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200164{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if (ctx != NULL) {
168 mbedtls_rsa_init((mbedtls_rsa_context *) ctx, 0, 0);
169 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200172}
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200175{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
177 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200178}
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200181{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200183 items->name = "rsa.N";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 items->value = &(((mbedtls_rsa_context *) ctx)->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200185
186 items++;
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200189 items->name = "rsa.E";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 items->value = &(((mbedtls_rsa_context *) ctx)->E);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200191}
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193const mbedtls_pk_info_t mbedtls_rsa_info = {
194 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200195 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200196 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200197 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200198 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200199 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200200#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200201 NULL,
202 NULL,
203#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200204 rsa_decrypt_wrap,
205 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100206 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200207 rsa_alloc_wrap,
208 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200209#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200210 NULL,
211 NULL,
212#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200213 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200214};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200218/*
219 * Generic EC key
220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200222{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return type == MBEDTLS_PK_ECKEY ||
224 type == MBEDTLS_PK_ECKEY_DH ||
225 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200226}
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228static size_t eckey_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200229{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 return ((mbedtls_ecp_keypair *) ctx)->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200231}
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200234/* Forward declarations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
236 const unsigned char *hash, size_t hash_len,
237 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
240 const unsigned char *hash, size_t hash_len,
241 unsigned char *sig, size_t *sig_len,
242 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
245 const unsigned char *hash, size_t hash_len,
246 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200247{
Janos Follath24eed8d2019-11-22 13:21:35 +0000248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 if ((ret = mbedtls_ecdsa_from_keypair(&ecdsa, ctx)) == 0) {
254 ret = ecdsa_verify_wrap(&ecdsa, md_alg, hash, hash_len, sig, sig_len);
255 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return ret;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200260}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
263 const unsigned char *hash, size_t hash_len,
264 unsigned char *sig, size_t *sig_len,
265 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200266{
Janos Follath24eed8d2019-11-22 13:21:35 +0000267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if ((ret = mbedtls_ecdsa_from_keypair(&ecdsa, ctx)) == 0) {
273 ret = ecdsa_sign_wrap(&ecdsa, md_alg, hash, hash_len, sig, sig_len,
274 f_rng, p_rng);
275 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return ret;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200280}
281
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200282#if defined(MBEDTLS_ECP_RESTARTABLE)
283/* Forward declarations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
285 const unsigned char *hash, size_t hash_len,
286 const unsigned char *sig, size_t sig_len,
287 void *rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
290 const unsigned char *hash, size_t hash_len,
291 unsigned char *sig, size_t *sig_len,
292 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
293 void *rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200294
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200295/*
296 * Restart context for ECDSA operations with ECKEY context
297 *
298 * We need to store an actual ECDSA context, as we need to pass the same to
299 * the underlying ecdsa function, so we can't create it on the fly every time.
300 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301typedef struct {
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200302 mbedtls_ecdsa_restart_ctx ecdsa_rs;
303 mbedtls_ecdsa_context ecdsa_ctx;
304} eckey_restart_ctx;
305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306static void *eckey_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200307{
308 eckey_restart_ctx *rs_ctx;
309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 if (ctx != NULL) {
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200313 rs_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
315 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200316 }
317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200319}
320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321static void eckey_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200322{
323 eckey_restart_ctx *rs_ctx;
324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 if (ctx == NULL) {
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200326 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200328
329 rs_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
331 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200334}
335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
337 const unsigned char *hash, size_t hash_len,
338 const unsigned char *sig, size_t sig_len,
339 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200340{
Janos Follath24eed8d2019-11-22 13:21:35 +0000341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200342 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200343
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200344 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 if (rs == NULL) {
346 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
347 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200348
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200349 /* set up our own sub-context if needed (that is, on first run) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (rs->ecdsa_ctx.grp.pbits == 0) {
351 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
352 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
355 md_alg, hash, hash_len,
356 sig, sig_len, &rs->ecdsa_rs));
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200357
358cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200360}
361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
363 const unsigned char *hash, size_t hash_len,
364 unsigned char *sig, size_t *sig_len,
365 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
366 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200367{
Janos Follath24eed8d2019-11-22 13:21:35 +0000368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200369 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200370
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200371 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if (rs == NULL) {
373 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
374 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200375
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200376 /* set up our own sub-context if needed (that is, on first run) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 if (rs->ecdsa_ctx.grp.pbits == 0) {
378 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
379 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
382 hash, hash_len, sig, sig_len,
383 f_rng, p_rng, &rs->ecdsa_rs));
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200384
385cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200387}
388#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391static int eckey_check_pair(const void *pub, const void *prv)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100392{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
394 (const mbedtls_ecp_keypair *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100395}
396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397static void *eckey_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200398{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 if (ctx != NULL) {
402 mbedtls_ecp_keypair_init(ctx);
403 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200406}
407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408static void eckey_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200409{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
411 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200412}
413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200417 items->name = "eckey.Q";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200419}
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421const mbedtls_pk_info_t mbedtls_eckey_info = {
422 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200423 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200424 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200425 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200427 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200428 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200429#if defined(MBEDTLS_ECP_RESTARTABLE)
430 eckey_verify_rs_wrap,
431 eckey_sign_rs_wrap,
432#endif
433#else /* MBEDTLS_ECDSA_C */
434 NULL,
435 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200436#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200437 NULL,
438 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100439 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200440 eckey_alloc_wrap,
441 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200442#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200443 eckey_rs_alloc,
444 eckey_rs_free,
445#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200446 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200447};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200448
449/*
Paul Bakker75342a62014-04-08 17:35:40 +0200450 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200451 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452static int eckeydh_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200453{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 return type == MBEDTLS_PK_ECKEY ||
455 type == MBEDTLS_PK_ECKEY_DH;
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200456}
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458const mbedtls_pk_info_t mbedtls_eckeydh_info = {
459 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200460 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200461 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200462 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200463 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200464 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200465#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200466 NULL,
467 NULL,
468#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200469 NULL,
470 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100471 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200472 eckey_alloc_wrap, /* Same underlying key structure */
473 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200474#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200475 NULL,
476 NULL,
477#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200478 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200479};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483static int ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200484{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 return type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200486}
487
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400488#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400489/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500490 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
491 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500492 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
494 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500495{
Janos Follath24eed8d2019-11-22 13:21:35 +0000496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500497 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
500 MBEDTLS_ASN1_INTEGER)) != 0) {
501 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500502 }
503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 while (unpadded_len > 0 && **from == 0x00) {
505 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500506 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500507 }
508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 if (unpadded_len > to_len || unpadded_len == 0) {
510 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
511 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500512
Andrzej Kurek9241d182018-11-20 05:04:35 -0500513 padding_len = to_len - unpadded_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 memset(to, 0x00, padding_len);
515 memcpy(to + padding_len, *from, unpadded_len);
516 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100518 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500519}
520
521/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400522 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500523 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500524 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400525 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
527 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400528{
Janos Follath24eed8d2019-11-22 13:21:35 +0000529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500530 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
533 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
534 return ret;
535 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400536
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500537 /* Extract r */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
539 return ret;
540 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500541 /* Extract s */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
543 return ret;
544 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400547}
548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
550 const unsigned char *hash, size_t hash_len,
551 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400552{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100553 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200556 psa_key_id_t key_id = 0;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200557 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400558 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400559 int key_len;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500560 /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */
561 unsigned char buf[30 + 2 * MBEDTLS_ECP_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000562 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400563 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700564 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100565 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100566 psa_ecc_family_t curve =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
568 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700569 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 if (curve == 0) {
572 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
573 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500574
Hanno Beckerccf574e2019-01-29 08:26:15 +0000575 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500576 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400577 key.pk_info = &pk_info;
578 key.pk_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 p = buf + sizeof(buf);
580 key_len = mbedtls_pk_write_pubkey(&p, buf, &key);
581 if (key_len <= 0) {
582 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
583 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
586 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
587 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 status = psa_import_key(&attributes,
590 buf + sizeof(buf) - key_len, key_len,
591 &key_id);
592 if (status != PSA_SUCCESS) {
593 ret = mbedtls_psa_err_translate_pk(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400594 goto cleanup;
595 }
596
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500597 /* We don't need the exported key anymore and can
598 * reuse its buffer for signature extraction. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500600 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
601 goto cleanup;
602 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 p = (unsigned char *) sig;
605 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
606 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500607 goto cleanup;
608 }
609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 if (psa_verify_hash(key_id, psa_sig_md,
611 hash, hash_len,
612 buf, 2 * signature_part_size)
613 != PSA_SUCCESS) {
614 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
615 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400616 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500619 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
620 goto cleanup;
621 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400622 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400623
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500624cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625 psa_destroy_key(key_id);
626 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400627}
628#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
630 const unsigned char *hash, size_t hash_len,
631 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200632{
Janos Follath24eed8d2019-11-22 13:21:35 +0000633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200634 ((void) md_alg);
635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
637 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
640 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
641 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200644}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400645#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
648 const unsigned char *hash, size_t hash_len,
649 unsigned char *sig, size_t *sig_len,
650 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200651{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
653 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200654}
655
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200656#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
658 const unsigned char *hash, size_t hash_len,
659 const unsigned char *sig, size_t sig_len,
660 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200661{
Janos Follath24eed8d2019-11-22 13:21:35 +0000662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200663 ((void) md_alg);
664
665 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 (mbedtls_ecdsa_context *) ctx,
667 hash, hash_len, sig, sig_len,
668 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
671 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
672 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200675}
676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
678 const unsigned char *hash, size_t hash_len,
679 unsigned char *sig, size_t *sig_len,
680 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
681 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200682{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 return mbedtls_ecdsa_write_signature_restartable(
684 (mbedtls_ecdsa_context *) ctx,
685 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
686 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200687
688}
689#endif /* MBEDTLS_ECP_RESTARTABLE */
690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691static void *ecdsa_alloc_wrap(void)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200692{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_context));
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100695 if (ctx != NULL) {
696 mbedtls_ecdsa_init((mbedtls_ecdsa_context *) ctx);
697 }
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 return ctx;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200700}
701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702static void ecdsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200703{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 mbedtls_ecdsa_free((mbedtls_ecdsa_context *) ctx);
705 mbedtls_free(ctx);
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200706}
707
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200708#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200710{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 if (ctx != NULL) {
714 mbedtls_ecdsa_restart_init(ctx);
715 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200718}
719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200721{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 mbedtls_ecdsa_restart_free(ctx);
723 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200724}
725#endif /* MBEDTLS_ECP_RESTARTABLE */
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727const mbedtls_pk_info_t mbedtls_ecdsa_info = {
728 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200729 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200730 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200731 ecdsa_can_do,
732 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200733 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200734#if defined(MBEDTLS_ECP_RESTARTABLE)
735 ecdsa_verify_rs_wrap,
736 ecdsa_sign_rs_wrap,
737#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200738 NULL,
739 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100740 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200741 ecdsa_alloc_wrap,
742 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200743#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200744 ecdsa_rs_alloc,
745 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200746#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200747 eckey_debug, /* Compatible key structures */
748};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200752/*
753 * Support for alternative RSA-private implementations
754 */
755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200757{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200759}
760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200766}
767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
769 const unsigned char *hash, size_t hash_len,
770 unsigned char *sig, size_t *sig_len,
771 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200774
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100775#if SIZE_MAX > UINT_MAX
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776 if (UINT_MAX < hash_len) {
777 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
778 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100779#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
782 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
783 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
784 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
787 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200788}
789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790static int rsa_alt_decrypt_wrap(void *ctx,
791 const unsigned char *input, size_t ilen,
792 unsigned char *output, size_t *olen, size_t osize,
793 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200796
797 ((void) f_rng);
798 ((void) p_rng);
799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
801 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
802 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 return rsa_alt->decrypt_func(rsa_alt->key,
805 MBEDTLS_RSA_PRIVATE, olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200806}
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809static int rsa_alt_check_pair(const void *pub, const void *prv)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100810{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100812 unsigned char hash[32];
813 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +0000814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100816 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
817 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100818 }
819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820 memset(hash, 0x2a, sizeof(hash));
821
822 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
823 hash, sizeof(hash),
824 sig, &sig_len, NULL, NULL)) != 0) {
825 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100826 }
827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
829 hash, sizeof(hash), sig, sig_len) != 0) {
830 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
831 }
832
833 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200838{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 if (ctx != NULL) {
842 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
843 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200846}
847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200849{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
851 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200852}
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
855 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200856 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200857 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200858 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200859 NULL,
860 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200861#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200862 NULL,
863 NULL,
864#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200865 rsa_alt_decrypt_wrap,
866 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100868 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100869#else
870 NULL,
871#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200872 rsa_alt_alloc_wrap,
873 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200874#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200875 NULL,
876 NULL,
877#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200878 NULL,
879};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200882
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200883#if defined(MBEDTLS_USE_PSA_CRYPTO)
884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100885static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100886{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 void *ctx = mbedtls_calloc(1, sizeof(psa_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100888
Tom Cosgrove5205c972022-07-28 06:12:08 +0100889 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100892}
893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100895{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 mbedtls_platform_zeroize(ctx, sizeof(psa_key_id_t));
897 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100898}
899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100901{
Ronald Croncf56a0a2020-08-04 09:51:30 +0200902 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100903 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200904 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
907 return 0;
908 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 bits = psa_get_key_bits(&attributes);
911 psa_reset_key_attributes(&attributes);
912 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +0100913}
914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100915static int pk_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100916{
917 /* For now opaque PSA keys can only wrap ECC keypairs,
918 * as checked by setup_psa().
919 * Also, ECKEY_DH does not really make sense with the current API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 return type == MBEDTLS_PK_ECKEY ||
921 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100922}
923
John Durkopf35069a2020-08-17 22:05:14 -0700924#if defined(MBEDTLS_ECDSA_C)
925
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100926/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +0100927 * Simultaneously convert and move raw MPI from the beginning of a buffer
928 * to an ASN.1 MPI at the end of the buffer.
929 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100930 *
931 * p: pointer to the end of the output buffer
932 * start: start of the output buffer, and also of the mpi to write at the end
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +0100933 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100934 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
936 size_t n_len)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100937{
Janos Follath24eed8d2019-11-22 13:21:35 +0000938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100939 size_t len = 0;
940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100941 if ((size_t) (*p - start) < n_len) {
942 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
943 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100944
945 len = n_len;
946 *p -= len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 memmove(*p, start, len);
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100948
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100949 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100950 * Neither r nor s should be 0, but as a failsafe measure, still detect
951 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 while (len > 0 && **p == 0x00) {
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100953 ++(*p);
954 --len;
955 }
956
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100957 /* this is only reached if the signature was invalid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100958 if (len == 0) {
959 return MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
960 }
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +0100961
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100962 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +0100963 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 if (**p & 0x80) {
965 if (*p - start < 1) {
966 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
967 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100968
969 *--(*p) = 0x00;
970 len += 1;
971 }
972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
974 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
975 MBEDTLS_ASN1_INTEGER));
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 return (int) len;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100978}
979
980/* Transcode signature from PSA format to ASN.1 sequence.
981 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
982 * MPIs, and in-place.
983 *
984 * [in/out] sig: the signature pre- and post-transcoding
985 * [in/out] sig_len: signature length pre- and post-transcoding
986 * [int] buf_len: the available size the in/out buffer
987 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
989 size_t buf_len)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100990{
Janos Follath24eed8d2019-11-22 13:21:35 +0000991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100992 size_t len = 0;
993 const size_t rs_len = *sig_len / 2;
994 unsigned char *p = sig + buf_len;
995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
997 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +0100998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
1000 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
1001 MBEDTLS_ASN1_CONSTRUCTED |
1002 MBEDTLS_ASN1_SEQUENCE));
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001004 memmove(sig, p, len);
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001005 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +01001006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 return 0;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +01001008}
1009
John Durkopd46ede02020-08-24 09:51:00 -07001010#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -07001011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1013 const unsigned char *hash, size_t hash_len,
1014 unsigned char *sig, size_t *sig_len,
1015 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001016{
John Durkopf35069a2020-08-17 22:05:14 -07001017#if !defined(MBEDTLS_ECDSA_C)
1018 ((void) ctx);
1019 ((void) md_alg);
1020 ((void) hash);
1021 ((void) hash_len);
1022 ((void) sig);
1023 ((void) sig_len);
1024 ((void) f_rng);
1025 ((void) p_rng);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
John Durkopaf5363c2020-08-24 08:29:39 -07001027#else /* !MBEDTLS_ECDSA_C */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001028 const psa_key_id_t *key = (const psa_key_id_t *) ctx;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 psa_algorithm_t alg = PSA_ALG_ECDSA(mbedtls_psa_translate_md(md_alg));
Gilles Peskined2d45c12019-05-27 14:53:13 +02001031 size_t buf_len;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001032 psa_status_t status;
1033
1034 /* PSA has its own RNG */
1035 (void) f_rng;
1036 (void) p_rng;
1037
1038 /* PSA needs an output buffer of known size, but our API doesn't provide
1039 * that information. Assume that the buffer is large enough for a
1040 * maximal-length signature with that key (otherwise the application is
1041 * buggy anyway). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001042 status = psa_get_key_attributes(*key, &attributes);
1043 if (status != PSA_SUCCESS) {
1044 return mbedtls_psa_err_translate_pk(status);
1045 }
1046 buf_len = MBEDTLS_ECDSA_MAX_SIG_LEN(psa_get_key_bits(&attributes));
1047 psa_reset_key_attributes(&attributes);
1048 if (buf_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1049 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1050 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001051
1052 /* make the signature */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001053 status = psa_sign_hash(*key, alg, hash, hash_len,
1054 sig, buf_len, sig_len);
1055 if (status != PSA_SUCCESS) {
1056 return mbedtls_psa_err_translate_pk(status);
1057 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001058
1059 /* transcode it to ASN.1 sequence */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, buf_len);
John Durkopaf5363c2020-08-24 08:29:39 -07001061#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001062}
1063
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001064const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1065 MBEDTLS_PK_OPAQUE,
1066 "Opaque",
1067 pk_opaque_get_bitlen,
1068 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001069 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001070 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001071#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1072 NULL, /* restartable verify - not relevant */
1073 NULL, /* restartable sign - not relevant */
1074#endif
1075 NULL, /* decrypt - will be done later */
1076 NULL, /* encrypt - will be done later */
1077 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001078 pk_opaque_alloc_wrap,
1079 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001080#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1081 NULL, /* restart alloc - not relevant */
1082 NULL, /* restart free - not relevant */
1083#endif
1084 NULL, /* debug - could be done later, or even left NULL */
1085};
1086
1087#endif /* MBEDTLS_USE_PSA_CRYPTO */
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#endif /* MBEDTLS_PK_C */