Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 1 | /** |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 2 | * \file pk_wrap.h |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 3 | * |
| 4 | * \brief Public Key abstraction layer: wrapper functions |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | * 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é-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_PK_WRAP_H |
| 24 | #define MBEDTLS_PK_WRAP_H |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 25 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 27 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 28 | #include "mbedtls/pk.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 29 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 30 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 31 | #include "psa/crypto.h" |
| 32 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 33 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame^] | 34 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) || defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
| 35 | #define MBEDTLS_PK_CAN_ECDSA_SOME |
| 36 | #endif |
| 37 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | struct mbedtls_pk_info_t { |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 39 | /** Public key type */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_pk_type_t type; |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 41 | |
| 42 | /** Type name */ |
| 43 | const char *name; |
| 44 | |
| 45 | /** Get key size in bits */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | size_t (*get_bitlen)(const void *); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 47 | |
| 48 | /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | int (*can_do)(mbedtls_pk_type_t type); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 50 | |
| 51 | /** Verify signature */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | 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é-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 55 | |
| 56 | /** Make signature */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | 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é-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 62 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame^] | 63 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 64 | /** Verify signature (restartable) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | 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é-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 69 | |
| 70 | /** Make signature (restartable) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | 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é-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 76 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 77 | |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 78 | /** Decrypt message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | 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é-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 83 | |
| 84 | /** Encrypt message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | 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é-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 89 | |
| 90 | /** Check public-private key pair */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | 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é-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 94 | |
| 95 | /** Allocate a new context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | void * (*ctx_alloc_func)(void); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 97 | |
| 98 | /** Free the given context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | void (*ctx_free_func)(void *ctx); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 100 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 101 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 102 | /** Allocate the restart context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | void *(*rs_alloc_func)(void); |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 104 | |
| 105 | /** Free the restart context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | void (*rs_free_func)(void *rs_ctx); |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 107 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 109 | /** Interface with the debug module */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | void (*debug_func)(const void *ctx, mbedtls_pk_debug_item *items); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 111 | |
| 112 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 114 | /* Container for RSA-alt */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | typedef struct { |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 116 | void *key; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | 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é-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 121 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | #if defined(MBEDTLS_RSA_C) |
| 124 | extern const mbedtls_pk_info_t mbedtls_rsa_info; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 125 | #endif |
| 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | #if defined(MBEDTLS_ECP_C) |
| 128 | extern const mbedtls_pk_info_t mbedtls_eckey_info; |
| 129 | extern const mbedtls_pk_info_t mbedtls_eckeydh_info; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 130 | #endif |
| 131 | |
Valerio Setti | 7ca1318 | 2023-01-27 13:22:42 +0100 | [diff] [blame] | 132 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | extern const mbedtls_pk_info_t mbedtls_ecdsa_info; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 134 | #endif |
| 135 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
| 137 | extern const mbedtls_pk_info_t mbedtls_rsa_alt_info; |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 138 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | 1ecf92c3 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 140 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 141 | extern const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info; |
| 142 | extern const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 143 | |
| 144 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status); |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 146 | #endif |
| 147 | |
Jerry Yu | f8aa9a4 | 2022-03-23 20:40:28 +0800 | [diff] [blame] | 148 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 7533982 | 2022-03-23 12:06:31 +0800 | [diff] [blame] | 149 | |
| 150 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | int mbedtls_pk_error_from_psa(psa_status_t status); |
Jerry Yu | 7533982 | 2022-03-23 12:06:31 +0800 | [diff] [blame] | 152 | |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 153 | #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \ |
| 154 | defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | int mbedtls_pk_error_from_psa_rsa(psa_status_t status); |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 156 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 157 | |
Jerry Yu | 89107d1 | 2022-03-22 14:20:15 +0800 | [diff] [blame] | 158 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | int 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 Yu | 89107d1 | 2022-03-22 14:20:15 +0800 | [diff] [blame] | 164 | #endif /* MBEDTLS_RSA_C */ |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 165 | |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 166 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | #endif /* MBEDTLS_PK_WRAP_H */ |