1 /* Written by Corinne Dive-Reclus(cdive@baltimore.com)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * Written by Corinne Dive-Reclus(cdive@baltimore.com)
37 * Copyright@2001 Baltimore Technologies Ltd.
40 * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND *
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
43 * ARE DISCLAIMED. IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE *
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
51 ====================================================================*/
55 #include <openssl/crypto.h>
56 #include <openssl/pem.h>
57 #include <openssl/dso.h>
58 #include <openssl/engine.h>
59 #include <openssl/rand.h>
62 #ifndef OPENSSL_NO_HW_SUREWARE
67 #include "vendor_defns/sureware.h"
70 #define SUREWARE_LIB_NAME "sureware engine"
71 #include "e_sureware_err.c"
73 static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
74 static int surewarehk_destroy(ENGINE *e);
75 static int surewarehk_init(ENGINE *e);
76 static int surewarehk_finish(ENGINE *e);
77 static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
78 const BIGNUM *m, BN_CTX *ctx);
81 static int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to,
82 RSA *rsa,int padding);
83 static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to,
84 RSA *rsa,int padding);
87 static int surewarehk_rand_bytes(unsigned char *buf, int num);
88 static void surewarehk_rand_seed(const void *buf, int num);
89 static void surewarehk_rand_add(const void *buf, int num, double entropy);
92 static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
93 UI_METHOD *ui_method, void *callback_data);
94 static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
95 UI_METHOD *ui_method, void *callback_data);
96 static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
97 int idx,long argl, void *argp);
99 static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
100 int idx,long argl, void *argp);
103 #ifndef OPENSSL_NO_RSA
104 /* This function is aliased to mod_exp (with the mont stuff dropped). */
105 static int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
106 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
108 return surewarehk_modexp(r, a, p, m, ctx);
111 /* Our internal RSA_METHOD that we provide pointers to */
112 static RSA_METHOD surewarehk_rsa =
114 "SureWare RSA method",
117 surewarehk_rsa_sign, /* our rsa_sign is OpenSSL priv_enc*/
118 surewarehk_rsa_priv_dec, /* priv_dec*/
120 surewarehk_mod_exp_mont, /*mod_exp_mongomery*/
125 NULL, /* OpenSSL sign*/
126 NULL, /* OpenSSL verify*/
131 #ifndef OPENSSL_NO_DH
132 /* Our internal DH_METHOD that we provide pointers to */
133 /* This function is aliased to mod_exp (with the dh and mont dropped). */
134 static int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
135 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
137 return surewarehk_modexp(r, a, p, m, ctx);
140 static DH_METHOD surewarehk_dh =
142 "SureWare DH method",
145 surewarehk_modexp_dh, /*dh mod exp*/
154 static RAND_METHOD surewarehk_rand =
156 /* "SureWare RAND method", */
157 surewarehk_rand_seed,
158 surewarehk_rand_bytes,
161 surewarehk_rand_bytes,
165 #ifndef OPENSSL_NO_DSA
167 static DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
168 static int surewarehk_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
169 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
170 BN_CTX *ctx, BN_MONT_CTX *in_mont)
175 /* let rr = a1 ^ p1 mod m */
176 if (!surewarehk_modexp(rr,a1,p1,m,ctx)) goto end;
177 /* let t = a2 ^ p2 mod m */
178 if (!surewarehk_modexp(&t,a2,p2,m,ctx)) goto end;
179 /* let rr = rr * t mod m */
180 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
187 static DSA_METHOD surewarehk_dsa =
189 "SureWare DSA method",
190 surewarehk_dsa_do_sign,
193 surewarehk_dsa_mod_exp,/*mod exp*/
204 static const char *engine_sureware_id = "sureware";
205 static const char *engine_sureware_name = "SureWare hardware engine support";
207 /* Now, to our own code */
209 /* As this is only ever called once, there's no need for locking
210 * (indeed - the lock will already be held by our caller!!!) */
211 static int bind_sureware(ENGINE *e)
213 #ifndef OPENSSL_NO_RSA
214 const RSA_METHOD *meth1;
216 #ifndef OPENSSL_NO_DSA
217 const DSA_METHOD *meth2;
219 #ifndef OPENSSL_NO_DH
220 const DH_METHOD *meth3;
223 if(!ENGINE_set_id(e, engine_sureware_id) ||
224 !ENGINE_set_name(e, engine_sureware_name) ||
225 #ifndef OPENSSL_NO_RSA
226 !ENGINE_set_RSA(e, &surewarehk_rsa) ||
228 #ifndef OPENSSL_NO_DSA
229 !ENGINE_set_DSA(e, &surewarehk_dsa) ||
231 #ifndef OPENSSL_NO_DH
232 !ENGINE_set_DH(e, &surewarehk_dh) ||
234 !ENGINE_set_RAND(e, &surewarehk_rand) ||
235 !ENGINE_set_destroy_function(e, surewarehk_destroy) ||
236 !ENGINE_set_init_function(e, surewarehk_init) ||
237 !ENGINE_set_finish_function(e, surewarehk_finish) ||
238 !ENGINE_set_ctrl_function(e, surewarehk_ctrl) ||
239 !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) ||
240 !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey))
243 #ifndef OPENSSL_NO_RSA
244 /* We know that the "PKCS1_SSLeay()" functions hook properly
245 * to the cswift-specific mod_exp and mod_exp_crt so we use
246 * those functions. NB: We don't use ENGINE_openssl() or
247 * anything "more generic" because something like the RSAref
248 * code may not hook properly, and if you own one of these
249 * cards then you have the right to do RSA operations on it
251 meth1 = RSA_PKCS1_SSLeay();
254 surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
255 surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
259 #ifndef OPENSSL_NO_DSA
260 /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
262 meth2 = DSA_OpenSSL();
265 surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify;
269 #ifndef OPENSSL_NO_DH
270 /* Much the same for Diffie-Hellman */
271 meth3 = DH_OpenSSL();
274 surewarehk_dh.generate_key = meth3->generate_key;
275 surewarehk_dh.compute_key = meth3->compute_key;
279 /* Ensure the sureware error handling is set up */
280 ERR_load_SUREWARE_strings();
284 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
285 static int bind_helper(ENGINE *e, const char *id)
287 if(id && (strcmp(id, engine_sureware_id) != 0))
289 if(!bind_sureware(e))
293 IMPLEMENT_DYNAMIC_CHECK_FN()
294 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
296 static ENGINE *engine_sureware(void)
298 ENGINE *ret = ENGINE_new();
301 if(!bind_sureware(ret))
309 void ENGINE_load_sureware(void)
311 /* Copied from eng_[openssl|dyn].c */
312 ENGINE *toadd = engine_sureware();
320 /* This is a process-global DSO handle used for loading and unloading
321 * the SureWareHook library. NB: This is only set (or unset) during an
322 * init() or finish() call (reference counts permitting) and they're
323 * operating with global locks, so this should be thread-safe
325 static DSO *surewarehk_dso = NULL;
326 #ifndef OPENSSL_NO_RSA
327 static int rsaHndidx = -1; /* Index for KM handle. Not really used yet. */
329 #ifndef OPENSSL_NO_DSA
330 static int dsaHndidx = -1; /* Index for KM handle. Not really used yet. */
333 /* These are the function pointers that are (un)set when the library has
334 * successfully (un)loaded. */
335 static SureWareHook_Init_t *p_surewarehk_Init = NULL;
336 static SureWareHook_Finish_t *p_surewarehk_Finish = NULL;
337 static SureWareHook_Rand_Bytes_t *p_surewarehk_Rand_Bytes = NULL;
338 static SureWareHook_Rand_Seed_t *p_surewarehk_Rand_Seed = NULL;
339 static SureWareHook_Load_Privkey_t *p_surewarehk_Load_Privkey = NULL;
340 static SureWareHook_Info_Pubkey_t *p_surewarehk_Info_Pubkey = NULL;
341 static SureWareHook_Load_Rsa_Pubkey_t *p_surewarehk_Load_Rsa_Pubkey = NULL;
342 static SureWareHook_Load_Dsa_Pubkey_t *p_surewarehk_Load_Dsa_Pubkey = NULL;
343 static SureWareHook_Free_t *p_surewarehk_Free=NULL;
344 static SureWareHook_Rsa_Priv_Dec_t *p_surewarehk_Rsa_Priv_Dec=NULL;
345 static SureWareHook_Rsa_Sign_t *p_surewarehk_Rsa_Sign=NULL;
346 static SureWareHook_Dsa_Sign_t *p_surewarehk_Dsa_Sign=NULL;
347 static SureWareHook_Mod_Exp_t *p_surewarehk_Mod_Exp=NULL;
349 /* Used in the DSO operations. */
350 static const char *surewarehk_LIBNAME = "SureWareHook";
351 static const char *n_surewarehk_Init = "SureWareHook_Init";
352 static const char *n_surewarehk_Finish = "SureWareHook_Finish";
353 static const char *n_surewarehk_Rand_Bytes="SureWareHook_Rand_Bytes";
354 static const char *n_surewarehk_Rand_Seed="SureWareHook_Rand_Seed";
355 static const char *n_surewarehk_Load_Privkey="SureWareHook_Load_Privkey";
356 static const char *n_surewarehk_Info_Pubkey="SureWareHook_Info_Pubkey";
357 static const char *n_surewarehk_Load_Rsa_Pubkey="SureWareHook_Load_Rsa_Pubkey";
358 static const char *n_surewarehk_Load_Dsa_Pubkey="SureWareHook_Load_Dsa_Pubkey";
359 static const char *n_surewarehk_Free="SureWareHook_Free";
360 static const char *n_surewarehk_Rsa_Priv_Dec="SureWareHook_Rsa_Priv_Dec";
361 static const char *n_surewarehk_Rsa_Sign="SureWareHook_Rsa_Sign";
362 static const char *n_surewarehk_Dsa_Sign="SureWareHook_Dsa_Sign";
363 static const char *n_surewarehk_Mod_Exp="SureWareHook_Mod_Exp";
364 static BIO *logstream = NULL;
366 /* SureWareHook library functions and mechanics - these are used by the
367 * higher-level functions further down. NB: As and where there's no
368 * error checking, take a look lower down where these functions are
369 * called, the checking and error handling is probably down there.
371 static int threadsafe=1;
372 static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
378 case ENGINE_CTRL_SET_LOGSTREAM:
381 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
387 if (CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO) > 1)
390 SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,SUREWARE_R_BIO_WAS_FREED);
392 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
394 /* This will prevent the initialisation function from "installing"
395 * the mutex-handling callbacks, even if they are available from
396 * within the library (or were provided to the library from the
397 * calling application). This is to remove any baggage for
398 * applications not using multithreading. */
399 case ENGINE_CTRL_CHIL_NO_LOCKING:
400 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
402 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
405 /* The command isn't understood by this engine */
407 SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
408 ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
416 /* Destructor (complements the "ENGINE_surewarehk()" constructor) */
417 static int surewarehk_destroy(ENGINE *e)
419 ERR_unload_SUREWARE_strings();
423 /* (de)initialisation functions. */
424 static int surewarehk_init(ENGINE *e)
426 char msg[64]="ENGINE_init";
427 SureWareHook_Init_t *p1=NULL;
428 SureWareHook_Finish_t *p2=NULL;
429 SureWareHook_Rand_Bytes_t *p3=NULL;
430 SureWareHook_Rand_Seed_t *p4=NULL;
431 SureWareHook_Load_Privkey_t *p5=NULL;
432 SureWareHook_Load_Rsa_Pubkey_t *p6=NULL;
433 SureWareHook_Free_t *p7=NULL;
434 SureWareHook_Rsa_Priv_Dec_t *p8=NULL;
435 SureWareHook_Rsa_Sign_t *p9=NULL;
436 SureWareHook_Dsa_Sign_t *p12=NULL;
437 SureWareHook_Info_Pubkey_t *p13=NULL;
438 SureWareHook_Load_Dsa_Pubkey_t *p14=NULL;
439 SureWareHook_Mod_Exp_t *p15=NULL;
441 if(surewarehk_dso != NULL)
443 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_ALREADY_LOADED);
446 /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */
447 surewarehk_dso = DSO_load(NULL, surewarehk_LIBNAME, NULL, 0);
448 if(surewarehk_dso == NULL)
450 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE);
453 if(!(p1=(SureWareHook_Init_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Init)) ||
454 !(p2=(SureWareHook_Finish_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Finish)) ||
455 !(p3=(SureWareHook_Rand_Bytes_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Bytes)) ||
456 !(p4=(SureWareHook_Rand_Seed_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Seed)) ||
457 !(p5=(SureWareHook_Load_Privkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Privkey)) ||
458 !(p6=(SureWareHook_Load_Rsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Rsa_Pubkey)) ||
459 !(p7=(SureWareHook_Free_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Free)) ||
460 !(p8=(SureWareHook_Rsa_Priv_Dec_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Priv_Dec)) ||
461 !(p9=(SureWareHook_Rsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Sign)) ||
462 !(p12=(SureWareHook_Dsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Dsa_Sign)) ||
463 !(p13=(SureWareHook_Info_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Info_Pubkey)) ||
464 !(p14=(SureWareHook_Load_Dsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Dsa_Pubkey)) ||
465 !(p15=(SureWareHook_Mod_Exp_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Mod_Exp)))
467 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE);
470 /* Copy the pointers */
471 p_surewarehk_Init = p1;
472 p_surewarehk_Finish = p2;
473 p_surewarehk_Rand_Bytes = p3;
474 p_surewarehk_Rand_Seed = p4;
475 p_surewarehk_Load_Privkey = p5;
476 p_surewarehk_Load_Rsa_Pubkey = p6;
477 p_surewarehk_Free = p7;
478 p_surewarehk_Rsa_Priv_Dec = p8;
479 p_surewarehk_Rsa_Sign = p9;
480 p_surewarehk_Dsa_Sign = p12;
481 p_surewarehk_Info_Pubkey = p13;
482 p_surewarehk_Load_Dsa_Pubkey = p14;
483 p_surewarehk_Mod_Exp = p15;
484 /* Contact the hardware and initialises it. */
485 if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE)
487 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE);
490 if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE)
492 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE);
495 /* try to load the default private key, if failed does not return a failure but
496 wait for an explicit ENGINE_load_privakey */
497 surewarehk_load_privkey(e,NULL,NULL,NULL);
499 /* Everything's fine. */
500 #ifndef OPENSSL_NO_RSA
502 rsaHndidx = RSA_get_ex_new_index(0,
503 "SureWareHook RSA key handle",
504 NULL, NULL, surewarehk_ex_free);
506 #ifndef OPENSSL_NO_DSA
508 dsaHndidx = DSA_get_ex_new_index(0,
509 "SureWareHook DSA key handle",
510 NULL, NULL, surewarehk_ex_free);
516 DSO_free(surewarehk_dso);
517 surewarehk_dso = NULL;
518 p_surewarehk_Init = NULL;
519 p_surewarehk_Finish = NULL;
520 p_surewarehk_Rand_Bytes = NULL;
521 p_surewarehk_Rand_Seed = NULL;
522 p_surewarehk_Load_Privkey = NULL;
523 p_surewarehk_Load_Rsa_Pubkey = NULL;
524 p_surewarehk_Free = NULL;
525 p_surewarehk_Rsa_Priv_Dec = NULL;
526 p_surewarehk_Rsa_Sign = NULL;
527 p_surewarehk_Dsa_Sign = NULL;
528 p_surewarehk_Info_Pubkey = NULL;
529 p_surewarehk_Load_Dsa_Pubkey = NULL;
530 p_surewarehk_Mod_Exp = NULL;
534 static int surewarehk_finish(ENGINE *e)
537 if(surewarehk_dso == NULL)
539 SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH,ENGINE_R_NOT_LOADED);
543 p_surewarehk_Finish();
544 if(!DSO_free(surewarehk_dso))
546 SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH,ENGINE_R_DSO_FAILURE);
553 surewarehk_dso = NULL;
554 p_surewarehk_Init = NULL;
555 p_surewarehk_Finish = NULL;
556 p_surewarehk_Rand_Bytes = NULL;
557 p_surewarehk_Rand_Seed = NULL;
558 p_surewarehk_Load_Privkey = NULL;
559 p_surewarehk_Load_Rsa_Pubkey = NULL;
560 p_surewarehk_Free = NULL;
561 p_surewarehk_Rsa_Priv_Dec = NULL;
562 p_surewarehk_Rsa_Sign = NULL;
563 p_surewarehk_Dsa_Sign = NULL;
564 p_surewarehk_Info_Pubkey = NULL;
565 p_surewarehk_Load_Dsa_Pubkey = NULL;
566 p_surewarehk_Mod_Exp = NULL;
570 static void surewarehk_error_handling(char *const msg,int func,int ret)
574 case SUREWAREHOOK_ERROR_UNIT_FAILURE:
575 ENGINEerr(func,SUREWARE_R_UNIT_FAILURE);
577 case SUREWAREHOOK_ERROR_FALLBACK:
578 ENGINEerr(func,SUREWARE_R_REQUEST_FALLBACK);
580 case SUREWAREHOOK_ERROR_DATA_SIZE:
581 ENGINEerr(func,SUREWARE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
583 case SUREWAREHOOK_ERROR_INVALID_PAD:
584 ENGINEerr(func,RSA_R_PADDING_CHECK_FAILED);
587 ENGINEerr(func,SUREWARE_R_REQUEST_FAILED);
594 ERR_add_error_data(1,msg);
597 CRYPTO_w_lock(CRYPTO_LOCK_BIO);
598 BIO_write(logstream, msg, strlen(msg));
599 CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
604 static int surewarehk_rand_bytes(unsigned char *buf, int num)
607 char msg[64]="ENGINE_rand_bytes";
608 if(!p_surewarehk_Rand_Bytes)
610 SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_BYTES,ENGINE_R_NOT_INITIALISED);
614 ret = p_surewarehk_Rand_Bytes(msg,buf, num);
615 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RAND_BYTES,ret);
617 return ret==1 ? 1 : 0;
620 static void surewarehk_rand_seed(const void *buf, int num)
623 char msg[64]="ENGINE_rand_seed";
624 if(!p_surewarehk_Rand_Seed)
626 SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_SEED,ENGINE_R_NOT_INITIALISED);
630 ret = p_surewarehk_Rand_Seed(msg,buf, num);
631 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RAND_SEED,ret);
635 static void surewarehk_rand_add(const void *buf, int num, double entropy)
637 surewarehk_rand_seed(buf,num);
640 static EVP_PKEY* sureware_load_public(ENGINE *e,const char *key_id,char *hptr,unsigned long el,char keytype)
642 EVP_PKEY *res = NULL;
643 #ifndef OPENSSL_NO_RSA
646 #ifndef OPENSSL_NO_DSA
649 char msg[64]="sureware_load_public";
651 if(!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey)
653 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBLIC_KEY,ENGINE_R_NOT_INITIALISED);
658 #ifndef OPENSSL_NO_RSA
660 /* set private external reference */
661 rsatmp = RSA_new_method(e);
662 RSA_set_ex_data(rsatmp,rsaHndidx,hptr);
663 rsatmp->flags |= RSA_FLAG_EXT_PKEY;
665 /* set public big nums*/
666 rsatmp->e = BN_new();
667 rsatmp->n = BN_new();
668 bn_expand2(rsatmp->e, el/sizeof(BN_ULONG));
669 bn_expand2(rsatmp->n, el/sizeof(BN_ULONG));
670 if (!rsatmp->e || rsatmp->e->dmax!=(int)(el/sizeof(BN_ULONG))||
671 !rsatmp->n || rsatmp->n->dmax!=(int)(el/sizeof(BN_ULONG)))
673 ret=p_surewarehk_Load_Rsa_Pubkey(msg,key_id,el,
674 (unsigned long *)rsatmp->n->d,
675 (unsigned long *)rsatmp->e->d);
676 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_LOAD_PUBLIC_KEY,ret);
679 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVATE_KEY,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
682 /* normalise pub e and pub n */
683 rsatmp->e->top=el/sizeof(BN_ULONG);
684 bn_fix_top(rsatmp->e);
685 rsatmp->n->top=el/sizeof(BN_ULONG);
686 bn_fix_top(rsatmp->n);
687 /* create an EVP object: engine + rsa key */
688 res = EVP_PKEY_new();
689 EVP_PKEY_assign_RSA(res, rsatmp);
693 #ifndef OPENSSL_NO_DSA
695 /* set private/public external reference */
696 dsatmp = DSA_new_method(e);
697 DSA_set_ex_data(dsatmp,dsaHndidx,hptr);
698 /*dsatmp->flags |= DSA_FLAG_EXT_PKEY;*/
701 dsatmp->pub_key = BN_new();
702 dsatmp->p = BN_new();
703 dsatmp->q = BN_new();
704 dsatmp->g = BN_new();
705 bn_expand2(dsatmp->pub_key, el/sizeof(BN_ULONG));
706 bn_expand2(dsatmp->p, el/sizeof(BN_ULONG));
707 bn_expand2(dsatmp->q, 20/sizeof(BN_ULONG));
708 bn_expand2(dsatmp->g, el/sizeof(BN_ULONG));
709 if (!dsatmp->pub_key || dsatmp->pub_key->dmax!=(int)(el/sizeof(BN_ULONG))||
710 !dsatmp->p || dsatmp->p->dmax!=(int)(el/sizeof(BN_ULONG)) ||
711 !dsatmp->q || dsatmp->q->dmax!=20/sizeof(BN_ULONG) ||
712 !dsatmp->g || dsatmp->g->dmax!=(int)(el/sizeof(BN_ULONG)))
715 ret=p_surewarehk_Load_Dsa_Pubkey(msg,key_id,el,
716 (unsigned long *)dsatmp->pub_key->d,
717 (unsigned long *)dsatmp->p->d,
718 (unsigned long *)dsatmp->q->d,
719 (unsigned long *)dsatmp->g->d);
720 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_LOAD_PUBLIC_KEY,ret);
723 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVATE_KEY,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
727 /* normalise pubkey and parameters in case of */
728 dsatmp->pub_key->top=el/sizeof(BN_ULONG);
729 bn_fix_top(dsatmp->pub_key);
730 dsatmp->p->top=el/sizeof(BN_ULONG);
731 bn_fix_top(dsatmp->p);
732 dsatmp->q->top=20/sizeof(BN_ULONG);
733 bn_fix_top(dsatmp->q);
734 dsatmp->g->top=el/sizeof(BN_ULONG);
735 bn_fix_top(dsatmp->g);
737 /* create an EVP object: engine + rsa key */
738 res = EVP_PKEY_new();
739 EVP_PKEY_assign_DSA(res, dsatmp);
744 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVATE_KEY,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
751 #ifndef OPENSSL_NO_RSA
755 #ifndef OPENSSL_NO_DSA
762 static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
763 UI_METHOD *ui_method, void *callback_data)
765 EVP_PKEY *res = NULL;
770 char msg[64]="ENGINE_load_privkey";
772 if(!p_surewarehk_Load_Privkey)
774 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVATE_KEY,ENGINE_R_NOT_INITIALISED);
778 ret=p_surewarehk_Load_Privkey(msg,key_id,&hptr,&el,&keytype);
781 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVATE_KEY,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
782 ERR_add_error_data(1,msg);
785 res=sureware_load_public(e,key_id,hptr,el,keytype);
790 static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
791 UI_METHOD *ui_method, void *callback_data)
793 EVP_PKEY *res = NULL;
798 char msg[64]="ENGINE_load_pubkey";
800 if(!p_surewarehk_Info_Pubkey)
802 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBLIC_KEY,ENGINE_R_NOT_INITIALISED);
806 /* call once to identify if DSA or RSA */
807 ret=p_surewarehk_Info_Pubkey(msg,key_id,&el,&keytype);
810 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBLIC_KEY,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
811 ERR_add_error_data(1,msg);
814 res=sureware_load_public(e,key_id,hptr,el,keytype);
819 /* This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
820 , called when ex_data is freed */
821 static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
822 int idx,long argl, void *argp)
824 if(!p_surewarehk_Free)
826 SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE,ENGINE_R_NOT_INITIALISED);
829 p_surewarehk_Free((char *)item,0);
833 /* not currently used (bug?) */
834 /* This cleans up an DH KM key (destroys the key into hardware),
835 called when ex_data is freed */
836 static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
837 int idx,long argl, void *argp)
839 if(!p_surewarehk_Free)
841 SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE,ENGINE_R_NOT_INITIALISED);
844 p_surewarehk_Free((char *)item,1);
849 * return number of decrypted bytes
851 #ifndef OPENSSL_NO_RSA
852 static int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to,
853 RSA *rsa,int padding)
856 char *buf=NULL,*hptr=NULL;
857 char msg[64]="ENGINE_rsa_priv_dec";
858 if (!p_surewarehk_Rsa_Priv_Dec)
860 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ENGINE_R_NOT_INITIALISED);
862 /* extract ref to private key */
863 else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx)))
865 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_MISSING_KEY_COMPONENTS);
868 /* analyse what padding we can do into the hardware */
869 if (padding==RSA_PKCS1_PADDING)
872 ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD);
873 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ret);
878 else /* do with no padding into hardware */
880 ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_NO_PAD);
881 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ret);
884 /* intermediate buffer for padding */
885 if ((buf=OPENSSL_malloc(tlen)) == NULL)
887 RSAerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ERR_R_MALLOC_FAILURE);
890 memcpy(buf,to,tlen);/* transfert to into buf */
891 switch (padding) /* check padding in software */
893 #ifndef OPENSSL_NO_SHA
894 case RSA_PKCS1_OAEP_PADDING:
895 ret=RSA_padding_check_PKCS1_OAEP(to,tlen,(unsigned char *)buf,tlen,tlen,NULL,0);
898 case RSA_SSLV23_PADDING:
899 ret=RSA_padding_check_SSLv23(to,tlen,(unsigned char *)buf,flen,tlen);
902 ret=RSA_padding_check_none(to,tlen,(unsigned char *)buf,flen,tlen);
905 RSAerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,RSA_R_UNKNOWN_PADDING_TYPE);
909 RSAerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,RSA_R_PADDING_CHECK_FAILED);
914 OPENSSL_cleanse(buf,tlen);
921 * Does what OpenSSL rsa_priv_enc does.
923 static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to,
924 RSA *rsa,int padding)
928 char msg[64]="ENGINE_rsa_sign";
929 if (!p_surewarehk_Rsa_Sign)
931 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_ENC,ENGINE_R_NOT_INITIALISED);
933 /* extract ref to private key */
934 else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx)))
936 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_ENC,SUREWARE_R_MISSING_KEY_COMPONENTS);
942 case RSA_PKCS1_PADDING: /* do it in one shot */
943 ret=p_surewarehk_Rsa_Sign(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD);
944 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_ENC,ret);
948 RSAerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_ENC,RSA_R_UNKNOWN_PADDING_TYPE);
951 return ret==1 ? tlen : ret;
956 #ifndef OPENSSL_NO_DSA
957 /* DSA sign and verify */
958 static DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *from, int flen, DSA *dsa)
963 char msg[64]="ENGINE_dsa_do_sign";
964 if (!p_surewarehk_Dsa_Sign)
966 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ENGINE_R_NOT_INITIALISED);
968 /* extract ref to private key */
969 else if (!(hptr=DSA_get_ex_data(dsa, dsaHndidx)))
971 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,SUREWARE_R_MISSING_KEY_COMPONENTS);
975 if((psign = DSA_SIG_new()) == NULL)
977 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ERR_R_MALLOC_FAILURE);
982 bn_expand2(psign->r, 20/sizeof(BN_ULONG));
983 bn_expand2(psign->s, 20/sizeof(BN_ULONG));
984 if (!psign->r || psign->r->dmax!=20/sizeof(BN_ULONG) ||
985 !psign->s || psign->s->dmax!=20/sizeof(BN_ULONG))
987 ret=p_surewarehk_Dsa_Sign(msg,flen,from,
988 (unsigned long *)psign->r->d,
989 (unsigned long *)psign->s->d,
991 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ret);
993 psign->r->top=20/sizeof(BN_ULONG);
994 bn_fix_top(psign->r);
995 psign->s->top=20/sizeof(BN_ULONG);
996 bn_fix_top(psign->s);
1001 DSA_SIG_free(psign);
1008 static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1009 const BIGNUM *m, BN_CTX *ctx)
1012 char msg[64]="ENGINE_modexp";
1013 if (!p_surewarehk_Mod_Exp)
1015 SUREWAREerr(SUREWARE_F_SUREWAREHK_MOD_EXP,ENGINE_R_NOT_INITIALISED);
1019 bn_expand2(r,m->top);
1020 if (r && r->dmax==m->top)
1023 ret=p_surewarehk_Mod_Exp(msg,
1024 m->top*sizeof(BN_ULONG),
1025 (unsigned long *)m->d,
1026 p->top*sizeof(BN_ULONG),
1027 (unsigned long *)p->d,
1028 a->top*sizeof(BN_ULONG),
1029 (unsigned long *)a->d,
1030 (unsigned long *)r->d);
1031 surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_MOD_EXP,ret);
1034 /* normalise result */
1042 #endif /* !OPENSSL_NO_HW_SureWare */
1043 #endif /* !OPENSSL_NO_HW */