crypto_engine.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Handle async block request by crypto hardware engine.
  4. *
  5. * Copyright (C) 2016 Linaro, Inc.
  6. *
  7. * Author: Baolin Wang <baolin.wang@linaro.org>
  8. */
  9. #include <crypto/internal/aead.h>
  10. #include <crypto/internal/akcipher.h>
  11. #include <crypto/internal/engine.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/internal/kpp.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <linux/err.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <uapi/linux/sched/types.h>
  21. #include "internal.h"
  22. #define CRYPTO_ENGINE_MAX_QLEN 10
  23. /* Temporary algorithm flag used to indicate an updated driver. */
  24. #define CRYPTO_ALG_ENGINE 0x200
  25. struct crypto_engine_alg {
  26. struct crypto_alg base;
  27. struct crypto_engine_op op;
  28. };
  29. /**
  30. * crypto_finalize_request - finalize one request if the request is done
  31. * @engine: the hardware engine
  32. * @req: the request need to be finalized
  33. * @err: error number
  34. */
  35. static void crypto_finalize_request(struct crypto_engine *engine,
  36. struct crypto_async_request *req, int err)
  37. {
  38. unsigned long flags;
  39. /*
  40. * If hardware cannot enqueue more requests
  41. * and retry mechanism is not supported
  42. * make sure we are completing the current request
  43. */
  44. if (!engine->retry_support) {
  45. spin_lock_irqsave(&engine->queue_lock, flags);
  46. if (engine->cur_req == req) {
  47. engine->cur_req = NULL;
  48. }
  49. spin_unlock_irqrestore(&engine->queue_lock, flags);
  50. }
  51. lockdep_assert_in_softirq();
  52. crypto_request_complete(req, err);
  53. kthread_queue_work(engine->kworker, &engine->pump_requests);
  54. }
  55. /**
  56. * crypto_pump_requests - dequeue one request from engine queue to process
  57. * @engine: the hardware engine
  58. * @in_kthread: true if we are in the context of the request pump thread
  59. *
  60. * This function checks if there is any request in the engine queue that
  61. * needs processing and if so call out to the driver to initialize hardware
  62. * and handle each request.
  63. */
  64. static void crypto_pump_requests(struct crypto_engine *engine,
  65. bool in_kthread)
  66. {
  67. struct crypto_async_request *async_req, *backlog;
  68. struct crypto_engine_alg *alg;
  69. struct crypto_engine_op *op;
  70. unsigned long flags;
  71. bool was_busy = false;
  72. int ret;
  73. spin_lock_irqsave(&engine->queue_lock, flags);
  74. /* Make sure we are not already running a request */
  75. if (!engine->retry_support && engine->cur_req)
  76. goto out;
  77. /* If another context is idling then defer */
  78. if (engine->idling) {
  79. kthread_queue_work(engine->kworker, &engine->pump_requests);
  80. goto out;
  81. }
  82. /* Check if the engine queue is idle */
  83. if (!crypto_queue_len(&engine->queue) || !engine->running) {
  84. if (!engine->busy)
  85. goto out;
  86. /* Only do teardown in the thread */
  87. if (!in_kthread) {
  88. kthread_queue_work(engine->kworker,
  89. &engine->pump_requests);
  90. goto out;
  91. }
  92. engine->busy = false;
  93. engine->idling = true;
  94. spin_unlock_irqrestore(&engine->queue_lock, flags);
  95. if (engine->unprepare_crypt_hardware &&
  96. engine->unprepare_crypt_hardware(engine))
  97. dev_err(engine->dev, "failed to unprepare crypt hardware\n");
  98. spin_lock_irqsave(&engine->queue_lock, flags);
  99. engine->idling = false;
  100. goto out;
  101. }
  102. start_request:
  103. /* Get the fist request from the engine queue to handle */
  104. backlog = crypto_get_backlog(&engine->queue);
  105. async_req = crypto_dequeue_request(&engine->queue);
  106. if (!async_req)
  107. goto out;
  108. /*
  109. * If hardware doesn't support the retry mechanism,
  110. * keep track of the request we are processing now.
  111. * We'll need it on completion (crypto_finalize_request).
  112. */
  113. if (!engine->retry_support)
  114. engine->cur_req = async_req;
  115. if (engine->busy)
  116. was_busy = true;
  117. else
  118. engine->busy = true;
  119. spin_unlock_irqrestore(&engine->queue_lock, flags);
  120. /* Until here we get the request need to be encrypted successfully */
  121. if (!was_busy && engine->prepare_crypt_hardware) {
  122. ret = engine->prepare_crypt_hardware(engine);
  123. if (ret) {
  124. dev_err(engine->dev, "failed to prepare crypt hardware\n");
  125. goto req_err_1;
  126. }
  127. }
  128. if (async_req->tfm->__crt_alg->cra_flags & CRYPTO_ALG_ENGINE) {
  129. alg = container_of(async_req->tfm->__crt_alg,
  130. struct crypto_engine_alg, base);
  131. op = &alg->op;
  132. } else {
  133. dev_err(engine->dev, "failed to do request\n");
  134. ret = -EINVAL;
  135. goto req_err_1;
  136. }
  137. ret = op->do_one_request(engine, async_req);
  138. /* Request unsuccessfully executed by hardware */
  139. if (ret < 0) {
  140. /*
  141. * If hardware queue is full (-ENOSPC), requeue request
  142. * regardless of backlog flag.
  143. * Otherwise, unprepare and complete the request.
  144. */
  145. if (!engine->retry_support ||
  146. (ret != -ENOSPC)) {
  147. dev_err(engine->dev,
  148. "Failed to do one request from queue: %d\n",
  149. ret);
  150. goto req_err_1;
  151. }
  152. spin_lock_irqsave(&engine->queue_lock, flags);
  153. /*
  154. * If hardware was unable to execute request, enqueue it
  155. * back in front of crypto-engine queue, to keep the order
  156. * of requests.
  157. */
  158. crypto_enqueue_request_head(&engine->queue, async_req);
  159. kthread_queue_work(engine->kworker, &engine->pump_requests);
  160. goto out;
  161. }
  162. goto retry;
  163. req_err_1:
  164. crypto_request_complete(async_req, ret);
  165. retry:
  166. if (backlog)
  167. crypto_request_complete(backlog, -EINPROGRESS);
  168. /* If retry mechanism is supported, send new requests to engine */
  169. if (engine->retry_support) {
  170. spin_lock_irqsave(&engine->queue_lock, flags);
  171. goto start_request;
  172. }
  173. return;
  174. out:
  175. spin_unlock_irqrestore(&engine->queue_lock, flags);
  176. /*
  177. * Batch requests is possible only if
  178. * hardware can enqueue multiple requests
  179. */
  180. if (engine->do_batch_requests) {
  181. ret = engine->do_batch_requests(engine);
  182. if (ret)
  183. dev_err(engine->dev, "failed to do batch requests: %d\n",
  184. ret);
  185. }
  186. return;
  187. }
  188. static void crypto_pump_work(struct kthread_work *work)
  189. {
  190. struct crypto_engine *engine =
  191. container_of(work, struct crypto_engine, pump_requests);
  192. crypto_pump_requests(engine, true);
  193. }
  194. /**
  195. * crypto_transfer_request - transfer the new request into the engine queue
  196. * @engine: the hardware engine
  197. * @req: the request need to be listed into the engine queue
  198. * @need_pump: indicates whether queue the pump of request to kthread_work
  199. */
  200. static int crypto_transfer_request(struct crypto_engine *engine,
  201. struct crypto_async_request *req,
  202. bool need_pump)
  203. {
  204. unsigned long flags;
  205. int ret;
  206. spin_lock_irqsave(&engine->queue_lock, flags);
  207. if (!engine->running) {
  208. spin_unlock_irqrestore(&engine->queue_lock, flags);
  209. return -ESHUTDOWN;
  210. }
  211. ret = crypto_enqueue_request(&engine->queue, req);
  212. if (!engine->busy && need_pump)
  213. kthread_queue_work(engine->kworker, &engine->pump_requests);
  214. spin_unlock_irqrestore(&engine->queue_lock, flags);
  215. return ret;
  216. }
  217. /**
  218. * crypto_transfer_request_to_engine - transfer one request to list
  219. * into the engine queue
  220. * @engine: the hardware engine
  221. * @req: the request need to be listed into the engine queue
  222. */
  223. static int crypto_transfer_request_to_engine(struct crypto_engine *engine,
  224. struct crypto_async_request *req)
  225. {
  226. return crypto_transfer_request(engine, req, true);
  227. }
  228. /**
  229. * crypto_transfer_aead_request_to_engine - transfer one aead_request
  230. * to list into the engine queue
  231. * @engine: the hardware engine
  232. * @req: the request need to be listed into the engine queue
  233. */
  234. int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine,
  235. struct aead_request *req)
  236. {
  237. return crypto_transfer_request_to_engine(engine, &req->base);
  238. }
  239. EXPORT_SYMBOL_GPL(crypto_transfer_aead_request_to_engine);
  240. /**
  241. * crypto_transfer_akcipher_request_to_engine - transfer one akcipher_request
  242. * to list into the engine queue
  243. * @engine: the hardware engine
  244. * @req: the request need to be listed into the engine queue
  245. */
  246. int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine,
  247. struct akcipher_request *req)
  248. {
  249. return crypto_transfer_request_to_engine(engine, &req->base);
  250. }
  251. EXPORT_SYMBOL_GPL(crypto_transfer_akcipher_request_to_engine);
  252. /**
  253. * crypto_transfer_hash_request_to_engine - transfer one ahash_request
  254. * to list into the engine queue
  255. * @engine: the hardware engine
  256. * @req: the request need to be listed into the engine queue
  257. */
  258. int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
  259. struct ahash_request *req)
  260. {
  261. return crypto_transfer_request_to_engine(engine, &req->base);
  262. }
  263. EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
  264. /**
  265. * crypto_transfer_kpp_request_to_engine - transfer one kpp_request to list
  266. * into the engine queue
  267. * @engine: the hardware engine
  268. * @req: the request need to be listed into the engine queue
  269. */
  270. int crypto_transfer_kpp_request_to_engine(struct crypto_engine *engine,
  271. struct kpp_request *req)
  272. {
  273. return crypto_transfer_request_to_engine(engine, &req->base);
  274. }
  275. EXPORT_SYMBOL_GPL(crypto_transfer_kpp_request_to_engine);
  276. /**
  277. * crypto_transfer_skcipher_request_to_engine - transfer one skcipher_request
  278. * to list into the engine queue
  279. * @engine: the hardware engine
  280. * @req: the request need to be listed into the engine queue
  281. */
  282. int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine,
  283. struct skcipher_request *req)
  284. {
  285. return crypto_transfer_request_to_engine(engine, &req->base);
  286. }
  287. EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request_to_engine);
  288. /**
  289. * crypto_finalize_aead_request - finalize one aead_request if
  290. * the request is done
  291. * @engine: the hardware engine
  292. * @req: the request need to be finalized
  293. * @err: error number
  294. */
  295. void crypto_finalize_aead_request(struct crypto_engine *engine,
  296. struct aead_request *req, int err)
  297. {
  298. return crypto_finalize_request(engine, &req->base, err);
  299. }
  300. EXPORT_SYMBOL_GPL(crypto_finalize_aead_request);
  301. /**
  302. * crypto_finalize_akcipher_request - finalize one akcipher_request if
  303. * the request is done
  304. * @engine: the hardware engine
  305. * @req: the request need to be finalized
  306. * @err: error number
  307. */
  308. void crypto_finalize_akcipher_request(struct crypto_engine *engine,
  309. struct akcipher_request *req, int err)
  310. {
  311. return crypto_finalize_request(engine, &req->base, err);
  312. }
  313. EXPORT_SYMBOL_GPL(crypto_finalize_akcipher_request);
  314. /**
  315. * crypto_finalize_hash_request - finalize one ahash_request if
  316. * the request is done
  317. * @engine: the hardware engine
  318. * @req: the request need to be finalized
  319. * @err: error number
  320. */
  321. void crypto_finalize_hash_request(struct crypto_engine *engine,
  322. struct ahash_request *req, int err)
  323. {
  324. return crypto_finalize_request(engine, &req->base, err);
  325. }
  326. EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
  327. /**
  328. * crypto_finalize_kpp_request - finalize one kpp_request if the request is done
  329. * @engine: the hardware engine
  330. * @req: the request need to be finalized
  331. * @err: error number
  332. */
  333. void crypto_finalize_kpp_request(struct crypto_engine *engine,
  334. struct kpp_request *req, int err)
  335. {
  336. return crypto_finalize_request(engine, &req->base, err);
  337. }
  338. EXPORT_SYMBOL_GPL(crypto_finalize_kpp_request);
  339. /**
  340. * crypto_finalize_skcipher_request - finalize one skcipher_request if
  341. * the request is done
  342. * @engine: the hardware engine
  343. * @req: the request need to be finalized
  344. * @err: error number
  345. */
  346. void crypto_finalize_skcipher_request(struct crypto_engine *engine,
  347. struct skcipher_request *req, int err)
  348. {
  349. return crypto_finalize_request(engine, &req->base, err);
  350. }
  351. EXPORT_SYMBOL_GPL(crypto_finalize_skcipher_request);
  352. /**
  353. * crypto_engine_start - start the hardware engine
  354. * @engine: the hardware engine need to be started
  355. *
  356. * Return 0 on success, else on fail.
  357. */
  358. int crypto_engine_start(struct crypto_engine *engine)
  359. {
  360. unsigned long flags;
  361. spin_lock_irqsave(&engine->queue_lock, flags);
  362. if (engine->running || engine->busy) {
  363. spin_unlock_irqrestore(&engine->queue_lock, flags);
  364. return -EBUSY;
  365. }
  366. engine->running = true;
  367. spin_unlock_irqrestore(&engine->queue_lock, flags);
  368. kthread_queue_work(engine->kworker, &engine->pump_requests);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL_GPL(crypto_engine_start);
  372. /**
  373. * crypto_engine_stop - stop the hardware engine
  374. * @engine: the hardware engine need to be stopped
  375. *
  376. * Return 0 on success, else on fail.
  377. */
  378. int crypto_engine_stop(struct crypto_engine *engine)
  379. {
  380. unsigned long flags;
  381. unsigned int limit = 500;
  382. int ret = 0;
  383. spin_lock_irqsave(&engine->queue_lock, flags);
  384. /*
  385. * If the engine queue is not empty or the engine is on busy state,
  386. * we need to wait for a while to pump the requests of engine queue.
  387. */
  388. while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
  389. spin_unlock_irqrestore(&engine->queue_lock, flags);
  390. msleep(20);
  391. spin_lock_irqsave(&engine->queue_lock, flags);
  392. }
  393. if (crypto_queue_len(&engine->queue) || engine->busy)
  394. ret = -EBUSY;
  395. else
  396. engine->running = false;
  397. spin_unlock_irqrestore(&engine->queue_lock, flags);
  398. if (ret)
  399. dev_warn(engine->dev, "could not stop engine\n");
  400. return ret;
  401. }
  402. EXPORT_SYMBOL_GPL(crypto_engine_stop);
  403. /**
  404. * crypto_engine_alloc_init_and_set - allocate crypto hardware engine structure
  405. * and initialize it by setting the maximum number of entries in the software
  406. * crypto-engine queue.
  407. * @dev: the device attached with one hardware engine
  408. * @retry_support: whether hardware has support for retry mechanism
  409. * @cbk_do_batch: pointer to a callback function to be invoked when executing
  410. * a batch of requests.
  411. * This has the form:
  412. * callback(struct crypto_engine *engine)
  413. * where:
  414. * engine: the crypto engine structure.
  415. * @rt: whether this queue is set to run as a realtime task
  416. * @qlen: maximum size of the crypto-engine queue
  417. *
  418. * This must be called from context that can sleep.
  419. * Return: the crypto engine structure on success, else NULL.
  420. */
  421. struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev,
  422. bool retry_support,
  423. int (*cbk_do_batch)(struct crypto_engine *engine),
  424. bool rt, int qlen)
  425. {
  426. struct crypto_engine *engine;
  427. if (!dev)
  428. return NULL;
  429. engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
  430. if (!engine)
  431. return NULL;
  432. engine->dev = dev;
  433. engine->rt = rt;
  434. engine->running = false;
  435. engine->busy = false;
  436. engine->idling = false;
  437. engine->retry_support = retry_support;
  438. engine->priv_data = dev;
  439. /*
  440. * Batch requests is possible only if
  441. * hardware has support for retry mechanism.
  442. */
  443. engine->do_batch_requests = retry_support ? cbk_do_batch : NULL;
  444. snprintf(engine->name, sizeof(engine->name),
  445. "%s-engine", dev_name(dev));
  446. crypto_init_queue(&engine->queue, qlen);
  447. spin_lock_init(&engine->queue_lock);
  448. engine->kworker = kthread_create_worker(0, "%s", engine->name);
  449. if (IS_ERR(engine->kworker)) {
  450. dev_err(dev, "failed to create crypto request pump task\n");
  451. return NULL;
  452. }
  453. kthread_init_work(&engine->pump_requests, crypto_pump_work);
  454. if (engine->rt) {
  455. dev_info(dev, "will run requests pump with realtime priority\n");
  456. sched_set_fifo(engine->kworker->task);
  457. }
  458. return engine;
  459. }
  460. EXPORT_SYMBOL_GPL(crypto_engine_alloc_init_and_set);
  461. /**
  462. * crypto_engine_alloc_init - allocate crypto hardware engine structure and
  463. * initialize it.
  464. * @dev: the device attached with one hardware engine
  465. * @rt: whether this queue is set to run as a realtime task
  466. *
  467. * This must be called from context that can sleep.
  468. * Return: the crypto engine structure on success, else NULL.
  469. */
  470. struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
  471. {
  472. return crypto_engine_alloc_init_and_set(dev, false, NULL, rt,
  473. CRYPTO_ENGINE_MAX_QLEN);
  474. }
  475. EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
  476. /**
  477. * crypto_engine_exit - free the resources of hardware engine when exit
  478. * @engine: the hardware engine need to be freed
  479. */
  480. void crypto_engine_exit(struct crypto_engine *engine)
  481. {
  482. int ret;
  483. ret = crypto_engine_stop(engine);
  484. if (ret)
  485. return;
  486. kthread_destroy_worker(engine->kworker);
  487. }
  488. EXPORT_SYMBOL_GPL(crypto_engine_exit);
  489. int crypto_engine_register_aead(struct aead_engine_alg *alg)
  490. {
  491. if (!alg->op.do_one_request)
  492. return -EINVAL;
  493. alg->base.base.cra_flags |= CRYPTO_ALG_ENGINE;
  494. return crypto_register_aead(&alg->base);
  495. }
  496. EXPORT_SYMBOL_GPL(crypto_engine_register_aead);
  497. void crypto_engine_unregister_aead(struct aead_engine_alg *alg)
  498. {
  499. crypto_unregister_aead(&alg->base);
  500. }
  501. EXPORT_SYMBOL_GPL(crypto_engine_unregister_aead);
  502. int crypto_engine_register_aeads(struct aead_engine_alg *algs, int count)
  503. {
  504. int i, ret;
  505. for (i = 0; i < count; i++) {
  506. ret = crypto_engine_register_aead(&algs[i]);
  507. if (ret)
  508. goto err;
  509. }
  510. return 0;
  511. err:
  512. crypto_engine_unregister_aeads(algs, i);
  513. return ret;
  514. }
  515. EXPORT_SYMBOL_GPL(crypto_engine_register_aeads);
  516. void crypto_engine_unregister_aeads(struct aead_engine_alg *algs, int count)
  517. {
  518. int i;
  519. for (i = count - 1; i >= 0; --i)
  520. crypto_engine_unregister_aead(&algs[i]);
  521. }
  522. EXPORT_SYMBOL_GPL(crypto_engine_unregister_aeads);
  523. int crypto_engine_register_ahash(struct ahash_engine_alg *alg)
  524. {
  525. if (!alg->op.do_one_request)
  526. return -EINVAL;
  527. alg->base.halg.base.cra_flags |= CRYPTO_ALG_ENGINE;
  528. return crypto_register_ahash(&alg->base);
  529. }
  530. EXPORT_SYMBOL_GPL(crypto_engine_register_ahash);
  531. void crypto_engine_unregister_ahash(struct ahash_engine_alg *alg)
  532. {
  533. crypto_unregister_ahash(&alg->base);
  534. }
  535. EXPORT_SYMBOL_GPL(crypto_engine_unregister_ahash);
  536. int crypto_engine_register_ahashes(struct ahash_engine_alg *algs, int count)
  537. {
  538. int i, ret;
  539. for (i = 0; i < count; i++) {
  540. ret = crypto_engine_register_ahash(&algs[i]);
  541. if (ret)
  542. goto err;
  543. }
  544. return 0;
  545. err:
  546. crypto_engine_unregister_ahashes(algs, i);
  547. return ret;
  548. }
  549. EXPORT_SYMBOL_GPL(crypto_engine_register_ahashes);
  550. void crypto_engine_unregister_ahashes(struct ahash_engine_alg *algs,
  551. int count)
  552. {
  553. int i;
  554. for (i = count - 1; i >= 0; --i)
  555. crypto_engine_unregister_ahash(&algs[i]);
  556. }
  557. EXPORT_SYMBOL_GPL(crypto_engine_unregister_ahashes);
  558. int crypto_engine_register_akcipher(struct akcipher_engine_alg *alg)
  559. {
  560. if (!alg->op.do_one_request)
  561. return -EINVAL;
  562. alg->base.base.cra_flags |= CRYPTO_ALG_ENGINE;
  563. return crypto_register_akcipher(&alg->base);
  564. }
  565. EXPORT_SYMBOL_GPL(crypto_engine_register_akcipher);
  566. void crypto_engine_unregister_akcipher(struct akcipher_engine_alg *alg)
  567. {
  568. crypto_unregister_akcipher(&alg->base);
  569. }
  570. EXPORT_SYMBOL_GPL(crypto_engine_unregister_akcipher);
  571. int crypto_engine_register_kpp(struct kpp_engine_alg *alg)
  572. {
  573. if (!alg->op.do_one_request)
  574. return -EINVAL;
  575. alg->base.base.cra_flags |= CRYPTO_ALG_ENGINE;
  576. return crypto_register_kpp(&alg->base);
  577. }
  578. EXPORT_SYMBOL_GPL(crypto_engine_register_kpp);
  579. void crypto_engine_unregister_kpp(struct kpp_engine_alg *alg)
  580. {
  581. crypto_unregister_kpp(&alg->base);
  582. }
  583. EXPORT_SYMBOL_GPL(crypto_engine_unregister_kpp);
  584. int crypto_engine_register_skcipher(struct skcipher_engine_alg *alg)
  585. {
  586. if (!alg->op.do_one_request)
  587. return -EINVAL;
  588. alg->base.base.cra_flags |= CRYPTO_ALG_ENGINE;
  589. return crypto_register_skcipher(&alg->base);
  590. }
  591. EXPORT_SYMBOL_GPL(crypto_engine_register_skcipher);
  592. void crypto_engine_unregister_skcipher(struct skcipher_engine_alg *alg)
  593. {
  594. return crypto_unregister_skcipher(&alg->base);
  595. }
  596. EXPORT_SYMBOL_GPL(crypto_engine_unregister_skcipher);
  597. int crypto_engine_register_skciphers(struct skcipher_engine_alg *algs,
  598. int count)
  599. {
  600. int i, ret;
  601. for (i = 0; i < count; i++) {
  602. ret = crypto_engine_register_skcipher(&algs[i]);
  603. if (ret)
  604. goto err;
  605. }
  606. return 0;
  607. err:
  608. crypto_engine_unregister_skciphers(algs, i);
  609. return ret;
  610. }
  611. EXPORT_SYMBOL_GPL(crypto_engine_register_skciphers);
  612. void crypto_engine_unregister_skciphers(struct skcipher_engine_alg *algs,
  613. int count)
  614. {
  615. int i;
  616. for (i = count - 1; i >= 0; --i)
  617. crypto_engine_unregister_skcipher(&algs[i]);
  618. }
  619. EXPORT_SYMBOL_GPL(crypto_engine_unregister_skciphers);
  620. MODULE_LICENSE("GPL");
  621. MODULE_DESCRIPTION("Crypto hardware engine framework");