1 /* crypto/engine/hw_zencod.c */
2 /* Written by Fred Donnat (frederic.donnat@zencod.com) for "zencod"
3 * engine integration in order to redirect crypto computing on a crypto
4 * hardware accelerator zenssl32 ;-)
7 * Revision : 17 Ju7 2002
8 * Version : zencod_engine-0.9.7
11 /* ====================================================================
12 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
26 * 3. All advertising materials mentioning features or use of this
27 * software must display the following acknowledgment:
28 * "This product includes software developed by the OpenSSL Project
29 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
31 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
32 * endorse or promote products derived from this software without
33 * prior written permission. For written permission, please contact
34 * licensing@OpenSSL.org.
36 * 5. Products derived from this software may not be called "OpenSSL"
37 * nor may "OpenSSL" appear in their names without prior written
38 * permission of the OpenSSL Project.
40 * 6. Redistributions of any form whatsoever must retain the following
42 * "This product includes software developed by the OpenSSL Project
43 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
45 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
46 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
49 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 * ====================================================================
59 * This product includes cryptographic software written by Eric Young
60 * (eay@cryptsoft.com). This product includes software written by Tim
61 * Hudson (tjh@cryptsoft.com).
66 /* ENGINE general include */
68 #include <openssl/crypto.h>
69 #include <openssl/dso.h>
70 #include <openssl/engine.h>
73 #ifndef OPENSSL_NO_HW_ZENCOD
76 # include "hw_zencod.h"
78 # include "vendor_defns/hw_zencod.h"
81 #define ZENCOD_LIB_NAME "zencod engine"
82 #include "hw_zencod_err.c"
84 #define FAIL_TO_SOFTWARE -15
86 #define ZEN_LIBRARY "zenbridge"
89 # define PERROR(s) perror(s)
90 # define CHEESE() fputs("## [ZenEngine] ## " __FUNCTION__ "\n", stderr)
99 static inline void esrever ( unsigned char *d
, int l
)
101 for(;--l
>0;--l
,d
++){*d
^=*(d
+l
);*(d
+l
)^=*d
;*d
^=*(d
+l
);}
104 static inline void ypcmem ( unsigned char *d
, const unsigned char *s
, int l
)
106 for(d
+=l
;l
--;)*--d
=*s
++;
109 static __inline
void esrever ( unsigned char *d
, int l
)
111 for(;--l
>0;--l
,d
++){*d
^=*(d
+l
);*(d
+l
)^=*d
;*d
^=*(d
+l
);}
114 static __inline
void ypcmem ( unsigned char *d
, const unsigned char *s
, int l
)
116 for(d
+=l
;l
--;)*--d
=*s
++;
121 #define BIGNUM2ZEN(n, bn) (ptr_zencod_init_number((n), \
122 (unsigned long) ((bn)->top * BN_BITS2), \
123 (unsigned char *) ((bn)->d)))
125 #define ZEN_BITS(n, bytes) (ptr_zencod_bytes2bits((unsigned char *) (n), (unsigned long) (bytes)))
126 #define ZEN_BYTES(bits) (ptr_zencod_bits2bytes((unsigned long) (bits)))
129 /* Function for ENGINE detection and control */
130 static int zencod_destroy ( ENGINE
*e
) ;
131 static int zencod_init ( ENGINE
*e
) ;
132 static int zencod_finish ( ENGINE
*e
) ;
133 static int zencod_ctrl ( ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) () ) ;
136 static int zencod_bn_mod_exp ( BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
) ;
139 #ifndef OPENSSL_NO_RSA
140 static int RSA_zencod_rsa_mod_exp ( BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
) ;
141 static int RSA_zencod_bn_mod_exp ( BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
142 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
) ;
146 #ifndef OPENSSL_NO_DSA
147 static int DSA_zencod_bn_mod_exp ( DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
148 BN_MONT_CTX
*m_ctx
) ;
150 static DSA_SIG
*DSA_zencod_do_sign ( const unsigned char *dgst
, int dlen
, DSA
*dsa
) ;
151 static int DSA_zencod_do_verify ( const unsigned char *dgst
, int dgst_len
, DSA_SIG
*sig
,
156 #ifndef OPENSSL_NO_DH
157 static int DH_zencod_bn_mod_exp ( const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
158 BN_MONT_CTX
*m_ctx
) ;
159 static int DH_zencod_generate_key ( DH
*dh
) ;
160 static int DH_zencod_compute_key ( unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
) ;
164 static void RAND_zencod_seed ( const void *buf
, int num
) ;
165 static int RAND_zencod_rand_bytes ( unsigned char *buf
, int num
) ;
166 static int RAND_zencod_rand_status ( void ) ;
169 static int engine_digests ( ENGINE
*e
, const EVP_MD
**digest
, const int **nids
, int nid
) ;
172 static int engine_ciphers ( ENGINE
*e
, const EVP_CIPHER
**cipher
, const int **nids
, int nid
) ;
175 #define ZENCOD_CMD_SO_PATH ENGINE_CMD_BASE
176 static const ENGINE_CMD_DEFN zencod_cmd_defns
[ ] =
178 { ZENCOD_CMD_SO_PATH
,
180 "Specifies the path to the 'zenbridge' shared library",
181 ENGINE_CMD_FLAG_STRING
},
186 #ifndef OPENSSL_NO_RSA
187 /* Our internal RSA_METHOD specific to zencod ENGINE providing pointers to our function */
188 static RSA_METHOD zencod_rsa
=
195 RSA_zencod_rsa_mod_exp
,
196 RSA_zencod_bn_mod_exp
,
206 #ifndef OPENSSL_NO_DSA
207 /* Our internal DSA_METHOD specific to zencod ENGINE providing pointers to our function */
208 static DSA_METHOD zencod_dsa
=
213 DSA_zencod_do_verify
,
215 DSA_zencod_bn_mod_exp
,
223 #ifndef OPENSSL_NO_DH
224 /* Our internal DH_METHOD specific to zencod ENGINE providing pointers to our function */
225 static DH_METHOD zencod_dh
=
228 DH_zencod_generate_key
,
229 DH_zencod_compute_key
,
230 DH_zencod_bn_mod_exp
,
238 /* Our internal RAND_meth specific to zencod ZNGINE providing pointers to our function */
239 static RAND_METHOD zencod_rand
=
242 RAND_zencod_rand_bytes
,
245 RAND_zencod_rand_bytes
,
246 RAND_zencod_rand_status
250 /* Constants used when creating the ENGINE */
251 static const char *engine_zencod_id
= "zencod";
252 static const char *engine_zencod_name
= "ZENCOD hardware engine support";
255 /* This internal function is used by ENGINE_zencod () and possibly by the
256 * "dynamic" ENGINE support too ;-)
258 static int bind_helper ( ENGINE
*e
)
261 #ifndef OPENSSL_NO_RSA
262 const RSA_METHOD
*meth_rsa
;
264 #ifndef OPENSSL_NO_DSA
265 const DSA_METHOD
*meth_dsa
;
267 #ifndef OPENSSL_NO_DH
268 const DH_METHOD
*meth_dh
;
271 const RAND_METHOD
*meth_rand
;
274 if ( !ENGINE_set_id ( e
, engine_zencod_id
) ||
275 !ENGINE_set_name ( e
, engine_zencod_name
) ||
276 #ifndef OPENSSL_NO_RSA
277 !ENGINE_set_RSA ( e
, &zencod_rsa
) ||
279 #ifndef OPENSSL_NO_DSA
280 !ENGINE_set_DSA ( e
, &zencod_dsa
) ||
282 #ifndef OPENSSL_NO_DH
283 !ENGINE_set_DH ( e
, &zencod_dh
) ||
285 !ENGINE_set_RAND ( e
, &zencod_rand
) ||
287 !ENGINE_set_destroy_function ( e
, zencod_destroy
) ||
288 !ENGINE_set_init_function ( e
, zencod_init
) ||
289 !ENGINE_set_finish_function ( e
, zencod_finish
) ||
290 !ENGINE_set_ctrl_function ( e
, zencod_ctrl
) ||
291 !ENGINE_set_cmd_defns ( e
, zencod_cmd_defns
) ||
292 !ENGINE_set_digests ( e
, engine_digests
) ||
293 !ENGINE_set_ciphers ( e
, engine_ciphers
) ) {
297 #ifndef OPENSSL_NO_RSA
298 /* We know that the "PKCS1_SSLeay()" functions hook properly
299 * to the Zencod-specific mod_exp and mod_exp_crt so we use
300 * those functions. NB: We don't use ENGINE_openssl() or
301 * anything "more generic" because something like the RSAref
302 * code may not hook properly, and if you own one of these
303 * cards then you have the right to do RSA operations on it
306 meth_rsa
= RSA_PKCS1_SSLeay () ;
308 zencod_rsa
.rsa_pub_enc
= meth_rsa
->rsa_pub_enc
;
309 zencod_rsa
.rsa_pub_dec
= meth_rsa
->rsa_pub_dec
;
310 zencod_rsa
.rsa_priv_enc
= meth_rsa
->rsa_priv_enc
;
311 zencod_rsa
.rsa_priv_dec
= meth_rsa
->rsa_priv_dec
;
312 /* meth_rsa->rsa_mod_exp */
313 /* meth_rsa->bn_mod_exp */
314 zencod_rsa
.init
= meth_rsa
->init
;
315 zencod_rsa
.finish
= meth_rsa
->finish
;
318 #ifndef OPENSSL_NO_DSA
319 /* We use OpenSSL meth to supply what we don't provide ;-*)
321 meth_dsa
= DSA_OpenSSL () ;
323 /* meth_dsa->dsa_do_sign */
324 zencod_dsa
.dsa_sign_setup
= meth_dsa
->dsa_sign_setup
;
325 /* meth_dsa->dsa_do_verify */
326 zencod_dsa
.dsa_mod_exp
= meth_dsa
->dsa_mod_exp
;
327 /* zencod_dsa.bn_mod_exp = meth_dsa->bn_mod_exp ; */
328 zencod_dsa
.init
= meth_dsa
->init
;
329 zencod_dsa
.finish
= meth_dsa
->finish
;
332 #ifndef OPENSSL_NO_DH
333 /* We use OpenSSL meth to supply what we don't provide ;-*)
335 meth_dh
= DH_OpenSSL () ;
337 /* zencod_dh.generate_key = meth_dh->generate_key ; */
338 /* zencod_dh.compute_key = meth_dh->compute_key ; */
339 /* zencod_dh.bn_mod_exp = meth_dh->bn_mod_exp ; */
340 zencod_dh
.init
= meth_dh
->init
;
341 zencod_dh
.finish
= meth_dh
->finish
;
345 /* We use OpenSSL (SSLeay) meth to supply what we don't provide ;-*)
347 meth_rand
= RAND_SSLeay () ;
349 /* meth_rand->seed ; */
350 /* zencod_rand.seed = meth_rand->seed ; */
351 /* meth_rand->bytes ; */
352 /* zencod_rand.bytes = meth_rand->bytes ; */
353 zencod_rand
.cleanup
= meth_rand
->cleanup
;
354 zencod_rand
.add
= meth_rand
->add
;
355 /* meth_rand->pseudorand ; */
356 /* zencod_rand.pseudorand = meth_rand->pseudorand ; */
357 /* zencod_rand.status = meth_rand->status ; */
358 /* meth_rand->status ; */
360 /* Ensure the zencod error handling is set up */
361 ERR_load_ZENCOD_strings () ;
366 /* As this is only ever called once, there's no need for locking
367 * (indeed - the lock will already be held by our caller!!!)
369 ENGINE
*ENGINE_zencod ( void )
372 ENGINE
*eng
= ENGINE_new () ;
377 if ( !bind_helper ( eng
) ) {
378 ENGINE_free ( eng
) ;
386 void ENGINE_load_zencod ( void )
388 /* Copied from eng_[openssl|dyn].c */
389 ENGINE
*toadd
= ENGINE_zencod ( ) ;
390 if ( !toadd
) return ;
391 ENGINE_add ( toadd
) ;
392 ENGINE_free ( toadd
) ;
393 ERR_clear_error ( ) ;
397 /* This is a process-global DSO handle used for loading and unloading
398 * the ZENBRIDGE library.
399 * NB: This is only set (or unset) during an * init () or finish () call
400 * (reference counts permitting) and they're * operating with global locks,
401 * so this should be thread-safe * implicitly.
403 static DSO
*zencod_dso
= NULL
;
405 static t_zencod_test
*ptr_zencod_test
= NULL
;
406 static t_zencod_bytes2bits
*ptr_zencod_bytes2bits
= NULL
;
407 static t_zencod_bits2bytes
*ptr_zencod_bits2bytes
= NULL
;
408 static t_zencod_new_number
*ptr_zencod_new_number
= NULL
;
409 static t_zencod_init_number
*ptr_zencod_init_number
= NULL
;
411 static t_zencod_rsa_mod_exp
*ptr_zencod_rsa_mod_exp
= NULL
;
412 static t_zencod_rsa_mod_exp_crt
*ptr_zencod_rsa_mod_exp_crt
= NULL
;
413 static t_zencod_dsa_do_sign
*ptr_zencod_dsa_do_sign
= NULL
;
414 static t_zencod_dsa_do_verify
*ptr_zencod_dsa_do_verify
= NULL
;
415 static t_zencod_dh_generate_key
*ptr_zencod_dh_generate_key
= NULL
;
416 static t_zencod_dh_compute_key
*ptr_zencod_dh_compute_key
= NULL
;
417 static t_zencod_rand_bytes
*ptr_zencod_rand_bytes
= NULL
;
418 static t_zencod_math_mod_exp
*ptr_zencod_math_mod_exp
= NULL
;
420 static t_zencod_md5_init
*ptr_zencod_md5_init
= NULL
;
421 static t_zencod_md5_update
*ptr_zencod_md5_update
= NULL
;
422 static t_zencod_md5_do_final
*ptr_zencod_md5_do_final
= NULL
;
423 static t_zencod_sha1_init
*ptr_zencod_sha1_init
= NULL
;
424 static t_zencod_sha1_update
*ptr_zencod_sha1_update
= NULL
;
425 static t_zencod_sha1_do_final
*ptr_zencod_sha1_do_final
= NULL
;
427 static t_zencod_xdes_cipher
*ptr_zencod_xdes_cipher
= NULL
;
428 static t_zencod_rc4_cipher
*ptr_zencod_rc4_cipher
= NULL
;
430 /* These are the static string constants for the DSO file name and the function
431 * symbol names to bind to.
433 static const char *ZENCOD_LIBNAME
= ZEN_LIBRARY
;
435 static const char *ZENCOD_Fct_0
= "test_device" ;
436 static const char *ZENCOD_Fct_1
= "zenbridge_bytes2bits" ;
437 static const char *ZENCOD_Fct_2
= "zenbridge_bits2bytes" ;
438 static const char *ZENCOD_Fct_3
= "zenbridge_new_number" ;
439 static const char *ZENCOD_Fct_4
= "zenbridge_init_number" ;
441 static const char *ZENCOD_Fct_exp_1
= "zenbridge_rsa_mod_exp" ;
442 static const char *ZENCOD_Fct_exp_2
= "zenbridge_rsa_mod_exp_crt" ;
443 static const char *ZENCOD_Fct_dsa_1
= "zenbridge_dsa_do_sign" ;
444 static const char *ZENCOD_Fct_dsa_2
= "zenbridge_dsa_do_verify" ;
445 static const char *ZENCOD_Fct_dh_1
= "zenbridge_dh_generate_key" ;
446 static const char *ZENCOD_Fct_dh_2
= "zenbridge_dh_compute_key" ;
447 static const char *ZENCOD_Fct_rand_1
= "zenbridge_rand_bytes" ;
448 static const char *ZENCOD_Fct_math_1
= "zenbridge_math_mod_exp" ;
450 static const char *ZENCOD_Fct_md5_1
= "zenbridge_md5_init" ;
451 static const char *ZENCOD_Fct_md5_2
= "zenbridge_md5_update" ;
452 static const char *ZENCOD_Fct_md5_3
= "zenbridge_md5_do_final" ;
453 static const char *ZENCOD_Fct_sha1_1
= "zenbridge_sha1_init" ;
454 static const char *ZENCOD_Fct_sha1_2
= "zenbridge_sha1_update" ;
455 static const char *ZENCOD_Fct_sha1_3
= "zenbridge_sha1_do_final" ;
457 static const char *ZENCOD_Fct_xdes_1
= "zenbridge_xdes_cipher" ;
458 static const char *ZENCOD_Fct_rc4_1
= "zenbridge_rc4_cipher" ;
460 /* Destructor (complements the "ENGINE_zencod ()" constructor)
462 static int zencod_destroy (ENGINE
*e
)
465 ERR_unload_ZENCOD_strings () ;
471 /* (de)initialisation functions. Control Function
473 static int zencod_init ( ENGINE
*e
)
476 t_zencod_test
*ptr_0
;
477 t_zencod_bytes2bits
*ptr_1
;
478 t_zencod_bits2bytes
*ptr_2
;
479 t_zencod_new_number
*ptr_3
;
480 t_zencod_init_number
*ptr_4
;
481 t_zencod_rsa_mod_exp
*ptr_exp_1
;
482 t_zencod_rsa_mod_exp_crt
*ptr_exp_2
;
483 t_zencod_dsa_do_sign
*ptr_dsa_1
;
484 t_zencod_dsa_do_verify
*ptr_dsa_2
;
485 t_zencod_dh_generate_key
*ptr_dh_1
;
486 t_zencod_dh_compute_key
*ptr_dh_2
;
487 t_zencod_rand_bytes
*ptr_rand_1
;
488 t_zencod_math_mod_exp
*ptr_math_1
;
489 t_zencod_md5_init
*ptr_md5_1
;
490 t_zencod_md5_update
*ptr_md5_2
;
491 t_zencod_md5_do_final
*ptr_md5_3
;
492 t_zencod_sha1_init
*ptr_sha1_1
;
493 t_zencod_sha1_update
*ptr_sha1_2
;
494 t_zencod_sha1_do_final
*ptr_sha1_3
;
495 t_zencod_xdes_cipher
*ptr_xdes_1
;
496 t_zencod_rc4_cipher
*ptr_rc4_1
;
501 * We Should add some tests for non NULL parameters or bad value !!
502 * Stuff to be done ...
505 if ( zencod_dso
!= NULL
) {
506 ZENCODerr ( ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_ALREADY_LOADED
) ;
509 /* Trying to load the Library "cryptozen"
511 zencod_dso
= DSO_load ( NULL
, ZENCOD_LIBNAME
, NULL
, 0 ) ;
512 if ( zencod_dso
== NULL
) {
513 ZENCODerr ( ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_DSO_FAILURE
) ;
517 /* Trying to load Function from the Library
519 if ( ! ( ptr_1
= (t_zencod_bytes2bits
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_1
) ) ||
520 ! ( ptr_2
= (t_zencod_bits2bytes
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_2
) ) ||
521 ! ( ptr_3
= (t_zencod_new_number
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_3
) ) ||
522 ! ( ptr_4
= (t_zencod_init_number
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_4
) ) ||
523 ! ( ptr_exp_1
= (t_zencod_rsa_mod_exp
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_exp_1
) ) ||
524 ! ( ptr_exp_2
= (t_zencod_rsa_mod_exp_crt
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_exp_2
) ) ||
525 ! ( ptr_dsa_1
= (t_zencod_dsa_do_sign
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_dsa_1
) ) ||
526 ! ( ptr_dsa_2
= (t_zencod_dsa_do_verify
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_dsa_2
) ) ||
527 ! ( ptr_dh_1
= (t_zencod_dh_generate_key
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_dh_1
) ) ||
528 ! ( ptr_dh_2
= (t_zencod_dh_compute_key
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_dh_2
) ) ||
529 ! ( ptr_rand_1
= (t_zencod_rand_bytes
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_rand_1
) ) ||
530 ! ( ptr_math_1
= (t_zencod_math_mod_exp
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_math_1
) ) ||
531 ! ( ptr_0
= (t_zencod_test
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_0
) ) ||
532 ! ( ptr_md5_1
= (t_zencod_md5_init
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_md5_1
) ) ||
533 ! ( ptr_md5_2
= (t_zencod_md5_update
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_md5_2
) ) ||
534 ! ( ptr_md5_3
= (t_zencod_md5_do_final
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_md5_3
) ) ||
535 ! ( ptr_sha1_1
= (t_zencod_sha1_init
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_sha1_1
) ) ||
536 ! ( ptr_sha1_2
= (t_zencod_sha1_update
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_sha1_2
) ) ||
537 ! ( ptr_sha1_3
= (t_zencod_sha1_do_final
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_sha1_3
) ) ||
538 ! ( ptr_xdes_1
= (t_zencod_xdes_cipher
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_xdes_1
) ) ||
539 ! ( ptr_rc4_1
= (t_zencod_rc4_cipher
*) DSO_bind_func ( zencod_dso
, ZENCOD_Fct_rc4_1
) ) ) {
541 ZENCODerr ( ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_DSO_FAILURE
) ;
545 /* The function from "cryptozen" Library have been correctly loaded so copy them
547 ptr_zencod_test
= ptr_0
;
548 ptr_zencod_bytes2bits
= ptr_1
;
549 ptr_zencod_bits2bytes
= ptr_2
;
550 ptr_zencod_new_number
= ptr_3
;
551 ptr_zencod_init_number
= ptr_4
;
552 ptr_zencod_rsa_mod_exp
= ptr_exp_1
;
553 ptr_zencod_rsa_mod_exp_crt
= ptr_exp_2
;
554 ptr_zencod_dsa_do_sign
= ptr_dsa_1
;
555 ptr_zencod_dsa_do_verify
= ptr_dsa_2
;
556 ptr_zencod_dh_generate_key
= ptr_dh_1
;
557 ptr_zencod_dh_compute_key
= ptr_dh_2
;
558 ptr_zencod_rand_bytes
= ptr_rand_1
;
559 ptr_zencod_math_mod_exp
= ptr_math_1
;
560 ptr_zencod_test
= ptr_0
;
561 ptr_zencod_md5_init
= ptr_md5_1
;
562 ptr_zencod_md5_update
= ptr_md5_2
;
563 ptr_zencod_md5_do_final
= ptr_md5_3
;
564 ptr_zencod_sha1_init
= ptr_sha1_1
;
565 ptr_zencod_sha1_update
= ptr_sha1_2
;
566 ptr_zencod_sha1_do_final
= ptr_sha1_3
;
567 ptr_zencod_xdes_cipher
= ptr_xdes_1
;
568 ptr_zencod_rc4_cipher
= ptr_rc4_1
;
570 /* We should peform a test to see if there is actually any unit runnig on the system ...
571 * Even if the cryptozen library is loaded the module coul not be loaded on the system ...
572 * For now we may just open and close the device !!
575 if ( ptr_zencod_test () != 0 ) {
576 ZENCODerr ( ZENCOD_F_ZENCOD_INIT
, ZENCOD_R_UNIT_FAILURE
) ;
583 DSO_free ( zencod_dso
) ;
586 ptr_zencod_bytes2bits
= NULL
;
587 ptr_zencod_bits2bytes
= NULL
;
588 ptr_zencod_new_number
= NULL
;
589 ptr_zencod_init_number
= NULL
;
590 ptr_zencod_rsa_mod_exp
= NULL
;
591 ptr_zencod_rsa_mod_exp_crt
= NULL
;
592 ptr_zencod_dsa_do_sign
= NULL
;
593 ptr_zencod_dsa_do_verify
= NULL
;
594 ptr_zencod_dh_generate_key
= NULL
;
595 ptr_zencod_dh_compute_key
= NULL
;
596 ptr_zencod_rand_bytes
= NULL
;
597 ptr_zencod_math_mod_exp
= NULL
;
598 ptr_zencod_test
= NULL
;
599 ptr_zencod_md5_init
= NULL
;
600 ptr_zencod_md5_update
= NULL
;
601 ptr_zencod_md5_do_final
= NULL
;
602 ptr_zencod_sha1_init
= NULL
;
603 ptr_zencod_sha1_update
= NULL
;
604 ptr_zencod_sha1_do_final
= NULL
;
605 ptr_zencod_xdes_cipher
= NULL
;
606 ptr_zencod_rc4_cipher
= NULL
;
612 static int zencod_finish ( ENGINE
*e
)
618 * We Should add some tests for non NULL parameters or bad value !!
619 * Stuff to be done ...
621 if ( zencod_dso
== NULL
) {
622 ZENCODerr ( ZENCOD_F_ZENCOD_FINISH
, ZENCOD_R_NOT_LOADED
) ;
625 if ( !DSO_free ( zencod_dso
) ) {
626 ZENCODerr ( ZENCOD_F_ZENCOD_FINISH
, ZENCOD_R_DSO_FAILURE
) ;
632 ptr_zencod_bytes2bits
= NULL
;
633 ptr_zencod_bits2bytes
= NULL
;
634 ptr_zencod_new_number
= NULL
;
635 ptr_zencod_init_number
= NULL
;
636 ptr_zencod_rsa_mod_exp
= NULL
;
637 ptr_zencod_rsa_mod_exp_crt
= NULL
;
638 ptr_zencod_dsa_do_sign
= NULL
;
639 ptr_zencod_dsa_do_verify
= NULL
;
640 ptr_zencod_dh_generate_key
= NULL
;
641 ptr_zencod_dh_compute_key
= NULL
;
642 ptr_zencod_rand_bytes
= NULL
;
643 ptr_zencod_math_mod_exp
= NULL
;
644 ptr_zencod_test
= NULL
;
645 ptr_zencod_md5_init
= NULL
;
646 ptr_zencod_md5_update
= NULL
;
647 ptr_zencod_md5_do_final
= NULL
;
648 ptr_zencod_sha1_init
= NULL
;
649 ptr_zencod_sha1_update
= NULL
;
650 ptr_zencod_sha1_do_final
= NULL
;
651 ptr_zencod_xdes_cipher
= NULL
;
652 ptr_zencod_rc4_cipher
= NULL
;
658 static int zencod_ctrl ( ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) () )
661 int initialised
= ( ( zencod_dso
== NULL
) ? 0 : 1 ) ;
666 * We Should add some tests for non NULL parameters or bad value !!
667 * Stuff to be done ...
670 case ZENCOD_CMD_SO_PATH
:
672 ZENCODerr ( ZENCOD_F_ZENCOD_CTRL
, ERR_R_PASSED_NULL_PARAMETER
) ;
676 ZENCODerr ( ZENCOD_F_ZENCOD_CTRL
, ZENCOD_R_ALREADY_LOADED
) ;
679 ZENCOD_LIBNAME
= (const char *) p
;
685 ZENCODerr ( ZENCOD_F_ZENCOD_CTRL
, ZENCOD_R_CTRL_COMMAND_NOT_IMPLEMENTED
) ;
691 /* BIGNUM stuff Functions
693 static int zencod_bn_mod_exp ( BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
)
701 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP
, ZENCOD_R_NOT_LOADED
);
705 if ( !bn_wexpand(r
, m
->top
+ 1) ) {
706 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP
, ZENCOD_R_BN_EXPAND_FAIL
);
710 memset(r
->d
, 0, BN_num_bytes(m
));
712 ptr_zencod_init_number ( &y
, (r
->dmax
- 1) * sizeof (BN_ULONG
) * 8, (unsigned char *) r
->d
) ;
713 BIGNUM2ZEN ( &x
, a
) ;
714 BIGNUM2ZEN ( &e
, p
) ;
715 BIGNUM2ZEN ( &n
, m
) ;
717 /* Must invert x and e parameter due to BN mod exp prototype ... */
718 ret
= ptr_zencod_math_mod_exp ( &y
, &e
, &x
, &n
) ;
721 PERROR("zenbridge_math_mod_exp");
722 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP
, ZENCOD_R_REQUEST_FAILED
);
726 r
->top
= (BN_num_bits(m
) + BN_BITS2
- 1) / BN_BITS2
;
732 /* RSA stuff Functions
734 #ifndef OPENSSL_NO_RSA
735 static int RSA_zencod_rsa_mod_exp ( BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
)
741 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
, ZENCOD_R_NOT_LOADED
);
745 if ( !rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
746 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
, ZENCOD_R_BAD_KEY_COMPONENTS
);
750 /* Do in software if argument is too large for hardware */
751 if ( RSA_size(rsa
) * 8 > ZENBRIDGE_MAX_KEYSIZE_RSA_CRT
) {
752 const RSA_METHOD
*meth
;
754 meth
= RSA_PKCS1_SSLeay();
755 return meth
->rsa_mod_exp(r0
, i
, rsa
);
757 zen_nb_t y
, x
, p
, q
, dmp1
, dmq1
, iqmp
;
759 if ( !bn_expand(r0
, RSA_size(rsa
) * 8) ) {
760 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
, ZENCOD_R_BN_EXPAND_FAIL
);
763 r0
->top
= (RSA_size(rsa
) * 8 + BN_BITS2
- 1) / BN_BITS2
;
765 BIGNUM2ZEN ( &x
, i
) ;
766 BIGNUM2ZEN ( &y
, r0
) ;
767 BIGNUM2ZEN ( &p
, rsa
->p
) ;
768 BIGNUM2ZEN ( &q
, rsa
->q
) ;
769 BIGNUM2ZEN ( &dmp1
, rsa
->dmp1
) ;
770 BIGNUM2ZEN ( &dmq1
, rsa
->dmq1
) ;
771 BIGNUM2ZEN ( &iqmp
, rsa
->iqmp
) ;
773 if ( ptr_zencod_rsa_mod_exp_crt ( &y
, &x
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
) < 0 ) {
774 PERROR("zenbridge_rsa_mod_exp_crt");
775 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT
, ZENCOD_R_REQUEST_FAILED
);
784 /* This function is aliased to RSA_mod_exp (with the mont stuff dropped).
786 static int RSA_zencod_bn_mod_exp ( BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
787 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
793 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP
, ZENCOD_R_NOT_LOADED
);
797 /* Do in software if argument is too large for hardware */
798 if ( BN_num_bits(m
) > ZENBRIDGE_MAX_KEYSIZE_RSA
) {
799 const RSA_METHOD
*meth
;
801 meth
= RSA_PKCS1_SSLeay();
802 return meth
->bn_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
);
806 if ( !bn_expand(r
, BN_num_bits(m
)) ) {
807 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP
, ZENCOD_R_BN_EXPAND_FAIL
);
810 r
->top
= (BN_num_bits(m
) + BN_BITS2
- 1) / BN_BITS2
;
812 BIGNUM2ZEN ( &x
, a
) ;
813 BIGNUM2ZEN ( &y
, r
) ;
814 BIGNUM2ZEN ( &e
, p
) ;
815 BIGNUM2ZEN ( &n
, m
) ;
817 if ( ptr_zencod_rsa_mod_exp ( &y
, &x
, &n
, &e
) < 0 ) {
818 PERROR("zenbridge_rsa_mod_exp");
819 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP
, ZENCOD_R_REQUEST_FAILED
);
826 #endif /* !OPENSSL_NO_RSA */
829 #ifndef OPENSSL_NO_DSA
830 /* DSA stuff Functions
832 static DSA_SIG
*DSA_zencod_do_sign ( const unsigned char *dgst
, int dlen
, DSA
*dsa
)
834 zen_nb_t p
, q
, g
, x
, y
, r
, s
, data
;
843 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_NOT_LOADED
);
848 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
852 /* Do in software if argument is too large for hardware */
853 if ( BN_num_bits(dsa
->p
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
||
854 BN_num_bits(dsa
->g
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
) {
855 const DSA_METHOD
*meth
;
856 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BAD_KEY_COMPONENTS
);
857 meth
= DSA_OpenSSL();
858 return meth
->dsa_do_sign(dgst
, dlen
, dsa
);
861 if ( !(bn_s
= BN_new()) || !(bn_r
= BN_new()) ) {
862 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BAD_KEY_COMPONENTS
);
866 if ( !bn_expand(bn_r
, 160) || !bn_expand(bn_s
, 160) ) {
867 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BN_EXPAND_FAIL
);
871 bn_r
->top
= bn_s
->top
= (160 + BN_BITS2
- 1) / BN_BITS2
;
872 BIGNUM2ZEN ( &p
, dsa
->p
) ;
873 BIGNUM2ZEN ( &q
, dsa
->q
) ;
874 BIGNUM2ZEN ( &g
, dsa
->g
) ;
875 BIGNUM2ZEN ( &x
, dsa
->priv_key
) ;
876 BIGNUM2ZEN ( &y
, dsa
->pub_key
) ;
877 BIGNUM2ZEN ( &r
, bn_r
) ;
878 BIGNUM2ZEN ( &s
, bn_s
) ;
881 ypcmem(msg
, dgst
, 20);
882 ptr_zencod_init_number ( &data
, 160, msg
) ;
884 if ( ptr_zencod_dsa_do_sign ( 0, &data
, &y
, &p
, &q
, &g
, &x
, &r
, &s
) < 0 ) {
885 PERROR("zenbridge_dsa_do_sign");
886 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
890 if ( !( sig
= DSA_SIG_new () ) ) {
891 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
907 static int DSA_zencod_do_verify ( const unsigned char *dgst
, int dlen
, DSA_SIG
*sig
, DSA
*dsa
)
909 zen_nb_t data
, p
, q
, g
, y
, r
, s
, v
;
917 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY
, ZENCOD_R_NOT_LOADED
);
922 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_REQUEST_FAILED
);
926 /* Do in software if argument is too large for hardware */
927 if ( BN_num_bits(dsa
->p
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
||
928 BN_num_bits(dsa
->g
) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN
) {
929 const DSA_METHOD
*meth
;
930 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN
, ZENCOD_R_BAD_KEY_COMPONENTS
);
931 meth
= DSA_OpenSSL();
932 return meth
->dsa_do_verify(dgst
, dlen
, sig
, dsa
);
935 BIGNUM2ZEN ( &p
, dsa
->p
) ;
936 BIGNUM2ZEN ( &q
, dsa
->q
) ;
937 BIGNUM2ZEN ( &g
, dsa
->g
) ;
938 BIGNUM2ZEN ( &y
, dsa
->pub_key
) ;
939 BIGNUM2ZEN ( &r
, sig
->r
) ;
940 BIGNUM2ZEN ( &s
, sig
->s
) ;
941 ptr_zencod_init_number ( &v
, 160, v_data
) ;
942 ypcmem(msg
, dgst
, 20);
943 ptr_zencod_init_number ( &data
, 160, msg
) ;
945 if ( ( ret
= ptr_zencod_dsa_do_verify ( 0, &data
, &p
, &q
, &g
, &y
, &r
, &s
, &v
) ) < 0 ) {
946 PERROR("zenbridge_dsa_do_verify");
947 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY
, ZENCOD_R_REQUEST_FAILED
);
951 return ( ( ret
== 0 ) ? 1 : ret
) ;
955 static int DSA_zencod_bn_mod_exp ( DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
, const BIGNUM
*p
, const BIGNUM
*m
,
956 BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
960 return zencod_bn_mod_exp ( r
, a
, p
, m
, ctx
) ;
962 #endif /* !OPENSSL_NO_DSA */
965 #ifndef OPENSSl_NO_DH
966 /* DH stuff Functions
968 static int DH_zencod_generate_key ( DH
*dh
)
970 BIGNUM
*bn_prv
= NULL
;
971 BIGNUM
*bn_pub
= NULL
;
978 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_NOT_LOADED
);
983 if ( dh
->priv_key
) {
984 bn_prv
= dh
->priv_key
;
987 if (!(bn_prv
= BN_new())) {
988 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_BN_EXPAND_FAIL
);
996 bn_pub
= dh
->pub_key
;
998 if ( !( bn_pub
= BN_new () ) ) {
999 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_BN_EXPAND_FAIL
);
1004 if ( !bn_wexpand ( bn_prv
, dh
->p
->dmax
) ||
1005 !bn_wexpand ( bn_pub
, dh
->p
->dmax
) ) {
1006 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_BN_EXPAND_FAIL
);
1009 bn_prv
->top
= dh
->p
->top
;
1010 bn_pub
->top
= dh
->p
->top
;
1012 /* Convert all keys */
1013 BIGNUM2ZEN ( &p
, dh
->p
) ;
1014 BIGNUM2ZEN ( &g
, dh
->g
) ;
1015 BIGNUM2ZEN ( &y
, bn_pub
) ;
1016 BIGNUM2ZEN ( &x
, bn_prv
) ;
1017 x
.len
= DH_size(dh
) * 8;
1019 /* Adjust the lengths of P and G */
1020 p
.len
= ptr_zencod_bytes2bits ( p
.data
, ZEN_BYTES ( p
.len
) ) ;
1021 g
.len
= ptr_zencod_bytes2bits ( g
.data
, ZEN_BYTES ( g
.len
) ) ;
1023 /* Send the request to the driver */
1024 if ( ptr_zencod_dh_generate_key ( &y
, &x
, &g
, &p
, generate_x
) < 0 ) {
1025 perror("zenbridge_dh_generate_key");
1026 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE
, ZENCOD_R_REQUEST_FAILED
);
1030 dh
->priv_key
= bn_prv
;
1031 dh
->pub_key
= bn_pub
;
1036 if (!dh
->priv_key
&& bn_prv
)
1038 if (!dh
->pub_key
&& bn_pub
)
1045 static int DH_zencod_compute_key ( unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
1047 zen_nb_t y
, x
, p
, k
;
1051 if ( !zencod_dso
) {
1052 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE
, ZENCOD_R_NOT_LOADED
);
1056 if ( !dh
->priv_key
) {
1057 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE
, ZENCOD_R_BAD_KEY_COMPONENTS
);
1061 /* Convert all keys */
1062 BIGNUM2ZEN ( &y
, pub_key
) ;
1063 BIGNUM2ZEN ( &x
, dh
->priv_key
) ;
1064 BIGNUM2ZEN ( &p
, dh
->p
) ;
1065 ptr_zencod_init_number ( &k
, p
.len
, key
) ;
1067 /* Adjust the lengths */
1068 p
.len
= ptr_zencod_bytes2bits ( p
.data
, ZEN_BYTES ( p
.len
) ) ;
1069 y
.len
= ptr_zencod_bytes2bits ( y
.data
, ZEN_BYTES ( y
.len
) ) ;
1070 x
.len
= ptr_zencod_bytes2bits ( x
.data
, ZEN_BYTES ( x
.len
) ) ;
1072 /* Call the hardware */
1073 if ( ptr_zencod_dh_compute_key ( &k
, &y
, &x
, &p
) < 0 ) {
1074 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE
, ZENCOD_R_REQUEST_FAILED
);
1078 /* The key must be written MSB -> LSB */
1079 k
.len
= ptr_zencod_bytes2bits ( k
.data
, ZEN_BYTES ( k
.len
) ) ;
1080 esrever ( key
, ZEN_BYTES ( k
.len
) ) ;
1082 return ZEN_BYTES ( k
.len
) ;
1086 static int DH_zencod_bn_mod_exp ( const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
1087 BN_MONT_CTX
*m_ctx
)
1091 return zencod_bn_mod_exp ( r
, a
, p
, m
, ctx
) ;
1093 #endif /* !OPENSSL_NO_DH */
1096 /* RAND stuff Functions
1098 static void RAND_zencod_seed ( const void *buf
, int num
)
1100 /* Nothing to do cause our crypto accelerator provide a true random generator */
1104 static int RAND_zencod_rand_bytes ( unsigned char *buf
, int num
)
1110 if ( !zencod_dso
) {
1111 ENGINEerr(ZENCOD_F_ZENCOD_RAND
, ZENCOD_R_NOT_LOADED
);
1115 ptr_zencod_init_number ( &r
, num
* 8, buf
) ;
1117 if ( ptr_zencod_rand_bytes ( &r
, ZENBRIDGE_RNG_DIRECT
) < 0 ) {
1118 PERROR("zenbridge_rand_bytes");
1119 ENGINEerr(ZENCOD_F_ZENCOD_RAND
, ZENCOD_R_REQUEST_FAILED
);
1127 static int RAND_zencod_rand_status ( void )
1135 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1138 #ifdef ENGINE_DYNAMIC_SUPPORT
1139 static int bind_fn ( ENGINE
*e
, const char *id
)
1142 if ( id
&& ( strcmp ( id
, engine_zencod_id
) != 0 ) ) {
1145 if ( !bind_helper ( e
) ) {
1152 IMPLEMENT_DYNAMIC_CHECK_FN ()
1153 IMPLEMENT_DYNAMIC_BIND_FN ( bind_fn
)
1154 #endif /* ENGINE_DYNAMIC_SUPPORT */
1160 * Adding "Digest" and "Cipher" tools ...
1161 * This is in development ... ;-)
1162 * In orfer to code this, i refer to hw_openbsd_dev_crypto and openssl engine made by Geoff Thorpe (if i'm rigth),
1163 * and evp, sha md5 definitions etc ...
1165 /* First add some include ... */
1166 #include <openssl/evp.h>
1167 #include <openssl/sha.h>
1168 #include <openssl/md5.h>
1169 #include <openssl/rc4.h>
1170 #include <openssl/des.h>
1173 /* Some variables declaration ... */
1175 * Disable symetric computation except DES and 3DES, but let part of the code
1177 /* static int engine_digest_nids [ ] = { NID_sha1, NID_md5 } ; */
1178 static int engine_digest_nids
[ ] = { } ;
1179 static int engine_digest_nids_num
= 0 ;
1180 /* static int engine_cipher_nids [ ] = { NID_rc4, NID_rc4_40, NID_des_cbc, NID_des_ede3_cbc } ; */
1181 static int engine_cipher_nids
[ ] = { NID_des_cbc
, NID_des_ede3_cbc
} ;
1182 static int engine_cipher_nids_num
= 2 ;
1185 /* Function prototype ... */
1187 static int engine_sha1_init ( EVP_MD_CTX
*ctx
) ;
1188 static int engine_sha1_update ( EVP_MD_CTX
*ctx
, const void *data
, unsigned long count
) ;
1189 static int engine_sha1_final ( EVP_MD_CTX
*ctx
, unsigned char *md
) ;
1192 static int engine_md5_init ( EVP_MD_CTX
*ctx
) ;
1193 static int engine_md5_update ( EVP_MD_CTX
*ctx
, const void *data
, unsigned long count
) ;
1194 static int engine_md5_final ( EVP_MD_CTX
*ctx
, unsigned char *md
) ;
1196 static int engine_md_cleanup ( EVP_MD_CTX
*ctx
) ;
1197 static int engine_md_copy ( EVP_MD_CTX
*to
, const EVP_MD_CTX
*from
) ;
1201 static int engine_rc4_init_key ( EVP_CIPHER_CTX
*ctx
, const unsigned char *key
, const unsigned char *iv
, int enc
) ;
1202 static int engine_rc4_cipher ( EVP_CIPHER_CTX
*ctx
, unsigned char *out
, const unsigned char *in
, unsigned int inl
) ;
1205 static int engine_des_init_key ( EVP_CIPHER_CTX
*ctx
, const unsigned char *key
, const unsigned char *iv
, int enc
) ;
1206 static int engine_des_cbc_cipher ( EVP_CIPHER_CTX
*ctx
, unsigned char *out
, const unsigned char *in
, unsigned int inl
) ;
1209 static int engine_des_ede3_init_key ( EVP_CIPHER_CTX
*ctx
, const unsigned char *key
, const unsigned char *iv
, int enc
) ;
1210 static int engine_des_ede3_cbc_cipher ( EVP_CIPHER_CTX
*ctx
, unsigned char *out
,const unsigned char *in
, unsigned int inl
) ;
1212 static int engine_cipher_cleanup ( EVP_CIPHER_CTX
*ctx
) ; /* cleanup ctx */
1215 /* The one for SHA ... */
1216 static const EVP_MD engine_sha1_md
=
1219 NID_sha1WithRSAEncryption
,
1221 EVP_MD_FLAG_ONESHOT
,
1222 /* 0, */ /* EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block
1223 * XXX: set according to device info ... */
1227 engine_md_copy
, /* dev_crypto_sha_copy */
1228 engine_md_cleanup
, /* dev_crypto_sha_cleanup */
1229 EVP_PKEY_RSA_method
,
1231 /* sizeof ( EVP_MD * ) + sizeof ( SHA_CTX ) */
1232 sizeof ( ZEN_MD_DATA
)
1233 /* sizeof ( MD_CTX_DATA ) The message digest data stucture ... */
1236 /* The one for MD5 ... */
1237 static const EVP_MD engine_md5_md
=
1240 NID_md5WithRSAEncryption
,
1242 EVP_MD_FLAG_ONESHOT
,
1243 /* 0, */ /* EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block
1244 * XXX: set according to device info ... */
1248 engine_md_copy
, /* dev_crypto_md5_copy */
1249 engine_md_cleanup
, /* dev_crypto_md5_cleanup */
1250 EVP_PKEY_RSA_method
,
1252 /* sizeof ( EVP_MD * ) + sizeof ( MD5_CTX ) */
1253 sizeof ( ZEN_MD_DATA
)
1254 /* sizeof ( MD_CTX_DATA ) The message digest data stucture ... */
1258 /* The one for RC4 ... */
1259 #define EVP_RC4_KEY_SIZE 16
1261 /* Try something static ... */
1265 unsigned int first
;
1266 unsigned char rc4_state
[ 260 ] ;
1269 #define rc4_data(ctx) ( (EVP_RC4_KEY *) ( ctx )->cipher_data )
1271 static const EVP_CIPHER engine_rc4
=
1275 16, /* EVP_RC4_KEY_SIZE should be 128 bits */
1276 0, /* FIXME: key should be up to 256 bytes */
1277 EVP_CIPH_VARIABLE_LENGTH
,
1278 engine_rc4_init_key
,
1280 engine_cipher_cleanup
,
1281 sizeof ( NEW_ZEN_RC4_KEY
),
1287 /* The one for RC4_40 ... */
1288 static const EVP_CIPHER engine_rc4_40
=
1294 EVP_CIPH_VARIABLE_LENGTH
,
1295 engine_rc4_init_key
,
1297 engine_cipher_cleanup
,
1298 sizeof ( NEW_ZEN_RC4_KEY
),
1304 /* The one for DES ... */
1306 /* Try something static ... */
1309 unsigned char des_key
[ 24 ] ;
1310 unsigned char des_iv
[ 8 ] ;
1313 static const EVP_CIPHER engine_des_cbc
=
1317 0 | EVP_CIPH_CBC_MODE
,
1318 engine_des_init_key
,
1319 engine_des_cbc_cipher
,
1320 engine_cipher_cleanup
,
1321 sizeof(ZEN_DES_KEY
),
1322 EVP_CIPHER_set_asn1_iv
,
1323 EVP_CIPHER_get_asn1_iv
,
1328 /* The one for 3DES ... */
1330 /* Try something static ... */
1333 unsigned char des3_key
[ 24 ] ;
1334 unsigned char des3_iv
[ 8 ] ;
1337 #define des_data(ctx) ( (DES_EDE_KEY *) ( ctx )->cipher_data )
1339 static const EVP_CIPHER engine_des_ede3_cbc
=
1343 0 | EVP_CIPH_CBC_MODE
,
1344 engine_des_ede3_init_key
,
1345 engine_des_ede3_cbc_cipher
,
1346 engine_cipher_cleanup
,
1347 sizeof(ZEN_3DES_KEY
),
1348 EVP_CIPHER_set_asn1_iv
,
1349 EVP_CIPHER_get_asn1_iv
,
1355 /* General function cloned on hw_openbsd_dev_crypto one ... */
1356 static int engine_digests ( ENGINE
*e
, const EVP_MD
**digest
, const int **nids
, int nid
)
1359 #ifdef DEBUG_ZENCOD_MD
1360 fprintf ( stderr
, "\t=>Function : static int engine_digests () called !\n" ) ;
1364 /* We are returning a list of supported nids */
1365 *nids
= engine_digest_nids
;
1366 return engine_digest_nids_num
;
1368 /* We are being asked for a specific digest */
1369 if ( nid
== NID_md5
) {
1370 *digest
= &engine_md5_md
;
1372 else if ( nid
== NID_sha1
) {
1373 *digest
= &engine_sha1_md
;
1383 /* SHA stuff Functions
1385 static int engine_sha1_init ( EVP_MD_CTX
*ctx
)
1390 /* Test with zenbridge library ... */
1391 to_return
= ptr_zencod_sha1_init ( (ZEN_MD_DATA
*) ctx
->md_data
) ;
1392 to_return
= !to_return
;
1398 static int engine_sha1_update ( EVP_MD_CTX
*ctx
, const void *data
, unsigned long count
)
1404 /* Convert parameters ... */
1406 input
.data
= (unsigned char *) data
;
1408 /* Test with zenbridge library ... */
1409 to_return
= ptr_zencod_sha1_update ( (ZEN_MD_DATA
*) ctx
->md_data
, (const zen_nb_t
*) &input
) ;
1410 to_return
= !to_return
;
1416 static int engine_sha1_final ( EVP_MD_CTX
*ctx
, unsigned char *md
)
1422 /* Convert parameters ... */
1423 output
.len
= SHA_DIGEST_LENGTH
;
1426 /* Test with zenbridge library ... */
1427 to_return
= ptr_zencod_sha1_do_final ( (ZEN_MD_DATA
*) ctx
->md_data
, (zen_nb_t
*) &output
) ;
1428 to_return
= !to_return
;
1435 /* MD5 stuff Functions
1437 static int engine_md5_init ( EVP_MD_CTX
*ctx
)
1442 /* Test with zenbridge library ... */
1443 to_return
= ptr_zencod_md5_init ( (ZEN_MD_DATA
*) ctx
->md_data
) ;
1444 to_return
= !to_return
;
1450 static int engine_md5_update ( EVP_MD_CTX
*ctx
, const void *data
, unsigned long count
)
1456 /* Convert parameters ... */
1458 input
.data
= (unsigned char *) data
;
1460 /* Test with zenbridge library ... */
1461 to_return
= ptr_zencod_md5_update ( (ZEN_MD_DATA
*) ctx
->md_data
, (const zen_nb_t
*) &input
) ;
1462 to_return
= !to_return
;
1468 static int engine_md5_final ( EVP_MD_CTX
*ctx
, unsigned char *md
)
1474 /* Convert parameters ... */
1475 output
.len
= MD5_DIGEST_LENGTH
;
1478 /* Test with zenbridge library ... */
1479 to_return
= ptr_zencod_md5_do_final ( (ZEN_MD_DATA
*) ctx
->md_data
, (zen_nb_t
*) &output
) ;
1480 to_return
= !to_return
;
1486 static int engine_md_cleanup ( EVP_MD_CTX
*ctx
)
1489 ZEN_MD_DATA
*zen_md_data
= (ZEN_MD_DATA
*) ctx
->md_data
;
1491 if ( zen_md_data
->HashBuffer
!= NULL
) {
1492 OPENSSL_free ( zen_md_data
->HashBuffer
) ;
1493 zen_md_data
->HashBufferSize
= 0 ;
1494 ctx
->md_data
= NULL
;
1501 static int engine_md_copy ( EVP_MD_CTX
*to
, const EVP_MD_CTX
*from
)
1503 const ZEN_MD_DATA
*from_md
= (ZEN_MD_DATA
*) from
->md_data
;
1504 ZEN_MD_DATA
*to_md
= (ZEN_MD_DATA
*) to
->md_data
;
1506 to_md
->HashBuffer
= OPENSSL_malloc ( from_md
->HashBufferSize
) ;
1507 memcpy ( to_md
->HashBuffer
, from_md
->HashBuffer
, from_md
->HashBufferSize
) ;
1513 /* General function cloned on hw_openbsd_dev_crypto one ... */
1514 static int engine_ciphers ( ENGINE
*e
, const EVP_CIPHER
**cipher
, const int **nids
, int nid
)
1518 /* We are returning a list of supported nids */
1519 *nids
= engine_cipher_nids
;
1520 return engine_cipher_nids_num
;
1522 /* We are being asked for a specific cipher */
1523 if ( nid
== NID_rc4
) {
1524 *cipher
= &engine_rc4
;
1526 else if ( nid
== NID_rc4_40
) {
1527 *cipher
= &engine_rc4_40
;
1529 else if ( nid
== NID_des_cbc
) {
1530 *cipher
= &engine_des_cbc
;
1532 else if ( nid
== NID_des_ede3_cbc
) {
1533 *cipher
= &engine_des_ede3_cbc
;
1544 static int engine_rc4_init_key ( EVP_CIPHER_CTX
*ctx
, const unsigned char *key
, const unsigned char *iv
, int enc
)
1549 NEW_ZEN_RC4_KEY
*tmp_rc4_key
= NULL
;
1551 tmp_rc4_key
= (NEW_ZEN_RC4_KEY
*) ( ctx
->cipher_data
) ;
1552 tmp_rc4_key
->first
= 0 ;
1553 tmp_rc4_key
->len
= ctx
->key_len
;
1554 tmp_rc4_key
->rc4_state
[ 0 ] = 0x00 ;
1555 tmp_rc4_key
->rc4_state
[ 2 ] = 0x00 ;
1556 nb
= 256 / ctx
->key_len
;
1557 for ( i
= 0; i
< nb
; i
++ ) {
1558 memcpy ( &( tmp_rc4_key
->rc4_state
[ 4 + i
*ctx
->key_len
] ), key
, ctx
->key_len
) ;
1567 static int engine_rc4_cipher ( EVP_CIPHER_CTX
*ctx
, unsigned char *out
, const unsigned char *in
, unsigned int in_len
)
1570 zen_nb_t output
, input
;
1573 NEW_ZEN_RC4_KEY
*tmp_rc4_key
= NULL
;
1575 /* Convert parameters ... */
1576 input
.len
= in_len
;
1577 input
.data
= (unsigned char *) in
;
1578 output
.len
= in_len
;
1579 output
.data
= (unsigned char *) out
;
1581 tmp_rc4_key
= ( (NEW_ZEN_RC4_KEY
*) ( ctx
->cipher_data
) ) ;
1583 rc4key
.data
= &( tmp_rc4_key
->rc4_state
[ 0 ] ) ;
1585 /* Test with zenbridge library ... */
1586 to_return
= ptr_zencod_rc4_cipher ( &output
, &input
, (const zen_nb_t
*) &rc4key
, &( tmp_rc4_key
->rc4_state
[0] ), &( tmp_rc4_key
->rc4_state
[3] ), !tmp_rc4_key
->first
) ;
1587 to_return
= !to_return
;
1589 /* Update encryption state ... */
1590 tmp_rc4_key
->first
= 1 ;
1591 tmp_rc4_key
= NULL
;
1597 static int engine_des_init_key ( EVP_CIPHER_CTX
*ctx
, const unsigned char *key
, const unsigned char *iv
, int enc
)
1600 ZEN_DES_KEY
*tmp_des_key
= NULL
;
1603 tmp_des_key
= (ZEN_DES_KEY
*) ( ctx
->cipher_data
) ;
1604 memcpy ( &( tmp_des_key
->des_key
[ 0 ] ), key
, 8 ) ;
1605 memcpy ( &( tmp_des_key
->des_key
[ 8 ] ), key
, 8 ) ;
1606 memcpy ( &( tmp_des_key
->des_key
[ 16 ] ), key
, 8 ) ;
1607 memcpy ( &( tmp_des_key
->des_iv
[ 0 ] ), iv
, 8 ) ;
1615 static int engine_des_cbc_cipher ( EVP_CIPHER_CTX
*ctx
, unsigned char *out
, const unsigned char *in
, unsigned int inl
)
1618 zen_nb_t output
, input
;
1619 zen_nb_t deskey_1
, deskey_2
, deskey_3
, iv
;
1622 /* Convert parameters ... */
1624 input
.data
= (unsigned char *) in
;
1628 /* Set key parameters ... */
1632 deskey_1
.data
= (unsigned char *) ( (ZEN_DES_KEY
*) ( ctx
->cipher_data
) )->des_key
;
1633 deskey_2
.data
= (unsigned char *) &( (ZEN_DES_KEY
*) ( ctx
->cipher_data
) )->des_key
[ 8 ] ;
1634 deskey_3
.data
= (unsigned char *) &( (ZEN_DES_KEY
*) ( ctx
->cipher_data
) )->des_key
[ 16 ] ;
1636 /* Key correct iv ... */
1637 memcpy ( ( (ZEN_DES_KEY
*) ( ctx
->cipher_data
) )->des_iv
, ctx
->iv
, 8 ) ;
1639 iv
.data
= (unsigned char *) ( (ZEN_DES_KEY
*) ( ctx
->cipher_data
) )->des_iv
;
1641 if ( ctx
->encrypt
== 0 ) {
1642 memcpy ( ctx
->iv
, &( input
.data
[ input
.len
- 8 ] ), 8 ) ;
1645 /* Test with zenbridge library ... */
1646 to_return
= ptr_zencod_xdes_cipher ( &output
, &input
,
1647 (zen_nb_t
*) &deskey_1
, (zen_nb_t
*) &deskey_2
, (zen_nb_t
*) &deskey_3
, &iv
, ctx
->encrypt
) ;
1648 to_return
= !to_return
;
1650 /* But we need to set up the rigth iv ...
1651 * Test ENCRYPT or DECRYPT mode to set iv ... */
1652 if ( ctx
->encrypt
== 1 ) {
1653 memcpy ( ctx
->iv
, &( output
.data
[ output
.len
- 8 ] ), 8 ) ;
1660 static int engine_des_ede3_init_key ( EVP_CIPHER_CTX
*ctx
, const unsigned char *key
, const unsigned char *iv
, int enc
)
1663 ZEN_3DES_KEY
*tmp_3des_key
= NULL
;
1666 tmp_3des_key
= (ZEN_3DES_KEY
*) ( ctx
->cipher_data
) ;
1667 memcpy ( &( tmp_3des_key
->des3_key
[ 0 ] ), key
, 24 ) ;
1668 memcpy ( &( tmp_3des_key
->des3_iv
[ 0 ] ), iv
, 8 ) ;
1676 static int engine_des_ede3_cbc_cipher ( EVP_CIPHER_CTX
*ctx
, unsigned char *out
, const unsigned char *in
,
1677 unsigned int in_len
)
1680 zen_nb_t output
, input
;
1681 zen_nb_t deskey_1
, deskey_2
, deskey_3
, iv
;
1684 /* Convert parameters ... */
1685 input
.len
= in_len
;
1686 input
.data
= (unsigned char *) in
;
1687 output
.len
= in_len
;
1694 deskey_1
.data
= (unsigned char *) ( (ZEN_3DES_KEY
*) ( ctx
->cipher_data
) )->des3_key
;
1695 deskey_2
.data
= (unsigned char *) &( (ZEN_3DES_KEY
*) ( ctx
->cipher_data
) )->des3_key
[ 8 ] ;
1696 deskey_3
.data
= (unsigned char *) &( (ZEN_3DES_KEY
*) ( ctx
->cipher_data
) )->des3_key
[ 16 ] ;
1698 /* Key correct iv ... */
1699 memcpy ( ( (ZEN_3DES_KEY
*) ( ctx
->cipher_data
) )->des3_iv
, ctx
->iv
, 8 ) ;
1701 iv
.data
= (unsigned char *) ( (ZEN_3DES_KEY
*) ( ctx
->cipher_data
) )->des3_iv
;
1703 if ( ctx
->encrypt
== 0 ) {
1704 memcpy ( ctx
->iv
, &( input
.data
[ input
.len
- 8 ] ), 8 ) ;
1707 /* Test with zenbridge library ... */
1708 to_return
= ptr_zencod_xdes_cipher ( &output
, &input
,
1709 (zen_nb_t
*) &deskey_1
, (zen_nb_t
*) &deskey_2
, (zen_nb_t
*) &deskey_3
, &iv
, ctx
->encrypt
) ;
1710 to_return
= !to_return
;
1712 if ( ctx
->encrypt
== 1 ) {
1713 memcpy ( ctx
->iv
, &( output
.data
[ output
.len
- 8 ] ), 8 ) ;
1720 static int engine_cipher_cleanup ( EVP_CIPHER_CTX
*ctx
)
1723 /* Set the key pointer ... */
1724 if ( ctx
->cipher
->nid
== NID_rc4
|| ctx
->cipher
->nid
== NID_rc4_40
) {
1726 else if ( ctx
->cipher
->nid
== NID_des_cbc
) {
1728 else if ( ctx
->cipher
->nid
== NID_des_ede3_cbc
) {
1735 #endif /* !OPENSSL_NO_HW_ZENCOD */
1736 #endif /* !OPENSSL_NO_HW */