2
0

e_devcrypto.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * Copyright 2017-2020 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 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. ERR_set_mark();
  1120. ENGINE_add(e);
  1121. /*
  1122. * If the "add" worked, it gets a structural reference. So either way, we
  1123. * release our just-created reference.
  1124. */
  1125. ENGINE_free(e); /* Loose our local reference */
  1126. /*
  1127. * If the "add" didn't work, it was probably a conflict because it was
  1128. * already added (eg. someone calling ENGINE_load_blah then calling
  1129. * ENGINE_load_builtin_engines() perhaps).
  1130. */
  1131. ERR_pop_to_mark();
  1132. }
  1133. }
  1134. #else
  1135. static int bind_helper(ENGINE *e, const char *id)
  1136. {
  1137. if ((id && (strcmp(id, engine_devcrypto_id) != 0))
  1138. || !open_devcrypto())
  1139. return 0;
  1140. if (!bind_devcrypto(e)) {
  1141. close_devcrypto();
  1142. return 0;
  1143. }
  1144. return 1;
  1145. }
  1146. IMPLEMENT_DYNAMIC_CHECK_FN()
  1147. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  1148. #endif