blob: ac5c72f10c62d97a5b59880b9bb03cde20c61d40 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/**
Chris Jonesdaacb592021-03-09 17:03:29 +00002 * \file pk_wrap.h
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02003 *
4 * \brief Public Key abstraction layer: wrapper functions
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_PK_WRAP_H
24#define MBEDTLS_PK_WRAP_H
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010028#include "mbedtls/pk.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020029
Jerry Yub02ee182022-03-16 10:30:41 +080030#if defined(MBEDTLS_PSA_CRYPTO_C)
31#include "psa/crypto.h"
32#endif /* MBEDTLS_PSA_CRYPTO_C */
33
Valerio Setti1cdddac2023-02-02 13:55:57 +010034#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) || defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
35#define MBEDTLS_PK_CAN_ECDSA_SOME
36#endif
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038struct mbedtls_pk_info_t {
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020039 /** Public key type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_pk_type_t type;
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020041
42 /** Type name */
43 const char *name;
44
45 /** Get key size in bits */
Gilles Peskine449bd832023-01-11 14:50:10 +010046 size_t (*get_bitlen)(const void *);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020047
48 /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +010049 int (*can_do)(mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020050
51 /** Verify signature */
Gilles Peskine449bd832023-01-11 14:50:10 +010052 int (*verify_func)(void *ctx, mbedtls_md_type_t md_alg,
53 const unsigned char *hash, size_t hash_len,
54 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020055
56 /** Make signature */
Gilles Peskine449bd832023-01-11 14:50:10 +010057 int (*sign_func)(void *ctx, mbedtls_md_type_t md_alg,
58 const unsigned char *hash, size_t hash_len,
59 unsigned char *sig, size_t sig_size, size_t *sig_len,
60 int (*f_rng)(void *, unsigned char *, size_t),
61 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020062
Valerio Setti5b16e9e2023-02-07 08:08:53 +010063#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020064 /** Verify signature (restartable) */
Gilles Peskine449bd832023-01-11 14:50:10 +010065 int (*verify_rs_func)(void *ctx, mbedtls_md_type_t md_alg,
66 const unsigned char *hash, size_t hash_len,
67 const unsigned char *sig, size_t sig_len,
68 void *rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020069
70 /** Make signature (restartable) */
Gilles Peskine449bd832023-01-11 14:50:10 +010071 int (*sign_rs_func)(void *ctx, mbedtls_md_type_t md_alg,
72 const unsigned char *hash, size_t hash_len,
73 unsigned char *sig, size_t sig_size, size_t *sig_len,
74 int (*f_rng)(void *, unsigned char *, size_t),
75 void *p_rng, void *rs_ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020076#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020077
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020078 /** Decrypt message */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 int (*decrypt_func)(void *ctx, const unsigned char *input, size_t ilen,
80 unsigned char *output, size_t *olen, size_t osize,
81 int (*f_rng)(void *, unsigned char *, size_t),
82 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020083
84 /** Encrypt message */
Gilles Peskine449bd832023-01-11 14:50:10 +010085 int (*encrypt_func)(void *ctx, const unsigned char *input, size_t ilen,
86 unsigned char *output, size_t *olen, size_t osize,
87 int (*f_rng)(void *, unsigned char *, size_t),
88 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020089
90 /** Check public-private key pair */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 int (*check_pair_func)(const void *pub, const void *prv,
92 int (*f_rng)(void *, unsigned char *, size_t),
93 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020094
95 /** Allocate a new context */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 void * (*ctx_alloc_func)(void);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020097
98 /** Free the given context */
Gilles Peskine449bd832023-01-11 14:50:10 +010099 void (*ctx_free_func)(void *ctx);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200100
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200101#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102 /** Allocate the restart context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 void *(*rs_alloc_func)(void);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200104
105 /** Free the restart context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 void (*rs_free_func)(void *rs_ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200107#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200108
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200109 /** Interface with the debug module */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 void (*debug_func)(const void *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200111
112};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200114/* Container for RSA-alt */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115typedef struct {
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200116 void *key;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
118 mbedtls_pk_rsa_alt_sign_func sign_func;
119 mbedtls_pk_rsa_alt_key_len_func key_len_func;
120} mbedtls_rsa_alt_context;
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200121#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_RSA_C)
124extern const mbedtls_pk_info_t mbedtls_rsa_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200125#endif
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_ECP_C)
128extern const mbedtls_pk_info_t mbedtls_eckey_info;
129extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200130#endif
131
Valerio Setti7ca13182023-01-27 13:22:42 +0100132#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200134#endif
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
137extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200138#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200139
Manuel Pégourié-Gonnard1ecf92c32018-10-22 12:11:15 +0200140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100141extern const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info;
142extern const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info;
Neil Armstrong19915c22022-03-01 15:21:02 +0100143
144#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status);
Neil Armstrong19915c22022-03-01 15:21:02 +0100146#endif
147
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800148#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu75339822022-03-23 12:06:31 +0800149
150#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151int mbedtls_pk_error_from_psa(psa_status_t status);
Jerry Yu75339822022-03-23 12:06:31 +0800152
Neil Armstrong30beca32022-05-03 15:42:13 +0200153#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
154 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100155int mbedtls_pk_error_from_psa_rsa(psa_status_t status);
Neil Armstrong30beca32022-05-03 15:42:13 +0200156#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu1d172a32022-03-12 19:12:05 +0800157
Jerry Yu89107d12022-03-22 14:20:15 +0800158#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t psa_alg_md,
160 mbedtls_rsa_context *rsa_ctx,
161 const unsigned char *hash, size_t hash_len,
162 unsigned char *sig, size_t sig_size,
163 size_t *sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800164#endif /* MBEDTLS_RSA_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800165
Jerry Yu406cf272022-03-22 11:33:42 +0800166#endif /* MBEDTLS_PSA_CRYPTO_C */
Neil Armstrong19915c22022-03-01 15:21:02 +0100167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_PK_WRAP_H */