hw_zencod.c 51 KB

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