eng_devcrypto.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "e_os.h"
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <sys/ioctl.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <openssl/conf.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/err.h>
  20. #include <openssl/engine.h>
  21. #include <openssl/objects.h>
  22. #include <crypto/cryptodev.h>
  23. #include "internal/engine.h"
  24. #ifdef CRYPTO_ALGORITHM_MIN
  25. # define CHECK_BSD_STYLE_MACROS
  26. #endif
  27. /*
  28. * ONE global file descriptor for all sessions. This allows operations
  29. * such as digest session data copying (see digest_copy()), but is also
  30. * saner... why re-open /dev/crypto for every session?
  31. */
  32. static int cfd;
  33. #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
  34. #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
  35. #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
  36. #define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
  37. static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
  38. /*
  39. * cipher/digest status & acceleration definitions
  40. * Make sure the defaults are set to 0
  41. */
  42. struct driver_info_st {
  43. enum devcrypto_status_t {
  44. DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
  45. DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
  46. DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
  47. DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
  48. DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
  49. } status;
  50. enum devcrypto_accelerated_t {
  51. DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
  52. DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
  53. DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
  54. } accelerated;
  55. char *driver_name;
  56. };
  57. /******************************************************************************
  58. *
  59. * Ciphers
  60. *
  61. * Because they all do the same basic operation, we have only one set of
  62. * method functions for them all to share, and a mapping table between
  63. * NIDs and cryptodev IDs, with all the necessary size data.
  64. *
  65. *****/
  66. struct cipher_ctx {
  67. struct session_op sess;
  68. int op; /* COP_ENCRYPT or COP_DECRYPT */
  69. unsigned long mode; /* EVP_CIPH_*_MODE */
  70. /* to handle ctr mode being a stream cipher */
  71. unsigned char partial[EVP_MAX_BLOCK_LENGTH];
  72. unsigned int blocksize, num;
  73. };
  74. static const struct cipher_data_st {
  75. int nid;
  76. int blocksize;
  77. int keylen;
  78. int ivlen;
  79. int flags;
  80. int devcryptoid;
  81. } cipher_data[] = {
  82. #ifndef OPENSSL_NO_DES
  83. { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
  84. { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
  85. #endif
  86. #ifndef OPENSSL_NO_BF
  87. { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
  88. #endif
  89. #ifndef OPENSSL_NO_CAST
  90. { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
  91. #endif
  92. { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  93. { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  94. { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  95. #ifndef OPENSSL_NO_RC4
  96. { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
  97. #endif
  98. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
  99. { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  100. { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  101. { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  102. #endif
  103. #if 0 /* Not yet supported */
  104. { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  105. { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  106. #endif
  107. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
  108. { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  109. { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  110. { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  111. #endif
  112. #if 0 /* Not yet supported */
  113. { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  114. { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  115. { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  116. #endif
  117. #ifndef OPENSSL_NO_CAMELLIA
  118. { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
  119. CRYPTO_CAMELLIA_CBC },
  120. { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
  121. CRYPTO_CAMELLIA_CBC },
  122. { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
  123. CRYPTO_CAMELLIA_CBC },
  124. #endif
  125. };
  126. static size_t find_cipher_data_index(int nid)
  127. {
  128. size_t i;
  129. for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  130. if (nid == cipher_data[i].nid)
  131. return i;
  132. return (size_t)-1;
  133. }
  134. static size_t get_cipher_data_index(int nid)
  135. {
  136. size_t i = find_cipher_data_index(nid);
  137. if (i != (size_t)-1)
  138. return i;
  139. /*
  140. * Code further down must make sure that only NIDs in the table above
  141. * are used. If any other NID reaches this function, there's a grave
  142. * coding error further down.
  143. */
  144. assert("Code that never should be reached" == NULL);
  145. return -1;
  146. }
  147. static const struct cipher_data_st *get_cipher_data(int nid)
  148. {
  149. return &cipher_data[get_cipher_data_index(nid)];
  150. }
  151. /*
  152. * Following are the three necessary functions to map OpenSSL functionality
  153. * with cryptodev.
  154. */
  155. static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  156. const unsigned char *iv, int enc)
  157. {
  158. struct cipher_ctx *cipher_ctx =
  159. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  160. const struct cipher_data_st *cipher_d =
  161. get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
  162. memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
  163. cipher_ctx->sess.cipher = cipher_d->devcryptoid;
  164. cipher_ctx->sess.keylen = cipher_d->keylen;
  165. cipher_ctx->sess.key = (void *)key;
  166. cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
  167. cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
  168. cipher_ctx->blocksize = cipher_d->blocksize;
  169. if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
  170. SYSerr(SYS_F_IOCTL, errno);
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  176. const unsigned char *in, size_t inl)
  177. {
  178. struct cipher_ctx *cipher_ctx =
  179. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  180. struct crypt_op cryp;
  181. unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
  182. #if !defined(COP_FLAG_WRITE_IV)
  183. unsigned char saved_iv[EVP_MAX_IV_LENGTH];
  184. const unsigned char *ivptr;
  185. size_t nblocks, ivlen;
  186. #endif
  187. memset(&cryp, 0, sizeof(cryp));
  188. cryp.ses = cipher_ctx->sess.ses;
  189. cryp.len = inl;
  190. cryp.src = (void *)in;
  191. cryp.dst = (void *)out;
  192. cryp.iv = (void *)iv;
  193. cryp.op = cipher_ctx->op;
  194. #if !defined(COP_FLAG_WRITE_IV)
  195. cryp.flags = 0;
  196. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  197. if (ivlen > 0)
  198. switch (cipher_ctx->mode) {
  199. case EVP_CIPH_CBC_MODE:
  200. assert(inl >= ivlen);
  201. if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  202. ivptr = in + inl - ivlen;
  203. memcpy(saved_iv, ivptr, ivlen);
  204. }
  205. break;
  206. case EVP_CIPH_CTR_MODE:
  207. break;
  208. default: /* should not happen */
  209. return 0;
  210. }
  211. #else
  212. cryp.flags = COP_FLAG_WRITE_IV;
  213. #endif
  214. if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
  215. SYSerr(SYS_F_IOCTL, errno);
  216. return 0;
  217. }
  218. #if !defined(COP_FLAG_WRITE_IV)
  219. if (ivlen > 0)
  220. switch (cipher_ctx->mode) {
  221. case EVP_CIPH_CBC_MODE:
  222. assert(inl >= ivlen);
  223. if (EVP_CIPHER_CTX_encrypting(ctx))
  224. ivptr = out + inl - ivlen;
  225. else
  226. ivptr = saved_iv;
  227. memcpy(iv, ivptr, ivlen);
  228. break;
  229. case EVP_CIPH_CTR_MODE:
  230. nblocks = (inl + cipher_ctx->blocksize - 1)
  231. / cipher_ctx->blocksize;
  232. do {
  233. ivlen--;
  234. nblocks += iv[ivlen];
  235. iv[ivlen] = (uint8_t) nblocks;
  236. nblocks >>= 8;
  237. } while (ivlen);
  238. break;
  239. default: /* should not happen */
  240. return 0;
  241. }
  242. #endif
  243. return 1;
  244. }
  245. static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  246. const unsigned char *in, size_t inl)
  247. {
  248. struct cipher_ctx *cipher_ctx =
  249. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  250. size_t nblocks, len;
  251. /* initial partial block */
  252. while (cipher_ctx->num && inl) {
  253. (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
  254. --inl;
  255. cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
  256. }
  257. /* full blocks */
  258. if (inl > (unsigned int) cipher_ctx->blocksize) {
  259. nblocks = inl/cipher_ctx->blocksize;
  260. len = nblocks * cipher_ctx->blocksize;
  261. if (cipher_do_cipher(ctx, out, in, len) < 1)
  262. return 0;
  263. inl -= len;
  264. out += len;
  265. in += len;
  266. }
  267. /* final partial block */
  268. if (inl) {
  269. memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
  270. if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
  271. cipher_ctx->blocksize) < 1)
  272. return 0;
  273. while (inl--) {
  274. out[cipher_ctx->num] = in[cipher_ctx->num]
  275. ^ cipher_ctx->partial[cipher_ctx->num];
  276. cipher_ctx->num++;
  277. }
  278. }
  279. return 1;
  280. }
  281. static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
  282. {
  283. EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
  284. struct cipher_ctx *cipher_ctx;
  285. if (type == EVP_CTRL_COPY) {
  286. /* when copying the context, a new session needs to be initialized */
  287. cipher_ctx = (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  288. return (cipher_ctx == NULL)
  289. || cipher_init(to_ctx, cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
  290. (cipher_ctx->op == COP_ENCRYPT));
  291. }
  292. return -1;
  293. }
  294. static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
  295. {
  296. struct cipher_ctx *cipher_ctx =
  297. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  298. if (ioctl(cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
  299. SYSerr(SYS_F_IOCTL, errno);
  300. return 0;
  301. }
  302. return 1;
  303. }
  304. /*
  305. * Keep tables of known nids, associated methods, selected ciphers, and driver
  306. * info.
  307. * Note that known_cipher_nids[] isn't necessarily indexed the same way as
  308. * cipher_data[] above, which the other tables are.
  309. */
  310. static int known_cipher_nids[OSSL_NELEM(cipher_data)];
  311. static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
  312. static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
  313. static int selected_ciphers[OSSL_NELEM(cipher_data)];
  314. static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
  315. static int devcrypto_test_cipher(size_t cipher_data_index)
  316. {
  317. return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
  318. && selected_ciphers[cipher_data_index] == 1
  319. && (cipher_driver_info[cipher_data_index].accelerated
  320. == DEVCRYPTO_ACCELERATED
  321. || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
  322. || (cipher_driver_info[cipher_data_index].accelerated
  323. != DEVCRYPTO_NOT_ACCELERATED
  324. && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
  325. }
  326. static void prepare_cipher_methods(void)
  327. {
  328. size_t i;
  329. struct session_op sess;
  330. unsigned long cipher_mode;
  331. #ifdef CIOCGSESSINFO
  332. struct session_info_op siop;
  333. #endif
  334. memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
  335. memset(&sess, 0, sizeof(sess));
  336. sess.key = (void *)"01234567890123456789012345678901234567890123456789";
  337. for (i = 0, known_cipher_nids_amount = 0;
  338. i < OSSL_NELEM(cipher_data); i++) {
  339. selected_ciphers[i] = 1;
  340. /*
  341. * Check that the cipher is usable
  342. */
  343. sess.cipher = cipher_data[i].devcryptoid;
  344. sess.keylen = cipher_data[i].keylen;
  345. if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
  346. cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
  347. continue;
  348. }
  349. cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
  350. if ((known_cipher_methods[i] =
  351. EVP_CIPHER_meth_new(cipher_data[i].nid,
  352. cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
  353. cipher_data[i].blocksize,
  354. cipher_data[i].keylen)) == NULL
  355. || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
  356. cipher_data[i].ivlen)
  357. || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
  358. cipher_data[i].flags
  359. | EVP_CIPH_CUSTOM_COPY
  360. | EVP_CIPH_FLAG_DEFAULT_ASN1)
  361. || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
  362. || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
  363. cipher_mode == EVP_CIPH_CTR_MODE ?
  364. ctr_do_cipher :
  365. cipher_do_cipher)
  366. || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
  367. || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
  368. cipher_cleanup)
  369. || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
  370. sizeof(struct cipher_ctx))) {
  371. cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  372. EVP_CIPHER_meth_free(known_cipher_methods[i]);
  373. known_cipher_methods[i] = NULL;
  374. } else {
  375. cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
  376. #ifdef CIOCGSESSINFO
  377. siop.ses = sess.ses;
  378. if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
  379. cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
  380. } else {
  381. cipher_driver_info[i].driver_name =
  382. OPENSSL_strndup(siop.cipher_info.cra_driver_name,
  383. CRYPTODEV_MAX_ALG_NAME);
  384. if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
  385. cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
  386. else
  387. cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
  388. }
  389. #endif /* CIOCGSESSINFO */
  390. }
  391. ioctl(cfd, CIOCFSESSION, &sess.ses);
  392. if (devcrypto_test_cipher(i)) {
  393. known_cipher_nids[known_cipher_nids_amount++] =
  394. cipher_data[i].nid;
  395. }
  396. }
  397. }
  398. static void rebuild_known_cipher_nids(ENGINE *e)
  399. {
  400. size_t i;
  401. for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
  402. if (devcrypto_test_cipher(i))
  403. known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
  404. }
  405. ENGINE_unregister_ciphers(e);
  406. ENGINE_register_ciphers(e);
  407. }
  408. static const EVP_CIPHER *get_cipher_method(int nid)
  409. {
  410. size_t i = get_cipher_data_index(nid);
  411. if (i == (size_t)-1)
  412. return NULL;
  413. return known_cipher_methods[i];
  414. }
  415. static int get_cipher_nids(const int **nids)
  416. {
  417. *nids = known_cipher_nids;
  418. return known_cipher_nids_amount;
  419. }
  420. static void destroy_cipher_method(int nid)
  421. {
  422. size_t i = get_cipher_data_index(nid);
  423. EVP_CIPHER_meth_free(known_cipher_methods[i]);
  424. known_cipher_methods[i] = NULL;
  425. }
  426. static void destroy_all_cipher_methods(void)
  427. {
  428. size_t i;
  429. for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
  430. destroy_cipher_method(cipher_data[i].nid);
  431. OPENSSL_free(cipher_driver_info[i].driver_name);
  432. cipher_driver_info[i].driver_name = NULL;
  433. }
  434. }
  435. static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  436. const int **nids, int nid)
  437. {
  438. if (cipher == NULL)
  439. return get_cipher_nids(nids);
  440. *cipher = get_cipher_method(nid);
  441. return *cipher != NULL;
  442. }
  443. static void devcrypto_select_all_ciphers(int *cipher_list)
  444. {
  445. size_t i;
  446. for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  447. cipher_list[i] = 1;
  448. }
  449. static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
  450. {
  451. int *cipher_list = (int *)usr;
  452. char *name;
  453. const EVP_CIPHER *EVP;
  454. size_t i;
  455. if (len == 0)
  456. return 1;
  457. if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
  458. return 0;
  459. EVP = EVP_get_cipherbyname(name);
  460. if (EVP == NULL)
  461. fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
  462. else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
  463. cipher_list[i] = 1;
  464. else
  465. fprintf(stderr, "devcrypto: cipher %s not available\n", name);
  466. OPENSSL_free(name);
  467. return 1;
  468. }
  469. static void dump_cipher_info(void)
  470. {
  471. size_t i;
  472. const char *name;
  473. fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
  474. " engine:\n");
  475. #ifndef CIOCGSESSINFO
  476. fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
  477. #endif
  478. for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
  479. name = OBJ_nid2sn(cipher_data[i].nid);
  480. fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
  481. name ? name : "unknown", cipher_data[i].nid,
  482. cipher_data[i].devcryptoid);
  483. if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
  484. fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
  485. continue;
  486. }
  487. fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
  488. cipher_driver_info[i].driver_name : "unknown");
  489. if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
  490. fprintf(stderr, "(hw accelerated)");
  491. else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
  492. fprintf(stderr, "(software)");
  493. else
  494. fprintf(stderr, "(acceleration status unknown)");
  495. if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
  496. fprintf (stderr, ". Cipher setup failed");
  497. fprintf(stderr, "\n");
  498. }
  499. fprintf(stderr, "\n");
  500. }
  501. /*
  502. * We only support digests if the cryptodev implementation supports multiple
  503. * data updates and session copying. Otherwise, we would be forced to maintain
  504. * a cache, which is perilous if there's a lot of data coming in (if someone
  505. * wants to checksum an OpenSSL tarball, for example).
  506. */
  507. #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  508. #define IMPLEMENT_DIGEST
  509. /******************************************************************************
  510. *
  511. * Digests
  512. *
  513. * Because they all do the same basic operation, we have only one set of
  514. * method functions for them all to share, and a mapping table between
  515. * NIDs and cryptodev IDs, with all the necessary size data.
  516. *
  517. *****/
  518. struct digest_ctx {
  519. struct session_op sess;
  520. /* This signals that the init function was called, not that it succeeded. */
  521. int init_called;
  522. unsigned char digest_res[HASH_MAX_LEN];
  523. };
  524. static const struct digest_data_st {
  525. int nid;
  526. int digestlen;
  527. int devcryptoid;
  528. } digest_data[] = {
  529. #ifndef OPENSSL_NO_MD5
  530. { NID_md5, 16, CRYPTO_MD5 },
  531. #endif
  532. { NID_sha1, 20, CRYPTO_SHA1 },
  533. #ifndef OPENSSL_NO_RMD160
  534. # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
  535. { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
  536. # endif
  537. #endif
  538. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
  539. { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
  540. #endif
  541. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
  542. { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
  543. #endif
  544. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
  545. { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
  546. #endif
  547. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
  548. { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
  549. #endif
  550. };
  551. static size_t find_digest_data_index(int nid)
  552. {
  553. size_t i;
  554. for (i = 0; i < OSSL_NELEM(digest_data); i++)
  555. if (nid == digest_data[i].nid)
  556. return i;
  557. return (size_t)-1;
  558. }
  559. static size_t get_digest_data_index(int nid)
  560. {
  561. size_t i = find_digest_data_index(nid);
  562. if (i != (size_t)-1)
  563. return i;
  564. /*
  565. * Code further down must make sure that only NIDs in the table above
  566. * are used. If any other NID reaches this function, there's a grave
  567. * coding error further down.
  568. */
  569. assert("Code that never should be reached" == NULL);
  570. return -1;
  571. }
  572. static const struct digest_data_st *get_digest_data(int nid)
  573. {
  574. return &digest_data[get_digest_data_index(nid)];
  575. }
  576. /*
  577. * Following are the five necessary functions to map OpenSSL functionality
  578. * with cryptodev: init, update, final, cleanup, and copy.
  579. */
  580. static int digest_init(EVP_MD_CTX *ctx)
  581. {
  582. struct digest_ctx *digest_ctx =
  583. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  584. const struct digest_data_st *digest_d =
  585. get_digest_data(EVP_MD_CTX_type(ctx));
  586. digest_ctx->init_called = 1;
  587. memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
  588. digest_ctx->sess.mac = digest_d->devcryptoid;
  589. if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
  590. SYSerr(SYS_F_IOCTL, errno);
  591. return 0;
  592. }
  593. return 1;
  594. }
  595. static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
  596. void *res, unsigned int flags)
  597. {
  598. struct crypt_op cryp;
  599. memset(&cryp, 0, sizeof(cryp));
  600. cryp.ses = ctx->sess.ses;
  601. cryp.len = srclen;
  602. cryp.src = (void *)src;
  603. cryp.dst = NULL;
  604. cryp.mac = res;
  605. cryp.flags = flags;
  606. return ioctl(cfd, CIOCCRYPT, &cryp);
  607. }
  608. static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  609. {
  610. struct digest_ctx *digest_ctx =
  611. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  612. if (count == 0)
  613. return 1;
  614. if (digest_ctx == NULL)
  615. return 0;
  616. if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  617. if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
  618. return 1;
  619. } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
  620. return 1;
  621. }
  622. SYSerr(SYS_F_IOCTL, errno);
  623. return 0;
  624. }
  625. static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  626. {
  627. struct digest_ctx *digest_ctx =
  628. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  629. if (md == NULL || digest_ctx == NULL)
  630. return 0;
  631. if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  632. memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
  633. } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
  634. SYSerr(SYS_F_IOCTL, errno);
  635. return 0;
  636. }
  637. return 1;
  638. }
  639. static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  640. {
  641. struct digest_ctx *digest_from =
  642. (struct digest_ctx *)EVP_MD_CTX_md_data(from);
  643. struct digest_ctx *digest_to =
  644. (struct digest_ctx *)EVP_MD_CTX_md_data(to);
  645. struct cphash_op cphash;
  646. if (digest_from == NULL || digest_from->init_called != 1)
  647. return 1;
  648. if (!digest_init(to)) {
  649. SYSerr(SYS_F_IOCTL, errno);
  650. return 0;
  651. }
  652. cphash.src_ses = digest_from->sess.ses;
  653. cphash.dst_ses = digest_to->sess.ses;
  654. if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
  655. SYSerr(SYS_F_IOCTL, errno);
  656. return 0;
  657. }
  658. return 1;
  659. }
  660. static int digest_cleanup(EVP_MD_CTX *ctx)
  661. {
  662. struct digest_ctx *digest_ctx =
  663. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  664. if (digest_ctx == NULL)
  665. return 1;
  666. if (ioctl(cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
  667. SYSerr(SYS_F_IOCTL, errno);
  668. return 0;
  669. }
  670. return 1;
  671. }
  672. /*
  673. * Keep tables of known nids, associated methods, selected digests, and
  674. * driver info.
  675. * Note that known_digest_nids[] isn't necessarily indexed the same way as
  676. * digest_data[] above, which the other tables are.
  677. */
  678. static int known_digest_nids[OSSL_NELEM(digest_data)];
  679. static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
  680. static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
  681. static int selected_digests[OSSL_NELEM(digest_data)];
  682. static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
  683. static int devcrypto_test_digest(size_t digest_data_index)
  684. {
  685. return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
  686. && selected_digests[digest_data_index] == 1
  687. && (digest_driver_info[digest_data_index].accelerated
  688. == DEVCRYPTO_ACCELERATED
  689. || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
  690. || (digest_driver_info[digest_data_index].accelerated
  691. != DEVCRYPTO_NOT_ACCELERATED
  692. && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
  693. }
  694. static void rebuild_known_digest_nids(ENGINE *e)
  695. {
  696. size_t i;
  697. for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
  698. if (devcrypto_test_digest(i))
  699. known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  700. }
  701. ENGINE_unregister_digests(e);
  702. ENGINE_register_digests(e);
  703. }
  704. static void prepare_digest_methods(void)
  705. {
  706. size_t i;
  707. struct session_op sess1, sess2;
  708. #ifdef CIOCGSESSINFO
  709. struct session_info_op siop;
  710. #endif
  711. struct cphash_op cphash;
  712. memset(&digest_driver_info, 0, sizeof(digest_driver_info));
  713. memset(&sess1, 0, sizeof(sess1));
  714. memset(&sess2, 0, sizeof(sess2));
  715. for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
  716. i++) {
  717. selected_digests[i] = 1;
  718. /*
  719. * Check that the digest is usable
  720. */
  721. sess1.mac = digest_data[i].devcryptoid;
  722. sess2.ses = 0;
  723. if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
  724. digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
  725. goto finish;
  726. }
  727. #ifdef CIOCGSESSINFO
  728. /* gather hardware acceleration info from the driver */
  729. siop.ses = sess1.ses;
  730. if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
  731. digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
  732. } else {
  733. digest_driver_info[i].driver_name =
  734. OPENSSL_strndup(siop.hash_info.cra_driver_name,
  735. CRYPTODEV_MAX_ALG_NAME);
  736. if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
  737. digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
  738. else
  739. digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
  740. }
  741. #endif
  742. /* digest must be capable of hash state copy */
  743. sess2.mac = sess1.mac;
  744. if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
  745. digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  746. goto finish;
  747. }
  748. cphash.src_ses = sess1.ses;
  749. cphash.dst_ses = sess2.ses;
  750. if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
  751. digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
  752. goto finish;
  753. }
  754. if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
  755. NID_undef)) == NULL
  756. || !EVP_MD_meth_set_result_size(known_digest_methods[i],
  757. digest_data[i].digestlen)
  758. || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
  759. || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
  760. || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
  761. || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
  762. || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
  763. || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
  764. sizeof(struct digest_ctx))) {
  765. digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  766. EVP_MD_meth_free(known_digest_methods[i]);
  767. known_digest_methods[i] = NULL;
  768. goto finish;
  769. }
  770. digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
  771. finish:
  772. ioctl(cfd, CIOCFSESSION, &sess1.ses);
  773. if (sess2.ses != 0)
  774. ioctl(cfd, CIOCFSESSION, &sess2.ses);
  775. if (devcrypto_test_digest(i))
  776. known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  777. }
  778. }
  779. static const EVP_MD *get_digest_method(int nid)
  780. {
  781. size_t i = get_digest_data_index(nid);
  782. if (i == (size_t)-1)
  783. return NULL;
  784. return known_digest_methods[i];
  785. }
  786. static int get_digest_nids(const int **nids)
  787. {
  788. *nids = known_digest_nids;
  789. return known_digest_nids_amount;
  790. }
  791. static void destroy_digest_method(int nid)
  792. {
  793. size_t i = get_digest_data_index(nid);
  794. EVP_MD_meth_free(known_digest_methods[i]);
  795. known_digest_methods[i] = NULL;
  796. }
  797. static void destroy_all_digest_methods(void)
  798. {
  799. size_t i;
  800. for (i = 0; i < OSSL_NELEM(digest_data); i++) {
  801. destroy_digest_method(digest_data[i].nid);
  802. OPENSSL_free(digest_driver_info[i].driver_name);
  803. digest_driver_info[i].driver_name = NULL;
  804. }
  805. }
  806. static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
  807. const int **nids, int nid)
  808. {
  809. if (digest == NULL)
  810. return get_digest_nids(nids);
  811. *digest = get_digest_method(nid);
  812. return *digest != NULL;
  813. }
  814. static void devcrypto_select_all_digests(int *digest_list)
  815. {
  816. size_t i;
  817. for (i = 0; i < OSSL_NELEM(digest_data); i++)
  818. digest_list[i] = 1;
  819. }
  820. static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
  821. {
  822. int *digest_list = (int *)usr;
  823. char *name;
  824. const EVP_MD *EVP;
  825. size_t i;
  826. if (len == 0)
  827. return 1;
  828. if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
  829. return 0;
  830. EVP = EVP_get_digestbyname(name);
  831. if (EVP == NULL)
  832. fprintf(stderr, "devcrypto: unknown digest %s\n", name);
  833. else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
  834. digest_list[i] = 1;
  835. else
  836. fprintf(stderr, "devcrypto: digest %s not available\n", name);
  837. OPENSSL_free(name);
  838. return 1;
  839. }
  840. static void dump_digest_info(void)
  841. {
  842. size_t i;
  843. const char *name;
  844. fprintf (stderr, "Information about digests supported by the /dev/crypto"
  845. " engine:\n");
  846. #ifndef CIOCGSESSINFO
  847. fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
  848. #endif
  849. for (i = 0; i < OSSL_NELEM(digest_data); i++) {
  850. name = OBJ_nid2sn(digest_data[i].nid);
  851. fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
  852. name ? name : "unknown", digest_data[i].nid,
  853. digest_data[i].devcryptoid,
  854. digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
  855. if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
  856. fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
  857. continue;
  858. }
  859. if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
  860. fprintf(stderr, " (hw accelerated)");
  861. else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
  862. fprintf(stderr, " (software)");
  863. else
  864. fprintf(stderr, " (acceleration status unknown)");
  865. if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
  866. fprintf (stderr, ". Cipher setup failed\n");
  867. else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
  868. fprintf(stderr, ", CIOCCPHASH failed\n");
  869. else
  870. fprintf(stderr, ", CIOCCPHASH capable\n");
  871. }
  872. fprintf(stderr, "\n");
  873. }
  874. #endif
  875. /******************************************************************************
  876. *
  877. * CONTROL COMMANDS
  878. *
  879. *****/
  880. #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
  881. #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
  882. #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
  883. #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
  884. static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
  885. #ifdef CIOCGSESSINFO
  886. {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
  887. "USE_SOFTDRIVERS",
  888. "specifies whether to use software (not accelerated) drivers ("
  889. OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
  890. OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
  891. OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
  892. "=use if acceleration can't be determined) [default="
  893. OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
  894. ENGINE_CMD_FLAG_NUMERIC},
  895. #endif
  896. {DEVCRYPTO_CMD_CIPHERS,
  897. "CIPHERS",
  898. "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
  899. ENGINE_CMD_FLAG_STRING},
  900. #ifdef IMPLEMENT_DIGEST
  901. {DEVCRYPTO_CMD_DIGESTS,
  902. "DIGESTS",
  903. "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
  904. ENGINE_CMD_FLAG_STRING},
  905. #endif
  906. {DEVCRYPTO_CMD_DUMP_INFO,
  907. "DUMP_INFO",
  908. "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
  909. ENGINE_CMD_FLAG_NO_INPUT},
  910. {0, NULL, NULL, 0}
  911. };
  912. static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  913. {
  914. int *new_list;
  915. switch (cmd) {
  916. #ifdef CIOCGSESSINFO
  917. case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
  918. switch (i) {
  919. case DEVCRYPTO_REQUIRE_ACCELERATED:
  920. case DEVCRYPTO_USE_SOFTWARE:
  921. case DEVCRYPTO_REJECT_SOFTWARE:
  922. break;
  923. default:
  924. fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
  925. return 0;
  926. }
  927. if (use_softdrivers == i)
  928. return 1;
  929. use_softdrivers = i;
  930. #ifdef IMPLEMENT_DIGEST
  931. rebuild_known_digest_nids(e);
  932. #endif
  933. rebuild_known_cipher_nids(e);
  934. return 1;
  935. #endif /* CIOCGSESSINFO */
  936. case DEVCRYPTO_CMD_CIPHERS:
  937. if (p == NULL)
  938. return 1;
  939. if (strcasecmp((const char *)p, "ALL") == 0) {
  940. devcrypto_select_all_ciphers(selected_ciphers);
  941. } else if (strcasecmp((const char*)p, "NONE") == 0) {
  942. memset(selected_ciphers, 0, sizeof(selected_ciphers));
  943. } else {
  944. new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
  945. if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
  946. OPENSSL_free(new_list);
  947. return 0;
  948. }
  949. memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
  950. OPENSSL_free(new_list);
  951. }
  952. rebuild_known_cipher_nids(e);
  953. return 1;
  954. #ifdef IMPLEMENT_DIGEST
  955. case DEVCRYPTO_CMD_DIGESTS:
  956. if (p == NULL)
  957. return 1;
  958. if (strcasecmp((const char *)p, "ALL") == 0) {
  959. devcrypto_select_all_digests(selected_digests);
  960. } else if (strcasecmp((const char*)p, "NONE") == 0) {
  961. memset(selected_digests, 0, sizeof(selected_digests));
  962. } else {
  963. new_list=OPENSSL_zalloc(sizeof(selected_digests));
  964. if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
  965. OPENSSL_free(new_list);
  966. return 0;
  967. }
  968. memcpy(selected_digests, new_list, sizeof(selected_digests));
  969. OPENSSL_free(new_list);
  970. }
  971. rebuild_known_digest_nids(e);
  972. return 1;
  973. #endif /* IMPLEMENT_DIGEST */
  974. case DEVCRYPTO_CMD_DUMP_INFO:
  975. dump_cipher_info();
  976. #ifdef IMPLEMENT_DIGEST
  977. dump_digest_info();
  978. #endif
  979. return 1;
  980. default:
  981. break;
  982. }
  983. return 0;
  984. }
  985. /******************************************************************************
  986. *
  987. * LOAD / UNLOAD
  988. *
  989. *****/
  990. static int devcrypto_unload(ENGINE *e)
  991. {
  992. destroy_all_cipher_methods();
  993. #ifdef IMPLEMENT_DIGEST
  994. destroy_all_digest_methods();
  995. #endif
  996. close(cfd);
  997. return 1;
  998. }
  999. /*
  1000. * This engine is always built into libcrypto, so it doesn't offer any
  1001. * ability to be dynamically loadable.
  1002. */
  1003. void engine_load_devcrypto_int()
  1004. {
  1005. ENGINE *e = NULL;
  1006. if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  1007. fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
  1008. return;
  1009. }
  1010. if ((e = ENGINE_new()) == NULL
  1011. || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
  1012. ENGINE_free(e);
  1013. /*
  1014. * We know that devcrypto_unload() won't be called when one of the
  1015. * above two calls have failed, so we close cfd explicitly here to
  1016. * avoid leaking resources.
  1017. */
  1018. close(cfd);
  1019. return;
  1020. }
  1021. prepare_cipher_methods();
  1022. #ifdef IMPLEMENT_DIGEST
  1023. prepare_digest_methods();
  1024. #endif
  1025. if (!ENGINE_set_id(e, "devcrypto")
  1026. || !ENGINE_set_name(e, "/dev/crypto engine")
  1027. || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
  1028. || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
  1029. /*
  1030. * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
  1031. * implementations, it seems to only exist in FreeBSD, and regarding the
  1032. * parameters in its crypt_kop, the manual crypto(4) has this to say:
  1033. *
  1034. * The semantics of these arguments are currently undocumented.
  1035. *
  1036. * Reading through the FreeBSD source code doesn't give much more than
  1037. * their CRK_MOD_EXP implementation for ubsec.
  1038. *
  1039. * It doesn't look much better with cryptodev-linux. They have the crypt_kop
  1040. * structure as well as the command (CRK_*) in cryptodev.h, but no support
  1041. * seems to be implemented at all for the moment.
  1042. *
  1043. * At the time of writing, it seems impossible to write proper support for
  1044. * FreeBSD's asym features without some very deep knowledge and access to
  1045. * specific kernel modules.
  1046. *
  1047. * /Richard Levitte, 2017-05-11
  1048. */
  1049. #if 0
  1050. # ifndef OPENSSL_NO_RSA
  1051. || !ENGINE_set_RSA(e, devcrypto_rsa)
  1052. # endif
  1053. # ifndef OPENSSL_NO_DSA
  1054. || !ENGINE_set_DSA(e, devcrypto_dsa)
  1055. # endif
  1056. # ifndef OPENSSL_NO_DH
  1057. || !ENGINE_set_DH(e, devcrypto_dh)
  1058. # endif
  1059. # ifndef OPENSSL_NO_EC
  1060. || !ENGINE_set_EC(e, devcrypto_ec)
  1061. # endif
  1062. #endif
  1063. || !ENGINE_set_ciphers(e, devcrypto_ciphers)
  1064. #ifdef IMPLEMENT_DIGEST
  1065. || !ENGINE_set_digests(e, devcrypto_digests)
  1066. #endif
  1067. ) {
  1068. ENGINE_free(e);
  1069. return;
  1070. }
  1071. ENGINE_add(e);
  1072. ENGINE_free(e); /* Loose our local reference */
  1073. ERR_clear_error();
  1074. }