1*4882a593SmuzhiyunKernel Crypto API Architecture 2*4882a593Smuzhiyun============================== 3*4882a593Smuzhiyun 4*4882a593SmuzhiyunCipher algorithm types 5*4882a593Smuzhiyun---------------------- 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunThe kernel crypto API provides different API calls for the following 8*4882a593Smuzhiyuncipher types: 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun- Symmetric ciphers 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun- AEAD ciphers 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun- Message digest, including keyed message digest 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun- Random number generation 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun- User space interface 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunCiphers And Templates 21*4882a593Smuzhiyun--------------------- 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunThe kernel crypto API provides implementations of single block ciphers 24*4882a593Smuzhiyunand message digests. In addition, the kernel crypto API provides 25*4882a593Smuzhiyunnumerous "templates" that can be used in conjunction with the single 26*4882a593Smuzhiyunblock ciphers and message digests. Templates include all types of block 27*4882a593Smuzhiyunchaining mode, the HMAC mechanism, etc. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunSingle block ciphers and message digests can either be directly used by 30*4882a593Smuzhiyuna caller or invoked together with a template to form multi-block ciphers 31*4882a593Smuzhiyunor keyed message digests. 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunA single block cipher may even be called with multiple templates. 34*4882a593SmuzhiyunHowever, templates cannot be used without a single cipher. 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunSee /proc/crypto and search for "name". For example: 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun- aes 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun- ecb(aes) 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun- cmac(aes) 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun- ccm(aes) 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun- rfc4106(gcm(aes)) 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun- sha1 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun- hmac(sha1) 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun- authenc(hmac(sha1),cbc(aes)) 53*4882a593Smuzhiyun 54*4882a593SmuzhiyunIn these examples, "aes" and "sha1" are the ciphers and all others are 55*4882a593Smuzhiyunthe templates. 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunSynchronous And Asynchronous Operation 58*4882a593Smuzhiyun-------------------------------------- 59*4882a593Smuzhiyun 60*4882a593SmuzhiyunThe kernel crypto API provides synchronous and asynchronous API 61*4882a593Smuzhiyunoperations. 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunWhen using the synchronous API operation, the caller invokes a cipher 64*4882a593Smuzhiyunoperation which is performed synchronously by the kernel crypto API. 65*4882a593SmuzhiyunThat means, the caller waits until the cipher operation completes. 66*4882a593SmuzhiyunTherefore, the kernel crypto API calls work like regular function calls. 67*4882a593SmuzhiyunFor synchronous operation, the set of API calls is small and 68*4882a593Smuzhiyunconceptually similar to any other crypto library. 69*4882a593Smuzhiyun 70*4882a593SmuzhiyunAsynchronous operation is provided by the kernel crypto API which 71*4882a593Smuzhiyunimplies that the invocation of a cipher operation will complete almost 72*4882a593Smuzhiyuninstantly. That invocation triggers the cipher operation but it does not 73*4882a593Smuzhiyunsignal its completion. Before invoking a cipher operation, the caller 74*4882a593Smuzhiyunmust provide a callback function the kernel crypto API can invoke to 75*4882a593Smuzhiyunsignal the completion of the cipher operation. Furthermore, the caller 76*4882a593Smuzhiyunmust ensure it can handle such asynchronous events by applying 77*4882a593Smuzhiyunappropriate locking around its data. The kernel crypto API does not 78*4882a593Smuzhiyunperform any special serialization operation to protect the caller's data 79*4882a593Smuzhiyunintegrity. 80*4882a593Smuzhiyun 81*4882a593SmuzhiyunCrypto API Cipher References And Priority 82*4882a593Smuzhiyun----------------------------------------- 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunA cipher is referenced by the caller with a string. That string has the 85*4882a593Smuzhiyunfollowing semantics: 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun:: 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun template(single block cipher) 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun 92*4882a593Smuzhiyunwhere "template" and "single block cipher" is the aforementioned 93*4882a593Smuzhiyuntemplate and single block cipher, respectively. If applicable, 94*4882a593Smuzhiyunadditional templates may enclose other templates, such as 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun:: 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun template1(template2(single block cipher))) 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunThe kernel crypto API may provide multiple implementations of a template 102*4882a593Smuzhiyunor a single block cipher. For example, AES on newer Intel hardware has 103*4882a593Smuzhiyunthe following implementations: AES-NI, assembler implementation, or 104*4882a593Smuzhiyunstraight C. Now, when using the string "aes" with the kernel crypto API, 105*4882a593Smuzhiyunwhich cipher implementation is used? The answer to that question is the 106*4882a593Smuzhiyunpriority number assigned to each cipher implementation by the kernel 107*4882a593Smuzhiyuncrypto API. When a caller uses the string to refer to a cipher during 108*4882a593Smuzhiyuninitialization of a cipher handle, the kernel crypto API looks up all 109*4882a593Smuzhiyunimplementations providing an implementation with that name and selects 110*4882a593Smuzhiyunthe implementation with the highest priority. 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunNow, a caller may have the need to refer to a specific cipher 113*4882a593Smuzhiyunimplementation and thus does not want to rely on the priority-based 114*4882a593Smuzhiyunselection. To accommodate this scenario, the kernel crypto API allows 115*4882a593Smuzhiyunthe cipher implementation to register a unique name in addition to 116*4882a593Smuzhiyuncommon names. When using that unique name, a caller is therefore always 117*4882a593Smuzhiyunsure to refer to the intended cipher implementation. 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunThe list of available ciphers is given in /proc/crypto. However, that 120*4882a593Smuzhiyunlist does not specify all possible permutations of templates and 121*4882a593Smuzhiyunciphers. Each block listed in /proc/crypto may contain the following 122*4882a593Smuzhiyuninformation -- if one of the components listed as follows are not 123*4882a593Smuzhiyunapplicable to a cipher, it is not displayed: 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun- name: the generic name of the cipher that is subject to the 126*4882a593Smuzhiyun priority-based selection -- this name can be used by the cipher 127*4882a593Smuzhiyun allocation API calls (all names listed above are examples for such 128*4882a593Smuzhiyun generic names) 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun- driver: the unique name of the cipher -- this name can be used by the 131*4882a593Smuzhiyun cipher allocation API calls 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun- module: the kernel module providing the cipher implementation (or 134*4882a593Smuzhiyun "kernel" for statically linked ciphers) 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun- priority: the priority value of the cipher implementation 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun- refcnt: the reference count of the respective cipher (i.e. the number 139*4882a593Smuzhiyun of current consumers of this cipher) 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun- selftest: specification whether the self test for the cipher passed 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun- type: 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun - skcipher for symmetric key ciphers 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun - cipher for single block ciphers that may be used with an 148*4882a593Smuzhiyun additional template 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun - shash for synchronous message digest 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun - ahash for asynchronous message digest 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun - aead for AEAD cipher type 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun - compression for compression type transformations 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun - rng for random number generator 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun - kpp for a Key-agreement Protocol Primitive (KPP) cipher such as 161*4882a593Smuzhiyun an ECDH or DH implementation 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun- blocksize: blocksize of cipher in bytes 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun- keysize: key size in bytes 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun- ivsize: IV size in bytes 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun- seedsize: required size of seed data for random number generator 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun- digestsize: output size of the message digest 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun- geniv: IV generator (obsolete) 174*4882a593Smuzhiyun 175*4882a593SmuzhiyunKey Sizes 176*4882a593Smuzhiyun--------- 177*4882a593Smuzhiyun 178*4882a593SmuzhiyunWhen allocating a cipher handle, the caller only specifies the cipher 179*4882a593Smuzhiyuntype. Symmetric ciphers, however, typically support multiple key sizes 180*4882a593Smuzhiyun(e.g. AES-128 vs. AES-192 vs. AES-256). These key sizes are determined 181*4882a593Smuzhiyunwith the length of the provided key. Thus, the kernel crypto API does 182*4882a593Smuzhiyunnot provide a separate way to select the particular symmetric cipher key 183*4882a593Smuzhiyunsize. 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunCipher Allocation Type And Masks 186*4882a593Smuzhiyun-------------------------------- 187*4882a593Smuzhiyun 188*4882a593SmuzhiyunThe different cipher handle allocation functions allow the specification 189*4882a593Smuzhiyunof a type and mask flag. Both parameters have the following meaning (and 190*4882a593Smuzhiyunare therefore not covered in the subsequent sections). 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunThe type flag specifies the type of the cipher algorithm. The caller 193*4882a593Smuzhiyunusually provides a 0 when the caller wants the default handling. 194*4882a593SmuzhiyunOtherwise, the caller may provide the following selections which match 195*4882a593Smuzhiyunthe aforementioned cipher types: 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_CIPHER Single block cipher 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_COMPRESS Compression 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_AEAD Authenticated Encryption with Associated Data 202*4882a593Smuzhiyun (MAC) 203*4882a593Smuzhiyun 204*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_KPP Key-agreement Protocol Primitive (KPP) such as 205*4882a593Smuzhiyun an ECDH or DH implementation 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_HASH Raw message digest 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash 210*4882a593Smuzhiyun 211*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_AHASH Asynchronous multi-block hash 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_RNG Random Number Generation 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_AKCIPHER Asymmetric cipher 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun- CRYPTO_ALG_TYPE_PCOMPRESS Enhanced version of 218*4882a593Smuzhiyun CRYPTO_ALG_TYPE_COMPRESS allowing for segmented compression / 219*4882a593Smuzhiyun decompression instead of performing the operation on one segment 220*4882a593Smuzhiyun only. CRYPTO_ALG_TYPE_PCOMPRESS is intended to replace 221*4882a593Smuzhiyun CRYPTO_ALG_TYPE_COMPRESS once existing consumers are converted. 222*4882a593Smuzhiyun 223*4882a593SmuzhiyunThe mask flag restricts the type of cipher. The only allowed flag is 224*4882a593SmuzhiyunCRYPTO_ALG_ASYNC to restrict the cipher lookup function to 225*4882a593Smuzhiyunasynchronous ciphers. Usually, a caller provides a 0 for the mask flag. 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunWhen the caller provides a mask and type specification, the caller 228*4882a593Smuzhiyunlimits the search the kernel crypto API can perform for a suitable 229*4882a593Smuzhiyuncipher implementation for the given cipher name. That means, even when a 230*4882a593Smuzhiyuncaller uses a cipher name that exists during its initialization call, 231*4882a593Smuzhiyunthe kernel crypto API may not select it due to the used type and mask 232*4882a593Smuzhiyunfield. 233*4882a593Smuzhiyun 234*4882a593SmuzhiyunInternal Structure of Kernel Crypto API 235*4882a593Smuzhiyun--------------------------------------- 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunThe kernel crypto API has an internal structure where a cipher 238*4882a593Smuzhiyunimplementation may use many layers and indirections. This section shall 239*4882a593Smuzhiyunhelp to clarify how the kernel crypto API uses various components to 240*4882a593Smuzhiyunimplement the complete cipher. 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunThe following subsections explain the internal structure based on 243*4882a593Smuzhiyunexisting cipher implementations. The first section addresses the most 244*4882a593Smuzhiyuncomplex scenario where all other scenarios form a logical subset. 245*4882a593Smuzhiyun 246*4882a593SmuzhiyunGeneric AEAD Cipher Structure 247*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 248*4882a593Smuzhiyun 249*4882a593SmuzhiyunThe following ASCII art decomposes the kernel crypto API layers when 250*4882a593Smuzhiyunusing the AEAD cipher with the automated IV generation. The shown 251*4882a593Smuzhiyunexample is used by the IPSEC layer. 252*4882a593Smuzhiyun 253*4882a593SmuzhiyunFor other use cases of AEAD ciphers, the ASCII art applies as well, but 254*4882a593Smuzhiyunthe caller may not use the AEAD cipher with a separate IV generator. In 255*4882a593Smuzhiyunthis case, the caller must generate the IV. 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunThe depicted example decomposes the AEAD cipher of GCM(AES) based on the 258*4882a593Smuzhiyungeneric C implementations (gcm.c, aes-generic.c, ctr.c, ghash-generic.c, 259*4882a593Smuzhiyunseqiv.c). The generic implementation serves as an example showing the 260*4882a593Smuzhiyuncomplete logic of the kernel crypto API. 261*4882a593Smuzhiyun 262*4882a593SmuzhiyunIt is possible that some streamlined cipher implementations (like 263*4882a593SmuzhiyunAES-NI) provide implementations merging aspects which in the view of the 264*4882a593Smuzhiyunkernel crypto API cannot be decomposed into layers any more. In case of 265*4882a593Smuzhiyunthe AES-NI implementation, the CTR mode, the GHASH implementation and 266*4882a593Smuzhiyunthe AES cipher are all merged into one cipher implementation registered 267*4882a593Smuzhiyunwith the kernel crypto API. In this case, the concept described by the 268*4882a593Smuzhiyunfollowing ASCII art applies too. However, the decomposition of GCM into 269*4882a593Smuzhiyunthe individual sub-components by the kernel crypto API is not done any 270*4882a593Smuzhiyunmore. 271*4882a593Smuzhiyun 272*4882a593SmuzhiyunEach block in the following ASCII art is an independent cipher instance 273*4882a593Smuzhiyunobtained from the kernel crypto API. Each block is accessed by the 274*4882a593Smuzhiyuncaller or by other blocks using the API functions defined by the kernel 275*4882a593Smuzhiyuncrypto API for the cipher implementation type. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunThe blocks below indicate the cipher type as well as the specific logic 278*4882a593Smuzhiyunimplemented in the cipher. 279*4882a593Smuzhiyun 280*4882a593SmuzhiyunThe ASCII art picture also indicates the call structure, i.e. who calls 281*4882a593Smuzhiyunwhich component. The arrows point to the invoked block where the caller 282*4882a593Smuzhiyunuses the API applicable to the cipher type specified for the block. 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun:: 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun 287*4882a593Smuzhiyun kernel crypto API | IPSEC Layer 288*4882a593Smuzhiyun | 289*4882a593Smuzhiyun +-----------+ | 290*4882a593Smuzhiyun | | (1) 291*4882a593Smuzhiyun | aead | <----------------------------------- esp_output 292*4882a593Smuzhiyun | (seqiv) | ---+ 293*4882a593Smuzhiyun +-----------+ | 294*4882a593Smuzhiyun | (2) 295*4882a593Smuzhiyun +-----------+ | 296*4882a593Smuzhiyun | | <--+ (2) 297*4882a593Smuzhiyun | aead | <----------------------------------- esp_input 298*4882a593Smuzhiyun | (gcm) | ------------+ 299*4882a593Smuzhiyun +-----------+ | 300*4882a593Smuzhiyun | (3) | (5) 301*4882a593Smuzhiyun v v 302*4882a593Smuzhiyun +-----------+ +-----------+ 303*4882a593Smuzhiyun | | | | 304*4882a593Smuzhiyun | skcipher | | ahash | 305*4882a593Smuzhiyun | (ctr) | ---+ | (ghash) | 306*4882a593Smuzhiyun +-----------+ | +-----------+ 307*4882a593Smuzhiyun | 308*4882a593Smuzhiyun +-----------+ | (4) 309*4882a593Smuzhiyun | | <--+ 310*4882a593Smuzhiyun | cipher | 311*4882a593Smuzhiyun | (aes) | 312*4882a593Smuzhiyun +-----------+ 313*4882a593Smuzhiyun 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun 316*4882a593SmuzhiyunThe following call sequence is applicable when the IPSEC layer triggers 317*4882a593Smuzhiyunan encryption operation with the esp_output function. During 318*4882a593Smuzhiyunconfiguration, the administrator set up the use of seqiv(rfc4106(gcm(aes))) 319*4882a593Smuzhiyunas the cipher for ESP. The following call sequence is now depicted in 320*4882a593Smuzhiyunthe ASCII art above: 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun1. esp_output() invokes crypto_aead_encrypt() to trigger an 323*4882a593Smuzhiyun encryption operation of the AEAD cipher with IV generator. 324*4882a593Smuzhiyun 325*4882a593Smuzhiyun The SEQIV generates the IV. 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun2. Now, SEQIV uses the AEAD API function calls to invoke the associated 328*4882a593Smuzhiyun AEAD cipher. In our case, during the instantiation of SEQIV, the 329*4882a593Smuzhiyun cipher handle for GCM is provided to SEQIV. This means that SEQIV 330*4882a593Smuzhiyun invokes AEAD cipher operations with the GCM cipher handle. 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun During instantiation of the GCM handle, the CTR(AES) and GHASH 333*4882a593Smuzhiyun ciphers are instantiated. The cipher handles for CTR(AES) and GHASH 334*4882a593Smuzhiyun are retained for later use. 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun The GCM implementation is responsible to invoke the CTR mode AES and 337*4882a593Smuzhiyun the GHASH cipher in the right manner to implement the GCM 338*4882a593Smuzhiyun specification. 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun3. The GCM AEAD cipher type implementation now invokes the SKCIPHER API 341*4882a593Smuzhiyun with the instantiated CTR(AES) cipher handle. 342*4882a593Smuzhiyun 343*4882a593Smuzhiyun During instantiation of the CTR(AES) cipher, the CIPHER type 344*4882a593Smuzhiyun implementation of AES is instantiated. The cipher handle for AES is 345*4882a593Smuzhiyun retained. 346*4882a593Smuzhiyun 347*4882a593Smuzhiyun That means that the SKCIPHER implementation of CTR(AES) only 348*4882a593Smuzhiyun implements the CTR block chaining mode. After performing the block 349*4882a593Smuzhiyun chaining operation, the CIPHER implementation of AES is invoked. 350*4882a593Smuzhiyun 351*4882a593Smuzhiyun4. The SKCIPHER of CTR(AES) now invokes the CIPHER API with the AES 352*4882a593Smuzhiyun cipher handle to encrypt one block. 353*4882a593Smuzhiyun 354*4882a593Smuzhiyun5. The GCM AEAD implementation also invokes the GHASH cipher 355*4882a593Smuzhiyun implementation via the AHASH API. 356*4882a593Smuzhiyun 357*4882a593SmuzhiyunWhen the IPSEC layer triggers the esp_input() function, the same call 358*4882a593Smuzhiyunsequence is followed with the only difference that the operation starts 359*4882a593Smuzhiyunwith step (2). 360*4882a593Smuzhiyun 361*4882a593SmuzhiyunGeneric Block Cipher Structure 362*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 363*4882a593Smuzhiyun 364*4882a593SmuzhiyunGeneric block ciphers follow the same concept as depicted with the ASCII 365*4882a593Smuzhiyunart picture above. 366*4882a593Smuzhiyun 367*4882a593SmuzhiyunFor example, CBC(AES) is implemented with cbc.c, and aes-generic.c. The 368*4882a593SmuzhiyunASCII art picture above applies as well with the difference that only 369*4882a593Smuzhiyunstep (4) is used and the SKCIPHER block chaining mode is CBC. 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunGeneric Keyed Message Digest Structure 372*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 373*4882a593Smuzhiyun 374*4882a593SmuzhiyunKeyed message digest implementations again follow the same concept as 375*4882a593Smuzhiyundepicted in the ASCII art picture above. 376*4882a593Smuzhiyun 377*4882a593SmuzhiyunFor example, HMAC(SHA256) is implemented with hmac.c and 378*4882a593Smuzhiyunsha256_generic.c. The following ASCII art illustrates the 379*4882a593Smuzhiyunimplementation: 380*4882a593Smuzhiyun 381*4882a593Smuzhiyun:: 382*4882a593Smuzhiyun 383*4882a593Smuzhiyun 384*4882a593Smuzhiyun kernel crypto API | Caller 385*4882a593Smuzhiyun | 386*4882a593Smuzhiyun +-----------+ (1) | 387*4882a593Smuzhiyun | | <------------------ some_function 388*4882a593Smuzhiyun | ahash | 389*4882a593Smuzhiyun | (hmac) | ---+ 390*4882a593Smuzhiyun +-----------+ | 391*4882a593Smuzhiyun | (2) 392*4882a593Smuzhiyun +-----------+ | 393*4882a593Smuzhiyun | | <--+ 394*4882a593Smuzhiyun | shash | 395*4882a593Smuzhiyun | (sha256) | 396*4882a593Smuzhiyun +-----------+ 397*4882a593Smuzhiyun 398*4882a593Smuzhiyun 399*4882a593Smuzhiyun 400*4882a593SmuzhiyunThe following call sequence is applicable when a caller triggers an HMAC 401*4882a593Smuzhiyunoperation: 402*4882a593Smuzhiyun 403*4882a593Smuzhiyun1. The AHASH API functions are invoked by the caller. The HMAC 404*4882a593Smuzhiyun implementation performs its operation as needed. 405*4882a593Smuzhiyun 406*4882a593Smuzhiyun During initialization of the HMAC cipher, the SHASH cipher type of 407*4882a593Smuzhiyun SHA256 is instantiated. The cipher handle for the SHA256 instance is 408*4882a593Smuzhiyun retained. 409*4882a593Smuzhiyun 410*4882a593Smuzhiyun At one time, the HMAC implementation requires a SHA256 operation 411*4882a593Smuzhiyun where the SHA256 cipher handle is used. 412*4882a593Smuzhiyun 413*4882a593Smuzhiyun2. The HMAC instance now invokes the SHASH API with the SHA256 cipher 414*4882a593Smuzhiyun handle to calculate the message digest. 415