hw_zencod.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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. # if 0
  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_SSLeay()" 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_SSLeay();
  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 (SSLeay) meth to supply what we don't provide ;-*)
  323. */
  324. meth_rand = RAND_SSLeay();
  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. if (!
  477. (ptr_1 =
  478. (t_zencod_bytes2bits *) DSO_bind_func(zencod_dso, ZENCOD_Fct_1))
  479. || !(ptr_2 = (t_zencod_bits2bytes *) DSO_bind_func(zencod_dso, ZENCOD_Fct_2))
  480. || !(ptr_3 = (t_zencod_new_number *) DSO_bind_func(zencod_dso, ZENCOD_Fct_3))
  481. || !(ptr_4 = (t_zencod_init_number *) DSO_bind_func(zencod_dso, ZENCOD_Fct_4))
  482. || !(ptr_exp_1 =
  483. (t_zencod_rsa_mod_exp *) DSO_bind_func(zencod_dso, ZENCOD_Fct_exp_1))
  484. || !(ptr_exp_2 =
  485. (t_zencod_rsa_mod_exp_crt *) DSO_bind_func(zencod_dso, ZENCOD_Fct_exp_2))
  486. || !(ptr_dsa_1 =
  487. (t_zencod_dsa_do_sign *) DSO_bind_func(zencod_dso, ZENCOD_Fct_dsa_1))
  488. || !(ptr_dsa_2 =
  489. (t_zencod_dsa_do_verify *) DSO_bind_func(zencod_dso, ZENCOD_Fct_dsa_2))
  490. || !(ptr_dh_1 =
  491. (t_zencod_dh_generate_key *) DSO_bind_func(zencod_dso, ZENCOD_Fct_dh_1))
  492. || !(ptr_dh_2 =
  493. (t_zencod_dh_compute_key *) DSO_bind_func(zencod_dso, ZENCOD_Fct_dh_2))
  494. || !(ptr_rand_1 =
  495. (t_zencod_rand_bytes *) DSO_bind_func(zencod_dso, ZENCOD_Fct_rand_1))
  496. || !(ptr_math_1 =
  497. (t_zencod_math_mod_exp *) DSO_bind_func(zencod_dso, ZENCOD_Fct_math_1))
  498. || !(ptr_0 = (t_zencod_test *) DSO_bind_func(zencod_dso, ZENCOD_Fct_0))
  499. || !(ptr_md5_1 =
  500. (t_zencod_md5_init *) DSO_bind_func(zencod_dso, ZENCOD_Fct_md5_1))
  501. || !(ptr_md5_2 =
  502. (t_zencod_md5_update *) DSO_bind_func(zencod_dso, ZENCOD_Fct_md5_2))
  503. || !(ptr_md5_3 =
  504. (t_zencod_md5_do_final *) DSO_bind_func(zencod_dso, ZENCOD_Fct_md5_3))
  505. || !(ptr_sha1_1 =
  506. (t_zencod_sha1_init *) DSO_bind_func(zencod_dso, ZENCOD_Fct_sha1_1))
  507. || !(ptr_sha1_2 =
  508. (t_zencod_sha1_update *) DSO_bind_func(zencod_dso, ZENCOD_Fct_sha1_2))
  509. || !(ptr_sha1_3 =
  510. (t_zencod_sha1_do_final *) DSO_bind_func(zencod_dso, ZENCOD_Fct_sha1_3))
  511. || !(ptr_xdes_1 =
  512. (t_zencod_xdes_cipher *) DSO_bind_func(zencod_dso, ZENCOD_Fct_xdes_1))
  513. || !(ptr_rc4_1 =
  514. (t_zencod_rc4_cipher *) DSO_bind_func(zencod_dso, ZENCOD_Fct_rc4_1))) {
  515. ZENCODerr(ZENCOD_F_ZENCOD_INIT, ZENCOD_R_DSO_FAILURE);
  516. goto err;
  517. }
  518. /*
  519. * The function from "cryptozen" Library have been correctly loaded so
  520. * copy them
  521. */
  522. ptr_zencod_test = ptr_0;
  523. ptr_zencod_bytes2bits = ptr_1;
  524. ptr_zencod_bits2bytes = ptr_2;
  525. ptr_zencod_new_number = ptr_3;
  526. ptr_zencod_init_number = ptr_4;
  527. ptr_zencod_rsa_mod_exp = ptr_exp_1;
  528. ptr_zencod_rsa_mod_exp_crt = ptr_exp_2;
  529. ptr_zencod_dsa_do_sign = ptr_dsa_1;
  530. ptr_zencod_dsa_do_verify = ptr_dsa_2;
  531. ptr_zencod_dh_generate_key = ptr_dh_1;
  532. ptr_zencod_dh_compute_key = ptr_dh_2;
  533. ptr_zencod_rand_bytes = ptr_rand_1;
  534. ptr_zencod_math_mod_exp = ptr_math_1;
  535. ptr_zencod_test = ptr_0;
  536. ptr_zencod_md5_init = ptr_md5_1;
  537. ptr_zencod_md5_update = ptr_md5_2;
  538. ptr_zencod_md5_do_final = ptr_md5_3;
  539. ptr_zencod_sha1_init = ptr_sha1_1;
  540. ptr_zencod_sha1_update = ptr_sha1_2;
  541. ptr_zencod_sha1_do_final = ptr_sha1_3;
  542. ptr_zencod_xdes_cipher = ptr_xdes_1;
  543. ptr_zencod_rc4_cipher = ptr_rc4_1;
  544. /*
  545. * We should perform a test to see if there is actually any unit runnig on
  546. * the system ... Even if the cryptozen library is loaded the module coul
  547. * not be loaded on the system ... For now we may just open and close the
  548. * device !!
  549. */
  550. if (ptr_zencod_test() != 0) {
  551. ZENCODerr(ZENCOD_F_ZENCOD_INIT, ZENCOD_R_UNIT_FAILURE);
  552. goto err;
  553. }
  554. return 1;
  555. err:
  556. if (zencod_dso) {
  557. DSO_free(zencod_dso);
  558. }
  559. zencod_dso = NULL;
  560. ptr_zencod_bytes2bits = NULL;
  561. ptr_zencod_bits2bytes = NULL;
  562. ptr_zencod_new_number = NULL;
  563. ptr_zencod_init_number = NULL;
  564. ptr_zencod_rsa_mod_exp = NULL;
  565. ptr_zencod_rsa_mod_exp_crt = NULL;
  566. ptr_zencod_dsa_do_sign = NULL;
  567. ptr_zencod_dsa_do_verify = NULL;
  568. ptr_zencod_dh_generate_key = NULL;
  569. ptr_zencod_dh_compute_key = NULL;
  570. ptr_zencod_rand_bytes = NULL;
  571. ptr_zencod_math_mod_exp = NULL;
  572. ptr_zencod_test = NULL;
  573. ptr_zencod_md5_init = NULL;
  574. ptr_zencod_md5_update = NULL;
  575. ptr_zencod_md5_do_final = NULL;
  576. ptr_zencod_sha1_init = NULL;
  577. ptr_zencod_sha1_update = NULL;
  578. ptr_zencod_sha1_do_final = NULL;
  579. ptr_zencod_xdes_cipher = NULL;
  580. ptr_zencod_rc4_cipher = NULL;
  581. return 0;
  582. }
  583. static int zencod_finish(ENGINE *e)
  584. {
  585. CHEESE();
  586. /*
  587. * We Should add some tests for non NULL parameters or bad value !!
  588. * Stuff to be done ...
  589. */
  590. if (zencod_dso == NULL) {
  591. ZENCODerr(ZENCOD_F_ZENCOD_FINISH, ZENCOD_R_NOT_LOADED);
  592. return 0;
  593. }
  594. if (!DSO_free(zencod_dso)) {
  595. ZENCODerr(ZENCOD_F_ZENCOD_FINISH, ZENCOD_R_DSO_FAILURE);
  596. return 0;
  597. }
  598. zencod_dso = NULL;
  599. ptr_zencod_bytes2bits = NULL;
  600. ptr_zencod_bits2bytes = NULL;
  601. ptr_zencod_new_number = NULL;
  602. ptr_zencod_init_number = NULL;
  603. ptr_zencod_rsa_mod_exp = NULL;
  604. ptr_zencod_rsa_mod_exp_crt = NULL;
  605. ptr_zencod_dsa_do_sign = NULL;
  606. ptr_zencod_dsa_do_verify = NULL;
  607. ptr_zencod_dh_generate_key = NULL;
  608. ptr_zencod_dh_compute_key = NULL;
  609. ptr_zencod_rand_bytes = NULL;
  610. ptr_zencod_math_mod_exp = NULL;
  611. ptr_zencod_test = NULL;
  612. ptr_zencod_md5_init = NULL;
  613. ptr_zencod_md5_update = NULL;
  614. ptr_zencod_md5_do_final = NULL;
  615. ptr_zencod_sha1_init = NULL;
  616. ptr_zencod_sha1_update = NULL;
  617. ptr_zencod_sha1_do_final = NULL;
  618. ptr_zencod_xdes_cipher = NULL;
  619. ptr_zencod_rc4_cipher = NULL;
  620. return 1;
  621. }
  622. static int zencod_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
  623. {
  624. int initialised = ((zencod_dso == NULL) ? 0 : 1);
  625. CHEESE();
  626. /*
  627. * We Should add some tests for non NULL parameters or bad value !!
  628. * Stuff to be done ...
  629. */
  630. switch (cmd) {
  631. case ZENCOD_CMD_SO_PATH:
  632. if (p == NULL) {
  633. ZENCODerr(ZENCOD_F_ZENCOD_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  634. return 0;
  635. }
  636. if (initialised) {
  637. ZENCODerr(ZENCOD_F_ZENCOD_CTRL, ZENCOD_R_ALREADY_LOADED);
  638. return 0;
  639. }
  640. ZENCOD_LIBNAME = (const char *)p;
  641. return 1;
  642. default:
  643. break;
  644. }
  645. ZENCODerr(ZENCOD_F_ZENCOD_CTRL, ZENCOD_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  646. return 0;
  647. }
  648. /*
  649. * BIGNUM stuff Functions
  650. */
  651. static int zencod_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  652. const BIGNUM *m, BN_CTX *ctx)
  653. {
  654. zen_nb_t y, x, e, n;
  655. int ret;
  656. CHEESE();
  657. if (!zencod_dso) {
  658. ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_NOT_LOADED);
  659. return 0;
  660. }
  661. if (!bn_wexpand(r, m->top + 1)) {
  662. ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_BN_EXPAND_FAIL);
  663. return 0;
  664. }
  665. memset(r->d, 0, BN_num_bytes(m));
  666. ptr_zencod_init_number(&y, (r->dmax - 1) * sizeof(BN_ULONG) * 8,
  667. (unsigned char *)r->d);
  668. BIGNUM2ZEN(&x, a);
  669. BIGNUM2ZEN(&e, p);
  670. BIGNUM2ZEN(&n, m);
  671. /* Must invert x and e parameter due to BN mod exp prototype ... */
  672. ret = ptr_zencod_math_mod_exp(&y, &e, &x, &n);
  673. if (ret) {
  674. PERROR("zenbridge_math_mod_exp");
  675. ENGINEerr(ZENCOD_F_ZENCOD_BN_MOD_EXP, ZENCOD_R_REQUEST_FAILED);
  676. return 0;
  677. }
  678. r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2;
  679. return 1;
  680. }
  681. /*
  682. * RSA stuff Functions
  683. */
  684. # ifndef OPENSSL_NO_RSA
  685. static int RSA_zencod_rsa_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa)
  686. {
  687. CHEESE();
  688. if (!zencod_dso) {
  689. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT, ZENCOD_R_NOT_LOADED);
  690. return 0;
  691. }
  692. if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  693. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT,
  694. ZENCOD_R_BAD_KEY_COMPONENTS);
  695. return 0;
  696. }
  697. /* Do in software if argument is too large for hardware */
  698. if (RSA_size(rsa) * 8 > ZENBRIDGE_MAX_KEYSIZE_RSA_CRT) {
  699. const RSA_METHOD *meth;
  700. meth = RSA_PKCS1_SSLeay();
  701. return meth->rsa_mod_exp(r0, i, rsa);
  702. } else {
  703. zen_nb_t y, x, p, q, dmp1, dmq1, iqmp;
  704. if (!bn_expand(r0, RSA_size(rsa) * 8)) {
  705. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT,
  706. ZENCOD_R_BN_EXPAND_FAIL);
  707. return 0;
  708. }
  709. r0->top = (RSA_size(rsa) * 8 + BN_BITS2 - 1) / BN_BITS2;
  710. BIGNUM2ZEN(&x, i);
  711. BIGNUM2ZEN(&y, r0);
  712. BIGNUM2ZEN(&p, rsa->p);
  713. BIGNUM2ZEN(&q, rsa->q);
  714. BIGNUM2ZEN(&dmp1, rsa->dmp1);
  715. BIGNUM2ZEN(&dmq1, rsa->dmq1);
  716. BIGNUM2ZEN(&iqmp, rsa->iqmp);
  717. if (ptr_zencod_rsa_mod_exp_crt(&y, &x, &p, &q, &dmp1, &dmq1, &iqmp) <
  718. 0) {
  719. PERROR("zenbridge_rsa_mod_exp_crt");
  720. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP_CRT,
  721. ZENCOD_R_REQUEST_FAILED);
  722. return 0;
  723. }
  724. return 1;
  725. }
  726. }
  727. /*
  728. * This function is aliased to RSA_mod_exp (with the mont stuff dropped).
  729. */
  730. static int RSA_zencod_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  731. const BIGNUM *m, BN_CTX *ctx,
  732. BN_MONT_CTX *m_ctx)
  733. {
  734. CHEESE();
  735. if (!zencod_dso) {
  736. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_NOT_LOADED);
  737. return 0;
  738. }
  739. /* Do in software if argument is too large for hardware */
  740. if (BN_num_bits(m) > ZENBRIDGE_MAX_KEYSIZE_RSA) {
  741. const RSA_METHOD *meth;
  742. meth = RSA_PKCS1_SSLeay();
  743. return meth->bn_mod_exp(r, a, p, m, ctx, m_ctx);
  744. } else {
  745. zen_nb_t y, x, e, n;
  746. if (!bn_expand(r, BN_num_bits(m))) {
  747. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_BN_EXPAND_FAIL);
  748. return 0;
  749. }
  750. r->top = (BN_num_bits(m) + BN_BITS2 - 1) / BN_BITS2;
  751. BIGNUM2ZEN(&x, a);
  752. BIGNUM2ZEN(&y, r);
  753. BIGNUM2ZEN(&e, p);
  754. BIGNUM2ZEN(&n, m);
  755. if (ptr_zencod_rsa_mod_exp(&y, &x, &n, &e) < 0) {
  756. PERROR("zenbridge_rsa_mod_exp");
  757. ENGINEerr(ZENCOD_F_ZENCOD_RSA_MOD_EXP, ZENCOD_R_REQUEST_FAILED);
  758. return 0;
  759. }
  760. return 1;
  761. }
  762. }
  763. # endif /* !OPENSSL_NO_RSA */
  764. # ifndef OPENSSL_NO_DSA
  765. /*
  766. * DSA stuff Functions
  767. */
  768. static DSA_SIG *DSA_zencod_do_sign(const unsigned char *dgst, int dlen,
  769. DSA *dsa)
  770. {
  771. zen_nb_t p, q, g, x, y, r, s, data;
  772. DSA_SIG *sig;
  773. BIGNUM *bn_r = NULL;
  774. BIGNUM *bn_s = NULL;
  775. char msg[20];
  776. CHEESE();
  777. if (!zencod_dso) {
  778. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_NOT_LOADED);
  779. goto FAILED;
  780. }
  781. if (dlen > 160) {
  782. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
  783. goto FAILED;
  784. }
  785. /* Do in software if argument is too large for hardware */
  786. if (BN_num_bits(dsa->p) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN ||
  787. BN_num_bits(dsa->g) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN) {
  788. const DSA_METHOD *meth;
  789. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
  790. meth = DSA_OpenSSL();
  791. return meth->dsa_do_sign(dgst, dlen, dsa);
  792. }
  793. if (!(bn_s = BN_new()) || !(bn_r = BN_new())) {
  794. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
  795. goto FAILED;
  796. }
  797. if (!bn_expand(bn_r, 160) || !bn_expand(bn_s, 160)) {
  798. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BN_EXPAND_FAIL);
  799. goto FAILED;
  800. }
  801. bn_r->top = bn_s->top = (160 + BN_BITS2 - 1) / BN_BITS2;
  802. BIGNUM2ZEN(&p, dsa->p);
  803. BIGNUM2ZEN(&q, dsa->q);
  804. BIGNUM2ZEN(&g, dsa->g);
  805. BIGNUM2ZEN(&x, dsa->priv_key);
  806. BIGNUM2ZEN(&y, dsa->pub_key);
  807. BIGNUM2ZEN(&r, bn_r);
  808. BIGNUM2ZEN(&s, bn_s);
  809. q.len = x.len = 160;
  810. ypcmem(msg, dgst, 20);
  811. ptr_zencod_init_number(&data, 160, msg);
  812. if (ptr_zencod_dsa_do_sign(0, &data, &y, &p, &q, &g, &x, &r, &s) < 0) {
  813. PERROR("zenbridge_dsa_do_sign");
  814. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
  815. goto FAILED;
  816. }
  817. if (!(sig = DSA_SIG_new())) {
  818. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
  819. goto FAILED;
  820. }
  821. sig->r = bn_r;
  822. sig->s = bn_s;
  823. return sig;
  824. FAILED:
  825. if (bn_r)
  826. BN_free(bn_r);
  827. if (bn_s)
  828. BN_free(bn_s);
  829. return NULL;
  830. }
  831. static int DSA_zencod_do_verify(const unsigned char *dgst, int dlen,
  832. DSA_SIG *sig, DSA *dsa)
  833. {
  834. zen_nb_t data, p, q, g, y, r, s, v;
  835. char msg[20];
  836. char v_data[20];
  837. int ret;
  838. CHEESE();
  839. if (!zencod_dso) {
  840. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY, ZENCOD_R_NOT_LOADED);
  841. return 0;
  842. }
  843. if (dlen > 160) {
  844. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_REQUEST_FAILED);
  845. return 0;
  846. }
  847. /* Do in software if argument is too large for hardware */
  848. if (BN_num_bits(dsa->p) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN ||
  849. BN_num_bits(dsa->g) > ZENBRIDGE_MAX_KEYSIZE_DSA_SIGN) {
  850. const DSA_METHOD *meth;
  851. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_SIGN, ZENCOD_R_BAD_KEY_COMPONENTS);
  852. meth = DSA_OpenSSL();
  853. return meth->dsa_do_verify(dgst, dlen, sig, dsa);
  854. }
  855. BIGNUM2ZEN(&p, dsa->p);
  856. BIGNUM2ZEN(&q, dsa->q);
  857. BIGNUM2ZEN(&g, dsa->g);
  858. BIGNUM2ZEN(&y, dsa->pub_key);
  859. BIGNUM2ZEN(&r, sig->r);
  860. BIGNUM2ZEN(&s, sig->s);
  861. ptr_zencod_init_number(&v, 160, v_data);
  862. ypcmem(msg, dgst, 20);
  863. ptr_zencod_init_number(&data, 160, msg);
  864. if ((ret =
  865. ptr_zencod_dsa_do_verify(0, &data, &p, &q, &g, &y, &r, &s,
  866. &v)) < 0) {
  867. PERROR("zenbridge_dsa_do_verify");
  868. ENGINEerr(ZENCOD_F_ZENCOD_DSA_DO_VERIFY, ZENCOD_R_REQUEST_FAILED);
  869. return 0;
  870. }
  871. return ((ret == 0) ? 1 : ret);
  872. }
  873. static int DSA_zencod_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
  874. const BIGNUM *p, const BIGNUM *m,
  875. BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  876. {
  877. CHEESE();
  878. return zencod_bn_mod_exp(r, a, p, m, ctx);
  879. }
  880. # endif /* !OPENSSL_NO_DSA */
  881. # ifndef OPENSSl_NO_DH
  882. /*
  883. * DH stuff Functions
  884. */
  885. static int DH_zencod_generate_key(DH *dh)
  886. {
  887. BIGNUM *bn_prv = NULL;
  888. BIGNUM *bn_pub = NULL;
  889. zen_nb_t y, x, g, p;
  890. int generate_x;
  891. CHEESE();
  892. if (!zencod_dso) {
  893. ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_NOT_LOADED);
  894. return 0;
  895. }
  896. /* Private key */
  897. if (dh->priv_key) {
  898. bn_prv = dh->priv_key;
  899. generate_x = 0;
  900. } else {
  901. if (!(bn_prv = BN_new())) {
  902. ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
  903. goto FAILED;
  904. }
  905. generate_x = 1;
  906. }
  907. /* Public key */
  908. if (dh->pub_key)
  909. bn_pub = dh->pub_key;
  910. else if (!(bn_pub = BN_new())) {
  911. ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
  912. goto FAILED;
  913. }
  914. /* Expand */
  915. if (!bn_wexpand(bn_prv, dh->p->dmax) || !bn_wexpand(bn_pub, dh->p->dmax)) {
  916. ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_BN_EXPAND_FAIL);
  917. goto FAILED;
  918. }
  919. bn_prv->top = dh->p->top;
  920. bn_pub->top = dh->p->top;
  921. /* Convert all keys */
  922. BIGNUM2ZEN(&p, dh->p);
  923. BIGNUM2ZEN(&g, dh->g);
  924. BIGNUM2ZEN(&y, bn_pub);
  925. BIGNUM2ZEN(&x, bn_prv);
  926. x.len = DH_size(dh) * 8;
  927. /* Adjust the lengths of P and G */
  928. p.len = ptr_zencod_bytes2bits(p.data, ZEN_BYTES(p.len));
  929. g.len = ptr_zencod_bytes2bits(g.data, ZEN_BYTES(g.len));
  930. /* Send the request to the driver */
  931. if (ptr_zencod_dh_generate_key(&y, &x, &g, &p, generate_x) < 0) {
  932. perror("zenbridge_dh_generate_key");
  933. ENGINEerr(ZENCOD_F_ZENCOD_DH_GENERATE, ZENCOD_R_REQUEST_FAILED);
  934. goto FAILED;
  935. }
  936. dh->priv_key = bn_prv;
  937. dh->pub_key = bn_pub;
  938. return 1;
  939. FAILED:
  940. if (!dh->priv_key && bn_prv)
  941. BN_free(bn_prv);
  942. if (!dh->pub_key && bn_pub)
  943. BN_free(bn_pub);
  944. return 0;
  945. }
  946. static int DH_zencod_compute_key(unsigned char *key, const BIGNUM *pub_key,
  947. DH *dh)
  948. {
  949. zen_nb_t y, x, p, k;
  950. CHEESE();
  951. if (!zencod_dso) {
  952. ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_NOT_LOADED);
  953. return 0;
  954. }
  955. if (!dh->priv_key) {
  956. ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_BAD_KEY_COMPONENTS);
  957. return 0;
  958. }
  959. /* Convert all keys */
  960. BIGNUM2ZEN(&y, pub_key);
  961. BIGNUM2ZEN(&x, dh->priv_key);
  962. BIGNUM2ZEN(&p, dh->p);
  963. ptr_zencod_init_number(&k, p.len, key);
  964. /* Adjust the lengths */
  965. p.len = ptr_zencod_bytes2bits(p.data, ZEN_BYTES(p.len));
  966. y.len = ptr_zencod_bytes2bits(y.data, ZEN_BYTES(y.len));
  967. x.len = ptr_zencod_bytes2bits(x.data, ZEN_BYTES(x.len));
  968. /* Call the hardware */
  969. if (ptr_zencod_dh_compute_key(&k, &y, &x, &p) < 0) {
  970. ENGINEerr(ZENCOD_F_ZENCOD_DH_COMPUTE, ZENCOD_R_REQUEST_FAILED);
  971. return 0;
  972. }
  973. /* The key must be written MSB -> LSB */
  974. k.len = ptr_zencod_bytes2bits(k.data, ZEN_BYTES(k.len));
  975. esrever(key, ZEN_BYTES(k.len));
  976. return ZEN_BYTES(k.len);
  977. }
  978. static int DH_zencod_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a,
  979. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  980. BN_MONT_CTX *m_ctx)
  981. {
  982. CHEESE();
  983. return zencod_bn_mod_exp(r, a, p, m, ctx);
  984. }
  985. # endif /* !OPENSSL_NO_DH */
  986. /*
  987. * RAND stuff Functions
  988. */
  989. static void RAND_zencod_seed(const void *buf, int num)
  990. {
  991. /*
  992. * Nothing to do cause our crypto accelerator provide a true random
  993. * generator
  994. */
  995. }
  996. static int RAND_zencod_rand_bytes(unsigned char *buf, int num)
  997. {
  998. zen_nb_t r;
  999. CHEESE();
  1000. if (!zencod_dso) {
  1001. ENGINEerr(ZENCOD_F_ZENCOD_RAND, ZENCOD_R_NOT_LOADED);
  1002. return 0;
  1003. }
  1004. ptr_zencod_init_number(&r, num * 8, buf);
  1005. if (ptr_zencod_rand_bytes(&r, ZENBRIDGE_RNG_DIRECT) < 0) {
  1006. PERROR("zenbridge_rand_bytes");
  1007. ENGINEerr(ZENCOD_F_ZENCOD_RAND, ZENCOD_R_REQUEST_FAILED);
  1008. return 0;
  1009. }
  1010. return 1;
  1011. }
  1012. static int RAND_zencod_rand_status(void)
  1013. {
  1014. CHEESE();
  1015. return 1;
  1016. }
  1017. /*
  1018. * This stuff is needed if this ENGINE is being compiled into a
  1019. * self-contained shared-library.
  1020. */
  1021. # ifdef ENGINE_DYNAMIC_SUPPORT
  1022. static int bind_fn(ENGINE *e, const char *id)
  1023. {
  1024. if (id && (strcmp(id, engine_zencod_id) != 0)) {
  1025. return 0;
  1026. }
  1027. if (!bind_helper(e)) {
  1028. return 0;
  1029. }
  1030. return 1;
  1031. }
  1032. IMPLEMENT_DYNAMIC_CHECK_FN()
  1033. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  1034. # endif /* ENGINE_DYNAMIC_SUPPORT */
  1035. /*
  1036. * Adding "Digest" and "Cipher" tools ...
  1037. * This is in development ... ;-)
  1038. * In orfer to code this, i refer to hw_openbsd_dev_crypto and openssl engine made by Geoff Thorpe (if i'm rigth),
  1039. * and evp, sha md5 definitions etc ...
  1040. */
  1041. /* First add some include ... */
  1042. # include <openssl/evp.h>
  1043. # include <openssl/sha.h>
  1044. # include <openssl/md5.h>
  1045. # include <openssl/rc4.h>
  1046. # include <openssl/des.h>
  1047. /* Some variables declaration ... */
  1048. /*
  1049. * DONS: Disable symetric computation except DES and 3DES, but let part
  1050. * of the code
  1051. */
  1052. /* static int engine_digest_nids [ ] = { NID_sha1, NID_md5 } ; */
  1053. static int engine_digest_nids[] = { };
  1054. static int engine_digest_nids_num = 0;
  1055. /*
  1056. * static int engine_cipher_nids [ ] = { NID_rc4, NID_rc4_40, NID_des_cbc,
  1057. * NID_des_ede3_cbc } ;
  1058. */
  1059. static int engine_cipher_nids[] = { NID_des_cbc, NID_des_ede3_cbc };
  1060. static int engine_cipher_nids_num = 2;
  1061. /* Function prototype ... */
  1062. /* SHA stuff */
  1063. static int engine_sha1_init(EVP_MD_CTX *ctx);
  1064. static int engine_sha1_update(EVP_MD_CTX *ctx, const void *data,
  1065. unsigned long count);
  1066. static int engine_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
  1067. /* MD5 stuff */
  1068. static int engine_md5_init(EVP_MD_CTX *ctx);
  1069. static int engine_md5_update(EVP_MD_CTX *ctx, const void *data,
  1070. unsigned long count);
  1071. static int engine_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
  1072. static int engine_md_cleanup(EVP_MD_CTX *ctx);
  1073. static int engine_md_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
  1074. /* RC4 Stuff */
  1075. static int engine_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1076. const unsigned char *iv, int enc);
  1077. static int engine_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1078. const unsigned char *in, unsigned int inl);
  1079. /* DES Stuff */
  1080. static int engine_des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1081. const unsigned char *iv, int enc);
  1082. static int engine_des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1083. const unsigned char *in, unsigned int inl);
  1084. /* 3DES Stuff */
  1085. static int engine_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
  1086. const unsigned char *key,
  1087. const unsigned char *iv, int enc);
  1088. static int engine_des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1089. const unsigned char *in,
  1090. unsigned int inl);
  1091. static int engine_cipher_cleanup(EVP_CIPHER_CTX *ctx); /* cleanup ctx */
  1092. /* The one for SHA ... */
  1093. static const EVP_MD engine_sha1_md = {
  1094. NID_sha1,
  1095. NID_sha1WithRSAEncryption,
  1096. SHA_DIGEST_LENGTH,
  1097. EVP_MD_FLAG_ONESHOT,
  1098. /*
  1099. * 0,
  1100. *//*
  1101. * EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block *
  1102. * XXX: set according to device info ...
  1103. */
  1104. engine_sha1_init,
  1105. engine_sha1_update,
  1106. engine_sha1_final,
  1107. engine_md_copy, /* dev_crypto_sha_copy */
  1108. engine_md_cleanup, /* dev_crypto_sha_cleanup */
  1109. EVP_PKEY_RSA_method,
  1110. SHA_CBLOCK,
  1111. /* sizeof ( EVP_MD * ) + sizeof ( SHA_CTX ) */
  1112. sizeof(ZEN_MD_DATA)
  1113. /*
  1114. * sizeof ( MD_CTX_DATA ) The message digest data structure ...
  1115. */
  1116. };
  1117. /* The one for MD5 ... */
  1118. static const EVP_MD engine_md5_md = {
  1119. NID_md5,
  1120. NID_md5WithRSAEncryption,
  1121. MD5_DIGEST_LENGTH,
  1122. EVP_MD_FLAG_ONESHOT,
  1123. /*
  1124. * 0,
  1125. *//*
  1126. * EVP_MD_FLAG_ONESHOT = x0001 digest can only handle a single block *
  1127. * XXX: set according to device info ...
  1128. */
  1129. engine_md5_init,
  1130. engine_md5_update,
  1131. engine_md5_final,
  1132. engine_md_copy, /* dev_crypto_md5_copy */
  1133. engine_md_cleanup, /* dev_crypto_md5_cleanup */
  1134. EVP_PKEY_RSA_method,
  1135. MD5_CBLOCK,
  1136. /* sizeof ( EVP_MD * ) + sizeof ( MD5_CTX ) */
  1137. sizeof(ZEN_MD_DATA)
  1138. /*
  1139. * sizeof ( MD_CTX_DATA ) The message digest data structure ...
  1140. */
  1141. };
  1142. /* The one for RC4 ... */
  1143. # define EVP_RC4_KEY_SIZE 16
  1144. /* Try something static ... */
  1145. typedef struct {
  1146. unsigned int len;
  1147. unsigned int first;
  1148. unsigned char rc4_state[260];
  1149. } NEW_ZEN_RC4_KEY;
  1150. # define rc4_data(ctx) ( (EVP_RC4_KEY *) ( ctx )->cipher_data )
  1151. static const EVP_CIPHER engine_rc4 = {
  1152. NID_rc4,
  1153. 1,
  1154. 16, /* EVP_RC4_KEY_SIZE should be 128 bits */
  1155. 0, /* FIXME: key should be up to 256 bytes */
  1156. EVP_CIPH_VARIABLE_LENGTH,
  1157. engine_rc4_init_key,
  1158. engine_rc4_cipher,
  1159. engine_cipher_cleanup,
  1160. sizeof(NEW_ZEN_RC4_KEY),
  1161. NULL,
  1162. NULL,
  1163. NULL
  1164. };
  1165. /* The one for RC4_40 ... */
  1166. static const EVP_CIPHER engine_rc4_40 = {
  1167. NID_rc4_40,
  1168. 1,
  1169. 5, /* 40 bits */
  1170. 0,
  1171. EVP_CIPH_VARIABLE_LENGTH,
  1172. engine_rc4_init_key,
  1173. engine_rc4_cipher,
  1174. engine_cipher_cleanup,
  1175. sizeof(NEW_ZEN_RC4_KEY),
  1176. NULL,
  1177. NULL,
  1178. NULL
  1179. };
  1180. /* The one for DES ... */
  1181. /* Try something static ... */
  1182. typedef struct {
  1183. unsigned char des_key[24];
  1184. unsigned char des_iv[8];
  1185. } ZEN_DES_KEY;
  1186. static const EVP_CIPHER engine_des_cbc = {
  1187. NID_des_cbc,
  1188. 8, 8, 8,
  1189. 0 | EVP_CIPH_CBC_MODE,
  1190. engine_des_init_key,
  1191. engine_des_cbc_cipher,
  1192. engine_cipher_cleanup,
  1193. sizeof(ZEN_DES_KEY),
  1194. EVP_CIPHER_set_asn1_iv,
  1195. EVP_CIPHER_get_asn1_iv,
  1196. NULL,
  1197. NULL
  1198. };
  1199. /* The one for 3DES ... */
  1200. /* Try something static ... */
  1201. typedef struct {
  1202. unsigned char des3_key[24];
  1203. unsigned char des3_iv[8];
  1204. } ZEN_3DES_KEY;
  1205. # define des_data(ctx) ( (DES_EDE_KEY *) ( ctx )->cipher_data )
  1206. static const EVP_CIPHER engine_des_ede3_cbc = {
  1207. NID_des_ede3_cbc,
  1208. 8, 8, 8,
  1209. 0 | EVP_CIPH_CBC_MODE,
  1210. engine_des_ede3_init_key,
  1211. engine_des_ede3_cbc_cipher,
  1212. engine_cipher_cleanup,
  1213. sizeof(ZEN_3DES_KEY),
  1214. EVP_CIPHER_set_asn1_iv,
  1215. EVP_CIPHER_get_asn1_iv,
  1216. NULL,
  1217. NULL
  1218. };
  1219. /* General function cloned on hw_openbsd_dev_crypto one ... */
  1220. static int engine_digests(ENGINE *e, const EVP_MD **digest, const int **nids,
  1221. int nid)
  1222. {
  1223. # ifdef DEBUG_ZENCOD_MD
  1224. fprintf(stderr, "\t=>Function : static int engine_digests () called !\n");
  1225. # endif
  1226. if (!digest) {
  1227. /* We are returning a list of supported nids */
  1228. *nids = engine_digest_nids;
  1229. return engine_digest_nids_num;
  1230. }
  1231. /* We are being asked for a specific digest */
  1232. if (nid == NID_md5) {
  1233. *digest = &engine_md5_md;
  1234. } else if (nid == NID_sha1) {
  1235. *digest = &engine_sha1_md;
  1236. } else {
  1237. *digest = NULL;
  1238. return 0;
  1239. }
  1240. return 1;
  1241. }
  1242. /*
  1243. * SHA stuff Functions
  1244. */
  1245. static int engine_sha1_init(EVP_MD_CTX *ctx)
  1246. {
  1247. int to_return = 0;
  1248. /* Test with zenbridge library ... */
  1249. to_return = ptr_zencod_sha1_init((ZEN_MD_DATA *)ctx->md_data);
  1250. to_return = !to_return;
  1251. return to_return;
  1252. }
  1253. static int engine_sha1_update(EVP_MD_CTX *ctx, const void *data,
  1254. unsigned long count)
  1255. {
  1256. zen_nb_t input;
  1257. int to_return = 0;
  1258. /* Convert parameters ... */
  1259. input.len = count;
  1260. input.data = (unsigned char *)data;
  1261. /* Test with zenbridge library ... */
  1262. to_return =
  1263. ptr_zencod_sha1_update((ZEN_MD_DATA *)ctx->md_data,
  1264. (const zen_nb_t *)&input);
  1265. to_return = !to_return;
  1266. return to_return;
  1267. }
  1268. static int engine_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  1269. {
  1270. zen_nb_t output;
  1271. int to_return = 0;
  1272. /* Convert parameters ... */
  1273. output.len = SHA_DIGEST_LENGTH;
  1274. output.data = md;
  1275. /* Test with zenbridge library ... */
  1276. to_return =
  1277. ptr_zencod_sha1_do_final((ZEN_MD_DATA *)ctx->md_data,
  1278. (zen_nb_t *) & output);
  1279. to_return = !to_return;
  1280. return to_return;
  1281. }
  1282. /*
  1283. * MD5 stuff Functions
  1284. */
  1285. static int engine_md5_init(EVP_MD_CTX *ctx)
  1286. {
  1287. int to_return = 0;
  1288. /* Test with zenbridge library ... */
  1289. to_return = ptr_zencod_md5_init((ZEN_MD_DATA *)ctx->md_data);
  1290. to_return = !to_return;
  1291. return to_return;
  1292. }
  1293. static int engine_md5_update(EVP_MD_CTX *ctx, const void *data,
  1294. unsigned long count)
  1295. {
  1296. zen_nb_t input;
  1297. int to_return = 0;
  1298. /* Convert parameters ... */
  1299. input.len = count;
  1300. input.data = (unsigned char *)data;
  1301. /* Test with zenbridge library ... */
  1302. to_return =
  1303. ptr_zencod_md5_update((ZEN_MD_DATA *)ctx->md_data,
  1304. (const zen_nb_t *)&input);
  1305. to_return = !to_return;
  1306. return to_return;
  1307. }
  1308. static int engine_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
  1309. {
  1310. zen_nb_t output;
  1311. int to_return = 0;
  1312. /* Convert parameters ... */
  1313. output.len = MD5_DIGEST_LENGTH;
  1314. output.data = md;
  1315. /* Test with zenbridge library ... */
  1316. to_return =
  1317. ptr_zencod_md5_do_final((ZEN_MD_DATA *)ctx->md_data,
  1318. (zen_nb_t *) & output);
  1319. to_return = !to_return;
  1320. return to_return;
  1321. }
  1322. static int engine_md_cleanup(EVP_MD_CTX *ctx)
  1323. {
  1324. ZEN_MD_DATA *zen_md_data = (ZEN_MD_DATA *)ctx->md_data;
  1325. if (zen_md_data->HashBuffer != NULL) {
  1326. OPENSSL_free(zen_md_data->HashBuffer);
  1327. zen_md_data->HashBufferSize = 0;
  1328. ctx->md_data = NULL;
  1329. }
  1330. return 1;
  1331. }
  1332. static int engine_md_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  1333. {
  1334. const ZEN_MD_DATA *from_md = (ZEN_MD_DATA *)from->md_data;
  1335. ZEN_MD_DATA *to_md = (ZEN_MD_DATA *)to->md_data;
  1336. to_md->HashBuffer = OPENSSL_malloc(from_md->HashBufferSize);
  1337. memcpy(to_md->HashBuffer, from_md->HashBuffer, from_md->HashBufferSize);
  1338. return 1;
  1339. }
  1340. /* General function cloned on hw_openbsd_dev_crypto one ... */
  1341. static int engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  1342. const int **nids, int nid)
  1343. {
  1344. if (!cipher) {
  1345. /* We are returning a list of supported nids */
  1346. *nids = engine_cipher_nids;
  1347. return engine_cipher_nids_num;
  1348. }
  1349. /* We are being asked for a specific cipher */
  1350. if (nid == NID_rc4) {
  1351. *cipher = &engine_rc4;
  1352. } else if (nid == NID_rc4_40) {
  1353. *cipher = &engine_rc4_40;
  1354. } else if (nid == NID_des_cbc) {
  1355. *cipher = &engine_des_cbc;
  1356. } else if (nid == NID_des_ede3_cbc) {
  1357. *cipher = &engine_des_ede3_cbc;
  1358. } else {
  1359. *cipher = NULL;
  1360. return 0;
  1361. }
  1362. return 1;
  1363. }
  1364. static int engine_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1365. const unsigned char *iv, int enc)
  1366. {
  1367. int to_return = 0;
  1368. int i = 0;
  1369. int nb = 0;
  1370. NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL;
  1371. tmp_rc4_key = (NEW_ZEN_RC4_KEY *) (ctx->cipher_data);
  1372. tmp_rc4_key->first = 0;
  1373. tmp_rc4_key->len = ctx->key_len;
  1374. tmp_rc4_key->rc4_state[0] = 0x00;
  1375. tmp_rc4_key->rc4_state[2] = 0x00;
  1376. nb = 256 / ctx->key_len;
  1377. for (i = 0; i < nb; i++) {
  1378. memcpy(&(tmp_rc4_key->rc4_state[4 + i * ctx->key_len]), key,
  1379. ctx->key_len);
  1380. }
  1381. to_return = 1;
  1382. return to_return;
  1383. }
  1384. static int engine_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1385. const unsigned char *in, unsigned int in_len)
  1386. {
  1387. zen_nb_t output, input;
  1388. zen_nb_t rc4key;
  1389. int to_return = 0;
  1390. NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL;
  1391. /* Convert parameters ... */
  1392. input.len = in_len;
  1393. input.data = (unsigned char *)in;
  1394. output.len = in_len;
  1395. output.data = (unsigned char *)out;
  1396. tmp_rc4_key = ((NEW_ZEN_RC4_KEY *) (ctx->cipher_data));
  1397. rc4key.len = 260;
  1398. rc4key.data = &(tmp_rc4_key->rc4_state[0]);
  1399. /* Test with zenbridge library ... */
  1400. to_return =
  1401. ptr_zencod_rc4_cipher(&output, &input, (const zen_nb_t *)&rc4key,
  1402. &(tmp_rc4_key->rc4_state[0]),
  1403. &(tmp_rc4_key->rc4_state[3]),
  1404. !tmp_rc4_key->first);
  1405. to_return = !to_return;
  1406. /* Update encryption state ... */
  1407. tmp_rc4_key->first = 1;
  1408. tmp_rc4_key = NULL;
  1409. return to_return;
  1410. }
  1411. static int engine_des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1412. const unsigned char *iv, int enc)
  1413. {
  1414. ZEN_DES_KEY *tmp_des_key = NULL;
  1415. int to_return = 0;
  1416. tmp_des_key = (ZEN_DES_KEY *) (ctx->cipher_data);
  1417. memcpy(&(tmp_des_key->des_key[0]), key, 8);
  1418. memcpy(&(tmp_des_key->des_key[8]), key, 8);
  1419. memcpy(&(tmp_des_key->des_key[16]), key, 8);
  1420. memcpy(&(tmp_des_key->des_iv[0]), iv, 8);
  1421. to_return = 1;
  1422. return to_return;
  1423. }
  1424. static int engine_des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1425. const unsigned char *in, unsigned int inl)
  1426. {
  1427. zen_nb_t output, input;
  1428. zen_nb_t deskey_1, deskey_2, deskey_3, iv;
  1429. int to_return = 0;
  1430. /* Convert parameters ... */
  1431. input.len = inl;
  1432. input.data = (unsigned char *)in;
  1433. output.len = inl;
  1434. output.data = out;
  1435. /* Set key parameters ... */
  1436. deskey_1.len = 8;
  1437. deskey_2.len = 8;
  1438. deskey_3.len = 8;
  1439. deskey_1.data =
  1440. (unsigned char *)((ZEN_DES_KEY *) (ctx->cipher_data))->des_key;
  1441. deskey_2.data =
  1442. (unsigned char *)&((ZEN_DES_KEY *) (ctx->cipher_data))->des_key[8];
  1443. deskey_3.data =
  1444. (unsigned char *)&((ZEN_DES_KEY *) (ctx->cipher_data))->des_key[16];
  1445. /* Key correct iv ... */
  1446. memcpy(((ZEN_DES_KEY *) (ctx->cipher_data))->des_iv, ctx->iv, 8);
  1447. iv.len = 8;
  1448. iv.data = (unsigned char *)((ZEN_DES_KEY *) (ctx->cipher_data))->des_iv;
  1449. if (ctx->encrypt == 0) {
  1450. memcpy(ctx->iv, &(input.data[input.len - 8]), 8);
  1451. }
  1452. /* Test with zenbridge library ... */
  1453. to_return = ptr_zencod_xdes_cipher(&output, &input,
  1454. (zen_nb_t *) & deskey_1,
  1455. (zen_nb_t *) & deskey_2,
  1456. (zen_nb_t *) & deskey_3, &iv,
  1457. ctx->encrypt);
  1458. to_return = !to_return;
  1459. /*
  1460. * But we need to set up the rigth iv ... Test ENCRYPT or DECRYPT mode to
  1461. * set iv ...
  1462. */
  1463. if (ctx->encrypt == 1) {
  1464. memcpy(ctx->iv, &(output.data[output.len - 8]), 8);
  1465. }
  1466. return to_return;
  1467. }
  1468. static int engine_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
  1469. const unsigned char *key,
  1470. const unsigned char *iv, int enc)
  1471. {
  1472. ZEN_3DES_KEY *tmp_3des_key = NULL;
  1473. int to_return = 0;
  1474. tmp_3des_key = (ZEN_3DES_KEY *) (ctx->cipher_data);
  1475. memcpy(&(tmp_3des_key->des3_key[0]), key, 24);
  1476. memcpy(&(tmp_3des_key->des3_iv[0]), iv, 8);
  1477. to_return = 1;
  1478. return to_return;
  1479. }
  1480. static int engine_des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1481. const unsigned char *in,
  1482. unsigned int in_len)
  1483. {
  1484. zen_nb_t output, input;
  1485. zen_nb_t deskey_1, deskey_2, deskey_3, iv;
  1486. int to_return = 0;
  1487. /* Convert parameters ... */
  1488. input.len = in_len;
  1489. input.data = (unsigned char *)in;
  1490. output.len = in_len;
  1491. output.data = out;
  1492. /* Set key ... */
  1493. deskey_1.len = 8;
  1494. deskey_2.len = 8;
  1495. deskey_3.len = 8;
  1496. deskey_1.data =
  1497. (unsigned char *)((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_key;
  1498. deskey_2.data =
  1499. (unsigned char *)&((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_key[8];
  1500. deskey_3.data =
  1501. (unsigned char *)&((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_key[16];
  1502. /* Key correct iv ... */
  1503. memcpy(((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_iv, ctx->iv, 8);
  1504. iv.len = 8;
  1505. iv.data = (unsigned char *)((ZEN_3DES_KEY *) (ctx->cipher_data))->des3_iv;
  1506. if (ctx->encrypt == 0) {
  1507. memcpy(ctx->iv, &(input.data[input.len - 8]), 8);
  1508. }
  1509. /* Test with zenbridge library ... */
  1510. to_return = ptr_zencod_xdes_cipher(&output, &input,
  1511. (zen_nb_t *) & deskey_1,
  1512. (zen_nb_t *) & deskey_2,
  1513. (zen_nb_t *) & deskey_3, &iv,
  1514. ctx->encrypt);
  1515. to_return = !to_return;
  1516. if (ctx->encrypt == 1) {
  1517. memcpy(ctx->iv, &(output.data[output.len - 8]), 8);
  1518. }
  1519. return to_return;
  1520. }
  1521. static int engine_cipher_cleanup(EVP_CIPHER_CTX *ctx)
  1522. {
  1523. /* Set the key pointer ... */
  1524. if (ctx->cipher->nid == NID_rc4 || ctx->cipher->nid == NID_rc4_40) {
  1525. } else if (ctx->cipher->nid == NID_des_cbc) {
  1526. } else if (ctx->cipher->nid == NID_des_ede3_cbc) {
  1527. }
  1528. return 1;
  1529. }
  1530. # endif /* !OPENSSL_NO_HW_ZENCOD */
  1531. #endif /* !OPENSSL_NO_HW */