eng_cryptodev.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
  3. * Copyright (c) 2002 Theo de Raadt
  4. * Copyright (c) 2002 Markus Friedl
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. #include <string.h>
  29. #include <openssl/objects.h>
  30. #include <openssl/engine.h>
  31. #include <openssl/evp.h>
  32. #include <openssl/bn.h>
  33. #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
  34. (defined(OpenBSD) || defined(__FreeBSD__))
  35. # include <sys/param.h>
  36. # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
  37. # define HAVE_CRYPTODEV
  38. # endif
  39. # if (OpenBSD >= 200110)
  40. # define HAVE_SYSLOG_R
  41. # endif
  42. #endif
  43. #ifndef HAVE_CRYPTODEV
  44. void ENGINE_load_cryptodev(void)
  45. {
  46. /* This is a NOP on platforms without /dev/crypto */
  47. return;
  48. }
  49. #else
  50. # include <sys/types.h>
  51. # include <crypto/cryptodev.h>
  52. # include <openssl/dh.h>
  53. # include <openssl/dsa.h>
  54. # include <openssl/err.h>
  55. # include <openssl/rsa.h>
  56. # include <sys/ioctl.h>
  57. # include <errno.h>
  58. # include <stdio.h>
  59. # include <unistd.h>
  60. # include <fcntl.h>
  61. # include <stdarg.h>
  62. # include <syslog.h>
  63. # include <errno.h>
  64. # include <string.h>
  65. struct dev_crypto_state {
  66. struct session_op d_sess;
  67. int d_fd;
  68. # ifdef USE_CRYPTODEV_DIGESTS
  69. char dummy_mac_key[HASH_MAX_LEN];
  70. unsigned char digest_res[HASH_MAX_LEN];
  71. char *mac_data;
  72. int mac_len;
  73. # endif
  74. };
  75. static u_int32_t cryptodev_asymfeat = 0;
  76. static int get_asym_dev_crypto(void);
  77. static int open_dev_crypto(void);
  78. static int get_dev_crypto(void);
  79. static int get_cryptodev_ciphers(const int **cnids);
  80. # ifdef USE_CRYPTODEV_DIGESTS
  81. static int get_cryptodev_digests(const int **cnids);
  82. # endif
  83. static int cryptodev_usable_ciphers(const int **nids);
  84. static int cryptodev_usable_digests(const int **nids);
  85. static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  86. const unsigned char *in, size_t inl);
  87. static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  88. const unsigned char *iv, int enc);
  89. static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
  90. static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  91. const int **nids, int nid);
  92. static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  93. const int **nids, int nid);
  94. static int bn2crparam(const BIGNUM *a, struct crparam *crp);
  95. static int crparam2bn(struct crparam *crp, BIGNUM *a);
  96. static void zapparams(struct crypt_kop *kop);
  97. static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
  98. int slen, BIGNUM *s);
  99. static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
  100. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  101. BN_MONT_CTX *m_ctx);
  102. static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  103. BN_CTX *ctx);
  104. static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  105. BN_CTX *ctx);
  106. static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
  107. const BIGNUM *p, const BIGNUM *m,
  108. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  109. static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
  110. BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
  111. BIGNUM *p, BN_CTX *ctx,
  112. BN_MONT_CTX *mont);
  113. static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  114. DSA *dsa);
  115. static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
  116. DSA_SIG *sig, DSA *dsa);
  117. static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  118. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  119. BN_MONT_CTX *m_ctx);
  120. static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
  121. DH *dh);
  122. static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
  123. void (*f) (void));
  124. void ENGINE_load_cryptodev(void);
  125. static const ENGINE_CMD_DEFN cryptodev_defns[] = {
  126. {0, NULL, NULL, 0}
  127. };
  128. static struct {
  129. int id;
  130. int nid;
  131. int ivmax;
  132. int keylen;
  133. } ciphers[] = {
  134. {
  135. CRYPTO_ARC4, NID_rc4, 0, 16,
  136. },
  137. {
  138. CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
  139. },
  140. {
  141. CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
  142. },
  143. {
  144. CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
  145. },
  146. {
  147. CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
  148. },
  149. {
  150. CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
  151. },
  152. # ifdef CRYPTO_AES_CTR
  153. {
  154. CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
  155. },
  156. {
  157. CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
  158. },
  159. {
  160. CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
  161. },
  162. # endif
  163. {
  164. CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
  165. },
  166. {
  167. CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
  168. },
  169. {
  170. CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
  171. },
  172. {
  173. 0, NID_undef, 0, 0,
  174. },
  175. };
  176. # ifdef USE_CRYPTODEV_DIGESTS
  177. static struct {
  178. int id;
  179. int nid;
  180. int keylen;
  181. } digests[] = {
  182. {
  183. CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
  184. },
  185. {
  186. CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
  187. },
  188. {
  189. CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
  190. /* ? */
  191. },
  192. {
  193. CRYPTO_MD5_KPDK, NID_undef, 0
  194. },
  195. {
  196. CRYPTO_SHA1_KPDK, NID_undef, 0
  197. },
  198. {
  199. CRYPTO_MD5, NID_md5, 16
  200. },
  201. {
  202. CRYPTO_SHA1, NID_sha1, 20
  203. },
  204. {
  205. 0, NID_undef, 0
  206. },
  207. };
  208. # endif
  209. /*
  210. * Return a fd if /dev/crypto seems usable, 0 otherwise.
  211. */
  212. static int open_dev_crypto(void)
  213. {
  214. static int fd = -1;
  215. if (fd == -1) {
  216. if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
  217. return (-1);
  218. /* close on exec */
  219. if (fcntl(fd, F_SETFD, 1) == -1) {
  220. close(fd);
  221. fd = -1;
  222. return (-1);
  223. }
  224. }
  225. return (fd);
  226. }
  227. static int get_dev_crypto(void)
  228. {
  229. int fd, retfd;
  230. if ((fd = open_dev_crypto()) == -1)
  231. return (-1);
  232. # ifndef CRIOGET_NOT_NEEDED
  233. if (ioctl(fd, CRIOGET, &retfd) == -1)
  234. return (-1);
  235. /* close on exec */
  236. if (fcntl(retfd, F_SETFD, 1) == -1) {
  237. close(retfd);
  238. return (-1);
  239. }
  240. # else
  241. retfd = fd;
  242. # endif
  243. return (retfd);
  244. }
  245. static void put_dev_crypto(int fd)
  246. {
  247. # ifndef CRIOGET_NOT_NEEDED
  248. close(fd);
  249. # endif
  250. }
  251. /* Caching version for asym operations */
  252. static int get_asym_dev_crypto(void)
  253. {
  254. static int fd = -1;
  255. if (fd == -1)
  256. fd = get_dev_crypto();
  257. return fd;
  258. }
  259. /*
  260. * Find out what ciphers /dev/crypto will let us have a session for.
  261. * XXX note, that some of these openssl doesn't deal with yet!
  262. * returning them here is harmless, as long as we return NULL
  263. * when asked for a handler in the cryptodev_engine_ciphers routine
  264. */
  265. static int get_cryptodev_ciphers(const int **cnids)
  266. {
  267. static int nids[CRYPTO_ALGORITHM_MAX];
  268. struct session_op sess;
  269. int fd, i, count = 0;
  270. if ((fd = get_dev_crypto()) < 0) {
  271. *cnids = NULL;
  272. return (0);
  273. }
  274. memset(&sess, 0, sizeof(sess));
  275. sess.key = (caddr_t) "123456789abcdefghijklmno";
  276. for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  277. if (ciphers[i].nid == NID_undef)
  278. continue;
  279. sess.cipher = ciphers[i].id;
  280. sess.keylen = ciphers[i].keylen;
  281. sess.mac = 0;
  282. if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  283. ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  284. nids[count++] = ciphers[i].nid;
  285. }
  286. put_dev_crypto(fd);
  287. if (count > 0)
  288. *cnids = nids;
  289. else
  290. *cnids = NULL;
  291. return (count);
  292. }
  293. # ifdef USE_CRYPTODEV_DIGESTS
  294. /*
  295. * Find out what digests /dev/crypto will let us have a session for.
  296. * XXX note, that some of these openssl doesn't deal with yet!
  297. * returning them here is harmless, as long as we return NULL
  298. * when asked for a handler in the cryptodev_engine_digests routine
  299. */
  300. static int get_cryptodev_digests(const int **cnids)
  301. {
  302. static int nids[CRYPTO_ALGORITHM_MAX];
  303. struct session_op sess;
  304. int fd, i, count = 0;
  305. if ((fd = get_dev_crypto()) < 0) {
  306. *cnids = NULL;
  307. return (0);
  308. }
  309. memset(&sess, 0, sizeof(sess));
  310. sess.mackey = (caddr_t) "123456789abcdefghijklmno";
  311. for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  312. if (digests[i].nid == NID_undef)
  313. continue;
  314. sess.mac = digests[i].id;
  315. sess.mackeylen = digests[i].keylen;
  316. sess.cipher = 0;
  317. if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  318. ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  319. nids[count++] = digests[i].nid;
  320. }
  321. put_dev_crypto(fd);
  322. if (count > 0)
  323. *cnids = nids;
  324. else
  325. *cnids = NULL;
  326. return (count);
  327. }
  328. # endif /* 0 */
  329. /*
  330. * Find the useable ciphers|digests from dev/crypto - this is the first
  331. * thing called by the engine init crud which determines what it
  332. * can use for ciphers from this engine. We want to return
  333. * only what we can do, anythine else is handled by software.
  334. *
  335. * If we can't initialize the device to do anything useful for
  336. * any reason, we want to return a NULL array, and 0 length,
  337. * which forces everything to be done is software. By putting
  338. * the initalization of the device in here, we ensure we can
  339. * use this engine as the default, and if for whatever reason
  340. * /dev/crypto won't do what we want it will just be done in
  341. * software
  342. *
  343. * This can (should) be greatly expanded to perhaps take into
  344. * account speed of the device, and what we want to do.
  345. * (although the disabling of particular alg's could be controlled
  346. * by the device driver with sysctl's.) - this is where we
  347. * want most of the decisions made about what we actually want
  348. * to use from /dev/crypto.
  349. */
  350. static int cryptodev_usable_ciphers(const int **nids)
  351. {
  352. return (get_cryptodev_ciphers(nids));
  353. }
  354. static int cryptodev_usable_digests(const int **nids)
  355. {
  356. # ifdef USE_CRYPTODEV_DIGESTS
  357. return (get_cryptodev_digests(nids));
  358. # else
  359. /*
  360. * XXXX just disable all digests for now, because it sucks.
  361. * we need a better way to decide this - i.e. I may not
  362. * want digests on slow cards like hifn on fast machines,
  363. * but might want them on slow or loaded machines, etc.
  364. * will also want them when using crypto cards that don't
  365. * suck moose gonads - would be nice to be able to decide something
  366. * as reasonable default without having hackery that's card dependent.
  367. * of course, the default should probably be just do everything,
  368. * with perhaps a sysctl to turn algoritms off (or have them off
  369. * by default) on cards that generally suck like the hifn.
  370. */
  371. *nids = NULL;
  372. return (0);
  373. # endif
  374. }
  375. static int
  376. cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  377. const unsigned char *in, size_t inl)
  378. {
  379. struct crypt_op cryp;
  380. struct dev_crypto_state *state = ctx->cipher_data;
  381. struct session_op *sess = &state->d_sess;
  382. const void *iiv;
  383. unsigned char save_iv[EVP_MAX_IV_LENGTH];
  384. if (state->d_fd < 0)
  385. return (0);
  386. if (!inl)
  387. return (1);
  388. if ((inl % ctx->cipher->block_size) != 0)
  389. return (0);
  390. memset(&cryp, 0, sizeof(cryp));
  391. cryp.ses = sess->ses;
  392. cryp.flags = 0;
  393. cryp.len = inl;
  394. cryp.src = (caddr_t) in;
  395. cryp.dst = (caddr_t) out;
  396. cryp.mac = 0;
  397. cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  398. if (ctx->cipher->iv_len) {
  399. cryp.iv = (caddr_t) ctx->iv;
  400. if (!ctx->encrypt) {
  401. iiv = in + inl - ctx->cipher->iv_len;
  402. memcpy(save_iv, iiv, ctx->cipher->iv_len);
  403. }
  404. } else
  405. cryp.iv = NULL;
  406. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
  407. /*
  408. * XXX need better errror handling this can fail for a number of
  409. * different reasons.
  410. */
  411. return (0);
  412. }
  413. if (ctx->cipher->iv_len) {
  414. if (ctx->encrypt)
  415. iiv = out + inl - ctx->cipher->iv_len;
  416. else
  417. iiv = save_iv;
  418. memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
  419. }
  420. return (1);
  421. }
  422. static int
  423. cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  424. const unsigned char *iv, int enc)
  425. {
  426. struct dev_crypto_state *state = ctx->cipher_data;
  427. struct session_op *sess = &state->d_sess;
  428. int cipher = -1, i;
  429. for (i = 0; ciphers[i].id; i++)
  430. if (ctx->cipher->nid == ciphers[i].nid &&
  431. ctx->cipher->iv_len <= ciphers[i].ivmax &&
  432. ctx->key_len == ciphers[i].keylen) {
  433. cipher = ciphers[i].id;
  434. break;
  435. }
  436. if (!ciphers[i].id) {
  437. state->d_fd = -1;
  438. return (0);
  439. }
  440. memset(sess, 0, sizeof(struct session_op));
  441. if ((state->d_fd = get_dev_crypto()) < 0)
  442. return (0);
  443. sess->key = (caddr_t) key;
  444. sess->keylen = ctx->key_len;
  445. sess->cipher = cipher;
  446. if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
  447. put_dev_crypto(state->d_fd);
  448. state->d_fd = -1;
  449. return (0);
  450. }
  451. return (1);
  452. }
  453. /*
  454. * free anything we allocated earlier when initting a
  455. * session, and close the session.
  456. */
  457. static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
  458. {
  459. int ret = 0;
  460. struct dev_crypto_state *state = ctx->cipher_data;
  461. struct session_op *sess = &state->d_sess;
  462. if (state->d_fd < 0)
  463. return (0);
  464. /*
  465. * XXX if this ioctl fails, someting's wrong. the invoker may have called
  466. * us with a bogus ctx, or we could have a device that for whatever
  467. * reason just doesn't want to play ball - it's not clear what's right
  468. * here - should this be an error? should it just increase a counter,
  469. * hmm. For right now, we return 0 - I don't believe that to be "right".
  470. * we could call the gorpy openssl lib error handlers that print messages
  471. * to users of the library. hmm..
  472. */
  473. if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
  474. ret = 0;
  475. } else {
  476. ret = 1;
  477. }
  478. put_dev_crypto(state->d_fd);
  479. state->d_fd = -1;
  480. return (ret);
  481. }
  482. /*
  483. * libcrypto EVP stuff - this is how we get wired to EVP so the engine
  484. * gets called when libcrypto requests a cipher NID.
  485. */
  486. /* RC4 */
  487. const EVP_CIPHER cryptodev_rc4 = {
  488. NID_rc4,
  489. 1, 16, 0,
  490. EVP_CIPH_VARIABLE_LENGTH,
  491. cryptodev_init_key,
  492. cryptodev_cipher,
  493. cryptodev_cleanup,
  494. sizeof(struct dev_crypto_state),
  495. NULL,
  496. NULL,
  497. NULL
  498. };
  499. /* DES CBC EVP */
  500. const EVP_CIPHER cryptodev_des_cbc = {
  501. NID_des_cbc,
  502. 8, 8, 8,
  503. EVP_CIPH_CBC_MODE,
  504. cryptodev_init_key,
  505. cryptodev_cipher,
  506. cryptodev_cleanup,
  507. sizeof(struct dev_crypto_state),
  508. EVP_CIPHER_set_asn1_iv,
  509. EVP_CIPHER_get_asn1_iv,
  510. NULL
  511. };
  512. /* 3DES CBC EVP */
  513. const EVP_CIPHER cryptodev_3des_cbc = {
  514. NID_des_ede3_cbc,
  515. 8, 24, 8,
  516. EVP_CIPH_CBC_MODE,
  517. cryptodev_init_key,
  518. cryptodev_cipher,
  519. cryptodev_cleanup,
  520. sizeof(struct dev_crypto_state),
  521. EVP_CIPHER_set_asn1_iv,
  522. EVP_CIPHER_get_asn1_iv,
  523. NULL
  524. };
  525. const EVP_CIPHER cryptodev_bf_cbc = {
  526. NID_bf_cbc,
  527. 8, 16, 8,
  528. EVP_CIPH_CBC_MODE,
  529. cryptodev_init_key,
  530. cryptodev_cipher,
  531. cryptodev_cleanup,
  532. sizeof(struct dev_crypto_state),
  533. EVP_CIPHER_set_asn1_iv,
  534. EVP_CIPHER_get_asn1_iv,
  535. NULL
  536. };
  537. const EVP_CIPHER cryptodev_cast_cbc = {
  538. NID_cast5_cbc,
  539. 8, 16, 8,
  540. EVP_CIPH_CBC_MODE,
  541. cryptodev_init_key,
  542. cryptodev_cipher,
  543. cryptodev_cleanup,
  544. sizeof(struct dev_crypto_state),
  545. EVP_CIPHER_set_asn1_iv,
  546. EVP_CIPHER_get_asn1_iv,
  547. NULL
  548. };
  549. const EVP_CIPHER cryptodev_aes_cbc = {
  550. NID_aes_128_cbc,
  551. 16, 16, 16,
  552. EVP_CIPH_CBC_MODE,
  553. cryptodev_init_key,
  554. cryptodev_cipher,
  555. cryptodev_cleanup,
  556. sizeof(struct dev_crypto_state),
  557. EVP_CIPHER_set_asn1_iv,
  558. EVP_CIPHER_get_asn1_iv,
  559. NULL
  560. };
  561. const EVP_CIPHER cryptodev_aes_192_cbc = {
  562. NID_aes_192_cbc,
  563. 16, 24, 16,
  564. EVP_CIPH_CBC_MODE,
  565. cryptodev_init_key,
  566. cryptodev_cipher,
  567. cryptodev_cleanup,
  568. sizeof(struct dev_crypto_state),
  569. EVP_CIPHER_set_asn1_iv,
  570. EVP_CIPHER_get_asn1_iv,
  571. NULL
  572. };
  573. const EVP_CIPHER cryptodev_aes_256_cbc = {
  574. NID_aes_256_cbc,
  575. 16, 32, 16,
  576. EVP_CIPH_CBC_MODE,
  577. cryptodev_init_key,
  578. cryptodev_cipher,
  579. cryptodev_cleanup,
  580. sizeof(struct dev_crypto_state),
  581. EVP_CIPHER_set_asn1_iv,
  582. EVP_CIPHER_get_asn1_iv,
  583. NULL
  584. };
  585. # ifdef CRYPTO_AES_CTR
  586. const EVP_CIPHER cryptodev_aes_ctr = {
  587. NID_aes_128_ctr,
  588. 16, 16, 14,
  589. EVP_CIPH_CTR_MODE,
  590. cryptodev_init_key,
  591. cryptodev_cipher,
  592. cryptodev_cleanup,
  593. sizeof(struct dev_crypto_state),
  594. EVP_CIPHER_set_asn1_iv,
  595. EVP_CIPHER_get_asn1_iv,
  596. NULL
  597. };
  598. const EVP_CIPHER cryptodev_aes_ctr_192 = {
  599. NID_aes_192_ctr,
  600. 16, 24, 14,
  601. EVP_CIPH_CTR_MODE,
  602. cryptodev_init_key,
  603. cryptodev_cipher,
  604. cryptodev_cleanup,
  605. sizeof(struct dev_crypto_state),
  606. EVP_CIPHER_set_asn1_iv,
  607. EVP_CIPHER_get_asn1_iv,
  608. NULL
  609. };
  610. const EVP_CIPHER cryptodev_aes_ctr_256 = {
  611. NID_aes_256_ctr,
  612. 16, 32, 14,
  613. EVP_CIPH_CTR_MODE,
  614. cryptodev_init_key,
  615. cryptodev_cipher,
  616. cryptodev_cleanup,
  617. sizeof(struct dev_crypto_state),
  618. EVP_CIPHER_set_asn1_iv,
  619. EVP_CIPHER_get_asn1_iv,
  620. NULL
  621. };
  622. # endif
  623. /*
  624. * Registered by the ENGINE when used to find out how to deal with
  625. * a particular NID in the ENGINE. this says what we'll do at the
  626. * top level - note, that list is restricted by what we answer with
  627. */
  628. static int
  629. cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  630. const int **nids, int nid)
  631. {
  632. if (!cipher)
  633. return (cryptodev_usable_ciphers(nids));
  634. switch (nid) {
  635. case NID_rc4:
  636. *cipher = &cryptodev_rc4;
  637. break;
  638. case NID_des_ede3_cbc:
  639. *cipher = &cryptodev_3des_cbc;
  640. break;
  641. case NID_des_cbc:
  642. *cipher = &cryptodev_des_cbc;
  643. break;
  644. case NID_bf_cbc:
  645. *cipher = &cryptodev_bf_cbc;
  646. break;
  647. case NID_cast5_cbc:
  648. *cipher = &cryptodev_cast_cbc;
  649. break;
  650. case NID_aes_128_cbc:
  651. *cipher = &cryptodev_aes_cbc;
  652. break;
  653. case NID_aes_192_cbc:
  654. *cipher = &cryptodev_aes_192_cbc;
  655. break;
  656. case NID_aes_256_cbc:
  657. *cipher = &cryptodev_aes_256_cbc;
  658. break;
  659. # ifdef CRYPTO_AES_CTR
  660. case NID_aes_128_ctr:
  661. *cipher = &cryptodev_aes_ctr;
  662. break;
  663. case NID_aes_192_ctr:
  664. *cipher = &cryptodev_aes_ctr_192;
  665. break;
  666. case NID_aes_256_ctr:
  667. *cipher = &cryptodev_aes_ctr_256;
  668. break;
  669. # endif
  670. default:
  671. *cipher = NULL;
  672. break;
  673. }
  674. return (*cipher != NULL);
  675. }
  676. # ifdef USE_CRYPTODEV_DIGESTS
  677. /* convert digest type to cryptodev */
  678. static int digest_nid_to_cryptodev(int nid)
  679. {
  680. int i;
  681. for (i = 0; digests[i].id; i++)
  682. if (digests[i].nid == nid)
  683. return (digests[i].id);
  684. return (0);
  685. }
  686. static int digest_key_length(int nid)
  687. {
  688. int i;
  689. for (i = 0; digests[i].id; i++)
  690. if (digests[i].nid == nid)
  691. return digests[i].keylen;
  692. return (0);
  693. }
  694. static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  695. {
  696. struct dev_crypto_state *state = ctx->md_data;
  697. struct session_op *sess = &state->d_sess;
  698. int digest;
  699. if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) {
  700. printf("cryptodev_digest_init: Can't get digest \n");
  701. return (0);
  702. }
  703. memset(state, 0, sizeof(struct dev_crypto_state));
  704. if ((state->d_fd = get_dev_crypto()) < 0) {
  705. printf("cryptodev_digest_init: Can't get Dev \n");
  706. return (0);
  707. }
  708. sess->mackey = state->dummy_mac_key;
  709. sess->mackeylen = digest_key_length(ctx->digest->type);
  710. sess->mac = digest;
  711. if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
  712. put_dev_crypto(state->d_fd);
  713. state->d_fd = -1;
  714. printf("cryptodev_digest_init: Open session failed\n");
  715. return (0);
  716. }
  717. return (1);
  718. }
  719. static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
  720. size_t count)
  721. {
  722. struct crypt_op cryp;
  723. struct dev_crypto_state *state = ctx->md_data;
  724. struct session_op *sess = &state->d_sess;
  725. if (!data || state->d_fd < 0) {
  726. printf("cryptodev_digest_update: illegal inputs \n");
  727. return (0);
  728. }
  729. if (!count) {
  730. return (0);
  731. }
  732. if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  733. /* if application doesn't support one buffer */
  734. char *mac_data =
  735. OPENSSL_realloc(state->mac_data, state->mac_len + count);
  736. if (mac_data == NULL) {
  737. printf("cryptodev_digest_update: realloc failed\n");
  738. return (0);
  739. }
  740. state->mac_data = mac_data;
  741. memcpy(state->mac_data + state->mac_len, data, count);
  742. state->mac_len += count;
  743. return (1);
  744. }
  745. memset(&cryp, 0, sizeof(cryp));
  746. cryp.ses = sess->ses;
  747. cryp.flags = 0;
  748. cryp.len = count;
  749. cryp.src = (caddr_t) data;
  750. cryp.dst = NULL;
  751. cryp.mac = (caddr_t) state->digest_res;
  752. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  753. printf("cryptodev_digest_update: digest failed\n");
  754. return (0);
  755. }
  756. return (1);
  757. }
  758. static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  759. {
  760. struct crypt_op cryp;
  761. struct dev_crypto_state *state = ctx->md_data;
  762. struct session_op *sess = &state->d_sess;
  763. int ret = 1;
  764. if (!md || state->d_fd < 0) {
  765. printf("cryptodev_digest_final: illegal input\n");
  766. return (0);
  767. }
  768. if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  769. /* if application doesn't support one buffer */
  770. memset(&cryp, 0, sizeof(cryp));
  771. cryp.ses = sess->ses;
  772. cryp.flags = 0;
  773. cryp.len = state->mac_len;
  774. cryp.src = state->mac_data;
  775. cryp.dst = NULL;
  776. cryp.mac = (caddr_t) md;
  777. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  778. printf("cryptodev_digest_final: digest failed\n");
  779. return (0);
  780. }
  781. return 1;
  782. }
  783. memcpy(md, state->digest_res, ctx->digest->md_size);
  784. return (ret);
  785. }
  786. static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
  787. {
  788. int ret = 1;
  789. struct dev_crypto_state *state = ctx->md_data;
  790. struct session_op *sess = &state->d_sess;
  791. if (state == NULL)
  792. return 0;
  793. if (state->d_fd < 0) {
  794. printf("cryptodev_digest_cleanup: illegal input\n");
  795. return (0);
  796. }
  797. if (state->mac_data) {
  798. OPENSSL_free(state->mac_data);
  799. state->mac_data = NULL;
  800. state->mac_len = 0;
  801. }
  802. if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
  803. printf("cryptodev_digest_cleanup: failed to close session\n");
  804. ret = 0;
  805. } else {
  806. ret = 1;
  807. }
  808. put_dev_crypto(state->d_fd);
  809. state->d_fd = -1;
  810. return (ret);
  811. }
  812. static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  813. {
  814. struct dev_crypto_state *fstate = from->md_data;
  815. struct dev_crypto_state *dstate = to->md_data;
  816. struct session_op *sess;
  817. int digest;
  818. if (dstate == NULL || fstate == NULL)
  819. return 1;
  820. memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
  821. sess = &dstate->d_sess;
  822. digest = digest_nid_to_cryptodev(to->digest->type);
  823. sess->mackey = dstate->dummy_mac_key;
  824. sess->mackeylen = digest_key_length(to->digest->type);
  825. sess->mac = digest;
  826. dstate->d_fd = get_dev_crypto();
  827. if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
  828. put_dev_crypto(dstate->d_fd);
  829. dstate->d_fd = -1;
  830. printf("cryptodev_digest_init: Open session failed\n");
  831. return (0);
  832. }
  833. dstate->mac_len = fstate->mac_len;
  834. if (fstate->mac_len != 0) {
  835. if (fstate->mac_data != NULL) {
  836. dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
  837. if (dstate->mac_data == NULL) {
  838. printf("cryptodev_digest_init: malloc failed\n");
  839. return 0;
  840. }
  841. memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
  842. }
  843. }
  844. return 1;
  845. }
  846. const EVP_MD cryptodev_sha1 = {
  847. NID_sha1,
  848. NID_undef,
  849. SHA_DIGEST_LENGTH,
  850. EVP_MD_FLAG_ONESHOT,
  851. cryptodev_digest_init,
  852. cryptodev_digest_update,
  853. cryptodev_digest_final,
  854. cryptodev_digest_copy,
  855. cryptodev_digest_cleanup,
  856. EVP_PKEY_NULL_method,
  857. SHA_CBLOCK,
  858. sizeof(struct dev_crypto_state),
  859. };
  860. const EVP_MD cryptodev_md5 = {
  861. NID_md5,
  862. NID_undef,
  863. 16 /* MD5_DIGEST_LENGTH */ ,
  864. EVP_MD_FLAG_ONESHOT,
  865. cryptodev_digest_init,
  866. cryptodev_digest_update,
  867. cryptodev_digest_final,
  868. cryptodev_digest_copy,
  869. cryptodev_digest_cleanup,
  870. EVP_PKEY_NULL_method,
  871. 64 /* MD5_CBLOCK */ ,
  872. sizeof(struct dev_crypto_state),
  873. };
  874. # endif /* USE_CRYPTODEV_DIGESTS */
  875. static int
  876. cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  877. const int **nids, int nid)
  878. {
  879. if (!digest)
  880. return (cryptodev_usable_digests(nids));
  881. switch (nid) {
  882. # ifdef USE_CRYPTODEV_DIGESTS
  883. case NID_md5:
  884. *digest = &cryptodev_md5;
  885. break;
  886. case NID_sha1:
  887. *digest = &cryptodev_sha1;
  888. break;
  889. default:
  890. # endif /* USE_CRYPTODEV_DIGESTS */
  891. *digest = NULL;
  892. break;
  893. }
  894. return (*digest != NULL);
  895. }
  896. /*
  897. * Convert a BIGNUM to the representation that /dev/crypto needs.
  898. * Upon completion of use, the caller is responsible for freeing
  899. * crp->crp_p.
  900. */
  901. static int bn2crparam(const BIGNUM *a, struct crparam *crp)
  902. {
  903. int i, j, k;
  904. ssize_t bytes, bits;
  905. u_char *b;
  906. crp->crp_p = NULL;
  907. crp->crp_nbits = 0;
  908. bits = BN_num_bits(a);
  909. bytes = (bits + 7) / 8;
  910. b = malloc(bytes);
  911. if (b == NULL)
  912. return (1);
  913. memset(b, 0, bytes);
  914. crp->crp_p = (caddr_t) b;
  915. crp->crp_nbits = bits;
  916. for (i = 0, j = 0; i < a->top; i++) {
  917. for (k = 0; k < BN_BITS2 / 8; k++) {
  918. if ((j + k) >= bytes)
  919. return (0);
  920. b[j + k] = a->d[i] >> (k * 8);
  921. }
  922. j += BN_BITS2 / 8;
  923. }
  924. return (0);
  925. }
  926. /* Convert a /dev/crypto parameter to a BIGNUM */
  927. static int crparam2bn(struct crparam *crp, BIGNUM *a)
  928. {
  929. u_int8_t *pd;
  930. int i, bytes;
  931. bytes = (crp->crp_nbits + 7) / 8;
  932. if (bytes == 0)
  933. return (-1);
  934. if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
  935. return (-1);
  936. for (i = 0; i < bytes; i++)
  937. pd[i] = crp->crp_p[bytes - i - 1];
  938. BN_bin2bn(pd, bytes, a);
  939. free(pd);
  940. return (0);
  941. }
  942. static void zapparams(struct crypt_kop *kop)
  943. {
  944. int i;
  945. for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
  946. OPENSSL_free(kop->crk_param[i].crp_p);
  947. kop->crk_param[i].crp_p = NULL;
  948. kop->crk_param[i].crp_nbits = 0;
  949. }
  950. }
  951. static int
  952. cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
  953. BIGNUM *s)
  954. {
  955. int fd, ret = -1;
  956. if ((fd = get_asym_dev_crypto()) < 0)
  957. return ret;
  958. if (r) {
  959. kop->crk_param[kop->crk_iparams].crp_p = OPENSSL_malloc(rlen);
  960. if (kop->crk_param[kop->crk_iparams].crp_p == NULL)
  961. return ret;
  962. memset(kop->crk_param[kop->crk_iparams].crp_p, 0, (size_t)rlen);
  963. kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
  964. kop->crk_oparams++;
  965. }
  966. if (s) {
  967. kop->crk_param[kop->crk_iparams + 1].crp_p = OPENSSL_malloc(slen);
  968. /* No need to free the kop->crk_iparams parameter if it was allocated,
  969. * callers of this routine have to free allocated parameters through
  970. * zapparams both in case of success and failure
  971. */
  972. if (kop->crk_param[kop->crk_iparams+1].crp_p == NULL)
  973. return ret;
  974. memset(kop->crk_param[kop->crk_iparams + 1].crp_p, 0, (size_t)slen);
  975. kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
  976. kop->crk_oparams++;
  977. }
  978. if (ioctl(fd, CIOCKEY, kop) == 0) {
  979. if (r)
  980. crparam2bn(&kop->crk_param[kop->crk_iparams], r);
  981. if (s)
  982. crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
  983. ret = 0;
  984. }
  985. return ret;
  986. }
  987. static int
  988. cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  989. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  990. {
  991. struct crypt_kop kop;
  992. int ret = 1;
  993. /*
  994. * Currently, we know we can do mod exp iff we can do any asymmetric
  995. * operations at all.
  996. */
  997. if (cryptodev_asymfeat == 0) {
  998. ret = BN_mod_exp(r, a, p, m, ctx);
  999. return (ret);
  1000. }
  1001. memset(&kop, 0, sizeof kop);
  1002. kop.crk_op = CRK_MOD_EXP;
  1003. /* inputs: a^p % m */
  1004. if (bn2crparam(a, &kop.crk_param[0]))
  1005. goto err;
  1006. if (bn2crparam(p, &kop.crk_param[1]))
  1007. goto err;
  1008. if (bn2crparam(m, &kop.crk_param[2]))
  1009. goto err;
  1010. kop.crk_iparams = 3;
  1011. if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
  1012. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1013. printf("OCF asym process failed, Running in software\n");
  1014. ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  1015. } else if (ECANCELED == kop.crk_status) {
  1016. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1017. printf("OCF hardware operation cancelled. Running in Software\n");
  1018. ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  1019. }
  1020. /* else cryptodev operation worked ok ==> ret = 1 */
  1021. err:
  1022. zapparams(&kop);
  1023. return (ret);
  1024. }
  1025. static int
  1026. cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  1027. BN_CTX *ctx)
  1028. {
  1029. int r;
  1030. ctx = BN_CTX_new();
  1031. r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
  1032. BN_CTX_free(ctx);
  1033. return (r);
  1034. }
  1035. static int
  1036. cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  1037. {
  1038. struct crypt_kop kop;
  1039. int ret = 1;
  1040. if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  1041. /* XXX 0 means failure?? */
  1042. return (0);
  1043. }
  1044. memset(&kop, 0, sizeof kop);
  1045. kop.crk_op = CRK_MOD_EXP_CRT;
  1046. /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
  1047. if (bn2crparam(rsa->p, &kop.crk_param[0]))
  1048. goto err;
  1049. if (bn2crparam(rsa->q, &kop.crk_param[1]))
  1050. goto err;
  1051. if (bn2crparam(I, &kop.crk_param[2]))
  1052. goto err;
  1053. if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
  1054. goto err;
  1055. if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
  1056. goto err;
  1057. if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
  1058. goto err;
  1059. kop.crk_iparams = 6;
  1060. if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
  1061. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1062. printf("OCF asym process failed, running in Software\n");
  1063. ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
  1064. } else if (ECANCELED == kop.crk_status) {
  1065. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1066. printf("OCF hardware operation cancelled. Running in Software\n");
  1067. ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
  1068. }
  1069. /* else cryptodev operation worked ok ==> ret = 1 */
  1070. err:
  1071. zapparams(&kop);
  1072. return (ret);
  1073. }
  1074. static RSA_METHOD cryptodev_rsa = {
  1075. "cryptodev RSA method",
  1076. NULL, /* rsa_pub_enc */
  1077. NULL, /* rsa_pub_dec */
  1078. NULL, /* rsa_priv_enc */
  1079. NULL, /* rsa_priv_dec */
  1080. NULL,
  1081. NULL,
  1082. NULL, /* init */
  1083. NULL, /* finish */
  1084. 0, /* flags */
  1085. NULL, /* app_data */
  1086. NULL, /* rsa_sign */
  1087. NULL /* rsa_verify */
  1088. };
  1089. static int
  1090. cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
  1091. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  1092. {
  1093. return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  1094. }
  1095. static int
  1096. cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
  1097. BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
  1098. BN_CTX *ctx, BN_MONT_CTX *mont)
  1099. {
  1100. BIGNUM t2;
  1101. int ret = 0;
  1102. BN_init(&t2);
  1103. /* v = ( g^u1 * y^u2 mod p ) mod q */
  1104. /* let t1 = g ^ u1 mod p */
  1105. ret = 0;
  1106. if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
  1107. goto err;
  1108. /* let t2 = y ^ u2 mod p */
  1109. if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
  1110. goto err;
  1111. /* let u1 = t1 * t2 mod p */
  1112. if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
  1113. goto err;
  1114. BN_copy(t1, u1);
  1115. ret = 1;
  1116. err:
  1117. BN_free(&t2);
  1118. return (ret);
  1119. }
  1120. static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  1121. DSA *dsa)
  1122. {
  1123. struct crypt_kop kop;
  1124. BIGNUM *r = NULL, *s = NULL;
  1125. DSA_SIG *dsaret = NULL;
  1126. if ((r = BN_new()) == NULL)
  1127. goto err;
  1128. if ((s = BN_new()) == NULL) {
  1129. BN_free(r);
  1130. goto err;
  1131. }
  1132. memset(&kop, 0, sizeof kop);
  1133. kop.crk_op = CRK_DSA_SIGN;
  1134. /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
  1135. kop.crk_param[0].crp_p = (caddr_t) dgst;
  1136. kop.crk_param[0].crp_nbits = dlen * 8;
  1137. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  1138. goto err;
  1139. if (bn2crparam(dsa->q, &kop.crk_param[2]))
  1140. goto err;
  1141. if (bn2crparam(dsa->g, &kop.crk_param[3]))
  1142. goto err;
  1143. if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
  1144. goto err;
  1145. kop.crk_iparams = 5;
  1146. if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
  1147. BN_num_bytes(dsa->q), s) == 0) {
  1148. dsaret = DSA_SIG_new();
  1149. if (dsaret == NULL)
  1150. goto err;
  1151. dsaret->r = r;
  1152. dsaret->s = s;
  1153. r = s = NULL;
  1154. } else {
  1155. const DSA_METHOD *meth = DSA_OpenSSL();
  1156. dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
  1157. }
  1158. err:
  1159. BN_free(r);
  1160. BN_free(s);
  1161. kop.crk_param[0].crp_p = NULL;
  1162. zapparams(&kop);
  1163. return (dsaret);
  1164. }
  1165. static int
  1166. cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
  1167. DSA_SIG *sig, DSA *dsa)
  1168. {
  1169. struct crypt_kop kop;
  1170. int dsaret = 1;
  1171. memset(&kop, 0, sizeof kop);
  1172. kop.crk_op = CRK_DSA_VERIFY;
  1173. /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
  1174. kop.crk_param[0].crp_p = (caddr_t) dgst;
  1175. kop.crk_param[0].crp_nbits = dlen * 8;
  1176. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  1177. goto err;
  1178. if (bn2crparam(dsa->q, &kop.crk_param[2]))
  1179. goto err;
  1180. if (bn2crparam(dsa->g, &kop.crk_param[3]))
  1181. goto err;
  1182. if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
  1183. goto err;
  1184. if (bn2crparam(sig->r, &kop.crk_param[5]))
  1185. goto err;
  1186. if (bn2crparam(sig->s, &kop.crk_param[6]))
  1187. goto err;
  1188. kop.crk_iparams = 7;
  1189. if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
  1190. /*
  1191. * OCF success value is 0, if not zero, change dsaret to fail
  1192. */
  1193. if (0 != kop.crk_status)
  1194. dsaret = 0;
  1195. } else {
  1196. const DSA_METHOD *meth = DSA_OpenSSL();
  1197. dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
  1198. }
  1199. err:
  1200. kop.crk_param[0].crp_p = NULL;
  1201. zapparams(&kop);
  1202. return (dsaret);
  1203. }
  1204. static DSA_METHOD cryptodev_dsa = {
  1205. "cryptodev DSA method",
  1206. NULL,
  1207. NULL, /* dsa_sign_setup */
  1208. NULL,
  1209. NULL, /* dsa_mod_exp */
  1210. NULL,
  1211. NULL, /* init */
  1212. NULL, /* finish */
  1213. 0, /* flags */
  1214. NULL /* app_data */
  1215. };
  1216. static int
  1217. cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  1218. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  1219. BN_MONT_CTX *m_ctx)
  1220. {
  1221. return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  1222. }
  1223. static int
  1224. cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  1225. {
  1226. struct crypt_kop kop;
  1227. int dhret = 1;
  1228. int fd, keylen;
  1229. if ((fd = get_asym_dev_crypto()) < 0) {
  1230. const DH_METHOD *meth = DH_OpenSSL();
  1231. return ((meth->compute_key) (key, pub_key, dh));
  1232. }
  1233. keylen = BN_num_bits(dh->p);
  1234. memset(&kop, 0, sizeof kop);
  1235. kop.crk_op = CRK_DH_COMPUTE_KEY;
  1236. /* inputs: dh->priv_key pub_key dh->p key */
  1237. if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
  1238. goto err;
  1239. if (bn2crparam(pub_key, &kop.crk_param[1]))
  1240. goto err;
  1241. if (bn2crparam(dh->p, &kop.crk_param[2]))
  1242. goto err;
  1243. kop.crk_iparams = 3;
  1244. kop.crk_param[3].crp_p = (caddr_t) key;
  1245. kop.crk_param[3].crp_nbits = keylen * 8;
  1246. kop.crk_oparams = 1;
  1247. if (ioctl(fd, CIOCKEY, &kop) == -1) {
  1248. const DH_METHOD *meth = DH_OpenSSL();
  1249. dhret = (meth->compute_key) (key, pub_key, dh);
  1250. }
  1251. err:
  1252. kop.crk_param[3].crp_p = NULL;
  1253. zapparams(&kop);
  1254. return (dhret);
  1255. }
  1256. static DH_METHOD cryptodev_dh = {
  1257. "cryptodev DH method",
  1258. NULL, /* cryptodev_dh_generate_key */
  1259. NULL,
  1260. NULL,
  1261. NULL,
  1262. NULL,
  1263. 0, /* flags */
  1264. NULL /* app_data */
  1265. };
  1266. /*
  1267. * ctrl right now is just a wrapper that doesn't do much
  1268. * but I expect we'll want some options soon.
  1269. */
  1270. static int
  1271. cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  1272. {
  1273. # ifdef HAVE_SYSLOG_R
  1274. struct syslog_data sd = SYSLOG_DATA_INIT;
  1275. # endif
  1276. switch (cmd) {
  1277. default:
  1278. # ifdef HAVE_SYSLOG_R
  1279. syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
  1280. # else
  1281. syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
  1282. # endif
  1283. break;
  1284. }
  1285. return (1);
  1286. }
  1287. void ENGINE_load_cryptodev(void)
  1288. {
  1289. ENGINE *engine = ENGINE_new();
  1290. int fd;
  1291. if (engine == NULL)
  1292. return;
  1293. if ((fd = get_dev_crypto()) < 0) {
  1294. ENGINE_free(engine);
  1295. return;
  1296. }
  1297. /*
  1298. * find out what asymmetric crypto algorithms we support
  1299. */
  1300. if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
  1301. put_dev_crypto(fd);
  1302. ENGINE_free(engine);
  1303. return;
  1304. }
  1305. put_dev_crypto(fd);
  1306. if (!ENGINE_set_id(engine, "cryptodev") ||
  1307. !ENGINE_set_name(engine, "BSD cryptodev engine") ||
  1308. !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
  1309. !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
  1310. !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
  1311. !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
  1312. ENGINE_free(engine);
  1313. return;
  1314. }
  1315. if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
  1316. const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
  1317. cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
  1318. cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
  1319. cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
  1320. cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
  1321. cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
  1322. cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
  1323. if (cryptodev_asymfeat & CRF_MOD_EXP) {
  1324. cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
  1325. if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
  1326. cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
  1327. else
  1328. cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
  1329. }
  1330. }
  1331. if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
  1332. const DSA_METHOD *meth = DSA_OpenSSL();
  1333. memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
  1334. if (cryptodev_asymfeat & CRF_DSA_SIGN)
  1335. cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
  1336. if (cryptodev_asymfeat & CRF_MOD_EXP) {
  1337. cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
  1338. cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
  1339. }
  1340. if (cryptodev_asymfeat & CRF_DSA_VERIFY)
  1341. cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
  1342. }
  1343. if (ENGINE_set_DH(engine, &cryptodev_dh)) {
  1344. const DH_METHOD *dh_meth = DH_OpenSSL();
  1345. cryptodev_dh.generate_key = dh_meth->generate_key;
  1346. cryptodev_dh.compute_key = dh_meth->compute_key;
  1347. cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
  1348. if (cryptodev_asymfeat & CRF_MOD_EXP) {
  1349. cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
  1350. if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
  1351. cryptodev_dh.compute_key = cryptodev_dh_compute_key;
  1352. }
  1353. }
  1354. ENGINE_add(engine);
  1355. ENGINE_free(engine);
  1356. ERR_clear_error();
  1357. }
  1358. #endif /* HAVE_CRYPTODEV */