eng_cryptodev.c 36 KB

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