Constify DH-related code.
[openssl.git] / crypto / engine / engine.h
1 /* openssl/engine.h */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #ifndef HEADER_ENGINE_H
60 #define HEADER_ENGINE_H
61
62 #include <openssl/bn.h>
63 #include <openssl/rsa.h>
64 #include <openssl/dsa.h>
65 #include <openssl/dh.h>
66 #include <openssl/rand.h>
67 #include <openssl/evp.h>
68 #include <openssl/symhacks.h>
69
70 #ifdef  __cplusplus
71 extern "C" {
72 #endif
73
74 /* These flags are used to control combinations of algorithm (methods)
75  * by bitwise "OR"ing. */
76 #define ENGINE_METHOD_RSA               (unsigned int)0x0001
77 #define ENGINE_METHOD_DSA               (unsigned int)0x0002
78 #define ENGINE_METHOD_DH                (unsigned int)0x0004
79 #define ENGINE_METHOD_RAND              (unsigned int)0x0008
80 #define ENGINE_METHOD_BN_MOD_EXP        (unsigned int)0x0010
81 #define ENGINE_METHOD_BN_MOD_EXP_CRT    (unsigned int)0x0020
82 /* Obvious all-or-nothing cases. */
83 #define ENGINE_METHOD_ALL               (unsigned int)0xFFFF
84 #define ENGINE_METHOD_NONE              (unsigned int)0x0000
85
86 /* These flags are used to tell the ctrl function what should be done.
87  * All command numbers are shared between all engines, even if some don't
88  * make sense to some engines.  In such a case, they do nothing but return
89  * the error ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED. */
90 #define ENGINE_CTRL_SET_LOGSTREAM               1
91 #define ENGINE_CTRL_SET_PASSWORD_CALLBACK       2
92 /* Flags specific to the nCipher "chil" engine */
93 #define ENGINE_CTRL_CHIL_SET_FORKCHECK          100
94         /* Depending on the value of the (long)i argument, this sets or
95          * unsets the SimpleForkCheck flag in the CHIL API to enable or
96          * disable checking and workarounds for applications that fork().
97          */
98 #define ENGINE_CTRL_CHIL_NO_LOCKING             101
99         /* This prevents the initialisation function from providing mutex
100          * callbacks to the nCipher library. */
101
102 /* As we're missing a BIGNUM_METHOD, we need a couple of locally
103  * defined function types that engines can implement. */
104
105 /* mod_exp operation, calculates; r = a ^ p mod m
106  * NB: ctx can be NULL, but if supplied, the implementation may use
107  * it if it wishes. */
108 typedef int (*BN_MOD_EXP)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
109                 const BIGNUM *m, BN_CTX *ctx);
110
111 /* private key operation for RSA, provided seperately in case other
112  * RSA implementations wish to use it. */
113 typedef int (*BN_MOD_EXP_CRT)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
114                 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
115                 const BIGNUM *iqmp, BN_CTX *ctx);
116
117 /* Generic function pointer */
118 typedef int (*ENGINE_GEN_FUNC_PTR)();
119 /* Generic function pointer taking no arguments */
120 typedef int (*ENGINE_GEN_INT_FUNC_PTR)(void);
121 /* Specific control function pointer */
122 typedef int (*ENGINE_CTRL_FUNC_PTR)(int cmd, long i, void *p, void (*f)());
123
124 /* The list of "engine" types is a static array of (const ENGINE*)
125  * pointers (not dynamic because static is fine for now and we otherwise
126  * have to hook an appropriate load/unload function in to initialise and
127  * cleanup). */
128 struct engine_st;
129 typedef struct engine_st ENGINE;
130
131 /* STRUCTURE functions ... all of these functions deal with pointers to
132  * ENGINE structures where the pointers have a "structural reference".
133  * This means that their reference is to allow access to the structure
134  * but it does not imply that the structure is functional. To simply
135  * increment or decrement the structural reference count, use ENGINE_new
136  * and ENGINE_free. NB: This is not required when iterating using
137  * ENGINE_get_next as it will automatically decrement the structural
138  * reference count of the "current" ENGINE and increment the structural
139  * reference count of the ENGINE it returns (unless it is NULL). */
140
141 /* Get the first/last "ENGINE" type available. */
142 ENGINE *ENGINE_get_first(void);
143 ENGINE *ENGINE_get_last(void);
144 /* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
145 ENGINE *ENGINE_get_next(ENGINE *e);
146 ENGINE *ENGINE_get_prev(ENGINE *e);
147 /* Add another "ENGINE" type into the array. */
148 int ENGINE_add(ENGINE *e);
149 /* Remove an existing "ENGINE" type from the array. */
150 int ENGINE_remove(ENGINE *e);
151 /* Retrieve an engine from the list by its unique "id" value. */
152 ENGINE *ENGINE_by_id(const char *id);
153 /* Add all the built-in engines.  By default, only the OpenSSL software
154    engine is loaded */
155 void ENGINE_load_cswift(void);
156 void ENGINE_load_chil(void);
157 void ENGINE_load_atalla(void);
158 void ENGINE_load_nuron(void);
159 void ENGINE_load_builtin_engines(void);
160
161 /* These functions are useful for manufacturing new ENGINE
162  * structures. They don't address reference counting at all -
163  * one uses them to populate an ENGINE structure with personalised
164  * implementations of things prior to using it directly or adding
165  * it to the builtin ENGINE list in OpenSSL. These are also here
166  * so that the ENGINE structure doesn't have to be exposed and
167  * break binary compatibility!
168  *
169  * NB: I'm changing ENGINE_new to force the ENGINE structure to
170  * be allocated from within OpenSSL. See the comment for
171  * ENGINE_get_struct_size().
172  */
173 #if 0
174 ENGINE *ENGINE_new(ENGINE *e);
175 #else
176 ENGINE *ENGINE_new(void);
177 #endif
178 int ENGINE_free(ENGINE *e);
179 int ENGINE_set_id(ENGINE *e, const char *id);
180 int ENGINE_set_name(ENGINE *e, const char *name);
181 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
182 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
183 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
184 int ENGINE_set_RAND(ENGINE *e, RAND_METHOD *rand_meth);
185 int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp);
186 int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt);
187 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
188 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
189 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
190
191 /* These return values from within the ENGINE structure. These can
192  * be useful with functional references as well as structural
193  * references - it depends which you obtained. Using the result
194  * for functional purposes if you only obtained a structural
195  * reference may be problematic! */
196 const char *ENGINE_get_id(ENGINE *e);
197 const char *ENGINE_get_name(ENGINE *e);
198 const RSA_METHOD *ENGINE_get_RSA(ENGINE *e);
199 const DSA_METHOD *ENGINE_get_DSA(ENGINE *e);
200 const DH_METHOD *ENGINE_get_DH(ENGINE *e);
201 RAND_METHOD *ENGINE_get_RAND(ENGINE *e);
202 BN_MOD_EXP ENGINE_get_BN_mod_exp(ENGINE *e);
203 BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(ENGINE *e);
204 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(ENGINE *e);
205 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(ENGINE *e);
206 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(ENGINE *e);
207
208 /* ENGINE_new is normally passed a NULL in the first parameter because
209  * the calling code doesn't have access to the definition of the ENGINE
210  * structure (for good reason). However, if the caller wishes to use
211  * its own memory allocation or use a static array, the following call
212  * should be used to check the amount of memory the ENGINE structure
213  * will occupy. This will make the code more future-proof.
214  *
215  * NB: I'm "#if 0"-ing this out because it's better to force the use of
216  * internally allocated memory. See similar change in ENGINE_new().
217  */
218 #if 0
219 int ENGINE_get_struct_size(void);
220 #endif
221
222 /* FUNCTIONAL functions. These functions deal with ENGINE structures
223  * that have (or will) be initialised for use. Broadly speaking, the
224  * structural functions are useful for iterating the list of available
225  * engine types, creating new engine types, and other "list" operations.
226  * These functions actually deal with ENGINEs that are to be used. As
227  * such these functions can fail (if applicable) when particular
228  * engines are unavailable - eg. if a hardware accelerator is not
229  * attached or not functioning correctly. Each ENGINE has 2 reference
230  * counts; structural and functional. Every time a functional reference
231  * is obtained or released, a corresponding structural reference is
232  * automatically obtained or released too. */
233
234 /* Initialise a engine type for use (or up its reference count if it's
235  * already in use). This will fail if the engine is not currently
236  * operational and cannot initialise. */
237 int ENGINE_init(ENGINE *e);
238 /* Free a functional reference to a engine type. This does not require
239  * a corresponding call to ENGINE_free as it also releases a structural
240  * reference. */
241 int ENGINE_finish(ENGINE *e);
242 /* Send control parametrised commands to the engine.  The possibilities
243  * to send down an integer, a pointer to data or a function pointer are
244  * provided.  Any of the parameters may or may not be NULL, depending
245  * on the command number */
246 /* WARNING: This is currently experimental and may change radically! */
247 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
248
249 /* The following functions handle keys that are stored in some secondary
250  * location, handled by the engine.  The storage may be on a card or
251  * whatever. */
252 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
253         const char *passphrase);
254 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
255         const char *passphrase);
256
257 /* This returns a pointer for the current ENGINE structure that
258  * is (by default) performing any RSA operations. The value returned
259  * is an incremented reference, so it should be free'd (ENGINE_finish)
260  * before it is discarded. */
261 ENGINE *ENGINE_get_default_RSA(void);
262 /* Same for the other "methods" */
263 ENGINE *ENGINE_get_default_DSA(void);
264 ENGINE *ENGINE_get_default_DH(void);
265 ENGINE *ENGINE_get_default_RAND(void);
266 ENGINE *ENGINE_get_default_BN_mod_exp(void);
267 ENGINE *ENGINE_get_default_BN_mod_exp_crt(void);
268
269 /* This sets a new default ENGINE structure for performing RSA
270  * operations. If the result is non-zero (success) then the ENGINE
271  * structure will have had its reference count up'd so the caller
272  * should still free their own reference 'e'. */
273 int ENGINE_set_default_RSA(ENGINE *e);
274 /* Same for the other "methods" */
275 int ENGINE_set_default_DSA(ENGINE *e);
276 int ENGINE_set_default_DH(ENGINE *e);
277 int ENGINE_set_default_RAND(ENGINE *e);
278 int ENGINE_set_default_BN_mod_exp(ENGINE *e);
279 int ENGINE_set_default_BN_mod_exp_crt(ENGINE *e);
280
281 /* The combination "set" - the flags are bitwise "OR"d from the
282  * ENGINE_METHOD_*** defines above. */
283 int ENGINE_set_default(ENGINE *e, unsigned int flags);
284
285 /* Obligatory error function. */
286 void ERR_load_ENGINE_strings(void);
287
288 /*
289  * Error codes for all engine functions. NB: We use "generic"
290  * function names instead of per-implementation ones because this
291  * levels the playing field for externally implemented bootstrapped
292  * support code. As the filename and line number is included, it's
293  * more important to indicate the type of function, so that
294  * bootstrapped code (that can't easily add its own errors in) can
295  * use the same error codes too.
296  */
297
298 /* BEGIN ERROR CODES */
299 /* The following lines are auto generated by the script mkerr.pl. Any changes
300  * made after this point may be overwritten when the script is next run.
301  */
302
303 /* Error codes for the ENGINE functions. */
304
305 /* Function codes. */
306 #define ENGINE_F_ATALLA_FINISH                           159
307 #define ENGINE_F_ATALLA_INIT                             160
308 #define ENGINE_F_ATALLA_MOD_EXP                          161
309 #define ENGINE_F_ATALLA_RSA_MOD_EXP                      162
310 #define ENGINE_F_CSWIFT_DSA_SIGN                         133
311 #define ENGINE_F_CSWIFT_DSA_VERIFY                       134
312 #define ENGINE_F_CSWIFT_FINISH                           100
313 #define ENGINE_F_CSWIFT_INIT                             101
314 #define ENGINE_F_CSWIFT_MOD_EXP                          102
315 #define ENGINE_F_CSWIFT_MOD_EXP_CRT                      103
316 #define ENGINE_F_CSWIFT_RSA_MOD_EXP                      104
317 #define ENGINE_F_ENGINE_ADD                              105
318 #define ENGINE_F_ENGINE_BY_ID                            106
319 #define ENGINE_F_ENGINE_CTRL                             142
320 #define ENGINE_F_ENGINE_FINISH                           107
321 #define ENGINE_F_ENGINE_FREE                             108
322 #define ENGINE_F_ENGINE_GET_BN_MOD_EXP                   109
323 #define ENGINE_F_ENGINE_GET_BN_MOD_EXP_CRT               110
324 #define ENGINE_F_ENGINE_GET_CTRL_FUNCTION                144
325 #define ENGINE_F_ENGINE_GET_DH                           111
326 #define ENGINE_F_ENGINE_GET_DSA                          112
327 #define ENGINE_F_ENGINE_GET_FINISH_FUNCTION              145
328 #define ENGINE_F_ENGINE_GET_ID                           113
329 #define ENGINE_F_ENGINE_GET_INIT_FUNCTION                146
330 #define ENGINE_F_ENGINE_GET_NAME                         114
331 #define ENGINE_F_ENGINE_GET_NEXT                         115
332 #define ENGINE_F_ENGINE_GET_PREV                         116
333 #define ENGINE_F_ENGINE_GET_RAND                         117
334 #define ENGINE_F_ENGINE_GET_RSA                          118
335 #define ENGINE_F_ENGINE_INIT                             119
336 #define ENGINE_F_ENGINE_LIST_ADD                         120
337 #define ENGINE_F_ENGINE_LIST_REMOVE                      121
338 #define ENGINE_F_ENGINE_LOAD_PRIVATE_KEY                 150
339 #define ENGINE_F_ENGINE_LOAD_PUBLIC_KEY                  151
340 #define ENGINE_F_ENGINE_NEW                              122
341 #define ENGINE_F_ENGINE_REMOVE                           123
342 #define ENGINE_F_ENGINE_SET_BN_MOD_EXP                   124
343 #define ENGINE_F_ENGINE_SET_BN_MOD_EXP_CRT               125
344 #define ENGINE_F_ENGINE_SET_CTRL_FUNCTION                147
345 #define ENGINE_F_ENGINE_SET_DEFAULT_TYPE                 126
346 #define ENGINE_F_ENGINE_SET_DH                           127
347 #define ENGINE_F_ENGINE_SET_DSA                          128
348 #define ENGINE_F_ENGINE_SET_FINISH_FUNCTION              148
349 #define ENGINE_F_ENGINE_SET_ID                           129
350 #define ENGINE_F_ENGINE_SET_INIT_FUNCTION                149
351 #define ENGINE_F_ENGINE_SET_NAME                         130
352 #define ENGINE_F_ENGINE_SET_RAND                         131
353 #define ENGINE_F_ENGINE_SET_RSA                          132
354 #define ENGINE_F_ENGINE_UNLOAD_KEY                       152
355 #define ENGINE_F_HWCRHK_CTRL                             143
356 #define ENGINE_F_HWCRHK_FINISH                           135
357 #define ENGINE_F_HWCRHK_GET_PASS                         155
358 #define ENGINE_F_HWCRHK_INIT                             136
359 #define ENGINE_F_HWCRHK_LOAD_PRIVKEY                     153
360 #define ENGINE_F_HWCRHK_LOAD_PUBKEY                      154
361 #define ENGINE_F_HWCRHK_MOD_EXP                          137
362 #define ENGINE_F_HWCRHK_MOD_EXP_CRT                      138
363 #define ENGINE_F_HWCRHK_RAND_BYTES                       139
364 #define ENGINE_F_HWCRHK_RSA_MOD_EXP                      140
365 #define ENGINE_F_LOG_MESSAGE                             141
366 #define ENGINE_F_NURON_FINISH                            157
367 #define ENGINE_F_NURON_INIT                              156
368 #define ENGINE_F_NURON_MOD_EXP                           158
369
370 /* Reason codes. */
371 #define ENGINE_R_ALREADY_LOADED                          100
372 #define ENGINE_R_BIO_WAS_FREED                           121
373 #define ENGINE_R_BN_CTX_FULL                             101
374 #define ENGINE_R_BN_EXPAND_FAIL                          102
375 #define ENGINE_R_CHIL_ERROR                              123
376 #define ENGINE_R_CONFLICTING_ENGINE_ID                   103
377 #define ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED            119
378 #define ENGINE_R_DSO_FAILURE                             104
379 #define ENGINE_R_DSO_FUNCTION_NOT_FOUND                  131
380 #define ENGINE_R_DSO_NOT_FOUND                           132
381 #define ENGINE_R_ENGINE_IS_NOT_IN_LIST                   105
382 #define ENGINE_R_FAILED_LOADING_PRIVATE_KEY              128
383 #define ENGINE_R_FAILED_LOADING_PUBLIC_KEY               129
384 #define ENGINE_R_FINISH_FAILED                           106
385 #define ENGINE_R_GET_HANDLE_FAILED                       107
386 #define ENGINE_R_ID_OR_NAME_MISSING                      108
387 #define ENGINE_R_INIT_FAILED                             109
388 #define ENGINE_R_INTERNAL_LIST_ERROR                     110
389 #define ENGINE_R_MISSING_KEY_COMPONENTS                  111
390 #define ENGINE_R_NOT_INITIALISED                         117
391 #define ENGINE_R_NOT_LOADED                              112
392 #define ENGINE_R_NO_CALLBACK                             127
393 #define ENGINE_R_NO_CONTROL_FUNCTION                     120
394 #define ENGINE_R_NO_KEY                                  124
395 #define ENGINE_R_NO_LOAD_FUNCTION                        125
396 #define ENGINE_R_NO_REFERENCE                            130
397 #define ENGINE_R_NO_SUCH_ENGINE                          116
398 #define ENGINE_R_NO_UNLOAD_FUNCTION                      126
399 #define ENGINE_R_PROVIDE_PARAMETERS                      113
400 #define ENGINE_R_REQUEST_FAILED                          114
401 #define ENGINE_R_REQUEST_FALLBACK                        118
402 #define ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL             122
403 #define ENGINE_R_UNIT_FAILURE                            115
404
405 #ifdef  __cplusplus
406 }
407 #endif
408 #endif
409