hw_cluster_labs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /* crypto/engine/hw_cluster_labs.c */
  2. /*
  3. * Written by Jan Tschirschwitz ([email protected] 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. #define MSC_VER /* only used cryptic.h */
  60. #include <stdio.h>
  61. #include <openssl/crypto.h>
  62. #include <openssl/dso.h>
  63. #include <openssl/des.h>
  64. #include <openssl/engine.h>
  65. #ifndef NO_HW
  66. # ifndef NO_HW_CLUSTER_LABS
  67. # ifdef FLAT_INC
  68. # include "cluster_labs.h"
  69. # else
  70. # include "vendor_defns/cluster_labs.h"
  71. # endif
  72. # define CL_LIB_NAME "cluster_labs engine"
  73. # include "hw_cluster_labs_err.c"
  74. static int cluster_labs_destroy(ENGINE *e);
  75. static int cluster_labs_init(ENGINE *e);
  76. static int cluster_labs_finish(ENGINE *e);
  77. static int cluster_labs_ctrl(ENGINE *e, int cmd, long i, void *p,
  78. void (*f) ());
  79. /* BIGNUM stuff */
  80. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  81. static int cluster_labs_mod_exp_mont(BIGNUM *r, const BIGNUM *a,
  82. const BIGNUM *p, const BIGNUM *m,
  83. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  84. /* RSA stuff */
  85. # ifndef OPENSSL_NO_RSA
  86. static int cluster_labs_rsa_pub_enc(int flen, const unsigned char *from,
  87. unsigned char *to, RSA *rsa, int padding);
  88. static int cluster_labs_rsa_pub_dec(int flen, const unsigned char *from,
  89. unsigned char *to, RSA *rsa, int padding);
  90. static int cluster_labs_rsa_priv_enc(int flen, const unsigned char *from,
  91. unsigned char *to, RSA *rsa,
  92. int padding);
  93. static int cluster_labs_rsa_priv_dec(int flen, const unsigned char *from,
  94. unsigned char *to, RSA *rsa,
  95. int padding);
  96. static int cluster_labs_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
  97. # endif
  98. /* DSA stuff */
  99. # ifndef OPENSSL_NO_DSA
  100. static DSA_SIG *cluster_labs_dsa_sign(const unsigned char *dgst, int dlen,
  101. DSA *dsa);
  102. static int cluster_labs_dsa_verify(const unsigned char *dgst, int dgst_len,
  103. DSA_SIG *sig, DSA *dsa);
  104. static int cluster_labs_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  105. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2,
  106. BIGNUM *m, BN_CTX *ctx,
  107. BN_MONT_CTX *in_mont);
  108. static int cluster_labs_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  109. const BIGNUM *p, const BIGNUM *m,
  110. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  111. # endif
  112. /* DH stuff */
  113. # ifndef OPENSSL_NO_DH
  114. /* This function is alised to mod_exp (with the DH and mont dropped). */
  115. static int cluster_labs_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  116. const BIGNUM *p, const BIGNUM *m,
  117. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  118. # endif
  119. /* RANDOM stuff */
  120. static int cluster_labs_rand_bytes(unsigned char *buf, int num);
  121. /* The definitions for control commands specific to this engine */
  122. # define CLUSTER_LABS_CMD_SO_PATH ENGINE_CMD_BASE
  123. static const ENGINE_CMD_DEFN cluster_labs_cmd_defns[] = {
  124. {CLUSTER_LABS_CMD_SO_PATH,
  125. "SO_PATH",
  126. "Specifies the path to the 'cluster labs' shared library",
  127. ENGINE_CMD_FLAG_STRING},
  128. {0, NULL, NULL, 0}
  129. };
  130. /* Our internal RSA_METHOD that we provide pointers to */
  131. # ifndef OPENSSL_NO_RSA
  132. static RSA_METHOD cluster_labs_rsa = {
  133. "Cluster Labs RSA method",
  134. cluster_labs_rsa_pub_enc, /* rsa_pub_enc */
  135. cluster_labs_rsa_pub_dec, /* rsa_pub_dec */
  136. cluster_labs_rsa_priv_enc, /* rsa_priv_enc */
  137. cluster_labs_rsa_priv_dec, /* rsa_priv_dec */
  138. cluster_labs_rsa_mod_exp, /* rsa_mod_exp */
  139. cluster_labs_mod_exp_mont, /* bn_mod_exp */
  140. NULL, /* init */
  141. NULL, /* finish */
  142. 0, /* flags */
  143. NULL, /* apps_data */
  144. NULL, /* rsa_sign */
  145. NULL /* rsa_verify */
  146. };
  147. # endif
  148. /* Our internal DSA_METHOD that we provide pointers to */
  149. # ifndef OPENSSL_NO_DSA
  150. static DSA_METHOD cluster_labs_dsa = {
  151. "Cluster Labs DSA method",
  152. cluster_labs_dsa_sign, /* dsa_do_sign */
  153. NULL, /* dsa_sign_setup */
  154. cluster_labs_dsa_verify, /* dsa_do_verify */
  155. cluster_labs_dsa_mod_exp, /* dsa_mod_exp */
  156. cluster_labs_mod_exp_dsa, /* bn_mod_exp */
  157. NULL, /* init */
  158. NULL, /* finish */
  159. 0, /* flags */
  160. NULL /* app_data */
  161. };
  162. # endif
  163. /* Our internal DH_METHOD that we provide pointers to */
  164. # ifndef OPENSSL_NO_DH
  165. static DH_METHOD cluster_labs_dh = {
  166. "Cluster Labs DH method",
  167. NULL, /* generate key */
  168. NULL, /* compute key */
  169. cluster_labs_mod_exp_dh, /* bn_mod_exp */
  170. NULL, /* init */
  171. NULL, /* finish */
  172. 0, /* flags */
  173. NULL /* app_data */
  174. };
  175. # endif
  176. static RAND_METHOD cluster_labs_rand = {
  177. /* "Cluster Labs RAND method", */
  178. NULL, /* seed */
  179. cluster_labs_rand_bytes, /* bytes */
  180. NULL, /* cleanup */
  181. NULL, /* add */
  182. cluster_labs_rand_bytes, /* pseudorand */
  183. NULL, /* status */
  184. };
  185. static const char *engine_cluster_labs_id = "cluster_labs";
  186. static const char *engine_cluster_labs_name =
  187. "Cluster Labs hardware engine support";
  188. /* engine implementation */
  189. /* ---------------------*/
  190. static int bind_helper(ENGINE *e)
  191. {
  192. if (!ENGINE_set_id(e, engine_cluster_labs_id) ||
  193. !ENGINE_set_name(e, engine_cluster_labs_name) ||
  194. # ifndef OPENSSL_NO_RSA
  195. !ENGINE_set_RSA(e, &cluster_labs_rsa) ||
  196. # endif
  197. # ifndef OPENSSL_NO_DSA
  198. !ENGINE_set_DSA(e, &cluster_labs_dsa) ||
  199. # endif
  200. # ifndef OPENSSL_NO_DH
  201. !ENGINE_set_DH(e, &cluster_labs_dh) ||
  202. # endif
  203. !ENGINE_set_RAND(e, &cluster_labs_rand) ||
  204. !ENGINE_set_destroy_function(e, cluster_labs_destroy) ||
  205. !ENGINE_set_init_function(e, cluster_labs_init) ||
  206. !ENGINE_set_finish_function(e, cluster_labs_finish) ||
  207. !ENGINE_set_ctrl_function(e, cluster_labs_ctrl) ||
  208. !ENGINE_set_cmd_defns(e, cluster_labs_cmd_defns))
  209. return 0;
  210. /* Ensure the error handling is set up */
  211. ERR_load_CL_strings();
  212. return 1;
  213. }
  214. # ifndef ENGINE_DYNAMIC_SUPPORT
  215. static ENGINE *engine_cluster_labs(void)
  216. {
  217. ENGINE *ret = ENGINE_new();
  218. if (!ret)
  219. return NULL;
  220. if (!bind_helper(ret)) {
  221. ENGINE_free(ret);
  222. return NULL;
  223. }
  224. return ret;
  225. }
  226. # ifdef ENGINE_DYNAMIC_SUPPORT
  227. static
  228. # endif
  229. void ENGINE_load_cluster_labs(void)
  230. {
  231. ENGINE *cluster_labs = engine_cluster_labs();
  232. if (!cluster_labs)
  233. return;
  234. ENGINE_add(cluster_labs);
  235. ENGINE_free(cluster_labs);
  236. ERR_clear_error();
  237. }
  238. # endif /* !ENGINE_DYNAMIC_SUPPORT */
  239. static int cluster_labs_destroy(ENGINE *e)
  240. {
  241. ERR_unload_CL_strings();
  242. return 1;
  243. }
  244. /*
  245. * This is a process-global DSO handle used for loading and unloading the
  246. * Cluster Labs library. NB: This is only set (or unset) during an init() or
  247. * finish() call (reference counts permitting) and they're operating with
  248. * global locks, so this should be thread-safe implicitly.
  249. */
  250. static DSO *cluster_labs_dso = NULL;
  251. /*
  252. * These are the function pointers that are (un)set when the library has
  253. * successfully (un)loaded.
  254. */
  255. static cl_engine_init *p_cl_engine_init = NULL;
  256. static cl_mod_exp *p_cl_mod_exp = NULL;
  257. static cl_mod_exp_crt *p_cl_mod_exp_crt = NULL;
  258. static cl_rsa_mod_exp *p_cl_rsa_mod_exp = NULL;
  259. static cl_rsa_priv_enc *p_cl_rsa_priv_enc = NULL;
  260. static cl_rsa_priv_dec *p_cl_rsa_priv_dec = NULL;
  261. static cl_rsa_pub_enc *p_cl_rsa_pub_enc = NULL;
  262. static cl_rsa_pub_dec *p_cl_rsa_pub_dec = NULL;
  263. static cl_rand_bytes *p_cl_rand_bytes = NULL;
  264. static cl_dsa_sign *p_cl_dsa_sign = NULL;
  265. static cl_dsa_verify *p_cl_dsa_verify = NULL;
  266. int cluster_labs_init(ENGINE *e)
  267. {
  268. cl_engine_init *p1;
  269. cl_mod_exp *p2;
  270. cl_mod_exp_crt *p3;
  271. cl_rsa_mod_exp *p4;
  272. cl_rsa_priv_enc *p5;
  273. cl_rsa_priv_dec *p6;
  274. cl_rsa_pub_enc *p7;
  275. cl_rsa_pub_dec *p8;
  276. cl_rand_bytes *p20;
  277. cl_dsa_sign *p30;
  278. cl_dsa_verify *p31;
  279. /* engine already loaded */
  280. if (cluster_labs_dso != NULL) {
  281. CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_ALREADY_LOADED);
  282. goto err;
  283. }
  284. /* try to load engine */
  285. cluster_labs_dso = DSO_load(NULL, CLUSTER_LABS_LIB_NAME, NULL, 0);
  286. if (cluster_labs_dso == NULL) {
  287. CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_DSO_FAILURE);
  288. goto err;
  289. }
  290. /* bind functions */
  291. if (!
  292. (p1 =
  293. (cl_engine_init *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F1))
  294. || !(p2 = (cl_mod_exp *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F2))
  295. || !(p3 = (cl_mod_exp_crt *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F3))
  296. || !(p4 = (cl_rsa_mod_exp *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F4))
  297. || !(p5 =
  298. (cl_rsa_priv_enc *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F5))
  299. || !(p6 =
  300. (cl_rsa_priv_dec *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F6))
  301. || !(p7 = (cl_rsa_pub_enc *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F7))
  302. || !(p8 = (cl_rsa_pub_dec *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F8))
  303. || !(p20 =
  304. (cl_rand_bytes *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F20))
  305. || !(p30 = (cl_dsa_sign *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F30))
  306. || !(p31 =
  307. (cl_dsa_verify *) DSO_bind_func(cluster_labs_dso, CLUSTER_LABS_F31))) {
  308. CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_DSO_FAILURE);
  309. goto err;
  310. }
  311. /* copy function pointers */
  312. p_cl_engine_init = p1;
  313. p_cl_mod_exp = p2;
  314. p_cl_mod_exp_crt = p3;
  315. p_cl_rsa_mod_exp = p4;
  316. p_cl_rsa_priv_enc = p5;
  317. p_cl_rsa_priv_dec = p6;
  318. p_cl_rsa_pub_enc = p7;
  319. p_cl_rsa_pub_dec = p8;
  320. p_cl_rand_bytes = p20;
  321. p_cl_dsa_sign = p30;
  322. p_cl_dsa_verify = p31;
  323. /* cluster labs engine init */
  324. if (p_cl_engine_init() == 0) {
  325. CLerr(CL_F_CLUSTER_LABS_INIT, CL_R_INIT_FAILED);
  326. goto err;
  327. }
  328. return (1);
  329. err:
  330. /* reset all pointers */
  331. if (cluster_labs_dso)
  332. DSO_free(cluster_labs_dso);
  333. cluster_labs_dso = NULL;
  334. p_cl_engine_init = NULL;
  335. p_cl_mod_exp = NULL;
  336. p_cl_mod_exp_crt = NULL;
  337. p_cl_rsa_mod_exp = NULL;
  338. p_cl_rsa_priv_enc = NULL;
  339. p_cl_rsa_priv_dec = NULL;
  340. p_cl_rsa_pub_enc = NULL;
  341. p_cl_rsa_pub_dec = NULL;
  342. p_cl_rand_bytes = NULL;
  343. p_cl_dsa_sign = NULL;
  344. p_cl_dsa_verify = NULL;
  345. return (0);
  346. }
  347. static int cluster_labs_finish(ENGINE *e)
  348. {
  349. if (cluster_labs_dso == NULL) {
  350. CLerr(CL_F_CLUSTER_LABS_FINISH, CL_R_NOT_LOADED);
  351. return 0;
  352. }
  353. if (!DSO_free(cluster_labs_dso)) {
  354. CLerr(CL_F_CLUSTER_LABS_FINISH, CL_R_DSO_FAILURE);
  355. return 0;
  356. }
  357. cluster_labs_dso = NULL;
  358. p_cl_engine_init = NULL;
  359. p_cl_mod_exp = NULL;
  360. p_cl_rsa_mod_exp = NULL;
  361. p_cl_mod_exp_crt = NULL;
  362. p_cl_rsa_priv_enc = NULL;
  363. p_cl_rsa_priv_dec = NULL;
  364. p_cl_rsa_pub_enc = NULL;
  365. p_cl_rsa_pub_dec = NULL;
  366. p_cl_rand_bytes = NULL;
  367. p_cl_dsa_sign = NULL;
  368. p_cl_dsa_verify = NULL;
  369. return (1);
  370. }
  371. static int cluster_labs_ctrl(ENGINE *e, int cmd, long i, void *p,
  372. void (*f) ())
  373. {
  374. int initialised = ((cluster_labs_dso == NULL) ? 0 : 1);
  375. switch (cmd) {
  376. case CLUSTER_LABS_CMD_SO_PATH:
  377. if (p == NULL) {
  378. CLerr(CL_F_CLUSTER_LABS_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  379. return 0;
  380. }
  381. if (initialised) {
  382. CLerr(CL_F_CLUSTER_LABS_CTRL, CL_R_ALREADY_LOADED);
  383. return 0;
  384. }
  385. CLUSTER_LABS_LIB_NAME = (const char *)p;
  386. return 1;
  387. default:
  388. break;
  389. }
  390. CLerr(CL_F_CLUSTER_LABS_CTRL, CL_R_COMMAND_NOT_IMPLEMENTED);
  391. return 0;
  392. }
  393. static int cluster_labs_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  394. const BIGNUM *m, BN_CTX *ctx)
  395. {
  396. if (cluster_labs_dso == NULL) {
  397. CLerr(CL_F_CLUSTER_LABS_MOD_EXP, CL_R_NOT_LOADED);
  398. return 0;
  399. }
  400. if (p_cl_mod_exp == NULL) {
  401. CLerr(CL_F_CLUSTER_LABS_MOD_EXP, CL_R_FUNCTION_NOT_BINDED);
  402. return 0;
  403. }
  404. return p_cl_mod_exp(r, a, p, m, ctx);
  405. }
  406. static int cluster_labs_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
  407. const BIGNUM *q, const BIGNUM *dmp1,
  408. const BIGNUM *dmq1, const BIGNUM *iqmp,
  409. BN_CTX *ctx)
  410. {
  411. if (cluster_labs_dso == NULL) {
  412. CLerr(CL_F_CLUSTER_LABS_MOD_EXP_CRT, CL_R_NOT_LOADED);
  413. return 0;
  414. }
  415. if (p_cl_mod_exp_crt == NULL) {
  416. CLerr(CL_F_CLUSTER_LABS_MOD_EXP_CRT, CL_R_FUNCTION_NOT_BINDED);
  417. return 0;
  418. }
  419. return p_cl_mod_exp_crt(r, a, p, q, dmp1, dmq1, iqmp, ctx);
  420. }
  421. static int cluster_labs_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
  422. {
  423. if (cluster_labs_dso == NULL) {
  424. CLerr(CL_F_CLUSTER_LABS_RSA_MOD_EXP, CL_R_NOT_LOADED);
  425. return 0;
  426. }
  427. if (p_cl_rsa_mod_exp == NULL) {
  428. CLerr(CL_F_CLUSTER_LABS_RSA_MOD_EXP, CL_R_FUNCTION_NOT_BINDED);
  429. return 0;
  430. }
  431. return p_cl_rsa_mod_exp(r0, I, rsa);
  432. }
  433. static DSA_SIG *cluster_labs_dsa_sign(const unsigned char *dgst, int dlen,
  434. DSA *dsa)
  435. {
  436. if (cluster_labs_dso == NULL) {
  437. CLerr(CL_F_CLUSTER_LABS_DSA_SIGN, CL_R_NOT_LOADED);
  438. return 0;
  439. }
  440. if (p_cl_dsa_sign == NULL) {
  441. CLerr(CL_F_CLUSTER_LABS_DSA_SIGN, CL_R_FUNCTION_NOT_BINDED);
  442. return 0;
  443. }
  444. return p_cl_dsa_sign(dgst, dlen, dsa);
  445. }
  446. static int cluster_labs_dsa_verify(const unsigned char *dgst, int dgst_len,
  447. DSA_SIG *sig, DSA *dsa)
  448. {
  449. if (cluster_labs_dso == NULL) {
  450. CLerr(CL_F_CLUSTER_LABS_DSA_VERIFY, CL_R_NOT_LOADED);
  451. return 0;
  452. }
  453. if (p_cl_dsa_verify == NULL) {
  454. CLerr(CL_F_CLUSTER_LABS_DSA_VERIFY, CL_R_FUNCTION_NOT_BINDED);
  455. return 0;
  456. }
  457. return p_cl_dsa_verify(dgst, dgst_len, sig, dsa);
  458. }
  459. static int cluster_labs_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  460. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2,
  461. BIGNUM *m, BN_CTX *ctx,
  462. BN_MONT_CTX *in_mont)
  463. {
  464. BIGNUM t;
  465. int status = 0;
  466. BN_init(&t);
  467. /* let rr = a1 ^ p1 mod m */
  468. if (!cluster_labs_mod_exp(rr, a1, p1, m, ctx))
  469. goto end;
  470. /* let t = a2 ^ p2 mod m */
  471. if (!cluster_labs_mod_exp(&t, a2, p2, m, ctx))
  472. goto end;
  473. /* let rr = rr * t mod m */
  474. if (!BN_mod_mul(rr, rr, &t, m, ctx))
  475. goto end;
  476. status = 1;
  477. end:
  478. BN_free(&t);
  479. return (1);
  480. }
  481. static int cluster_labs_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  482. const BIGNUM *p, const BIGNUM *m,
  483. BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  484. {
  485. return cluster_labs_mod_exp(r, a, p, m, ctx);
  486. }
  487. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  488. static int cluster_labs_mod_exp_mont(BIGNUM *r, const BIGNUM *a,
  489. const BIGNUM *p, const BIGNUM *m,
  490. BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  491. {
  492. return cluster_labs_mod_exp(r, a, p, m, ctx);
  493. }
  494. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  495. static int cluster_labs_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  496. const BIGNUM *p, const BIGNUM *m,
  497. BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  498. {
  499. return cluster_labs_mod_exp(r, a, p, m, ctx);
  500. }
  501. static int cluster_labs_rsa_pub_enc(int flen, const unsigned char *from,
  502. unsigned char *to, RSA *rsa, int padding)
  503. {
  504. if (cluster_labs_dso == NULL) {
  505. CLerr(CL_F_CLUSTER_LABS_RSA_PUB_ENC, CL_R_NOT_LOADED);
  506. return 0;
  507. }
  508. if (p_cl_rsa_priv_enc == NULL) {
  509. CLerr(CL_F_CLUSTER_LABS_RSA_PUB_ENC, CL_R_FUNCTION_NOT_BINDED);
  510. return 0;
  511. }
  512. return p_cl_rsa_pub_enc(flen, from, to, rsa, padding);
  513. }
  514. static int cluster_labs_rsa_pub_dec(int flen, const unsigned char *from,
  515. unsigned char *to, RSA *rsa, int padding)
  516. {
  517. if (cluster_labs_dso == NULL) {
  518. CLerr(CL_F_CLUSTER_LABS_RSA_PUB_DEC, CL_R_NOT_LOADED);
  519. return 0;
  520. }
  521. if (p_cl_rsa_priv_enc == NULL) {
  522. CLerr(CL_F_CLUSTER_LABS_RSA_PUB_DEC, CL_R_FUNCTION_NOT_BINDED);
  523. return 0;
  524. }
  525. return p_cl_rsa_pub_dec(flen, from, to, rsa, padding);
  526. }
  527. static int cluster_labs_rsa_priv_enc(int flen, const unsigned char *from,
  528. unsigned char *to, RSA *rsa, int padding)
  529. {
  530. if (cluster_labs_dso == NULL) {
  531. CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_ENC, CL_R_NOT_LOADED);
  532. return 0;
  533. }
  534. if (p_cl_rsa_priv_enc == NULL) {
  535. CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_ENC, CL_R_FUNCTION_NOT_BINDED);
  536. return 0;
  537. }
  538. return p_cl_rsa_priv_enc(flen, from, to, rsa, padding);
  539. }
  540. static int cluster_labs_rsa_priv_dec(int flen, const unsigned char *from,
  541. unsigned char *to, RSA *rsa, int padding)
  542. {
  543. if (cluster_labs_dso == NULL) {
  544. CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_DEC, CL_R_NOT_LOADED);
  545. return 0;
  546. }
  547. if (p_cl_rsa_priv_dec == NULL) {
  548. CLerr(CL_F_CLUSTER_LABS_RSA_PRIV_DEC, CL_R_FUNCTION_NOT_BINDED);
  549. return 0;
  550. }
  551. return p_cl_rsa_priv_dec(flen, from, to, rsa, padding);
  552. }
  553. /************************************************************************************
  554. * Symmetric algorithms
  555. ************************************************************************************/
  556. /* this will be come soon! */
  557. /************************************************************************************
  558. * Random generator
  559. ************************************************************************************/
  560. static int cluster_labs_rand_bytes(unsigned char *buf, int num)
  561. {
  562. if (cluster_labs_dso == NULL) {
  563. CLerr(CL_F_CLUSTER_LABS_RAND_BYTES, CL_R_NOT_LOADED);
  564. return 0;
  565. }
  566. if (p_cl_mod_exp_crt == NULL) {
  567. CLerr(CL_F_CLUSTER_LABS_RAND_BYTES, CL_R_FUNCTION_NOT_BINDED);
  568. return 0;
  569. }
  570. return p_cl_rand_bytes(buf, num);
  571. }
  572. /*
  573. * This stuff is needed if this ENGINE is being compiled into a
  574. * self-contained shared-library.
  575. */
  576. # ifdef ENGINE_DYNAMIC_SUPPORT
  577. static int bind_fn(ENGINE *e, const char *id)
  578. {
  579. fprintf(stderr, "bind_fn CLUSTER_LABS\n");
  580. if (id && (strcmp(id, engine_cluster_labs_id) != 0)) {
  581. fprintf(stderr, "bind_fn return(0) first\n");
  582. return 0;
  583. }
  584. if (!bind_helper(e)) {
  585. fprintf(stderr, "bind_fn return(1) first\n");
  586. return 0;
  587. }
  588. fprintf(stderr, "bind_fn return(1)\n");
  589. return 1;
  590. }
  591. IMPLEMENT_DYNAMIC_CHECK_FN()
  592. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  593. # endif /* ENGINE_DYNAMIC_SUPPORT */
  594. # endif /* !NO_HW_CLUSTER_LABS */
  595. #endif /* !NO_HW */