Remove a bunch of false positives, fix one true positive.
[openssl.git] / demos / engines / zencod / hw_zencod.c
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  ;-)
5  *
6  * Date : 25 jun 2002
7  * Revision : 17 Ju7 2002
8  * Version : zencod_engine-0.9.7
9  */
10
11 /* ====================================================================
12  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  *
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
24  *    distribution.
25  *
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/)"
30  *
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.
35  *
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.
39  *
40  * 6. Redistributions of any form whatsoever must retain the following
41  *    acknowledgment:
42  *    "This product includes software developed by the OpenSSL Project
43  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
44  *
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  * ====================================================================
58  *
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).
62  *
63  */
64
65
66 /* ENGINE general include */
67 #include <stdio.h>
68 #include <openssl/crypto.h>
69 #include <openssl/dso.h>
70 #include <openssl/engine.h>
71
72 #ifndef OPENSSL_NO_HW
73 #ifndef OPENSSL_NO_HW_ZENCOD
74
75 #ifdef FLAT_INC
76 #  include "hw_zencod.h"
77 #else
78 #  include "vendor_defns/hw_zencod.h"
79 #endif
80
81 #define ZENCOD_LIB_NAME "zencod engine"
82 #include "hw_zencod_err.c"
83
84 #define FAIL_TO_SOFTWARE                -15
85
86 #define ZEN_LIBRARY     "zenbridge"
87
88 #if 0
89 #  define PERROR(s)     perror(s)
90 #  define CHEESE()      fputs("## [ZenEngine] ## " __FUNCTION__ "\n", stderr)
91 #else
92 #  define PERROR(s)
93 #  define CHEESE()
94 #endif
95
96
97 /* Sorry ;) */
98 #ifndef WIN32
99 static inline void esrever ( unsigned char *d, int l )
100 {
101         for(;--l>0;--l,d++){*d^=*(d+l);*(d+l)^=*d;*d^=*(d+l);}
102 }
103
104 static inline void ypcmem ( unsigned char *d, const unsigned char *s, int l )
105 {
106         for(d+=l;l--;)*--d=*s++;
107 }
108 #else
109 static __inline void esrever ( unsigned char *d, int l )
110 {
111         for(;--l>0;--l,d++){*d^=*(d+l);*(d+l)^=*d;*d^=*(d+l);}
112 }
113
114 static __inline void ypcmem ( unsigned char *d, const unsigned char *s, int l )
115 {
116         for(d+=l;l--;)*--d=*s++;
117 }
118 #endif
119
120
121 #define BIGNUM2ZEN(n, bn)       (ptr_zencod_init_number((n), \
122                                         (unsigned long) ((bn)->top * BN_BITS2), \
123                                         (unsigned char *) ((bn)->d)))
124
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)))
127
128
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) () ) ;
134
135 /* BIGNUM stuff */
136 static int zencod_bn_mod_exp ( BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx ) ;
137
138 /* RSA stuff */
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 ) ;
143 #endif
144
145 /* DSA stuff */
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 ) ;
149
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,
152                 DSA *dsa ) ;
153 #endif
154
155 /* DH stuff */
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 ) ;
161 #endif
162
163 /* Rand stuff */
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 ) ;
167
168 /* Digest Stuff */
169 static int engine_digests ( ENGINE *e, const EVP_MD **digest, const int **nids, int nid ) ;
170
171 /* Cipher Stuff */
172 static int engine_ciphers ( ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid ) ;
173
174
175 #define ZENCOD_CMD_SO_PATH                      ENGINE_CMD_BASE
176 static const ENGINE_CMD_DEFN zencod_cmd_defns [ ] =
177 {
178         { ZENCOD_CMD_SO_PATH,
179           "SO_PATH",
180           "Specifies the path to the 'zenbridge' shared library",
181           ENGINE_CMD_FLAG_STRING},
182         { 0, NULL, NULL, 0 }
183 } ;
184
185
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 =
189 {
190         "ZENCOD RSA method",
191         NULL,
192         NULL,
193         NULL,
194         NULL,
195         RSA_zencod_rsa_mod_exp,
196         RSA_zencod_bn_mod_exp,
197         NULL,
198         NULL,
199         0,
200         NULL,
201         NULL,
202         NULL
203 } ;
204 #endif
205
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 =
209 {
210         "ZENCOD DSA method",
211         DSA_zencod_do_sign,
212         NULL,
213         DSA_zencod_do_verify,
214         NULL,
215         DSA_zencod_bn_mod_exp,
216         NULL,
217         NULL,
218         0,
219         NULL
220 } ;
221 #endif
222
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 =
226 {
227         "ZENCOD DH method",
228         DH_zencod_generate_key,
229         DH_zencod_compute_key,
230         DH_zencod_bn_mod_exp,
231         NULL,
232         NULL,
233         0,
234         NULL
235 } ;
236 #endif
237
238 /* Our internal RAND_meth specific to zencod ZNGINE providing pointers to  our function */
239 static RAND_METHOD zencod_rand =
240 {
241         RAND_zencod_seed,
242         RAND_zencod_rand_bytes,
243         NULL,
244         NULL,
245         RAND_zencod_rand_bytes,
246         RAND_zencod_rand_status
247 } ;
248
249
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";
253
254
255 /* This internal function is used by ENGINE_zencod () and possibly by the
256  * "dynamic" ENGINE support too   ;-)
257  */
258 static int bind_helper ( ENGINE *e )
259 {
260
261 #ifndef OPENSSL_NO_RSA
262         const RSA_METHOD *meth_rsa ;
263 #endif
264 #ifndef OPENSSL_NO_DSA
265         const DSA_METHOD *meth_dsa ;
266 #endif
267 #ifndef OPENSSL_NO_DH
268         const DH_METHOD *meth_dh ;
269 #endif
270
271         const RAND_METHOD *meth_rand ;
272
273
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 ) ||
278 #endif
279 #ifndef OPENSSL_NO_DSA
280                         !ENGINE_set_DSA ( e, &zencod_dsa ) ||
281 #endif
282 #ifndef OPENSSL_NO_DH
283                         !ENGINE_set_DH ( e, &zencod_dh ) ||
284 #endif
285                         !ENGINE_set_RAND ( e, &zencod_rand ) ||
286
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 ) ) {
294                 return 0 ;
295         }
296
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
304          * anyway!
305          */
306         meth_rsa = RSA_PKCS1_SSLeay () ;
307
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 ;
316 #endif
317
318 #ifndef OPENSSL_NO_DSA
319         /* We use OpenSSL meth to supply what we don't provide ;-*)
320          */
321         meth_dsa = DSA_OpenSSL () ;
322
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 ;
330 #endif
331
332 #ifndef OPENSSL_NO_DH
333         /* We use OpenSSL meth to supply what we don't provide ;-*)
334          */
335         meth_dh = DH_OpenSSL () ;
336
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 ;
342
343 #endif
344
345         /* We use OpenSSL (SSLeay) meth to supply what we don't provide ;-*)
346          */
347         meth_rand = RAND_SSLeay () ;
348
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 ; */
359
360         /* Ensure the zencod error handling is set up */
361         ERR_load_ZENCOD_strings () ;
362         return 1 ;
363 }
364
365
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!!!)
368  */
369 ENGINE *ENGINE_zencod ( void )
370 {
371
372         ENGINE *eng = ENGINE_new () ;
373
374         if ( !eng ) {
375                 return NULL ;
376         }
377         if ( !bind_helper ( eng ) ) {
378                 ENGINE_free ( eng ) ;
379                 return NULL ;
380         }
381
382         return eng ;
383 }
384
385
386 void ENGINE_load_zencod ( void )
387 {
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 ( ) ;
394 }
395
396
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.
402  */
403 static DSO *zencod_dso = NULL ;
404
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 ;
410
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 ;
419
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 ;
426
427 static t_zencod_xdes_cipher *ptr_zencod_xdes_cipher = NULL ;
428 static t_zencod_rc4_cipher *ptr_zencod_rc4_cipher = NULL ;
429
430 /* These are the static string constants for the DSO file name and the function
431  * symbol names to bind to.
432  */
433 static const char *ZENCOD_LIBNAME = ZEN_LIBRARY ;
434
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" ;
440
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" ;
449
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" ;
456
457 static const char *ZENCOD_Fct_xdes_1 = "zenbridge_xdes_cipher" ;
458 static const char *ZENCOD_Fct_rc4_1 = "zenbridge_rc4_cipher" ;
459
460 /* Destructor (complements the "ENGINE_zencod ()" constructor)
461  */
462 static int zencod_destroy (ENGINE *e )
463 {
464
465         ERR_unload_ZENCOD_strings () ;
466
467         return 1 ;
468 }
469
470
471 /* (de)initialisation functions. Control Function
472  */
473 static int zencod_init ( ENGINE *e )
474 {
475
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 ;
497
498         CHEESE () ;
499
500         /*
501          * We Should add some tests for non NULL parameters or bad value !!
502          * Stuff to be done ...
503          */
504
505         if ( zencod_dso != NULL ) {
506                 ZENCODerr ( ZENCOD_F_ZENCOD_INIT, ZENCOD_R_ALREADY_LOADED ) ;
507                 goto err ;
508         }
509         /* Trying to load the Library "cryptozen"
510          */
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 ) ;
514                 goto err ;
515         }
516
517         /* Trying to load Function from the Library
518          */
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 ) ) ) {
540
541                 ZENCODerr ( ZENCOD_F_ZENCOD_INIT, ZENCOD_R_DSO_FAILURE ) ;
542                 goto err ;
543         }
544
545         /* The function from "cryptozen" Library have been correctly loaded so copy them
546          */
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 ;
569
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 !!
573          */
574
575         if ( ptr_zencod_test () != 0 ) {
576                 ZENCODerr ( ZENCOD_F_ZENCOD_INIT, ZENCOD_R_UNIT_FAILURE ) ;
577                 goto err ;
578         }
579
580         return 1 ;
581 err :
582         if ( zencod_dso ) {
583                 DSO_free ( zencod_dso ) ;
584         }
585         zencod_dso = NULL ;
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 ;
607
608         return 0 ;
609 }
610
611
612 static int zencod_finish ( ENGINE *e )
613 {
614
615         CHEESE () ;
616
617         /*
618          * We Should add some tests for non NULL parameters or bad value !!
619          * Stuff to be done ...
620          */
621         if ( zencod_dso == NULL ) {
622                 ZENCODerr ( ZENCOD_F_ZENCOD_FINISH, ZENCOD_R_NOT_LOADED ) ;
623                 return 0 ;
624         }
625         if ( !DSO_free ( zencod_dso ) ) {
626                 ZENCODerr ( ZENCOD_F_ZENCOD_FINISH, ZENCOD_R_DSO_FAILURE ) ;
627                 return 0 ;
628         }
629
630         zencod_dso = NULL ;
631
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 ;
653
654         return 1 ;
655 }
656
657
658 static int zencod_ctrl ( ENGINE *e, int cmd, long i, void *p, void (*f) () )
659 {
660
661         int initialised = ( ( zencod_dso == NULL ) ? 0 : 1 ) ;
662
663         CHEESE () ;
664
665         /*
666          * We Should add some tests for non NULL parameters or bad value !!
667          * Stuff to be done ...
668          */
669         switch ( cmd ) {
670         case ZENCOD_CMD_SO_PATH :
671                 if ( p == NULL ) {
672                         ZENCODerr ( ZENCOD_F_ZENCOD_CTRL, ERR_R_PASSED_NULL_PARAMETER ) ;
673                         return 0 ;
674                 }
675                 if ( initialised ) {
676                         ZENCODerr ( ZENCOD_F_ZENCOD_CTRL, ZENCOD_R_ALREADY_LOADED ) ;
677                         return 0 ;
678                 }
679                 ZENCOD_LIBNAME = (const char *) p ;
680                 return 1 ;
681         default :
682                 break ;
683         }
684
685         ZENCODerr ( ZENCOD_F_ZENCOD_CTRL, ZENCOD_R_CTRL_COMMAND_NOT_IMPLEMENTED ) ;
686
687         return 0 ;
688 }
689
690
691 /* BIGNUM stuff Functions
692  */
693 static int zencod_bn_mod_exp ( BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx )
694 {
695         zen_nb_t y, x, e, n;
696         int ret;
697
698         CHEESE () ;
699
700         if ( !zencod_dso ) {
701                 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_NOT_LOADED);
702                 return 0;
703         }
704
705         if ( !bn_wexpand(r, m->top + 1) ) {
706                 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_BN_EXPAND_FAIL);
707                 return 0;
708         }
709
710         memset(r->d, 0, BN_num_bytes(m));
711
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 ) ;
716
717         /* Must invert x and e parameter due to BN mod exp prototype ... */
718         ret = ptr_zencod_math_mod_exp ( &y, &e, &x, &n ) ;
719
720         if ( ret )  {
721                 PERROR("zenbridge_math_mod_exp");
722                 ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_REQUEST_FAILED);
723                 return 0;
724         }
725
726         r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2;
727
728         return 1;
729 }
730
731
732 /* RSA stuff Functions
733  */
734 #ifndef OPENSSL_NO_RSA
735 static int RSA_zencod_rsa_mod_exp ( BIGNUM *r0, const BIGNUM *i, RSA *rsa )
736 {
737
738         CHEESE () ;
739
740         if ( !zencod_dso ) {
741                 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT, ZENCOD_R_NOT_LOADED);
742                 return 0;
743         }
744
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);
747                 return 0;
748         }
749
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;
753
754                 meth = RSA_PKCS1_SSLeay();
755                 return meth->rsa_mod_exp(r0, i, rsa);
756         } else {
757                 zen_nb_t y, x, p, q, dmp1, dmq1, iqmp;
758
759                 if ( !bn_expand(r0, RSA_size(rsa) * 8) ) {
760                         ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT, ZENCOD_R_BN_EXPAND_FAIL);
761                         return 0;
762                 }
763                 r0->top = (RSA_size(rsa) * 8 + BN_BITS2 - 1) / BN_BITS2;
764
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 ) ;
772
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);
776                         return 0;
777                 }
778
779                 return 1;
780         }
781 }
782
783
784 /* This function is aliased to RSA_mod_exp (with the mont stuff dropped).
785  */
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 )
788 {
789
790         CHEESE () ;
791
792         if ( !zencod_dso ) {
793                 ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_NOT_LOADED);
794                 return 0;
795         }
796
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;
800
801                 meth = RSA_PKCS1_SSLeay();
802                 return meth->bn_mod_exp(r, a, p, m, ctx, m_ctx);
803         } else {
804                 zen_nb_t y, x, e, n;
805
806                 if ( !bn_expand(r, BN_num_bits(m)) ) {
807                         ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_BN_EXPAND_FAIL);
808                         return 0;
809                 }
810                 r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2;
811
812                 BIGNUM2ZEN ( &x, a ) ;
813                 BIGNUM2ZEN ( &y, r ) ;
814                 BIGNUM2ZEN ( &e, p ) ;
815                 BIGNUM2ZEN ( &n, m ) ;
816
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);
820                         return 0;
821                 }
822
823                 return 1;
824         }
825 }
826 #endif /* !OPENSSL_NO_RSA */
827
828
829 #ifndef OPENSSL_NO_DSA
830 /* DSA stuff Functions
831  */
832 static DSA_SIG *DSA_zencod_do_sign ( const unsigned char *dgst, int dlen, DSA *dsa )
833 {
834         zen_nb_t p, q, g, x, y, r, s, data;
835         DSA_SIG *sig;
836         BIGNUM *bn_r = NULL;
837         BIGNUM *bn_s = NULL;
838         char msg[20];
839
840         CHEESE();
841
842         if ( !zencod_dso ) {
843                 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_NOT_LOADED);
844                 goto FAILED;
845         }
846
847         if ( dlen > 160 ) {
848                 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
849                 goto FAILED;
850         }
851
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);
859         }
860
861         if ( !(bn_s = BN_new()) || !(bn_r = BN_new()) ) {
862                 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
863                 goto FAILED;
864         }
865
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);
868                 goto FAILED;
869         }
870
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 ) ;
879         q.len = x.len = 160;
880
881         ypcmem(msg, dgst, 20);
882         ptr_zencod_init_number ( &data, 160, msg ) ;
883
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);
887                 goto FAILED;
888         }
889
890         if ( !( sig = DSA_SIG_new () ) ) {
891                 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
892                 goto FAILED;
893         }
894         sig->r = bn_r;
895         sig->s = bn_s;
896         return sig;
897
898  FAILED:
899         if (bn_r)
900                 BN_free(bn_r);
901         if (bn_s)
902                 BN_free(bn_s);
903         return NULL;
904 }
905
906
907 static int DSA_zencod_do_verify ( const unsigned char *dgst, int dlen, DSA_SIG *sig, DSA *dsa )
908 {
909         zen_nb_t data, p, q, g, y, r, s, v;
910         char msg[20];
911         char v_data[20];
912         int ret;
913
914         CHEESE();
915
916         if ( !zencod_dso ) {
917                 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY, ZENCOD_R_NOT_LOADED);
918                 return 0;
919         }
920
921         if ( dlen > 160 ) {
922                 ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
923                 return 0;
924         }
925
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);
933         }
934
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 ) ;
944
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);
948                 return 0;
949         }
950
951         return ( ( ret == 0 ) ? 1 : ret ) ;
952 }
953
954
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 )
957 {
958         CHEESE () ;
959
960         return zencod_bn_mod_exp ( r, a, p, m, ctx ) ;
961 }
962 #endif /* !OPENSSL_NO_DSA */
963
964
965 #ifndef OPENSSl_NO_DH
966 /* DH stuff Functions
967  */
968 static int DH_zencod_generate_key ( DH *dh )
969 {
970         BIGNUM *bn_prv = NULL;
971         BIGNUM *bn_pub = NULL;
972         zen_nb_t y, x, g, p;
973         int generate_x;
974
975         CHEESE();
976
977         if ( !zencod_dso ) {
978                 ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_NOT_LOADED);
979                 return 0;
980         }
981
982         /* Private key */
983         if ( dh->priv_key ) {
984                 bn_prv = dh->priv_key;
985                 generate_x = 0;
986         } else {
987                 if (!(bn_prv = BN_new())) {
988                         ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
989                         goto FAILED;
990                 }
991                 generate_x = 1;
992         }
993
994         /* Public key */
995         if ( dh->pub_key )
996                 bn_pub = dh->pub_key;
997         else
998                 if ( !( bn_pub = BN_new () ) ) {
999                         ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
1000                         goto FAILED;
1001                 }
1002
1003         /* Expand */
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);
1007                 goto FAILED;
1008         }
1009         bn_prv->top = dh->p->top;
1010         bn_pub->top = dh->p->top;
1011
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;
1018
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 ) ) ;
1022
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);
1027                 goto FAILED;
1028         }
1029
1030         dh->priv_key = bn_prv;
1031         dh->pub_key  = bn_pub;
1032
1033         return 1;
1034
1035  FAILED:
1036         if (!dh->priv_key && bn_prv)
1037                 BN_free(bn_prv);
1038         if (!dh->pub_key && bn_pub)
1039                 BN_free(bn_pub);
1040
1041         return 0;
1042 }
1043
1044
1045 static int DH_zencod_compute_key ( unsigned char *key, const BIGNUM *pub_key, DH *dh )
1046 {
1047         zen_nb_t y, x, p, k;
1048
1049         CHEESE();
1050
1051         if ( !zencod_dso ) {
1052                 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_NOT_LOADED);
1053                 return 0;
1054         }
1055
1056         if ( !dh->priv_key ) {
1057                 ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_BAD_KEY_COMPONENTS);
1058                 return 0;
1059         }
1060
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 ) ;
1066
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 ) ) ;
1071
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);
1075                 return 0;
1076         }
1077
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 ) ) ;
1081
1082         return ZEN_BYTES ( k.len ) ;
1083 }
1084
1085
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 )
1088 {
1089         CHEESE () ;
1090
1091         return zencod_bn_mod_exp ( r, a, p, m, ctx ) ;
1092 }
1093 #endif  /* !OPENSSL_NO_DH */
1094
1095
1096 /* RAND stuff Functions
1097  */
1098 static void RAND_zencod_seed ( const void *buf, int num )
1099 {
1100         /* Nothing to do cause our crypto accelerator provide a true random generator */
1101 }
1102
1103
1104 static int RAND_zencod_rand_bytes ( unsigned char *buf, int num )
1105 {
1106         zen_nb_t r;
1107
1108         CHEESE();
1109
1110         if ( !zencod_dso ) {
1111                 ENGINEerr(ZENCOD_F_ZENCOD_RAND, ZENCOD_R_NOT_LOADED);
1112                 return 0;
1113         }
1114
1115         ptr_zencod_init_number ( &r, num * 8, buf ) ;
1116
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);
1120                 return 0;
1121         }
1122
1123         return 1;
1124 }
1125
1126
1127 static int RAND_zencod_rand_status ( void )
1128 {
1129         CHEESE () ;
1130
1131         return 1;
1132 }
1133
1134
1135 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1136  * shared-library.
1137  */
1138 #ifdef ENGINE_DYNAMIC_SUPPORT
1139 static int bind_fn ( ENGINE *e, const char *id )
1140 {
1141
1142         if ( id && ( strcmp ( id, engine_zencod_id ) != 0 ) ) {
1143                 return 0 ;
1144         }
1145         if ( !bind_helper ( e ) )  {
1146                 return 0 ;
1147         }
1148
1149         return 1 ;
1150 }
1151
1152 IMPLEMENT_DYNAMIC_CHECK_FN ()
1153 IMPLEMENT_DYNAMIC_BIND_FN ( bind_fn )
1154 #endif /* ENGINE_DYNAMIC_SUPPORT */
1155
1156
1157
1158
1159 /*
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 ...
1164  */
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>
1171
1172
1173 /* Some variables declaration ... */
1174 /* DONS:
1175  * Disable symetric computation except DES and 3DES, but let part of the code
1176  */
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 ;
1183
1184
1185 /* Function prototype ... */
1186 /*  SHA stuff */
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 ) ;
1190
1191 /*  MD5 stuff */
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 ) ;
1195
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 ) ;
1198
1199
1200 /* RC4 Stuff */
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 ) ;
1203
1204 /* DES Stuff */
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 ) ;
1207
1208 /*  3DES Stuff */
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 ) ;
1211
1212 static int engine_cipher_cleanup ( EVP_CIPHER_CTX *ctx ) ;      /* cleanup ctx */
1213
1214
1215 /* The one for SHA ... */
1216 static const EVP_MD engine_sha1_md =
1217 {
1218         NID_sha1,
1219         NID_sha1WithRSAEncryption,
1220         SHA_DIGEST_LENGTH,
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 ... */
1224         engine_sha1_init,
1225         engine_sha1_update,
1226         engine_sha1_final,
1227         engine_md_copy,         /* dev_crypto_sha_copy */
1228         engine_md_cleanup,              /* dev_crypto_sha_cleanup */
1229         EVP_PKEY_RSA_method,
1230         SHA_CBLOCK,
1231         /* sizeof ( EVP_MD * ) + sizeof ( SHA_CTX ) */
1232         sizeof ( ZEN_MD_DATA )
1233         /* sizeof ( MD_CTX_DATA )       The message digest data stucture ... */
1234 } ;
1235
1236 /* The one for MD5 ... */
1237 static const EVP_MD engine_md5_md =
1238 {
1239         NID_md5,
1240         NID_md5WithRSAEncryption,
1241         MD5_DIGEST_LENGTH,
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 ... */
1245         engine_md5_init,
1246         engine_md5_update,
1247         engine_md5_final,
1248         engine_md_copy,         /* dev_crypto_md5_copy */
1249         engine_md_cleanup,              /* dev_crypto_md5_cleanup */
1250         EVP_PKEY_RSA_method,
1251         MD5_CBLOCK,
1252         /* sizeof ( EVP_MD * ) + sizeof ( MD5_CTX ) */
1253         sizeof ( ZEN_MD_DATA )
1254         /* sizeof ( MD_CTX_DATA )       The message digest data stucture ... */
1255 } ;
1256
1257
1258 /* The one for RC4 ... */
1259 #define EVP_RC4_KEY_SIZE                        16
1260
1261 /* Try something static ... */
1262 typedef struct
1263 {
1264         unsigned int len ;
1265         unsigned int first ;
1266         unsigned char rc4_state [ 260 ] ;
1267 } NEW_ZEN_RC4_KEY ;
1268
1269 #define rc4_data(ctx)                           ( (EVP_RC4_KEY *) ( ctx )->cipher_data )
1270
1271 static const EVP_CIPHER engine_rc4 =
1272 {
1273         NID_rc4,
1274         1,
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,
1279         engine_rc4_cipher,
1280         engine_cipher_cleanup,
1281         sizeof ( NEW_ZEN_RC4_KEY ),
1282         NULL,
1283         NULL,
1284         NULL
1285 } ;
1286
1287 /* The one for RC4_40 ... */
1288 static const EVP_CIPHER engine_rc4_40 =
1289 {
1290         NID_rc4_40,
1291         1,
1292         5,                              /* 40 bits */
1293         0,
1294         EVP_CIPH_VARIABLE_LENGTH,
1295         engine_rc4_init_key,
1296         engine_rc4_cipher,
1297         engine_cipher_cleanup,
1298         sizeof ( NEW_ZEN_RC4_KEY ),
1299         NULL,
1300         NULL,
1301         NULL
1302 } ;
1303
1304 /* The one for DES ... */
1305
1306 /* Try something static ... */
1307 typedef struct
1308 {
1309         unsigned char des_key [ 24 ] ;
1310         unsigned char des_iv [ 8 ] ;
1311 } ZEN_DES_KEY ;
1312
1313 static const EVP_CIPHER engine_des_cbc =
1314         {
1315         NID_des_cbc,
1316         8, 8, 8,
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,
1324         NULL,
1325         NULL
1326         };
1327
1328 /* The one for 3DES ... */
1329
1330 /* Try something static ... */
1331 typedef struct
1332 {
1333         unsigned char des3_key [ 24 ] ;
1334         unsigned char des3_iv [ 8 ] ;
1335 } ZEN_3DES_KEY ;
1336
1337 #define des_data(ctx)                            ( (DES_EDE_KEY *) ( ctx )->cipher_data )
1338
1339 static const EVP_CIPHER engine_des_ede3_cbc =
1340         {
1341         NID_des_ede3_cbc,
1342         8, 8, 8,
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,
1350         NULL,
1351         NULL
1352         };
1353
1354
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 )
1357 {
1358
1359 #ifdef DEBUG_ZENCOD_MD
1360         fprintf ( stderr, "\t=>Function : static int engine_digests () called !\n" ) ;
1361 #endif
1362
1363         if ( !digest ) {
1364                 /* We are returning a list of supported nids */
1365                 *nids = engine_digest_nids ;
1366                 return engine_digest_nids_num ;
1367         }
1368         /* We are being asked for a specific digest */
1369         if ( nid == NID_md5 ) {
1370                 *digest = &engine_md5_md ;
1371         }
1372         else if ( nid == NID_sha1 ) {
1373                 *digest = &engine_sha1_md ;
1374         }
1375         else {
1376                 *digest = NULL ;
1377                 return 0 ;
1378         }
1379         return 1 ;
1380 }
1381
1382
1383 /* SHA stuff Functions
1384  */
1385 static int engine_sha1_init ( EVP_MD_CTX *ctx )
1386 {
1387
1388         int to_return = 0 ;
1389
1390         /* Test with zenbridge library ... */
1391         to_return = ptr_zencod_sha1_init ( (ZEN_MD_DATA *) ctx->md_data ) ;
1392         to_return = !to_return ;
1393
1394         return to_return ;
1395 }
1396
1397
1398 static int engine_sha1_update ( EVP_MD_CTX *ctx, const void *data, unsigned long count )
1399 {
1400
1401         zen_nb_t input ;
1402         int to_return = 0 ;
1403
1404         /* Convert parameters ... */
1405         input.len = count ;
1406         input.data = (unsigned char *) data ;
1407
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 ;
1411
1412         return to_return ;
1413 }
1414
1415
1416 static int engine_sha1_final ( EVP_MD_CTX *ctx, unsigned char *md )
1417 {
1418
1419         zen_nb_t output ;
1420         int to_return = 0 ;
1421
1422         /* Convert parameters ... */
1423         output.len = SHA_DIGEST_LENGTH ;
1424         output.data = md ;
1425
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 ;
1429
1430         return to_return ;
1431 }
1432
1433
1434
1435 /* MD5 stuff Functions
1436  */
1437 static int engine_md5_init ( EVP_MD_CTX *ctx )
1438 {
1439
1440         int to_return = 0 ;
1441
1442         /* Test with zenbridge library ... */
1443         to_return = ptr_zencod_md5_init ( (ZEN_MD_DATA *) ctx->md_data ) ;
1444         to_return = !to_return ;
1445
1446         return to_return ;
1447 }
1448
1449
1450 static int engine_md5_update ( EVP_MD_CTX *ctx, const void *data, unsigned long count )
1451 {
1452
1453         zen_nb_t input ;
1454         int to_return = 0 ;
1455
1456         /* Convert parameters ... */
1457         input.len = count ;
1458         input.data = (unsigned char *) data ;
1459
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 ;
1463
1464         return to_return ;
1465 }
1466
1467
1468 static int engine_md5_final ( EVP_MD_CTX *ctx, unsigned char *md )
1469 {
1470
1471         zen_nb_t output ;
1472         int to_return = 0 ;
1473
1474         /* Convert parameters ... */
1475         output.len = MD5_DIGEST_LENGTH ;
1476         output.data = md ;
1477
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 ;
1481
1482         return to_return ;
1483 }
1484
1485
1486 static int engine_md_cleanup ( EVP_MD_CTX *ctx )
1487 {
1488
1489         ZEN_MD_DATA *zen_md_data = (ZEN_MD_DATA *) ctx->md_data ;
1490
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 ;
1495         }
1496
1497         return 1 ;
1498 }
1499
1500
1501 static int engine_md_copy ( EVP_MD_CTX *to, const EVP_MD_CTX *from )
1502 {
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 ;
1505
1506         to_md->HashBuffer = OPENSSL_malloc ( from_md->HashBufferSize ) ;
1507         memcpy ( to_md->HashBuffer, from_md->HashBuffer, from_md->HashBufferSize ) ;
1508
1509         return 1;
1510 }
1511
1512
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 )
1515 {
1516
1517         if ( !cipher ) {
1518                 /* We are returning a list of supported nids */
1519                 *nids = engine_cipher_nids ;
1520                 return engine_cipher_nids_num ;
1521         }
1522         /* We are being asked for a specific cipher */
1523         if ( nid == NID_rc4 ) {
1524                 *cipher = &engine_rc4 ;
1525         }
1526         else if ( nid == NID_rc4_40 ) {
1527                 *cipher = &engine_rc4_40 ;
1528         }
1529         else if ( nid == NID_des_cbc ) {
1530                 *cipher = &engine_des_cbc ;
1531         }
1532         else if ( nid == NID_des_ede3_cbc ) {
1533                 *cipher = &engine_des_ede3_cbc ;
1534         }
1535         else {
1536                 *cipher = NULL ;
1537                 return 0 ;
1538         }
1539
1540         return 1 ;
1541 }
1542
1543
1544 static int engine_rc4_init_key ( EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc )
1545 {
1546         int to_return = 0 ;
1547         int i = 0 ;
1548         int nb = 0 ;
1549         NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL ;
1550
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 ) ;
1559         }
1560
1561         to_return = 1 ;
1562
1563         return to_return ;
1564 }
1565
1566
1567 static int engine_rc4_cipher ( EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int in_len )
1568 {
1569
1570         zen_nb_t output, input ;
1571         zen_nb_t rc4key ;
1572         int to_return = 0 ;
1573         NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL ;
1574
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 ;
1580
1581         tmp_rc4_key = ( (NEW_ZEN_RC4_KEY *) ( ctx->cipher_data ) ) ;
1582         rc4key.len = 260 ;
1583         rc4key.data = &( tmp_rc4_key->rc4_state [ 0 ] ) ;
1584
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 ;
1588
1589         /* Update encryption state ... */
1590         tmp_rc4_key->first = 1 ;
1591         tmp_rc4_key = NULL ;
1592
1593         return to_return ;
1594 }
1595
1596
1597 static int engine_des_init_key ( EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc )
1598 {
1599
1600         ZEN_DES_KEY *tmp_des_key = NULL ;
1601         int to_return = 0 ;
1602
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 ) ;
1608
1609         to_return = 1 ;
1610
1611         return to_return ;
1612 }
1613
1614
1615 static int engine_des_cbc_cipher ( EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl )
1616 {
1617
1618         zen_nb_t output, input ;
1619         zen_nb_t deskey_1, deskey_2, deskey_3, iv ;
1620         int to_return = 0 ;
1621
1622         /* Convert parameters ... */
1623         input.len = inl ;
1624         input.data = (unsigned char *) in ;
1625         output.len = inl ;
1626         output.data = out ;
1627
1628         /* Set key parameters ... */
1629         deskey_1.len = 8 ;
1630         deskey_2.len = 8 ;
1631         deskey_3.len = 8 ;
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 ] ;
1635
1636         /* Key correct iv ... */
1637         memcpy ( ( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_iv, ctx->iv, 8 ) ;
1638         iv.len = 8 ;
1639         iv.data = (unsigned char *) ( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_iv ;
1640
1641         if ( ctx->encrypt == 0 ) {
1642                 memcpy ( ctx->iv, &( input.data [ input.len - 8 ] ), 8 ) ;
1643         }
1644
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 ;
1649
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 ) ;
1654         }
1655
1656         return to_return ;
1657 }
1658
1659
1660 static int engine_des_ede3_init_key ( EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc )
1661 {
1662
1663         ZEN_3DES_KEY *tmp_3des_key = NULL ;
1664         int to_return = 0 ;
1665
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 ) ;
1669
1670         to_return = 1;
1671
1672         return to_return ;
1673 }
1674
1675
1676 static int engine_des_ede3_cbc_cipher ( EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,
1677         unsigned int in_len )
1678 {
1679
1680         zen_nb_t output, input ;
1681         zen_nb_t deskey_1, deskey_2, deskey_3, iv ;
1682         int to_return = 0 ;
1683
1684         /* Convert parameters ... */
1685         input.len = in_len ;
1686         input.data = (unsigned char *) in ;
1687         output.len = in_len ;
1688         output.data = out ;
1689
1690         /* Set key ... */
1691         deskey_1.len = 8 ;
1692         deskey_2.len = 8 ;
1693         deskey_3.len = 8 ;
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 ] ;
1697
1698         /* Key correct iv ... */
1699         memcpy ( ( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_iv, ctx->iv, 8 ) ;
1700         iv.len = 8 ;
1701         iv.data = (unsigned char *) ( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_iv ;
1702
1703         if ( ctx->encrypt == 0 ) {
1704                 memcpy ( ctx->iv, &( input.data [ input.len - 8 ] ), 8 ) ;
1705         }
1706
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 ;
1711
1712         if ( ctx->encrypt == 1 ) {
1713                 memcpy ( ctx->iv, &( output.data [ output.len - 8 ] ), 8 ) ;
1714         }
1715
1716         return to_return ;
1717 }
1718
1719
1720 static int engine_cipher_cleanup ( EVP_CIPHER_CTX *ctx )
1721 {
1722
1723         /* Set the key pointer ... */
1724         if ( ctx->cipher->nid == NID_rc4 || ctx->cipher->nid == NID_rc4_40 ) {
1725         }
1726         else if ( ctx->cipher->nid == NID_des_cbc ) {
1727         }
1728         else if ( ctx->cipher->nid == NID_des_ede3_cbc ) {
1729         }
1730
1731         return 1 ;
1732 }
1733
1734
1735 #endif /* !OPENSSL_NO_HW_ZENCOD */
1736 #endif /* !OPENSSL_NO_HW */