^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Crypto Engine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) =============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) Overview
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) --------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) The crypto engine (CE) API is a crypto queue manager.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) Requirement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) -----------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) You must put, at the start of your transform context your_tfm_ctx, the structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) crypto_engine:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct your_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct crypto_engine engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) The crypto engine only manages asynchronous requests in the form of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) crypto_async_request. It cannot know the underlying request type and thus only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) has access to the transform structure. It is not possible to access the context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) using container_of. In addition, the engine knows nothing about your
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) structure "``struct your_tfm_ctx``". The engine assumes (requires) the placement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) of the known member ``struct crypto_engine`` at the beginning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) Order of operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) -------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) You are required to obtain a struct crypto_engine via ``crypto_engine_alloc_init()``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) Start it via ``crypto_engine_start()``. When finished with your work, shut down the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) engine using ``crypto_engine_stop()`` and destroy the engine with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ``crypto_engine_exit()``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) Before transferring any request, you have to fill the context enginectx by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) providing functions for the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * ``prepare_crypt_hardware``: Called once before any prepare functions are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * ``unprepare_crypt_hardware``: Called once after all unprepare functions have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) been called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * ``prepare_cipher_request``/``prepare_hash_request``: Called before each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) corresponding request is performed. If some processing or other preparatory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) work is required, do it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * ``unprepare_cipher_request``/``unprepare_hash_request``: Called after each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) request is handled. Clean up / undo what was done in the prepare function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * ``cipher_one_request``/``hash_one_request``: Handle the current request by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) performing the operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) Note that these functions access the crypto_async_request structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) associated with the received request. You are able to retrieve the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) request by using:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) container_of(areq, struct yourrequesttype_request, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) When your driver receives a crypto_request, you must to transfer it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) the crypto engine via one of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * crypto_transfer_aead_request_to_engine()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * crypto_transfer_akcipher_request_to_engine()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * crypto_transfer_hash_request_to_engine()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * crypto_transfer_skcipher_request_to_engine()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) At the end of the request process, a call to one of the following functions is needed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * crypto_finalize_aead_request()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * crypto_finalize_akcipher_request()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * crypto_finalize_hash_request()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * crypto_finalize_skcipher_request()