eng_devcrypto.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "e_os.h"
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <sys/ioctl.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/err.h>
  19. #include <openssl/engine.h>
  20. #include <openssl/objects.h>
  21. #include <crypto/cryptodev.h>
  22. #include "internal/engine.h"
  23. #ifdef CRYPTO_ALGORITHM_MIN
  24. # define CHECK_BSD_STYLE_MACROS
  25. #endif
  26. /******************************************************************************
  27. *
  28. * Ciphers
  29. *
  30. * Because they all do the same basic operation, we have only one set of
  31. * method functions for them all to share, and a mapping table between
  32. * NIDs and cryptodev IDs, with all the necessary size data.
  33. *
  34. *****/
  35. struct cipher_ctx {
  36. int cfd;
  37. struct session_op sess;
  38. /* to pass from init to do_cipher */
  39. const unsigned char *iv;
  40. int op; /* COP_ENCRYPT or COP_DECRYPT */
  41. };
  42. static const struct cipher_data_st {
  43. int nid;
  44. int blocksize;
  45. int keylen;
  46. int ivlen;
  47. int flags;
  48. int devcryptoid;
  49. } cipher_data[] = {
  50. #ifndef OPENSSL_NO_DES
  51. { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
  52. { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
  53. #endif
  54. #ifndef OPENSSL_NO_BF
  55. { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
  56. #endif
  57. #ifndef OPENSSL_NO_CAST
  58. { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
  59. #endif
  60. { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  61. { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  62. { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  63. #ifndef OPENSSL_NO_RC4
  64. { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
  65. #endif
  66. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
  67. { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  68. { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  69. { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  70. #endif
  71. #if 0 /* Not yet supported */
  72. { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  73. { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  74. #endif
  75. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
  76. { NID_aes_128_ecb, 16, 128 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  77. { NID_aes_192_ecb, 16, 192 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  78. { NID_aes_256_ecb, 16, 256 / 8, 16, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  79. #endif
  80. #if 0 /* Not yet supported */
  81. { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  82. { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  83. { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  84. #endif
  85. #ifndef OPENSSL_NO_CAMELLIA
  86. { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
  87. CRYPTO_CAMELLIA_CBC },
  88. { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
  89. CRYPTO_CAMELLIA_CBC },
  90. { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
  91. CRYPTO_CAMELLIA_CBC },
  92. #endif
  93. };
  94. static size_t get_cipher_data_index(int nid)
  95. {
  96. size_t i;
  97. for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  98. if (nid == cipher_data[i].nid)
  99. return i;
  100. /*
  101. * Code further down must make sure that only NIDs in the table above
  102. * are used. If any other NID reaches this function, there's a grave
  103. * coding error further down.
  104. */
  105. assert("Code that never should be reached" == NULL);
  106. return -1;
  107. }
  108. static const struct cipher_data_st *get_cipher_data(int nid)
  109. {
  110. return &cipher_data[get_cipher_data_index(nid)];
  111. }
  112. /*
  113. * Following are the three necessary functions to map OpenSSL functionality
  114. * with cryptodev.
  115. */
  116. static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  117. const unsigned char *iv, int enc)
  118. {
  119. struct cipher_ctx *cipher_ctx =
  120. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  121. const struct cipher_data_st *cipher_d =
  122. get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
  123. if ((cipher_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  124. SYSerr(SYS_F_OPEN, errno);
  125. return 0;
  126. }
  127. memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
  128. cipher_ctx->sess.cipher = cipher_d->devcryptoid;
  129. cipher_ctx->sess.keylen = cipher_d->keylen;
  130. cipher_ctx->sess.key = (void *)key;
  131. cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
  132. if (ioctl(cipher_ctx->cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
  133. SYSerr(SYS_F_IOCTL, errno);
  134. close(cipher_ctx->cfd);
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  140. const unsigned char *in, size_t inl)
  141. {
  142. struct cipher_ctx *cipher_ctx =
  143. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  144. struct crypt_op cryp;
  145. #if !defined(COP_FLAG_WRITE_IV)
  146. unsigned char saved_iv[EVP_MAX_IV_LENGTH];
  147. #endif
  148. memset(&cryp, 0, sizeof(cryp));
  149. cryp.ses = cipher_ctx->sess.ses;
  150. cryp.len = inl;
  151. cryp.src = (void *)in;
  152. cryp.dst = (void *)out;
  153. cryp.iv = (void *)EVP_CIPHER_CTX_iv_noconst(ctx);
  154. cryp.op = cipher_ctx->op;
  155. #if !defined(COP_FLAG_WRITE_IV)
  156. cryp.flags = 0;
  157. if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
  158. assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
  159. if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  160. unsigned char *ivptr = in + inl - EVP_CIPHER_CTX_iv_length(ctx);
  161. memcpy(saved_iv, ivptr, EVP_CIPHER_CTX_iv_length(ctx));
  162. }
  163. }
  164. #else
  165. cryp.flags = COP_FLAG_WRITE_IV;
  166. #endif
  167. if (ioctl(cipher_ctx->cfd, CIOCCRYPT, &cryp) < 0) {
  168. SYSerr(SYS_F_IOCTL, errno);
  169. return 0;
  170. }
  171. #if !defined(COP_FLAG_WRITE_IV)
  172. if (EVP_CIPHER_CTX_iv_length(ctx) > 0) {
  173. unsigned char *ivptr = saved_iv;
  174. assert(inl >= EVP_CIPHER_CTX_iv_length(ctx));
  175. if (!EVP_CIPHER_CTX_encrypting(ctx))
  176. ivptr = out + inl - EVP_CIPHER_CTX_iv_length(ctx);
  177. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), ivptr,
  178. EVP_CIPHER_CTX_iv_length(ctx));
  179. }
  180. #endif
  181. return 1;
  182. }
  183. static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
  184. {
  185. struct cipher_ctx *cipher_ctx =
  186. (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  187. if (ioctl(cipher_ctx->cfd, CIOCFSESSION, &cipher_ctx->sess.ses) < 0) {
  188. SYSerr(SYS_F_IOCTL, errno);
  189. return 0;
  190. }
  191. if (close(cipher_ctx->cfd) < 0) {
  192. SYSerr(SYS_F_CLOSE, errno);
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. /*
  198. * Keep a table of known nids and associated methods.
  199. * Note that known_cipher_nids[] isn't necessarily indexed the same way as
  200. * cipher_data[] above, which known_cipher_methods[] is.
  201. */
  202. static int known_cipher_nids[OSSL_NELEM(cipher_data)];
  203. static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
  204. static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
  205. static void prepare_cipher_methods()
  206. {
  207. size_t i;
  208. struct session_op sess;
  209. int cfd;
  210. if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
  211. return;
  212. memset(&sess, 0, sizeof(sess));
  213. sess.key = (void *)"01234567890123456789012345678901234567890123456789";
  214. for (i = 0, known_cipher_nids_amount = 0;
  215. i < OSSL_NELEM(cipher_data); i++) {
  216. /*
  217. * Check that the algo is really availably by trying to open and close
  218. * a session.
  219. */
  220. sess.cipher = cipher_data[i].devcryptoid;
  221. sess.keylen = cipher_data[i].keylen;
  222. if (ioctl(cfd, CIOCGSESSION, &sess) < 0
  223. || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
  224. continue;
  225. if ((known_cipher_methods[i] =
  226. EVP_CIPHER_meth_new(cipher_data[i].nid,
  227. cipher_data[i].blocksize,
  228. cipher_data[i].keylen)) == NULL
  229. || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
  230. cipher_data[i].ivlen)
  231. || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
  232. cipher_data[i].flags
  233. | EVP_CIPH_FLAG_DEFAULT_ASN1)
  234. || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
  235. || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
  236. cipher_do_cipher)
  237. || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
  238. cipher_cleanup)
  239. || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
  240. sizeof(struct cipher_ctx))) {
  241. EVP_CIPHER_meth_free(known_cipher_methods[i]);
  242. known_cipher_methods[i] = NULL;
  243. } else {
  244. known_cipher_nids[known_cipher_nids_amount++] =
  245. cipher_data[i].nid;
  246. }
  247. }
  248. close(cfd);
  249. }
  250. static const EVP_CIPHER *get_cipher_method(int nid)
  251. {
  252. size_t i = get_cipher_data_index(nid);
  253. if (i == (size_t)-1)
  254. return NULL;
  255. return known_cipher_methods[i];
  256. }
  257. static int get_cipher_nids(const int **nids)
  258. {
  259. *nids = known_cipher_nids;
  260. return known_cipher_nids_amount;
  261. }
  262. static void destroy_cipher_method(int nid)
  263. {
  264. size_t i = get_cipher_data_index(nid);
  265. EVP_CIPHER_meth_free(known_cipher_methods[i]);
  266. known_cipher_methods[i] = NULL;
  267. }
  268. static void destroy_all_cipher_methods()
  269. {
  270. size_t i;
  271. for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  272. destroy_cipher_method(cipher_data[i].nid);
  273. }
  274. static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  275. const int **nids, int nid)
  276. {
  277. if (cipher == NULL)
  278. return get_cipher_nids(nids);
  279. *cipher = get_cipher_method(nid);
  280. return *cipher != NULL;
  281. }
  282. /*
  283. * We only support digests if the cryptodev implementation supports multiple
  284. * data updates. Otherwise, we would be forced to maintain a cache, which is
  285. * perilous if there's a lot of data coming in (if someone wants to checksum
  286. * an OpenSSL tarball, for example).
  287. */
  288. #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  289. /******************************************************************************
  290. *
  291. * Digests
  292. *
  293. * Because they all do the same basic operation, we have only one set of
  294. * method functions for them all to share, and a mapping table between
  295. * NIDs and cryptodev IDs, with all the necessary size data.
  296. *
  297. *****/
  298. struct digest_ctx {
  299. int cfd;
  300. struct session_op sess;
  301. int init;
  302. };
  303. static const struct digest_data_st {
  304. int nid;
  305. int digestlen;
  306. int devcryptoid;
  307. } digest_data[] = {
  308. #ifndef OPENSSL_NO_MD5
  309. { NID_md5, 16, CRYPTO_MD5 },
  310. #endif
  311. { NID_sha1, 20, CRYPTO_SHA1 },
  312. #ifndef OPENSSL_NO_RMD160
  313. # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
  314. { NID_ripemd160, 20, CRYPTO_RIPEMD160 },
  315. # endif
  316. #endif
  317. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
  318. { NID_sha224, 224 / 8, CRYPTO_SHA2_224 },
  319. #endif
  320. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
  321. { NID_sha256, 256 / 8, CRYPTO_SHA2_256 },
  322. #endif
  323. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
  324. { NID_sha384, 384 / 8, CRYPTO_SHA2_384 },
  325. #endif
  326. #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
  327. { NID_sha512, 512 / 8, CRYPTO_SHA2_512 },
  328. #endif
  329. };
  330. static size_t get_digest_data_index(int nid)
  331. {
  332. size_t i;
  333. for (i = 0; i < OSSL_NELEM(digest_data); i++)
  334. if (nid == digest_data[i].nid)
  335. return i;
  336. /*
  337. * Code further down must make sure that only NIDs in the table above
  338. * are used. If any other NID reaches this function, there's a grave
  339. * coding error further down.
  340. */
  341. assert("Code that never should be reached" == NULL);
  342. return -1;
  343. }
  344. static const struct digest_data_st *get_digest_data(int nid)
  345. {
  346. return &digest_data[get_digest_data_index(nid)];
  347. }
  348. /*
  349. * Following are the four necessary functions to map OpenSSL functionality
  350. * with cryptodev.
  351. */
  352. static int digest_init(EVP_MD_CTX *ctx)
  353. {
  354. struct digest_ctx *digest_ctx =
  355. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  356. const struct digest_data_st *digest_d =
  357. get_digest_data(EVP_MD_CTX_type(ctx));
  358. if (digest_ctx->init == 0
  359. && (digest_ctx->cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  360. SYSerr(SYS_F_OPEN, errno);
  361. return 0;
  362. }
  363. digest_ctx->init = 1;
  364. memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
  365. digest_ctx->sess.mac = digest_d->devcryptoid;
  366. if (ioctl(digest_ctx->cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
  367. SYSerr(SYS_F_IOCTL, errno);
  368. close(digest_ctx->cfd);
  369. return 0;
  370. }
  371. return 1;
  372. }
  373. static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
  374. void *res, unsigned int flags)
  375. {
  376. struct crypt_op cryp;
  377. memset(&cryp, 0, sizeof(cryp));
  378. cryp.ses = ctx->sess.ses;
  379. cryp.len = srclen;
  380. cryp.src = (void *)src;
  381. cryp.dst = NULL;
  382. cryp.mac = res;
  383. cryp.flags = flags;
  384. return ioctl(ctx->cfd, CIOCCRYPT, &cryp);
  385. }
  386. static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  387. {
  388. struct digest_ctx *digest_ctx =
  389. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  390. if (count == 0)
  391. return 1;
  392. if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
  393. SYSerr(SYS_F_IOCTL, errno);
  394. return 0;
  395. }
  396. return 1;
  397. }
  398. static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  399. {
  400. struct digest_ctx *digest_ctx =
  401. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  402. if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
  403. SYSerr(SYS_F_IOCTL, errno);
  404. return 0;
  405. }
  406. if (ioctl(digest_ctx->cfd, CIOCFSESSION, &digest_ctx->sess.ses) < 0) {
  407. SYSerr(SYS_F_IOCTL, errno);
  408. return 0;
  409. }
  410. return 1;
  411. }
  412. static int digest_cleanup(EVP_MD_CTX *ctx)
  413. {
  414. struct digest_ctx *digest_ctx =
  415. (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  416. if (close(digest_ctx->cfd) < 0) {
  417. SYSerr(SYS_F_CLOSE, errno);
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. /*
  423. * Keep a table of known nids and associated methods.
  424. * Note that known_digest_nids[] isn't necessarily indexed the same way as
  425. * digest_data[] above, which known_digest_methods[] is.
  426. */
  427. static int known_digest_nids[OSSL_NELEM(digest_data)];
  428. static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
  429. static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
  430. static void prepare_digest_methods()
  431. {
  432. size_t i;
  433. struct session_op sess;
  434. int cfd;
  435. if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0)
  436. return;
  437. memset(&sess, 0, sizeof(sess));
  438. for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
  439. i++) {
  440. /*
  441. * Check that the algo is really availably by trying to open and close
  442. * a session.
  443. */
  444. sess.mac = digest_data[i].devcryptoid;
  445. if (ioctl(cfd, CIOCGSESSION, &sess) < 0
  446. || ioctl(cfd, CIOCFSESSION, &sess.ses) < 0)
  447. continue;
  448. if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
  449. NID_undef)) == NULL
  450. || !EVP_MD_meth_set_result_size(known_digest_methods[i],
  451. digest_data[i].digestlen)
  452. || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
  453. || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
  454. || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
  455. || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
  456. || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
  457. sizeof(struct digest_ctx))) {
  458. EVP_MD_meth_free(known_digest_methods[i]);
  459. known_digest_methods[i] = NULL;
  460. } else {
  461. known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  462. }
  463. }
  464. close(cfd);
  465. }
  466. static const EVP_MD *get_digest_method(int nid)
  467. {
  468. size_t i = get_digest_data_index(nid);
  469. if (i == (size_t)-1)
  470. return NULL;
  471. return known_digest_methods[i];
  472. }
  473. static int get_digest_nids(const int **nids)
  474. {
  475. *nids = known_digest_nids;
  476. return known_digest_nids_amount;
  477. }
  478. static void destroy_digest_method(int nid)
  479. {
  480. size_t i = get_digest_data_index(nid);
  481. EVP_MD_meth_free(known_digest_methods[i]);
  482. known_digest_methods[i] = NULL;
  483. }
  484. static void destroy_all_digest_methods()
  485. {
  486. size_t i;
  487. for (i = 0; i < OSSL_NELEM(digest_data); i++)
  488. destroy_digest_method(digest_data[i].nid);
  489. }
  490. static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
  491. const int **nids, int nid)
  492. {
  493. if (digest == NULL)
  494. return get_digest_nids(nids);
  495. *digest = get_digest_method(nid);
  496. return *digest != NULL;
  497. }
  498. #endif
  499. /******************************************************************************
  500. *
  501. * LOAD / UNLOAD
  502. *
  503. *****/
  504. static int devcrypto_unload(ENGINE *e)
  505. {
  506. destroy_all_cipher_methods();
  507. #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  508. destroy_all_digest_methods();
  509. #endif
  510. return 1;
  511. }
  512. /*
  513. * This engine is always built into libcrypto, so it doesn't offer any
  514. * ability to be dynamically loadable.
  515. */
  516. void engine_load_devcrypto_int()
  517. {
  518. ENGINE *e = NULL;
  519. if (access("/dev/crypto", R_OK | W_OK) < 0) {
  520. fprintf(stderr,
  521. "/dev/crypto not present, not enabling devcrypto engine\n");
  522. return;
  523. }
  524. prepare_cipher_methods();
  525. #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  526. prepare_digest_methods();
  527. #endif
  528. if ((e = ENGINE_new()) == NULL)
  529. return;
  530. if (!ENGINE_set_id(e, "devcrypto")
  531. || !ENGINE_set_name(e, "/dev/crypto engine")
  532. || !ENGINE_set_destroy_function(e, devcrypto_unload)
  533. /*
  534. * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
  535. * implementations, it seems to only exist in FreeBSD, and regarding the
  536. * parameters in its crypt_kop, the manual crypto(4) has this to say:
  537. *
  538. * The semantics of these arguments are currently undocumented.
  539. *
  540. * Reading through the FreeBSD source code doesn't give much more than
  541. * their CRK_MOD_EXP implementation for ubsec.
  542. *
  543. * It doesn't look much better with cryptodev-linux. They have the crypt_kop
  544. * structure as well as the command (CRK_*) in cryptodev.h, but no support
  545. * seems to be implemented at all for the moment.
  546. *
  547. * At the time of writing, it seems impossible to write proper support for
  548. * FreeBSD's asym features without some very deep knowledge and access to
  549. * specific kernel modules.
  550. *
  551. * /Richard Levitte, 2017-05-11
  552. */
  553. #if 0
  554. # ifndef OPENSSL_NO_RSA
  555. || !ENGINE_set_RSA(e, devcrypto_rsa)
  556. # endif
  557. # ifndef OPENSSL_NO_DSA
  558. || !ENGINE_set_DSA(e, devcrypto_dsa)
  559. # endif
  560. # ifndef OPENSSL_NO_DH
  561. || !ENGINE_set_DH(e, devcrypto_dh)
  562. # endif
  563. # ifndef OPENSSL_NO_EC
  564. || !ENGINE_set_EC(e, devcrypto_ec)
  565. # endif
  566. #endif
  567. || !ENGINE_set_ciphers(e, devcrypto_ciphers)
  568. #if defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  569. || !ENGINE_set_digests(e, devcrypto_digests)
  570. #endif
  571. ) {
  572. ENGINE_free(e);
  573. return;
  574. }
  575. ENGINE_add(e);
  576. ENGINE_free(e); /* Loose our local reference */
  577. ERR_clear_error();
  578. }