e_cswift.c 35 KB

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