c475bb78ddec03d92306eb5e5266544616ed619f
[openssl.git] / engines / e_sureware.c
1 /*-
2 *  Written by Corinne Dive-Reclus(cdive@baltimore.com)
3
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer. 
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in
14 *    the documentation and/or other materials provided with the
15 *    distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 *    software must display the following acknowledgment:
19 *    "This product includes software developed by the OpenSSL Project
20 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 *    endorse or promote products derived from this software without
24 *    prior written permission. For written permission, please contact
25 *    licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 *    nor may "OpenSSL" appear in their names without prior written
29 *    permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 *    acknowledgment:
33 *    "This product includes software developed by the OpenSSL Project
34 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * Written by Corinne Dive-Reclus(cdive@baltimore.com)
37 *
38 * Copyright@2001 Baltimore Technologies Ltd.
39 * All right Reserved.
40 *                                                                                                                                                                                               *       
41 *               THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND                                                                                                                                                   *
42 *               ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE                                   * 
43 *               IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE                              *
44 *               ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE                                             *
45 *               FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL                              *
46 *               DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS                                 *
47 *               OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                                   *
48 *               HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT                              *
49 *               LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY                               *
50 *               OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF                                  *
51 *               SUCH DAMAGE.                                                                                                                                                    *
52 ====================================================================*/
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <openssl/crypto.h>
57 #include <openssl/pem.h>
58 #include <openssl/dso.h>
59 #include <openssl/engine.h>
60 #include <openssl/rand.h>
61 #ifndef OPENSSL_NO_RSA
62 #include <openssl/rsa.h>
63 #endif
64 #ifndef OPENSSL_NO_DSA
65 #include <openssl/dsa.h>
66 #endif
67 #ifndef OPENSSL_NO_DH
68 #include <openssl/dh.h>
69 #endif
70 #include <openssl/bn.h>
71
72 #ifndef OPENSSL_NO_HW
73 #ifndef OPENSSL_NO_HW_SUREWARE
74
75 #ifdef FLAT_INC
76 #include "sureware.h"
77 #else
78 #include "vendor_defns/sureware.h"
79 #endif
80
81 #define SUREWARE_LIB_NAME "sureware engine"
82 #include "e_sureware_err.c"
83
84 static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
85 static int surewarehk_destroy(ENGINE *e);
86 static int surewarehk_init(ENGINE *e);
87 static int surewarehk_finish(ENGINE *e);
88 static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
89         const BIGNUM *m, BN_CTX *ctx);
90
91 /* RSA stuff */
92 #ifndef OPENSSL_NO_RSA
93 static int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to,
94                         RSA *rsa,int padding);
95 static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to,
96                             RSA *rsa,int padding);
97 #endif
98
99 /* RAND stuff */
100 static int surewarehk_rand_bytes(unsigned char *buf, int num);
101 static int surewarehk_rand_seed(const void *buf, int num);
102 static int surewarehk_rand_add(const void *buf, int num, double entropy);
103
104 /* KM stuff */
105 static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
106         UI_METHOD *ui_method, void *callback_data);
107 static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
108         UI_METHOD *ui_method, void *callback_data);
109 static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
110         int idx,long argl, void *argp);
111 #if 0
112 static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
113         int idx,long argl, void *argp);
114 #endif
115
116 #ifndef OPENSSL_NO_RSA
117 /* This function is aliased to mod_exp (with the mont stuff dropped). */
118 static int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
119                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
120 {
121         return surewarehk_modexp(r, a, p, m, ctx);
122 }
123
124 /* Our internal RSA_METHOD that we provide pointers to */
125 static RSA_METHOD surewarehk_rsa =
126         {
127         "SureWare RSA method",
128         NULL, /* pub_enc*/
129         NULL, /* pub_dec*/
130         surewarehk_rsa_sign, /* our rsa_sign is OpenSSL priv_enc*/
131         surewarehk_rsa_priv_dec, /* priv_dec*/
132         NULL, /*mod_exp*/
133         surewarehk_mod_exp_mont, /*mod_exp_mongomery*/
134         NULL, /* init*/
135         NULL, /* finish*/
136         0,      /* RSA flag*/
137         NULL, 
138         NULL, /* OpenSSL sign*/
139         NULL, /* OpenSSL verify*/
140         NULL  /* keygen */
141         };
142 #endif
143
144 #ifndef OPENSSL_NO_DH
145 /* Our internal DH_METHOD that we provide pointers to */
146 /* This function is aliased to mod_exp (with the dh and mont dropped). */
147 static int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
148         const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
149 {
150         return surewarehk_modexp(r, a, p, m, ctx);
151 }
152
153 static DH_METHOD surewarehk_dh =
154         {
155         "SureWare DH method",
156         NULL,/*gen_key*/
157         NULL,/*agree,*/
158         surewarehk_modexp_dh, /*dh mod exp*/
159         NULL, /* init*/
160         NULL, /* finish*/
161         0,    /* flags*/
162         NULL,
163         NULL
164         };
165 #endif
166
167 static RAND_METHOD surewarehk_rand =
168         {
169         /* "SureWare RAND method", */
170         surewarehk_rand_seed,
171         surewarehk_rand_bytes,
172         NULL,/*cleanup*/
173         surewarehk_rand_add,
174         surewarehk_rand_bytes,
175         NULL,/*rand_status*/
176         };
177
178 #ifndef OPENSSL_NO_DSA
179 /* DSA stuff */
180 static  DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
181 static int surewarehk_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
182                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
183                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
184 {
185         BIGNUM t;
186         int to_return = 0;
187         BN_init(&t);
188         /* let rr = a1 ^ p1 mod m */
189         if (!surewarehk_modexp(rr,a1,p1,m,ctx)) goto end;
190         /* let t = a2 ^ p2 mod m */
191         if (!surewarehk_modexp(&t,a2,p2,m,ctx)) goto end;
192         /* let rr = rr * t mod m */
193         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
194         to_return = 1;
195 end:
196         BN_free(&t);
197         return to_return;
198 }
199
200 static DSA_METHOD surewarehk_dsa =
201         {
202          "SureWare DSA method", 
203         surewarehk_dsa_do_sign,
204         NULL,/*sign setup*/
205         NULL,/*verify,*/
206         surewarehk_dsa_mod_exp,/*mod exp*/
207         NULL,/*bn mod exp*/
208         NULL, /*init*/
209         NULL,/*finish*/
210         0,
211         NULL,
212         NULL,
213         NULL
214         };
215 #endif
216
217 static const char *engine_sureware_id = "sureware";
218 static const char *engine_sureware_name = "SureWare hardware engine support";
219
220 /* Now, to our own code */
221
222 /* As this is only ever called once, there's no need for locking
223  * (indeed - the lock will already be held by our caller!!!) */
224 static int bind_sureware(ENGINE *e)
225 {
226 #ifndef OPENSSL_NO_RSA
227         const RSA_METHOD *meth1;
228 #endif
229 #ifndef OPENSSL_NO_DSA
230         const DSA_METHOD *meth2;
231 #endif
232 #ifndef OPENSSL_NO_DH
233         const DH_METHOD *meth3;
234 #endif
235
236         if(!ENGINE_set_id(e, engine_sureware_id) ||
237            !ENGINE_set_name(e, engine_sureware_name) ||
238 #ifndef OPENSSL_NO_RSA
239            !ENGINE_set_RSA(e, &surewarehk_rsa) ||
240 #endif
241 #ifndef OPENSSL_NO_DSA
242            !ENGINE_set_DSA(e, &surewarehk_dsa) ||
243 #endif
244 #ifndef OPENSSL_NO_DH
245            !ENGINE_set_DH(e, &surewarehk_dh) ||
246 #endif
247            !ENGINE_set_RAND(e, &surewarehk_rand) ||
248            !ENGINE_set_destroy_function(e, surewarehk_destroy) ||
249            !ENGINE_set_init_function(e, surewarehk_init) ||
250            !ENGINE_set_finish_function(e, surewarehk_finish) ||
251            !ENGINE_set_ctrl_function(e, surewarehk_ctrl) ||
252            !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) ||
253            !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey))
254           return 0;
255
256 #ifndef OPENSSL_NO_RSA
257         /* We know that the "PKCS1_SSLeay()" functions hook properly
258          * to the cswift-specific mod_exp and mod_exp_crt so we use
259          * those functions. NB: We don't use ENGINE_openssl() or
260          * anything "more generic" because something like the RSAref
261          * code may not hook properly, and if you own one of these
262          * cards then you have the right to do RSA operations on it
263          * anyway! */ 
264         meth1 = RSA_PKCS1_SSLeay();
265         if (meth1)
266         {
267                 surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
268                 surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
269         }
270 #endif
271
272 #ifndef OPENSSL_NO_DSA
273         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
274          * bits. */
275         meth2 = DSA_OpenSSL();
276         if (meth2)
277         {
278                 surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify;
279         }
280 #endif
281
282 #ifndef OPENSSL_NO_DH
283         /* Much the same for Diffie-Hellman */
284         meth3 = DH_OpenSSL();
285         if (meth3)
286         {
287                 surewarehk_dh.generate_key = meth3->generate_key;
288                 surewarehk_dh.compute_key = meth3->compute_key;
289         }
290 #endif
291
292         /* Ensure the sureware error handling is set up */
293         ERR_load_SUREWARE_strings();
294         return 1;
295 }
296
297 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
298 static int bind_helper(ENGINE *e, const char *id)
299         {
300         if(id && (strcmp(id, engine_sureware_id) != 0))
301                 return 0;
302         if(!bind_sureware(e))
303                 return 0;
304         return 1;
305         }       
306 IMPLEMENT_DYNAMIC_CHECK_FN()
307 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
308 #else
309 static ENGINE *engine_sureware(void)
310         {
311         ENGINE *ret = ENGINE_new();
312         if(!ret)
313                 return NULL;
314         if(!bind_sureware(ret))
315                 {
316                 ENGINE_free(ret);
317                 return NULL;
318                 }
319         return ret;
320         }
321
322 void ENGINE_load_sureware(void)
323         {
324         /* Copied from eng_[openssl|dyn].c */
325         ENGINE *toadd = engine_sureware();
326         if(!toadd) return;
327         ENGINE_add(toadd);
328         ENGINE_free(toadd);
329         ERR_clear_error();
330         }
331 #endif
332
333 /* This is a process-global DSO handle used for loading and unloading
334  * the SureWareHook library. NB: This is only set (or unset) during an
335  * init() or finish() call (reference counts permitting) and they're
336  * operating with global locks, so this should be thread-safe
337  * implicitly. */
338 static DSO *surewarehk_dso = NULL;
339 #ifndef OPENSSL_NO_RSA
340 static int rsaHndidx = -1;      /* Index for KM handle.  Not really used yet. */
341 #endif
342 #ifndef OPENSSL_NO_DSA
343 static int dsaHndidx = -1;      /* Index for KM handle.  Not really used yet. */
344 #endif
345
346 /* These are the function pointers that are (un)set when the library has
347  * successfully (un)loaded. */
348 static SureWareHook_Init_t *p_surewarehk_Init = NULL;
349 static SureWareHook_Finish_t *p_surewarehk_Finish = NULL;
350 static SureWareHook_Rand_Bytes_t *p_surewarehk_Rand_Bytes = NULL;
351 static SureWareHook_Rand_Seed_t *p_surewarehk_Rand_Seed = NULL;
352 static SureWareHook_Load_Privkey_t *p_surewarehk_Load_Privkey = NULL;
353 static SureWareHook_Info_Pubkey_t *p_surewarehk_Info_Pubkey = NULL;
354 static SureWareHook_Load_Rsa_Pubkey_t *p_surewarehk_Load_Rsa_Pubkey = NULL;
355 static SureWareHook_Load_Dsa_Pubkey_t *p_surewarehk_Load_Dsa_Pubkey = NULL;
356 static SureWareHook_Free_t *p_surewarehk_Free=NULL;
357 static SureWareHook_Rsa_Priv_Dec_t *p_surewarehk_Rsa_Priv_Dec=NULL;
358 static SureWareHook_Rsa_Sign_t *p_surewarehk_Rsa_Sign=NULL;
359 static SureWareHook_Dsa_Sign_t *p_surewarehk_Dsa_Sign=NULL;
360 static SureWareHook_Mod_Exp_t *p_surewarehk_Mod_Exp=NULL;
361
362 /* Used in the DSO operations. */
363 static const char *surewarehk_LIBNAME = "SureWareHook";
364 static const char *n_surewarehk_Init = "SureWareHook_Init";
365 static const char *n_surewarehk_Finish = "SureWareHook_Finish";
366 static const char *n_surewarehk_Rand_Bytes="SureWareHook_Rand_Bytes";
367 static const char *n_surewarehk_Rand_Seed="SureWareHook_Rand_Seed";
368 static const char *n_surewarehk_Load_Privkey="SureWareHook_Load_Privkey";
369 static const char *n_surewarehk_Info_Pubkey="SureWareHook_Info_Pubkey";
370 static const char *n_surewarehk_Load_Rsa_Pubkey="SureWareHook_Load_Rsa_Pubkey";
371 static const char *n_surewarehk_Load_Dsa_Pubkey="SureWareHook_Load_Dsa_Pubkey";
372 static const char *n_surewarehk_Free="SureWareHook_Free";
373 static const char *n_surewarehk_Rsa_Priv_Dec="SureWareHook_Rsa_Priv_Dec";
374 static const char *n_surewarehk_Rsa_Sign="SureWareHook_Rsa_Sign";
375 static const char *n_surewarehk_Dsa_Sign="SureWareHook_Dsa_Sign";
376 static const char *n_surewarehk_Mod_Exp="SureWareHook_Mod_Exp";
377 static BIO *logstream = NULL;
378
379 /* SureWareHook library functions and mechanics - these are used by the
380  * higher-level functions further down. NB: As and where there's no
381  * error checking, take a look lower down where these functions are
382  * called, the checking and error handling is probably down there. 
383 */
384 static int threadsafe=1;
385 static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
386 {
387         int to_return = 1;
388
389         switch(cmd)
390         {
391                 case ENGINE_CTRL_SET_LOGSTREAM:
392                 {
393                         BIO *bio = (BIO *)p;
394                         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
395                         if (logstream)
396                         {
397                                 BIO_free(logstream);
398                                 logstream = NULL;
399                         }
400                         if (CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO) > 1)
401                                 logstream = bio;
402                         else
403                                 SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,SUREWARE_R_BIO_WAS_FREED);
404                 }
405                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
406                 break;
407         /* This will prevent the initialisation function from "installing"
408          * the mutex-handling callbacks, even if they are available from
409          * within the library (or were provided to the library from the
410          * calling application). This is to remove any baggage for
411          * applications not using multithreading. */
412         case ENGINE_CTRL_CHIL_NO_LOCKING:
413                 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
414                 threadsafe = 0;
415                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
416                 break;
417
418         /* The command isn't understood by this engine */
419         default:
420                 SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
421                         ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
422                 to_return = 0;
423                 break;
424                 }
425
426         return to_return;
427 }
428
429 /* Destructor (complements the "ENGINE_surewarehk()" constructor) */
430 static int surewarehk_destroy(ENGINE *e)
431 {
432         ERR_unload_SUREWARE_strings();
433         return 1;
434 }
435
436 /* (de)initialisation functions. */
437 static int surewarehk_init(ENGINE *e)
438 {
439         char msg[64]="ENGINE_init";
440         SureWareHook_Init_t *p1=NULL;
441         SureWareHook_Finish_t *p2=NULL;
442         SureWareHook_Rand_Bytes_t *p3=NULL;
443         SureWareHook_Rand_Seed_t *p4=NULL;
444         SureWareHook_Load_Privkey_t *p5=NULL;
445         SureWareHook_Load_Rsa_Pubkey_t *p6=NULL;
446         SureWareHook_Free_t *p7=NULL;
447         SureWareHook_Rsa_Priv_Dec_t *p8=NULL;
448         SureWareHook_Rsa_Sign_t *p9=NULL;
449         SureWareHook_Dsa_Sign_t *p12=NULL;
450         SureWareHook_Info_Pubkey_t *p13=NULL;
451         SureWareHook_Load_Dsa_Pubkey_t *p14=NULL;
452         SureWareHook_Mod_Exp_t *p15=NULL;
453
454         if(surewarehk_dso != NULL)
455         {
456                 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_ALREADY_LOADED);
457                 goto err;
458         }
459         /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */
460         surewarehk_dso = DSO_load(NULL, surewarehk_LIBNAME, NULL, 0);
461         if(surewarehk_dso == NULL)
462         {
463                 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE);
464                 goto err;
465         }
466         if(!(p1=(SureWareHook_Init_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Init)) ||
467            !(p2=(SureWareHook_Finish_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Finish)) ||
468            !(p3=(SureWareHook_Rand_Bytes_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Bytes)) ||
469            !(p4=(SureWareHook_Rand_Seed_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Seed)) ||
470            !(p5=(SureWareHook_Load_Privkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Privkey)) ||
471            !(p6=(SureWareHook_Load_Rsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Rsa_Pubkey)) ||
472            !(p7=(SureWareHook_Free_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Free)) ||
473            !(p8=(SureWareHook_Rsa_Priv_Dec_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Priv_Dec)) ||
474            !(p9=(SureWareHook_Rsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Sign)) ||
475            !(p12=(SureWareHook_Dsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Dsa_Sign)) ||
476            !(p13=(SureWareHook_Info_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Info_Pubkey)) ||
477            !(p14=(SureWareHook_Load_Dsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Dsa_Pubkey)) ||
478            !(p15=(SureWareHook_Mod_Exp_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Mod_Exp)))
479         {
480                 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE);
481                 goto err;
482         }
483         /* Copy the pointers */
484         p_surewarehk_Init = p1;
485         p_surewarehk_Finish = p2;
486         p_surewarehk_Rand_Bytes = p3;
487         p_surewarehk_Rand_Seed = p4;
488         p_surewarehk_Load_Privkey = p5;
489         p_surewarehk_Load_Rsa_Pubkey = p6;
490         p_surewarehk_Free = p7;
491         p_surewarehk_Rsa_Priv_Dec = p8;
492         p_surewarehk_Rsa_Sign = p9;
493         p_surewarehk_Dsa_Sign = p12;
494         p_surewarehk_Info_Pubkey = p13;
495         p_surewarehk_Load_Dsa_Pubkey = p14;
496         p_surewarehk_Mod_Exp = p15;
497         /* Contact the hardware and initialises it. */
498         if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE)
499         {
500                 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE);
501                 goto err;
502         }
503         if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE)
504         {
505                 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE);
506                 goto err;
507         }
508         /* try to load the default private key, if failed does not return a failure but
509            wait for an explicit ENGINE_load_privakey */
510         surewarehk_load_privkey(e,NULL,NULL,NULL);
511
512         /* Everything's fine. */
513 #ifndef OPENSSL_NO_RSA
514         if (rsaHndidx == -1)
515                 rsaHndidx = RSA_get_ex_new_index(0,
516                                                 "SureWareHook RSA key handle",
517                                                 NULL, NULL, surewarehk_ex_free);
518 #endif
519 #ifndef OPENSSL_NO_DSA
520         if (dsaHndidx == -1)
521                 dsaHndidx = DSA_get_ex_new_index(0,
522                                                 "SureWareHook DSA key handle",
523                                                 NULL, NULL, surewarehk_ex_free);
524 #endif
525
526         return 1;
527 err:
528         if(surewarehk_dso)
529                 DSO_free(surewarehk_dso);
530         surewarehk_dso = NULL;
531         p_surewarehk_Init = NULL;
532         p_surewarehk_Finish = NULL;
533         p_surewarehk_Rand_Bytes = NULL;
534         p_surewarehk_Rand_Seed = NULL;
535         p_surewarehk_Load_Privkey = NULL;
536         p_surewarehk_Load_Rsa_Pubkey = NULL;
537         p_surewarehk_Free = NULL;
538         p_surewarehk_Rsa_Priv_Dec = NULL;
539         p_surewarehk_Rsa_Sign = NULL;
540         p_surewarehk_Dsa_Sign = NULL;
541         p_surewarehk_Info_Pubkey = NULL;
542         p_surewarehk_Load_Dsa_Pubkey = NULL;
543         p_surewarehk_Mod_Exp = NULL;
544         return 0;
545 }
546
547 static int surewarehk_finish(ENGINE *e)
548 {
549         int to_return = 1;
550         if(surewarehk_dso == NULL)
551                 {
552                 SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH,ENGINE_R_NOT_LOADED);
553                 to_return = 0;
554                 goto err;
555                 }
556         p_surewarehk_Finish();
557         if(!DSO_free(surewarehk_dso))
558                 {
559                 SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH,ENGINE_R_DSO_FAILURE);
560                 to_return = 0;
561                 goto err;
562                 }
563  err:
564         if (logstream)
565                 BIO_free(logstream);
566         surewarehk_dso = NULL;
567         p_surewarehk_Init = NULL;
568         p_surewarehk_Finish = NULL;
569         p_surewarehk_Rand_Bytes = NULL;
570         p_surewarehk_Rand_Seed = NULL;
571         p_surewarehk_Load_Privkey = NULL;
572         p_surewarehk_Load_Rsa_Pubkey = NULL;
573         p_surewarehk_Free = NULL;
574         p_surewarehk_Rsa_Priv_Dec = NULL;
575         p_surewarehk_Rsa_Sign = NULL;
576         p_surewarehk_Dsa_Sign = NULL;
577         p_surewarehk_Info_Pubkey = NULL;
578         p_surewarehk_Load_Dsa_Pubkey = NULL;
579         p_surewarehk_Mod_Exp = NULL;
580         return to_return;
581 }
582
583 static void surewarehk_error_handling(char *const msg,int func,int ret)
584 {
585         switch (ret)
586         {
587                 case SUREWAREHOOK_ERROR_UNIT_FAILURE:
588                         ENGINEerr(func,SUREWARE_R_UNIT_FAILURE);
589                         break;
590                 case SUREWAREHOOK_ERROR_FALLBACK:
591                         ENGINEerr(func,SUREWARE_R_REQUEST_FALLBACK);
592                         break;
593                 case SUREWAREHOOK_ERROR_DATA_SIZE:
594                         ENGINEerr(func,SUREWARE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
595                         break;
596                 case SUREWAREHOOK_ERROR_INVALID_PAD:
597                         ENGINEerr(func,SUREWARE_R_PADDING_CHECK_FAILED);
598                         break;
599                 default:
600                         ENGINEerr(func,SUREWARE_R_REQUEST_FAILED);
601                         break;
602                 case 1:/*nothing*/
603                         msg[0]='\0';
604         }
605         if (*msg)
606         {
607                 ERR_add_error_data(1,msg);
608                 if (logstream)
609                 {
610                         CRYPTO_w_lock(CRYPTO_LOCK_BIO);
611                         BIO_write(logstream, msg, strlen(msg));
612                         CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
613                 }
614         }
615 }
616
617 static int surewarehk_rand_bytes(unsigned char *buf, int num)
618 {
619         int ret=0;
620         char msg[64]="ENGINE_rand_bytes";
621         if(!p_surewarehk_Rand_Bytes)
622         {
623                 SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_BYTES,ENGINE_R_NOT_INITIALISED);
624         }
625         else
626         {
627                 ret = p_surewarehk_Rand_Bytes(msg,buf, num);
628                 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RAND_BYTES,ret);
629         }
630         return ret==1 ? 1 : 0;
631 }
632
633 static int surewarehk_rand_seed(const void *buf, int num)
634 {
635         int ret=0;
636         char msg[64]="ENGINE_rand_seed";
637         if(!p_surewarehk_Rand_Seed)
638         {
639                 SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_SEED,ENGINE_R_NOT_INITIALISED);
640                 return 0;
641         }
642         else
643         {
644                 ret = p_surewarehk_Rand_Seed(msg,buf, num);
645                 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RAND_SEED,ret);
646                 if (ret == 1)
647                         return 1;
648                 else
649                         return 0;
650         }
651 }
652
653 static int surewarehk_rand_add(const void *buf, int num, double entropy)
654 {
655         return surewarehk_rand_seed(buf,num);
656 }
657
658 static EVP_PKEY* sureware_load_public(ENGINE *e,const char *key_id,char *hptr,unsigned long el,char keytype)
659 {
660         EVP_PKEY *res = NULL;
661 #ifndef OPENSSL_NO_RSA
662         RSA *rsatmp = NULL;
663 #endif
664 #ifndef OPENSSL_NO_DSA
665         DSA *dsatmp=NULL;
666 #endif
667         char msg[64]="sureware_load_public";
668         int ret=0;
669         if(!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey)
670         {
671                 SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_NOT_INITIALISED);
672                 goto err;
673         }
674         switch (keytype)
675         {
676 #ifndef OPENSSL_NO_RSA
677         case 1: /*RSA*/
678                 /* set private external reference */
679                 rsatmp = RSA_new_method(e);
680                 RSA_set_ex_data(rsatmp,rsaHndidx,hptr);
681                 rsatmp->flags |= RSA_FLAG_EXT_PKEY;
682
683                 /* set public big nums*/
684                 rsatmp->e = BN_new();
685                 rsatmp->n = BN_new();
686                 bn_expand2(rsatmp->e, el/sizeof(BN_ULONG));
687                 bn_expand2(rsatmp->n, el/sizeof(BN_ULONG));
688                 if (!rsatmp->e || rsatmp->e->dmax!=(int)(el/sizeof(BN_ULONG))|| 
689                         !rsatmp->n || rsatmp->n->dmax!=(int)(el/sizeof(BN_ULONG)))
690                         goto err;
691                 ret=p_surewarehk_Load_Rsa_Pubkey(msg,key_id,el,
692                                                  (unsigned long *)rsatmp->n->d,
693                                                  (unsigned long *)rsatmp->e->d);
694                 surewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);
695                 if (ret!=1)
696                 {
697                         SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
698                         goto err;
699                 }
700                 /* normalise pub e and pub n */
701                 rsatmp->e->top=el/sizeof(BN_ULONG);
702                 bn_fix_top(rsatmp->e);
703                 rsatmp->n->top=el/sizeof(BN_ULONG);
704                 bn_fix_top(rsatmp->n);
705                 /* create an EVP object: engine + rsa key */
706                 res = EVP_PKEY_new();
707                 EVP_PKEY_assign_RSA(res, rsatmp);
708                 break;
709 #endif
710
711 #ifndef OPENSSL_NO_DSA
712         case 2:/*DSA*/
713                 /* set private/public external reference */
714                 dsatmp = DSA_new_method(e);
715                 DSA_set_ex_data(dsatmp,dsaHndidx,hptr);
716                 /*dsatmp->flags |= DSA_FLAG_EXT_PKEY;*/
717
718                 /* set public key*/
719                 dsatmp->pub_key = BN_new();
720                 dsatmp->p = BN_new();
721                 dsatmp->q = BN_new();
722                 dsatmp->g = BN_new();
723                 bn_expand2(dsatmp->pub_key, el/sizeof(BN_ULONG));
724                 bn_expand2(dsatmp->p, el/sizeof(BN_ULONG));
725                 bn_expand2(dsatmp->q, 20/sizeof(BN_ULONG));
726                 bn_expand2(dsatmp->g, el/sizeof(BN_ULONG));
727                 if (!dsatmp->pub_key || dsatmp->pub_key->dmax!=(int)(el/sizeof(BN_ULONG))|| 
728                         !dsatmp->p || dsatmp->p->dmax!=(int)(el/sizeof(BN_ULONG)) ||
729                         !dsatmp->q || dsatmp->q->dmax!=20/sizeof(BN_ULONG) ||
730                         !dsatmp->g || dsatmp->g->dmax!=(int)(el/sizeof(BN_ULONG)))
731                         goto err;
732
733                 ret=p_surewarehk_Load_Dsa_Pubkey(msg,key_id,el,
734                                                  (unsigned long *)dsatmp->pub_key->d, 
735                                                  (unsigned long *)dsatmp->p->d,
736                                                  (unsigned long *)dsatmp->q->d,
737                                                  (unsigned long *)dsatmp->g->d);
738                 surewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);
739                 if (ret!=1)
740                 {
741                         SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
742                         goto err;
743                 }
744                 /* set parameters */
745                 /* normalise pubkey and parameters in case of */
746                 dsatmp->pub_key->top=el/sizeof(BN_ULONG);
747                 bn_fix_top(dsatmp->pub_key);
748                 dsatmp->p->top=el/sizeof(BN_ULONG);
749                 bn_fix_top(dsatmp->p);
750                 dsatmp->q->top=20/sizeof(BN_ULONG);
751                 bn_fix_top(dsatmp->q);
752                 dsatmp->g->top=el/sizeof(BN_ULONG);
753                 bn_fix_top(dsatmp->g);
754
755                 /* create an EVP object: engine + rsa key */
756                 res = EVP_PKEY_new();
757                 EVP_PKEY_assign_DSA(res, dsatmp);
758                 break;
759 #endif
760
761         default:
762                 SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
763                 goto err;
764         }
765         return res;
766  err:
767 #ifndef OPENSSL_NO_RSA
768         if (rsatmp)
769                 RSA_free(rsatmp);
770 #endif
771 #ifndef OPENSSL_NO_DSA
772         if (dsatmp)
773                 DSA_free(dsatmp);
774 #endif
775         return NULL;
776 }
777
778 static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
779                                          UI_METHOD *ui_method, void *callback_data)
780 {
781         EVP_PKEY *res = NULL;
782         int ret=0;
783         unsigned long el=0;
784         char *hptr=NULL;
785         char keytype=0;
786         char msg[64]="ENGINE_load_privkey";
787
788         if(!p_surewarehk_Load_Privkey)
789         {
790                 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,ENGINE_R_NOT_INITIALISED);
791         }
792         else
793         {
794                 ret=p_surewarehk_Load_Privkey(msg,key_id,&hptr,&el,&keytype);
795                 if (ret!=1)
796                 {
797                         SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
798                         ERR_add_error_data(1,msg);              
799                 }
800                 else
801                         res=sureware_load_public(e,key_id,hptr,el,keytype);
802         }
803         return res;
804 }
805
806 static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
807                                          UI_METHOD *ui_method, void *callback_data)
808 {
809         EVP_PKEY *res = NULL;
810         int ret=0;
811         unsigned long el=0;
812         char *hptr=NULL;
813         char keytype=0;
814         char msg[64]="ENGINE_load_pubkey";
815
816         if(!p_surewarehk_Info_Pubkey)
817         {
818                 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,ENGINE_R_NOT_INITIALISED);
819         }
820         else
821         {
822                 /* call once to identify if DSA or RSA */
823                 ret=p_surewarehk_Info_Pubkey(msg,key_id,&el,&keytype);
824                 if (ret!=1)
825                 {
826                         SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
827                         ERR_add_error_data(1,msg);
828                 }
829                 else
830                         res=sureware_load_public(e,key_id,hptr,el,keytype);
831         }
832         return res;
833 }
834
835 /* This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
836 , called when ex_data is freed */
837 static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
838         int idx,long argl, void *argp)
839 {
840         if(!p_surewarehk_Free)
841         {
842                 SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE,ENGINE_R_NOT_INITIALISED);
843         }
844         else
845                 p_surewarehk_Free((char *)item,0);
846 }
847
848 #if 0
849 /* not currently used (bug?) */
850 /* This cleans up an DH KM key (destroys the key into hardware), 
851 called when ex_data is freed */
852 static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
853         int idx,long argl, void *argp)
854 {
855         if(!p_surewarehk_Free)
856         {
857                 SUREWAREerr(SUREWARE_F_SUREWAREHK_DH_EX_FREE,ENGINE_R_NOT_INITIALISED);
858         }
859         else
860                 p_surewarehk_Free((char *)item,1);
861 }
862 #endif
863
864 /*
865 * return number of decrypted bytes
866 */
867 #ifndef OPENSSL_NO_RSA
868 static int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to,
869                         RSA *rsa,int padding)
870 {
871         int ret=0,tlen;
872         char *buf=NULL,*hptr=NULL;
873         char msg[64]="ENGINE_rsa_priv_dec";
874         if (!p_surewarehk_Rsa_Priv_Dec)
875         {
876                 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ENGINE_R_NOT_INITIALISED);
877         }
878         /* extract ref to private key */
879         else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx)))
880         {
881                 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_MISSING_KEY_COMPONENTS);
882                 goto err;
883         }
884         /* analyse what padding we can do into the hardware */
885         if (padding==RSA_PKCS1_PADDING)
886         {
887                 /* do it one shot */
888                 ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD);
889                 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ret);
890                 if (ret!=1)
891                         goto err;
892                 ret=tlen;
893         }
894         else /* do with no padding into hardware */
895         {
896                 ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_NO_PAD);
897                 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ret);
898                 if (ret!=1)
899                         goto err;
900                 /* intermediate buffer for padding */
901                 if ((buf=OPENSSL_malloc(tlen)) == NULL)
902                 {
903                         SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ERR_R_MALLOC_FAILURE);
904                         goto err;
905                 }
906                 memcpy(buf,to,tlen);/* transfert to into buf */
907                 switch (padding) /* check padding in software */
908                 {
909 #ifndef OPENSSL_NO_SHA
910                 case RSA_PKCS1_OAEP_PADDING:
911                         ret=RSA_padding_check_PKCS1_OAEP(to,tlen,(unsigned char *)buf,tlen,tlen,NULL,0);
912                         break;
913 #endif
914                 case RSA_SSLV23_PADDING:
915                         ret=RSA_padding_check_SSLv23(to,tlen,(unsigned char *)buf,flen,tlen);
916                         break;
917                 case RSA_NO_PADDING:
918                         ret=RSA_padding_check_none(to,tlen,(unsigned char *)buf,flen,tlen);
919                         break;
920                 default:
921                         SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_UNKNOWN_PADDING_TYPE);
922                         goto err;
923                 }
924                 if (ret < 0)
925                         SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_PADDING_CHECK_FAILED);
926         }
927 err:
928         if (buf)
929         {
930                 OPENSSL_cleanse(buf,tlen);
931                 OPENSSL_free(buf);
932         }
933         return ret;
934 }
935
936 /*
937 * Does what OpenSSL rsa_priv_enc does.
938 */
939 static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to,
940                             RSA *rsa,int padding)
941 {
942         int ret=0,tlen;
943         char *hptr=NULL;
944         char msg[64]="ENGINE_rsa_sign";
945         if (!p_surewarehk_Rsa_Sign)
946         {
947                 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,ENGINE_R_NOT_INITIALISED);
948         }
949         /* extract ref to private key */
950         else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx)))
951         {
952                 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,SUREWARE_R_MISSING_KEY_COMPONENTS);
953         }
954         else
955         {
956                 switch (padding)
957                 {
958                 case RSA_PKCS1_PADDING: /* do it in one shot */
959                         ret=p_surewarehk_Rsa_Sign(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD);
960                         surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_SIGN,ret);
961                         break;
962                 case RSA_NO_PADDING:
963                 default:
964                         SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,SUREWARE_R_UNKNOWN_PADDING_TYPE);
965                 }
966         }
967         return ret==1 ? tlen : ret;
968 }
969
970 #endif
971
972 #ifndef OPENSSL_NO_DSA
973 /* DSA sign and verify */
974 static  DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *from, int flen, DSA *dsa)
975 {
976         int ret=0;
977         char *hptr=NULL;
978         DSA_SIG *psign=NULL;
979         char msg[64]="ENGINE_dsa_do_sign";
980         if (!p_surewarehk_Dsa_Sign)
981         {
982                 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ENGINE_R_NOT_INITIALISED);
983                 goto err;
984         }
985         /* extract ref to private key */
986         else if (!(hptr=DSA_get_ex_data(dsa, dsaHndidx)))
987         {
988                 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,SUREWARE_R_MISSING_KEY_COMPONENTS);
989                 goto err;
990         }
991         else
992         {
993                 if((psign = DSA_SIG_new()) == NULL)
994                 {
995                         SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ERR_R_MALLOC_FAILURE);
996                         goto err;
997                 }
998                 psign->r=BN_new();
999                 psign->s=BN_new();
1000                 bn_expand2(psign->r, 20/sizeof(BN_ULONG));
1001                 bn_expand2(psign->s, 20/sizeof(BN_ULONG));
1002                 if (!psign->r || psign->r->dmax!=20/sizeof(BN_ULONG) ||
1003                         !psign->s || psign->s->dmax!=20/sizeof(BN_ULONG))
1004                         goto err;
1005                 ret=p_surewarehk_Dsa_Sign(msg,flen,from,
1006                                           (unsigned long *)psign->r->d,
1007                                           (unsigned long *)psign->s->d,
1008                                           hptr);
1009                 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ret);
1010         }
1011         psign->r->top=20/sizeof(BN_ULONG);
1012         bn_fix_top(psign->r);
1013         psign->s->top=20/sizeof(BN_ULONG);
1014         bn_fix_top(psign->s);
1015
1016 err:    
1017         if (psign)
1018         {
1019                 DSA_SIG_free(psign);
1020                 psign=NULL;
1021         }
1022         return psign;
1023 }
1024 #endif
1025
1026 static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1027                              const BIGNUM *m, BN_CTX *ctx)
1028 {
1029         int ret=0;
1030         char msg[64]="ENGINE_modexp";
1031         if (!p_surewarehk_Mod_Exp)
1032         {
1033                 SUREWAREerr(SUREWARE_F_SUREWAREHK_MODEXP,ENGINE_R_NOT_INITIALISED);
1034         }
1035         else
1036         {
1037                 bn_expand2(r,m->top);
1038                 if (r && r->dmax==m->top)
1039                 {
1040                         /* do it*/
1041                         ret=p_surewarehk_Mod_Exp(msg,
1042                                                  m->top*sizeof(BN_ULONG),
1043                                                  (unsigned long *)m->d,
1044                                                  p->top*sizeof(BN_ULONG),
1045                                                  (unsigned long *)p->d,
1046                                                  a->top*sizeof(BN_ULONG),
1047                                                  (unsigned long *)a->d,
1048                                                  (unsigned long *)r->d);
1049                         surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_MODEXP,ret);
1050                         if (ret==1)
1051                         {
1052                                 /* normalise result */
1053                                 r->top=m->top;
1054                                 bn_fix_top(r);
1055                         }
1056                 }
1057         }
1058         return ret;
1059 }
1060 #endif /* !OPENSSL_NO_HW_SUREWARE */
1061 #endif /* !OPENSSL_NO_HW */