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