e_cswift.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /* crypto/engine/hw_cswift.c */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <openssl/crypto.h>
  61. #include <openssl/buffer.h>
  62. #include <openssl/dso.h>
  63. #include <openssl/engine.h>
  64. #ifndef OPENSSL_NO_RSA
  65. #include <openssl/rsa.h>
  66. #endif
  67. #ifndef OPENSSL_NO_DSA
  68. #include <openssl/dsa.h>
  69. #endif
  70. #ifndef OPENSSL_NO_DH
  71. #include <openssl/dh.h>
  72. #endif
  73. #include <openssl/rand.h>
  74. #include <openssl/bn.h>
  75. #ifndef OPENSSL_NO_HW
  76. #ifndef OPENSSL_NO_HW_CSWIFT
  77. /* Attribution notice: Rainbow have generously allowed me to reproduce
  78. * the necessary definitions here from their API. This means the support
  79. * can build independently of whether application builders have the
  80. * API or hardware. This will allow developers to easily produce software
  81. * that has latent hardware support for any users that have accelerators
  82. * installed, without the developers themselves needing anything extra.
  83. *
  84. * I have only clipped the parts from the CryptoSwift header files that
  85. * are (or seem) relevant to the CryptoSwift support code. This is
  86. * simply to keep the file sizes reasonable.
  87. * [Geoff]
  88. */
  89. #ifdef FLAT_INC
  90. #include "cswift.h"
  91. #else
  92. #include "vendor_defns/cswift.h"
  93. #endif
  94. #define CSWIFT_LIB_NAME "cswift engine"
  95. #include "e_cswift_err.c"
  96. #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
  97. static int cswift_destroy(ENGINE *e);
  98. static int cswift_init(ENGINE *e);
  99. static int cswift_finish(ENGINE *e);
  100. static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
  101. #ifndef OPENSSL_NO_RSA
  102. static int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in);
  103. #endif
  104. /* BIGNUM stuff */
  105. static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  106. const BIGNUM *m, BN_CTX *ctx);
  107. #ifndef OPENSSL_NO_RSA
  108. static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  109. const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
  110. const BIGNUM *iqmp, BN_CTX *ctx);
  111. #endif
  112. #ifndef OPENSSL_NO_RSA
  113. /* RSA stuff */
  114. static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  115. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  116. static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  117. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  118. #endif
  119. #ifndef OPENSSL_NO_DSA
  120. /* DSA stuff */
  121. static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
  122. static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
  123. DSA_SIG *sig, DSA *dsa);
  124. #endif
  125. #ifndef OPENSSL_NO_DH
  126. /* DH stuff */
  127. /* This function is alised to mod_exp (with the DH and mont dropped). */
  128. static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
  129. const BIGNUM *a, const BIGNUM *p,
  130. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  131. #endif
  132. /* RAND stuff */
  133. static int cswift_rand_bytes(unsigned char *buf, int num);
  134. static int cswift_rand_status(void);
  135. /* The definitions for control commands specific to this engine */
  136. #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
  137. static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
  138. {CSWIFT_CMD_SO_PATH,
  139. "SO_PATH",
  140. "Specifies the path to the 'cswift' shared library",
  141. ENGINE_CMD_FLAG_STRING},
  142. {0, NULL, NULL, 0}
  143. };
  144. #ifndef OPENSSL_NO_RSA
  145. /* Our internal RSA_METHOD that we provide pointers to */
  146. static RSA_METHOD cswift_rsa =
  147. {
  148. "CryptoSwift RSA method",
  149. NULL,
  150. NULL,
  151. NULL,
  152. NULL,
  153. cswift_rsa_mod_exp,
  154. cswift_mod_exp_mont,
  155. NULL,
  156. NULL,
  157. 0,
  158. NULL,
  159. NULL,
  160. NULL,
  161. NULL
  162. };
  163. #endif
  164. #ifndef OPENSSL_NO_DSA
  165. /* Our internal DSA_METHOD that we provide pointers to */
  166. static DSA_METHOD cswift_dsa =
  167. {
  168. "CryptoSwift DSA method",
  169. cswift_dsa_sign,
  170. NULL, /* dsa_sign_setup */
  171. cswift_dsa_verify,
  172. NULL, /* dsa_mod_exp */
  173. NULL, /* bn_mod_exp */
  174. NULL, /* init */
  175. NULL, /* finish */
  176. 0, /* flags */
  177. NULL, /* app_data */
  178. NULL, /* dsa_paramgen */
  179. NULL /* dsa_keygen */
  180. };
  181. #endif
  182. #ifndef OPENSSL_NO_DH
  183. /* Our internal DH_METHOD that we provide pointers to */
  184. static DH_METHOD cswift_dh =
  185. {
  186. "CryptoSwift DH method",
  187. NULL,
  188. NULL,
  189. cswift_mod_exp_dh,
  190. NULL,
  191. NULL,
  192. 0,
  193. NULL,
  194. NULL
  195. };
  196. #endif
  197. static RAND_METHOD cswift_random =
  198. {
  199. /* "CryptoSwift RAND method", */
  200. NULL,
  201. cswift_rand_bytes,
  202. NULL,
  203. NULL,
  204. cswift_rand_bytes,
  205. cswift_rand_status,
  206. };
  207. /* Constants used when creating the ENGINE */
  208. static const char *engine_cswift_id = "cswift";
  209. static const char *engine_cswift_name = "CryptoSwift hardware engine support";
  210. /* This internal function is used by ENGINE_cswift() and possibly by the
  211. * "dynamic" ENGINE support too */
  212. static int bind_helper(ENGINE *e)
  213. {
  214. #ifndef OPENSSL_NO_RSA
  215. const RSA_METHOD *meth1;
  216. #endif
  217. #ifndef OPENSSL_NO_DH
  218. const DH_METHOD *meth2;
  219. #endif
  220. if(!ENGINE_set_id(e, engine_cswift_id) ||
  221. !ENGINE_set_name(e, engine_cswift_name) ||
  222. #ifndef OPENSSL_NO_RSA
  223. !ENGINE_set_RSA(e, &cswift_rsa) ||
  224. #endif
  225. #ifndef OPENSSL_NO_DSA
  226. !ENGINE_set_DSA(e, &cswift_dsa) ||
  227. #endif
  228. #ifndef OPENSSL_NO_DH
  229. !ENGINE_set_DH(e, &cswift_dh) ||
  230. #endif
  231. !ENGINE_set_RAND(e, &cswift_random) ||
  232. !ENGINE_set_destroy_function(e, cswift_destroy) ||
  233. !ENGINE_set_init_function(e, cswift_init) ||
  234. !ENGINE_set_finish_function(e, cswift_finish) ||
  235. !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
  236. !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
  237. return 0;
  238. #ifndef OPENSSL_NO_RSA
  239. /* We know that the "PKCS1_SSLeay()" functions hook properly
  240. * to the cswift-specific mod_exp and mod_exp_crt so we use
  241. * those functions. NB: We don't use ENGINE_openssl() or
  242. * anything "more generic" because something like the RSAref
  243. * code may not hook properly, and if you own one of these
  244. * cards then you have the right to do RSA operations on it
  245. * anyway! */
  246. meth1 = RSA_PKCS1_SSLeay();
  247. cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  248. cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  249. cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  250. cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  251. #endif
  252. #ifndef OPENSSL_NO_DH
  253. /* Much the same for Diffie-Hellman */
  254. meth2 = DH_OpenSSL();
  255. cswift_dh.generate_key = meth2->generate_key;
  256. cswift_dh.compute_key = meth2->compute_key;
  257. #endif
  258. /* Ensure the cswift error handling is set up */
  259. ERR_load_CSWIFT_strings();
  260. return 1;
  261. }
  262. #ifdef OPENSSL_NO_DYNAMIC_ENGINE
  263. static ENGINE *engine_cswift(void)
  264. {
  265. ENGINE *ret = ENGINE_new();
  266. if(!ret)
  267. return NULL;
  268. if(!bind_helper(ret))
  269. {
  270. ENGINE_free(ret);
  271. return NULL;
  272. }
  273. return ret;
  274. }
  275. void ENGINE_load_cswift(void)
  276. {
  277. /* Copied from eng_[openssl|dyn].c */
  278. ENGINE *toadd = engine_cswift();
  279. if(!toadd) return;
  280. ENGINE_add(toadd);
  281. ENGINE_free(toadd);
  282. ERR_clear_error();
  283. }
  284. #endif
  285. /* This is a process-global DSO handle used for loading and unloading
  286. * the CryptoSwift library. NB: This is only set (or unset) during an
  287. * init() or finish() call (reference counts permitting) and they're
  288. * operating with global locks, so this should be thread-safe
  289. * implicitly. */
  290. static DSO *cswift_dso = NULL;
  291. /* These are the function pointers that are (un)set when the library has
  292. * successfully (un)loaded. */
  293. t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
  294. t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
  295. t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
  296. t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
  297. /* Used in the DSO operations. */
  298. static const char *CSWIFT_LIBNAME = NULL;
  299. static const char *get_CSWIFT_LIBNAME(void)
  300. {
  301. if(CSWIFT_LIBNAME)
  302. return CSWIFT_LIBNAME;
  303. return "swift";
  304. }
  305. static void free_CSWIFT_LIBNAME(void)
  306. {
  307. if(CSWIFT_LIBNAME)
  308. OPENSSL_free((void*)CSWIFT_LIBNAME);
  309. CSWIFT_LIBNAME = NULL;
  310. }
  311. static long set_CSWIFT_LIBNAME(const char *name)
  312. {
  313. free_CSWIFT_LIBNAME();
  314. return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
  315. }
  316. static const char *CSWIFT_F1 = "swAcquireAccContext";
  317. static const char *CSWIFT_F2 = "swAttachKeyParam";
  318. static const char *CSWIFT_F3 = "swSimpleRequest";
  319. static const char *CSWIFT_F4 = "swReleaseAccContext";
  320. /* CryptoSwift library functions and mechanics - these are used by the
  321. * higher-level functions further down. NB: As and where there's no
  322. * error checking, take a look lower down where these functions are
  323. * called, the checking and error handling is probably down there. */
  324. /* utility function to obtain a context */
  325. static int get_context(SW_CONTEXT_HANDLE *hac)
  326. {
  327. SW_STATUS status;
  328. status = p_CSwift_AcquireAccContext(hac);
  329. if(status != SW_OK)
  330. return 0;
  331. return 1;
  332. }
  333. /* similarly to release one. */
  334. static void release_context(SW_CONTEXT_HANDLE hac)
  335. {
  336. p_CSwift_ReleaseAccContext(hac);
  337. }
  338. /* Destructor (complements the "ENGINE_cswift()" constructor) */
  339. static int cswift_destroy(ENGINE *e)
  340. {
  341. free_CSWIFT_LIBNAME();
  342. ERR_unload_CSWIFT_strings();
  343. return 1;
  344. }
  345. /* (de)initialisation functions. */
  346. static int cswift_init(ENGINE *e)
  347. {
  348. SW_CONTEXT_HANDLE hac;
  349. t_swAcquireAccContext *p1;
  350. t_swAttachKeyParam *p2;
  351. t_swSimpleRequest *p3;
  352. t_swReleaseAccContext *p4;
  353. if(cswift_dso != NULL)
  354. {
  355. CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
  356. goto err;
  357. }
  358. /* Attempt to load libswift.so/swift.dll/whatever. */
  359. cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
  360. if(cswift_dso == NULL)
  361. {
  362. CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
  363. goto err;
  364. }
  365. if(!(p1 = (t_swAcquireAccContext *)
  366. DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
  367. !(p2 = (t_swAttachKeyParam *)
  368. DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
  369. !(p3 = (t_swSimpleRequest *)
  370. DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
  371. !(p4 = (t_swReleaseAccContext *)
  372. DSO_bind_func(cswift_dso, CSWIFT_F4)))
  373. {
  374. CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
  375. goto err;
  376. }
  377. /* Copy the pointers */
  378. p_CSwift_AcquireAccContext = p1;
  379. p_CSwift_AttachKeyParam = p2;
  380. p_CSwift_SimpleRequest = p3;
  381. p_CSwift_ReleaseAccContext = p4;
  382. /* Try and get a context - if not, we may have a DSO but no
  383. * accelerator! */
  384. if(!get_context(&hac))
  385. {
  386. CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
  387. goto err;
  388. }
  389. release_context(hac);
  390. /* Everything's fine. */
  391. return 1;
  392. err:
  393. if(cswift_dso)
  394. {
  395. DSO_free(cswift_dso);
  396. cswift_dso = NULL;
  397. }
  398. p_CSwift_AcquireAccContext = NULL;
  399. p_CSwift_AttachKeyParam = NULL;
  400. p_CSwift_SimpleRequest = NULL;
  401. p_CSwift_ReleaseAccContext = NULL;
  402. return 0;
  403. }
  404. static int cswift_finish(ENGINE *e)
  405. {
  406. free_CSWIFT_LIBNAME();
  407. if(cswift_dso == NULL)
  408. {
  409. CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
  410. return 0;
  411. }
  412. if(!DSO_free(cswift_dso))
  413. {
  414. CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
  415. return 0;
  416. }
  417. cswift_dso = NULL;
  418. p_CSwift_AcquireAccContext = NULL;
  419. p_CSwift_AttachKeyParam = NULL;
  420. p_CSwift_SimpleRequest = NULL;
  421. p_CSwift_ReleaseAccContext = NULL;
  422. return 1;
  423. }
  424. static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  425. {
  426. int initialised = ((cswift_dso == NULL) ? 0 : 1);
  427. switch(cmd)
  428. {
  429. case CSWIFT_CMD_SO_PATH:
  430. if(p == NULL)
  431. {
  432. CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
  433. return 0;
  434. }
  435. if(initialised)
  436. {
  437. CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
  438. return 0;
  439. }
  440. return set_CSWIFT_LIBNAME((const char *)p);
  441. default:
  442. break;
  443. }
  444. CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  445. return 0;
  446. }
  447. /* Un petit mod_exp */
  448. static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  449. const BIGNUM *m, BN_CTX *ctx)
  450. {
  451. /* I need somewhere to store temporary serialised values for
  452. * use with the CryptoSwift API calls. A neat cheat - I'll use
  453. * BIGNUMs from the BN_CTX but access their arrays directly as
  454. * byte arrays <grin>. This way I don't have to clean anything
  455. * up. */
  456. BIGNUM *modulus;
  457. BIGNUM *exponent;
  458. BIGNUM *argument;
  459. BIGNUM *result;
  460. SW_STATUS sw_status;
  461. SW_LARGENUMBER arg, res;
  462. SW_PARAM sw_param;
  463. SW_CONTEXT_HANDLE hac;
  464. int to_return, acquired;
  465. modulus = exponent = argument = result = NULL;
  466. to_return = 0; /* expect failure */
  467. acquired = 0;
  468. if(!get_context(&hac))
  469. {
  470. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
  471. goto err;
  472. }
  473. acquired = 1;
  474. /* Prepare the params */
  475. BN_CTX_start(ctx);
  476. modulus = BN_CTX_get(ctx);
  477. exponent = BN_CTX_get(ctx);
  478. argument = BN_CTX_get(ctx);
  479. result = BN_CTX_get(ctx);
  480. if(!result)
  481. {
  482. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
  483. goto err;
  484. }
  485. if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
  486. !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
  487. {
  488. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
  489. goto err;
  490. }
  491. sw_param.type = SW_ALG_EXP;
  492. sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
  493. (unsigned char *)modulus->d);
  494. sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
  495. sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
  496. (unsigned char *)exponent->d);
  497. sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
  498. /* Attach the key params */
  499. sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
  500. switch(sw_status)
  501. {
  502. case SW_OK:
  503. break;
  504. case SW_ERR_INPUT_SIZE:
  505. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
  506. goto err;
  507. default:
  508. {
  509. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  510. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
  511. sprintf(tmpbuf, "%ld", sw_status);
  512. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  513. }
  514. goto err;
  515. }
  516. /* Prepare the argument and response */
  517. arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
  518. arg.value = (unsigned char *)argument->d;
  519. res.nbytes = BN_num_bytes(m);
  520. memset(result->d, 0, res.nbytes);
  521. res.value = (unsigned char *)result->d;
  522. /* Perform the operation */
  523. if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
  524. &res, 1)) != SW_OK)
  525. {
  526. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  527. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
  528. sprintf(tmpbuf, "%ld", sw_status);
  529. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  530. goto err;
  531. }
  532. /* Convert the response */
  533. BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
  534. to_return = 1;
  535. err:
  536. if(acquired)
  537. release_context(hac);
  538. BN_CTX_end(ctx);
  539. return to_return;
  540. }
  541. #ifndef OPENSSL_NO_RSA
  542. int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in)
  543. {
  544. int mod;
  545. int numbytes = BN_num_bytes(in);
  546. mod = 0;
  547. while( ((out->nbytes = (numbytes+mod)) % 32) )
  548. {
  549. mod++;
  550. }
  551. out->value = (unsigned char*)OPENSSL_malloc(out->nbytes);
  552. if(!out->value)
  553. {
  554. return 0;
  555. }
  556. BN_bn2bin(in, &out->value[mod]);
  557. if(mod)
  558. memset(out->value, 0, mod);
  559. return 1;
  560. }
  561. #endif
  562. #ifndef OPENSSL_NO_RSA
  563. /* Un petit mod_exp chinois */
  564. static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  565. const BIGNUM *q, const BIGNUM *dmp1,
  566. const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
  567. {
  568. SW_STATUS sw_status;
  569. SW_LARGENUMBER arg, res;
  570. SW_PARAM sw_param;
  571. SW_CONTEXT_HANDLE hac;
  572. BIGNUM *result = NULL;
  573. BIGNUM *argument = NULL;
  574. int to_return = 0; /* expect failure */
  575. int acquired = 0;
  576. sw_param.up.crt.p.value = NULL;
  577. sw_param.up.crt.q.value = NULL;
  578. sw_param.up.crt.dmp1.value = NULL;
  579. sw_param.up.crt.dmq1.value = NULL;
  580. sw_param.up.crt.iqmp.value = NULL;
  581. if(!get_context(&hac))
  582. {
  583. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
  584. goto err;
  585. }
  586. acquired = 1;
  587. /* Prepare the params */
  588. argument = BN_new();
  589. result = BN_new();
  590. if(!result || !argument)
  591. {
  592. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
  593. goto err;
  594. }
  595. sw_param.type = SW_ALG_CRT;
  596. /************************************************************************/
  597. /* 04/02/2003 */
  598. /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */
  599. /* limitation of cswift with values not a multiple of 32 */
  600. /************************************************************************/
  601. if(!cswift_bn_32copy(&sw_param.up.crt.p, p))
  602. {
  603. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
  604. goto err;
  605. }
  606. if(!cswift_bn_32copy(&sw_param.up.crt.q, q))
  607. {
  608. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
  609. goto err;
  610. }
  611. if(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1))
  612. {
  613. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
  614. goto err;
  615. }
  616. if(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1))
  617. {
  618. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
  619. goto err;
  620. }
  621. if(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp))
  622. {
  623. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
  624. goto err;
  625. }
  626. if( !bn_wexpand(argument, a->top) ||
  627. !bn_wexpand(result, p->top + q->top))
  628. {
  629. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
  630. goto err;
  631. }
  632. /* Attach the key params */
  633. sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
  634. switch(sw_status)
  635. {
  636. case SW_OK:
  637. break;
  638. case SW_ERR_INPUT_SIZE:
  639. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
  640. goto err;
  641. default:
  642. {
  643. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  644. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
  645. sprintf(tmpbuf, "%ld", sw_status);
  646. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  647. }
  648. goto err;
  649. }
  650. /* Prepare the argument and response */
  651. arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
  652. arg.value = (unsigned char *)argument->d;
  653. res.nbytes = 2 * BN_num_bytes(p);
  654. memset(result->d, 0, res.nbytes);
  655. res.value = (unsigned char *)result->d;
  656. /* Perform the operation */
  657. if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
  658. &res, 1)) != SW_OK)
  659. {
  660. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  661. CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
  662. sprintf(tmpbuf, "%ld", sw_status);
  663. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  664. goto err;
  665. }
  666. /* Convert the response */
  667. BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
  668. to_return = 1;
  669. err:
  670. if(sw_param.up.crt.p.value)
  671. OPENSSL_free(sw_param.up.crt.p.value);
  672. if(sw_param.up.crt.q.value)
  673. OPENSSL_free(sw_param.up.crt.q.value);
  674. if(sw_param.up.crt.dmp1.value)
  675. OPENSSL_free(sw_param.up.crt.dmp1.value);
  676. if(sw_param.up.crt.dmq1.value)
  677. OPENSSL_free(sw_param.up.crt.dmq1.value);
  678. if(sw_param.up.crt.iqmp.value)
  679. OPENSSL_free(sw_param.up.crt.iqmp.value);
  680. if(result)
  681. BN_free(result);
  682. if(argument)
  683. BN_free(argument);
  684. if(acquired)
  685. release_context(hac);
  686. return to_return;
  687. }
  688. #endif
  689. #ifndef OPENSSL_NO_RSA
  690. static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  691. {
  692. int to_return = 0;
  693. const RSA_METHOD * def_rsa_method;
  694. if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
  695. {
  696. CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
  697. goto err;
  698. }
  699. /* Try the limits of RSA (2048 bits) */
  700. if(BN_num_bytes(rsa->p) > 128 ||
  701. BN_num_bytes(rsa->q) > 128 ||
  702. BN_num_bytes(rsa->dmp1) > 128 ||
  703. BN_num_bytes(rsa->dmq1) > 128 ||
  704. BN_num_bytes(rsa->iqmp) > 128)
  705. {
  706. #ifdef RSA_NULL
  707. def_rsa_method=RSA_null_method();
  708. #else
  709. #if 0
  710. def_rsa_method=RSA_PKCS1_RSAref();
  711. #else
  712. def_rsa_method=RSA_PKCS1_SSLeay();
  713. #endif
  714. #endif
  715. if(def_rsa_method)
  716. return def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx);
  717. }
  718. to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
  719. rsa->dmq1, rsa->iqmp, ctx);
  720. err:
  721. return to_return;
  722. }
  723. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  724. static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  725. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  726. {
  727. const RSA_METHOD * def_rsa_method;
  728. /* Try the limits of RSA (2048 bits) */
  729. if(BN_num_bytes(r) > 256 ||
  730. BN_num_bytes(a) > 256 ||
  731. BN_num_bytes(m) > 256)
  732. {
  733. #ifdef RSA_NULL
  734. def_rsa_method=RSA_null_method();
  735. #else
  736. #if 0
  737. def_rsa_method=RSA_PKCS1_RSAref();
  738. #else
  739. def_rsa_method=RSA_PKCS1_SSLeay();
  740. #endif
  741. #endif
  742. if(def_rsa_method)
  743. return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx);
  744. }
  745. return cswift_mod_exp(r, a, p, m, ctx);
  746. }
  747. #endif /* OPENSSL_NO_RSA */
  748. #ifndef OPENSSL_NO_DSA
  749. static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  750. {
  751. SW_CONTEXT_HANDLE hac;
  752. SW_PARAM sw_param;
  753. SW_STATUS sw_status;
  754. SW_LARGENUMBER arg, res;
  755. unsigned char *ptr;
  756. BN_CTX *ctx;
  757. BIGNUM *dsa_p = NULL;
  758. BIGNUM *dsa_q = NULL;
  759. BIGNUM *dsa_g = NULL;
  760. BIGNUM *dsa_key = NULL;
  761. BIGNUM *result = NULL;
  762. DSA_SIG *to_return = NULL;
  763. int acquired = 0;
  764. if((ctx = BN_CTX_new()) == NULL)
  765. goto err;
  766. if(!get_context(&hac))
  767. {
  768. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
  769. goto err;
  770. }
  771. acquired = 1;
  772. /* Prepare the params */
  773. BN_CTX_start(ctx);
  774. dsa_p = BN_CTX_get(ctx);
  775. dsa_q = BN_CTX_get(ctx);
  776. dsa_g = BN_CTX_get(ctx);
  777. dsa_key = BN_CTX_get(ctx);
  778. result = BN_CTX_get(ctx);
  779. if(!result)
  780. {
  781. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
  782. goto err;
  783. }
  784. if(!bn_wexpand(dsa_p, dsa->p->top) ||
  785. !bn_wexpand(dsa_q, dsa->q->top) ||
  786. !bn_wexpand(dsa_g, dsa->g->top) ||
  787. !bn_wexpand(dsa_key, dsa->priv_key->top) ||
  788. !bn_wexpand(result, dsa->p->top))
  789. {
  790. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
  791. goto err;
  792. }
  793. sw_param.type = SW_ALG_DSA;
  794. sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
  795. (unsigned char *)dsa_p->d);
  796. sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
  797. sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
  798. (unsigned char *)dsa_q->d);
  799. sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
  800. sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
  801. (unsigned char *)dsa_g->d);
  802. sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
  803. sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
  804. (unsigned char *)dsa_key->d);
  805. sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
  806. /* Attach the key params */
  807. sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
  808. switch(sw_status)
  809. {
  810. case SW_OK:
  811. break;
  812. case SW_ERR_INPUT_SIZE:
  813. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
  814. goto err;
  815. default:
  816. {
  817. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  818. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
  819. sprintf(tmpbuf, "%ld", sw_status);
  820. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  821. }
  822. goto err;
  823. }
  824. /* Prepare the argument and response */
  825. arg.nbytes = dlen;
  826. arg.value = (unsigned char *)dgst;
  827. res.nbytes = BN_num_bytes(dsa->p);
  828. memset(result->d, 0, res.nbytes);
  829. res.value = (unsigned char *)result->d;
  830. /* Perform the operation */
  831. sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
  832. &res, 1);
  833. if(sw_status != SW_OK)
  834. {
  835. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  836. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
  837. sprintf(tmpbuf, "%ld", sw_status);
  838. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  839. goto err;
  840. }
  841. /* Convert the response */
  842. ptr = (unsigned char *)result->d;
  843. if((to_return = DSA_SIG_new()) == NULL)
  844. goto err;
  845. to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
  846. to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
  847. err:
  848. if(acquired)
  849. release_context(hac);
  850. if(ctx)
  851. {
  852. BN_CTX_end(ctx);
  853. BN_CTX_free(ctx);
  854. }
  855. return to_return;
  856. }
  857. static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
  858. DSA_SIG *sig, DSA *dsa)
  859. {
  860. SW_CONTEXT_HANDLE hac;
  861. SW_PARAM sw_param;
  862. SW_STATUS sw_status;
  863. SW_LARGENUMBER arg[2], res;
  864. unsigned long sig_result;
  865. BN_CTX *ctx;
  866. BIGNUM *dsa_p = NULL;
  867. BIGNUM *dsa_q = NULL;
  868. BIGNUM *dsa_g = NULL;
  869. BIGNUM *dsa_key = NULL;
  870. BIGNUM *argument = NULL;
  871. int to_return = -1;
  872. int acquired = 0;
  873. if((ctx = BN_CTX_new()) == NULL)
  874. goto err;
  875. if(!get_context(&hac))
  876. {
  877. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
  878. goto err;
  879. }
  880. acquired = 1;
  881. /* Prepare the params */
  882. BN_CTX_start(ctx);
  883. dsa_p = BN_CTX_get(ctx);
  884. dsa_q = BN_CTX_get(ctx);
  885. dsa_g = BN_CTX_get(ctx);
  886. dsa_key = BN_CTX_get(ctx);
  887. argument = BN_CTX_get(ctx);
  888. if(!argument)
  889. {
  890. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
  891. goto err;
  892. }
  893. if(!bn_wexpand(dsa_p, dsa->p->top) ||
  894. !bn_wexpand(dsa_q, dsa->q->top) ||
  895. !bn_wexpand(dsa_g, dsa->g->top) ||
  896. !bn_wexpand(dsa_key, dsa->pub_key->top) ||
  897. !bn_wexpand(argument, 40))
  898. {
  899. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
  900. goto err;
  901. }
  902. sw_param.type = SW_ALG_DSA;
  903. sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
  904. (unsigned char *)dsa_p->d);
  905. sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
  906. sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
  907. (unsigned char *)dsa_q->d);
  908. sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
  909. sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
  910. (unsigned char *)dsa_g->d);
  911. sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
  912. sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
  913. (unsigned char *)dsa_key->d);
  914. sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
  915. /* Attach the key params */
  916. sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
  917. switch(sw_status)
  918. {
  919. case SW_OK:
  920. break;
  921. case SW_ERR_INPUT_SIZE:
  922. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
  923. goto err;
  924. default:
  925. {
  926. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  927. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
  928. sprintf(tmpbuf, "%ld", sw_status);
  929. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  930. }
  931. goto err;
  932. }
  933. /* Prepare the argument and response */
  934. arg[0].nbytes = dgst_len;
  935. arg[0].value = (unsigned char *)dgst;
  936. arg[1].nbytes = 40;
  937. arg[1].value = (unsigned char *)argument->d;
  938. memset(arg[1].value, 0, 40);
  939. BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
  940. BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
  941. res.nbytes = 4; /* unsigned long */
  942. res.value = (unsigned char *)(&sig_result);
  943. /* Perform the operation */
  944. sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
  945. &res, 1);
  946. if(sw_status != SW_OK)
  947. {
  948. char tmpbuf[DECIMAL_SIZE(sw_status)+1];
  949. CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
  950. sprintf(tmpbuf, "%ld", sw_status);
  951. ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
  952. goto err;
  953. }
  954. /* Convert the response */
  955. to_return = ((sig_result == 0) ? 0 : 1);
  956. err:
  957. if(acquired)
  958. release_context(hac);
  959. if(ctx)
  960. {
  961. BN_CTX_end(ctx);
  962. BN_CTX_free(ctx);
  963. }
  964. return to_return;
  965. }
  966. #endif
  967. #ifndef OPENSSL_NO_DH
  968. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  969. static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
  970. const BIGNUM *a, const BIGNUM *p,
  971. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  972. {
  973. return cswift_mod_exp(r, a, p, m, ctx);
  974. }
  975. #endif
  976. /* Random bytes are good */
  977. static int cswift_rand_bytes(unsigned char *buf, int num)
  978. {
  979. SW_CONTEXT_HANDLE hac;
  980. SW_STATUS swrc;
  981. SW_LARGENUMBER largenum;
  982. int acquired = 0;
  983. int to_return = 0; /* assume failure */
  984. unsigned char buf32[1024];
  985. if (!get_context(&hac))
  986. {
  987. CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE);
  988. goto err;
  989. }
  990. acquired = 1;
  991. /************************************************************************/
  992. /* 04/02/2003 */
  993. /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */
  994. /* limitation of cswift with values not a multiple of 32 */
  995. /************************************************************************/
  996. while(num >= (int)sizeof(buf32))
  997. {
  998. largenum.value = buf;
  999. largenum.nbytes = sizeof(buf32);
  1000. /* tell CryptoSwift how many bytes we want and where we want it.
  1001. * Note: - CryptoSwift cannot do more than 4096 bytes at a time.
  1002. * - CryptoSwift can only do multiple of 32-bits. */
  1003. swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
  1004. if (swrc != SW_OK)
  1005. {
  1006. char tmpbuf[20];
  1007. CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
  1008. sprintf(tmpbuf, "%ld", swrc);
  1009. ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
  1010. goto err;
  1011. }
  1012. buf += sizeof(buf32);
  1013. num -= sizeof(buf32);
  1014. }
  1015. if(num)
  1016. {
  1017. largenum.nbytes = sizeof(buf32);
  1018. largenum.value = buf32;
  1019. swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
  1020. if (swrc != SW_OK)
  1021. {
  1022. char tmpbuf[20];
  1023. CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
  1024. sprintf(tmpbuf, "%ld", swrc);
  1025. ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
  1026. goto err;
  1027. }
  1028. memcpy(buf, largenum.value, num);
  1029. }
  1030. to_return = 1; /* success */
  1031. err:
  1032. if (acquired)
  1033. release_context(hac);
  1034. return to_return;
  1035. }
  1036. static int cswift_rand_status(void)
  1037. {
  1038. return 1;
  1039. }
  1040. /* This stuff is needed if this ENGINE is being compiled into a self-contained
  1041. * shared-library. */
  1042. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  1043. static int bind_fn(ENGINE *e, const char *id)
  1044. {
  1045. if(id && (strcmp(id, engine_cswift_id) != 0))
  1046. return 0;
  1047. if(!bind_helper(e))
  1048. return 0;
  1049. return 1;
  1050. }
  1051. IMPLEMENT_DYNAMIC_CHECK_FN()
  1052. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  1053. #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  1054. #endif /* !OPENSSL_NO_HW_CSWIFT */
  1055. #endif /* !OPENSSL_NO_HW */