blob: c5cd4df151b49f997149732f71407517d269fc53 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010034struct mbedtls_pk_info_t {
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020035 /** Public key type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036 mbedtls_pk_type_t type;
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020037
38 /** Type name */
39 const char *name;
40
41 /** Get key size in bits */
Gilles Peskine449bd832023-01-11 14:50:10 +010042 size_t (*get_bitlen)(const void *);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020043
44 /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +010045 int (*can_do)(mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020046
47 /** Verify signature */
Gilles Peskine449bd832023-01-11 14:50:10 +010048 int (*verify_func)(void *ctx, mbedtls_md_type_t md_alg,
49 const unsigned char *hash, size_t hash_len,
50 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020051
52 /** Make signature */
Gilles Peskine449bd832023-01-11 14:50:10 +010053 int (*sign_func)(void *ctx, mbedtls_md_type_t md_alg,
54 const unsigned char *hash, size_t hash_len,
55 unsigned char *sig, size_t sig_size, size_t *sig_len,
56 int (*f_rng)(void *, unsigned char *, size_t),
57 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020058
Valerio Setti5b16e9e2023-02-07 08:08:53 +010059#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020060 /** Verify signature (restartable) */
Gilles Peskine449bd832023-01-11 14:50:10 +010061 int (*verify_rs_func)(void *ctx, mbedtls_md_type_t md_alg,
62 const unsigned char *hash, size_t hash_len,
63 const unsigned char *sig, size_t sig_len,
64 void *rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020065
66 /** Make signature (restartable) */
Gilles Peskine449bd832023-01-11 14:50:10 +010067 int (*sign_rs_func)(void *ctx, mbedtls_md_type_t md_alg,
68 const unsigned char *hash, size_t hash_len,
69 unsigned char *sig, size_t sig_size, size_t *sig_len,
70 int (*f_rng)(void *, unsigned char *, size_t),
71 void *p_rng, void *rs_ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020072#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020073
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020074 /** Decrypt message */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 int (*decrypt_func)(void *ctx, const unsigned char *input, size_t ilen,
76 unsigned char *output, size_t *olen, size_t osize,
77 int (*f_rng)(void *, unsigned char *, size_t),
78 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020079
80 /** Encrypt message */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 int (*encrypt_func)(void *ctx, const unsigned char *input, size_t ilen,
82 unsigned char *output, size_t *olen, size_t osize,
83 int (*f_rng)(void *, unsigned char *, size_t),
84 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020085
86 /** Check public-private key pair */
Gilles Peskine449bd832023-01-11 14:50:10 +010087 int (*check_pair_func)(const void *pub, const void *prv,
88 int (*f_rng)(void *, unsigned char *, size_t),
89 void *p_rng);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020090
91 /** Allocate a new context */
Gilles Peskine449bd832023-01-11 14:50:10 +010092 void * (*ctx_alloc_func)(void);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020093
94 /** Free the given context */
Gilles Peskine449bd832023-01-11 14:50:10 +010095 void (*ctx_free_func)(void *ctx);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020096
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020097#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 /** Allocate the restart context */
Gilles Peskine449bd832023-01-11 14:50:10 +010099 void *(*rs_alloc_func)(void);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200100
101 /** Free the restart context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 void (*rs_free_func)(void *rs_ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200103#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200104
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200105 /** Interface with the debug module */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 void (*debug_func)(const void *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200107
108};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200110/* Container for RSA-alt */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111typedef struct {
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200112 void *key;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
114 mbedtls_pk_rsa_alt_sign_func sign_func;
115 mbedtls_pk_rsa_alt_key_len_func key_len_func;
116} mbedtls_rsa_alt_context;
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200117#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_RSA_C)
120extern const mbedtls_pk_info_t mbedtls_rsa_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200121#endif
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_ECP_C)
124extern const mbedtls_pk_info_t mbedtls_eckey_info;
125extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200126#endif
127
Valerio Setti7ca13182023-01-27 13:22:42 +0100128#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200130#endif
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
133extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200134#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200135
Manuel Pégourié-Gonnard1ecf92c32018-10-22 12:11:15 +0200136#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100137extern const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info;
138extern const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info;
Neil Armstrong19915c22022-03-01 15:21:02 +0100139
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500140#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Neil Armstrong19915c22022-03-01 15:21:02 +0100141#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500142int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa_ecdsa(psa_status_t status);
143#endif
Neil Armstrong19915c22022-03-01 15:21:02 +0100144#endif
145
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800146#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu75339822022-03-23 12:06:31 +0800147
148#if defined(MBEDTLS_PSA_CRYPTO_C)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500149#if !defined(MBEDTLS_DEPRECATED_REMOVED)
150int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa(psa_status_t status);
Jerry Yu75339822022-03-23 12:06:31 +0800151
Neil Armstrong30beca32022-05-03 15:42:13 +0200152#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
153 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500154int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa_rsa(psa_status_t status);
Neil Armstrong30beca32022-05-03 15:42:13 +0200155#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500156#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Jerry Yu1d172a32022-03-12 19:12:05 +0800157
Jerry Yu89107d12022-03-22 14:20:15 +0800158#if defined(MBEDTLS_RSA_C)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500159int 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 */