blob: 89752cea9b3a3136735ab423903f1cca4669a716 [file] [log] [blame]
Gilles Peskine5cc7bc52017-11-03 11:58:25 +01001/**
2 * \file pk_info.h
3 *
Andrzej Kurekbba09272018-02-14 07:16:27 -05004 * \brief Public Key cryptography abstraction layer: engine interface
Gilles Peskine5cc7bc52017-11-03 11:58:25 +01005 *
Andrzej Kurek0044ab12018-02-20 11:18:21 -05006 * This file defines the interface which public-key cryptography engines
Andrzej Kurekbba09272018-02-14 07:16:27 -05007 * (PK engines) must implement. A PK engine defines how a public-private
8 * key pair is represented and how to perform cryptographic operations
9 * with it. Mbed TLS contains built-in PK engines implemented either
10 * purely in software or with hardware acceleration support, depending
11 * on the target platform. In addition, it is possible to define custom
12 * opaque key engines that forward operation requests to cryptographic
13 * modules outside Mbed TLS, such as external cryptoprocessors or general
14 * PKCS#11 tokens.
15 */
16/*
Unknown60b25f02018-02-06 03:17:59 -050017 * Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010018 * SPDX-License-Identifier: Apache-2.0
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 *
32 * This file is part of mbed TLS (https://tls.mbed.org)
33 */
34
35#ifndef MBEDTLS_PK_INFO_H
36#define MBEDTLS_PK_INFO_H
37
38#if !defined(MBEDTLS_CONFIG_FILE)
39#include "config.h"
40#else
41#include MBEDTLS_CONFIG_FILE
42#endif
43
44#include "pk.h"
45
Gilles Peskine02768b42017-11-03 19:20:27 +010046#ifdef __cplusplus
47extern "C" {
48#endif
49
50/**
51 * Methods that opaque key pair objects must implement.
52 *
53 * Engines that interface with external cryptographic processors must
Unknown60b25f02018-02-06 03:17:59 -050054 * implement this interface. It allows using different engines for each key.
55 * Platform-specific hardware accelerators that can be used for all keys of
56 * a given type should not use this interface, but rather provide an
57 * alternative implementation of the respective cryptographic module - for
58 * example to use an RSA accelerator you can define MBEDTLS_RSA_ALT, and
59 * provide your own implementation of the RSA module.
60 *
61 * \warning: If you are using the PK interface to perform operations on
62 * keys, call the functions in pk.h. The interface in this file should only
63 * be used by implementers of opaque key engines.
Gilles Peskine02768b42017-11-03 19:20:27 +010064 *
65 * An engine for asymmetric cryptography must implement the interface
66 * described in this structure. The interface for the engine may be
67 * exposed in one of two ways:
68 *
69 * - Declare the mbedtls_pk_info_t structure and instruct users to call
70 * mbedtls_pk_setup with that structure.
71 * - Keep the mbedtls_pk_info_t structure hidden and declare a function
72 * to call instead of mbedtls_pk_setup. This function should have an
73 * interface of the form
Andrzej Kurek23537812018-02-14 07:43:37 -050074 * 'int mbedtls_pk_setup_myengine(mbedtls_pk_context *, ...)'
Gilles Peskine02768b42017-11-03 19:20:27 +010075 * where the extra parameters depend on the engine, e.g. handles to keys
76 * stored in an external cryptographic module.
77 *
78 * Unless otherwise indicated, functions returning int must return an
79 * Mbed TLS status code, either 0 for success or a negative value to indicate
80 * an error. It is recommended to use the MBEDTLS_ERR_PK_XXX error codes
81 * defined in pk.h.
82 *
83 * Some methods are optional; this is clearly indicated in their description.
84 * If a method is optional, then an opaque key implementation may put NULL
85 * in the corresponding field. The corresponding function in pk.h will
Andrzej Kurekbba09272018-02-14 07:16:27 -050086 * return #MBEDTLS_ERR_PK_TYPE_MISMATCH in this case.
Gilles Peskine02768b42017-11-03 19:20:27 +010087 *
Andrzej Kureke7353102018-01-22 07:14:34 -050088 *
89 * \warning: Do not declare this structure directly! It may be extended in
Andrzej Kurekbba09272018-02-14 07:16:27 -050090 * future versions of Mbed TLS. Call the macro
Unknown60b25f02018-02-06 03:17:59 -050091 * MBEDTLS_PK_OPAQUE_INFO_1() instead.
92 * This macro is guaranteed to take parameters with the same type
Andrzej Kureke7353102018-01-22 07:14:34 -050093 * and semantics as previous versions and fill any new field of the
94 * structure with sensible values.
Gilles Peskine02768b42017-11-03 19:20:27 +010095 */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010096struct mbedtls_pk_info_t
97{
Gilles Peskine02768b42017-11-03 19:20:27 +010098 /** Key pair type.
99 *
100 * mbedtls_pk_get_type() returns this value.
101 *
102 * For transparent keys, this contains an indication of supported
Andrzej Kurekbba09272018-02-14 07:16:27 -0500103 * algorithms. For opaque keys, this is #MBEDTLS_PK_OPAQUE. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100104 mbedtls_pk_type_t type;
105
Gilles Peskine02768b42017-11-03 19:20:27 +0100106 /** Type name.
107 *
108 * mbedtls_pk_get_name() returns this value. It must be a
109 * null-terminated string.
110 *
111 * For transparent keys, this reflects the key type. For opaque keys,
112 * this reflects the cryptographic module driver. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100113 const char *name;
114
Gilles Peskine02768b42017-11-03 19:20:27 +0100115 /** Get key size in bits.
116 *
117 * mbedtls_pk_get_bitlen() returns this value.
118 *
119 * This function cannot fail. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100120 size_t (*get_bitlen)( const void *ctx );
121
Unknown60b25f02018-02-06 03:17:59 -0500122 /** Tell if the context implements the algorithm specified by
Andrzej Kurek024ab062018-02-12 09:34:39 -0500123 * the provided type (e.g. ECKEY can do ECDSA).
Gilles Peskine02768b42017-11-03 19:20:27 +0100124 *
125 * mbedtls_pk_can_do() calls this function.
126 *
127 * This function is only based on the key type. It does not take any
128 * usage restrictions into account. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100129 int (*can_do)( const void * ctx, mbedtls_pk_type_t type );
130
Unknownd0d06022018-02-06 03:20:30 -0500131 /** Upper bound of the signature length
Andrzej Kurek8b6aaca2018-01-22 07:04:46 -0500132 *
Unknownd0d06022018-02-06 03:20:30 -0500133 * mbedtls_pk_get_signature_size() returns this value.
134 *
135 * In case of an error, or an unsupported key type, 0 should be returned.
Andrzej Kurek8b6aaca2018-01-22 07:04:46 -0500136 *
137 * Opaque implementations may omit this method if they do not support
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500138 * signing. */
Andrzej Kurek8b6aaca2018-01-22 07:04:46 -0500139 size_t (*signature_size_func)( const void *ctx );
140
Gilles Peskine02768b42017-11-03 19:20:27 +0100141 /** Verify signature
142 *
143 * mbedtls_pk_verify() calls this function.
144 *
145 * Opaque implementations may omit this method if they do not support
146 * signature verification. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100147 int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
148 const unsigned char *hash, size_t hash_len,
149 const unsigned char *sig, size_t sig_len );
150
Gilles Peskine02768b42017-11-03 19:20:27 +0100151 /** Make signature
152 *
153 * mbedtls_pk_sign() calls this function.
154 *
Andrzej Kurek024ab062018-02-12 09:34:39 -0500155 * Assumes that the buffer \c sig has room for
Gilles Peskine02768b42017-11-03 19:20:27 +0100156 * \c signature_size_func(ctx) bytes.
157 *
158 * The arguments \c f_rng and \c p_rng are provided in case the
159 * algorithm requires randomization. Implementations are not
160 * required to use it if they have their own random source. If \c
161 * f_rng is null, the implementation should operate if it can, and
162 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
163 *
164 * Opaque implementations may omit this method if they do not support
Andrzej Kurekbba09272018-02-14 07:16:27 -0500165 * signing. If this method is provided, so must be
166 * \ref mbedtls_pk_info_t.signature_size_func. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100167 int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
168 const unsigned char *hash, size_t hash_len,
169 unsigned char *sig, size_t *sig_len,
170 int (*f_rng)(void *, unsigned char *, size_t),
171 void *p_rng );
172
Gilles Peskine02768b42017-11-03 19:20:27 +0100173 /** Decrypt message
174 *
175 * mbedtls_pk_decrypt() calls this function.
176 *
177 * The arguments \c f_rng and \c p_rng are provided in case the
178 * algorithm requires randomization. Implementations are not
179 * required to use it if they have their own random source. If \c
180 * f_rng is null, the implementation should operate if it can, and
181 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
182 *
183 * Opaque implementations may omit this method if they do not support
184 * decryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100185 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
186 unsigned char *output, size_t *olen, size_t osize,
187 int (*f_rng)(void *, unsigned char *, size_t),
188 void *p_rng );
189
Gilles Peskine02768b42017-11-03 19:20:27 +0100190 /** Encrypt message
191 *
192 * mbedtls_pk_decrypt() calls this function.
193 *
194 * The arguments \c f_rng and \c p_rng are provided in case the
195 * algorithm requires randomization. Implementations are not
196 * required to use it if they have their own random source. If \c
197 * f_rng is null, the implementation should operate if it can, and
198 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
199 *
200 * Opaque implementations may omit this method if they do not support
201 * encryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100202 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
203 unsigned char *output, size_t *olen, size_t osize,
204 int (*f_rng)(void *, unsigned char *, size_t),
205 void *p_rng );
206
Gilles Peskine02768b42017-11-03 19:20:27 +0100207 /** Check public-private key pair
208 *
209 * mbedtls_pk_check_pair() calls this function on the private key pair
210 * object \c prv. The other argument \c pub may be of any type, but it
Andrzej Kurekbba09272018-02-14 07:16:27 -0500211 * is guaranteed to be initialized. The implementation is allowed to do
212 * a probabilistic and computationally expensive check.
Gilles Peskine02768b42017-11-03 19:20:27 +0100213 *
Andrzej Kurekbba09272018-02-14 07:16:27 -0500214 * If \c prv is an RSA key and \c pub is a transparent RSA key
215 * (i.e. \c pub has the type #MBEDTLS_PK_RSA or #MBEDTLS_PK_RSASSA_PSS),
216 * then \c check_pair_func must return 0 if the public key is
217 * mathematically equivalent to the public part of \c prv, and
218 * #MBEDTLS_ERR_RSA_KEY_CHECK_FAILED otherwise.
219 *
220 * If \c pub is an ECC key and \c pub is a transparent ECC key that can
221 * be used for ECDSA (i.e. \c pub has the type #MBEDTLS_PK_ECKEY or
Andrzej Kurek57b3ccb2018-02-21 05:13:28 -0500222 * #MBEDTLS_PK_ECDSA), then \c check_pair_func must return 0 if the public
223 * key is mathematically equivalent to the public part of \c prv, and
Andrzej Kurekbba09272018-02-14 07:16:27 -0500224 * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
225 *
226 * If \c pub is a transparent key (key of type #MBEDTLS_PK_RSA,
227 * #MBEDTLS_PK_RSASSA_PSS, #MBEDTLS_PK_ECKEY or #MBEDTLS_PK_ECDSA) whose
228 * type does not match the semantic type of \c prv (RSA, ECC or other),
Andrzej Kurek57b3ccb2018-02-21 05:13:28 -0500229 * then \c check_pair_func must return #MBEDTLS_ERR_PK_TYPE_MISMATCH.
Andrzej Kurekbba09272018-02-14 07:16:27 -0500230 *
Andrzej Kurekff8ddd12018-02-21 05:28:12 -0500231 * If \c pub and \c prv are opaque keys from the same engines (i.e. their
232 * pk_info fields are equal), then \c check_pair_func must return 0,
233 * #MBEDTLS_ERR_PK_TYPE_MISMATCH, or #MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
234 * or #MBEDTLS_ERR_ECP_BAD_INPUT_DATA as in the case of transparent keys.
Andrzej Kurekbba09272018-02-14 07:16:27 -0500235 *
236 * If \c pub is an opaque key which is not from the same engine as \c prv,
Andrzej Kurek57b3ccb2018-02-21 05:13:28 -0500237 * then \c check_pair_func may either return a semantically correct status
238 * as in the case of transparent keys, or it may return
Andrzej Kurekbba09272018-02-14 07:16:27 -0500239 * #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE.
240 *
Andrzej Kurek57b3ccb2018-02-21 05:13:28 -0500241 * Alternatively, \c check_pair_func may return another PK, RSA or ECP
242 * error code if applicable. */
Unknown4d092dc2018-02-08 07:45:41 -0500243 int (*check_pair_func)( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100244
Gilles Peskine02768b42017-11-03 19:20:27 +0100245 /** Allocate a new context
246 *
247 * mbedtls_pk_setup() calls this function.
248 *
249 * If this function returns NULL, the allocation is considered to
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500250 * have failed and the object remains uninitialized.
Gilles Peskine02768b42017-11-03 19:20:27 +0100251 *
252 * Opaque implementations may omit this method. In this case,
Andrzej Kurekbba09272018-02-14 07:16:27 -0500253 * mbedtls_pk_setup() will set the \c pk_ctx field of the
254 * mbedtls_pk_context object to NULL, and it is up to an engine-specific
255 * setup function to initialize the \c pk_ctx field. This is useful if
256 * the size of the memory depends on extra parameters passed to the
257 * engine-specific setup function. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100258 void * (*ctx_alloc_func)( void );
259
Gilles Peskine02768b42017-11-03 19:20:27 +0100260 /** Free the given context
261 *
262 * mbedtls_pk_free() calls this function. It must free the data allocated
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500263 * by \c ctx_alloc_func as well as any other resource that belongs to
264 * the object. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100265 void (*ctx_free_func)( void *ctx );
266
Gilles Peskine02768b42017-11-03 19:20:27 +0100267 /** Interface with the debug module
268 *
269 * mbedtls_pk_debug() calls this function.
270 *
271 * Opaque implementations may omit this method. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100272 void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
273
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100274};
275
Andrzej Kurekbba09272018-02-14 07:16:27 -0500276/**
Andrzej Kurekbba09272018-02-14 07:16:27 -0500277 * \brief Initializer for opaque key engines
278 *
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500279 * Methods that opaque key pair objects must implement.
280 *
Andrzej Kurekbba09272018-02-14 07:16:27 -0500281 * The value of this macro is a suitable initializer for an object of type
282 * mbedtls_pk_info_t. It is guaranteed to remain so in future versions of the
283 * library, even if the type mbedtls_pk_info_t changes.
284 *
285 * This macro is suitable for static initializers provided that all of its
286 * parameters are constant.
287 *
288 * \param name For transparent keys, this reflects the key type. For opaque
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500289 * keys, this reflects the cryptographic module driver.
Andrzej Kurekbba09272018-02-14 07:16:27 -0500290 * \param get_bitlen \ref mbedtls_pk_info_t.get_bitlen method
291 * \param can_do \ref mbedtls_pk_info_t.can_do method
292 * \param signature_size_func \ref mbedtls_pk_info_t.signature_size_func method
293 * \param verify_func \ref mbedtls_pk_info_t.verify_func method
294 * \param sign_func \ref mbedtls_pk_info_t.sign_func method
295 * \param decrypt_func \ref mbedtls_pk_info_t.decrypt_func method
296 * \param encrypt_func \ref mbedtls_pk_info_t.encrypt_func method
297 * \param check_pair_func \ref mbedtls_pk_info_t.check_pair_func method
298 * \param ctx_alloc_func \ref mbedtls_pk_info_t.ctx_alloc_func method
299 * \param ctx_free_func \ref mbedtls_pk_info_t.ctx_free_func method
300 * \param debug_func \ref mbedtls_pk_info_t.debug_func method
301 *
302 * \return Initializer for an object of type mbedtls_pk_info_t with the
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500303 * specified field values */
Andrzej Kureke7353102018-01-22 07:14:34 -0500304#define MBEDTLS_PK_OPAQUE_INFO_1( \
305 name \
306 , get_bitlen \
307 , can_do \
308 , signature_size_func \
309 , verify_func \
310 , sign_func \
311 , decrypt_func \
312 , encrypt_func \
313 , check_pair_func \
314 , ctx_alloc_func \
315 , ctx_free_func \
316 , debug_func \
317 ) \
318 { \
319 MBEDTLS_PK_OPAQUE \
320 , name \
321 , get_bitlen \
322 , can_do \
323 , signature_size_func \
324 , verify_func \
325 , sign_func \
326 , decrypt_func \
327 , encrypt_func \
328 , check_pair_func \
329 , ctx_alloc_func \
330 , ctx_free_func \
331 , debug_func \
332 }
333
Gilles Peskine02768b42017-11-03 19:20:27 +0100334#ifdef __cplusplus
335}
336#endif
337
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100338#endif /* MBEDTLS_PK_INFO_H */