Add missing EVP param utility functions
[openssl.git] / include / openssl / core_numbers.h
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OSSL_CORE_NUMBERS_H
11 # define OSSL_CORE_NUMBERS_H
12
13 # include <stdarg.h>
14 # include <openssl/core.h>
15
16 # ifdef __cplusplus
17 extern "C" {
18 # endif
19
20 /*-
21  * Identities
22  * ----------
23  *
24  * All series start with 1, to allow 0 to be an array terminator.
25  * For any FUNC identity, we also provide a function signature typedef
26  * and a static inline function to extract a function pointer from a
27  * OSSL_DISPATCH element in a type safe manner.
28  *
29  * Names:
30  * for any function base name 'foo' (uppercase form 'FOO'), we will have
31  * the following:
32  * - a macro for the identity with the name OSSL_FUNC_'FOO' or derivates
33  *   thereof (to be specified further down)
34  * - a function signature typedef with the name OSSL_'foo'_fn
35  * - a function pointer extractor function with the name OSSL_'foo'
36  */
37
38 /*
39  * Helper macro to create the function signature typedef and the extractor
40  * |type| is the return-type of the function, |name| is the name of the
41  * function to fetch, and |args| is a parenthesized list of parameters
42  * for the function (that is, it is |name|'s function signature).
43  */
44 #define OSSL_CORE_MAKE_FUNC(type,name,args)                             \
45     typedef type (OSSL_##name##_fn)args;                                \
46     static ossl_inline \
47     OSSL_##name##_fn *OSSL_get_##name(const OSSL_DISPATCH *opf)         \
48     {                                                                   \
49         return (OSSL_##name##_fn *)opf->function;                       \
50     }
51
52 /*
53  * Core function identities, for the two OSSL_DISPATCH tables being passed
54  * in the OSSL_provider_init call.
55  *
56  * 0 serves as a marker for the end of the OSSL_DISPATCH array, and must
57  * therefore NEVER be used as a function identity.
58  */
59 /* Functions provided by the Core to the provider, reserved numbers 1-1023 */
60 # define OSSL_FUNC_CORE_GET_PARAM_TYPES        1
61 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *,
62                     core_get_param_types,(const OSSL_PROVIDER *prov))
63 # define OSSL_FUNC_CORE_GET_PARAMS             2
64 OSSL_CORE_MAKE_FUNC(int,core_get_params,(const OSSL_PROVIDER *prov,
65                                          OSSL_PARAM params[]))
66 # define OSSL_FUNC_CORE_THREAD_START           3
67 OSSL_CORE_MAKE_FUNC(int,core_thread_start,(const OSSL_PROVIDER *prov,
68                                            OSSL_thread_stop_handler_fn handfn))
69 # define OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT    4
70 OSSL_CORE_MAKE_FUNC(OPENSSL_CTX *,core_get_library_context,
71                     (const OSSL_PROVIDER *prov))
72 # define OSSL_FUNC_CORE_NEW_ERROR              5
73 OSSL_CORE_MAKE_FUNC(void,core_new_error,(const OSSL_PROVIDER *prov))
74 # define OSSL_FUNC_CORE_SET_ERROR_DEBUG        6
75 OSSL_CORE_MAKE_FUNC(void,core_set_error_debug,
76                     (const OSSL_PROVIDER *prov,
77                      const char *file, int line, const char *func))
78 # define OSSL_FUNC_CORE_VSET_ERROR             7
79 OSSL_CORE_MAKE_FUNC(void,core_vset_error,
80                     (const OSSL_PROVIDER *prov,
81                      uint32_t reason, const char *fmt, va_list args))
82
83 /* Memory allocation, freeing, clearing. */
84 #define OSSL_FUNC_CRYPTO_MALLOC               10
85 OSSL_CORE_MAKE_FUNC(void *,
86         CRYPTO_malloc, (size_t num, const char *file, int line))
87 #define OSSL_FUNC_CRYPTO_ZALLOC               11
88 OSSL_CORE_MAKE_FUNC(void *,
89         CRYPTO_zalloc, (size_t num, const char *file, int line))
90 #define OSSL_FUNC_CRYPTO_FREE                 12
91 OSSL_CORE_MAKE_FUNC(void,
92         CRYPTO_free, (void *ptr, const char *file, int line))
93 #define OSSL_FUNC_CRYPTO_CLEAR_FREE           13
94 OSSL_CORE_MAKE_FUNC(void,
95         CRYPTO_clear_free, (void *ptr, size_t num, const char *file, int line))
96 #define OSSL_FUNC_CRYPTO_REALLOC              14
97 OSSL_CORE_MAKE_FUNC(void *,
98         CRYPTO_realloc, (void *addr, size_t num, const char *file, int line))
99 #define OSSL_FUNC_CRYPTO_CLEAR_REALLOC        15
100 OSSL_CORE_MAKE_FUNC(void *,
101         CRYPTO_clear_realloc, (void *addr, size_t old_num, size_t num, const char *file, int line))
102 #define OSSL_FUNC_CRYPTO_SECURE_MALLOC        16
103 OSSL_CORE_MAKE_FUNC(void *,
104         CRYPTO_secure_malloc, (size_t num, const char *file, int line))
105 #define OSSL_FUNC_CRYPTO_SECURE_ZALLOC        17
106 OSSL_CORE_MAKE_FUNC(void *,
107         CRYPTO_secure_zalloc, (size_t num, const char *file, int line))
108 #define OSSL_FUNC_CRYPTO_SECURE_FREE          18
109 OSSL_CORE_MAKE_FUNC(void,
110         CRYPTO_secure_free, (void *ptr, const char *file, int line))
111 #define OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE    19
112 OSSL_CORE_MAKE_FUNC(void,
113         CRYPTO_secure_clear_free, (void *ptr, size_t num, const char *file, int line))
114 #define OSSL_FUNC_CRYPTO_SECURE_ALLOCATED     20
115 OSSL_CORE_MAKE_FUNC(int,
116         CRYPTO_secure_allocated, (const void *ptr))
117 #define OSSL_FUNC_OPENSSL_CLEANSE             21
118 OSSL_CORE_MAKE_FUNC(void,
119         OPENSSL_cleanse, (void *ptr, size_t len))
120
121 /* Functions provided by the provider to the Core, reserved numbers 1024-1535 */
122 # define OSSL_FUNC_PROVIDER_TEARDOWN         1024
123 OSSL_CORE_MAKE_FUNC(void,provider_teardown,(void *provctx))
124 # define OSSL_FUNC_PROVIDER_GET_PARAM_TYPES  1025
125 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *,
126                     provider_get_param_types,(void *provctx))
127 # define OSSL_FUNC_PROVIDER_GET_PARAMS       1026
128 OSSL_CORE_MAKE_FUNC(int,provider_get_params,(void *provctx,
129                                              OSSL_PARAM params[]))
130 # define OSSL_FUNC_PROVIDER_QUERY_OPERATION  1027
131 OSSL_CORE_MAKE_FUNC(const OSSL_ALGORITHM *,provider_query_operation,
132                     (void *provctx, int operation_id, const int *no_store))
133 # define OSSL_FUNC_PROVIDER_GET_REASON_STRINGS 1028
134 OSSL_CORE_MAKE_FUNC(const OSSL_ITEM *,provider_get_reason_strings,
135                     (void *provctx))
136
137 /* Digests */
138
139 # define OSSL_OP_DIGEST                              1
140
141 # define OSSL_FUNC_DIGEST_NEWCTX                     1
142 # define OSSL_FUNC_DIGEST_INIT                       2
143 # define OSSL_FUNC_DIGEST_UPDATE                     3
144 # define OSSL_FUNC_DIGEST_FINAL                      4
145 # define OSSL_FUNC_DIGEST_DIGEST                     5
146 # define OSSL_FUNC_DIGEST_FREECTX                    6
147 # define OSSL_FUNC_DIGEST_DUPCTX                     7
148 # define OSSL_FUNC_DIGEST_GET_PARAMS                 8
149 # define OSSL_FUNC_DIGEST_CTX_SET_PARAMS             9
150 # define OSSL_FUNC_DIGEST_CTX_GET_PARAMS            10
151 # define OSSL_FUNC_DIGEST_GETTABLE_PARAMS           11
152 # define OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS       12
153 # define OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS       13
154
155 OSSL_CORE_MAKE_FUNC(void *, OP_digest_newctx, (void *provctx))
156 OSSL_CORE_MAKE_FUNC(int, OP_digest_init, (void *dctx))
157 OSSL_CORE_MAKE_FUNC(int, OP_digest_update,
158                     (void *dctx, const unsigned char *in, size_t inl))
159 OSSL_CORE_MAKE_FUNC(int, OP_digest_final,
160                     (void *dctx,
161                      unsigned char *out, size_t *outl, size_t outsz))
162 OSSL_CORE_MAKE_FUNC(int, OP_digest_digest,
163                     (void *provctx, const unsigned char *in, size_t inl,
164                      unsigned char *out, size_t *outl, size_t outsz))
165
166 OSSL_CORE_MAKE_FUNC(void, OP_digest_freectx, (void *dctx))
167 OSSL_CORE_MAKE_FUNC(void *, OP_digest_dupctx, (void *dctx))
168
169 OSSL_CORE_MAKE_FUNC(int, OP_digest_get_params, (OSSL_PARAM params[]))
170 OSSL_CORE_MAKE_FUNC(int, OP_digest_ctx_set_params,
171                     (void *vctx, const OSSL_PARAM params[]))
172 OSSL_CORE_MAKE_FUNC(int, OP_digest_ctx_get_params,
173                     (void *vctx, OSSL_PARAM params[]))
174 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_digest_gettable_params, (void))
175 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_digest_settable_ctx_params, (void))
176 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_digest_gettable_ctx_params, (void))
177
178 /* Symmetric Ciphers */
179
180 # define OSSL_OP_CIPHER                              2
181
182 # define OSSL_FUNC_CIPHER_NEWCTX                     1
183 # define OSSL_FUNC_CIPHER_ENCRYPT_INIT               2
184 # define OSSL_FUNC_CIPHER_DECRYPT_INIT               3
185 # define OSSL_FUNC_CIPHER_UPDATE                     4
186 # define OSSL_FUNC_CIPHER_FINAL                      5
187 # define OSSL_FUNC_CIPHER_CIPHER                     6
188 # define OSSL_FUNC_CIPHER_FREECTX                    7
189 # define OSSL_FUNC_CIPHER_DUPCTX                     8
190 # define OSSL_FUNC_CIPHER_GET_PARAMS                 9
191 # define OSSL_FUNC_CIPHER_CTX_GET_PARAMS            10
192 # define OSSL_FUNC_CIPHER_CTX_SET_PARAMS            11
193 # define OSSL_FUNC_CIPHER_GETTABLE_PARAMS           12
194 # define OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS       13
195 # define OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS       14
196
197 OSSL_CORE_MAKE_FUNC(void *, OP_cipher_newctx, (void *provctx))
198 OSSL_CORE_MAKE_FUNC(int, OP_cipher_encrypt_init, (void *cctx,
199                                                   const unsigned char *key,
200                                                   size_t keylen,
201                                                   const unsigned char *iv,
202                                                   size_t ivlen))
203 OSSL_CORE_MAKE_FUNC(int, OP_cipher_decrypt_init, (void *cctx,
204                                                   const unsigned char *key,
205                                                   size_t keylen,
206                                                   const unsigned char *iv,
207                                                   size_t ivlen))
208 OSSL_CORE_MAKE_FUNC(int, OP_cipher_update,
209                     (void *cctx,
210                      unsigned char *out, size_t *outl, size_t outsize,
211                      const unsigned char *in, size_t inl))
212 OSSL_CORE_MAKE_FUNC(int, OP_cipher_final,
213                     (void *cctx,
214                      unsigned char *out, size_t *outl, size_t outsize))
215 OSSL_CORE_MAKE_FUNC(int, OP_cipher_cipher,
216                     (void *cctx,
217                      unsigned char *out, size_t *outl, size_t outsize,
218                      const unsigned char *in, size_t inl))
219 OSSL_CORE_MAKE_FUNC(void, OP_cipher_freectx, (void *cctx))
220 OSSL_CORE_MAKE_FUNC(void *, OP_cipher_dupctx, (void *cctx))
221 OSSL_CORE_MAKE_FUNC(int, OP_cipher_get_params, (OSSL_PARAM params[]))
222 OSSL_CORE_MAKE_FUNC(int, OP_cipher_ctx_get_params, (void *cctx,
223                                                     OSSL_PARAM params[]))
224 OSSL_CORE_MAKE_FUNC(int, OP_cipher_ctx_set_params, (void *cctx,
225                                                     const OSSL_PARAM params[]))
226 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_cipher_gettable_params,
227                     (void))
228 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_cipher_settable_ctx_params,
229                     (void))
230 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_cipher_gettable_ctx_params,
231                     (void))
232
233 /*-
234  * Key management
235  *
236  * Key domain parameter references can be created in several manners:
237  * - by importing the domain parameter material via an OSSL_PARAM array.
238  * - by generating key domain parameters, given input via an OSSL_PARAM
239  *   array.
240  *
241  * Key references can be created in several manners:
242  * - by importing the key material via an OSSL_PARAM array.
243  * - by generating a key, given optional domain parameters and
244  *   additional keygen parameters.
245  *   If domain parameters are given, they must have been generated using
246  *   the domain parameter generator functions.
247  *   If the domain parameters comes from a different provider, results
248  *   are undefined.
249  *   THE CALLER MUST ENSURE THAT CORRECT DOMAIN PARAMETERS ARE USED.
250  * - by loading an internal key, given a binary blob that forms an identity.
251  *   THE CALLER MUST ENSURE THAT A CORRECT IDENTITY IS USED.
252  */
253
254 # define OSSL_OP_KEYMGMT                           10
255
256 /* Key domain parameter creation and destruction */
257 # define OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS          1
258 # define OSSL_FUNC_KEYMGMT_GENDOMPARAMS             2
259 # define OSSL_FUNC_KEYMGMT_FREEDOMPARAMS            3
260 OSSL_CORE_MAKE_FUNC(void *, OP_keymgmt_importdomparams,
261                     (void *provctx, const OSSL_PARAM params[]))
262 OSSL_CORE_MAKE_FUNC(void *, OP_keymgmt_gendomparams,
263                     (void *provctx, const OSSL_PARAM params[]))
264 OSSL_CORE_MAKE_FUNC(void, OP_keymgmt_freedomparams, (void *domparams))
265
266 /* Key domain parameter export */
267 # define OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS          4
268 OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_exportdomparams,
269                     (void *domparams, OSSL_PARAM params[]))
270
271 /* Key domain parameter discovery */
272 # define OSSL_FUNC_KEYMGMT_IMPORTDOMPARAM_TYPES     5
273 # define OSSL_FUNC_KEYMGMT_EXPORTDOMPARAM_TYPES     6
274 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_importdomparam_types,
275                     (void))
276 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_exportdomparam_types,
277                     (void))
278
279 /* Key creation and destruction */
280 # define OSSL_FUNC_KEYMGMT_IMPORTKEY               10
281 # define OSSL_FUNC_KEYMGMT_GENKEY                  11
282 # define OSSL_FUNC_KEYMGMT_LOADKEY                 12
283 # define OSSL_FUNC_KEYMGMT_FREEKEY                 13
284 OSSL_CORE_MAKE_FUNC(void *, OP_keymgmt_importkey,
285                     (void *provctx, const OSSL_PARAM params[]))
286 OSSL_CORE_MAKE_FUNC(void *, OP_keymgmt_genkey,
287                     (void *provctx,
288                      void *domparams, const OSSL_PARAM genkeyparams[]))
289 OSSL_CORE_MAKE_FUNC(void *, OP_keymgmt_loadkey,
290                     (void *provctx, void *id, size_t idlen))
291 OSSL_CORE_MAKE_FUNC(void, OP_keymgmt_freekey, (void *key))
292
293 /* Key export */
294 # define OSSL_FUNC_KEYMGMT_EXPORTKEY               14
295 OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_exportkey,
296                     (void *key, OSSL_PARAM params[]))
297
298 /* Key discovery */
299 # define OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES         15
300 # define OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES         16
301 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_importkey_types, (void))
302 OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_exportkey_types, (void))
303
304 /* Key Exchange */
305
306 # define OSSL_OP_KEYEXCH                              11
307
308 # define OSSL_FUNC_KEYEXCH_NEWCTX                      1
309 # define OSSL_FUNC_KEYEXCH_INIT                        2
310 # define OSSL_FUNC_KEYEXCH_DERIVE                      3
311 # define OSSL_FUNC_KEYEXCH_SET_PEER                    4
312 # define OSSL_FUNC_KEYEXCH_FREECTX                     5
313 # define OSSL_FUNC_KEYEXCH_DUPCTX                      6
314 # define OSSL_FUNC_KEYEXCH_SET_PARAMS                  7
315
316 OSSL_CORE_MAKE_FUNC(void *, OP_keyexch_newctx, (void *provctx))
317 OSSL_CORE_MAKE_FUNC(int, OP_keyexch_init, (void *ctx, void *provkey))
318 OSSL_CORE_MAKE_FUNC(int, OP_keyexch_derive, (void *ctx,  unsigned char *secret,
319                                              size_t *secretlen, size_t outlen))
320 OSSL_CORE_MAKE_FUNC(int, OP_keyexch_set_peer, (void *ctx, void *provkey))
321 OSSL_CORE_MAKE_FUNC(void, OP_keyexch_freectx, (void *ctx))
322 OSSL_CORE_MAKE_FUNC(void *, OP_keyexch_dupctx, (void *ctx))
323 OSSL_CORE_MAKE_FUNC(int, OP_keyexch_set_params, (void *ctx,
324                                                  const OSSL_PARAM params[]))
325
326 /* Highest known operation number */
327 # define OSSL_OP__HIGHEST                            3
328
329 # ifdef __cplusplus
330 }
331 # endif
332
333 #endif