blob: a90e489c464f96048cc4c75fd211b82004e4241e [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
Gilles Peskine02768b42017-11-03 19:20:27 +0100109 /** Verify signature
110 *
111 * mbedtls_pk_verify() calls this function.
112 *
113 * Opaque implementations may omit this method if they do not support
114 * signature verification. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100115 int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
116 const unsigned char *hash, size_t hash_len,
117 const unsigned char *sig, size_t sig_len );
118
Gilles Peskine02768b42017-11-03 19:20:27 +0100119 /** Make signature
120 *
121 * mbedtls_pk_sign() calls this function.
122 *
123 * Assume that the buffer \c sig has room for
124 * \c signature_size_func(ctx) bytes.
125 *
126 * The arguments \c f_rng and \c p_rng are provided in case the
127 * algorithm requires randomization. Implementations are not
128 * required to use it if they have their own random source. If \c
129 * f_rng is null, the implementation should operate if it can, and
130 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
131 *
132 * Opaque implementations may omit this method if they do not support
133 * signature. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100134 int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
135 const unsigned char *hash, size_t hash_len,
136 unsigned char *sig, size_t *sig_len,
137 int (*f_rng)(void *, unsigned char *, size_t),
138 void *p_rng );
139
Gilles Peskine02768b42017-11-03 19:20:27 +0100140 /** Decrypt message
141 *
142 * mbedtls_pk_decrypt() calls this function.
143 *
144 * The arguments \c f_rng and \c p_rng are provided in case the
145 * algorithm requires randomization. Implementations are not
146 * required to use it if they have their own random source. If \c
147 * f_rng is null, the implementation should operate if it can, and
148 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
149 *
150 * Opaque implementations may omit this method if they do not support
151 * decryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100152 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
153 unsigned char *output, size_t *olen, size_t osize,
154 int (*f_rng)(void *, unsigned char *, size_t),
155 void *p_rng );
156
Gilles Peskine02768b42017-11-03 19:20:27 +0100157 /** Encrypt message
158 *
159 * mbedtls_pk_decrypt() calls this function.
160 *
161 * The arguments \c f_rng and \c p_rng are provided in case the
162 * algorithm requires randomization. Implementations are not
163 * required to use it if they have their own random source. If \c
164 * f_rng is null, the implementation should operate if it can, and
165 * return #MBEDTLS_ERR_PK_BAD_INPUT_DATA otherwise.
166 *
167 * Opaque implementations may omit this method if they do not support
168 * encryption. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100169 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
170 unsigned char *output, size_t *olen, size_t osize,
171 int (*f_rng)(void *, unsigned char *, size_t),
172 void *p_rng );
173
Gilles Peskine02768b42017-11-03 19:20:27 +0100174 /** Check public-private key pair
175 *
176 * mbedtls_pk_check_pair() calls this function on the private key pair
177 * object \c prv. The other argument \c pub may be of any type, but it
178 * is guaranteed to be initialized.
179 *
180 * Opaque implementations may omit this method. */
181 int (*check_pair_func)( const mbedtls_pk_context *pub, const void *prv );
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100182
Gilles Peskine02768b42017-11-03 19:20:27 +0100183 /** Allocate a new context
184 *
185 * mbedtls_pk_setup() calls this function.
186 *
187 * If this function returns NULL, the allocation is considered to
188 * have failed and the the object remains uninitialized.
189 *
190 * Opaque implementations may omit this method. In this case,
191 * mbedtls_pk_setup will set the \c pk_ctx field of the mbedtls_pk_context
192 * object to NULL, and it is up to an engine-specific setup function to
193 * initialize the \c pk_ctx field. This is useful if the size of the
194 * memory depends on extra parameters passed to the engine-specific setup
195 * function. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100196 void * (*ctx_alloc_func)( void );
197
Gilles Peskine02768b42017-11-03 19:20:27 +0100198 /** Free the given context
199 *
200 * mbedtls_pk_free() calls this function. It must free the data allocated
201 * by \b ctx_alloc_func as well as any other resource that belongs to
202 * the object.
203 * */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100204 void (*ctx_free_func)( void *ctx );
205
Gilles Peskine02768b42017-11-03 19:20:27 +0100206 /** Interface with the debug module
207 *
208 * mbedtls_pk_debug() calls this function.
209 *
210 * Opaque implementations may omit this method. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100211 void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
212
Gilles Peskine02768b42017-11-03 19:20:27 +0100213 /** Signature size
214 *
215 * mbedtls_pk_signature_size() returns this value.
216 *
217 * Opaque implementations may omit this method. In this case, the value
218 * returned by \c get_bitlen (rounded up to a whole number of bytes)
219 * is used instead. */
Gilles Peskine5cc7bc52017-11-03 11:58:25 +0100220 size_t (*signature_size_func)( const void *ctx );
221};
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 */