eng_cryptodev.c 39 KB

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