hw_ibmca.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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_SSLeay()" 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_SSLeay();
  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)) ||
  349. !(p2 = DSO_bind_func(ibmca_dso, IBMCA_F2)) ||
  350. !(p3 = DSO_bind_func(ibmca_dso, IBMCA_F3)) ||
  351. !(p4 = DSO_bind_func(ibmca_dso, IBMCA_F4)) ||
  352. !(p5 = DSO_bind_func(ibmca_dso, IBMCA_F5))) {
  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. if (ibmca_dso)
  369. DSO_free(ibmca_dso);
  370. p_icaOpenAdapter = NULL;
  371. p_icaCloseAdapter = NULL;
  372. p_icaRsaModExpo = NULL;
  373. p_icaRandomNumberGenerate = NULL;
  374. return 0;
  375. }
  376. static int ibmca_finish(ENGINE *e)
  377. {
  378. if (ibmca_dso == NULL) {
  379. IBMCAerr(IBMCA_F_IBMCA_FINISH, IBMCA_R_NOT_LOADED);
  380. return 0;
  381. }
  382. release_context(handle);
  383. if (!DSO_free(ibmca_dso)) {
  384. IBMCAerr(IBMCA_F_IBMCA_FINISH, IBMCA_R_DSO_FAILURE);
  385. return 0;
  386. }
  387. ibmca_dso = NULL;
  388. return 1;
  389. }
  390. static int ibmca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
  391. {
  392. int initialised = ((ibmca_dso == NULL) ? 0 : 1);
  393. switch (cmd) {
  394. case IBMCA_CMD_SO_PATH:
  395. if (p == NULL) {
  396. IBMCAerr(IBMCA_F_IBMCA_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  397. return 0;
  398. }
  399. if (initialised) {
  400. IBMCAerr(IBMCA_F_IBMCA_CTRL, IBMCA_R_ALREADY_LOADED);
  401. return 0;
  402. }
  403. IBMCA_LIBNAME = (const char *)p;
  404. return 1;
  405. default:
  406. break;
  407. }
  408. IBMCAerr(IBMCA_F_IBMCA_CTRL, IBMCA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  409. return 0;
  410. }
  411. static int ibmca_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  412. const BIGNUM *m, BN_CTX *ctx)
  413. {
  414. /*
  415. * I need somewhere to store temporary serialised values for use with the
  416. * Ibmca API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
  417. * access their arrays directly as byte arrays <grin>. This way I don't
  418. * have to clean anything up.
  419. */
  420. BIGNUM *argument = NULL;
  421. BIGNUM *result = NULL;
  422. BIGNUM *key = NULL;
  423. int to_return;
  424. int inLen, outLen, tmpLen;
  425. ICA_KEY_RSA_MODEXPO *publKey = NULL;
  426. unsigned int rc;
  427. to_return = 0; /* expect failure */
  428. if (!ibmca_dso) {
  429. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_NOT_LOADED);
  430. goto err;
  431. }
  432. /* Prepare the params */
  433. BN_CTX_start(ctx);
  434. argument = BN_CTX_get(ctx);
  435. result = BN_CTX_get(ctx);
  436. key = BN_CTX_get(ctx);
  437. if (!argument || !result || !key) {
  438. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_BN_CTX_FULL);
  439. goto err;
  440. }
  441. if (!bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top) ||
  442. !bn_wexpand(key, sizeof(*publKey) / BN_BYTES)) {
  443. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_BN_EXPAND_FAIL);
  444. goto err;
  445. }
  446. publKey = (ICA_KEY_RSA_MODEXPO *)key->d;
  447. if (publKey == NULL) {
  448. goto err;
  449. }
  450. memset(publKey, 0, sizeof(ICA_KEY_RSA_MODEXPO));
  451. publKey->keyType = CORRECT_ENDIANNESS(ME_KEY_TYPE);
  452. publKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_MODEXPO));
  453. publKey->expOffset = (char *)publKey->keyRecord - (char *)publKey;
  454. /*
  455. * A quirk of the card: the exponent length has to be the same as the
  456. * modulus (key) length
  457. */
  458. outLen = BN_num_bytes(m);
  459. /* check for modulus length SAB*/
  460. if (outLen > 256) {
  461. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_MEXP_LENGTH_TO_LARGE);
  462. goto err;
  463. }
  464. /* check for modulus length SAB*/
  465. publKey->expLength = publKey->nLength = outLen;
  466. /*
  467. * SAB Check for underflow condition the size of the exponent is less
  468. * than the size of the parameter then we have a big problem and will
  469. * underflow the keyRecord buffer. Bad stuff could happen then
  470. */
  471. if (outLen < BN_num_bytes(p)) {
  472. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_UNDERFLOW_KEYRECORD);
  473. goto err;
  474. }
  475. /* SAB End check for underflow */
  476. BN_bn2bin(p, &publKey->keyRecord[publKey->expLength - BN_num_bytes(p)]);
  477. BN_bn2bin(m, &publKey->keyRecord[publKey->expLength]);
  478. publKey->modulusBitLength = CORRECT_ENDIANNESS(publKey->nLength * 8);
  479. publKey->nOffset = CORRECT_ENDIANNESS(publKey->expOffset +
  480. publKey->expLength);
  481. publKey->expOffset = CORRECT_ENDIANNESS((char *)publKey->keyRecord -
  482. (char *)publKey);
  483. tmpLen = outLen;
  484. publKey->expLength = publKey->nLength = CORRECT_ENDIANNESS(tmpLen);
  485. /* Prepare the argument */
  486. memset(argument->d, 0, outLen);
  487. BN_bn2bin(a, (unsigned char *)argument->d + outLen - BN_num_bytes(a));
  488. inLen = outLen;
  489. /* Perform the operation */
  490. if ((rc = p_icaRsaModExpo(handle, inLen, (unsigned char *)argument->d,
  491. publKey, &outLen, (unsigned char *)result->d))
  492. != 0) {
  493. printf("rc = %d\n", rc);
  494. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_REQUEST_FAILED);
  495. goto err;
  496. }
  497. /* Convert the response */
  498. BN_bin2bn((unsigned char *)result->d, outLen, r);
  499. to_return = 1;
  500. err:
  501. BN_CTX_end(ctx);
  502. return to_return;
  503. }
  504. # ifndef OPENSSL_NO_RSA
  505. static int ibmca_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
  506. {
  507. BN_CTX *ctx;
  508. int to_return = 0;
  509. if ((ctx = BN_CTX_new()) == NULL)
  510. goto err;
  511. if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  512. if (!rsa->d || !rsa->n) {
  513. IBMCAerr(IBMCA_F_IBMCA_RSA_MOD_EXP,
  514. IBMCA_R_MISSING_KEY_COMPONENTS);
  515. goto err;
  516. }
  517. to_return = ibmca_mod_exp(r0, I, rsa->d, rsa->n, ctx);
  518. } else {
  519. to_return = ibmca_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
  520. rsa->dmq1, rsa->iqmp, ctx);
  521. }
  522. err:
  523. if (ctx)
  524. BN_CTX_free(ctx);
  525. return to_return;
  526. }
  527. # endif
  528. /* Ein kleines chinesisches "Restessen" */
  529. static int ibmca_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  530. const BIGNUM *q, const BIGNUM *dmp1,
  531. const BIGNUM *dmq1, const BIGNUM *iqmp,
  532. BN_CTX *ctx)
  533. {
  534. BIGNUM *argument = NULL;
  535. BIGNUM *result = NULL;
  536. BIGNUM *key = NULL;
  537. int to_return = 0; /* expect failure */
  538. char *pkey = NULL;
  539. ICA_KEY_RSA_CRT *privKey = NULL;
  540. int inLen, outLen;
  541. int rc;
  542. unsigned int offset, pSize, qSize;
  543. /* SAB New variables */
  544. unsigned int keyRecordSize;
  545. unsigned int pbytes = BN_num_bytes(p);
  546. unsigned int qbytes = BN_num_bytes(q);
  547. unsigned int dmp1bytes = BN_num_bytes(dmp1);
  548. unsigned int dmq1bytes = BN_num_bytes(dmq1);
  549. unsigned int iqmpbytes = BN_num_bytes(iqmp);
  550. /* Prepare the params */
  551. BN_CTX_start(ctx);
  552. argument = BN_CTX_get(ctx);
  553. result = BN_CTX_get(ctx);
  554. key = BN_CTX_get(ctx);
  555. if (!argument || !result || !key) {
  556. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_BN_CTX_FULL);
  557. goto err;
  558. }
  559. if (!bn_wexpand(argument, p->top + q->top) ||
  560. !bn_wexpand(result, p->top + q->top) ||
  561. !bn_wexpand(key, sizeof(*privKey) / BN_BYTES)) {
  562. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_BN_EXPAND_FAIL);
  563. goto err;
  564. }
  565. privKey = (ICA_KEY_RSA_CRT *)key->d;
  566. /*
  567. * SAB Add check for total size in bytes of the parms does not exceed the
  568. * buffer space we have do this first
  569. */
  570. keyRecordSize = pbytes + qbytes + dmp1bytes + dmq1bytes + iqmpbytes;
  571. if (keyRecordSize > sizeof(privKey->keyRecord)) {
  572. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  573. goto err;
  574. }
  575. if ((qbytes + dmq1bytes) > 256) {
  576. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  577. goto err;
  578. }
  579. if (pbytes + dmp1bytes > 256) {
  580. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  581. goto err;
  582. }
  583. /* end SAB additions */
  584. memset(privKey, 0, sizeof(ICA_KEY_RSA_CRT));
  585. privKey->keyType = CORRECT_ENDIANNESS(CRT_KEY_TYPE);
  586. privKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_CRT));
  587. privKey->modulusBitLength = CORRECT_ENDIANNESS(BN_num_bytes(q) * 2 * 8);
  588. /*
  589. * p,dp & qInv are 1 QWORD Larger
  590. */
  591. privKey->pLength = CORRECT_ENDIANNESS(BN_num_bytes(p) + 8);
  592. privKey->qLength = CORRECT_ENDIANNESS(BN_num_bytes(q));
  593. privKey->dpLength = CORRECT_ENDIANNESS(BN_num_bytes(dmp1) + 8);
  594. privKey->dqLength = CORRECT_ENDIANNESS(BN_num_bytes(dmq1));
  595. privKey->qInvLength = CORRECT_ENDIANNESS(BN_num_bytes(iqmp) + 8);
  596. offset = (char *)privKey->keyRecord - (char *)privKey;
  597. qSize = BN_num_bytes(q);
  598. pSize = qSize + 8; /* 1 QWORD larger */
  599. /*
  600. * SAB probably aittle redundant, but we'll verify that each of the
  601. * components which make up a key record sent ot the card does not exceed
  602. * the space that is allocated for it. this handles the case where even
  603. * if the total length does not exceed keyrecord zied, if the operands are
  604. * funny sized they could cause potential side affects on either the card
  605. * or the result
  606. */
  607. if ((pbytes > pSize) || (dmp1bytes > pSize) ||
  608. (iqmpbytes > pSize) || (qbytes > qSize) || (dmq1bytes > qSize)) {
  609. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
  610. goto err;
  611. }
  612. privKey->dpOffset = CORRECT_ENDIANNESS(offset);
  613. offset += pSize;
  614. privKey->dqOffset = CORRECT_ENDIANNESS(offset);
  615. offset += qSize;
  616. privKey->pOffset = CORRECT_ENDIANNESS(offset);
  617. offset += pSize;
  618. privKey->qOffset = CORRECT_ENDIANNESS(offset);
  619. offset += qSize;
  620. privKey->qInvOffset = CORRECT_ENDIANNESS(offset);
  621. pkey = (char *)privKey->keyRecord;
  622. /* SAB first check that we don;t under flow the buffer */
  623. if (pSize < pbytes) {
  624. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_UNDERFLOW_CONDITION);
  625. goto err;
  626. }
  627. /* pkey += pSize - BN_num_bytes(p); WROING this should be dmp1) */
  628. pkey += pSize - BN_num_bytes(dmp1);
  629. BN_bn2bin(dmp1, pkey);
  630. pkey += BN_num_bytes(dmp1); /* move the pointer */
  631. BN_bn2bin(dmq1, pkey); /* Copy over dmq1 */
  632. pkey += qSize; /* move pointer */
  633. /* set up for zero padding of next field */
  634. pkey += pSize - BN_num_bytes(p);
  635. BN_bn2bin(p, pkey);
  636. /* increment pointer by number of bytes moved */
  637. pkey += BN_num_bytes(p);
  638. BN_bn2bin(q, pkey);
  639. pkey += qSize; /* move the pointer */
  640. pkey += pSize - BN_num_bytes(iqmp); /* Adjust for padding */
  641. BN_bn2bin(iqmp, pkey);
  642. /* Prepare the argument and response */
  643. /*
  644. * Correct endianess is used because the fields were converted above
  645. */
  646. outLen = CORRECT_ENDIANNESS(privKey->qLength) * 2;
  647. if (outLen > 256) {
  648. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OUTLEN_TO_LARGE);
  649. goto err;
  650. }
  651. /* SAB check for underflow here on the argeument */
  652. if (outLen < BN_num_bytes(a)) {
  653. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_UNDERFLOW_CONDITION);
  654. goto err;
  655. }
  656. BN_bn2bin(a, (unsigned char *)argument->d + outLen - BN_num_bytes(a));
  657. inLen = outLen;
  658. memset(result->d, 0, outLen);
  659. /* Perform the operation */
  660. if ((rc = p_icaRsaCrt(handle, inLen, (unsigned char *)argument->d,
  661. privKey, &outLen, (unsigned char *)result->d)) != 0)
  662. {
  663. printf("rc = %d\n", rc);
  664. IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_REQUEST_FAILED);
  665. goto err;
  666. }
  667. /* Convert the response */
  668. BN_bin2bn((unsigned char *)result->d, outLen, r);
  669. to_return = 1;
  670. err:
  671. BN_CTX_end(ctx);
  672. return to_return;
  673. }
  674. # ifndef OPENSSL_NO_DSA
  675. /*
  676. * This code was liberated and adapted from the commented-out code in
  677. * dsa_ossl.c. Because of the unoptimised form of the Ibmca acceleration (it
  678. * doesn't have a CRT form for RSA), this function means that an Ibmca system
  679. * running with a DSA server certificate can handshake around 5 or 6 times
  680. * faster/more than an equivalent system running with RSA. Just check out the
  681. * "signs" statistics from the RSA and DSA parts of "openssl speed -engine
  682. * ibmca dsa1024 rsa1024".
  683. */
  684. static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  685. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  686. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  687. {
  688. BIGNUM t;
  689. int to_return = 0;
  690. BN_init(&t);
  691. /* let rr = a1 ^ p1 mod m */
  692. if (!ibmca_mod_exp(rr, a1, p1, m, ctx))
  693. goto end;
  694. /* let t = a2 ^ p2 mod m */
  695. if (!ibmca_mod_exp(&t, a2, p2, m, ctx))
  696. goto end;
  697. /* let rr = rr * t mod m */
  698. if (!BN_mod_mul(rr, rr, &t, m, ctx))
  699. goto end;
  700. to_return = 1;
  701. end:
  702. BN_free(&t);
  703. return to_return;
  704. }
  705. static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  706. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  707. BN_MONT_CTX *m_ctx)
  708. {
  709. return ibmca_mod_exp(r, a, p, m, ctx);
  710. }
  711. # endif
  712. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  713. static int ibmca_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. return ibmca_mod_exp(r, a, p, m, ctx);
  718. }
  719. # ifndef OPENSSL_NO_DH
  720. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  721. static int ibmca_mod_exp_dh(DH const *dh, BIGNUM *r,
  722. const BIGNUM *a, const BIGNUM *p,
  723. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  724. {
  725. return ibmca_mod_exp(r, a, p, m, ctx);
  726. }
  727. # endif
  728. /* Random bytes are good */
  729. static int ibmca_rand_bytes(unsigned char *buf, int num)
  730. {
  731. int to_return = 0; /* assume failure */
  732. unsigned int ret;
  733. if (handle == 0) {
  734. IBMCAerr(IBMCA_F_IBMCA_RAND_BYTES, IBMCA_R_NOT_INITIALISED);
  735. goto err;
  736. }
  737. ret = p_icaRandomNumberGenerate(handle, num, buf);
  738. if (ret < 0) {
  739. IBMCAerr(IBMCA_F_IBMCA_RAND_BYTES, IBMCA_R_REQUEST_FAILED);
  740. goto err;
  741. }
  742. to_return = 1;
  743. err:
  744. return to_return;
  745. }
  746. static int ibmca_rand_status(void)
  747. {
  748. return 1;
  749. }
  750. /*
  751. * This stuff is needed if this ENGINE is being compiled into a
  752. * self-contained shared-library.
  753. */
  754. # ifdef ENGINE_DYNAMIC_SUPPORT
  755. static int bind_fn(ENGINE *e, const char *id)
  756. {
  757. if (id && (strcmp(id, engine_ibmca_id) != 0)) /* WJH XXX */
  758. return 0;
  759. if (!bind_helper(e))
  760. return 0;
  761. return 1;
  762. }
  763. IMPLEMENT_DYNAMIC_CHECK_FN()
  764. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  765. # endif /* ENGINE_DYNAMIC_SUPPORT */
  766. # endif /* !OPENSSL_NO_HW_IBMCA */
  767. #endif /* !OPENSSL_NO_HW */