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