Use p==NULL not !p (in if statements, mainly)
[openssl.git] / demos / engines / zencod / hw_zencod.c
1 /* crypto/engine/hw_zencod.c */
2  /*
3   * Written by Fred Donnat (frederic.donnat@zencod.com) for "zencod" * engine
4   * integration in order to redirect crypto computing on a crypto * hardware
5   * accelerator zenssl32 ;-) * * Date : 25 jun 2002 * Revision : 17 Ju7 2002
6   * * Version : zencod_engine-0.9.7
7   */
8
9 /* ====================================================================
10  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  *
24  * 3. All advertising materials mentioning features or use of this
25  *    software must display the following acknowledgment:
26  *    "This product includes software developed by the OpenSSL Project
27  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
28  *
29  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
30  *    endorse or promote products derived from this software without
31  *    prior written permission. For written permission, please contact
32  *    licensing@OpenSSL.org.
33  *
34  * 5. Products derived from this software may not be called "OpenSSL"
35  *    nor may "OpenSSL" appear in their names without prior written
36  *    permission of the OpenSSL Project.
37  *
38  * 6. Redistributions of any form whatsoever must retain the following
39  *    acknowledgment:
40  *    "This product includes software developed by the OpenSSL Project
41  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
44  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
47  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54  * OF THE POSSIBILITY OF SUCH DAMAGE.
55  * ====================================================================
56  *
57  * This product includes cryptographic software written by Eric Young
58  * (eay@cryptsoft.com).  This product includes software written by Tim
59  * Hudson (tjh@cryptsoft.com).
60  *
61  */
62
63 /* ENGINE general include */
64 #include <stdio.h>
65 #include <openssl/crypto.h>
66 #include <openssl/dso.h>
67 #include <openssl/engine.h>
68
69 #ifndef OPENSSL_NO_HW
70 # ifndef OPENSSL_NO_HW_ZENCOD
71
72 #  ifdef FLAT_INC
73 #   include "hw_zencod.h"
74 #  else
75 #   include "vendor_defns/hw_zencod.h"
76 #  endif
77
78 #  define ZENCOD_LIB_NAME "zencod engine"
79 #  include "hw_zencod_err.c"
80
81 #  define FAIL_TO_SOFTWARE                -15
82
83 #  define ZEN_LIBRARY     "zenbridge"
84
85 #  ifdef ZENCOD_TRACING
86 #   define PERROR(s)     perror(s)
87 #   define CHEESE()      fputs("## [ZenEngine] ## " __FUNCTION__ "\n", stderr)
88 #  else
89 #   define PERROR(s)
90 #   define CHEESE()
91 #  endif
92
93 /* Sorry ;) */
94 #  ifndef WIN32
95 static inline void esrever(unsigned char *d, int l)
96 {
97     for (; --l > 0; --l, d++) {
98         *d ^= *(d + l);
99         *(d + l) ^= *d;
100         *d ^= *(d + l);
101     }
102 }
103
104 static inline void ypcmem(unsigned char *d, const unsigned char *s, int l)
105 {
106     for (d += l; l--;)
107         *--d = *s++;
108 }
109 #  else
110 static __inline void esrever(unsigned char *d, int l)
111 {
112     for (; --l > 0; --l, d++) {
113         *d ^= *(d + l);
114         *(d + l) ^= *d;
115         *d ^= *(d + l);
116     }
117 }
118
119 static __inline void ypcmem(unsigned char *d, const unsigned char *s, int l)
120 {
121     for (d += l; l--;)
122         *--d = *s++;
123 }
124 #  endif
125
126 #  define BIGNUM2ZEN(n, bn)       (ptr_zencod_init_number((n), \
127                                         (unsigned long) ((bn)->top * BN_BITS2), \
128                                         (unsigned char *) ((bn)->d)))
129
130 #  define ZEN_BITS(n, bytes)      (ptr_zencod_bytes2bits((unsigned char *) (n), (unsigned long) (bytes)))
131 #  define ZEN_BYTES(bits) (ptr_zencod_bits2bytes((unsigned long) (bits)))
132
133 /* Function for ENGINE detection and control */
134 static int zencod_destroy(ENGINE *e);
135 static int zencod_init(ENGINE *e);
136 static int zencod_finish(ENGINE *e);
137 static int zencod_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
138
139 /* BIGNUM stuff */
140 static int zencod_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
141                              const BIGNUM *m, BN_CTX *ctx);
142
143 /* RSA stuff */
144 #  ifndef OPENSSL_NO_RSA
145 static int RSA_zencod_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
146 static int RSA_zencod_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
147                                  const BIGNUM *m, BN_CTX *ctx,
148                                  BN_MONT_CTX *m_ctx);
149 #  endif
150
151 /* DSA stuff */
152 #  ifndef OPENSSL_NO_DSA
153 static int DSA_zencod_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
154                                  const BIGNUM *p, const BIGNUM *m,
155                                  BN_CTX *ctx, BN_MONT_CTX *m_ctx);
156
157 static DSA_SIG *DSA_zencod_do_sign(const unsigned char *dgst, int dlen,
158                                    DSA *dsa);
159 static int DSA_zencod_do_verify(const unsigned char *dgst, int dgst_len,
160                                 DSA_SIG *sig, DSA *dsa);
161 #  endif
162
163 /* DH stuff */
164 #  ifndef OPENSSL_NO_DH
165 static int DH_zencod_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a,
166                                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
167                                 BN_MONT_CTX *m_ctx);
168 static int DH_zencod_generate_key(DH *dh);
169 static int DH_zencod_compute_key(unsigned char *key, const BIGNUM *pub_key,
170                                  DH *dh);
171 #  endif
172
173 /* Rand stuff */
174 static void RAND_zencod_seed(const void *buf, int num);
175 static int RAND_zencod_rand_bytes(unsigned char *buf, int num);
176 static int RAND_zencod_rand_status(void);
177
178 /* Digest Stuff */
179 static int engine_digests(ENGINE *e, const EVP_MD **digest, const int **nids,
180                           int nid);
181
182 /* Cipher Stuff */
183 static int engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
184                           const int **nids, int nid);
185
186 #  define ZENCOD_CMD_SO_PATH                      ENGINE_CMD_BASE
187 static const ENGINE_CMD_DEFN zencod_cmd_defns[] = {
188     {ZENCOD_CMD_SO_PATH,
189      "SO_PATH",
190      "Specifies the path to the 'zenbridge' shared library",
191      ENGINE_CMD_FLAG_STRING},
192     {0, NULL, NULL, 0}
193 };
194
195 #  ifndef OPENSSL_NO_RSA
196 /*
197  * Our internal RSA_METHOD specific to zencod ENGINE providing pointers to
198  * our function
199  */
200 static RSA_METHOD zencod_rsa = {
201     "ZENCOD RSA method",
202     NULL,
203     NULL,
204     NULL,
205     NULL,
206     RSA_zencod_rsa_mod_exp,
207     RSA_zencod_bn_mod_exp,
208     NULL,
209     NULL,
210     0,
211     NULL,
212     NULL,
213     NULL
214 };
215 #  endif
216
217 #  ifndef OPENSSL_NO_DSA
218 /*
219  * Our internal DSA_METHOD specific to zencod ENGINE providing pointers to
220  * our function
221  */
222 static DSA_METHOD zencod_dsa = {
223     "ZENCOD DSA method",
224     DSA_zencod_do_sign,
225     NULL,
226     DSA_zencod_do_verify,
227     NULL,
228     DSA_zencod_bn_mod_exp,
229     NULL,
230     NULL,
231     0,
232     NULL
233 };
234 #  endif
235
236 #  ifndef OPENSSL_NO_DH
237 /*
238  * Our internal DH_METHOD specific to zencod ENGINE providing pointers to our
239  * function
240  */
241 static DH_METHOD zencod_dh = {
242     "ZENCOD DH method",
243     DH_zencod_generate_key,
244     DH_zencod_compute_key,
245     DH_zencod_bn_mod_exp,
246     NULL,
247     NULL,
248     0,
249     NULL
250 };
251 #  endif
252
253 /*
254  * Our internal RAND_meth specific to zencod ZNGINE providing pointers to our
255  * function
256  */
257 static RAND_METHOD zencod_rand = {
258     RAND_zencod_seed,
259     RAND_zencod_rand_bytes,
260     NULL,
261     NULL,
262     RAND_zencod_rand_bytes,
263     RAND_zencod_rand_status
264 };
265
266 /* Constants used when creating the ENGINE */
267 static const char *engine_zencod_id = "zencod";
268 static const char *engine_zencod_name = "ZENCOD hardware engine support";
269
270 /*
271  * This internal function is used by ENGINE_zencod () and possibly by the
272  * "dynamic" ENGINE support too ;-)
273  */
274 static int bind_helper(ENGINE *e)
275 {
276
277 #  ifndef OPENSSL_NO_RSA
278     const RSA_METHOD *meth_rsa;
279 #  endif
280 #  ifndef OPENSSL_NO_DSA
281     const DSA_METHOD *meth_dsa;
282 #  endif
283 #  ifndef OPENSSL_NO_DH
284     const DH_METHOD *meth_dh;
285 #  endif
286
287     const RAND_METHOD *meth_rand;
288
289     if (!ENGINE_set_id(e, engine_zencod_id) ||
290         !ENGINE_set_name(e, engine_zencod_name) ||
291 #  ifndef OPENSSL_NO_RSA
292         !ENGINE_set_RSA(e, &zencod_rsa) ||
293 #  endif
294 #  ifndef OPENSSL_NO_DSA
295         !ENGINE_set_DSA(e, &zencod_dsa) ||
296 #  endif
297 #  ifndef OPENSSL_NO_DH
298         !ENGINE_set_DH(e, &zencod_dh) ||
299 #  endif
300         !ENGINE_set_RAND(e, &zencod_rand) ||
301         !ENGINE_set_destroy_function(e, zencod_destroy) ||
302         !ENGINE_set_init_function(e, zencod_init) ||
303         !ENGINE_set_finish_function(e, zencod_finish) ||
304         !ENGINE_set_ctrl_function(e, zencod_ctrl) ||
305         !ENGINE_set_cmd_defns(e, zencod_cmd_defns) ||
306         !ENGINE_set_digests(e, engine_digests) ||
307         !ENGINE_set_ciphers(e, engine_ciphers)) {
308         return 0;
309     }
310 #  ifndef OPENSSL_NO_RSA
311     /*
312      * We know that the "PKCS1_SSLeay()" functions hook properly to the
313      * Zencod-specific mod_exp and mod_exp_crt so we use those functions. NB:
314      * We don't use ENGINE_openssl() or anything "more generic" because
315      * something like the RSAref code may not hook properly, and if you own
316      * one of these cards then you have the right to do RSA operations on it
317      * anyway!
318      */
319     meth_rsa = RSA_PKCS1_SSLeay();
320
321     zencod_rsa.rsa_pub_enc = meth_rsa->rsa_pub_enc;
322     zencod_rsa.rsa_pub_dec = meth_rsa->rsa_pub_dec;
323     zencod_rsa.rsa_priv_enc = meth_rsa->rsa_priv_enc;
324     zencod_rsa.rsa_priv_dec = meth_rsa->rsa_priv_dec;
325     /* meth_rsa->rsa_mod_exp */
326     /* meth_rsa->bn_mod_exp */
327     zencod_rsa.init = meth_rsa->init;
328     zencod_rsa.finish = meth_rsa->finish;
329 #  endif
330
331 #  ifndef OPENSSL_NO_DSA
332     /*
333      * We use OpenSSL meth to supply what we don't provide ;-*)
334      */
335     meth_dsa = DSA_OpenSSL();
336
337     /* meth_dsa->dsa_do_sign */
338     zencod_dsa.dsa_sign_setup = meth_dsa->dsa_sign_setup;
339     /* meth_dsa->dsa_do_verify */
340     zencod_dsa.dsa_mod_exp = meth_dsa->dsa_mod_exp;
341     /* zencod_dsa.bn_mod_exp = meth_dsa->bn_mod_exp ; */
342     zencod_dsa.init = meth_dsa->init;
343     zencod_dsa.finish = meth_dsa->finish;
344 #  endif
345
346 #  ifndef OPENSSL_NO_DH
347     /*
348      * We use OpenSSL meth to supply what we don't provide ;-*)
349      */
350     meth_dh = DH_OpenSSL();
351
352     /* zencod_dh.generate_key = meth_dh->generate_key ; */
353     /* zencod_dh.compute_key = meth_dh->compute_key ; */
354     /* zencod_dh.bn_mod_exp = meth_dh->bn_mod_exp ; */
355     zencod_dh.init = meth_dh->init;
356     zencod_dh.finish = meth_dh->finish;
357
358 #  endif
359
360     /*
361      * We use OpenSSL (SSLeay) meth to supply what we don't provide ;-*)
362      */
363     meth_rand = RAND_SSLeay();
364
365     /* meth_rand->seed ; */
366     /* zencod_rand.seed = meth_rand->seed ; */
367     /* meth_rand->bytes ; */
368     /* zencod_rand.bytes = meth_rand->bytes ; */
369     zencod_rand.cleanup = meth_rand->cleanup;
370     zencod_rand.add = meth_rand->add;
371     /* meth_rand->pseudorand ; */
372     /* zencod_rand.pseudorand = meth_rand->pseudorand ; */
373     /* zencod_rand.status = meth_rand->status ; */
374     /* meth_rand->status ; */
375
376     /* Ensure the zencod error handling is set up */
377     ERR_load_ZENCOD_strings();
378     return 1;
379 }
380
381 /*
382  * As this is only ever called once, there's no need for locking (indeed -
383  * the lock will already be held by our caller!!!)
384  */
385 static ENGINE *ENGINE_zencod(void)
386 {
387
388     ENGINE *eng = ENGINE_new();
389
390     if (!eng) {
391         return NULL;
392     }
393     if (!bind_helper(eng)) {
394         ENGINE_free(eng);
395         return NULL;
396     }
397
398     return eng;
399 }
400
401 #  ifdef ENGINE_DYNAMIC_SUPPORT
402 static
403 #  endif
404 void ENGINE_load_zencod(void)
405 {
406     /* Copied from eng_[openssl|dyn].c */
407     ENGINE *toadd = ENGINE_zencod();
408     if (!toadd)
409         return;
410     ENGINE_add(toadd);
411     ENGINE_free(toadd);
412     ERR_clear_error();
413 }
414
415 /*
416  * This is a process-global DSO handle used for loading and unloading the
417  * ZENBRIDGE library. NB: This is only set (or unset) during an * init () or
418  * finish () call (reference counts permitting) and they're * operating with
419  * global locks, so this should be thread-safe * implicitly.
420  */
421 static DSO *zencod_dso = NULL;
422
423 static t_zencod_test *ptr_zencod_test = NULL;
424 static t_zencod_bytes2bits *ptr_zencod_bytes2bits = NULL;
425 static t_zencod_bits2bytes *ptr_zencod_bits2bytes = NULL;
426 static t_zencod_new_number *ptr_zencod_new_number = NULL;
427 static t_zencod_init_number *ptr_zencod_init_number = NULL;
428
429 static t_zencod_rsa_mod_exp *ptr_zencod_rsa_mod_exp = NULL;
430 static t_zencod_rsa_mod_exp_crt *ptr_zencod_rsa_mod_exp_crt = NULL;
431 static t_zencod_dsa_do_sign *ptr_zencod_dsa_do_sign = NULL;
432 static t_zencod_dsa_do_verify *ptr_zencod_dsa_do_verify = NULL;
433 static t_zencod_dh_generate_key *ptr_zencod_dh_generate_key = NULL;
434 static t_zencod_dh_compute_key *ptr_zencod_dh_compute_key = NULL;
435 static t_zencod_rand_bytes *ptr_zencod_rand_bytes = NULL;
436 static t_zencod_math_mod_exp *ptr_zencod_math_mod_exp = NULL;
437
438 static t_zencod_md5_init *ptr_zencod_md5_init = NULL;
439 static t_zencod_md5_update *ptr_zencod_md5_update = NULL;
440 static t_zencod_md5_do_final *ptr_zencod_md5_do_final = NULL;
441 static t_zencod_sha1_init *ptr_zencod_sha1_init = NULL;
442 static t_zencod_sha1_update *ptr_zencod_sha1_update = NULL;
443 static t_zencod_sha1_do_final *ptr_zencod_sha1_do_final = NULL;
444
445 static t_zencod_xdes_cipher *ptr_zencod_xdes_cipher = NULL;
446 static t_zencod_rc4_cipher *ptr_zencod_rc4_cipher = NULL;
447
448 /*
449  * These are the static string constants for the DSO file name and the
450  * function symbol names to bind to.
451  */
452 static const char *ZENCOD_LIBNAME = ZEN_LIBRARY;
453
454 static const char *ZENCOD_Fct_0 = "test_device";
455 static const char *ZENCOD_Fct_1 = "zenbridge_bytes2bits";
456 static const char *ZENCOD_Fct_2 = "zenbridge_bits2bytes";
457 static const char *ZENCOD_Fct_3 = "zenbridge_new_number";
458 static const char *ZENCOD_Fct_4 = "zenbridge_init_number";
459
460 static const char *ZENCOD_Fct_exp_1 = "zenbridge_rsa_mod_exp";
461 static const char *ZENCOD_Fct_exp_2 = "zenbridge_rsa_mod_exp_crt";
462 static const char *ZENCOD_Fct_dsa_1 = "zenbridge_dsa_do_sign";
463 static const char *ZENCOD_Fct_dsa_2 = "zenbridge_dsa_do_verify";
464 static const char *ZENCOD_Fct_dh_1 = "zenbridge_dh_generate_key";
465 static const char *ZENCOD_Fct_dh_2 = "zenbridge_dh_compute_key";
466 static const char *ZENCOD_Fct_rand_1 = "zenbridge_rand_bytes";
467 static const char *ZENCOD_Fct_math_1 = "zenbridge_math_mod_exp";
468
469 static const char *ZENCOD_Fct_md5_1 = "zenbridge_md5_init";
470 static const char *ZENCOD_Fct_md5_2 = "zenbridge_md5_update";
471 static const char *ZENCOD_Fct_md5_3 = "zenbridge_md5_do_final";
472 static const char *ZENCOD_Fct_sha1_1 = "zenbridge_sha1_init";
473 static const char *ZENCOD_Fct_sha1_2 = "zenbridge_sha1_update";
474 static const char *ZENCOD_Fct_sha1_3 = "zenbridge_sha1_do_final";
475
476 static const char *ZENCOD_Fct_xdes_1 = "zenbridge_xdes_cipher";
477 static const char *ZENCOD_Fct_rc4_1 = "zenbridge_rc4_cipher";
478
479 /*
480  * Destructor (complements the "ENGINE_zencod ()" constructor)
481  */
482 static int zencod_destroy(ENGINE *e)
483 {
484
485     ERR_unload_ZENCOD_strings();
486
487     return 1;
488 }
489
490 /*
491  * (de)initialisation functions. Control Function
492  */
493 static int zencod_init(ENGINE *e)
494 {
495
496     t_zencod_test *ptr_0;
497     t_zencod_bytes2bits *ptr_1;
498     t_zencod_bits2bytes *ptr_2;
499     t_zencod_new_number *ptr_3;
500     t_zencod_init_number *ptr_4;
501     t_zencod_rsa_mod_exp *ptr_exp_1;
502     t_zencod_rsa_mod_exp_crt *ptr_exp_2;
503     t_zencod_dsa_do_sign *ptr_dsa_1;
504     t_zencod_dsa_do_verify *ptr_dsa_2;
505     t_zencod_dh_generate_key *ptr_dh_1;
506     t_zencod_dh_compute_key *ptr_dh_2;
507     t_zencod_rand_bytes *ptr_rand_1;
508     t_zencod_math_mod_exp *ptr_math_1;
509     t_zencod_md5_init *ptr_md5_1;
510     t_zencod_md5_update *ptr_md5_2;
511     t_zencod_md5_do_final *ptr_md5_3;
512     t_zencod_sha1_init *ptr_sha1_1;
513     t_zencod_sha1_update *ptr_sha1_2;
514     t_zencod_sha1_do_final *ptr_sha1_3;
515     t_zencod_xdes_cipher *ptr_xdes_1;
516     t_zencod_rc4_cipher *ptr_rc4_1;
517
518     CHEESE();
519
520     /*
521      * We Should add some tests for non NULL parameters or bad value !!
522      * Stuff to be done ...
523      */
524
525     if (zencod_dso != NULL) {
526         ZENCODerr(ZENCOD_F_ZENCOD_INIT, ZENCOD_R_ALREADY_LOADED);
527         goto err;
528     }
529     /*
530      * Trying to load the Library "cryptozen"
531      */
532     zencod_dso = DSO_load(NULL, ZENCOD_LIBNAME, NULL, 0);
533     if (zencod_dso == NULL) {
534         ZENCODerr(ZENCOD_F_ZENCOD_INIT, ZENCOD_R_DSO_FAILURE);
535         goto err;
536     }
537
538     /*
539      * Trying to load Function from the Library
540      */
541 #define BINDIT(t, name) (t*)DSO_bindfunc(zencod_dso, name)
542     if ((ptr_1 = BINDIT(t_zencod_bytes2bits ZENCOD_Fct_1)) == NULL
543     || (ptr_2 = BINDIT(t_zencod_bits2bytes ZENCOD_Fct_2)) == NULL
544     || (ptr_3 = BINDIT(t_zencod_new_number ZENCOD_Fct_3)) == NULL
545     || (ptr_4 = BINDIT(t_zencod_init_number ZENCOD_Fct_4)) == NULL
546     || (ptr_exp_1 = BINDIT(t_zencod_rsa_mod_exp, ZENCOD_Fct_exp_1)) == NULL
547     || (ptr_exp_2 = BINDIT(t_zencod_rsa_mod_exp_crt, ZENCOD_Fct_exp_2)) == NULL
548     || (ptr_dsa_1 = BINDIT(t_zencod_dsa_do_sign, ZENCOD_Fct_dsa_1)) == NULL
549     || (ptr_dsa_2 = BINDIT(t_zencod_dsa_do_verify, ZENCOD_Fct_dsa_2)) == NULL
550     || (ptr_dh_1 = BINDIT(t_zencod_dh_generate_key, ZENCOD_Fct_dh_1)) == NULL
551     || (ptr_dh_2 = BINDIT(t_zencod_dh_compute_key, ZENCOD_Fct_dh_2)) == NULL
552     || (ptr_rand_1 = BINDIT(t_zencod_rand_bytes, ZENCOD_Fct_rand_1)) == NULL
553     || (ptr_math_1 = BINDIT(t_zencod_math_mod_exp, ZENCOD_Fct_math_1)) == NULL
554     || (ptr_0 = BINDIT(t_zencod_test, ZENCOD_Fct_0)) == NULL
555     || (ptr_md5_1 = BINDIT(t_zencod_md5_init, ZENCOD_Fct_md5_1)) == NULL
556     || (ptr_md5_2 = BINDIT(t_zencod_md5_update, ZENCOD_Fct_md5_2)) == NULL
557     || (ptr_md5_3 = BINDIT(t_zencod_md5_do_final, ZENCOD_Fct_md5_3)) == NULL
558     || (ptr_sha1_1 = BINDIT(t_zencod_sha1_init, ZENCOD_Fct_sha1_1)) == NULL
559     || (ptr_sha1_2 = BINDIT(t_zencod_sha1_update, ZENCOD_Fct_sha1_2)) == NULL
560     || (ptr_sha1_3 = BINDIT(t_zencod_sha1_do_final, ZENCOD_Fct_sha1_3)) == NULL
561     || (ptr_xdes_1 = BINDIT(t_zencod_xdes_cipher, ZENCOD_Fct_xdes_1)) == NULL
562     || (ptr_rc4_1 = BINDIT(t_zencod_rc4_cipher, ZENCOD_Fct_rc4_1)) == NULL) {
563         ZENCODerr(ZENCOD_F_ZENCOD_INIT, ZENCOD_R_DSO_FAILURE);
564         goto err;
565     }
566
567     /*
568      * The function from "cryptozen" Library have been correctly loaded so
569      * copy them
570      */
571     ptr_zencod_test = ptr_0;
572     ptr_zencod_bytes2bits = ptr_1;
573     ptr_zencod_bits2bytes = ptr_2;
574     ptr_zencod_new_number = ptr_3;
575     ptr_zencod_init_number = ptr_4;
576     ptr_zencod_rsa_mod_exp = ptr_exp_1;
577     ptr_zencod_rsa_mod_exp_crt = ptr_exp_2;
578     ptr_zencod_dsa_do_sign = ptr_dsa_1;
579     ptr_zencod_dsa_do_verify = ptr_dsa_2;
580     ptr_zencod_dh_generate_key = ptr_dh_1;
581     ptr_zencod_dh_compute_key = ptr_dh_2;
582     ptr_zencod_rand_bytes = ptr_rand_1;
583     ptr_zencod_math_mod_exp = ptr_math_1;
584     ptr_zencod_test = ptr_0;
585     ptr_zencod_md5_init = ptr_md5_1;
586     ptr_zencod_md5_update = ptr_md5_2;
587     ptr_zencod_md5_do_final = ptr_md5_3;
588     ptr_zencod_sha1_init = ptr_sha1_1;
589     ptr_zencod_sha1_update = ptr_sha1_2;
590     ptr_zencod_sha1_do_final = ptr_sha1_3;
591     ptr_zencod_xdes_cipher = ptr_xdes_1;
592     ptr_zencod_rc4_cipher = ptr_rc4_1;
593
594     /*
595      * We should peform a test to see if there is actually any unit runnig on
596      * the system ... Even if the cryptozen library is loaded the module coul
597      * not be loaded on the system ... For now we may just open and close the
598      * device !!
599      */
600
601     if (ptr_zencod_test() != 0) {
602         ZENCODerr(ZENCOD_F_ZENCOD_INIT, ZENCOD_R_UNIT_FAILURE);
603         goto err;
604     }
605
606     return 1;
607  err:
608     DSO_free(zencod_dso);
609     zencod_dso = NULL;
610     ptr_zencod_bytes2bits = NULL;
611     ptr_zencod_bits2bytes = NULL;
612     ptr_zencod_new_number = NULL;
613     ptr_zencod_init_number = NULL;
614     ptr_zencod_rsa_mod_exp = NULL;
615     ptr_zencod_rsa_mod_exp_crt = NULL;
616     ptr_zencod_dsa_do_sign = NULL;
617     ptr_zencod_dsa_do_verify = NULL;
618     ptr_zencod_dh_generate_key = NULL;
619     ptr_zencod_dh_compute_key = NULL;
620     ptr_zencod_rand_bytes = NULL;
621     ptr_zencod_math_mod_exp = NULL;
622     ptr_zencod_test = NULL;
623     ptr_zencod_md5_init = NULL;
624     ptr_zencod_md5_update = NULL;
625     ptr_zencod_md5_do_final = NULL;
626     ptr_zencod_sha1_init = NULL;
627     ptr_zencod_sha1_update = NULL;
628     ptr_zencod_sha1_do_final = NULL;
629     ptr_zencod_xdes_cipher = NULL;
630     ptr_zencod_rc4_cipher = NULL;
631
632     return 0;
633 }
634
635 static int zencod_finish(ENGINE *e)
636 {
637
638     CHEESE();
639
640     /*
641      * We Should add some tests for non NULL parameters or bad value !!
642      * Stuff to be done ...
643      */
644     if (zencod_dso == NULL) {
645         ZENCODerr(ZENCOD_F_ZENCOD_FINISH, ZENCOD_R_NOT_LOADED);
646         return 0;
647     }
648     if (!DSO_free(zencod_dso)) {
649         ZENCODerr(ZENCOD_F_ZENCOD_FINISH, ZENCOD_R_DSO_FAILURE);
650         return 0;
651     }
652
653     zencod_dso = NULL;
654
655     ptr_zencod_bytes2bits = NULL;
656     ptr_zencod_bits2bytes = NULL;
657     ptr_zencod_new_number = NULL;
658     ptr_zencod_init_number = NULL;
659     ptr_zencod_rsa_mod_exp = NULL;
660     ptr_zencod_rsa_mod_exp_crt = NULL;
661     ptr_zencod_dsa_do_sign = NULL;
662     ptr_zencod_dsa_do_verify = NULL;
663     ptr_zencod_dh_generate_key = NULL;
664     ptr_zencod_dh_compute_key = NULL;
665     ptr_zencod_rand_bytes = NULL;
666     ptr_zencod_math_mod_exp = NULL;
667     ptr_zencod_test = NULL;
668     ptr_zencod_md5_init = NULL;
669     ptr_zencod_md5_update = NULL;
670     ptr_zencod_md5_do_final = NULL;
671     ptr_zencod_sha1_init = NULL;
672     ptr_zencod_sha1_update = NULL;
673     ptr_zencod_sha1_do_final = NULL;
674     ptr_zencod_xdes_cipher = NULL;
675     ptr_zencod_rc4_cipher = NULL;
676
677     return 1;
678 }
679
680 static int zencod_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
681 {
682
683     int initialised = ((zencod_dso == NULL) ? 0 : 1);
684
685     CHEESE();
686
687     /*
688      * We Should add some tests for non NULL parameters or bad value !!
689      * Stuff to be done ...
690      */
691     switch (cmd) {
692     case ZENCOD_CMD_SO_PATH:
693         if (p == NULL) {
694             ZENCODerr(ZENCOD_F_ZENCOD_CTRL, ERR_R_PASSED_NULL_PARAMETER);
695             return 0;
696         }
697         if (initialised) {
698             ZENCODerr(ZENCOD_F_ZENCOD_CTRL, ZENCOD_R_ALREADY_LOADED);
699             return 0;
700         }
701         ZENCOD_LIBNAME = (const char *)p;
702         return 1;
703     default:
704         break;
705     }
706
707     ZENCODerr(ZENCOD_F_ZENCOD_CTRL, ZENCOD_R_CTRL_COMMAND_NOT_IMPLEMENTED);
708
709     return 0;
710 }
711
712 /*
713  * BIGNUM stuff Functions
714  */
715 static int zencod_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
716                              const BIGNUM *m, BN_CTX *ctx)
717 {
718     zen_nb_t y, x, e, n;
719     int ret;
720
721     CHEESE();
722
723     if (!zencod_dso) {
724         ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_NOT_LOADED);
725         return 0;
726     }
727
728     if (!bn_wexpand(r, m->top + 1)) {
729         ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_BN_EXPAND_FAIL);
730         return 0;
731     }
732
733     memset(r->d, 0, BN_num_bytes(m));
734
735     ptr_zencod_init_number(&y, (r->dmax - 1) * sizeof(BN_ULONG) * 8,
736                            (unsigned char *)r->d);
737     BIGNUM2ZEN(&x, a);
738     BIGNUM2ZEN(&e, p);
739     BIGNUM2ZEN(&n, m);
740
741     /* Must invert x and e parameter due to BN mod exp prototype ... */
742     ret = ptr_zencod_math_mod_exp(&y, &e, &x, &n);
743
744     if (ret) {
745         PERROR("zenbridge_math_mod_exp");
746         ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_REQUEST_FAILED);
747         return 0;
748     }
749
750     r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2;
751
752     return 1;
753 }
754
755 /*
756  * RSA stuff Functions
757  */
758 #  ifndef OPENSSL_NO_RSA
759 static int RSA_zencod_rsa_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa)
760 {
761
762     CHEESE();
763
764     if (!zencod_dso) {
765         ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT, ZENCOD_R_NOT_LOADED);
766         return 0;
767     }
768
769     if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
770         ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT,
771                   ZENCOD_R_BAD_KEY_COMPONENTS);
772         return 0;
773     }
774
775     /* Do in software if argument is too large for hardware */
776     if (RSA_size(rsa) * 8 > ZENBRIDGE_MAX_KEYSIZE_RSA_CRT) {
777         const RSA_METHOD *meth;
778
779         meth = RSA_PKCS1_SSLeay();
780         return meth->rsa_mod_exp(r0, i, rsa);
781     } else {
782         zen_nb_t y, x, p, q, dmp1, dmq1, iqmp;
783
784         if (!bn_expand(r0, RSA_size(rsa) * 8)) {
785             ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT,
786                       ZENCOD_R_BN_EXPAND_FAIL);
787             return 0;
788         }
789         r0->top = (RSA_size(rsa) * 8 + BN_BITS2 - 1) / BN_BITS2;
790
791         BIGNUM2ZEN(&x, i);
792         BIGNUM2ZEN(&y, r0);
793         BIGNUM2ZEN(&p, rsa->p);
794         BIGNUM2ZEN(&q, rsa->q);
795         BIGNUM2ZEN(&dmp1, rsa->dmp1);
796         BIGNUM2ZEN(&dmq1, rsa->dmq1);
797         BIGNUM2ZEN(&iqmp, rsa->iqmp);
798
799         if (ptr_zencod_rsa_mod_exp_crt(&y, &x, &p, &q, &dmp1, &dmq1, &iqmp) <
800             0) {
801             PERROR("zenbridge_rsa_mod_exp_crt");
802             ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT,
803                       ZENCOD_R_REQUEST_FAILED);
804             return 0;
805         }
806
807         return 1;
808     }
809 }
810
811 /*
812  * This function is aliased to RSA_mod_exp (with the mont stuff dropped).
813  */
814 static int RSA_zencod_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
815                                  const BIGNUM *m, BN_CTX *ctx,
816                                  BN_MONT_CTX *m_ctx)
817 {
818
819     CHEESE();
820
821     if (!zencod_dso) {
822         ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_NOT_LOADED);
823         return 0;
824     }
825
826     /* Do in software if argument is too large for hardware */
827     if (BN_num_bits(m) > ZENBRIDGE_MAX_KEYSIZE_RSA) {
828         const RSA_METHOD *meth;
829
830         meth = RSA_PKCS1_SSLeay();
831         return meth->bn_mod_exp(r, a, p, m, ctx, m_ctx);
832     } else {
833         zen_nb_t y, x, e, n;
834
835         if (!bn_expand(r, BN_num_bits(m))) {
836             ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_BN_EXPAND_FAIL);
837             return 0;
838         }
839         r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2;
840
841         BIGNUM2ZEN(&x, a);
842         BIGNUM2ZEN(&y, r);
843         BIGNUM2ZEN(&e, p);
844         BIGNUM2ZEN(&n, m);
845
846         if (ptr_zencod_rsa_mod_exp(&y, &x, &n, &e) < 0) {
847             PERROR("zenbridge_rsa_mod_exp");
848             ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_REQUEST_FAILED);
849             return 0;
850         }
851
852         return 1;
853     }
854 }
855 #  endif                        /* !OPENSSL_NO_RSA */
856
857 #  ifndef OPENSSL_NO_DSA
858 /*
859  * DSA stuff Functions
860  */
861 static DSA_SIG *DSA_zencod_do_sign(const unsigned char *dgst, int dlen,
862                                    DSA *dsa)
863 {
864     zen_nb_t p, q, g, x, y, r, s, data;
865     DSA_SIG *sig;
866     BIGNUM *bn_r = NULL;
867     BIGNUM *bn_s = NULL;
868     char msg[20];
869
870     CHEESE();
871
872     if (!zencod_dso) {
873         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_NOT_LOADED);
874         goto FAILED;
875     }
876
877     if (dlen > 160) {
878         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
879         goto FAILED;
880     }
881
882     /* Do in software if argument is too large for hardware */
883     if (BN_num_bits(dsa->p) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN ||
884         BN_num_bits(dsa->g) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN) {
885         const DSA_METHOD *meth;
886         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
887         meth = DSA_OpenSSL();
888         return meth->dsa_do_sign(dgst, dlen, dsa);
889     }
890
891     if ((bn_s = BN_new()) == NULL || (bn_r = BN_new()) == NULL) {
892         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
893         goto FAILED;
894     }
895
896     if (!bn_expand(bn_r, 160) || !bn_expand(bn_s, 160)) {
897         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BN_EXPAND_FAIL);
898         goto FAILED;
899     }
900
901     bn_r->top = bn_s->top = (160 + BN_BITS2 - 1) / BN_BITS2;
902     BIGNUM2ZEN(&p, dsa->p);
903     BIGNUM2ZEN(&q, dsa->q);
904     BIGNUM2ZEN(&g, dsa->g);
905     BIGNUM2ZEN(&x, dsa->priv_key);
906     BIGNUM2ZEN(&y, dsa->pub_key);
907     BIGNUM2ZEN(&r, bn_r);
908     BIGNUM2ZEN(&s, bn_s);
909     q.len = x.len = 160;
910
911     ypcmem(msg, dgst, 20);
912     ptr_zencod_init_number(&data, 160, msg);
913
914     if (ptr_zencod_dsa_do_sign(0, &data, &y, &p, &q, &g, &x, &r, &s) < 0) {
915         PERROR("zenbridge_dsa_do_sign");
916         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
917         goto FAILED;
918     }
919
920     if ((sig = DSA_SIG_new()) == NULL) {
921         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
922         goto FAILED;
923     }
924     sig->r = bn_r;
925     sig->s = bn_s;
926     return sig;
927
928  FAILED:
929     BN_free(bn_r);
930     BN_free(bn_s);
931     return NULL;
932 }
933
934 static int DSA_zencod_do_verify(const unsigned char *dgst, int dlen,
935                                 DSA_SIG *sig, DSA *dsa)
936 {
937     zen_nb_t data, p, q, g, y, r, s, v;
938     char msg[20];
939     char v_data[20];
940     int ret;
941
942     CHEESE();
943
944     if (!zencod_dso) {
945         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY, ZENCOD_R_NOT_LOADED);
946         return 0;
947     }
948
949     if (dlen > 160) {
950         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
951         return 0;
952     }
953
954     /* Do in software if argument is too large for hardware */
955     if (BN_num_bits(dsa->p) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN ||
956         BN_num_bits(dsa->g) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN) {
957         const DSA_METHOD *meth;
958         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
959         meth = DSA_OpenSSL();
960         return meth->dsa_do_verify(dgst, dlen, sig, dsa);
961     }
962
963     BIGNUM2ZEN(&p, dsa->p);
964     BIGNUM2ZEN(&q, dsa->q);
965     BIGNUM2ZEN(&g, dsa->g);
966     BIGNUM2ZEN(&y, dsa->pub_key);
967     BIGNUM2ZEN(&r, sig->r);
968     BIGNUM2ZEN(&s, sig->s);
969     ptr_zencod_init_number(&v, 160, v_data);
970     ypcmem(msg, dgst, 20);
971     ptr_zencod_init_number(&data, 160, msg);
972
973     if ((ret =
974          ptr_zencod_dsa_do_verify(0, &data, &p, &q, &g, &y, &r, &s,
975                                   &v)) < 0) {
976         PERROR("zenbridge_dsa_do_verify");
977         ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY, ZENCOD_R_REQUEST_FAILED);
978         return 0;
979     }
980
981     return ((ret == 0) ? 1 : ret);
982 }
983
984 static int DSA_zencod_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
985                                  const BIGNUM *p, const BIGNUM *m,
986                                  BN_CTX *ctx, BN_MONT_CTX *m_ctx)
987 {
988     CHEESE();
989
990     return zencod_bn_mod_exp(r, a, p, m, ctx);
991 }
992 #  endif                        /* !OPENSSL_NO_DSA */
993
994 #  ifndef OPENSSl_NO_DH
995 /*
996  * DH stuff Functions
997  */
998 static int DH_zencod_generate_key(DH *dh)
999 {
1000     BIGNUM *bn_prv = NULL;
1001     BIGNUM *bn_pub = NULL;
1002     zen_nb_t y, x, g, p;
1003     int generate_x;
1004
1005     CHEESE();
1006
1007     if (!zencod_dso) {
1008         ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_NOT_LOADED);
1009         return 0;
1010     }
1011
1012     /* Private key */
1013     if (dh->priv_key) {
1014         bn_prv = dh->priv_key;
1015         generate_x = 0;
1016     } else {
1017         if ((bn_prv = BN_new()) == NULL) {
1018             ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
1019             goto FAILED;
1020         }
1021         generate_x = 1;
1022     }
1023
1024     /* Public key */
1025     if (dh->pub_key)
1026         bn_pub = dh->pub_key;
1027     else if ((bn_pub = BN_new()) == NULL) {
1028         ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
1029         goto FAILED;
1030     }
1031
1032     /* Expand */
1033     if (!bn_wexpand(bn_prv, dh->p->dmax) || !bn_wexpand(bn_pub, dh->p->dmax)) {
1034         ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
1035         goto FAILED;
1036     }
1037     bn_prv->top = dh->p->top;
1038     bn_pub->top = dh->p->top;
1039
1040     /* Convert all keys */
1041     BIGNUM2ZEN(&p, dh->p);
1042     BIGNUM2ZEN(&g, dh->g);
1043     BIGNUM2ZEN(&y, bn_pub);
1044     BIGNUM2ZEN(&x, bn_prv);
1045     x.len = DH_size(dh) * 8;
1046
1047     /* Adjust the lengths of P and G */
1048     p.len = ptr_zencod_bytes2bits(p.data, ZEN_BYTES(p.len));
1049     g.len = ptr_zencod_bytes2bits(g.data, ZEN_BYTES(g.len));
1050
1051     /* Send the request to the driver */
1052     if (ptr_zencod_dh_generate_key(&y, &x, &g, &p, generate_x) < 0) {
1053         perror("zenbridge_dh_generate_key");
1054         ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_REQUEST_FAILED);
1055         goto FAILED;
1056     }
1057
1058     dh->priv_key = bn_prv;
1059     dh->pub_key = bn_pub;
1060
1061     return 1;
1062
1063  FAILED:
1064     if (!dh->priv_key)
1065         BN_free(bn_prv);
1066     if (!dh->pub_key)
1067         BN_free(bn_pub);
1068
1069     return 0;
1070 }
1071
1072 static int DH_zencod_compute_key(unsigned char *key, const BIGNUM *pub_key,
1073                                  DH *dh)
1074 {
1075     zen_nb_t y, x, p, k;
1076
1077     CHEESE();
1078
1079     if (!zencod_dso) {
1080         ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_NOT_LOADED);
1081         return 0;
1082     }
1083
1084     if (!dh->priv_key) {
1085         ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_BAD_KEY_COMPONENTS);
1086         return 0;
1087     }
1088
1089     /* Convert all keys */
1090     BIGNUM2ZEN(&y, pub_key);
1091     BIGNUM2ZEN(&x, dh->priv_key);
1092     BIGNUM2ZEN(&p, dh->p);
1093     ptr_zencod_init_number(&k, p.len, key);
1094
1095     /* Adjust the lengths */
1096     p.len = ptr_zencod_bytes2bits(p.data, ZEN_BYTES(p.len));
1097     y.len = ptr_zencod_bytes2bits(y.data, ZEN_BYTES(y.len));
1098     x.len = ptr_zencod_bytes2bits(x.data, ZEN_BYTES(x.len));
1099
1100     /* Call the hardware */
1101     if (ptr_zencod_dh_compute_key(&k, &y, &x, &p) < 0) {
1102         ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_REQUEST_FAILED);
1103         return 0;
1104     }
1105
1106     /* The key must be written MSB -> LSB */
1107     k.len = ptr_zencod_bytes2bits(k.data, ZEN_BYTES(k.len));
1108     esrever(key, ZEN_BYTES(k.len));
1109
1110     return ZEN_BYTES(k.len);
1111 }
1112
1113 static int DH_zencod_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a,
1114                                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1115                                 BN_MONT_CTX *m_ctx)
1116 {
1117     CHEESE();
1118
1119     return zencod_bn_mod_exp(r, a, p, m, ctx);
1120 }
1121 #  endif                        /* !OPENSSL_NO_DH */
1122
1123 /*
1124  * RAND stuff Functions
1125  */
1126 static void RAND_zencod_seed(const void *buf, int num)
1127 {
1128     /*
1129      * Nothing to do cause our crypto accelerator provide a true random
1130      * generator
1131      */
1132 }
1133
1134 static int RAND_zencod_rand_bytes(unsigned char *buf, int num)
1135 {
1136     zen_nb_t r;
1137
1138     CHEESE();
1139
1140     if (!zencod_dso) {
1141         ENGINEerr(ZENCOD_F_ZENCOD_RAND, ZENCOD_R_NOT_LOADED);
1142         return 0;
1143     }
1144
1145     ptr_zencod_init_number(&r, num * 8, buf);
1146
1147     if (ptr_zencod_rand_bytes(&r, ZENBRIDGE_RNG_DIRECT) < 0) {
1148         PERROR("zenbridge_rand_bytes");
1149         ENGINEerr(ZENCOD_F_ZENCOD_RAND, ZENCOD_R_REQUEST_FAILED);
1150         return 0;
1151     }
1152
1153     return 1;
1154 }
1155
1156 static int RAND_zencod_rand_status(void)
1157 {
1158     CHEESE();
1159
1160     return 1;
1161 }
1162
1163 /*
1164  * This stuff is needed if this ENGINE is being compiled into a
1165  * self-contained shared-library.
1166  */
1167 #  ifdef ENGINE_DYNAMIC_SUPPORT
1168 static int bind_fn(ENGINE *e, const char *id)
1169 {
1170
1171     if (id && (strcmp(id, engine_zencod_id) != 0)) {
1172         return 0;
1173     }
1174     if (!bind_helper(e)) {
1175         return 0;
1176     }
1177
1178     return 1;
1179 }
1180
1181 IMPLEMENT_DYNAMIC_CHECK_FN()
1182     IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1183 #  endif                        /* ENGINE_DYNAMIC_SUPPORT */
1184     /*
1185      * Adding "Digest" and "Cipher" tools ...
1186      * This is in development ... ;-)
1187      * In orfer to code this, i refer to hw_openbsd_dev_crypto and openssl engine made by Geoff Thorpe (if i'm rigth),
1188      * and evp, sha md5 definitions etc ...
1189      */
1190 /* First add some include ... */
1191 #  include <openssl/evp.h>
1192 #  include <openssl/sha.h>
1193 #  include <openssl/md5.h>
1194 #  include <openssl/rc4.h>
1195 #  include <openssl/des.h>
1196 /* Some variables declaration ... */
1197     /*
1198      * DONS: Disable symetric computation except DES and 3DES, but let part
1199      * of the code
1200      */
1201 /* static int engine_digest_nids [ ] = { NID_sha1, NID_md5 } ; */
1202 static int engine_digest_nids[] = { };
1203
1204 static int engine_digest_nids_num = 0;
1205 /*
1206  * static int engine_cipher_nids [ ] = { NID_rc4, NID_rc4_40, NID_des_cbc,
1207  * NID_des_ede3_cbc } ;
1208  */
1209 static int engine_cipher_nids[] = { NID_des_cbc, NID_des_ede3_cbc };
1210
1211 static int engine_cipher_nids_num = 2;
1212
1213 /* Function prototype ... */
1214 /*  SHA stuff */
1215 static int engine_sha1_init(EVP_MD_CTX *ctx);
1216 static int engine_sha1_update(EVP_MD_CTX *ctx, const void *data,
1217                               unsigned long count);
1218 static int engine_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
1219
1220 /*  MD5 stuff */
1221 static int engine_md5_init(EVP_MD_CTX *ctx);
1222 static int engine_md5_update(EVP_MD_CTX *ctx, const void *data,
1223                              unsigned long count);
1224 static int engine_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
1225
1226 static int engine_md_cleanup(EVP_MD_CTX *ctx);
1227 static int engine_md_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
1228
1229 /* RC4 Stuff */
1230 static int engine_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1231                                const unsigned char *iv, int enc);
1232 static int engine_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1233                              const unsigned char *in, unsigned int inl);
1234
1235 /* DES Stuff */
1236 static int engine_des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1237                                const unsigned char *iv, int enc);
1238 static int engine_des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1239                                  const unsigned char *in, unsigned int inl);
1240
1241 /*  3DES Stuff */
1242 static int engine_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
1243                                     const unsigned char *key,
1244                                     const unsigned char *iv, int enc);
1245 static int engine_des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1246                                       const unsigned char *in,
1247                                       unsigned int inl);
1248
1249 static int engine_cipher_cleanup(EVP_CIPHER_CTX *ctx); /* cleanup ctx */
1250
1251 /* The one for SHA ... */
1252 static const EVP_MD engine_sha1_md = {
1253     NID_sha1,
1254     NID_sha1WithRSAEncryption,
1255     SHA_DIGEST_LENGTH,
1256     EVP_MD_FLAG_ONESHOT,
1257     /*
1258      * 0,
1259      *//*
1260      * EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block *
1261      * XXX: set according to device info ...
1262      */
1263     engine_sha1_init,
1264     engine_sha1_update,
1265     engine_sha1_final,
1266     engine_md_copy,             /* dev_crypto_sha_copy */
1267     engine_md_cleanup,          /* dev_crypto_sha_cleanup */
1268     EVP_PKEY_RSA_method,
1269     SHA_CBLOCK,
1270     /* sizeof ( EVP_MD * ) + sizeof ( SHA_CTX ) */
1271     sizeof(ZEN_MD_DATA)
1272         /*
1273          * sizeof ( MD_CTX_DATA ) The message digest data structure ...
1274          */
1275 };
1276
1277 /* The one for MD5 ... */
1278 static const EVP_MD engine_md5_md = {
1279     NID_md5,
1280     NID_md5WithRSAEncryption,
1281     MD5_DIGEST_LENGTH,
1282     EVP_MD_FLAG_ONESHOT,
1283     /*
1284      * 0,
1285      *//*
1286      * EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block *
1287      * XXX: set according to device info ...
1288      */
1289     engine_md5_init,
1290     engine_md5_update,
1291     engine_md5_final,
1292     engine_md_copy,             /* dev_crypto_md5_copy */
1293     engine_md_cleanup,          /* dev_crypto_md5_cleanup */
1294     EVP_PKEY_RSA_method,
1295     MD5_CBLOCK,
1296     /* sizeof ( EVP_MD * ) + sizeof ( MD5_CTX ) */
1297     sizeof(ZEN_MD_DATA)
1298         /*
1299          * sizeof ( MD_CTX_DATA ) The message digest data structure ...
1300          */
1301 };
1302
1303 /* The one for RC4 ... */
1304 #  define EVP_RC4_KEY_SIZE                        16
1305
1306 /* Try something static ... */
1307 typedef struct {
1308     unsigned int len;
1309     unsigned int first;
1310     unsigned char rc4_state[260];
1311 } NEW_ZEN_RC4_KEY;
1312
1313 #  define rc4_data(ctx)                           ( (EVP_RC4_KEY *) ( ctx )->cipher_data )
1314
1315 static const EVP_CIPHER engine_rc4 = {
1316     NID_rc4,
1317     1,
1318     16,                         /* EVP_RC4_KEY_SIZE should be 128 bits */
1319     0,                          /* FIXME: key should be up to 256 bytes */
1320     EVP_CIPH_VARIABLE_LENGTH,
1321     engine_rc4_init_key,
1322     engine_rc4_cipher,
1323     engine_cipher_cleanup,
1324     sizeof(NEW_ZEN_RC4_KEY),
1325     NULL,
1326     NULL,
1327     NULL
1328 };
1329
1330 /* The one for RC4_40 ... */
1331 static const EVP_CIPHER engine_rc4_40 = {
1332     NID_rc4_40,
1333     1,
1334     5,                          /* 40 bits */
1335     0,
1336     EVP_CIPH_VARIABLE_LENGTH,
1337     engine_rc4_init_key,
1338     engine_rc4_cipher,
1339     engine_cipher_cleanup,
1340     sizeof(NEW_ZEN_RC4_KEY),
1341     NULL,
1342     NULL,
1343     NULL
1344 };
1345
1346 /* The one for DES ... */
1347
1348 /* Try something static ... */
1349 typedef struct {
1350     unsigned char des_key[24];
1351     unsigned char des_iv[8];
1352 } ZEN_DES_KEY;
1353
1354 static const EVP_CIPHER engine_des_cbc = {
1355     NID_des_cbc,
1356     8, 8, 8,
1357     0 | EVP_CIPH_CBC_MODE,
1358     engine_des_init_key,
1359     engine_des_cbc_cipher,
1360     engine_cipher_cleanup,
1361     sizeof(ZEN_DES_KEY),
1362     EVP_CIPHER_set_asn1_iv,
1363     EVP_CIPHER_get_asn1_iv,
1364     NULL,
1365     NULL
1366 };
1367
1368 /* The one for 3DES ... */
1369
1370 /* Try something static ... */
1371 typedef struct {
1372     unsigned char des3_key[24];
1373     unsigned char des3_iv[8];
1374 } ZEN_3DES_KEY;
1375
1376 #  define des_data(ctx)                            ( (DES_EDE_KEY *) ( ctx )->cipher_data )
1377
1378 static const EVP_CIPHER engine_des_ede3_cbc = {
1379     NID_des_ede3_cbc,
1380     8, 8, 8,
1381     0 | EVP_CIPH_CBC_MODE,
1382     engine_des_ede3_init_key,
1383     engine_des_ede3_cbc_cipher,
1384     engine_cipher_cleanup,
1385     sizeof(ZEN_3DES_KEY),
1386     EVP_CIPHER_set_asn1_iv,
1387     EVP_CIPHER_get_asn1_iv,
1388     NULL,
1389     NULL
1390 };
1391
1392 /* General function cloned on hw_openbsd_dev_crypto one ... */
1393 static int engine_digests(ENGINE *e, const EVP_MD **digest, const int **nids,
1394                           int nid)
1395 {
1396
1397 #  ifdef DEBUG_ZENCOD_MD
1398     fprintf(stderr, "\t=>Function : static int engine_digests () called !\n");
1399 #  endif
1400
1401     if (!digest) {
1402         /* We are returning a list of supported nids */
1403         *nids = engine_digest_nids;
1404         return engine_digest_nids_num;
1405     }
1406     /* We are being asked for a specific digest */
1407     if (nid == NID_md5) {
1408         *digest = &engine_md5_md;
1409     } else if (nid == NID_sha1) {
1410         *digest = &engine_sha1_md;
1411     } else {
1412         *digest = NULL;
1413         return 0;
1414     }
1415     return 1;
1416 }
1417
1418 /*
1419  * SHA stuff Functions
1420  */
1421 static int engine_sha1_init(EVP_MD_CTX *ctx)
1422 {
1423
1424     int to_return = 0;
1425
1426     /* Test with zenbridge library ... */
1427     to_return = ptr_zencod_sha1_init((ZEN_MD_DATA *)ctx->md_data);
1428     to_return = !to_return;
1429
1430     return to_return;
1431 }
1432
1433 static int engine_sha1_update(EVP_MD_CTX *ctx, const void *data,
1434                               unsigned long count)
1435 {
1436
1437     zen_nb_t input;
1438     int to_return = 0;
1439
1440     /* Convert parameters ... */
1441     input.len = count;
1442     input.data = (unsigned char *)data;
1443
1444     /* Test with zenbridge library ... */
1445     to_return =
1446         ptr_zencod_sha1_update((ZEN_MD_DATA *)ctx->md_data,
1447                                (const zen_nb_t *)&input);
1448     to_return = !to_return;
1449
1450     return to_return;
1451 }
1452
1453 static int engine_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
1454 {
1455
1456     zen_nb_t output;
1457     int to_return = 0;
1458
1459     /* Convert parameters ... */
1460     output.len = SHA_DIGEST_LENGTH;
1461     output.data = md;
1462
1463     /* Test with zenbridge library ... */
1464     to_return =
1465         ptr_zencod_sha1_do_final((ZEN_MD_DATA *)ctx->md_data,
1466                                  (zen_nb_t *) & output);
1467     to_return = !to_return;
1468
1469     return to_return;
1470 }
1471
1472 /*
1473  * MD5 stuff Functions
1474  */
1475 static int engine_md5_init(EVP_MD_CTX *ctx)
1476 {
1477
1478     int to_return = 0;
1479
1480     /* Test with zenbridge library ... */
1481     to_return = ptr_zencod_md5_init((ZEN_MD_DATA *)ctx->md_data);
1482     to_return = !to_return;
1483
1484     return to_return;
1485 }
1486
1487 static int engine_md5_update(EVP_MD_CTX *ctx, const void *data,
1488                              unsigned long count)
1489 {
1490
1491     zen_nb_t input;
1492     int to_return = 0;
1493
1494     /* Convert parameters ... */
1495     input.len = count;
1496     input.data = (unsigned char *)data;
1497
1498     /* Test with zenbridge library ... */
1499     to_return =
1500         ptr_zencod_md5_update((ZEN_MD_DATA *)ctx->md_data,
1501                               (const zen_nb_t *)&input);
1502     to_return = !to_return;
1503
1504     return to_return;
1505 }
1506
1507 static int engine_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
1508 {
1509
1510     zen_nb_t output;
1511     int to_return = 0;
1512
1513     /* Convert parameters ... */
1514     output.len = MD5_DIGEST_LENGTH;
1515     output.data = md;
1516
1517     /* Test with zenbridge library ... */
1518     to_return =
1519         ptr_zencod_md5_do_final((ZEN_MD_DATA *)ctx->md_data,
1520                                 (zen_nb_t *) & output);
1521     to_return = !to_return;
1522
1523     return to_return;
1524 }
1525
1526 static int engine_md_cleanup(EVP_MD_CTX *ctx)
1527 {
1528
1529     ZEN_MD_DATA *zen_md_data = (ZEN_MD_DATA *)ctx->md_data;
1530
1531     OPENSSL_free(zen_md_data->HashBuffer);
1532     zen_md_data->HashBufferSize = 0;
1533     ctx->md_data = NULL;
1534     return 1;
1535 }
1536
1537 static int engine_md_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
1538 {
1539     const ZEN_MD_DATA *from_md = (ZEN_MD_DATA *)from->md_data;
1540     ZEN_MD_DATA *to_md = (ZEN_MD_DATA *)to->md_data;
1541
1542     to_md->HashBuffer = OPENSSL_malloc(from_md->HashBufferSize);
1543     if (to_md->HashBuffer == NULL)
1544         return 0;
1545     memcpy(to_md->HashBuffer, from_md->HashBuffer, from_md->HashBufferSize);
1546
1547     return 1;
1548 }
1549
1550 /* General function cloned on hw_openbsd_dev_crypto one ... */
1551 static int engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
1552                           const int **nids, int nid)
1553 {
1554
1555     if (!cipher) {
1556         /* We are returning a list of supported nids */
1557         *nids = engine_cipher_nids;
1558         return engine_cipher_nids_num;
1559     }
1560     /* We are being asked for a specific cipher */
1561     if (nid == NID_rc4) {
1562         *cipher = &engine_rc4;
1563     } else if (nid == NID_rc4_40) {
1564         *cipher = &engine_rc4_40;
1565     } else if (nid == NID_des_cbc) {
1566         *cipher = &engine_des_cbc;
1567     } else if (nid == NID_des_ede3_cbc) {
1568         *cipher = &engine_des_ede3_cbc;
1569     } else {
1570         *cipher = NULL;
1571         return 0;
1572     }
1573
1574     return 1;
1575 }
1576
1577 static int engine_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1578                                const unsigned char *iv, int enc)
1579 {
1580     int to_return = 0;
1581     int i = 0;
1582     int nb = 0;
1583     NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL;
1584
1585     tmp_rc4_key = (NEW_ZEN_RC4_KEY *) (ctx->cipher_data);
1586     tmp_rc4_key->first = 0;
1587     tmp_rc4_key->len = ctx->key_len;
1588     tmp_rc4_key->rc4_state[0] = 0x00;
1589     tmp_rc4_key->rc4_state[2] = 0x00;
1590     nb = 256 / ctx->key_len;
1591     for (i = 0; i < nb; i++) {
1592         memcpy(&(tmp_rc4_key->rc4_state[4 + i * ctx->key_len]), key,
1593                ctx->key_len);
1594     }
1595
1596     to_return = 1;
1597
1598     return to_return;
1599 }
1600
1601 static int engine_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1602                              const unsigned char *in, unsigned int in_len)
1603 {
1604
1605     zen_nb_t output, input;
1606     zen_nb_t rc4key;
1607     int to_return = 0;
1608     NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL;
1609
1610     /* Convert parameters ... */
1611     input.len = in_len;
1612     input.data = (unsigned char *)in;
1613     output.len = in_len;
1614     output.data = (unsigned char *)out;
1615
1616     tmp_rc4_key = ((NEW_ZEN_RC4_KEY *) (ctx->cipher_data));
1617     rc4key.len = 260;
1618     rc4key.data = &(tmp_rc4_key->rc4_state[0]);
1619
1620     /* Test with zenbridge library ... */
1621     to_return =
1622         ptr_zencod_rc4_cipher(&output, &input, (const zen_nb_t *)&rc4key,
1623                               &(tmp_rc4_key->rc4_state[0]),
1624                               &(tmp_rc4_key->rc4_state[3]),
1625                               !tmp_rc4_key->first);
1626     to_return = !to_return;
1627
1628     /* Update encryption state ... */
1629     tmp_rc4_key->first = 1;
1630     tmp_rc4_key = NULL;
1631
1632     return to_return;
1633 }
1634
1635 static int engine_des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1636                                const unsigned char *iv, int enc)
1637 {
1638
1639     ZEN_DES_KEY *tmp_des_key = NULL;
1640     int to_return = 0;
1641
1642     tmp_des_key = (ZEN_DES_KEY *) (ctx->cipher_data);
1643     memcpy(&(tmp_des_key->des_key[0]), key, 8);
1644     memcpy(&(tmp_des_key->des_key[8]), key, 8);
1645     memcpy(&(tmp_des_key->des_key[16]), key, 8);
1646     memcpy(&(tmp_des_key->des_iv[0]), iv, 8);
1647
1648     to_return = 1;
1649
1650     return to_return;
1651 }
1652
1653 static int engine_des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1654                                  const unsigned char *in, unsigned int inl)
1655 {
1656
1657     zen_nb_t output, input;
1658     zen_nb_t deskey_1, deskey_2, deskey_3, iv;
1659     int to_return = 0;
1660
1661     /* Convert parameters ... */
1662     input.len = inl;
1663     input.data = (unsigned char *)in;
1664     output.len = inl;
1665     output.data = out;
1666
1667     /* Set key parameters ... */
1668     deskey_1.len = 8;
1669     deskey_2.len = 8;
1670     deskey_3.len = 8;
1671     deskey_1.data =
1672         (unsigned char *)((ZEN_DES_KEY *) (ctx->cipher_data))->des_key;
1673     deskey_2.data =
1674         (unsigned char *)&((ZEN_DES_KEY *) (ctx->cipher_data))->des_key[8];
1675     deskey_3.data =
1676         (unsigned char *)&((ZEN_DES_KEY *) (ctx->cipher_data))->des_key[16];
1677
1678     /* Key correct iv ... */
1679     memcpy(((ZEN_DES_KEY *) (ctx->cipher_data))->des_iv, ctx->iv, 8);
1680     iv.len = 8;
1681     iv.data = (unsigned char *)((ZEN_DES_KEY *) (ctx->cipher_data))->des_iv;
1682
1683     if (ctx->encrypt == 0) {
1684         memcpy(ctx->iv, &(input.data[input.len - 8]), 8);
1685     }
1686
1687     /* Test with zenbridge library ... */
1688     to_return = ptr_zencod_xdes_cipher(&output, &input,
1689                                        (zen_nb_t *) & deskey_1,
1690                                        (zen_nb_t *) & deskey_2,
1691                                        (zen_nb_t *) & deskey_3, &iv,
1692                                        ctx->encrypt);
1693     to_return = !to_return;
1694
1695     /*
1696      * But we need to set up the rigth iv ... Test ENCRYPT or DECRYPT mode to
1697      * set iv ...
1698      */
1699     if (ctx->encrypt == 1) {
1700         memcpy(ctx->iv, &(output.data[output.len - 8]), 8);
1701     }
1702
1703     return to_return;
1704 }
1705
1706 static int engine_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
1707                                     const unsigned char *key,
1708                                     const unsigned char *iv, int enc)
1709 {
1710
1711     ZEN_3DES_KEY *tmp_3des_key = NULL;
1712     int to_return = 0;
1713
1714     tmp_3des_key = (ZEN_3DES_KEY *) (ctx->cipher_data);
1715     memcpy(&(tmp_3des_key->des3_key[0]), key, 24);
1716     memcpy(&(tmp_3des_key->des3_iv[0]), iv, 8);
1717
1718     to_return = 1;
1719
1720     return to_return;
1721 }
1722
1723 static int engine_des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1724                                       const unsigned char *in,
1725                                       unsigned int in_len)
1726 {
1727
1728     zen_nb_t output, input;
1729     zen_nb_t deskey_1, deskey_2, deskey_3, iv;
1730     int to_return = 0;
1731
1732     /* Convert parameters ... */
1733     input.len = in_len;
1734     input.data = (unsigned char *)in;
1735     output.len = in_len;
1736     output.data = out;
1737
1738     /* Set key ... */
1739     deskey_1.len = 8;
1740     deskey_2.len = 8;
1741     deskey_3.len = 8;
1742     deskey_1.data =
1743         (unsigned char *)((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_key;
1744     deskey_2.data =
1745         (unsigned char *)&((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_key[8];
1746     deskey_3.data =
1747         (unsigned char *)&((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_key[16];
1748
1749     /* Key correct iv ... */
1750     memcpy(((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_iv, ctx->iv, 8);
1751     iv.len = 8;
1752     iv.data = (unsigned char *)((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_iv;
1753
1754     if (ctx->encrypt == 0) {
1755         memcpy(ctx->iv, &(input.data[input.len - 8]), 8);
1756     }
1757
1758     /* Test with zenbridge library ... */
1759     to_return = ptr_zencod_xdes_cipher(&output, &input,
1760                                        (zen_nb_t *) & deskey_1,
1761                                        (zen_nb_t *) & deskey_2,
1762                                        (zen_nb_t *) & deskey_3, &iv,
1763                                        ctx->encrypt);
1764     to_return = !to_return;
1765
1766     if (ctx->encrypt == 1) {
1767         memcpy(ctx->iv, &(output.data[output.len - 8]), 8);
1768     }
1769
1770     return to_return;
1771 }
1772
1773 static int engine_cipher_cleanup(EVP_CIPHER_CTX *ctx)
1774 {
1775
1776     /* Set the key pointer ... */
1777     if (ctx->cipher->nid == NID_rc4 || ctx->cipher->nid == NID_rc4_40) {
1778     } else if (ctx->cipher->nid == NID_des_cbc) {
1779     } else if (ctx->cipher->nid == NID_des_ede3_cbc) {
1780     }
1781
1782     return 1;
1783 }
1784
1785 # endif                         /* !OPENSSL_NO_HW_ZENCOD */
1786 #endif                          /* !OPENSSL_NO_HW */