eng_cryptodev.c 41 KB

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