2
0

e_devcrypto.c 41 KB

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