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