162306a36Sopenharmony_ciKernel Crypto API Architecture
262306a36Sopenharmony_ci==============================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciCipher algorithm types
562306a36Sopenharmony_ci----------------------
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciThe kernel crypto API provides different API calls for the following
862306a36Sopenharmony_cicipher types:
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci-  Symmetric ciphers
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci-  AEAD ciphers
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci-  Message digest, including keyed message digest
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci-  Random number generation
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci-  User space interface
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciCiphers And Templates
2162306a36Sopenharmony_ci---------------------
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciThe kernel crypto API provides implementations of single block ciphers
2462306a36Sopenharmony_ciand message digests. In addition, the kernel crypto API provides
2562306a36Sopenharmony_cinumerous "templates" that can be used in conjunction with the single
2662306a36Sopenharmony_ciblock ciphers and message digests. Templates include all types of block
2762306a36Sopenharmony_cichaining mode, the HMAC mechanism, etc.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciSingle block ciphers and message digests can either be directly used by
3062306a36Sopenharmony_cia caller or invoked together with a template to form multi-block ciphers
3162306a36Sopenharmony_cior keyed message digests.
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciA single block cipher may even be called with multiple templates.
3462306a36Sopenharmony_ciHowever, templates cannot be used without a single cipher.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciSee /proc/crypto and search for "name". For example:
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci-  aes
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci-  ecb(aes)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci-  cmac(aes)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci-  ccm(aes)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci-  rfc4106(gcm(aes))
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci-  sha1
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci-  hmac(sha1)
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci-  authenc(hmac(sha1),cbc(aes))
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciIn these examples, "aes" and "sha1" are the ciphers and all others are
5562306a36Sopenharmony_cithe templates.
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciSynchronous And Asynchronous Operation
5862306a36Sopenharmony_ci--------------------------------------
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciThe kernel crypto API provides synchronous and asynchronous API
6162306a36Sopenharmony_cioperations.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciWhen using the synchronous API operation, the caller invokes a cipher
6462306a36Sopenharmony_cioperation which is performed synchronously by the kernel crypto API.
6562306a36Sopenharmony_ciThat means, the caller waits until the cipher operation completes.
6662306a36Sopenharmony_ciTherefore, the kernel crypto API calls work like regular function calls.
6762306a36Sopenharmony_ciFor synchronous operation, the set of API calls is small and
6862306a36Sopenharmony_ciconceptually similar to any other crypto library.
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciAsynchronous operation is provided by the kernel crypto API which
7162306a36Sopenharmony_ciimplies that the invocation of a cipher operation will complete almost
7262306a36Sopenharmony_ciinstantly. That invocation triggers the cipher operation but it does not
7362306a36Sopenharmony_cisignal its completion. Before invoking a cipher operation, the caller
7462306a36Sopenharmony_cimust provide a callback function the kernel crypto API can invoke to
7562306a36Sopenharmony_cisignal the completion of the cipher operation. Furthermore, the caller
7662306a36Sopenharmony_cimust ensure it can handle such asynchronous events by applying
7762306a36Sopenharmony_ciappropriate locking around its data. The kernel crypto API does not
7862306a36Sopenharmony_ciperform any special serialization operation to protect the caller's data
7962306a36Sopenharmony_ciintegrity.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciCrypto API Cipher References And Priority
8262306a36Sopenharmony_ci-----------------------------------------
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciA cipher is referenced by the caller with a string. That string has the
8562306a36Sopenharmony_cifollowing semantics:
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci::
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci        template(single block cipher)
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciwhere "template" and "single block cipher" is the aforementioned
9362306a36Sopenharmony_citemplate and single block cipher, respectively. If applicable,
9462306a36Sopenharmony_ciadditional templates may enclose other templates, such as
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci::
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci        template1(template2(single block cipher)))
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciThe kernel crypto API may provide multiple implementations of a template
10262306a36Sopenharmony_cior a single block cipher. For example, AES on newer Intel hardware has
10362306a36Sopenharmony_cithe following implementations: AES-NI, assembler implementation, or
10462306a36Sopenharmony_cistraight C. Now, when using the string "aes" with the kernel crypto API,
10562306a36Sopenharmony_ciwhich cipher implementation is used? The answer to that question is the
10662306a36Sopenharmony_cipriority number assigned to each cipher implementation by the kernel
10762306a36Sopenharmony_cicrypto API. When a caller uses the string to refer to a cipher during
10862306a36Sopenharmony_ciinitialization of a cipher handle, the kernel crypto API looks up all
10962306a36Sopenharmony_ciimplementations providing an implementation with that name and selects
11062306a36Sopenharmony_cithe implementation with the highest priority.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciNow, a caller may have the need to refer to a specific cipher
11362306a36Sopenharmony_ciimplementation and thus does not want to rely on the priority-based
11462306a36Sopenharmony_ciselection. To accommodate this scenario, the kernel crypto API allows
11562306a36Sopenharmony_cithe cipher implementation to register a unique name in addition to
11662306a36Sopenharmony_cicommon names. When using that unique name, a caller is therefore always
11762306a36Sopenharmony_cisure to refer to the intended cipher implementation.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciThe list of available ciphers is given in /proc/crypto. However, that
12062306a36Sopenharmony_cilist does not specify all possible permutations of templates and
12162306a36Sopenharmony_ciciphers. Each block listed in /proc/crypto may contain the following
12262306a36Sopenharmony_ciinformation -- if one of the components listed as follows are not
12362306a36Sopenharmony_ciapplicable to a cipher, it is not displayed:
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci-  name: the generic name of the cipher that is subject to the
12662306a36Sopenharmony_ci   priority-based selection -- this name can be used by the cipher
12762306a36Sopenharmony_ci   allocation API calls (all names listed above are examples for such
12862306a36Sopenharmony_ci   generic names)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci-  driver: the unique name of the cipher -- this name can be used by the
13162306a36Sopenharmony_ci   cipher allocation API calls
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci-  module: the kernel module providing the cipher implementation (or
13462306a36Sopenharmony_ci   "kernel" for statically linked ciphers)
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci-  priority: the priority value of the cipher implementation
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci-  refcnt: the reference count of the respective cipher (i.e. the number
13962306a36Sopenharmony_ci   of current consumers of this cipher)
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci-  selftest: specification whether the self test for the cipher passed
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci-  type:
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci   -  skcipher for symmetric key ciphers
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci   -  cipher for single block ciphers that may be used with an
14862306a36Sopenharmony_ci      additional template
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci   -  shash for synchronous message digest
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci   -  ahash for asynchronous message digest
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci   -  aead for AEAD cipher type
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci   -  compression for compression type transformations
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci   -  rng for random number generator
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci   -  kpp for a Key-agreement Protocol Primitive (KPP) cipher such as
16162306a36Sopenharmony_ci      an ECDH or DH implementation
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci-  blocksize: blocksize of cipher in bytes
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci-  keysize: key size in bytes
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci-  ivsize: IV size in bytes
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci-  seedsize: required size of seed data for random number generator
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci-  digestsize: output size of the message digest
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci-  geniv: IV generator (obsolete)
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciKey Sizes
17662306a36Sopenharmony_ci---------
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciWhen allocating a cipher handle, the caller only specifies the cipher
17962306a36Sopenharmony_citype. Symmetric ciphers, however, typically support multiple key sizes
18062306a36Sopenharmony_ci(e.g. AES-128 vs. AES-192 vs. AES-256). These key sizes are determined
18162306a36Sopenharmony_ciwith the length of the provided key. Thus, the kernel crypto API does
18262306a36Sopenharmony_cinot provide a separate way to select the particular symmetric cipher key
18362306a36Sopenharmony_cisize.
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciCipher Allocation Type And Masks
18662306a36Sopenharmony_ci--------------------------------
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciThe different cipher handle allocation functions allow the specification
18962306a36Sopenharmony_ciof a type and mask flag. Both parameters have the following meaning (and
19062306a36Sopenharmony_ciare therefore not covered in the subsequent sections).
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciThe type flag specifies the type of the cipher algorithm. The caller
19362306a36Sopenharmony_ciusually provides a 0 when the caller wants the default handling.
19462306a36Sopenharmony_ciOtherwise, the caller may provide the following selections which match
19562306a36Sopenharmony_cithe aforementioned cipher types:
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_CIPHER Single block cipher
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_COMPRESS Compression
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_AEAD Authenticated Encryption with Associated Data
20262306a36Sopenharmony_ci   (MAC)
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_KPP Key-agreement Protocol Primitive (KPP) such as
20562306a36Sopenharmony_ci   an ECDH or DH implementation
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_HASH Raw message digest
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_AHASH Asynchronous multi-block hash
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_RNG Random Number Generation
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_AKCIPHER Asymmetric cipher
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci-  CRYPTO_ALG_TYPE_PCOMPRESS Enhanced version of
21862306a36Sopenharmony_ci   CRYPTO_ALG_TYPE_COMPRESS allowing for segmented compression /
21962306a36Sopenharmony_ci   decompression instead of performing the operation on one segment
22062306a36Sopenharmony_ci   only. CRYPTO_ALG_TYPE_PCOMPRESS is intended to replace
22162306a36Sopenharmony_ci   CRYPTO_ALG_TYPE_COMPRESS once existing consumers are converted.
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciThe mask flag restricts the type of cipher. The only allowed flag is
22462306a36Sopenharmony_ciCRYPTO_ALG_ASYNC to restrict the cipher lookup function to
22562306a36Sopenharmony_ciasynchronous ciphers. Usually, a caller provides a 0 for the mask flag.
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciWhen the caller provides a mask and type specification, the caller
22862306a36Sopenharmony_cilimits the search the kernel crypto API can perform for a suitable
22962306a36Sopenharmony_cicipher implementation for the given cipher name. That means, even when a
23062306a36Sopenharmony_cicaller uses a cipher name that exists during its initialization call,
23162306a36Sopenharmony_cithe kernel crypto API may not select it due to the used type and mask
23262306a36Sopenharmony_cifield.
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciInternal Structure of Kernel Crypto API
23562306a36Sopenharmony_ci---------------------------------------
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ciThe kernel crypto API has an internal structure where a cipher
23862306a36Sopenharmony_ciimplementation may use many layers and indirections. This section shall
23962306a36Sopenharmony_cihelp to clarify how the kernel crypto API uses various components to
24062306a36Sopenharmony_ciimplement the complete cipher.
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ciThe following subsections explain the internal structure based on
24362306a36Sopenharmony_ciexisting cipher implementations. The first section addresses the most
24462306a36Sopenharmony_cicomplex scenario where all other scenarios form a logical subset.
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ciGeneric AEAD Cipher Structure
24762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ciThe following ASCII art decomposes the kernel crypto API layers when
25062306a36Sopenharmony_ciusing the AEAD cipher with the automated IV generation. The shown
25162306a36Sopenharmony_ciexample is used by the IPSEC layer.
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciFor other use cases of AEAD ciphers, the ASCII art applies as well, but
25462306a36Sopenharmony_cithe caller may not use the AEAD cipher with a separate IV generator. In
25562306a36Sopenharmony_cithis case, the caller must generate the IV.
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ciThe depicted example decomposes the AEAD cipher of GCM(AES) based on the
25862306a36Sopenharmony_cigeneric C implementations (gcm.c, aes-generic.c, ctr.c, ghash-generic.c,
25962306a36Sopenharmony_ciseqiv.c). The generic implementation serves as an example showing the
26062306a36Sopenharmony_cicomplete logic of the kernel crypto API.
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ciIt is possible that some streamlined cipher implementations (like
26362306a36Sopenharmony_ciAES-NI) provide implementations merging aspects which in the view of the
26462306a36Sopenharmony_cikernel crypto API cannot be decomposed into layers any more. In case of
26562306a36Sopenharmony_cithe AES-NI implementation, the CTR mode, the GHASH implementation and
26662306a36Sopenharmony_cithe AES cipher are all merged into one cipher implementation registered
26762306a36Sopenharmony_ciwith the kernel crypto API. In this case, the concept described by the
26862306a36Sopenharmony_cifollowing ASCII art applies too. However, the decomposition of GCM into
26962306a36Sopenharmony_cithe individual sub-components by the kernel crypto API is not done any
27062306a36Sopenharmony_cimore.
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ciEach block in the following ASCII art is an independent cipher instance
27362306a36Sopenharmony_ciobtained from the kernel crypto API. Each block is accessed by the
27462306a36Sopenharmony_cicaller or by other blocks using the API functions defined by the kernel
27562306a36Sopenharmony_cicrypto API for the cipher implementation type.
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ciThe blocks below indicate the cipher type as well as the specific logic
27862306a36Sopenharmony_ciimplemented in the cipher.
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ciThe ASCII art picture also indicates the call structure, i.e. who calls
28162306a36Sopenharmony_ciwhich component. The arrows point to the invoked block where the caller
28262306a36Sopenharmony_ciuses the API applicable to the cipher type specified for the block.
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci::
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci    kernel crypto API                                |   IPSEC Layer
28862306a36Sopenharmony_ci                                                     |
28962306a36Sopenharmony_ci    +-----------+                                    |
29062306a36Sopenharmony_ci    |           |            (1)
29162306a36Sopenharmony_ci    |   aead    | <-----------------------------------  esp_output
29262306a36Sopenharmony_ci    |  (seqiv)  | ---+
29362306a36Sopenharmony_ci    +-----------+    |
29462306a36Sopenharmony_ci                     | (2)
29562306a36Sopenharmony_ci    +-----------+    |
29662306a36Sopenharmony_ci    |           | <--+                (2)
29762306a36Sopenharmony_ci    |   aead    | <-----------------------------------  esp_input
29862306a36Sopenharmony_ci    |   (gcm)   | ------------+
29962306a36Sopenharmony_ci    +-----------+             |
30062306a36Sopenharmony_ci          | (3)               | (5)
30162306a36Sopenharmony_ci          v                   v
30262306a36Sopenharmony_ci    +-----------+       +-----------+
30362306a36Sopenharmony_ci    |           |       |           |
30462306a36Sopenharmony_ci    |  skcipher |       |   ahash   |
30562306a36Sopenharmony_ci    |   (ctr)   | ---+  |  (ghash)  |
30662306a36Sopenharmony_ci    +-----------+    |  +-----------+
30762306a36Sopenharmony_ci                     |
30862306a36Sopenharmony_ci    +-----------+    | (4)
30962306a36Sopenharmony_ci    |           | <--+
31062306a36Sopenharmony_ci    |   cipher  |
31162306a36Sopenharmony_ci    |   (aes)   |
31262306a36Sopenharmony_ci    +-----------+
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ciThe following call sequence is applicable when the IPSEC layer triggers
31762306a36Sopenharmony_cian encryption operation with the esp_output function. During
31862306a36Sopenharmony_ciconfiguration, the administrator set up the use of seqiv(rfc4106(gcm(aes)))
31962306a36Sopenharmony_cias the cipher for ESP. The following call sequence is now depicted in
32062306a36Sopenharmony_cithe ASCII art above:
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci1. esp_output() invokes crypto_aead_encrypt() to trigger an
32362306a36Sopenharmony_ci   encryption operation of the AEAD cipher with IV generator.
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci   The SEQIV generates the IV.
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci2. Now, SEQIV uses the AEAD API function calls to invoke the associated
32862306a36Sopenharmony_ci   AEAD cipher. In our case, during the instantiation of SEQIV, the
32962306a36Sopenharmony_ci   cipher handle for GCM is provided to SEQIV. This means that SEQIV
33062306a36Sopenharmony_ci   invokes AEAD cipher operations with the GCM cipher handle.
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci   During instantiation of the GCM handle, the CTR(AES) and GHASH
33362306a36Sopenharmony_ci   ciphers are instantiated. The cipher handles for CTR(AES) and GHASH
33462306a36Sopenharmony_ci   are retained for later use.
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci   The GCM implementation is responsible to invoke the CTR mode AES and
33762306a36Sopenharmony_ci   the GHASH cipher in the right manner to implement the GCM
33862306a36Sopenharmony_ci   specification.
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci3. The GCM AEAD cipher type implementation now invokes the SKCIPHER API
34162306a36Sopenharmony_ci   with the instantiated CTR(AES) cipher handle.
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci   During instantiation of the CTR(AES) cipher, the CIPHER type
34462306a36Sopenharmony_ci   implementation of AES is instantiated. The cipher handle for AES is
34562306a36Sopenharmony_ci   retained.
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci   That means that the SKCIPHER implementation of CTR(AES) only
34862306a36Sopenharmony_ci   implements the CTR block chaining mode. After performing the block
34962306a36Sopenharmony_ci   chaining operation, the CIPHER implementation of AES is invoked.
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci4. The SKCIPHER of CTR(AES) now invokes the CIPHER API with the AES
35262306a36Sopenharmony_ci   cipher handle to encrypt one block.
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci5. The GCM AEAD implementation also invokes the GHASH cipher
35562306a36Sopenharmony_ci   implementation via the AHASH API.
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ciWhen the IPSEC layer triggers the esp_input() function, the same call
35862306a36Sopenharmony_cisequence is followed with the only difference that the operation starts
35962306a36Sopenharmony_ciwith step (2).
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ciGeneric Block Cipher Structure
36262306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ciGeneric block ciphers follow the same concept as depicted with the ASCII
36562306a36Sopenharmony_ciart picture above.
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ciFor example, CBC(AES) is implemented with cbc.c, and aes-generic.c. The
36862306a36Sopenharmony_ciASCII art picture above applies as well with the difference that only
36962306a36Sopenharmony_cistep (4) is used and the SKCIPHER block chaining mode is CBC.
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ciGeneric Keyed Message Digest Structure
37262306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ciKeyed message digest implementations again follow the same concept as
37562306a36Sopenharmony_cidepicted in the ASCII art picture above.
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ciFor example, HMAC(SHA256) is implemented with hmac.c and
37862306a36Sopenharmony_cisha256_generic.c. The following ASCII art illustrates the
37962306a36Sopenharmony_ciimplementation:
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci::
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci    kernel crypto API            |       Caller
38562306a36Sopenharmony_ci                                 |
38662306a36Sopenharmony_ci    +-----------+         (1)    |
38762306a36Sopenharmony_ci    |           | <------------------  some_function
38862306a36Sopenharmony_ci    |   ahash   |
38962306a36Sopenharmony_ci    |   (hmac)  | ---+
39062306a36Sopenharmony_ci    +-----------+    |
39162306a36Sopenharmony_ci                     | (2)
39262306a36Sopenharmony_ci    +-----------+    |
39362306a36Sopenharmony_ci    |           | <--+
39462306a36Sopenharmony_ci    |   shash   |
39562306a36Sopenharmony_ci    |  (sha256) |
39662306a36Sopenharmony_ci    +-----------+
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ciThe following call sequence is applicable when a caller triggers an HMAC
40162306a36Sopenharmony_cioperation:
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci1. The AHASH API functions are invoked by the caller. The HMAC
40462306a36Sopenharmony_ci   implementation performs its operation as needed.
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci   During initialization of the HMAC cipher, the SHASH cipher type of
40762306a36Sopenharmony_ci   SHA256 is instantiated. The cipher handle for the SHA256 instance is
40862306a36Sopenharmony_ci   retained.
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci   At one time, the HMAC implementation requires a SHA256 operation
41162306a36Sopenharmony_ci   where the SHA256 cipher handle is used.
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci2. The HMAC instance now invokes the SHASH API with the SHA256 cipher
41462306a36Sopenharmony_ci   handle to calculate the message digest.
415