blob: a852ab8e37f0857c9723483f9d3e2a92310539d3 [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 *
6 * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24#ifndef MBEDTLS_PK_INFO_H
25#define MBEDTLS_PK_INFO_H
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
33#include "pk.h"
34
Gilles Peskine02768b42017-11-03 19:20:27 +010035#ifdef __cplusplus
36extern "C" {
37#endif
38
39/**
40 * Methods that opaque key pair objects must implement.
41 *
42 * Engines that interface with external cryptographic processors must
43 * implement this interface. Platform-specific hardware accelerators
44 * that can be used for all keys of a given type should use alternative
45 * ("xxx_alt") interfaces instead. This interface allows using different
46 * engines for each key.
47 *
48 * An engine for asymmetric cryptography must implement the interface
49 * described in this structure. The interface for the engine may be
50 * exposed in one of two ways:
51 *
52 * - Declare the mbedtls_pk_info_t structure and instruct users to call
53 * mbedtls_pk_setup with that structure.
54 * - Keep the mbedtls_pk_info_t structure hidden and declare a function
55 * to call instead of mbedtls_pk_setup. This function should have an
56 * interface of the form
57 * `int mbedtls_pk_setup_myengine(mbedtls_pk_context *, ...)`
58 * where the extra parameters depend on the engine, e.g. handles to keys
59 * stored in an external cryptographic module.
60 *
61 * Unless otherwise indicated, functions returning int must return an
62 * Mbed TLS status code, either 0 for success or a negative value to indicate
63 * an error. It is recommended to use the MBEDTLS_ERR_PK_XXX error codes
64 * defined in pk.h.
65 *
66 * Some methods are optional; this is clearly indicated in their description.
67 * If a method is optional, then an opaque key implementation may put NULL
68 * in the corresponding field. The corresponding function in pk.h will
69 * return MBEDTLS_ERR_PK_TYPE_MISMATCH in this case.
70 *
71 * \note If you are using the PK interface to perform operations on
72 * keys, call the functions in pk.h. The interface in this file should only
73 * be used by implementers of opaque key engines.
74 */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010075struct mbedtls_pk_info_t
76{
Gilles Peskine02768b42017-11-03 19:20:27 +010077 /** Key pair type.
78 *
79 * mbedtls_pk_get_type() returns this value.
80 *
81 * For transparent keys, this contains an indication of supported
82 * algorithms. For opaque keys, this is \c MBEDTLS_PK_OPAQUE. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010083 mbedtls_pk_type_t type;
84
Gilles Peskine02768b42017-11-03 19:20:27 +010085 /** Type name.
86 *
87 * mbedtls_pk_get_name() returns this value. It must be a
88 * null-terminated string.
89 *
90 * For transparent keys, this reflects the key type. For opaque keys,
91 * this reflects the cryptographic module driver. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010092 const char *name;
93
Gilles Peskine02768b42017-11-03 19:20:27 +010094 /** Get key size in bits.
95 *
96 * mbedtls_pk_get_bitlen() returns this value.
97 *
98 * This function cannot fail. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010099 size_t (*get_bitlen)( const void *ctx );
100
Gilles Peskine02768b42017-11-03 19:20:27 +0100101 /** Tell if the context implements this type (e.g.\ ECKEY can do ECDSA).
102 *
103 * mbedtls_pk_can_do() calls this function.
104 *
105 * This function is only based on the key type. It does not take any
106 * usage restrictions into account. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100107 int (*can_do)( const void * ctx, mbedtls_pk_type_t type );
108
Andrzej Kurek8b6aaca2018-01-22 07:04:46 -0500109 /** Signature size
110 *
111 * mbedtls_pk_signature_size() returns this value.
112 *
113 * Opaque implementations may omit this method if they do not support
114 * signature. */
115 size_t (*signature_size_func)( const void *ctx );
116
Gilles Peskine02768b42017-11-03 19:20:27 +0100117 /** Verify signature
118 *
119 * mbedtls_pk_verify() calls this function.
120 *
121 * Opaque implementations may omit this method if they do not support
122 * signature verification. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100123 int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
124 const unsigned char *hash, size_t hash_len,
125 const unsigned char *sig, size_t sig_len );
126
Gilles Peskine02768b42017-11-03 19:20:27 +0100127 /** Make signature
128 *
129 * mbedtls_pk_sign() calls this function.
130 *
131 * Assume that the buffer \c sig has room for
132 * \c signature_size_func(ctx) bytes.
133 *
134 * The arguments \c f_rng and \c p_rng are provided in case the
135 * algorithm requires randomization. Implementations are not
136 * required to use it if they have their own random source. If \c
137 * f_rng is null, the implementation should operate if it can, and
138 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
139 *
140 * Opaque implementations may omit this method if they do not support
141 * signature. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100142 int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
143 const unsigned char *hash, size_t hash_len,
144 unsigned char *sig, size_t *sig_len,
145 int (*f_rng)(void *, unsigned char *, size_t),
146 void *p_rng );
147
Gilles Peskine02768b42017-11-03 19:20:27 +0100148 /** Decrypt message
149 *
150 * mbedtls_pk_decrypt() calls this function.
151 *
152 * The arguments \c f_rng and \c p_rng are provided in case the
153 * algorithm requires randomization. Implementations are not
154 * required to use it if they have their own random source. If \c
155 * f_rng is null, the implementation should operate if it can, and
156 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
157 *
158 * Opaque implementations may omit this method if they do not support
159 * decryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100160 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
161 unsigned char *output, size_t *olen, size_t osize,
162 int (*f_rng)(void *, unsigned char *, size_t),
163 void *p_rng );
164
Gilles Peskine02768b42017-11-03 19:20:27 +0100165 /** Encrypt 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 * encryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100177 int (*encrypt_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 Peskine02768b42017-11-03 19:20:27 +0100182 /** Check public-private key pair
183 *
184 * mbedtls_pk_check_pair() calls this function on the private key pair
185 * object \c prv. The other argument \c pub may be of any type, but it
186 * is guaranteed to be initialized.
187 *
188 * Opaque implementations may omit this method. */
189 int (*check_pair_func)( const mbedtls_pk_context *pub, const void *prv );
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100190
Gilles Peskine02768b42017-11-03 19:20:27 +0100191 /** Allocate a new context
192 *
193 * mbedtls_pk_setup() calls this function.
194 *
195 * If this function returns NULL, the allocation is considered to
196 * have failed and the the object remains uninitialized.
197 *
198 * Opaque implementations may omit this method. In this case,
199 * mbedtls_pk_setup will set the \c pk_ctx field of the mbedtls_pk_context
200 * object to NULL, and it is up to an engine-specific setup function to
201 * initialize the \c pk_ctx field. This is useful if the size of the
202 * memory depends on extra parameters passed to the engine-specific setup
203 * function. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100204 void * (*ctx_alloc_func)( void );
205
Gilles Peskine02768b42017-11-03 19:20:27 +0100206 /** Free the given context
207 *
208 * mbedtls_pk_free() calls this function. It must free the data allocated
209 * by \b ctx_alloc_func as well as any other resource that belongs to
210 * the object.
211 * */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100212 void (*ctx_free_func)( void *ctx );
213
Gilles Peskine02768b42017-11-03 19:20:27 +0100214 /** Interface with the debug module
215 *
216 * mbedtls_pk_debug() calls this function.
217 *
218 * Opaque implementations may omit this method. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100219 void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
220
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100221};
222
Gilles Peskine02768b42017-11-03 19:20:27 +0100223#ifdef __cplusplus
224}
225#endif
226
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100227#endif /* MBEDTLS_PK_INFO_H */