hw_ibmca.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* crypto/engine/hw_ibmca.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * 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. /* (C) COPYRIGHT International Business Machines Corp. 2001 */
  60. #include <stdio.h>
  61. #include <openssl/crypto.h>
  62. #include <openssl/dso.h>
  63. #include <openssl/engine.h>
  64. #ifndef OPENSSL_NO_HW
  65. # ifndef OPENSSL_NO_HW_IBMCA
  66. # ifdef FLAT_INC
  67. # include "ica_openssl_api.h"
  68. # else
  69. # include "vendor_defns/ica_openssl_api.h"
  70. # endif
  71. # define IBMCA_LIB_NAME "ibmca engine"
  72. # include "hw_ibmca_err.c"
  73. static int ibmca_destroy(ENGINE *e);
  74. static int ibmca_init(ENGINE *e);
  75. static int ibmca_finish(ENGINE *e);
  76. static int ibmca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
  77. static const char *IBMCA_F1 = "icaOpenAdapter";
  78. static const char *IBMCA_F2 = "icaCloseAdapter";
  79. static const char *IBMCA_F3 = "icaRsaModExpo";
  80. static const char *IBMCA_F4 = "icaRandomNumberGenerate";
  81. static const char *IBMCA_F5 = "icaRsaCrt";
  82. ICA_ADAPTER_HANDLE handle = 0;
  83. /* BIGNUM stuff */
  84. static int ibmca_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  85. const BIGNUM *m, BN_CTX *ctx);
  86. static int ibmca_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  87. const BIGNUM *q, const BIGNUM *dmp1,
  88. const BIGNUM *dmq1, const BIGNUM *iqmp,
  89. BN_CTX *ctx);
  90. # ifndef OPENSSL_NO_RSA
  91. /* RSA stuff */
  92. static int ibmca_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
  93. # endif
  94. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  95. static int ibmca_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  96. const BIGNUM *m, BN_CTX *ctx,
  97. BN_MONT_CTX *m_ctx);
  98. # ifndef OPENSSL_NO_DSA
  99. /* DSA stuff */
  100. static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  101. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  102. BN_CTX *ctx, BN_MONT_CTX *in_mont);
  103. static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  104. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  105. BN_MONT_CTX *m_ctx);
  106. # endif
  107. # ifndef OPENSSL_NO_DH
  108. /* DH stuff */
  109. /* This function is alised to mod_exp (with the DH and mont dropped). */
  110. static int ibmca_mod_exp_dh(const DH *dh, BIGNUM *r,
  111. const BIGNUM *a, const BIGNUM *p,
  112. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  113. # endif
  114. /* RAND stuff */
  115. static int ibmca_rand_bytes(unsigned char *buf, int num);
  116. static int ibmca_rand_status(void);
  117. /* WJH - check for more commands, like in nuron */
  118. /* The definitions for control commands specific to this engine */
  119. # define IBMCA_CMD_SO_PATH ENGINE_CMD_BASE
  120. static const ENGINE_CMD_DEFN ibmca_cmd_defns[] = {
  121. {IBMCA_CMD_SO_PATH,
  122. "SO_PATH",
  123. "Specifies the path to the 'atasi' shared library",
  124. ENGINE_CMD_FLAG_STRING},
  125. {0, NULL, NULL, 0}
  126. };
  127. # ifndef OPENSSL_NO_RSA
  128. /* Our internal RSA_METHOD that we provide pointers to */
  129. static RSA_METHOD ibmca_rsa = {
  130. "Ibmca RSA method",
  131. NULL,
  132. NULL,
  133. NULL,
  134. NULL,
  135. ibmca_rsa_mod_exp,
  136. ibmca_mod_exp_mont,
  137. NULL,
  138. NULL,
  139. 0,
  140. NULL,
  141. NULL,
  142. NULL
  143. };
  144. # endif
  145. # ifndef OPENSSL_NO_DSA
  146. /* Our internal DSA_METHOD that we provide pointers to */
  147. static DSA_METHOD ibmca_dsa = {
  148. "Ibmca DSA method",
  149. NULL, /* dsa_do_sign */
  150. NULL, /* dsa_sign_setup */
  151. NULL, /* dsa_do_verify */
  152. ibmca_dsa_mod_exp, /* dsa_mod_exp */
  153. ibmca_mod_exp_dsa, /* bn_mod_exp */
  154. NULL, /* init */
  155. NULL, /* finish */
  156. 0, /* flags */
  157. NULL /* app_data */
  158. };
  159. # endif
  160. # ifndef OPENSSL_NO_DH
  161. /* Our internal DH_METHOD that we provide pointers to */
  162. static DH_METHOD ibmca_dh = {
  163. "Ibmca DH method",
  164. NULL,
  165. NULL,
  166. ibmca_mod_exp_dh,
  167. NULL,
  168. NULL,
  169. 0,
  170. NULL
  171. };
  172. # endif
  173. static RAND_METHOD ibmca_rand = {
  174. /* "IBMCA RAND method", */
  175. NULL,
  176. ibmca_rand_bytes,
  177. NULL,
  178. NULL,
  179. ibmca_rand_bytes,
  180. ibmca_rand_status,
  181. };
  182. /* Constants used when creating the ENGINE */
  183. static const char *engine_ibmca_id = "ibmca";
  184. static const char *engine_ibmca_name = "Ibmca hardware engine support";
  185. /*
  186. * This internal function is used by ENGINE_ibmca() and possibly by the
  187. * "dynamic" ENGINE support too
  188. */
  189. static int bind_helper(ENGINE *e)
  190. {
  191. # ifndef OPENSSL_NO_RSA
  192. const RSA_METHOD *meth1;
  193. # endif
  194. # ifndef OPENSSL_NO_DSA
  195. const DSA_METHOD *meth2;
  196. # endif
  197. # ifndef OPENSSL_NO_DH
  198. const DH_METHOD *meth3;
  199. # endif
  200. if (!ENGINE_set_id(e, engine_ibmca_id) ||
  201. !ENGINE_set_name(e, engine_ibmca_name) ||
  202. # ifndef OPENSSL_NO_RSA
  203. !ENGINE_set_RSA(e, &ibmca_rsa) ||
  204. # endif
  205. # ifndef OPENSSL_NO_DSA
  206. !ENGINE_set_DSA(e, &ibmca_dsa) ||
  207. # endif
  208. # ifndef OPENSSL_NO_DH
  209. !ENGINE_set_DH(e, &ibmca_dh) ||
  210. # endif
  211. !ENGINE_set_RAND(e, &ibmca_rand) ||
  212. !ENGINE_set_destroy_function(e, ibmca_destroy) ||
  213. !ENGINE_set_init_function(e, ibmca_init) ||
  214. !ENGINE_set_finish_function(e, ibmca_finish) ||
  215. !ENGINE_set_ctrl_function(e, ibmca_ctrl) ||
  216. !ENGINE_set_cmd_defns(e, ibmca_cmd_defns))
  217. return 0;
  218. # ifndef OPENSSL_NO_RSA
  219. /*
  220. * We know that the "PKCS1_OpenSSL()" functions hook properly to the
  221. * ibmca-specific mod_exp and mod_exp_crt so we use those functions. NB:
  222. * We don't use ENGINE_openssl() or anything "more generic" because
  223. * something like the RSAref code may not hook properly, and if you own
  224. * one of these cards then you have the right to do RSA operations on it
  225. * anyway!
  226. */
  227. meth1 = RSA_PKCS1_OpenSSL();
  228. ibmca_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  229. ibmca_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  230. ibmca_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  231. ibmca_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  232. # endif
  233. # ifndef OPENSSL_NO_DSA
  234. /*
  235. * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
  236. */
  237. meth2 = DSA_OpenSSL();
  238. ibmca_dsa.dsa_do_sign = meth2->dsa_do_sign;
  239. ibmca_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
  240. ibmca_dsa.dsa_do_verify = meth2->dsa_do_verify;
  241. # endif
  242. # ifndef OPENSSL_NO_DH
  243. /* Much the same for Diffie-Hellman */
  244. meth3 = DH_OpenSSL();
  245. ibmca_dh.generate_key = meth3->generate_key;
  246. ibmca_dh.compute_key = meth3->compute_key;
  247. # endif
  248. /* Ensure the ibmca error handling is set up */
  249. ERR_load_IBMCA_strings();
  250. return 1;
  251. }
  252. static ENGINE *engine_ibmca(void)
  253. {
  254. ENGINE *ret = ENGINE_new();
  255. if (!ret)
  256. return NULL;
  257. if (!bind_helper(ret)) {
  258. ENGINE_free(ret);
  259. return NULL;
  260. }
  261. return ret;
  262. }
  263. # ifdef ENGINE_DYNAMIC_SUPPORT
  264. static
  265. # endif
  266. void ENGINE_load_ibmca(void)
  267. {
  268. /* Copied from eng_[openssl|dyn].c */
  269. ENGINE *toadd = engine_ibmca();
  270. if (!toadd)
  271. return;
  272. ENGINE_add(toadd);
  273. ENGINE_free(toadd);
  274. ERR_clear_error();
  275. }
  276. /* Destructor (complements the "ENGINE_ibmca()" constructor) */
  277. static int ibmca_destroy(ENGINE *e)
  278. {
  279. /*
  280. * Unload the ibmca error strings so any error state including our functs
  281. * or reasons won't lead to a segfault (they simply get displayed without
  282. * corresponding string data because none will be found).
  283. */
  284. ERR_unload_IBMCA_strings();
  285. return 1;
  286. }
  287. /*
  288. * This is a process-global DSO handle used for loading and unloading the
  289. * Ibmca library. NB: This is only set (or unset) during an init() or
  290. * finish() call (reference counts permitting) and they're operating with
  291. * global locks, so this should be thread-safe implicitly.
  292. */
  293. static DSO *ibmca_dso = NULL;
  294. /*
  295. * These are the function pointers that are (un)set when the library has
  296. * successfully (un)loaded.
  297. */
  298. static unsigned int (ICA_CALL * p_icaOpenAdapter) ();
  299. static unsigned int (ICA_CALL * p_icaCloseAdapter) ();
  300. static unsigned int (ICA_CALL * p_icaRsaModExpo) ();
  301. static unsigned int (ICA_CALL * p_icaRandomNumberGenerate) ();
  302. static unsigned int (ICA_CALL * p_icaRsaCrt) ();
  303. /* utility function to obtain a context */
  304. static int get_context(ICA_ADAPTER_HANDLE * p_handle)
  305. {
  306. unsigned int status = 0;
  307. status = p_icaOpenAdapter(0, p_handle);
  308. if (status != 0)
  309. return 0;
  310. return 1;
  311. }
  312. /* similarly to release one. */
  313. static void release_context(ICA_ADAPTER_HANDLE handle)
  314. {
  315. p_icaCloseAdapter(handle);
  316. }
  317. /* (de)initialisation functions. */
  318. static int ibmca_init(ENGINE *e)
  319. {
  320. void (*p1) ();
  321. void (*p2) ();
  322. void (*p3) ();
  323. void (*p4) ();
  324. void (*p5) ();
  325. if (ibmca_dso != NULL) {
  326. IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_ALREADY_LOADED);
  327. goto err;
  328. }
  329. /*
  330. * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed
  331. * unfortunately because the Ibmca drivers don't have standard library
  332. * names that can be platform-translated well.
  333. */
  334. /*
  335. * TODO: Work out how to actually map to the names the Ibmca drivers
  336. * really use - for now a symbollic link needs to be created on the host
  337. * system from libatasi.so to atasi.so on unix variants.
  338. */
  339. /* WJH XXX check name translation */
  340. ibmca_dso = DSO_load(NULL, IBMCA_LIBNAME, NULL,
  341. /*
  342. * DSO_FLAG_NAME_TRANSLATION
  343. */ 0);
  344. if (ibmca_dso == NULL) {
  345. IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_DSO_FAILURE);
  346. goto err;
  347. }
  348. if ((p1 = DSO_bind_func(ibmca_dso, IBMCA_F1)) == NULL
  349. || (p2 = DSO_bind_func(ibmca_dso, IBMCA_F2)) == NULL
  350. || (p3 = DSO_bind_func(ibmca_dso, IBMCA_F3)) == NULL
  351. || (p4 = DSO_bind_func(ibmca_dso, IBMCA_F4)) == NULL
  352. || (p5 = DSO_bind_func(ibmca_dso, IBMCA_F5)) == NULL) {
  353. IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_DSO_FAILURE);
  354. goto err;
  355. }
  356. /* Copy the pointers */
  357. p_icaOpenAdapter = (unsigned int (ICA_CALL *) ())p1;
  358. p_icaCloseAdapter = (unsigned int (ICA_CALL *) ())p2;
  359. p_icaRsaModExpo = (unsigned int (ICA_CALL *) ())p3;
  360. p_icaRandomNumberGenerate = (unsigned int (ICA_CALL *) ())p4;
  361. p_icaRsaCrt = (unsigned int (ICA_CALL *) ())p5;
  362. if (!get_context(&handle)) {
  363. IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_UNIT_FAILURE);
  364. goto err;
  365. }
  366. return 1;
  367. err:
  368. DSO_free(ibmca_dso);
  369. p_icaOpenAdapter = NULL;
  370. p_icaCloseAdapter = NULL;
  371. p_icaRsaModExpo = NULL;
  372. p_icaRandomNumberGenerate = NULL;
  373. return 0;
  374. }
  375. static int ibmca_finish(ENGINE *e)
  376. {
  377. if (ibmca_dso == NULL) {
  378. IBMCAerr(IBMCA_F_IBMCA_FINISH, IBMCA_R_NOT_LOADED);
  379. return 0;
  380. }
  381. release_context(handle);
  382. if (!DSO_free(ibmca_dso)) {
  383. IBMCAerr(IBMCA_F_IBMCA_FINISH, IBMCA_R_DSO_FAILURE);
  384. return 0;
  385. }
  386. ibmca_dso = NULL;
  387. return 1;
  388. }
  389. static int ibmca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
  390. {
  391. int initialised = ((ibmca_dso == NULL) ? 0 : 1);
  392. switch (cmd) {
  393. case IBMCA_CMD_SO_PATH:
  394. if (p == NULL) {
  395. IBMCAerr(IBMCA_F_IBMCA_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  396. return 0;
  397. }
  398. if (initialised) {
  399. IBMCAerr(IBMCA_F_IBMCA_CTRL, IBMCA_R_ALREADY_LOADED);
  400. return 0;
  401. }
  402. IBMCA_LIBNAME = (const char *)p;
  403. return 1;
  404. default:
  405. break;
  406. }
  407. IBMCAerr(IBMCA_F_IBMCA_CTRL, IBMCA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  408. return 0;
  409. }
  410. static int ibmca_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  411. const BIGNUM *m, BN_CTX *ctx)
  412. {
  413. /*
  414. * I need somewhere to store temporary serialised values for use with the
  415. * Ibmca API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
  416. * access their arrays directly as byte arrays <grin>. This way I don't
  417. * have to clean anything up.
  418. */
  419. BIGNUM *argument = NULL;
  420. BIGNUM *result = NULL;
  421. BIGNUM *key = NULL;
  422. int to_return;
  423. int inLen, outLen, tmpLen;
  424. ICA_KEY_RSA_MODEXPO *publKey = NULL;
  425. unsigned int rc;
  426. to_return = 0; /* expect failure */
  427. if (!ibmca_dso) {
  428. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_NOT_LOADED);
  429. goto err;
  430. }
  431. /* Prepare the params */
  432. BN_CTX_start(ctx);
  433. argument = BN_CTX_get(ctx);
  434. result = BN_CTX_get(ctx);
  435. key = BN_CTX_get(ctx);
  436. if (!argument || !result || !key) {
  437. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_BN_CTX_FULL);
  438. goto err;
  439. }
  440. if (!bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top) ||
  441. !bn_wexpand(key, sizeof(*publKey) / BN_BYTES)) {
  442. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_BN_EXPAND_FAIL);
  443. goto err;
  444. }
  445. publKey = (ICA_KEY_RSA_MODEXPO *)key->d;
  446. if (publKey == NULL) {
  447. goto err;
  448. }
  449. memset(publKey, 0, sizeof(*publKey));
  450. publKey->keyType = CORRECT_ENDIANNESS(ME_KEY_TYPE);
  451. publKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_MODEXPO));
  452. publKey->expOffset = (char *)publKey->keyRecord - (char *)publKey;
  453. /*
  454. * A quirk of the card: the exponent length has to be the same as the
  455. * modulus (key) length
  456. */
  457. outLen = BN_num_bytes(m);
  458. /* check for modulus length SAB*/
  459. if (outLen > 256) {
  460. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_MEXP_LENGTH_TO_LARGE);
  461. goto err;
  462. }
  463. /* check for modulus length SAB*/
  464. publKey->expLength = publKey->nLength = outLen;
  465. /*
  466. * SAB Check for underflow condition the size of the exponent is less
  467. * than the size of the parameter then we have a big problem and will
  468. * underflow the keyRecord buffer. Bad stuff could happen then
  469. */
  470. if (outLen < BN_num_bytes(p)) {
  471. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_UNDERFLOW_KEYRECORD);
  472. goto err;
  473. }
  474. /* SAB End check for underflow */
  475. BN_bn2bin(p, &publKey->keyRecord[publKey->expLength - BN_num_bytes(p)]);
  476. BN_bn2bin(m, &publKey->keyRecord[publKey->expLength]);
  477. publKey->modulusBitLength = CORRECT_ENDIANNESS(publKey->nLength * 8);
  478. publKey->nOffset = CORRECT_ENDIANNESS(publKey->expOffset +
  479. publKey->expLength);
  480. publKey->expOffset = CORRECT_ENDIANNESS((char *)publKey->keyRecord -
  481. (char *)publKey);
  482. tmpLen = outLen;
  483. publKey->expLength = publKey->nLength = CORRECT_ENDIANNESS(tmpLen);
  484. /* Prepare the argument */
  485. memset(argument->d, 0, outLen);
  486. BN_bn2bin(a, (unsigned char *)argument->d + outLen - BN_num_bytes(a));
  487. inLen = outLen;
  488. /* Perform the operation */
  489. if ((rc = p_icaRsaModExpo(handle, inLen, (unsigned char *)argument->d,
  490. publKey, &outLen, (unsigned char *)result->d))
  491. != 0) {
  492. printf("rc = %d\n", rc);
  493. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_REQUEST_FAILED);
  494. goto err;
  495. }
  496. /* Convert the response */
  497. BN_bin2bn((unsigned char *)result->d, outLen, r);
  498. to_return = 1;
  499. err:
  500. BN_CTX_end(ctx);
  501. return to_return;
  502. }
  503. # ifndef OPENSSL_NO_RSA
  504. static int ibmca_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
  505. {
  506. BN_CTX *ctx;
  507. int to_return = 0;
  508. if ((ctx = BN_CTX_new()) == NULL)
  509. goto err;
  510. if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  511. if (!rsa->d || !rsa->n) {
  512. IBMCAerr(IBMCA_F_IBMCA_RSA_MOD_EXP,
  513. IBMCA_R_MISSING_KEY_COMPONENTS);
  514. goto err;
  515. }
  516. to_return = ibmca_mod_exp(r0, I, rsa->d, rsa->n, ctx);
  517. } else {
  518. to_return = ibmca_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
  519. rsa->dmq1, rsa->iqmp, ctx);
  520. }
  521. err:
  522. BN_CTX_free(ctx);
  523. return to_return;
  524. }
  525. # endif
  526. /* Ein kleines chinesisches "Restessen" */
  527. static int ibmca_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  528. const BIGNUM *q, const BIGNUM *dmp1,
  529. const BIGNUM *dmq1, const BIGNUM *iqmp,
  530. BN_CTX *ctx)
  531. {
  532. BIGNUM *argument = NULL;
  533. BIGNUM *result = NULL;
  534. BIGNUM *key = NULL;
  535. int to_return = 0; /* expect failure */
  536. char *pkey = NULL;
  537. ICA_KEY_RSA_CRT *privKey = NULL;
  538. int inLen, outLen;
  539. int rc;
  540. unsigned int offset, pSize, qSize;
  541. /* SAB New variables */
  542. unsigned int keyRecordSize;
  543. unsigned int pbytes = BN_num_bytes(p);
  544. unsigned int qbytes = BN_num_bytes(q);
  545. unsigned int dmp1bytes = BN_num_bytes(dmp1);
  546. unsigned int dmq1bytes = BN_num_bytes(dmq1);
  547. unsigned int iqmpbytes = BN_num_bytes(iqmp);
  548. /* Prepare the params */
  549. BN_CTX_start(ctx);
  550. argument = BN_CTX_get(ctx);
  551. result = BN_CTX_get(ctx);
  552. key = BN_CTX_get(ctx);
  553. if (!argument || !result || !key) {
  554. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_BN_CTX_FULL);
  555. goto err;
  556. }
  557. if (!bn_wexpand(argument, p->top + q->top) ||
  558. !bn_wexpand(result, p->top + q->top) ||
  559. !bn_wexpand(key, sizeof(*privKey) / BN_BYTES)) {
  560. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_BN_EXPAND_FAIL);
  561. goto err;
  562. }
  563. privKey = (ICA_KEY_RSA_CRT *)key->d;
  564. /*
  565. * SAB Add check for total size in bytes of the parms does not exceed the
  566. * buffer space we have do this first
  567. */
  568. keyRecordSize = pbytes + qbytes + dmp1bytes + dmq1bytes + iqmpbytes;
  569. if (keyRecordSize > sizeof(privKey->keyRecord)) {
  570. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  571. goto err;
  572. }
  573. if ((qbytes + dmq1bytes) > 256) {
  574. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  575. goto err;
  576. }
  577. if (pbytes + dmp1bytes > 256) {
  578. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  579. goto err;
  580. }
  581. /* end SAB additions */
  582. memset(privKey, 0, sizeof(*privKey));
  583. privKey->keyType = CORRECT_ENDIANNESS(CRT_KEY_TYPE);
  584. privKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_CRT));
  585. privKey->modulusBitLength = CORRECT_ENDIANNESS(BN_num_bytes(q) * 2 * 8);
  586. /*
  587. * p,dp & qInv are 1 QWORD Larger
  588. */
  589. privKey->pLength = CORRECT_ENDIANNESS(BN_num_bytes(p) + 8);
  590. privKey->qLength = CORRECT_ENDIANNESS(BN_num_bytes(q));
  591. privKey->dpLength = CORRECT_ENDIANNESS(BN_num_bytes(dmp1) + 8);
  592. privKey->dqLength = CORRECT_ENDIANNESS(BN_num_bytes(dmq1));
  593. privKey->qInvLength = CORRECT_ENDIANNESS(BN_num_bytes(iqmp) + 8);
  594. offset = (char *)privKey->keyRecord - (char *)privKey;
  595. qSize = BN_num_bytes(q);
  596. pSize = qSize + 8; /* 1 QWORD larger */
  597. /*
  598. * SAB probably aittle redundant, but we'll verify that each of the
  599. * components which make up a key record sent ot the card does not exceed
  600. * the space that is allocated for it. this handles the case where even
  601. * if the total length does not exceed keyrecord zied, if the operands are
  602. * funny sized they could cause potential side affects on either the card
  603. * or the result
  604. */
  605. if ((pbytes > pSize) || (dmp1bytes > pSize) ||
  606. (iqmpbytes > pSize) || (qbytes > qSize) || (dmq1bytes > qSize)) {
  607. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  608. goto err;
  609. }
  610. privKey->dpOffset = CORRECT_ENDIANNESS(offset);
  611. offset += pSize;
  612. privKey->dqOffset = CORRECT_ENDIANNESS(offset);
  613. offset += qSize;
  614. privKey->pOffset = CORRECT_ENDIANNESS(offset);
  615. offset += pSize;
  616. privKey->qOffset = CORRECT_ENDIANNESS(offset);
  617. offset += qSize;
  618. privKey->qInvOffset = CORRECT_ENDIANNESS(offset);
  619. pkey = (char *)privKey->keyRecord;
  620. /* SAB first check that we don;t under flow the buffer */
  621. if (pSize < pbytes) {
  622. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_UNDERFLOW_CONDITION);
  623. goto err;
  624. }
  625. /* pkey += pSize - BN_num_bytes(p); WROING this should be dmp1) */
  626. pkey += pSize - BN_num_bytes(dmp1);
  627. BN_bn2bin(dmp1, pkey);
  628. pkey += BN_num_bytes(dmp1); /* move the pointer */
  629. BN_bn2bin(dmq1, pkey); /* Copy over dmq1 */
  630. pkey += qSize; /* move pointer */
  631. /* set up for zero padding of next field */
  632. pkey += pSize - BN_num_bytes(p);
  633. BN_bn2bin(p, pkey);
  634. /* increment pointer by number of bytes moved */
  635. pkey += BN_num_bytes(p);
  636. BN_bn2bin(q, pkey);
  637. pkey += qSize; /* move the pointer */
  638. pkey += pSize - BN_num_bytes(iqmp); /* Adjust for padding */
  639. BN_bn2bin(iqmp, pkey);
  640. /* Prepare the argument and response */
  641. /*
  642. * Correct endianess is used because the fields were converted above
  643. */
  644. outLen = CORRECT_ENDIANNESS(privKey->qLength) * 2;
  645. if (outLen > 256) {
  646. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OUTLEN_TO_LARGE);
  647. goto err;
  648. }
  649. /* SAB check for underflow here on the argeument */
  650. if (outLen < BN_num_bytes(a)) {
  651. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_UNDERFLOW_CONDITION);
  652. goto err;
  653. }
  654. BN_bn2bin(a, (unsigned char *)argument->d + outLen - BN_num_bytes(a));
  655. inLen = outLen;
  656. memset(result->d, 0, outLen);
  657. /* Perform the operation */
  658. if ((rc = p_icaRsaCrt(handle, inLen, (unsigned char *)argument->d,
  659. privKey, &outLen, (unsigned char *)result->d)) != 0)
  660. {
  661. printf("rc = %d\n", rc);
  662. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_REQUEST_FAILED);
  663. goto err;
  664. }
  665. /* Convert the response */
  666. BN_bin2bn((unsigned char *)result->d, outLen, r);
  667. to_return = 1;
  668. err:
  669. BN_CTX_end(ctx);
  670. return to_return;
  671. }
  672. # ifndef OPENSSL_NO_DSA
  673. /*
  674. * This code was liberated and adapted from the commented-out code in
  675. * dsa_ossl.c. Because of the unoptimised form of the Ibmca acceleration (it
  676. * doesn't have a CRT form for RSA), this function means that an Ibmca system
  677. * running with a DSA server certificate can handshake around 5 or 6 times
  678. * faster/more than an equivalent system running with RSA. Just check out the
  679. * "signs" statistics from the RSA and DSA parts of "openssl speed -engine
  680. * ibmca dsa1024 rsa1024".
  681. */
  682. static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  683. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  684. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  685. {
  686. BIGNUM t;
  687. int to_return = 0;
  688. BN_init(&t);
  689. /* let rr = a1 ^ p1 mod m */
  690. if (!ibmca_mod_exp(rr, a1, p1, m, ctx))
  691. goto end;
  692. /* let t = a2 ^ p2 mod m */
  693. if (!ibmca_mod_exp(&t, a2, p2, m, ctx))
  694. goto end;
  695. /* let rr = rr * t mod m */
  696. if (!BN_mod_mul(rr, rr, &t, m, ctx))
  697. goto end;
  698. to_return = 1;
  699. end:
  700. BN_free(&t);
  701. return to_return;
  702. }
  703. static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  704. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  705. BN_MONT_CTX *m_ctx)
  706. {
  707. return ibmca_mod_exp(r, a, p, m, ctx);
  708. }
  709. # endif
  710. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  711. static int ibmca_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  712. const BIGNUM *m, BN_CTX *ctx,
  713. BN_MONT_CTX *m_ctx)
  714. {
  715. return ibmca_mod_exp(r, a, p, m, ctx);
  716. }
  717. # ifndef OPENSSL_NO_DH
  718. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  719. static int ibmca_mod_exp_dh(DH const *dh, BIGNUM *r,
  720. const BIGNUM *a, const BIGNUM *p,
  721. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  722. {
  723. return ibmca_mod_exp(r, a, p, m, ctx);
  724. }
  725. # endif
  726. /* Random bytes are good */
  727. static int ibmca_rand_bytes(unsigned char *buf, int num)
  728. {
  729. int to_return = 0; /* assume failure */
  730. unsigned int ret;
  731. if (handle == 0) {
  732. IBMCAerr(IBMCA_F_IBMCA_RAND_BYTES, IBMCA_R_NOT_INITIALISED);
  733. goto err;
  734. }
  735. ret = p_icaRandomNumberGenerate(handle, num, buf);
  736. if (ret < 0) {
  737. IBMCAerr(IBMCA_F_IBMCA_RAND_BYTES, IBMCA_R_REQUEST_FAILED);
  738. goto err;
  739. }
  740. to_return = 1;
  741. err:
  742. return to_return;
  743. }
  744. static int ibmca_rand_status(void)
  745. {
  746. return 1;
  747. }
  748. /*
  749. * This stuff is needed if this ENGINE is being compiled into a
  750. * self-contained shared-library.
  751. */
  752. # ifdef ENGINE_DYNAMIC_SUPPORT
  753. static int bind_fn(ENGINE *e, const char *id)
  754. {
  755. if (id && (strcmp(id, engine_ibmca_id) != 0)) /* WJH XXX */
  756. return 0;
  757. if (!bind_helper(e))
  758. return 0;
  759. return 1;
  760. }
  761. IMPLEMENT_DYNAMIC_CHECK_FN()
  762. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  763. # endif /* ENGINE_DYNAMIC_SUPPORT */
  764. # endif /* !OPENSSL_NO_HW_IBMCA */
  765. #endif /* !OPENSSL_NO_HW */