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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 30 | struct mbedtls_pk_info_t { |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 31 | /** Public key type */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | mbedtls_pk_type_t type; |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 33 | |
| 34 | /** Type name */ |
| 35 | const char *name; |
| 36 | |
| 37 | /** Get key size in bits */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | size_t (*get_bitlen)(const void *); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 39 | |
| 40 | /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 41 | int (*can_do)(mbedtls_pk_type_t type); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 42 | |
| 43 | /** Verify signature */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 44 | int (*verify_func)(void *ctx, |
| 45 | mbedtls_md_type_t md_alg, |
| 46 | const unsigned char *hash, |
| 47 | size_t hash_len, |
| 48 | const unsigned char *sig, |
| 49 | size_t sig_len); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 50 | |
| 51 | /** Make signature */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 52 | int (*sign_func)(void *ctx, |
| 53 | mbedtls_md_type_t md_alg, |
| 54 | const unsigned char *hash, |
| 55 | size_t hash_len, |
| 56 | unsigned char *sig, |
| 57 | size_t sig_size, |
| 58 | size_t *sig_len, |
| 59 | int (*f_rng)(void *, unsigned char *, size_t), |
| 60 | void *p_rng); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 63 | /** Verify signature (restartable) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 64 | int (*verify_rs_func)(void *ctx, |
| 65 | mbedtls_md_type_t md_alg, |
| 66 | const unsigned char *hash, |
| 67 | size_t hash_len, |
| 68 | const unsigned char *sig, |
| 69 | size_t sig_len, |
| 70 | void *rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 71 | |
| 72 | /** Make signature (restartable) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 73 | int (*sign_rs_func)(void *ctx, |
| 74 | mbedtls_md_type_t md_alg, |
| 75 | const unsigned char *hash, |
| 76 | size_t hash_len, |
| 77 | unsigned char *sig, |
| 78 | size_t sig_size, |
| 79 | size_t *sig_len, |
| 80 | int (*f_rng)(void *, unsigned char *, size_t), |
| 81 | void *p_rng, |
| 82 | void *rs_ctx); |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 83 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 85 | /** Decrypt message */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 86 | int (*decrypt_func)(void *ctx, |
| 87 | const unsigned char *input, |
| 88 | size_t ilen, |
| 89 | unsigned char *output, |
| 90 | size_t *olen, |
| 91 | size_t osize, |
| 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 | /** Encrypt message */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 96 | int (*encrypt_func)(void *ctx, |
| 97 | const unsigned char *input, |
| 98 | size_t ilen, |
| 99 | unsigned char *output, |
| 100 | size_t *olen, |
| 101 | size_t osize, |
| 102 | int (*f_rng)(void *, unsigned char *, size_t), |
| 103 | void *p_rng); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 104 | |
| 105 | /** Check public-private key pair */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 106 | int (*check_pair_func)(const void *pub, |
| 107 | const void *prv, |
| 108 | int (*f_rng)(void *, unsigned char *, size_t), |
| 109 | void *p_rng); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 110 | |
| 111 | /** Allocate a new context */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 112 | void *(*ctx_alloc_func)(void); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 113 | |
| 114 | /** Free the given context */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 115 | void (*ctx_free_func)(void *ctx); |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 118 | /** Allocate the restart context */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 119 | void *(*rs_alloc_func)(void); |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 120 | |
| 121 | /** Free the restart context */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 122 | void (*rs_free_func)(void *rs_ctx); |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 123 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 124 | |
Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 125 | /** Interface with the debug module */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 126 | 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] | 127 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 129 | /* Container for RSA-alt */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 130 | typedef struct { |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 131 | void *key; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func; |
| 133 | mbedtls_pk_rsa_alt_sign_func sign_func; |
| 134 | mbedtls_pk_rsa_alt_key_len_func key_len_func; |
| 135 | } mbedtls_rsa_alt_context; |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 136 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | #if defined(MBEDTLS_RSA_C) |
| 139 | extern const mbedtls_pk_info_t mbedtls_rsa_info; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 140 | #endif |
| 141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | #if defined(MBEDTLS_ECP_C) |
| 143 | extern const mbedtls_pk_info_t mbedtls_eckey_info; |
| 144 | extern const mbedtls_pk_info_t mbedtls_eckeydh_info; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 145 | #endif |
| 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_ECDSA_C) |
| 148 | extern const mbedtls_pk_info_t mbedtls_ecdsa_info; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 149 | #endif |
| 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
| 152 | extern const mbedtls_pk_info_t mbedtls_rsa_alt_info; |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 153 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 154 | |
Manuel Pégourié-Gonnard | 1ecf92c3 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 276cb64 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 156 | extern const mbedtls_pk_info_t mbedtls_pk_opaque_info; |
Manuel Pégourié-Gonnard | 1ecf92c3 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_PK_WRAP_H */ |