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