blob: d1d95ef4f0bf12dee1c52cace73bc05b217dc1ad [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
Andrzej Kurek8b6aaca2018-01-22 07:04:46 -0500123 /** Signature size
124 *
125 * mbedtls_pk_signature_size() returns this value.
126 *
127 * Opaque implementations may omit this method if they do not support
128 * signature. */
129 size_t (*signature_size_func)( const void *ctx );
130
Gilles Peskine02768b42017-11-03 19:20:27 +0100131 /** Verify signature
132 *
133 * mbedtls_pk_verify() calls this function.
134 *
135 * Opaque implementations may omit this method if they do not support
136 * signature verification. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100137 int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
138 const unsigned char *hash, size_t hash_len,
139 const unsigned char *sig, size_t sig_len );
140
Gilles Peskine02768b42017-11-03 19:20:27 +0100141 /** Make signature
142 *
143 * mbedtls_pk_sign() calls this function.
144 *
145 * Assume that the buffer \c sig has room for
146 * \c signature_size_func(ctx) bytes.
147 *
148 * The arguments \c f_rng and \c p_rng are provided in case the
149 * algorithm requires randomization. Implementations are not
150 * required to use it if they have their own random source. If \c
151 * f_rng is null, the implementation should operate if it can, and
152 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
153 *
154 * Opaque implementations may omit this method if they do not support
155 * signature. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100156 int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
157 const unsigned char *hash, size_t hash_len,
158 unsigned char *sig, size_t *sig_len,
159 int (*f_rng)(void *, unsigned char *, size_t),
160 void *p_rng );
161
Gilles Peskine02768b42017-11-03 19:20:27 +0100162 /** Decrypt message
163 *
164 * mbedtls_pk_decrypt() calls this function.
165 *
166 * The arguments \c f_rng and \c p_rng are provided in case the
167 * algorithm requires randomization. Implementations are not
168 * required to use it if they have their own random source. If \c
169 * f_rng is null, the implementation should operate if it can, and
170 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
171 *
172 * Opaque implementations may omit this method if they do not support
173 * decryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100174 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
175 unsigned char *output, size_t *olen, size_t osize,
176 int (*f_rng)(void *, unsigned char *, size_t),
177 void *p_rng );
178
Gilles Peskine02768b42017-11-03 19:20:27 +0100179 /** Encrypt message
180 *
181 * mbedtls_pk_decrypt() calls this function.
182 *
183 * The arguments \c f_rng and \c p_rng are provided in case the
184 * algorithm requires randomization. Implementations are not
185 * required to use it if they have their own random source. If \c
186 * f_rng is null, the implementation should operate if it can, and
187 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
188 *
189 * Opaque implementations may omit this method if they do not support
190 * encryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100191 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
192 unsigned char *output, size_t *olen, size_t osize,
193 int (*f_rng)(void *, unsigned char *, size_t),
194 void *p_rng );
195
Gilles Peskine02768b42017-11-03 19:20:27 +0100196 /** Check public-private key pair
197 *
198 * mbedtls_pk_check_pair() calls this function on the private key pair
199 * object \c prv. The other argument \c pub may be of any type, but it
200 * is guaranteed to be initialized.
201 *
202 * Opaque implementations may omit this method. */
203 int (*check_pair_func)( const mbedtls_pk_context *pub, const void *prv );
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100204
Gilles Peskine02768b42017-11-03 19:20:27 +0100205 /** Allocate a new context
206 *
207 * mbedtls_pk_setup() calls this function.
208 *
209 * If this function returns NULL, the allocation is considered to
210 * have failed and the the object remains uninitialized.
211 *
212 * Opaque implementations may omit this method. In this case,
213 * mbedtls_pk_setup will set the \c pk_ctx field of the mbedtls_pk_context
214 * object to NULL, and it is up to an engine-specific setup function to
215 * initialize the \c pk_ctx field. This is useful if the size of the
216 * memory depends on extra parameters passed to the engine-specific setup
217 * function. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100218 void * (*ctx_alloc_func)( void );
219
Gilles Peskine02768b42017-11-03 19:20:27 +0100220 /** Free the given context
221 *
222 * mbedtls_pk_free() calls this function. It must free the data allocated
223 * by \b ctx_alloc_func as well as any other resource that belongs to
224 * the object.
225 * */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100226 void (*ctx_free_func)( void *ctx );
227
Gilles Peskine02768b42017-11-03 19:20:27 +0100228 /** Interface with the debug module
229 *
230 * mbedtls_pk_debug() calls this function.
231 *
232 * Opaque implementations may omit this method. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100233 void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
234
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100235};
236
Andrzej Kureke7353102018-01-22 07:14:34 -0500237#define MBEDTLS_PK_OPAQUE_INFO_1( \
238 name \
239 , get_bitlen \
240 , can_do \
241 , signature_size_func \
242 , verify_func \
243 , sign_func \
244 , decrypt_func \
245 , encrypt_func \
246 , check_pair_func \
247 , ctx_alloc_func \
248 , ctx_free_func \
249 , debug_func \
250 ) \
251 { \
252 MBEDTLS_PK_OPAQUE \
253 , name \
254 , get_bitlen \
255 , can_do \
256 , signature_size_func \
257 , verify_func \
258 , sign_func \
259 , decrypt_func \
260 , encrypt_func \
261 , check_pair_func \
262 , ctx_alloc_func \
263 , ctx_free_func \
264 , debug_func \
265 }
266
Gilles Peskine02768b42017-11-03 19:20:27 +0100267#ifdef __cplusplus
268}
269#endif
270
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100271#endif /* MBEDTLS_PK_INFO_H */