eng_cryptodev.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 <openssl/objects.h>
  29. #include <openssl/engine.h>
  30. #include <openssl/evp.h>
  31. #include <openssl/bn.h>
  32. #include <openssl/dsa.h>
  33. #include <openssl/rsa.h>
  34. #include <openssl/dh.h>
  35. #include <openssl/err.h>
  36. #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
  37. (defined(OpenBSD) || defined(__FreeBSD__))
  38. # include <sys/param.h>
  39. # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
  40. # define HAVE_CRYPTODEV
  41. # endif
  42. # if (OpenBSD >= 200110)
  43. # define HAVE_SYSLOG_R
  44. # endif
  45. #endif
  46. #ifndef HAVE_CRYPTODEV
  47. void ENGINE_load_cryptodev(void)
  48. {
  49. /* This is a NOP on platforms without /dev/crypto */
  50. return;
  51. }
  52. #else
  53. # include <sys/types.h>
  54. # include <crypto/cryptodev.h>
  55. # include <sys/ioctl.h>
  56. # include <errno.h>
  57. # include <stdio.h>
  58. # include <unistd.h>
  59. # include <fcntl.h>
  60. # include <stdarg.h>
  61. # include <syslog.h>
  62. # include <errno.h>
  63. # include <string.h>
  64. struct dev_crypto_state {
  65. struct session_op d_sess;
  66. int d_fd;
  67. };
  68. static u_int32_t cryptodev_asymfeat = 0;
  69. static int get_asym_dev_crypto(void);
  70. static int open_dev_crypto(void);
  71. static int get_dev_crypto(void);
  72. static int cryptodev_max_iv(int cipher);
  73. static int cryptodev_key_length_valid(int cipher, int len);
  74. static int cipher_nid_to_cryptodev(int nid);
  75. static int get_cryptodev_ciphers(const int **cnids);
  76. /*
  77. * static int get_cryptodev_digests(const int **cnids);
  78. */
  79. static int cryptodev_usable_ciphers(const int **nids);
  80. static int cryptodev_usable_digests(const int **nids);
  81. static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  82. const unsigned char *in, unsigned int inl);
  83. static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  84. const unsigned char *iv, int enc);
  85. static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
  86. static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  87. const int **nids, int nid);
  88. static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  89. const int **nids, int nid);
  90. static int bn2crparam(const BIGNUM *a, struct crparam *crp);
  91. static int crparam2bn(struct crparam *crp, BIGNUM *a);
  92. static void zapparams(struct crypt_kop *kop);
  93. static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
  94. int slen, BIGNUM *s);
  95. static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
  96. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  97. BN_MONT_CTX *m_ctx);
  98. static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  99. BN_CTX *ctx);
  100. static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  101. BN_CTX *ctx);
  102. static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
  103. const BIGNUM *p, const BIGNUM *m,
  104. BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  105. static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
  106. BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
  107. BIGNUM *p, BN_CTX *ctx,
  108. BN_MONT_CTX *mont);
  109. static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  110. DSA *dsa);
  111. static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
  112. DSA_SIG *sig, DSA *dsa);
  113. static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  114. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  115. BN_MONT_CTX *m_ctx);
  116. static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
  117. DH *dh);
  118. static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
  119. void ENGINE_load_cryptodev(void);
  120. static const ENGINE_CMD_DEFN cryptodev_defns[] = {
  121. {0, NULL, NULL, 0}
  122. };
  123. static struct {
  124. int id;
  125. int nid;
  126. int ivmax;
  127. int keylen;
  128. } ciphers[] = {
  129. {
  130. CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
  131. },
  132. {
  133. CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
  134. },
  135. {
  136. CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
  137. },
  138. {
  139. CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
  140. },
  141. {
  142. CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
  143. },
  144. {
  145. CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
  146. },
  147. {
  148. 0, NID_undef, 0, 0,
  149. },
  150. };
  151. # if 0
  152. static struct {
  153. int id;
  154. int nid;
  155. } digests[] = {
  156. {
  157. CRYPTO_SHA1_HMAC, NID_hmacWithSHA1,
  158. },
  159. {
  160. CRYPTO_RIPEMD160_HMAC, NID_ripemd160,
  161. },
  162. {
  163. CRYPTO_MD5_KPDK, NID_undef,
  164. },
  165. {
  166. CRYPTO_SHA1_KPDK, NID_undef,
  167. },
  168. {
  169. CRYPTO_MD5, NID_md5,
  170. },
  171. {
  172. CRYPTO_SHA1, NID_undef,
  173. },
  174. {
  175. 0, NID_undef,
  176. },
  177. };
  178. # endif
  179. /*
  180. * Return a fd if /dev/crypto seems usable, 0 otherwise.
  181. */
  182. static int open_dev_crypto(void)
  183. {
  184. static int fd = -1;
  185. if (fd == -1) {
  186. if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
  187. return (-1);
  188. /* close on exec */
  189. if (fcntl(fd, F_SETFD, 1) == -1) {
  190. close(fd);
  191. fd = -1;
  192. return (-1);
  193. }
  194. }
  195. return (fd);
  196. }
  197. static int get_dev_crypto(void)
  198. {
  199. int fd, retfd;
  200. if ((fd = open_dev_crypto()) == -1)
  201. return (-1);
  202. if (ioctl(fd, CRIOGET, &retfd) == -1)
  203. return (-1);
  204. /* close on exec */
  205. if (fcntl(retfd, F_SETFD, 1) == -1) {
  206. close(retfd);
  207. return (-1);
  208. }
  209. return (retfd);
  210. }
  211. /* Caching version for asym operations */
  212. static int get_asym_dev_crypto(void)
  213. {
  214. static int fd = -1;
  215. if (fd == -1)
  216. fd = get_dev_crypto();
  217. return fd;
  218. }
  219. /*
  220. * XXXX this needs to be set for each alg - and determined from
  221. * a running card.
  222. */
  223. static int cryptodev_max_iv(int cipher)
  224. {
  225. int i;
  226. for (i = 0; ciphers[i].id; i++)
  227. if (ciphers[i].id == cipher)
  228. return (ciphers[i].ivmax);
  229. return (0);
  230. }
  231. /*
  232. * XXXX this needs to be set for each alg - and determined from
  233. * a running card. For now, fake it out - but most of these
  234. * for real devices should return 1 for the supported key
  235. * sizes the device can handle.
  236. */
  237. static int cryptodev_key_length_valid(int cipher, int len)
  238. {
  239. int i;
  240. for (i = 0; ciphers[i].id; i++)
  241. if (ciphers[i].id == cipher)
  242. return (ciphers[i].keylen == len);
  243. return (0);
  244. }
  245. /* convert libcrypto nids to cryptodev */
  246. static int cipher_nid_to_cryptodev(int nid)
  247. {
  248. int i;
  249. for (i = 0; ciphers[i].id; i++)
  250. if (ciphers[i].nid == nid)
  251. return (ciphers[i].id);
  252. return (0);
  253. }
  254. /*
  255. * Find out what ciphers /dev/crypto will let us have a session for.
  256. * XXX note, that some of these openssl doesn't deal with yet!
  257. * returning them here is harmless, as long as we return NULL
  258. * when asked for a handler in the cryptodev_engine_ciphers routine
  259. */
  260. static int get_cryptodev_ciphers(const int **cnids)
  261. {
  262. static int nids[CRYPTO_ALGORITHM_MAX];
  263. struct session_op sess;
  264. int fd, i, count = 0;
  265. if ((fd = get_dev_crypto()) < 0) {
  266. *cnids = NULL;
  267. return (0);
  268. }
  269. memset(&sess, 0, sizeof(sess));
  270. sess.key = (caddr_t) "123456781234567812345678";
  271. for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  272. if (ciphers[i].nid == NID_undef)
  273. continue;
  274. sess.cipher = ciphers[i].id;
  275. sess.keylen = ciphers[i].keylen;
  276. sess.mac = 0;
  277. if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  278. ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  279. nids[count++] = ciphers[i].nid;
  280. }
  281. close(fd);
  282. if (count > 0)
  283. *cnids = nids;
  284. else
  285. *cnids = NULL;
  286. return (count);
  287. }
  288. # if 0 /* unused */
  289. /*
  290. * Find out what digests /dev/crypto will let us have a session for.
  291. * XXX note, that some of these openssl doesn't deal with yet!
  292. * returning them here is harmless, as long as we return NULL
  293. * when asked for a handler in the cryptodev_engine_digests routine
  294. */
  295. static int get_cryptodev_digests(const int **cnids)
  296. {
  297. static int nids[CRYPTO_ALGORITHM_MAX];
  298. struct session_op sess;
  299. int fd, i, count = 0;
  300. if ((fd = get_dev_crypto()) < 0) {
  301. *cnids = NULL;
  302. return (0);
  303. }
  304. memset(&sess, 0, sizeof(sess));
  305. for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  306. if (digests[i].nid == NID_undef)
  307. continue;
  308. sess.mac = digests[i].id;
  309. sess.cipher = 0;
  310. if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  311. ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  312. nids[count++] = digests[i].nid;
  313. }
  314. close(fd);
  315. if (count > 0)
  316. *cnids = nids;
  317. else
  318. *cnids = NULL;
  319. return (count);
  320. }
  321. # endif
  322. /*
  323. * Find the useable ciphers|digests from dev/crypto - this is the first
  324. * thing called by the engine init crud which determines what it
  325. * can use for ciphers from this engine. We want to return
  326. * only what we can do, anythine else is handled by software.
  327. *
  328. * If we can't initialize the device to do anything useful for
  329. * any reason, we want to return a NULL array, and 0 length,
  330. * which forces everything to be done is software. By putting
  331. * the initalization of the device in here, we ensure we can
  332. * use this engine as the default, and if for whatever reason
  333. * /dev/crypto won't do what we want it will just be done in
  334. * software
  335. *
  336. * This can (should) be greatly expanded to perhaps take into
  337. * account speed of the device, and what we want to do.
  338. * (although the disabling of particular alg's could be controlled
  339. * by the device driver with sysctl's.) - this is where we
  340. * want most of the decisions made about what we actually want
  341. * to use from /dev/crypto.
  342. */
  343. static int cryptodev_usable_ciphers(const int **nids)
  344. {
  345. return (get_cryptodev_ciphers(nids));
  346. }
  347. static int cryptodev_usable_digests(const int **nids)
  348. {
  349. /*
  350. * XXXX just disable all digests for now, because it sucks.
  351. * we need a better way to decide this - i.e. I may not
  352. * want digests on slow cards like hifn on fast machines,
  353. * but might want them on slow or loaded machines, etc.
  354. * will also want them when using crypto cards that don't
  355. * suck moose gonads - would be nice to be able to decide something
  356. * as reasonable default without having hackery that's card dependent.
  357. * of course, the default should probably be just do everything,
  358. * with perhaps a sysctl to turn algoritms off (or have them off
  359. * by default) on cards that generally suck like the hifn.
  360. */
  361. *nids = NULL;
  362. return (0);
  363. }
  364. static int
  365. cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  366. const unsigned char *in, unsigned int inl)
  367. {
  368. struct crypt_op cryp;
  369. struct dev_crypto_state *state = ctx->cipher_data;
  370. struct session_op *sess = &state->d_sess;
  371. const void *iiv;
  372. unsigned char save_iv[EVP_MAX_IV_LENGTH];
  373. if (state->d_fd < 0)
  374. return (0);
  375. if (!inl)
  376. return (1);
  377. if ((inl % ctx->cipher->block_size) != 0)
  378. return (0);
  379. memset(&cryp, 0, sizeof(cryp));
  380. cryp.ses = sess->ses;
  381. cryp.flags = 0;
  382. cryp.len = inl;
  383. cryp.src = (caddr_t) in;
  384. cryp.dst = (caddr_t) out;
  385. cryp.mac = 0;
  386. cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  387. if (ctx->cipher->iv_len) {
  388. cryp.iv = (caddr_t) ctx->iv;
  389. if (!ctx->encrypt) {
  390. iiv = in + inl - ctx->cipher->iv_len;
  391. memcpy(save_iv, iiv, ctx->cipher->iv_len);
  392. }
  393. } else
  394. cryp.iv = NULL;
  395. if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
  396. /*
  397. * XXX need better errror handling this can fail for a number of
  398. * different reasons.
  399. */
  400. return (0);
  401. }
  402. if (ctx->cipher->iv_len) {
  403. if (ctx->encrypt)
  404. iiv = out + inl - ctx->cipher->iv_len;
  405. else
  406. iiv = save_iv;
  407. memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
  408. }
  409. return (1);
  410. }
  411. static int
  412. cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  413. const unsigned char *iv, int enc)
  414. {
  415. struct dev_crypto_state *state = ctx->cipher_data;
  416. struct session_op *sess = &state->d_sess;
  417. int cipher;
  418. if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
  419. return (0);
  420. if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
  421. return (0);
  422. if (!cryptodev_key_length_valid(cipher, ctx->key_len))
  423. return (0);
  424. memset(sess, 0, sizeof(struct session_op));
  425. if ((state->d_fd = get_dev_crypto()) < 0)
  426. return (0);
  427. sess->key = (char *)key;
  428. sess->keylen = ctx->key_len;
  429. sess->cipher = cipher;
  430. if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
  431. close(state->d_fd);
  432. state->d_fd = -1;
  433. return (0);
  434. }
  435. return (1);
  436. }
  437. /*
  438. * free anything we allocated earlier when initting a
  439. * session, and close the session.
  440. */
  441. static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
  442. {
  443. int ret = 0;
  444. struct dev_crypto_state *state = ctx->cipher_data;
  445. struct session_op *sess = &state->d_sess;
  446. if (state->d_fd < 0)
  447. return (0);
  448. /*
  449. * XXX if this ioctl fails, someting's wrong. the invoker may have called
  450. * us with a bogus ctx, or we could have a device that for whatever
  451. * reason just doesn't want to play ball - it's not clear what's right
  452. * here - should this be an error? should it just increase a counter,
  453. * hmm. For right now, we return 0 - I don't believe that to be "right".
  454. * we could call the gorpy openssl lib error handlers that print messages
  455. * to users of the library. hmm..
  456. */
  457. if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
  458. ret = 0;
  459. } else {
  460. ret = 1;
  461. }
  462. close(state->d_fd);
  463. state->d_fd = -1;
  464. return (ret);
  465. }
  466. /*
  467. * libcrypto EVP stuff - this is how we get wired to EVP so the engine
  468. * gets called when libcrypto requests a cipher NID.
  469. */
  470. /* DES CBC EVP */
  471. const EVP_CIPHER cryptodev_des_cbc = {
  472. NID_des_cbc,
  473. 8, 8, 8,
  474. EVP_CIPH_CBC_MODE,
  475. cryptodev_init_key,
  476. cryptodev_cipher,
  477. cryptodev_cleanup,
  478. sizeof(struct dev_crypto_state),
  479. EVP_CIPHER_set_asn1_iv,
  480. EVP_CIPHER_get_asn1_iv,
  481. NULL
  482. };
  483. /* 3DES CBC EVP */
  484. const EVP_CIPHER cryptodev_3des_cbc = {
  485. NID_des_ede3_cbc,
  486. 8, 24, 8,
  487. EVP_CIPH_CBC_MODE,
  488. cryptodev_init_key,
  489. cryptodev_cipher,
  490. cryptodev_cleanup,
  491. sizeof(struct dev_crypto_state),
  492. EVP_CIPHER_set_asn1_iv,
  493. EVP_CIPHER_get_asn1_iv,
  494. NULL
  495. };
  496. const EVP_CIPHER cryptodev_bf_cbc = {
  497. NID_bf_cbc,
  498. 8, 16, 8,
  499. EVP_CIPH_CBC_MODE,
  500. cryptodev_init_key,
  501. cryptodev_cipher,
  502. cryptodev_cleanup,
  503. sizeof(struct dev_crypto_state),
  504. EVP_CIPHER_set_asn1_iv,
  505. EVP_CIPHER_get_asn1_iv,
  506. NULL
  507. };
  508. const EVP_CIPHER cryptodev_cast_cbc = {
  509. NID_cast5_cbc,
  510. 8, 16, 8,
  511. EVP_CIPH_CBC_MODE,
  512. cryptodev_init_key,
  513. cryptodev_cipher,
  514. cryptodev_cleanup,
  515. sizeof(struct dev_crypto_state),
  516. EVP_CIPHER_set_asn1_iv,
  517. EVP_CIPHER_get_asn1_iv,
  518. NULL
  519. };
  520. const EVP_CIPHER cryptodev_aes_cbc = {
  521. NID_aes_128_cbc,
  522. 16, 16, 16,
  523. EVP_CIPH_CBC_MODE,
  524. cryptodev_init_key,
  525. cryptodev_cipher,
  526. cryptodev_cleanup,
  527. sizeof(struct dev_crypto_state),
  528. EVP_CIPHER_set_asn1_iv,
  529. EVP_CIPHER_get_asn1_iv,
  530. NULL
  531. };
  532. /*
  533. * Registered by the ENGINE when used to find out how to deal with
  534. * a particular NID in the ENGINE. this says what we'll do at the
  535. * top level - note, that list is restricted by what we answer with
  536. */
  537. static int
  538. cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  539. const int **nids, int nid)
  540. {
  541. if (!cipher)
  542. return (cryptodev_usable_ciphers(nids));
  543. switch (nid) {
  544. case NID_des_ede3_cbc:
  545. *cipher = &cryptodev_3des_cbc;
  546. break;
  547. case NID_des_cbc:
  548. *cipher = &cryptodev_des_cbc;
  549. break;
  550. case NID_bf_cbc:
  551. *cipher = &cryptodev_bf_cbc;
  552. break;
  553. case NID_cast5_cbc:
  554. *cipher = &cryptodev_cast_cbc;
  555. break;
  556. case NID_aes_128_cbc:
  557. *cipher = &cryptodev_aes_cbc;
  558. break;
  559. default:
  560. *cipher = NULL;
  561. break;
  562. }
  563. return (*cipher != NULL);
  564. }
  565. static int
  566. cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  567. const int **nids, int nid)
  568. {
  569. if (!digest)
  570. return (cryptodev_usable_digests(nids));
  571. switch (nid) {
  572. case NID_md5:
  573. *digest = NULL; /* need to make a clean md5 critter */
  574. break;
  575. default:
  576. *digest = NULL;
  577. break;
  578. }
  579. return (*digest != NULL);
  580. }
  581. /*
  582. * Convert a BIGNUM to the representation that /dev/crypto needs.
  583. * Upon completion of use, the caller is responsible for freeing
  584. * crp->crp_p.
  585. */
  586. static int bn2crparam(const BIGNUM *a, struct crparam *crp)
  587. {
  588. int i, j, k;
  589. ssize_t bytes, bits;
  590. u_char *b;
  591. crp->crp_p = NULL;
  592. crp->crp_nbits = 0;
  593. bits = BN_num_bits(a);
  594. bytes = (bits + 7) / 8;
  595. b = malloc(bytes);
  596. if (b == NULL)
  597. return (1);
  598. crp->crp_p = (char *)b;
  599. crp->crp_nbits = bits;
  600. for (i = 0, j = 0; i < a->top; i++) {
  601. for (k = 0; k < BN_BITS2 / 8; k++) {
  602. if ((j + k) >= bytes)
  603. return (0);
  604. b[j + k] = a->d[i] >> (k * 8);
  605. }
  606. j += BN_BITS2 / 8;
  607. }
  608. return (0);
  609. }
  610. /* Convert a /dev/crypto parameter to a BIGNUM */
  611. static int crparam2bn(struct crparam *crp, BIGNUM *a)
  612. {
  613. u_int8_t *pd;
  614. int i, bytes;
  615. bytes = (crp->crp_nbits + 7) / 8;
  616. if (bytes == 0)
  617. return (-1);
  618. if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
  619. return (-1);
  620. for (i = 0; i < bytes; i++)
  621. pd[i] = crp->crp_p[bytes - i - 1];
  622. BN_bin2bn(pd, bytes, a);
  623. free(pd);
  624. return (0);
  625. }
  626. static void zapparams(struct crypt_kop *kop)
  627. {
  628. int i;
  629. for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
  630. if (kop->crk_param[i].crp_p)
  631. free(kop->crk_param[i].crp_p);
  632. kop->crk_param[i].crp_p = NULL;
  633. kop->crk_param[i].crp_nbits = 0;
  634. }
  635. }
  636. static int
  637. cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
  638. BIGNUM *s)
  639. {
  640. int fd, ret = -1;
  641. if ((fd = get_asym_dev_crypto()) < 0)
  642. return (ret);
  643. if (r) {
  644. kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
  645. kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
  646. kop->crk_oparams++;
  647. }
  648. if (s) {
  649. kop->crk_param[kop->crk_iparams + 1].crp_p =
  650. calloc(slen, sizeof(char));
  651. kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
  652. kop->crk_oparams++;
  653. }
  654. if (ioctl(fd, CIOCKEY, kop) == 0) {
  655. if (r)
  656. crparam2bn(&kop->crk_param[kop->crk_iparams], r);
  657. if (s)
  658. crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
  659. ret = 0;
  660. }
  661. return (ret);
  662. }
  663. static int
  664. cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  665. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  666. {
  667. struct crypt_kop kop;
  668. int ret = 1;
  669. /*
  670. * Currently, we know we can do mod exp iff we can do any asymmetric
  671. * operations at all.
  672. */
  673. if (cryptodev_asymfeat == 0) {
  674. ret = BN_mod_exp(r, a, p, m, ctx);
  675. return (ret);
  676. }
  677. memset(&kop, 0, sizeof kop);
  678. kop.crk_op = CRK_MOD_EXP;
  679. /* inputs: a^p % m */
  680. if (bn2crparam(a, &kop.crk_param[0]))
  681. goto err;
  682. if (bn2crparam(p, &kop.crk_param[1]))
  683. goto err;
  684. if (bn2crparam(m, &kop.crk_param[2]))
  685. goto err;
  686. kop.crk_iparams = 3;
  687. if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
  688. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  689. printf("OCF asym process failed, Running in software\n");
  690. ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  691. } else if (ECANCELED == kop.crk_status) {
  692. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  693. printf("OCF hardware operation cancelled. Running in Software\n");
  694. ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  695. }
  696. /* else cryptodev operation worked ok ==> ret = 1 */
  697. err:
  698. zapparams(&kop);
  699. return (ret);
  700. }
  701. static int
  702. cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  703. BN_CTX *ctx)
  704. {
  705. int r;
  706. r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
  707. return (r);
  708. }
  709. static int
  710. cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  711. {
  712. struct crypt_kop kop;
  713. int ret = 1;
  714. if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  715. /* XXX 0 means failure?? */
  716. return (0);
  717. }
  718. memset(&kop, 0, sizeof kop);
  719. kop.crk_op = CRK_MOD_EXP_CRT;
  720. /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
  721. if (bn2crparam(rsa->p, &kop.crk_param[0]))
  722. goto err;
  723. if (bn2crparam(rsa->q, &kop.crk_param[1]))
  724. goto err;
  725. if (bn2crparam(I, &kop.crk_param[2]))
  726. goto err;
  727. if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
  728. goto err;
  729. if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
  730. goto err;
  731. if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
  732. goto err;
  733. kop.crk_iparams = 6;
  734. if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
  735. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  736. printf("OCF asym process failed, running in Software\n");
  737. ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
  738. } else if (ECANCELED == kop.crk_status) {
  739. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  740. printf("OCF hardware operation cancelled. Running in Software\n");
  741. ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
  742. }
  743. /* else cryptodev operation worked ok ==> ret = 1 */
  744. err:
  745. zapparams(&kop);
  746. return (ret);
  747. }
  748. static RSA_METHOD cryptodev_rsa = {
  749. "cryptodev RSA method",
  750. NULL, /* rsa_pub_enc */
  751. NULL, /* rsa_pub_dec */
  752. NULL, /* rsa_priv_enc */
  753. NULL, /* rsa_priv_dec */
  754. NULL,
  755. NULL,
  756. NULL, /* init */
  757. NULL, /* finish */
  758. 0, /* flags */
  759. NULL, /* app_data */
  760. NULL, /* rsa_sign */
  761. NULL /* rsa_verify */
  762. };
  763. static int
  764. cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
  765. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  766. {
  767. return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  768. }
  769. static int
  770. cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
  771. BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
  772. BN_CTX *ctx, BN_MONT_CTX *mont)
  773. {
  774. BIGNUM t2;
  775. int ret = 0;
  776. BN_init(&t2);
  777. /* v = ( g^u1 * y^u2 mod p ) mod q */
  778. /* let t1 = g ^ u1 mod p */
  779. ret = 0;
  780. if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
  781. goto err;
  782. /* let t2 = y ^ u2 mod p */
  783. if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
  784. goto err;
  785. /* let u1 = t1 * t2 mod p */
  786. if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
  787. goto err;
  788. BN_copy(t1, u1);
  789. ret = 1;
  790. err:
  791. BN_free(&t2);
  792. return (ret);
  793. }
  794. static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  795. DSA *dsa)
  796. {
  797. struct crypt_kop kop;
  798. BIGNUM *r = NULL, *s = NULL;
  799. DSA_SIG *dsaret = NULL;
  800. if ((r = BN_new()) == NULL)
  801. goto err;
  802. if ((s = BN_new()) == NULL) {
  803. BN_free(r);
  804. goto err;
  805. }
  806. memset(&kop, 0, sizeof kop);
  807. kop.crk_op = CRK_DSA_SIGN;
  808. /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
  809. kop.crk_param[0].crp_p = (caddr_t) dgst;
  810. kop.crk_param[0].crp_nbits = dlen * 8;
  811. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  812. goto err;
  813. if (bn2crparam(dsa->q, &kop.crk_param[2]))
  814. goto err;
  815. if (bn2crparam(dsa->g, &kop.crk_param[3]))
  816. goto err;
  817. if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
  818. goto err;
  819. kop.crk_iparams = 5;
  820. if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
  821. BN_num_bytes(dsa->q), s) == 0) {
  822. dsaret = DSA_SIG_new();
  823. dsaret->r = r;
  824. dsaret->s = s;
  825. } else {
  826. const DSA_METHOD *meth = DSA_OpenSSL();
  827. BN_free(r);
  828. BN_free(s);
  829. dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
  830. }
  831. err:
  832. kop.crk_param[0].crp_p = NULL;
  833. zapparams(&kop);
  834. return (dsaret);
  835. }
  836. static int
  837. cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
  838. DSA_SIG *sig, DSA *dsa)
  839. {
  840. struct crypt_kop kop;
  841. int dsaret = 1;
  842. memset(&kop, 0, sizeof kop);
  843. kop.crk_op = CRK_DSA_VERIFY;
  844. /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
  845. kop.crk_param[0].crp_p = (caddr_t) dgst;
  846. kop.crk_param[0].crp_nbits = dlen * 8;
  847. if (bn2crparam(dsa->p, &kop.crk_param[1]))
  848. goto err;
  849. if (bn2crparam(dsa->q, &kop.crk_param[2]))
  850. goto err;
  851. if (bn2crparam(dsa->g, &kop.crk_param[3]))
  852. goto err;
  853. if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
  854. goto err;
  855. if (bn2crparam(sig->r, &kop.crk_param[5]))
  856. goto err;
  857. if (bn2crparam(sig->s, &kop.crk_param[6]))
  858. goto err;
  859. kop.crk_iparams = 7;
  860. if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
  861. /*
  862. * OCF success value is 0, if not zero, change dsaret to fail
  863. */
  864. if (0 != kop.crk_status)
  865. dsaret = 0;
  866. } else {
  867. const DSA_METHOD *meth = DSA_OpenSSL();
  868. dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
  869. }
  870. err:
  871. kop.crk_param[0].crp_p = NULL;
  872. zapparams(&kop);
  873. return (dsaret);
  874. }
  875. static DSA_METHOD cryptodev_dsa = {
  876. "cryptodev DSA method",
  877. NULL,
  878. NULL, /* dsa_sign_setup */
  879. NULL,
  880. NULL, /* dsa_mod_exp */
  881. NULL,
  882. NULL, /* init */
  883. NULL, /* finish */
  884. 0, /* flags */
  885. NULL /* app_data */
  886. };
  887. static int
  888. cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  889. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  890. BN_MONT_CTX *m_ctx)
  891. {
  892. return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  893. }
  894. static int
  895. cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  896. {
  897. struct crypt_kop kop;
  898. int dhret = 1;
  899. int fd, keylen;
  900. if ((fd = get_asym_dev_crypto()) < 0) {
  901. const DH_METHOD *meth = DH_OpenSSL();
  902. return ((meth->compute_key) (key, pub_key, dh));
  903. }
  904. keylen = BN_num_bits(dh->p);
  905. memset(&kop, 0, sizeof kop);
  906. kop.crk_op = CRK_DH_COMPUTE_KEY;
  907. /* inputs: dh->priv_key pub_key dh->p key */
  908. if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
  909. goto err;
  910. if (bn2crparam(pub_key, &kop.crk_param[1]))
  911. goto err;
  912. if (bn2crparam(dh->p, &kop.crk_param[2]))
  913. goto err;
  914. kop.crk_iparams = 3;
  915. kop.crk_param[3].crp_p = (char *)key;
  916. kop.crk_param[3].crp_nbits = keylen * 8;
  917. kop.crk_oparams = 1;
  918. if (ioctl(fd, CIOCKEY, &kop) == -1) {
  919. const DH_METHOD *meth = DH_OpenSSL();
  920. dhret = (meth->compute_key) (key, pub_key, dh);
  921. }
  922. err:
  923. kop.crk_param[3].crp_p = NULL;
  924. zapparams(&kop);
  925. return (dhret);
  926. }
  927. static DH_METHOD cryptodev_dh = {
  928. "cryptodev DH method",
  929. NULL, /* cryptodev_dh_generate_key */
  930. NULL,
  931. NULL,
  932. NULL,
  933. NULL,
  934. 0, /* flags */
  935. NULL /* app_data */
  936. };
  937. /*
  938. * ctrl right now is just a wrapper that doesn't do much
  939. * but I expect we'll want some options soon.
  940. */
  941. static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
  942. {
  943. # ifdef HAVE_SYSLOG_R
  944. struct syslog_data sd = SYSLOG_DATA_INIT;
  945. # endif
  946. switch (cmd) {
  947. default:
  948. # ifdef HAVE_SYSLOG_R
  949. syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
  950. # else
  951. syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
  952. # endif
  953. break;
  954. }
  955. return (1);
  956. }
  957. void ENGINE_load_cryptodev(void)
  958. {
  959. ENGINE *engine = ENGINE_new();
  960. int fd;
  961. if (engine == NULL)
  962. return;
  963. if ((fd = get_dev_crypto()) < 0) {
  964. ENGINE_free(engine);
  965. return;
  966. }
  967. /*
  968. * find out what asymmetric crypto algorithms we support
  969. */
  970. if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
  971. close(fd);
  972. ENGINE_free(engine);
  973. return;
  974. }
  975. close(fd);
  976. if (!ENGINE_set_id(engine, "cryptodev") ||
  977. !ENGINE_set_name(engine, "BSD cryptodev engine") ||
  978. !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
  979. !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
  980. !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
  981. !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
  982. ENGINE_free(engine);
  983. return;
  984. }
  985. if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
  986. const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
  987. cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
  988. cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
  989. cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
  990. cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
  991. cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
  992. cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
  993. if (cryptodev_asymfeat & CRF_MOD_EXP) {
  994. cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
  995. if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
  996. cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
  997. else
  998. cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
  999. }
  1000. }
  1001. if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
  1002. const DSA_METHOD *meth = DSA_OpenSSL();
  1003. memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
  1004. if (cryptodev_asymfeat & CRF_DSA_SIGN)
  1005. cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
  1006. if (cryptodev_asymfeat & CRF_MOD_EXP) {
  1007. cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
  1008. cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
  1009. }
  1010. if (cryptodev_asymfeat & CRF_DSA_VERIFY)
  1011. cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
  1012. }
  1013. if (ENGINE_set_DH(engine, &cryptodev_dh)) {
  1014. const DH_METHOD *dh_meth = DH_OpenSSL();
  1015. cryptodev_dh.generate_key = dh_meth->generate_key;
  1016. cryptodev_dh.compute_key = dh_meth->compute_key;
  1017. cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
  1018. if (cryptodev_asymfeat & CRF_MOD_EXP) {
  1019. cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
  1020. if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
  1021. cryptodev_dh.compute_key = cryptodev_dh_compute_key;
  1022. }
  1023. }
  1024. ENGINE_add(engine);
  1025. ENGINE_free(engine);
  1026. ERR_clear_error();
  1027. }
  1028. #endif /* HAVE_CRYPTODEV */