Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file pk_info.h |
| 3 | * |
| 4 | * \brief Public Key cryptography abstraction layer: object interface |
| 5 | * |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 6 | * This file contains the info structure interface used by developers to |
Andrzej Kurek | 024ab06 | 2018-02-12 09:34:39 -0500 | [diff] [blame^] | 7 | * provide target-specific implementations of opaque key handling functions |
| 8 | * (called engines in the following). |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2006-2018, ARM Limited, All Rights Reserved |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 11 | * SPDX-License-Identifier: Apache-2.0 |
| 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
| 24 | * |
| 25 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 26 | */ |
| 27 | |
| 28 | #ifndef MBEDTLS_PK_INFO_H |
| 29 | #define MBEDTLS_PK_INFO_H |
| 30 | |
| 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 32 | #include "config.h" |
| 33 | #else |
| 34 | #include MBEDTLS_CONFIG_FILE |
| 35 | #endif |
| 36 | |
| 37 | #include "pk.h" |
| 38 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | /** |
| 44 | * Methods that opaque key pair objects must implement. |
| 45 | * |
| 46 | * Engines that interface with external cryptographic processors must |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 47 | * implement this interface. It allows using different engines for each key. |
| 48 | * Platform-specific hardware accelerators that can be used for all keys of |
| 49 | * a given type should not use this interface, but rather provide an |
| 50 | * alternative implementation of the respective cryptographic module - for |
| 51 | * example to use an RSA accelerator you can define MBEDTLS_RSA_ALT, and |
| 52 | * provide your own implementation of the RSA module. |
| 53 | * |
| 54 | * \warning: If you are using the PK interface to perform operations on |
| 55 | * keys, call the functions in pk.h. The interface in this file should only |
| 56 | * be used by implementers of opaque key engines. |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 57 | * |
| 58 | * An engine for asymmetric cryptography must implement the interface |
| 59 | * described in this structure. The interface for the engine may be |
| 60 | * exposed in one of two ways: |
| 61 | * |
| 62 | * - Declare the mbedtls_pk_info_t structure and instruct users to call |
| 63 | * mbedtls_pk_setup with that structure. |
| 64 | * - Keep the mbedtls_pk_info_t structure hidden and declare a function |
| 65 | * to call instead of mbedtls_pk_setup. This function should have an |
| 66 | * interface of the form |
| 67 | * `int mbedtls_pk_setup_myengine(mbedtls_pk_context *, ...)` |
| 68 | * where the extra parameters depend on the engine, e.g. handles to keys |
| 69 | * stored in an external cryptographic module. |
| 70 | * |
| 71 | * Unless otherwise indicated, functions returning int must return an |
| 72 | * Mbed TLS status code, either 0 for success or a negative value to indicate |
| 73 | * an error. It is recommended to use the MBEDTLS_ERR_PK_XXX error codes |
| 74 | * defined in pk.h. |
| 75 | * |
| 76 | * Some methods are optional; this is clearly indicated in their description. |
| 77 | * If a method is optional, then an opaque key implementation may put NULL |
| 78 | * in the corresponding field. The corresponding function in pk.h will |
| 79 | * return MBEDTLS_ERR_PK_TYPE_MISMATCH in this case. |
| 80 | * |
Andrzej Kurek | e735310 | 2018-01-22 07:14:34 -0500 | [diff] [blame] | 81 | * |
| 82 | * \warning: Do not declare this structure directly! It may be extended in |
| 83 | * future* versions of Mbed TLS. Call the macro |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 84 | * MBEDTLS_PK_OPAQUE_INFO_1() instead. |
| 85 | * This macro is guaranteed to take parameters with the same type |
Andrzej Kurek | e735310 | 2018-01-22 07:14:34 -0500 | [diff] [blame] | 86 | * and semantics as previous versions and fill any new field of the |
| 87 | * structure with sensible values. |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 88 | */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 89 | struct mbedtls_pk_info_t |
| 90 | { |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 91 | /** Key pair type. |
| 92 | * |
| 93 | * mbedtls_pk_get_type() returns this value. |
| 94 | * |
| 95 | * For transparent keys, this contains an indication of supported |
| 96 | * algorithms. For opaque keys, this is \c MBEDTLS_PK_OPAQUE. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 97 | mbedtls_pk_type_t type; |
| 98 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 99 | /** Type name. |
| 100 | * |
| 101 | * mbedtls_pk_get_name() returns this value. It must be a |
| 102 | * null-terminated string. |
| 103 | * |
| 104 | * For transparent keys, this reflects the key type. For opaque keys, |
| 105 | * this reflects the cryptographic module driver. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 106 | const char *name; |
| 107 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 108 | /** Get key size in bits. |
| 109 | * |
| 110 | * mbedtls_pk_get_bitlen() returns this value. |
| 111 | * |
| 112 | * This function cannot fail. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 113 | size_t (*get_bitlen)( const void *ctx ); |
| 114 | |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 115 | /** Tell if the context implements the algorithm specified by |
Andrzej Kurek | 024ab06 | 2018-02-12 09:34:39 -0500 | [diff] [blame^] | 116 | * the provided type (e.g. ECKEY can do ECDSA). |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 117 | * |
| 118 | * mbedtls_pk_can_do() calls this function. |
| 119 | * |
| 120 | * This function is only based on the key type. It does not take any |
| 121 | * usage restrictions into account. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 122 | int (*can_do)( const void * ctx, mbedtls_pk_type_t type ); |
| 123 | |
Unknown | d0d0602 | 2018-02-06 03:20:30 -0500 | [diff] [blame] | 124 | /** Upper bound of the signature length |
Andrzej Kurek | 8b6aaca | 2018-01-22 07:04:46 -0500 | [diff] [blame] | 125 | * |
Unknown | d0d0602 | 2018-02-06 03:20:30 -0500 | [diff] [blame] | 126 | * mbedtls_pk_get_signature_size() returns this value. |
| 127 | * |
| 128 | * In case of an error, or an unsupported key type, 0 should be returned. |
Andrzej Kurek | 8b6aaca | 2018-01-22 07:04:46 -0500 | [diff] [blame] | 129 | * |
| 130 | * Opaque implementations may omit this method if they do not support |
| 131 | * signature. */ |
| 132 | size_t (*signature_size_func)( const void *ctx ); |
| 133 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 134 | /** Verify signature |
| 135 | * |
| 136 | * mbedtls_pk_verify() calls this function. |
| 137 | * |
| 138 | * Opaque implementations may omit this method if they do not support |
| 139 | * signature verification. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 140 | int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg, |
| 141 | const unsigned char *hash, size_t hash_len, |
| 142 | const unsigned char *sig, size_t sig_len ); |
| 143 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 144 | /** Make signature |
| 145 | * |
| 146 | * mbedtls_pk_sign() calls this function. |
| 147 | * |
Andrzej Kurek | 024ab06 | 2018-02-12 09:34:39 -0500 | [diff] [blame^] | 148 | * Assumes that the buffer \c sig has room for |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 149 | * \c signature_size_func(ctx) bytes. |
| 150 | * |
| 151 | * The arguments \c f_rng and \c p_rng are provided in case the |
| 152 | * algorithm requires randomization. Implementations are not |
| 153 | * required to use it if they have their own random source. If \c |
| 154 | * f_rng is null, the implementation should operate if it can, and |
| 155 | * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise. |
| 156 | * |
| 157 | * Opaque implementations may omit this method if they do not support |
Andrzej Kurek | 024ab06 | 2018-02-12 09:34:39 -0500 | [diff] [blame^] | 158 | * signing. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 159 | int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg, |
| 160 | const unsigned char *hash, size_t hash_len, |
| 161 | unsigned char *sig, size_t *sig_len, |
| 162 | int (*f_rng)(void *, unsigned char *, size_t), |
| 163 | void *p_rng ); |
| 164 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 165 | /** Decrypt message |
| 166 | * |
| 167 | * mbedtls_pk_decrypt() calls this function. |
| 168 | * |
| 169 | * The arguments \c f_rng and \c p_rng are provided in case the |
| 170 | * algorithm requires randomization. Implementations are not |
| 171 | * required to use it if they have their own random source. If \c |
| 172 | * f_rng is null, the implementation should operate if it can, and |
| 173 | * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise. |
| 174 | * |
| 175 | * Opaque implementations may omit this method if they do not support |
| 176 | * decryption. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 177 | int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen, |
| 178 | unsigned char *output, size_t *olen, size_t osize, |
| 179 | int (*f_rng)(void *, unsigned char *, size_t), |
| 180 | void *p_rng ); |
| 181 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 182 | /** Encrypt message |
| 183 | * |
| 184 | * mbedtls_pk_decrypt() calls this function. |
| 185 | * |
| 186 | * The arguments \c f_rng and \c p_rng are provided in case the |
| 187 | * algorithm requires randomization. Implementations are not |
| 188 | * required to use it if they have their own random source. If \c |
| 189 | * f_rng is null, the implementation should operate if it can, and |
| 190 | * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise. |
| 191 | * |
| 192 | * Opaque implementations may omit this method if they do not support |
| 193 | * encryption. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 194 | int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen, |
| 195 | unsigned char *output, size_t *olen, size_t osize, |
| 196 | int (*f_rng)(void *, unsigned char *, size_t), |
| 197 | void *p_rng ); |
| 198 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 199 | /** Check public-private key pair |
| 200 | * |
| 201 | * mbedtls_pk_check_pair() calls this function on the private key pair |
| 202 | * object \c prv. The other argument \c pub may be of any type, but it |
| 203 | * is guaranteed to be initialized. |
| 204 | * |
| 205 | * Opaque implementations may omit this method. */ |
Unknown | 4d092dc | 2018-02-08 07:45:41 -0500 | [diff] [blame] | 206 | int (*check_pair_func)( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv ); |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 207 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 208 | /** Allocate a new context |
| 209 | * |
| 210 | * mbedtls_pk_setup() calls this function. |
| 211 | * |
| 212 | * If this function returns NULL, the allocation is considered to |
| 213 | * have failed and the the object remains uninitialized. |
| 214 | * |
| 215 | * Opaque implementations may omit this method. In this case, |
| 216 | * mbedtls_pk_setup will set the \c pk_ctx field of the mbedtls_pk_context |
| 217 | * object to NULL, and it is up to an engine-specific setup function to |
| 218 | * initialize the \c pk_ctx field. This is useful if the size of the |
| 219 | * memory depends on extra parameters passed to the engine-specific setup |
| 220 | * function. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 221 | void * (*ctx_alloc_func)( void ); |
| 222 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 223 | /** Free the given context |
| 224 | * |
| 225 | * mbedtls_pk_free() calls this function. It must free the data allocated |
| 226 | * by \b ctx_alloc_func as well as any other resource that belongs to |
| 227 | * the object. |
| 228 | * */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 229 | void (*ctx_free_func)( void *ctx ); |
| 230 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 231 | /** Interface with the debug module |
| 232 | * |
| 233 | * mbedtls_pk_debug() calls this function. |
| 234 | * |
| 235 | * Opaque implementations may omit this method. */ |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 236 | void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items ); |
| 237 | |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 238 | }; |
| 239 | |
Andrzej Kurek | e735310 | 2018-01-22 07:14:34 -0500 | [diff] [blame] | 240 | #define MBEDTLS_PK_OPAQUE_INFO_1( \ |
| 241 | name \ |
| 242 | , get_bitlen \ |
| 243 | , can_do \ |
| 244 | , signature_size_func \ |
| 245 | , verify_func \ |
| 246 | , sign_func \ |
| 247 | , decrypt_func \ |
| 248 | , encrypt_func \ |
| 249 | , check_pair_func \ |
| 250 | , ctx_alloc_func \ |
| 251 | , ctx_free_func \ |
| 252 | , debug_func \ |
| 253 | ) \ |
| 254 | { \ |
| 255 | MBEDTLS_PK_OPAQUE \ |
| 256 | , name \ |
| 257 | , get_bitlen \ |
| 258 | , can_do \ |
| 259 | , signature_size_func \ |
| 260 | , verify_func \ |
| 261 | , sign_func \ |
| 262 | , decrypt_func \ |
| 263 | , encrypt_func \ |
| 264 | , check_pair_func \ |
| 265 | , ctx_alloc_func \ |
| 266 | , ctx_free_func \ |
| 267 | , debug_func \ |
| 268 | } |
| 269 | |
Gilles Peskine | 02768b4 | 2017-11-03 19:20:27 +0100 | [diff] [blame] | 270 | #ifdef __cplusplus |
| 271 | } |
| 272 | #endif |
| 273 | |
Gilles Peskine | 5cc7bc5 | 2017-11-03 11:58:25 +0100 | [diff] [blame] | 274 | #endif /* MBEDTLS_PK_INFO_H */ |