e_devcrypto.c 43 KB

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