1 /* crypto/engine/hw_zencod.c */
3 * Written by Fred Donnat (frederic.donnat@zencod.com) for "zencod" * engine
4 * integration in order to redirect crypto computing on a crypto * hardware
5 * accelerator zenssl32 ;-) * * Date : 25 jun 2002 * Revision : 17 Ju7 2002
6 * * Version : zencod_engine-0.9.7
9 /* ====================================================================
10 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
24 * 3. All advertising materials mentioning features or use of this
25 * software must display the following acknowledgment:
26 * "This product includes software developed by the OpenSSL Project
27 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
29 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
30 * endorse or promote products derived from this software without
31 * prior written permission. For written permission, please contact
32 * licensing@OpenSSL.org.
34 * 5. Products derived from this software may not be called "OpenSSL"
35 * nor may "OpenSSL" appear in their names without prior written
36 * permission of the OpenSSL Project.
38 * 6. Redistributions of any form whatsoever must retain the following
40 * "This product includes software developed by the OpenSSL Project
41 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
43 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
44 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
47 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 * ====================================================================
57 * This product includes cryptographic software written by Eric Young
58 * (eay@cryptsoft.com). This product includes software written by Tim
59 * Hudson (tjh@cryptsoft.com).
63 /* ENGINE general include */
65 #include <openssl/crypto.h>
66 #include <openssl/dso.h>
67 #include <openssl/engine.h>
70 # ifndef OPENSSL_NO_HW_ZENCOD
73 # include "hw_zencod.h"
75 # include "vendor_defns/hw_zencod.h"
78 # define ZENCOD_LIB_NAME "zencod engine"
79 # include "hw_zencod_err.c"
81 # define FAIL_TO_SOFTWARE -15
83 # define ZEN_LIBRARY "zenbridge"
85 # ifdef ZENCOD_TRACING
86 # define PERROR(s) perror(s)
87 # define CHEESE() fputs("## [ZenEngine] ## " __FUNCTION__ "\n", stderr)
95 static inline void esrever(unsigned char *d
, int l
)
97 for (; --l
> 0; --l
, d
++) {
104 static inline void ypcmem(unsigned char *d
, const unsigned char *s
, int l
)
110 static __inline
void esrever(unsigned char *d
, int l
)
112 for (; --l
> 0; --l
, d
++) {
119 static __inline
void ypcmem(unsigned char *d
, const unsigned char *s
, int l
)
126 # define BIGNUM2ZEN(n, bn) (ptr_zencod_init_number((n), \
127 (unsigned long) ((bn)->top * BN_BITS2), \
128 (unsigned char *) ((bn)->d)))
130 # define ZEN_BITS(n, bytes) (ptr_zencod_bytes2bits((unsigned char *) (n), (unsigned long) (bytes)))
131 # define ZEN_BYTES(bits) (ptr_zencod_bits2bytes((unsigned long) (bits)))
133 /* Function for ENGINE detection and control */
134 static int zencod_destroy(ENGINE
*e
);
135 static int zencod_init(ENGINE
*e
);
136 static int zencod_finish(ENGINE
*e
);
137 static int zencod_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) ());
140 static int zencod_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
141 const BIGNUM
*m
, BN_CTX
*ctx
);
144 # ifndef OPENSSL_NO_RSA
145 static int RSA_zencod_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
);
146 static int RSA_zencod_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
147 const BIGNUM
*m
, BN_CTX
*ctx
,
152 # ifndef OPENSSL_NO_DSA
153 static int DSA_zencod_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
154 const BIGNUM
*p
, const BIGNUM
*m
,
155 BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
157 static DSA_SIG
*DSA_zencod_do_sign(const unsigned char *dgst
, int dlen
,
159 static int DSA_zencod_do_verify(const unsigned char *dgst
, int dgst_len
,
160 DSA_SIG
*sig
, DSA
*dsa
);
164 # ifndef OPENSSL_NO_DH
165 static int DH_zencod_bn_mod_exp(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
166 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
168 static int DH_zencod_generate_key(DH
*dh
);
169 static int DH_zencod_compute_key(unsigned char *key
, const BIGNUM
*pub_key
,
174 static void RAND_zencod_seed(const void *buf
, int num
);
175 static int RAND_zencod_rand_bytes(unsigned char *buf
, int num
);
176 static int RAND_zencod_rand_status(void);
179 static int engine_digests(ENGINE
*e
, const EVP_MD
**digest
, const int **nids
,
183 static int engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
184 const int **nids
, int nid
);
186 # define ZENCOD_CMD_SO_PATH ENGINE_CMD_BASE
187 static const ENGINE_CMD_DEFN zencod_cmd_defns
[] = {
190 "Specifies the path to the 'zenbridge' shared library",
191 ENGINE_CMD_FLAG_STRING
},
195 # ifndef OPENSSL_NO_RSA
197 * Our internal RSA_METHOD specific to zencod ENGINE providing pointers to
200 static RSA_METHOD zencod_rsa
= {
206 RSA_zencod_rsa_mod_exp
,
207 RSA_zencod_bn_mod_exp
,
217 # ifndef OPENSSL_NO_DSA
219 * Our internal DSA_METHOD specific to zencod ENGINE providing pointers to
222 static DSA_METHOD zencod_dsa
= {
226 DSA_zencod_do_verify
,
228 DSA_zencod_bn_mod_exp
,
236 # ifndef OPENSSL_NO_DH
238 * Our internal DH_METHOD specific to zencod ENGINE providing pointers to our
241 static DH_METHOD zencod_dh
= {
243 DH_zencod_generate_key
,
244 DH_zencod_compute_key
,
245 DH_zencod_bn_mod_exp
,
254 * Our internal RAND_meth specific to zencod ZNGINE providing pointers to our
257 static RAND_METHOD zencod_rand
= {
259 RAND_zencod_rand_bytes
,
262 RAND_zencod_rand_bytes
,
263 RAND_zencod_rand_status
266 /* Constants used when creating the ENGINE */
267 static const char *engine_zencod_id
= "zencod";
268 static const char *engine_zencod_name
= "ZENCOD hardware engine support";
271 * This internal function is used by ENGINE_zencod () and possibly by the
272 * "dynamic" ENGINE support too ;-)
274 static int bind_helper(ENGINE
*e
)
277 # ifndef OPENSSL_NO_RSA
278 const RSA_METHOD
*meth_rsa
;
280 # ifndef OPENSSL_NO_DSA
281 const DSA_METHOD
*meth_dsa
;
283 # ifndef OPENSSL_NO_DH
284 const DH_METHOD
*meth_dh
;
287 const RAND_METHOD
*meth_rand
;
289 if (!ENGINE_set_id(e
, engine_zencod_id
) ||
290 !ENGINE_set_name(e
, engine_zencod_name
) ||
291 # ifndef OPENSSL_NO_RSA
292 !ENGINE_set_RSA(e
, &zencod_rsa
) ||
294 # ifndef OPENSSL_NO_DSA
295 !ENGINE_set_DSA(e
, &zencod_dsa
) ||
297 # ifndef OPENSSL_NO_DH
298 !ENGINE_set_DH(e
, &zencod_dh
) ||
300 !ENGINE_set_RAND(e
, &zencod_rand
) ||
301 !ENGINE_set_destroy_function(e
, zencod_destroy
) ||
302 !ENGINE_set_init_function(e
, zencod_init
) ||
303 !ENGINE_set_finish_function(e
, zencod_finish
) ||
304 !ENGINE_set_ctrl_function(e
, zencod_ctrl
) ||
305 !ENGINE_set_cmd_defns(e
, zencod_cmd_defns
) ||
306 !ENGINE_set_digests(e
, engine_digests
) ||
307 !ENGINE_set_ciphers(e
, engine_ciphers
)) {
310 # ifndef OPENSSL_NO_RSA
312 * We know that the "PKCS1_SSLeay()" functions hook properly to the
313 * Zencod-specific mod_exp and mod_exp_crt so we use those functions. NB:
314 * We don't use ENGINE_openssl() or anything "more generic" because
315 * something like the RSAref code may not hook properly, and if you own
316 * one of these cards then you have the right to do RSA operations on it
319 meth_rsa
= RSA_PKCS1_SSLeay();
321 zencod_rsa
.rsa_pub_enc
= meth_rsa
->rsa_pub_enc
;
322 zencod_rsa
.rsa_pub_dec
= meth_rsa
->rsa_pub_dec
;
323 zencod_rsa
.rsa_priv_enc
= meth_rsa
->rsa_priv_enc
;
324 zencod_rsa
.rsa_priv_dec
= meth_rsa
->rsa_priv_dec
;
325 /* meth_rsa->rsa_mod_exp */
326 /* meth_rsa->bn_mod_exp */
327 zencod_rsa
.init
= meth_rsa
->init
;
328 zencod_rsa
.finish
= meth_rsa
->finish
;
331 # ifndef OPENSSL_NO_DSA
333 * We use OpenSSL meth to supply what we don't provide ;-*)
335 meth_dsa
= DSA_OpenSSL();
337 /* meth_dsa->dsa_do_sign */
338 zencod_dsa
.dsa_sign_setup
= meth_dsa
->dsa_sign_setup
;
339 /* meth_dsa->dsa_do_verify */
340 zencod_dsa
.dsa_mod_exp
= meth_dsa
->dsa_mod_exp
;
341 /* zencod_dsa.bn_mod_exp = meth_dsa->bn_mod_exp ; */
342 zencod_dsa
.init
= meth_dsa
->init
;
343 zencod_dsa
.finish
= meth_dsa
->finish
;
346 # ifndef OPENSSL_NO_DH
348 * We use OpenSSL meth to supply what we don't provide ;-*)
350 meth_dh
= DH_OpenSSL();
352 /* zencod_dh.generate_key = meth_dh->generate_key ; */
353 /* zencod_dh.compute_key = meth_dh->compute_key ; */
354 /* zencod_dh.bn_mod_exp = meth_dh->bn_mod_exp ; */
355 zencod_dh
.init
= meth_dh
->init
;
356 zencod_dh
.finish
= meth_dh
->finish
;
361 * We use OpenSSL (SSLeay) meth to supply what we don't provide ;-*)
363 meth_rand
= RAND_SSLeay();
365 /* meth_rand->seed ; */
366 /* zencod_rand.seed = meth_rand->seed ; */
367 /* meth_rand->bytes ; */
368 /* zencod_rand.bytes = meth_rand->bytes ; */
369 zencod_rand
.cleanup
= meth_rand
->cleanup
;
370 zencod_rand
.add
= meth_rand
->add
;
371 /* meth_rand->pseudorand ; */
372 /* zencod_rand.pseudorand = meth_rand->pseudorand ; */
373 /* zencod_rand.status = meth_rand->status ; */
374 /* meth_rand->status ; */
376 /* Ensure the zencod error handling is set up */
377 ERR_load_ZENCOD_strings();
382 * As this is only ever called once, there's no need for locking (indeed -
383 * the lock will already be held by our caller!!!)
385 static ENGINE
*ENGINE_zencod(void)
388 ENGINE
*eng
= ENGINE_new();
393 if (!bind_helper(eng
)) {
401 # ifdef ENGINE_DYNAMIC_SUPPORT
404 void ENGINE_load_zencod(void)
406 /* Copied from eng_[openssl|dyn].c */
407 ENGINE
*toadd
= ENGINE_zencod();
416 * This is a process-global DSO handle used for loading and unloading the
417 * ZENBRIDGE library. NB: This is only set (or unset) during an * init () or
418 * finish () call (reference counts permitting) and they're * operating with
419 * global locks, so this should be thread-safe * implicitly.
421 static DSO
*zencod_dso
= NULL
;
423 static t_zencod_test
*ptr_zencod_test
= NULL
;
424 static t_zencod_bytes2bits
*ptr_zencod_bytes2bits
= NULL
;
425 static t_zencod_bits2bytes
*ptr_zencod_bits2bytes
= NULL
;
426 static t_zencod_new_number
*ptr_zencod_new_number
= NULL
;
427 static t_zencod_init_number
*ptr_zencod_init_number
= NULL
;
429 static t_zencod_rsa_mod_exp
*ptr_zencod_rsa_mod_exp
= NULL
;
430 static t_zencod_rsa_mod_exp_crt
*ptr_zencod_rsa_mod_exp_crt
= NULL
;
431 static t_zencod_dsa_do_sign
*ptr_zencod_dsa_do_sign
= NULL
;
432 static t_zencod_dsa_do_verify
*ptr_zencod_dsa_do_verify
= NULL
;
433 static t_zencod_dh_generate_key
*ptr_zencod_dh_generate_key
= NULL
;
434 static t_zencod_dh_compute_key
*ptr_zencod_dh_compute_key
= NULL
;
435 static t_zencod_rand_bytes
*ptr_zencod_rand_bytes
= NULL
;
436 static t_zencod_math_mod_exp
*ptr_zencod_math_mod_exp
= NULL
;
438 static t_zencod_md5_init
*ptr_zencod_md5_init
= NULL
;
439 static t_zencod_md5_update
*ptr_zencod_md5_update
= NULL
;
440 static t_zencod_md5_do_final
*ptr_zencod_md5_do_final
= NULL
;
441 static t_zencod_sha1_init
*ptr_zencod_sha1_init
= NULL
;
442 static t_zencod_sha1_update
*ptr_zencod_sha1_update
= NULL
;
443 static t_zencod_sha1_do_final
*ptr_zencod_sha1_do_final
= NULL
;
445 static t_zencod_xdes_cipher
*ptr_zencod_xdes_cipher
= NULL
;
446 static t_zencod_rc4_cipher
*ptr_zencod_rc4_cipher
= NULL
;
449 * These are the static string constants for the DSO file name and the
450 * function symbol names to bind to.
452 static const char *ZENCOD_LIBNAME
= ZEN_LIBRARY
;
454 static const char *ZENCOD_Fct_0
= "test_device";
455 static const char *ZENCOD_Fct_1
= "zenbridge_bytes2bits";
456 static const char *ZENCOD_Fct_2
= "zenbridge_bits2bytes";
457 static const char *ZENCOD_Fct_3
= "zenbridge_new_number";
458 static const char *ZENCOD_Fct_4
= "zenbridge_init_number";
460 static const char *ZENCOD_Fct_exp_1
= "zenbridge_rsa_mod_exp";
461 static const char *ZENCOD_Fct_exp_2
= "zenbridge_rsa_mod_exp_crt";
462 static const char *ZENCOD_Fct_dsa_1
= "zenbridge_dsa_do_sign";
463 static const char *ZENCOD_Fct_dsa_2
= "zenbridge_dsa_do_verify";
464 static const char *ZENCOD_Fct_dh_1
= "zenbridge_dh_generate_key";
465 static const char *ZENCOD_Fct_dh_2
= "zenbridge_dh_compute_key";
466 static const char *ZENCOD_Fct_rand_1
= "zenbridge_rand_bytes";
467 static const char *ZENCOD_Fct_math_1
= "zenbridge_math_mod_exp";
469 static const char *ZENCOD_Fct_md5_1
= "zenbridge_md5_init";
470 static const char *ZENCOD_Fct_md5_2
= "zenbridge_md5_update";
471 static const char *ZENCOD_Fct_md5_3
= "zenbridge_md5_do_final";
472 static const char *ZENCOD_Fct_sha1_1
= "zenbridge_sha1_init";
473 static const char *ZENCOD_Fct_sha1_2
= "zenbridge_sha1_update";
474 static const char *ZENCOD_Fct_sha1_3
= "zenbridge_sha1_do_final";
476 static const char *ZENCOD_Fct_xdes_1
= "zenbridge_xdes_cipher";
477 static const char *ZENCOD_Fct_rc4_1
= "zenbridge_rc4_cipher";
480 * Destructor (complements the "ENGINE_zencod ()" constructor)
482 static int zencod_destroy(ENGINE
*e
)
485 ERR_unload_ZENCOD_strings();
491 * (de)initialisation functions. Control Function
493 static int zencod_init(ENGINE
*e
)
496 t_zencod_test
*ptr_0
;
497 t_zencod_bytes2bits
*ptr_1
;
498 t_zencod_bits2bytes
*ptr_2
;
499 t_zencod_new_number
*ptr_3
;
500 t_zencod_init_number
*ptr_4
;
501 t_zencod_rsa_mod_exp
*ptr_exp_1
;
502 t_zencod_rsa_mod_exp_crt
*ptr_exp_2
;
503 t_zencod_dsa_do_sign
*ptr_dsa_1
;
504 t_zencod_dsa_do_verify
*ptr_dsa_2
;
505 t_zencod_dh_generate_key
*ptr_dh_1
;
506 t_zencod_dh_compute_key
*ptr_dh_2
;
507 t_zencod_rand_bytes
*ptr_rand_1
;
508 t_zencod_math_mod_exp
*ptr_math_1
;
509 t_zencod_md5_init
*ptr_md5_1
;
510 t_zencod_md5_update
*ptr_md5_2
;
511 t_zencod_md5_do_final
*ptr_md5_3
;
512 t_zencod_sha1_init
*ptr_sha1_1
;
513 t_zencod_sha1_update
*ptr_sha1_2
;
514 t_zencod_sha1_do_final
*ptr_sha1_3
;
515 t_zencod_xdes_cipher
*ptr_xdes_1
;
516 t_zencod_rc4_cipher
*ptr_rc4_1
;
521 * We Should add some tests for non NULL parameters or bad value !!
522 * Stuff to be done ...
525 if (zencod_dso
!= NULL
) {
526 ZENCODerr(ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_ALREADY_LOADED
);
530 * Trying to load the Library "cryptozen"
532 zencod_dso
= DSO_load(NULL
, ZENCOD_LIBNAME
, NULL
, 0);
533 if (zencod_dso
== NULL
) {
534 ZENCODerr(ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_DSO_FAILURE
);
539 * Trying to load Function from the Library
543 (t_zencod_bytes2bits
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_1
))
544 || !(ptr_2
= (t_zencod_bits2bytes
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_2
))
545 || !(ptr_3
= (t_zencod_new_number
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_3
))
546 || !(ptr_4
= (t_zencod_init_number
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_4
))
548 (t_zencod_rsa_mod_exp
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_exp_1
))
550 (t_zencod_rsa_mod_exp_crt
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_exp_2
))
552 (t_zencod_dsa_do_sign
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_dsa_1
))
554 (t_zencod_dsa_do_verify
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_dsa_2
))
556 (t_zencod_dh_generate_key
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_dh_1
))
558 (t_zencod_dh_compute_key
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_dh_2
))
560 (t_zencod_rand_bytes
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_rand_1
))
562 (t_zencod_math_mod_exp
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_math_1
))
563 || !(ptr_0
= (t_zencod_test
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_0
))
565 (t_zencod_md5_init
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_md5_1
))
567 (t_zencod_md5_update
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_md5_2
))
569 (t_zencod_md5_do_final
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_md5_3
))
571 (t_zencod_sha1_init
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_sha1_1
))
573 (t_zencod_sha1_update
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_sha1_2
))
575 (t_zencod_sha1_do_final
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_sha1_3
))
577 (t_zencod_xdes_cipher
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_xdes_1
))
579 (t_zencod_rc4_cipher
*) DSO_bind_func(zencod_dso
, ZENCOD_Fct_rc4_1
))) {
581 ZENCODerr(ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_DSO_FAILURE
);
586 * The function from "cryptozen" Library have been correctly loaded so
589 ptr_zencod_test
= ptr_0
;
590 ptr_zencod_bytes2bits
= ptr_1
;
591 ptr_zencod_bits2bytes
= ptr_2
;
592 ptr_zencod_new_number
= ptr_3
;
593 ptr_zencod_init_number
= ptr_4
;
594 ptr_zencod_rsa_mod_exp
= ptr_exp_1
;
595 ptr_zencod_rsa_mod_exp_crt
= ptr_exp_2
;
596 ptr_zencod_dsa_do_sign
= ptr_dsa_1
;
597 ptr_zencod_dsa_do_verify
= ptr_dsa_2
;
598 ptr_zencod_dh_generate_key
= ptr_dh_1
;
599 ptr_zencod_dh_compute_key
= ptr_dh_2
;
600 ptr_zencod_rand_bytes
= ptr_rand_1
;
601 ptr_zencod_math_mod_exp
= ptr_math_1
;
602 ptr_zencod_test
= ptr_0
;
603 ptr_zencod_md5_init
= ptr_md5_1
;
604 ptr_zencod_md5_update
= ptr_md5_2
;
605 ptr_zencod_md5_do_final
= ptr_md5_3
;
606 ptr_zencod_sha1_init
= ptr_sha1_1
;
607 ptr_zencod_sha1_update
= ptr_sha1_2
;
608 ptr_zencod_sha1_do_final
= ptr_sha1_3
;
609 ptr_zencod_xdes_cipher
= ptr_xdes_1
;
610 ptr_zencod_rc4_cipher
= ptr_rc4_1
;
613 * We should peform a test to see if there is actually any unit runnig on
614 * the system ... Even if the cryptozen library is loaded the module coul
615 * not be loaded on the system ... For now we may just open and close the
619 if (ptr_zencod_test() != 0) {
620 ZENCODerr(ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_UNIT_FAILURE
);
626 DSO_free(zencod_dso
);
628 ptr_zencod_bytes2bits
= NULL
;
629 ptr_zencod_bits2bytes
= NULL
;
630 ptr_zencod_new_number
= NULL
;
631 ptr_zencod_init_number
= NULL
;
632 ptr_zencod_rsa_mod_exp
= NULL
;
633 ptr_zencod_rsa_mod_exp_crt
= NULL
;
634 ptr_zencod_dsa_do_sign
= NULL
;
635 ptr_zencod_dsa_do_verify
= NULL
;
636 ptr_zencod_dh_generate_key
= NULL
;
637 ptr_zencod_dh_compute_key
= NULL
;
638 ptr_zencod_rand_bytes
= NULL
;
639 ptr_zencod_math_mod_exp
= NULL
;
640 ptr_zencod_test
= NULL
;
641 ptr_zencod_md5_init
= NULL
;
642 ptr_zencod_md5_update
= NULL
;
643 ptr_zencod_md5_do_final
= NULL
;
644 ptr_zencod_sha1_init
= NULL
;
645 ptr_zencod_sha1_update
= NULL
;
646 ptr_zencod_sha1_do_final
= NULL
;
647 ptr_zencod_xdes_cipher
= NULL
;
648 ptr_zencod_rc4_cipher
= NULL
;
653 static int zencod_finish(ENGINE
*e
)
659 * We Should add some tests for non NULL parameters or bad value !!
660 * Stuff to be done ...
662 if (zencod_dso
== NULL
) {
663 ZENCODerr(ZENCOD_F_ZENCOD_FINISH
, ZENCOD_R_NOT_LOADED
);
666 if (!DSO_free(zencod_dso
)) {
667 ZENCODerr(ZENCOD_F_ZENCOD_FINISH
, ZENCOD_R_DSO_FAILURE
);
673 ptr_zencod_bytes2bits
= NULL
;
674 ptr_zencod_bits2bytes
= NULL
;
675 ptr_zencod_new_number
= NULL
;
676 ptr_zencod_init_number
= NULL
;
677 ptr_zencod_rsa_mod_exp
= NULL
;
678 ptr_zencod_rsa_mod_exp_crt
= NULL
;
679 ptr_zencod_dsa_do_sign
= NULL
;
680 ptr_zencod_dsa_do_verify
= NULL
;
681 ptr_zencod_dh_generate_key
= NULL
;
682 ptr_zencod_dh_compute_key
= NULL
;
683 ptr_zencod_rand_bytes
= NULL
;
684 ptr_zencod_math_mod_exp
= NULL
;
685 ptr_zencod_test
= NULL
;
686 ptr_zencod_md5_init
= NULL
;
687 ptr_zencod_md5_update
= NULL
;
688 ptr_zencod_md5_do_final
= NULL
;
689 ptr_zencod_sha1_init
= NULL
;
690 ptr_zencod_sha1_update
= NULL
;
691 ptr_zencod_sha1_do_final
= NULL
;
692 ptr_zencod_xdes_cipher
= NULL
;
693 ptr_zencod_rc4_cipher
= NULL
;
698 static int zencod_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) ())
701 int initialised
= ((zencod_dso
== NULL
) ? 0 : 1);
706 * We Should add some tests for non NULL parameters or bad value !!
707 * Stuff to be done ...
710 case ZENCOD_CMD_SO_PATH
:
712 ZENCODerr(ZENCOD_F_ZENCOD_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
716 ZENCODerr(ZENCOD_F_ZENCOD_CTRL
, ZENCOD_R_ALREADY_LOADED
);
719 ZENCOD_LIBNAME
= (const char *)p
;
725 ZENCODerr(ZENCOD_F_ZENCOD_CTRL
, ZENCOD_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
731 * BIGNUM stuff Functions
733 static int zencod_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
734 const BIGNUM
*m
, BN_CTX
*ctx
)
742 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP
, ZENCOD_R_NOT_LOADED
);
746 if (!bn_wexpand(r
, m
->top
+ 1)) {
747 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP
, ZENCOD_R_BN_EXPAND_FAIL
);
751 memset(r
->d
, 0, BN_num_bytes(m
));
753 ptr_zencod_init_number(&y
, (r
->dmax
- 1) * sizeof(BN_ULONG
) * 8,
754 (unsigned char *)r
->d
);
759 /* Must invert x and e parameter due to BN mod exp prototype ... */
760 ret
= ptr_zencod_math_mod_exp(&y
, &e
, &x
, &n
);
763 PERROR("zenbridge_math_mod_exp");
764 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP
, ZENCOD_R_REQUEST_FAILED
);
768 r
->top
= (BN_num_bits(m
) + BN_BITS2
- 1) / BN_BITS2
;
774 * RSA stuff Functions
776 # ifndef OPENSSL_NO_RSA
777 static int RSA_zencod_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
)
783 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
, ZENCOD_R_NOT_LOADED
);
787 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
788 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
,
789 ZENCOD_R_BAD_KEY_COMPONENTS
);
793 /* Do in software if argument is too large for hardware */
794 if (RSA_size(rsa
) * 8 > ZENBRIDGE_MAX_KEYSIZE_RSA_CRT
) {
795 const RSA_METHOD
*meth
;
797 meth
= RSA_PKCS1_SSLeay();
798 return meth
->rsa_mod_exp(r0
, i
, rsa
);
800 zen_nb_t y
, x
, p
, q
, dmp1
, dmq1
, iqmp
;
802 if (!bn_expand(r0
, RSA_size(rsa
) * 8)) {
803 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
,
804 ZENCOD_R_BN_EXPAND_FAIL
);
807 r0
->top
= (RSA_size(rsa
) * 8 + BN_BITS2
- 1) / BN_BITS2
;
811 BIGNUM2ZEN(&p
, rsa
->p
);
812 BIGNUM2ZEN(&q
, rsa
->q
);
813 BIGNUM2ZEN(&dmp1
, rsa
->dmp1
);
814 BIGNUM2ZEN(&dmq1
, rsa
->dmq1
);
815 BIGNUM2ZEN(&iqmp
, rsa
->iqmp
);
817 if (ptr_zencod_rsa_mod_exp_crt(&y
, &x
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
) <
819 PERROR("zenbridge_rsa_mod_exp_crt");
820 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
,
821 ZENCOD_R_REQUEST_FAILED
);
830 * This function is aliased to RSA_mod_exp (with the mont stuff dropped).
832 static int RSA_zencod_bn_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
833 const BIGNUM
*m
, BN_CTX
*ctx
,
840 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP
, ZENCOD_R_NOT_LOADED
);
844 /* Do in software if argument is too large for hardware */
845 if (BN_num_bits(m
) > ZENBRIDGE_MAX_KEYSIZE_RSA
) {
846 const RSA_METHOD
*meth
;
848 meth
= RSA_PKCS1_SSLeay();
849 return meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
);
853 if (!bn_expand(r
, BN_num_bits(m
))) {
854 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP
, ZENCOD_R_BN_EXPAND_FAIL
);
857 r
->top
= (BN_num_bits(m
) + BN_BITS2
- 1) / BN_BITS2
;
864 if (ptr_zencod_rsa_mod_exp(&y
, &x
, &n
, &e
) < 0) {
865 PERROR("zenbridge_rsa_mod_exp");
866 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP
, ZENCOD_R_REQUEST_FAILED
);
873 # endif /* !OPENSSL_NO_RSA */
875 # ifndef OPENSSL_NO_DSA
877 * DSA stuff Functions
879 static DSA_SIG
*DSA_zencod_do_sign(const unsigned char *dgst
, int dlen
,
882 zen_nb_t p
, q
, g
, x
, y
, r
, s
, data
;
891 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_NOT_LOADED
);
896 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
900 /* Do in software if argument is too large for hardware */
901 if (BN_num_bits(dsa
->p
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
||
902 BN_num_bits(dsa
->g
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
) {
903 const DSA_METHOD
*meth
;
904 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BAD_KEY_COMPONENTS
);
905 meth
= DSA_OpenSSL();
906 return meth
->dsa_do_sign(dgst
, dlen
, dsa
);
909 if (!(bn_s
= BN_new()) || !(bn_r
= BN_new())) {
910 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BAD_KEY_COMPONENTS
);
914 if (!bn_expand(bn_r
, 160) || !bn_expand(bn_s
, 160)) {
915 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BN_EXPAND_FAIL
);
919 bn_r
->top
= bn_s
->top
= (160 + BN_BITS2
- 1) / BN_BITS2
;
920 BIGNUM2ZEN(&p
, dsa
->p
);
921 BIGNUM2ZEN(&q
, dsa
->q
);
922 BIGNUM2ZEN(&g
, dsa
->g
);
923 BIGNUM2ZEN(&x
, dsa
->priv_key
);
924 BIGNUM2ZEN(&y
, dsa
->pub_key
);
925 BIGNUM2ZEN(&r
, bn_r
);
926 BIGNUM2ZEN(&s
, bn_s
);
929 ypcmem(msg
, dgst
, 20);
930 ptr_zencod_init_number(&data
, 160, msg
);
932 if (ptr_zencod_dsa_do_sign(0, &data
, &y
, &p
, &q
, &g
, &x
, &r
, &s
) < 0) {
933 PERROR("zenbridge_dsa_do_sign");
934 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
938 if (!(sig
= DSA_SIG_new())) {
939 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
952 static int DSA_zencod_do_verify(const unsigned char *dgst
, int dlen
,
953 DSA_SIG
*sig
, DSA
*dsa
)
955 zen_nb_t data
, p
, q
, g
, y
, r
, s
, v
;
963 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY
, ZENCOD_R_NOT_LOADED
);
968 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
972 /* Do in software if argument is too large for hardware */
973 if (BN_num_bits(dsa
->p
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
||
974 BN_num_bits(dsa
->g
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
) {
975 const DSA_METHOD
*meth
;
976 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BAD_KEY_COMPONENTS
);
977 meth
= DSA_OpenSSL();
978 return meth
->dsa_do_verify(dgst
, dlen
, sig
, dsa
);
981 BIGNUM2ZEN(&p
, dsa
->p
);
982 BIGNUM2ZEN(&q
, dsa
->q
);
983 BIGNUM2ZEN(&g
, dsa
->g
);
984 BIGNUM2ZEN(&y
, dsa
->pub_key
);
985 BIGNUM2ZEN(&r
, sig
->r
);
986 BIGNUM2ZEN(&s
, sig
->s
);
987 ptr_zencod_init_number(&v
, 160, v_data
);
988 ypcmem(msg
, dgst
, 20);
989 ptr_zencod_init_number(&data
, 160, msg
);
992 ptr_zencod_dsa_do_verify(0, &data
, &p
, &q
, &g
, &y
, &r
, &s
,
994 PERROR("zenbridge_dsa_do_verify");
995 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY
, ZENCOD_R_REQUEST_FAILED
);
999 return ((ret
== 0) ? 1 : ret
);
1002 static int DSA_zencod_bn_mod_exp(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
1003 const BIGNUM
*p
, const BIGNUM
*m
,
1004 BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
1008 return zencod_bn_mod_exp(r
, a
, p
, m
, ctx
);
1010 # endif /* !OPENSSL_NO_DSA */
1012 # ifndef OPENSSl_NO_DH
1014 * DH stuff Functions
1016 static int DH_zencod_generate_key(DH
*dh
)
1018 BIGNUM
*bn_prv
= NULL
;
1019 BIGNUM
*bn_pub
= NULL
;
1020 zen_nb_t y
, x
, g
, p
;
1026 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_NOT_LOADED
);
1032 bn_prv
= dh
->priv_key
;
1035 if (!(bn_prv
= BN_new())) {
1036 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_BN_EXPAND_FAIL
);
1044 bn_pub
= dh
->pub_key
;
1045 else if (!(bn_pub
= BN_new())) {
1046 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_BN_EXPAND_FAIL
);
1051 if (!bn_wexpand(bn_prv
, dh
->p
->dmax
) || !bn_wexpand(bn_pub
, dh
->p
->dmax
)) {
1052 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_BN_EXPAND_FAIL
);
1055 bn_prv
->top
= dh
->p
->top
;
1056 bn_pub
->top
= dh
->p
->top
;
1058 /* Convert all keys */
1059 BIGNUM2ZEN(&p
, dh
->p
);
1060 BIGNUM2ZEN(&g
, dh
->g
);
1061 BIGNUM2ZEN(&y
, bn_pub
);
1062 BIGNUM2ZEN(&x
, bn_prv
);
1063 x
.len
= DH_size(dh
) * 8;
1065 /* Adjust the lengths of P and G */
1066 p
.len
= ptr_zencod_bytes2bits(p
.data
, ZEN_BYTES(p
.len
));
1067 g
.len
= ptr_zencod_bytes2bits(g
.data
, ZEN_BYTES(g
.len
));
1069 /* Send the request to the driver */
1070 if (ptr_zencod_dh_generate_key(&y
, &x
, &g
, &p
, generate_x
) < 0) {
1071 perror("zenbridge_dh_generate_key");
1072 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_REQUEST_FAILED
);
1076 dh
->priv_key
= bn_prv
;
1077 dh
->pub_key
= bn_pub
;
1090 static int DH_zencod_compute_key(unsigned char *key
, const BIGNUM
*pub_key
,
1093 zen_nb_t y
, x
, p
, k
;
1098 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE
, ZENCOD_R_NOT_LOADED
);
1102 if (!dh
->priv_key
) {
1103 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE
, ZENCOD_R_BAD_KEY_COMPONENTS
);
1107 /* Convert all keys */
1108 BIGNUM2ZEN(&y
, pub_key
);
1109 BIGNUM2ZEN(&x
, dh
->priv_key
);
1110 BIGNUM2ZEN(&p
, dh
->p
);
1111 ptr_zencod_init_number(&k
, p
.len
, key
);
1113 /* Adjust the lengths */
1114 p
.len
= ptr_zencod_bytes2bits(p
.data
, ZEN_BYTES(p
.len
));
1115 y
.len
= ptr_zencod_bytes2bits(y
.data
, ZEN_BYTES(y
.len
));
1116 x
.len
= ptr_zencod_bytes2bits(x
.data
, ZEN_BYTES(x
.len
));
1118 /* Call the hardware */
1119 if (ptr_zencod_dh_compute_key(&k
, &y
, &x
, &p
) < 0) {
1120 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE
, ZENCOD_R_REQUEST_FAILED
);
1124 /* The key must be written MSB -> LSB */
1125 k
.len
= ptr_zencod_bytes2bits(k
.data
, ZEN_BYTES(k
.len
));
1126 esrever(key
, ZEN_BYTES(k
.len
));
1128 return ZEN_BYTES(k
.len
);
1131 static int DH_zencod_bn_mod_exp(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
1132 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
1137 return zencod_bn_mod_exp(r
, a
, p
, m
, ctx
);
1139 # endif /* !OPENSSL_NO_DH */
1142 * RAND stuff Functions
1144 static void RAND_zencod_seed(const void *buf
, int num
)
1147 * Nothing to do cause our crypto accelerator provide a true random
1152 static int RAND_zencod_rand_bytes(unsigned char *buf
, int num
)
1159 ENGINEerr(ZENCOD_F_ZENCOD_RAND
, ZENCOD_R_NOT_LOADED
);
1163 ptr_zencod_init_number(&r
, num
* 8, buf
);
1165 if (ptr_zencod_rand_bytes(&r
, ZENBRIDGE_RNG_DIRECT
) < 0) {
1166 PERROR("zenbridge_rand_bytes");
1167 ENGINEerr(ZENCOD_F_ZENCOD_RAND
, ZENCOD_R_REQUEST_FAILED
);
1174 static int RAND_zencod_rand_status(void)
1182 * This stuff is needed if this ENGINE is being compiled into a
1183 * self-contained shared-library.
1185 # ifdef ENGINE_DYNAMIC_SUPPORT
1186 static int bind_fn(ENGINE
*e
, const char *id
)
1189 if (id
&& (strcmp(id
, engine_zencod_id
) != 0)) {
1192 if (!bind_helper(e
)) {
1199 IMPLEMENT_DYNAMIC_CHECK_FN()
1200 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
1201 # endif /* ENGINE_DYNAMIC_SUPPORT */
1203 * Adding "Digest" and "Cipher" tools ...
1204 * This is in development ... ;-)
1205 * In orfer to code this, i refer to hw_openbsd_dev_crypto and openssl engine made by Geoff Thorpe (if i'm rigth),
1206 * and evp, sha md5 definitions etc ...
1208 /* First add some include ... */
1209 # include <openssl/evp.h>
1210 # include <openssl/sha.h>
1211 # include <openssl/md5.h>
1212 # include <openssl/rc4.h>
1213 # include <openssl/des.h>
1214 /* Some variables declaration ... */
1216 * DONS: Disable symetric computation except DES and 3DES, but let part
1219 /* static int engine_digest_nids [ ] = { NID_sha1, NID_md5 } ; */
1220 static int engine_digest_nids
[] = { };
1222 static int engine_digest_nids_num
= 0;
1224 * static int engine_cipher_nids [ ] = { NID_rc4, NID_rc4_40, NID_des_cbc,
1225 * NID_des_ede3_cbc } ;
1227 static int engine_cipher_nids
[] = { NID_des_cbc
, NID_des_ede3_cbc
};
1229 static int engine_cipher_nids_num
= 2;
1231 /* Function prototype ... */
1233 static int engine_sha1_init(EVP_MD_CTX
*ctx
);
1234 static int engine_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
1235 unsigned long count
);
1236 static int engine_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
1239 static int engine_md5_init(EVP_MD_CTX
*ctx
);
1240 static int engine_md5_update(EVP_MD_CTX
*ctx
, const void *data
,
1241 unsigned long count
);
1242 static int engine_md5_final(EVP_MD_CTX
*ctx
, unsigned char *md
);
1244 static int engine_md_cleanup(EVP_MD_CTX
*ctx
);
1245 static int engine_md_copy(EVP_MD_CTX
*to
, const EVP_MD_CTX
*from
);
1248 static int engine_rc4_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1249 const unsigned char *iv
, int enc
);
1250 static int engine_rc4_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1251 const unsigned char *in
, unsigned int inl
);
1254 static int engine_des_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1255 const unsigned char *iv
, int enc
);
1256 static int engine_des_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1257 const unsigned char *in
, unsigned int inl
);
1260 static int engine_des_ede3_init_key(EVP_CIPHER_CTX
*ctx
,
1261 const unsigned char *key
,
1262 const unsigned char *iv
, int enc
);
1263 static int engine_des_ede3_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1264 const unsigned char *in
,
1267 static int engine_cipher_cleanup(EVP_CIPHER_CTX
*ctx
); /* cleanup ctx */
1269 /* The one for SHA ... */
1270 static const EVP_MD engine_sha1_md
= {
1272 NID_sha1WithRSAEncryption
,
1274 EVP_MD_FLAG_ONESHOT
,
1278 * EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block *
1279 * XXX: set according to device info ...
1284 engine_md_copy
, /* dev_crypto_sha_copy */
1285 engine_md_cleanup
, /* dev_crypto_sha_cleanup */
1286 EVP_PKEY_RSA_method
,
1288 /* sizeof ( EVP_MD * ) + sizeof ( SHA_CTX ) */
1291 * sizeof ( MD_CTX_DATA ) The message digest data structure ...
1295 /* The one for MD5 ... */
1296 static const EVP_MD engine_md5_md
= {
1298 NID_md5WithRSAEncryption
,
1300 EVP_MD_FLAG_ONESHOT
,
1304 * EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block *
1305 * XXX: set according to device info ...
1310 engine_md_copy
, /* dev_crypto_md5_copy */
1311 engine_md_cleanup
, /* dev_crypto_md5_cleanup */
1312 EVP_PKEY_RSA_method
,
1314 /* sizeof ( EVP_MD * ) + sizeof ( MD5_CTX ) */
1317 * sizeof ( MD_CTX_DATA ) The message digest data structure ...
1321 /* The one for RC4 ... */
1322 # define EVP_RC4_KEY_SIZE 16
1324 /* Try something static ... */
1328 unsigned char rc4_state
[260];
1331 # define rc4_data(ctx) ( (EVP_RC4_KEY *) ( ctx )->cipher_data )
1333 static const EVP_CIPHER engine_rc4
= {
1336 16, /* EVP_RC4_KEY_SIZE should be 128 bits */
1337 0, /* FIXME: key should be up to 256 bytes */
1338 EVP_CIPH_VARIABLE_LENGTH
,
1339 engine_rc4_init_key
,
1341 engine_cipher_cleanup
,
1342 sizeof(NEW_ZEN_RC4_KEY
),
1348 /* The one for RC4_40 ... */
1349 static const EVP_CIPHER engine_rc4_40
= {
1354 EVP_CIPH_VARIABLE_LENGTH
,
1355 engine_rc4_init_key
,
1357 engine_cipher_cleanup
,
1358 sizeof(NEW_ZEN_RC4_KEY
),
1364 /* The one for DES ... */
1366 /* Try something static ... */
1368 unsigned char des_key
[24];
1369 unsigned char des_iv
[8];
1372 static const EVP_CIPHER engine_des_cbc
= {
1375 0 | EVP_CIPH_CBC_MODE
,
1376 engine_des_init_key
,
1377 engine_des_cbc_cipher
,
1378 engine_cipher_cleanup
,
1379 sizeof(ZEN_DES_KEY
),
1380 EVP_CIPHER_set_asn1_iv
,
1381 EVP_CIPHER_get_asn1_iv
,
1386 /* The one for 3DES ... */
1388 /* Try something static ... */
1390 unsigned char des3_key
[24];
1391 unsigned char des3_iv
[8];
1394 # define des_data(ctx) ( (DES_EDE_KEY *) ( ctx )->cipher_data )
1396 static const EVP_CIPHER engine_des_ede3_cbc
= {
1399 0 | EVP_CIPH_CBC_MODE
,
1400 engine_des_ede3_init_key
,
1401 engine_des_ede3_cbc_cipher
,
1402 engine_cipher_cleanup
,
1403 sizeof(ZEN_3DES_KEY
),
1404 EVP_CIPHER_set_asn1_iv
,
1405 EVP_CIPHER_get_asn1_iv
,
1410 /* General function cloned on hw_openbsd_dev_crypto one ... */
1411 static int engine_digests(ENGINE
*e
, const EVP_MD
**digest
, const int **nids
,
1415 # ifdef DEBUG_ZENCOD_MD
1416 fprintf(stderr
, "\t=>Function : static int engine_digests () called !\n");
1420 /* We are returning a list of supported nids */
1421 *nids
= engine_digest_nids
;
1422 return engine_digest_nids_num
;
1424 /* We are being asked for a specific digest */
1425 if (nid
== NID_md5
) {
1426 *digest
= &engine_md5_md
;
1427 } else if (nid
== NID_sha1
) {
1428 *digest
= &engine_sha1_md
;
1437 * SHA stuff Functions
1439 static int engine_sha1_init(EVP_MD_CTX
*ctx
)
1444 /* Test with zenbridge library ... */
1445 to_return
= ptr_zencod_sha1_init((ZEN_MD_DATA
*)ctx
->md_data
);
1446 to_return
= !to_return
;
1451 static int engine_sha1_update(EVP_MD_CTX
*ctx
, const void *data
,
1452 unsigned long count
)
1458 /* Convert parameters ... */
1460 input
.data
= (unsigned char *)data
;
1462 /* Test with zenbridge library ... */
1464 ptr_zencod_sha1_update((ZEN_MD_DATA
*)ctx
->md_data
,
1465 (const zen_nb_t
*)&input
);
1466 to_return
= !to_return
;
1471 static int engine_sha1_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
1477 /* Convert parameters ... */
1478 output
.len
= SHA_DIGEST_LENGTH
;
1481 /* Test with zenbridge library ... */
1483 ptr_zencod_sha1_do_final((ZEN_MD_DATA
*)ctx
->md_data
,
1484 (zen_nb_t
*) & output
);
1485 to_return
= !to_return
;
1491 * MD5 stuff Functions
1493 static int engine_md5_init(EVP_MD_CTX
*ctx
)
1498 /* Test with zenbridge library ... */
1499 to_return
= ptr_zencod_md5_init((ZEN_MD_DATA
*)ctx
->md_data
);
1500 to_return
= !to_return
;
1505 static int engine_md5_update(EVP_MD_CTX
*ctx
, const void *data
,
1506 unsigned long count
)
1512 /* Convert parameters ... */
1514 input
.data
= (unsigned char *)data
;
1516 /* Test with zenbridge library ... */
1518 ptr_zencod_md5_update((ZEN_MD_DATA
*)ctx
->md_data
,
1519 (const zen_nb_t
*)&input
);
1520 to_return
= !to_return
;
1525 static int engine_md5_final(EVP_MD_CTX
*ctx
, unsigned char *md
)
1531 /* Convert parameters ... */
1532 output
.len
= MD5_DIGEST_LENGTH
;
1535 /* Test with zenbridge library ... */
1537 ptr_zencod_md5_do_final((ZEN_MD_DATA
*)ctx
->md_data
,
1538 (zen_nb_t
*) & output
);
1539 to_return
= !to_return
;
1544 static int engine_md_cleanup(EVP_MD_CTX
*ctx
)
1547 ZEN_MD_DATA
*zen_md_data
= (ZEN_MD_DATA
*)ctx
->md_data
;
1549 OPENSSL_free(zen_md_data
->HashBuffer
);
1550 zen_md_data
->HashBufferSize
= 0;
1551 ctx
->md_data
= NULL
;
1555 static int engine_md_copy(EVP_MD_CTX
*to
, const EVP_MD_CTX
*from
)
1557 const ZEN_MD_DATA
*from_md
= (ZEN_MD_DATA
*)from
->md_data
;
1558 ZEN_MD_DATA
*to_md
= (ZEN_MD_DATA
*)to
->md_data
;
1560 to_md
->HashBuffer
= OPENSSL_malloc(from_md
->HashBufferSize
);
1561 if (to_md
->HashBuffer
== NULL
)
1563 memcpy(to_md
->HashBuffer
, from_md
->HashBuffer
, from_md
->HashBufferSize
);
1568 /* General function cloned on hw_openbsd_dev_crypto one ... */
1569 static int engine_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
1570 const int **nids
, int nid
)
1574 /* We are returning a list of supported nids */
1575 *nids
= engine_cipher_nids
;
1576 return engine_cipher_nids_num
;
1578 /* We are being asked for a specific cipher */
1579 if (nid
== NID_rc4
) {
1580 *cipher
= &engine_rc4
;
1581 } else if (nid
== NID_rc4_40
) {
1582 *cipher
= &engine_rc4_40
;
1583 } else if (nid
== NID_des_cbc
) {
1584 *cipher
= &engine_des_cbc
;
1585 } else if (nid
== NID_des_ede3_cbc
) {
1586 *cipher
= &engine_des_ede3_cbc
;
1595 static int engine_rc4_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1596 const unsigned char *iv
, int enc
)
1601 NEW_ZEN_RC4_KEY
*tmp_rc4_key
= NULL
;
1603 tmp_rc4_key
= (NEW_ZEN_RC4_KEY
*) (ctx
->cipher_data
);
1604 tmp_rc4_key
->first
= 0;
1605 tmp_rc4_key
->len
= ctx
->key_len
;
1606 tmp_rc4_key
->rc4_state
[0] = 0x00;
1607 tmp_rc4_key
->rc4_state
[2] = 0x00;
1608 nb
= 256 / ctx
->key_len
;
1609 for (i
= 0; i
< nb
; i
++) {
1610 memcpy(&(tmp_rc4_key
->rc4_state
[4 + i
* ctx
->key_len
]), key
,
1619 static int engine_rc4_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1620 const unsigned char *in
, unsigned int in_len
)
1623 zen_nb_t output
, input
;
1626 NEW_ZEN_RC4_KEY
*tmp_rc4_key
= NULL
;
1628 /* Convert parameters ... */
1630 input
.data
= (unsigned char *)in
;
1631 output
.len
= in_len
;
1632 output
.data
= (unsigned char *)out
;
1634 tmp_rc4_key
= ((NEW_ZEN_RC4_KEY
*) (ctx
->cipher_data
));
1636 rc4key
.data
= &(tmp_rc4_key
->rc4_state
[0]);
1638 /* Test with zenbridge library ... */
1640 ptr_zencod_rc4_cipher(&output
, &input
, (const zen_nb_t
*)&rc4key
,
1641 &(tmp_rc4_key
->rc4_state
[0]),
1642 &(tmp_rc4_key
->rc4_state
[3]),
1643 !tmp_rc4_key
->first
);
1644 to_return
= !to_return
;
1646 /* Update encryption state ... */
1647 tmp_rc4_key
->first
= 1;
1653 static int engine_des_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
1654 const unsigned char *iv
, int enc
)
1657 ZEN_DES_KEY
*tmp_des_key
= NULL
;
1660 tmp_des_key
= (ZEN_DES_KEY
*) (ctx
->cipher_data
);
1661 memcpy(&(tmp_des_key
->des_key
[0]), key
, 8);
1662 memcpy(&(tmp_des_key
->des_key
[8]), key
, 8);
1663 memcpy(&(tmp_des_key
->des_key
[16]), key
, 8);
1664 memcpy(&(tmp_des_key
->des_iv
[0]), iv
, 8);
1671 static int engine_des_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1672 const unsigned char *in
, unsigned int inl
)
1675 zen_nb_t output
, input
;
1676 zen_nb_t deskey_1
, deskey_2
, deskey_3
, iv
;
1679 /* Convert parameters ... */
1681 input
.data
= (unsigned char *)in
;
1685 /* Set key parameters ... */
1690 (unsigned char *)((ZEN_DES_KEY
*) (ctx
->cipher_data
))->des_key
;
1692 (unsigned char *)&((ZEN_DES_KEY
*) (ctx
->cipher_data
))->des_key
[8];
1694 (unsigned char *)&((ZEN_DES_KEY
*) (ctx
->cipher_data
))->des_key
[16];
1696 /* Key correct iv ... */
1697 memcpy(((ZEN_DES_KEY
*) (ctx
->cipher_data
))->des_iv
, ctx
->iv
, 8);
1699 iv
.data
= (unsigned char *)((ZEN_DES_KEY
*) (ctx
->cipher_data
))->des_iv
;
1701 if (ctx
->encrypt
== 0) {
1702 memcpy(ctx
->iv
, &(input
.data
[input
.len
- 8]), 8);
1705 /* Test with zenbridge library ... */
1706 to_return
= ptr_zencod_xdes_cipher(&output
, &input
,
1707 (zen_nb_t
*) & deskey_1
,
1708 (zen_nb_t
*) & deskey_2
,
1709 (zen_nb_t
*) & deskey_3
, &iv
,
1711 to_return
= !to_return
;
1714 * But we need to set up the rigth iv ... Test ENCRYPT or DECRYPT mode to
1717 if (ctx
->encrypt
== 1) {
1718 memcpy(ctx
->iv
, &(output
.data
[output
.len
- 8]), 8);
1724 static int engine_des_ede3_init_key(EVP_CIPHER_CTX
*ctx
,
1725 const unsigned char *key
,
1726 const unsigned char *iv
, int enc
)
1729 ZEN_3DES_KEY
*tmp_3des_key
= NULL
;
1732 tmp_3des_key
= (ZEN_3DES_KEY
*) (ctx
->cipher_data
);
1733 memcpy(&(tmp_3des_key
->des3_key
[0]), key
, 24);
1734 memcpy(&(tmp_3des_key
->des3_iv
[0]), iv
, 8);
1741 static int engine_des_ede3_cbc_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
1742 const unsigned char *in
,
1743 unsigned int in_len
)
1746 zen_nb_t output
, input
;
1747 zen_nb_t deskey_1
, deskey_2
, deskey_3
, iv
;
1750 /* Convert parameters ... */
1752 input
.data
= (unsigned char *)in
;
1753 output
.len
= in_len
;
1761 (unsigned char *)((ZEN_3DES_KEY
*) (ctx
->cipher_data
))->des3_key
;
1763 (unsigned char *)&((ZEN_3DES_KEY
*) (ctx
->cipher_data
))->des3_key
[8];
1765 (unsigned char *)&((ZEN_3DES_KEY
*) (ctx
->cipher_data
))->des3_key
[16];
1767 /* Key correct iv ... */
1768 memcpy(((ZEN_3DES_KEY
*) (ctx
->cipher_data
))->des3_iv
, ctx
->iv
, 8);
1770 iv
.data
= (unsigned char *)((ZEN_3DES_KEY
*) (ctx
->cipher_data
))->des3_iv
;
1772 if (ctx
->encrypt
== 0) {
1773 memcpy(ctx
->iv
, &(input
.data
[input
.len
- 8]), 8);
1776 /* Test with zenbridge library ... */
1777 to_return
= ptr_zencod_xdes_cipher(&output
, &input
,
1778 (zen_nb_t
*) & deskey_1
,
1779 (zen_nb_t
*) & deskey_2
,
1780 (zen_nb_t
*) & deskey_3
, &iv
,
1782 to_return
= !to_return
;
1784 if (ctx
->encrypt
== 1) {
1785 memcpy(ctx
->iv
, &(output
.data
[output
.len
- 8]), 8);
1791 static int engine_cipher_cleanup(EVP_CIPHER_CTX
*ctx
)
1794 /* Set the key pointer ... */
1795 if (ctx
->cipher
->nid
== NID_rc4
|| ctx
->cipher
->nid
== NID_rc4_40
) {
1796 } else if (ctx
->cipher
->nid
== NID_des_cbc
) {
1797 } else if (ctx
->cipher
->nid
== NID_des_ede3_cbc
) {
1803 # endif /* !OPENSSL_NO_HW_ZENCOD */
1804 #endif /* !OPENSSL_NO_HW */