Use p==NULL not !p (in if statements, mainly)
[openssl.git] / demos / engines / cluster_labs / hw_cluster_labs.c
1 /* crypto/engine/hw_cluster_labs.c */
2 /*
3  * Written by Jan Tschirschwitz (jan.tschirschwitz@cluster-labs.com for the
4  * OpenSSL project 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #define MSC_VER                 /* only used cryptic.h */
61
62 #include <stdio.h>
63 #include <openssl/crypto.h>
64 #include <openssl/dso.h>
65 #include <openssl/des.h>
66 #include <openssl/engine.h>
67
68 #ifndef NO_HW
69 # ifndef NO_HW_CLUSTER_LABS
70
71 #  ifdef FLAT_INC
72 #   include "cluster_labs.h"
73 #  else
74 #   include "vendor_defns/cluster_labs.h"
75 #  endif
76
77 #  define CL_LIB_NAME "cluster_labs engine"
78 #  include "hw_cluster_labs_err.c"
79
80 static int cluster_labs_destroy(ENGINE *e);
81 static int cluster_labs_init(ENGINE *e);
82 static int cluster_labs_finish(ENGINE *e);
83 static int cluster_labs_ctrl(ENGINE *e, int cmd, long i, void *p,
84                              void (*f) ());
85
86 /* BIGNUM stuff */
87 /* This function is aliased to mod_exp (with the mont stuff dropped). */
88 static int cluster_labs_mod_exp_mont(BIGNUM *r, const BIGNUM *a,
89                                      const BIGNUM *p, const BIGNUM *m,
90                                      BN_CTX *ctx, BN_MONT_CTX *m_ctx);
91
92 /* RSA stuff */
93 #  ifndef OPENSSL_NO_RSA
94 static int cluster_labs_rsa_pub_enc(int flen, const unsigned char *from,
95                                     unsigned char *to, RSA *rsa, int padding);
96 static int cluster_labs_rsa_pub_dec(int flen, const unsigned char *from,
97                                     unsigned char *to, RSA *rsa, int padding);
98 static int cluster_labs_rsa_priv_enc(int flen, const unsigned char *from,
99                                      unsigned char *to, RSA *rsa,
100                                      int padding);
101 static int cluster_labs_rsa_priv_dec(int flen, const unsigned char *from,
102                                      unsigned char *to, RSA *rsa,
103                                      int padding);
104 static int cluster_labs_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
105 #  endif
106
107 /* DSA stuff */
108 #  ifndef OPENSSL_NO_DSA
109 static DSA_SIG *cluster_labs_dsa_sign(const unsigned char *dgst, int dlen,
110                                       DSA *dsa);
111 static int cluster_labs_dsa_verify(const unsigned char *dgst, int dgst_len,
112                                    DSA_SIG *sig, DSA *dsa);
113 static int cluster_labs_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
114                                     BIGNUM *p1, BIGNUM *a2, BIGNUM *p2,
115                                     BIGNUM *m, BN_CTX *ctx,
116                                     BN_MONT_CTX *in_mont);
117 static int cluster_labs_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
118                                     const BIGNUM *p, const BIGNUM *m,
119                                     BN_CTX *ctx, BN_MONT_CTX *m_ctx);
120 #  endif
121
122 /* DH stuff */
123 #  ifndef OPENSSL_NO_DH
124 /* This function is alised to mod_exp (with the DH and mont dropped). */
125 static int cluster_labs_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
126                                    const BIGNUM *p, const BIGNUM *m,
127                                    BN_CTX *ctx, BN_MONT_CTX *m_ctx);
128 #  endif
129
130 /* RANDOM stuff */
131 static int cluster_labs_rand_bytes(unsigned char *buf, int num);
132
133 /* The definitions for control commands specific to this engine */
134 #  define CLUSTER_LABS_CMD_SO_PATH                ENGINE_CMD_BASE
135 static const ENGINE_CMD_DEFN cluster_labs_cmd_defns[] = {
136     {CLUSTER_LABS_CMD_SO_PATH,
137      "SO_PATH",
138      "Specifies the path to the 'cluster labs' shared library",
139      ENGINE_CMD_FLAG_STRING},
140     {0, NULL, NULL, 0}
141 };
142
143 /* Our internal RSA_METHOD that we provide pointers to */
144 #  ifndef OPENSSL_NO_RSA
145 static RSA_METHOD cluster_labs_rsa = {
146     "Cluster Labs RSA method",
147     cluster_labs_rsa_pub_enc,   /* rsa_pub_enc */
148     cluster_labs_rsa_pub_dec,   /* rsa_pub_dec */
149     cluster_labs_rsa_priv_enc,  /* rsa_priv_enc */
150     cluster_labs_rsa_priv_dec,  /* rsa_priv_dec */
151     cluster_labs_rsa_mod_exp,   /* rsa_mod_exp */
152     cluster_labs_mod_exp_mont,  /* bn_mod_exp */
153     NULL,                       /* init */
154     NULL,                       /* finish */
155     0,                          /* flags */
156     NULL,                       /* apps_data */
157     NULL,                       /* rsa_sign */
158     NULL                        /* rsa_verify */
159 };
160 #  endif
161
162 /* Our internal DSA_METHOD that we provide pointers to */
163 #  ifndef OPENSSL_NO_DSA
164 static DSA_METHOD cluster_labs_dsa = {
165     "Cluster Labs DSA method",
166     cluster_labs_dsa_sign,      /* dsa_do_sign */
167     NULL,                       /* dsa_sign_setup */
168     cluster_labs_dsa_verify,    /* dsa_do_verify */
169     cluster_labs_dsa_mod_exp,   /* dsa_mod_exp */
170     cluster_labs_mod_exp_dsa,   /* bn_mod_exp */
171     NULL,                       /* init */
172     NULL,                       /* finish */
173     0,                          /* flags */
174     NULL                        /* app_data */
175 };
176 #  endif
177
178 /* Our internal DH_METHOD that we provide pointers to */
179 #  ifndef OPENSSL_NO_DH
180 static DH_METHOD cluster_labs_dh = {
181     "Cluster Labs DH method",
182     NULL,                       /* generate key */
183     NULL,                       /* compute key */
184     cluster_labs_mod_exp_dh,    /* bn_mod_exp */
185     NULL,                       /* init */
186     NULL,                       /* finish */
187     0,                          /* flags */
188     NULL                        /* app_data */
189 };
190 #  endif
191
192 static RAND_METHOD cluster_labs_rand = {
193     /* "Cluster Labs RAND method", */
194     NULL,                       /* seed */
195     cluster_labs_rand_bytes,    /* bytes */
196     NULL,                       /* cleanup */
197     NULL,                       /* add */
198     cluster_labs_rand_bytes,    /* pseudorand */
199     NULL,                       /* status */
200 };
201
202 static const char *engine_cluster_labs_id = "cluster_labs";
203 static const char *engine_cluster_labs_name =
204     "Cluster Labs hardware engine support";
205
206 /* engine implementation */
207 /* ---------------------*/
208 static int bind_helper(ENGINE *e)
209 {
210
211     if (!ENGINE_set_id(e, engine_cluster_labs_id) ||
212         !ENGINE_set_name(e, engine_cluster_labs_name) ||
213 #  ifndef OPENSSL_NO_RSA
214         !ENGINE_set_RSA(e, &cluster_labs_rsa) ||
215 #  endif
216 #  ifndef OPENSSL_NO_DSA
217         !ENGINE_set_DSA(e, &cluster_labs_dsa) ||
218 #  endif
219 #  ifndef OPENSSL_NO_DH
220         !ENGINE_set_DH(e, &cluster_labs_dh) ||
221 #  endif
222         !ENGINE_set_RAND(e, &cluster_labs_rand) ||
223         !ENGINE_set_destroy_function(e, cluster_labs_destroy) ||
224         !ENGINE_set_init_function(e, cluster_labs_init) ||
225         !ENGINE_set_finish_function(e, cluster_labs_finish) ||
226         !ENGINE_set_ctrl_function(e, cluster_labs_ctrl) ||
227         !ENGINE_set_cmd_defns(e, cluster_labs_cmd_defns))
228         return 0;
229     /* Ensure the error handling is set up */
230     ERR_load_CL_strings();
231     return 1;
232 }
233
234 #  ifndef ENGINE_DYNAMIC_SUPPORT
235 static ENGINE *engine_cluster_labs(void)
236 {
237     ENGINE *ret = ENGINE_new();
238
239     if (!ret)
240         return NULL;
241     if (!bind_helper(ret)) {
242         ENGINE_free(ret);
243         return NULL;
244     }
245     return ret;
246 }
247
248 #   ifdef ENGINE_DYNAMIC_SUPPORT
249 static
250 #   endif
251 void ENGINE_load_cluster_labs(void)
252 {
253
254     ENGINE *cluster_labs = engine_cluster_labs();
255
256     if (!cluster_labs)
257         return;
258     ENGINE_add(cluster_labs);
259     ENGINE_free(cluster_labs);
260     ERR_clear_error();
261 }
262 #  endif                        /* !ENGINE_DYNAMIC_SUPPORT */
263
264 static int cluster_labs_destroy(ENGINE *e)
265 {
266
267     ERR_unload_CL_strings();
268     return 1;
269 }
270
271 /*
272  * This is a process-global DSO handle used for loading and unloading the
273  * Cluster Labs library. NB: This is only set (or unset) during an init() or
274  * finish() call (reference counts permitting) and they're operating with
275  * global locks, so this should be thread-safe implicitly.
276  */
277 static DSO *cluster_labs_dso = NULL;
278
279 /*
280  * These are the function pointers that are (un)set when the library has
281  * successfully (un)loaded.
282  */
283 static cl_engine_init *p_cl_engine_init = NULL;
284 static cl_mod_exp *p_cl_mod_exp = NULL;
285 static cl_mod_exp_crt *p_cl_mod_exp_crt = NULL;
286 static cl_rsa_mod_exp *p_cl_rsa_mod_exp = NULL;
287 static cl_rsa_priv_enc *p_cl_rsa_priv_enc = NULL;
288 static cl_rsa_priv_dec *p_cl_rsa_priv_dec = NULL;
289 static cl_rsa_pub_enc *p_cl_rsa_pub_enc = NULL;
290 static cl_rsa_pub_dec *p_cl_rsa_pub_dec = NULL;
291 static cl_rand_bytes *p_cl_rand_bytes = NULL;
292 static cl_dsa_sign *p_cl_dsa_sign = NULL;
293 static cl_dsa_verify *p_cl_dsa_verify = NULL;
294
295 int cluster_labs_init(ENGINE *e)
296 {
297
298     cl_engine_init *p1;
299     cl_mod_exp *p2;
300     cl_mod_exp_crt *p3;
301     cl_rsa_mod_exp *p4;
302     cl_rsa_priv_enc *p5;
303     cl_rsa_priv_dec *p6;
304     cl_rsa_pub_enc *p7;
305     cl_rsa_pub_dec *p8;
306     cl_rand_bytes *p20;
307     cl_dsa_sign *p30;
308     cl_dsa_verify *p31;
309
310     /* engine already loaded */
311     if (cluster_labs_dso != NULL) {
312         CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_ALREADY_LOADED);
313         goto err;
314     }
315     /* try to load engine    */
316     cluster_labs_dso = DSO_load(NULL, CLUSTER_LABS_LIB_NAME, NULL, 0);
317     if (cluster_labs_dso == NULL) {
318         CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_DSO_FAILURE);
319         goto err;
320     }
321     /* bind functions */
322 #define BINDIT(t, name) (t *)DSO_bind_func(cluster_labs_dso, name)
323     if ((p1 = (cl_engine_init, CLUSTER_LABS_F1)) == NULL
324         || (p2 = BINDIT(cl_mod_exp, CLUSTER_LABS_F2)) == NULL
325         || (p3 = BINDIT(cl_mod_exp_crt, CLUSTER_LABS_F3)) == NULL
326         || (p4 = BINDIT(cl_rsa_mod_exp, CLUSTER_LABS_F4)) == NULL
327         || (p5 = BINDIT(cl_rsa_priv_enc, CLUSTER_LABS_F5)) == NULL
328         || (p6 = BINDIT(cl_rsa_priv_dec, CLUSTER_LABS_F6)) == NULL
329         || (p7 = BINDIT(cl_rsa_pub_enc, CLUSTER_LABS_F7)) == NULL
330         || (p8 = BINDIT(cl_rsa_pub_dec, CLUSTER_LABS_F8)) == NULL
331         || (p20 = BINDIT(cl_rand_bytes, CLUSTER_LABS_F20)) == NULL
332         || (p30 = BINDIT(cl_dsa_sign, CLUSTER_LABS_F30)) == NULL
333         || (p31 = BINDIT(cl_dsa_verify, CLUSTER_LABS_F31)) == NULL) {
334         CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_DSO_FAILURE);
335         goto err;
336     }
337
338     /* copy function pointers */
339     p_cl_engine_init = p1;
340     p_cl_mod_exp = p2;
341     p_cl_mod_exp_crt = p3;
342     p_cl_rsa_mod_exp = p4;
343     p_cl_rsa_priv_enc = p5;
344     p_cl_rsa_priv_dec = p6;
345     p_cl_rsa_pub_enc = p7;
346     p_cl_rsa_pub_dec = p8;
347     p_cl_rand_bytes = p20;
348     p_cl_dsa_sign = p30;
349     p_cl_dsa_verify = p31;
350
351     /* cluster labs engine init */
352     if (p_cl_engine_init() == 0) {
353         CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_INIT_FAILED);
354         goto err;
355     }
356
357     return (1);
358
359  err:
360     /* reset all pointers */
361     DSO_free(cluster_labs_dso);
362     cluster_labs_dso = NULL;
363     p_cl_engine_init = NULL;
364     p_cl_mod_exp = NULL;
365     p_cl_mod_exp_crt = NULL;
366     p_cl_rsa_mod_exp = NULL;
367     p_cl_rsa_priv_enc = NULL;
368     p_cl_rsa_priv_dec = NULL;
369     p_cl_rsa_pub_enc = NULL;
370     p_cl_rsa_pub_dec = NULL;
371     p_cl_rand_bytes = NULL;
372     p_cl_dsa_sign = NULL;
373     p_cl_dsa_verify = NULL;
374
375     return (0);
376 }
377
378 static int cluster_labs_finish(ENGINE *e)
379 {
380
381     if (cluster_labs_dso == NULL) {
382         CLerr(CL_F_CLUSTER_LABS_FINISH, CL_R_NOT_LOADED);
383         return 0;
384     }
385     if (!DSO_free(cluster_labs_dso)) {
386         CLerr(CL_F_CLUSTER_LABS_FINISH, CL_R_DSO_FAILURE);
387         return 0;
388     }
389
390     cluster_labs_dso = NULL;
391     p_cl_engine_init = NULL;
392     p_cl_mod_exp = NULL;
393     p_cl_rsa_mod_exp = NULL;
394     p_cl_mod_exp_crt = NULL;
395     p_cl_rsa_priv_enc = NULL;
396     p_cl_rsa_priv_dec = NULL;
397     p_cl_rsa_pub_enc = NULL;
398     p_cl_rsa_pub_dec = NULL;
399     p_cl_rand_bytes = NULL;
400     p_cl_dsa_sign = NULL;
401     p_cl_dsa_verify = NULL;
402
403     return (1);
404
405 }
406
407 static int cluster_labs_ctrl(ENGINE *e, int cmd, long i, void *p,
408                              void (*f) ())
409 {
410     int initialised = ((cluster_labs_dso == NULL) ? 0 : 1);
411
412     switch (cmd) {
413     case CLUSTER_LABS_CMD_SO_PATH:
414         if (p == NULL) {
415             CLerr(CL_F_CLUSTER_LABS_CTRL, ERR_R_PASSED_NULL_PARAMETER);
416             return 0;
417         }
418         if (initialised) {
419             CLerr(CL_F_CLUSTER_LABS_CTRL, CL_R_ALREADY_LOADED);
420             return 0;
421         }
422         CLUSTER_LABS_LIB_NAME = (const char *)p;
423         return 1;
424     default:
425         break;
426     }
427     CLerr(CL_F_CLUSTER_LABS_CTRL, CL_R_COMMAND_NOT_IMPLEMENTED);
428     return 0;
429 }
430
431 static int cluster_labs_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
432                                 const BIGNUM *m, BN_CTX *ctx)
433 {
434
435     if (cluster_labs_dso == NULL) {
436         CLerr(CL_F_CLUSTER_LABS_MOD_EXP, CL_R_NOT_LOADED);
437         return 0;
438     }
439     if (p_cl_mod_exp == NULL) {
440         CLerr(CL_F_CLUSTER_LABS_MOD_EXP, CL_R_FUNCTION_NOT_BINDED);
441         return 0;
442     }
443
444     return p_cl_mod_exp(r, a, p, m, ctx);
445
446 }
447
448 static int cluster_labs_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
449                                     const BIGNUM *q, const BIGNUM *dmp1,
450                                     const BIGNUM *dmq1, const BIGNUM *iqmp,
451                                     BN_CTX *ctx)
452 {
453
454     if (cluster_labs_dso == NULL) {
455         CLerr(CL_F_CLUSTER_LABS_MOD_EXP_CRT, CL_R_NOT_LOADED);
456         return 0;
457     }
458     if (p_cl_mod_exp_crt == NULL) {
459         CLerr(CL_F_CLUSTER_LABS_MOD_EXP_CRT, CL_R_FUNCTION_NOT_BINDED);
460         return 0;
461     }
462
463     return p_cl_mod_exp_crt(r, a, p, q, dmp1, dmq1, iqmp, ctx);
464
465 }
466
467 static int cluster_labs_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
468 {
469
470     if (cluster_labs_dso == NULL) {
471         CLerr(CL_F_CLUSTER_LABS_RSA_MOD_EXP, CL_R_NOT_LOADED);
472         return 0;
473     }
474     if (p_cl_rsa_mod_exp == NULL) {
475         CLerr(CL_F_CLUSTER_LABS_RSA_MOD_EXP, CL_R_FUNCTION_NOT_BINDED);
476         return 0;
477     }
478
479     return p_cl_rsa_mod_exp(r0, I, rsa);
480
481 }
482
483 static DSA_SIG *cluster_labs_dsa_sign(const unsigned char *dgst, int dlen,
484                                       DSA *dsa)
485 {
486
487     if (cluster_labs_dso == NULL) {
488         CLerr(CL_F_CLUSTER_LABS_DSA_SIGN, CL_R_NOT_LOADED);
489         return 0;
490     }
491     if (p_cl_dsa_sign == NULL) {
492         CLerr(CL_F_CLUSTER_LABS_DSA_SIGN, CL_R_FUNCTION_NOT_BINDED);
493         return 0;
494     }
495
496     return p_cl_dsa_sign(dgst, dlen, dsa);
497
498 }
499
500 static int cluster_labs_dsa_verify(const unsigned char *dgst, int dgst_len,
501                                    DSA_SIG *sig, DSA *dsa)
502 {
503
504     if (cluster_labs_dso == NULL) {
505         CLerr(CL_F_CLUSTER_LABS_DSA_VERIFY, CL_R_NOT_LOADED);
506         return 0;
507     }
508
509     if (p_cl_dsa_verify == NULL) {
510         CLerr(CL_F_CLUSTER_LABS_DSA_VERIFY, CL_R_FUNCTION_NOT_BINDED);
511         return 0;
512     }
513
514     return p_cl_dsa_verify(dgst, dgst_len, sig, dsa);
515
516 }
517
518 static int cluster_labs_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
519                                     BIGNUM *p1, BIGNUM *a2, BIGNUM *p2,
520                                     BIGNUM *m, BN_CTX *ctx,
521                                     BN_MONT_CTX *in_mont)
522 {
523     BIGNUM t;
524     int status = 0;
525
526     BN_init(&t);
527     /* let rr = a1 ^ p1 mod m */
528     if (!cluster_labs_mod_exp(rr, a1, p1, m, ctx))
529         goto end;
530     /* let t = a2 ^ p2 mod m */
531     if (!cluster_labs_mod_exp(&t, a2, p2, m, ctx))
532         goto end;
533     /* let rr = rr * t mod m */
534     if (!BN_mod_mul(rr, rr, &t, m, ctx))
535         goto end;
536     status = 1;
537  end:
538     BN_free(&t);
539
540     return (1);
541
542 }
543
544 static int cluster_labs_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
545                                     const BIGNUM *p, const BIGNUM *m,
546                                     BN_CTX *ctx, BN_MONT_CTX *m_ctx)
547 {
548     return cluster_labs_mod_exp(r, a, p, m, ctx);
549 }
550
551 /* This function is aliased to mod_exp (with the mont stuff dropped). */
552 static int cluster_labs_mod_exp_mont(BIGNUM *r, const BIGNUM *a,
553                                      const BIGNUM *p, const BIGNUM *m,
554                                      BN_CTX *ctx, BN_MONT_CTX *m_ctx)
555 {
556     return cluster_labs_mod_exp(r, a, p, m, ctx);
557 }
558
559 /* This function is aliased to mod_exp (with the dh and mont dropped). */
560 static int cluster_labs_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
561                                    const BIGNUM *p, const BIGNUM *m,
562                                    BN_CTX *ctx, BN_MONT_CTX *m_ctx)
563 {
564     return cluster_labs_mod_exp(r, a, p, m, ctx);
565 }
566
567 static int cluster_labs_rsa_pub_enc(int flen, const unsigned char *from,
568                                     unsigned char *to, RSA *rsa, int padding)
569 {
570
571     if (cluster_labs_dso == NULL) {
572         CLerr(CL_F_CLUSTER_LABS_RSA_PUB_ENC, CL_R_NOT_LOADED);
573         return 0;
574     }
575     if (p_cl_rsa_priv_enc == NULL) {
576         CLerr(CL_F_CLUSTER_LABS_RSA_PUB_ENC, CL_R_FUNCTION_NOT_BINDED);
577         return 0;
578     }
579
580     return p_cl_rsa_pub_enc(flen, from, to, rsa, padding);
581
582 }
583
584 static int cluster_labs_rsa_pub_dec(int flen, const unsigned char *from,
585                                     unsigned char *to, RSA *rsa, int padding)
586 {
587
588     if (cluster_labs_dso == NULL) {
589         CLerr(CL_F_CLUSTER_LABS_RSA_PUB_DEC, CL_R_NOT_LOADED);
590         return 0;
591     }
592     if (p_cl_rsa_priv_enc == NULL) {
593         CLerr(CL_F_CLUSTER_LABS_RSA_PUB_DEC, CL_R_FUNCTION_NOT_BINDED);
594         return 0;
595     }
596
597     return p_cl_rsa_pub_dec(flen, from, to, rsa, padding);
598
599 }
600
601 static int cluster_labs_rsa_priv_enc(int flen, const unsigned char *from,
602                                      unsigned char *to, RSA *rsa, int padding)
603 {
604
605     if (cluster_labs_dso == NULL) {
606         CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_ENC, CL_R_NOT_LOADED);
607         return 0;
608     }
609
610     if (p_cl_rsa_priv_enc == NULL) {
611         CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_ENC, CL_R_FUNCTION_NOT_BINDED);
612         return 0;
613     }
614
615     return p_cl_rsa_priv_enc(flen, from, to, rsa, padding);
616
617 }
618
619 static int cluster_labs_rsa_priv_dec(int flen, const unsigned char *from,
620                                      unsigned char *to, RSA *rsa, int padding)
621 {
622
623     if (cluster_labs_dso == NULL) {
624         CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_DEC, CL_R_NOT_LOADED);
625         return 0;
626     }
627     if (p_cl_rsa_priv_dec == NULL) {
628         CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_DEC, CL_R_FUNCTION_NOT_BINDED);
629         return 0;
630     }
631
632     return p_cl_rsa_priv_dec(flen, from, to, rsa, padding);
633
634 }
635
636 /************************************************************************************
637 * Symmetric algorithms
638 ************************************************************************************/
639 /* this will be come soon! */
640
641 /************************************************************************************
642 * Random generator
643 ************************************************************************************/
644
645 static int cluster_labs_rand_bytes(unsigned char *buf, int num)
646 {
647
648     if (cluster_labs_dso == NULL) {
649         CLerr(CL_F_CLUSTER_LABS_RAND_BYTES, CL_R_NOT_LOADED);
650         return 0;
651     }
652     if (p_cl_mod_exp_crt == NULL) {
653         CLerr(CL_F_CLUSTER_LABS_RAND_BYTES, CL_R_FUNCTION_NOT_BINDED);
654         return 0;
655     }
656
657     return p_cl_rand_bytes(buf, num);
658
659 }
660
661 /*
662  * This stuff is needed if this ENGINE is being compiled into a
663  * self-contained shared-library.
664  */
665 #  ifdef ENGINE_DYNAMIC_SUPPORT
666 static int bind_fn(ENGINE *e, const char *id)
667 {
668     fprintf(stderr, "bind_fn CLUSTER_LABS\n");
669     if (id && (strcmp(id, engine_cluster_labs_id) != 0)) {
670         fprintf(stderr, "bind_fn return(0) first\n");
671         return 0;
672     }
673     if (!bind_helper(e)) {
674         fprintf(stderr, "bind_fn return(1) first\n");
675         return 0;
676     }
677     fprintf(stderr, "bind_fn return(1)\n");
678     return 1;
679 }
680
681 IMPLEMENT_DYNAMIC_CHECK_FN()
682     IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
683 #  endif                        /* ENGINE_DYNAMIC_SUPPORT */
684 # endif                         /* !NO_HW_CLUSTER_LABS */
685 #endif                          /* !NO_HW */