diff options
author | Stephan Mueller <smueller@chronox.de> | 2016-10-20 22:57:00 -0400 |
---|---|---|
committer | Jonathan Corbet <corbet@lwn.net> | 2016-12-13 18:38:04 -0500 |
commit | c441a4781ff1c5b78db1410f708aa96dceec5fa2 (patch) | |
tree | ccd31f1748b0c54ec33925e3ac9d48437dc80e8b /Documentation/DocBook | |
parent | 3b72c814a8e8cd638e1ba0da4dfce501e9dff5af (diff) |
crypto: doc - remove crypto API DocBook
With the conversion of the documentation to Sphinx, the old DocBook is
now stale.
Signed-off-by: Stephan Mueller <smueller@chronox.de>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Diffstat (limited to 'Documentation/DocBook')
-rw-r--r-- | Documentation/DocBook/Makefile | 2 | ||||
-rw-r--r-- | Documentation/DocBook/crypto-API.tmpl | 2092 |
2 files changed, 1 insertions, 2093 deletions
diff --git a/Documentation/DocBook/Makefile b/Documentation/DocBook/Makefile index caab9039362f..c75e5d6b8fa8 100644 --- a/Documentation/DocBook/Makefile +++ b/Documentation/DocBook/Makefile | |||
@@ -13,7 +13,7 @@ DOCBOOKS := z8530book.xml \ | |||
13 | gadget.xml libata.xml mtdnand.xml librs.xml rapidio.xml \ | 13 | gadget.xml libata.xml mtdnand.xml librs.xml rapidio.xml \ |
14 | genericirq.xml s390-drivers.xml uio-howto.xml scsi.xml \ | 14 | genericirq.xml s390-drivers.xml uio-howto.xml scsi.xml \ |
15 | 80211.xml sh.xml regulator.xml w1.xml \ | 15 | 80211.xml sh.xml regulator.xml w1.xml \ |
16 | writing_musb_glue_layer.xml crypto-API.xml iio.xml | 16 | writing_musb_glue_layer.xml iio.xml |
17 | 17 | ||
18 | ifeq ($(DOCBOOKS),) | 18 | ifeq ($(DOCBOOKS),) |
19 | 19 | ||
diff --git a/Documentation/DocBook/crypto-API.tmpl b/Documentation/DocBook/crypto-API.tmpl deleted file mode 100644 index 088b79c341ff..000000000000 --- a/Documentation/DocBook/crypto-API.tmpl +++ /dev/null | |||
@@ -1,2092 +0,0 @@ | |||
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" | ||
3 | "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []> | ||
4 | |||
5 | <book id="KernelCryptoAPI"> | ||
6 | <bookinfo> | ||
7 | <title>Linux Kernel Crypto API</title> | ||
8 | |||
9 | <authorgroup> | ||
10 | <author> | ||
11 | <firstname>Stephan</firstname> | ||
12 | <surname>Mueller</surname> | ||
13 | <affiliation> | ||
14 | <address> | ||
15 | <email>smueller@chronox.de</email> | ||
16 | </address> | ||
17 | </affiliation> | ||
18 | </author> | ||
19 | <author> | ||
20 | <firstname>Marek</firstname> | ||
21 | <surname>Vasut</surname> | ||
22 | <affiliation> | ||
23 | <address> | ||
24 | <email>marek@denx.de</email> | ||
25 | </address> | ||
26 | </affiliation> | ||
27 | </author> | ||
28 | </authorgroup> | ||
29 | |||
30 | <copyright> | ||
31 | <year>2014</year> | ||
32 | <holder>Stephan Mueller</holder> | ||
33 | </copyright> | ||
34 | |||
35 | |||
36 | <legalnotice> | ||
37 | <para> | ||
38 | This documentation is free software; you can redistribute | ||
39 | it and/or modify it under the terms of the GNU General Public | ||
40 | License as published by the Free Software Foundation; either | ||
41 | version 2 of the License, or (at your option) any later | ||
42 | version. | ||
43 | </para> | ||
44 | |||
45 | <para> | ||
46 | This program is distributed in the hope that it will be | ||
47 | useful, but WITHOUT ANY WARRANTY; without even the implied | ||
48 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
49 | See the GNU General Public License for more details. | ||
50 | </para> | ||
51 | |||
52 | <para> | ||
53 | You should have received a copy of the GNU General Public | ||
54 | License along with this program; if not, write to the Free | ||
55 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
56 | MA 02111-1307 USA | ||
57 | </para> | ||
58 | |||
59 | <para> | ||
60 | For more details see the file COPYING in the source | ||
61 | distribution of Linux. | ||
62 | </para> | ||
63 | </legalnotice> | ||
64 | </bookinfo> | ||
65 | |||
66 | <toc></toc> | ||
67 | |||
68 | <chapter id="Intro"> | ||
69 | <title>Kernel Crypto API Interface Specification</title> | ||
70 | |||
71 | <sect1><title>Introduction</title> | ||
72 | |||
73 | <para> | ||
74 | The kernel crypto API offers a rich set of cryptographic ciphers as | ||
75 | well as other data transformation mechanisms and methods to invoke | ||
76 | these. This document contains a description of the API and provides | ||
77 | example code. | ||
78 | </para> | ||
79 | |||
80 | <para> | ||
81 | To understand and properly use the kernel crypto API a brief | ||
82 | explanation of its structure is given. Based on the architecture, | ||
83 | the API can be separated into different components. Following the | ||
84 | architecture specification, hints to developers of ciphers are | ||
85 | provided. Pointers to the API function call documentation are | ||
86 | given at the end. | ||
87 | </para> | ||
88 | |||
89 | <para> | ||
90 | The kernel crypto API refers to all algorithms as "transformations". | ||
91 | Therefore, a cipher handle variable usually has the name "tfm". | ||
92 | Besides cryptographic operations, the kernel crypto API also knows | ||
93 | compression transformations and handles them the same way as ciphers. | ||
94 | </para> | ||
95 | |||
96 | <para> | ||
97 | The kernel crypto API serves the following entity types: | ||
98 | |||
99 | <itemizedlist> | ||
100 | <listitem> | ||
101 | <para>consumers requesting cryptographic services</para> | ||
102 | </listitem> | ||
103 | <listitem> | ||
104 | <para>data transformation implementations (typically ciphers) | ||
105 | that can be called by consumers using the kernel crypto | ||
106 | API</para> | ||
107 | </listitem> | ||
108 | </itemizedlist> | ||
109 | </para> | ||
110 | |||
111 | <para> | ||
112 | This specification is intended for consumers of the kernel crypto | ||
113 | API as well as for developers implementing ciphers. This API | ||
114 | specification, however, does not discuss all API calls available | ||
115 | to data transformation implementations (i.e. implementations of | ||
116 | ciphers and other transformations (such as CRC or even compression | ||
117 | algorithms) that can register with the kernel crypto API). | ||
118 | </para> | ||
119 | |||
120 | <para> | ||
121 | Note: The terms "transformation" and cipher algorithm are used | ||
122 | interchangeably. | ||
123 | </para> | ||
124 | </sect1> | ||
125 | |||
126 | <sect1><title>Terminology</title> | ||
127 | <para> | ||
128 | The transformation implementation is an actual code or interface | ||
129 | to hardware which implements a certain transformation with precisely | ||
130 | defined behavior. | ||
131 | </para> | ||
132 | |||
133 | <para> | ||
134 | The transformation object (TFM) is an instance of a transformation | ||
135 | implementation. There can be multiple transformation objects | ||
136 | associated with a single transformation implementation. Each of | ||
137 | those transformation objects is held by a crypto API consumer or | ||
138 | another transformation. Transformation object is allocated when a | ||
139 | crypto API consumer requests a transformation implementation. | ||
140 | The consumer is then provided with a structure, which contains | ||
141 | a transformation object (TFM). | ||
142 | </para> | ||
143 | |||
144 | <para> | ||
145 | The structure that contains transformation objects may also be | ||
146 | referred to as a "cipher handle". Such a cipher handle is always | ||
147 | subject to the following phases that are reflected in the API calls | ||
148 | applicable to such a cipher handle: | ||
149 | </para> | ||
150 | |||
151 | <orderedlist> | ||
152 | <listitem> | ||
153 | <para>Initialization of a cipher handle.</para> | ||
154 | </listitem> | ||
155 | <listitem> | ||
156 | <para>Execution of all intended cipher operations applicable | ||
157 | for the handle where the cipher handle must be furnished to | ||
158 | every API call.</para> | ||
159 | </listitem> | ||
160 | <listitem> | ||
161 | <para>Destruction of a cipher handle.</para> | ||
162 | </listitem> | ||
163 | </orderedlist> | ||
164 | |||
165 | <para> | ||
166 | When using the initialization API calls, a cipher handle is | ||
167 | created and returned to the consumer. Therefore, please refer | ||
168 | to all initialization API calls that refer to the data | ||
169 | structure type a consumer is expected to receive and subsequently | ||
170 | to use. The initialization API calls have all the same naming | ||
171 | conventions of crypto_alloc_*. | ||
172 | </para> | ||
173 | |||
174 | <para> | ||
175 | The transformation context is private data associated with | ||
176 | the transformation object. | ||
177 | </para> | ||
178 | </sect1> | ||
179 | </chapter> | ||
180 | |||
181 | <chapter id="Architecture"><title>Kernel Crypto API Architecture</title> | ||
182 | <sect1><title>Cipher algorithm types</title> | ||
183 | <para> | ||
184 | The kernel crypto API provides different API calls for the | ||
185 | following cipher types: | ||
186 | |||
187 | <itemizedlist> | ||
188 | <listitem><para>Symmetric ciphers</para></listitem> | ||
189 | <listitem><para>AEAD ciphers</para></listitem> | ||
190 | <listitem><para>Message digest, including keyed message digest</para></listitem> | ||
191 | <listitem><para>Random number generation</para></listitem> | ||
192 | <listitem><para>User space interface</para></listitem> | ||
193 | </itemizedlist> | ||
194 | </para> | ||
195 | </sect1> | ||
196 | |||
197 | <sect1><title>Ciphers And Templates</title> | ||
198 | <para> | ||
199 | The kernel crypto API provides implementations of single block | ||
200 | ciphers and message digests. In addition, the kernel crypto API | ||
201 | provides numerous "templates" that can be used in conjunction | ||
202 | with the single block ciphers and message digests. Templates | ||
203 | include all types of block chaining mode, the HMAC mechanism, etc. | ||
204 | </para> | ||
205 | |||
206 | <para> | ||
207 | Single block ciphers and message digests can either be directly | ||
208 | used by a caller or invoked together with a template to form | ||
209 | multi-block ciphers or keyed message digests. | ||
210 | </para> | ||
211 | |||
212 | <para> | ||
213 | A single block cipher may even be called with multiple templates. | ||
214 | However, templates cannot be used without a single cipher. | ||
215 | </para> | ||
216 | |||
217 | <para> | ||
218 | See /proc/crypto and search for "name". For example: | ||
219 | |||
220 | <itemizedlist> | ||
221 | <listitem><para>aes</para></listitem> | ||
222 | <listitem><para>ecb(aes)</para></listitem> | ||
223 | <listitem><para>cmac(aes)</para></listitem> | ||
224 | <listitem><para>ccm(aes)</para></listitem> | ||
225 | <listitem><para>rfc4106(gcm(aes))</para></listitem> | ||
226 | <listitem><para>sha1</para></listitem> | ||
227 | <listitem><para>hmac(sha1)</para></listitem> | ||
228 | <listitem><para>authenc(hmac(sha1),cbc(aes))</para></listitem> | ||
229 | </itemizedlist> | ||
230 | </para> | ||
231 | |||
232 | <para> | ||
233 | In these examples, "aes" and "sha1" are the ciphers and all | ||
234 | others are the templates. | ||
235 | </para> | ||
236 | </sect1> | ||
237 | |||
238 | <sect1><title>Synchronous And Asynchronous Operation</title> | ||
239 | <para> | ||
240 | The kernel crypto API provides synchronous and asynchronous | ||
241 | API operations. | ||
242 | </para> | ||
243 | |||
244 | <para> | ||
245 | When using the synchronous API operation, the caller invokes | ||
246 | a cipher operation which is performed synchronously by the | ||
247 | kernel crypto API. That means, the caller waits until the | ||
248 | cipher operation completes. Therefore, the kernel crypto API | ||
249 | calls work like regular function calls. For synchronous | ||
250 | operation, the set of API calls is small and conceptually | ||
251 | similar to any other crypto library. | ||
252 | </para> | ||
253 | |||
254 | <para> | ||
255 | Asynchronous operation is provided by the kernel crypto API | ||
256 | which implies that the invocation of a cipher operation will | ||
257 | complete almost instantly. That invocation triggers the | ||
258 | cipher operation but it does not signal its completion. Before | ||
259 | invoking a cipher operation, the caller must provide a callback | ||
260 | function the kernel crypto API can invoke to signal the | ||
261 | completion of the cipher operation. Furthermore, the caller | ||
262 | must ensure it can handle such asynchronous events by applying | ||
263 | appropriate locking around its data. The kernel crypto API | ||
264 | does not perform any special serialization operation to protect | ||
265 | the caller's data integrity. | ||
266 | </para> | ||
267 | </sect1> | ||
268 | |||
269 | <sect1><title>Crypto API Cipher References And Priority</title> | ||
270 | <para> | ||
271 | A cipher is referenced by the caller with a string. That string | ||
272 | has the following semantics: | ||
273 | |||
274 | <programlisting> | ||
275 | template(single block cipher) | ||
276 | </programlisting> | ||
277 | |||
278 | where "template" and "single block cipher" is the aforementioned | ||
279 | template and single block cipher, respectively. If applicable, | ||
280 | additional templates may enclose other templates, such as | ||
281 | |||
282 | <programlisting> | ||
283 | template1(template2(single block cipher))) | ||
284 | </programlisting> | ||
285 | </para> | ||
286 | |||
287 | <para> | ||
288 | The kernel crypto API may provide multiple implementations of a | ||
289 | template or a single block cipher. For example, AES on newer | ||
290 | Intel hardware has the following implementations: AES-NI, | ||
291 | assembler implementation, or straight C. Now, when using the | ||
292 | string "aes" with the kernel crypto API, which cipher | ||
293 | implementation is used? The answer to that question is the | ||
294 | priority number assigned to each cipher implementation by the | ||
295 | kernel crypto API. When a caller uses the string to refer to a | ||
296 | cipher during initialization of a cipher handle, the kernel | ||
297 | crypto API looks up all implementations providing an | ||
298 | implementation with that name and selects the implementation | ||
299 | with the highest priority. | ||
300 | </para> | ||
301 | |||
302 | <para> | ||
303 | Now, a caller may have the need to refer to a specific cipher | ||
304 | implementation and thus does not want to rely on the | ||
305 | priority-based selection. To accommodate this scenario, the | ||
306 | kernel crypto API allows the cipher implementation to register | ||
307 | a unique name in addition to common names. When using that | ||
308 | unique name, a caller is therefore always sure to refer to | ||
309 | the intended cipher implementation. | ||
310 | </para> | ||
311 | |||
312 | <para> | ||
313 | The list of available ciphers is given in /proc/crypto. However, | ||
314 | that list does not specify all possible permutations of | ||
315 | templates and ciphers. Each block listed in /proc/crypto may | ||
316 | contain the following information -- if one of the components | ||
317 | listed as follows are not applicable to a cipher, it is not | ||
318 | displayed: | ||
319 | </para> | ||
320 | |||
321 | <itemizedlist> | ||
322 | <listitem> | ||
323 | <para>name: the generic name of the cipher that is subject | ||
324 | to the priority-based selection -- this name can be used by | ||
325 | the cipher allocation API calls (all names listed above are | ||
326 | examples for such generic names)</para> | ||
327 | </listitem> | ||
328 | <listitem> | ||
329 | <para>driver: the unique name of the cipher -- this name can | ||
330 | be used by the cipher allocation API calls</para> | ||
331 | </listitem> | ||
332 | <listitem> | ||
333 | <para>module: the kernel module providing the cipher | ||
334 | implementation (or "kernel" for statically linked ciphers)</para> | ||
335 | </listitem> | ||
336 | <listitem> | ||
337 | <para>priority: the priority value of the cipher implementation</para> | ||
338 | </listitem> | ||
339 | <listitem> | ||
340 | <para>refcnt: the reference count of the respective cipher | ||
341 | (i.e. the number of current consumers of this cipher)</para> | ||
342 | </listitem> | ||
343 | <listitem> | ||
344 | <para>selftest: specification whether the self test for the | ||
345 | cipher passed</para> | ||
346 | </listitem> | ||
347 | <listitem> | ||
348 | <para>type: | ||
349 | <itemizedlist> | ||
350 | <listitem> | ||
351 | <para>skcipher for symmetric key ciphers</para> | ||
352 | </listitem> | ||
353 | <listitem> | ||
354 | <para>cipher for single block ciphers that may be used with | ||
355 | an additional template</para> | ||
356 | </listitem> | ||
357 | <listitem> | ||
358 | <para>shash for synchronous message digest</para> | ||
359 | </listitem> | ||
360 | <listitem> | ||
361 | <para>ahash for asynchronous message digest</para> | ||
362 | </listitem> | ||
363 | <listitem> | ||
364 | <para>aead for AEAD cipher type</para> | ||
365 | </listitem> | ||
366 | <listitem> | ||
367 | <para>compression for compression type transformations</para> | ||
368 | </listitem> | ||
369 | <listitem> | ||
370 | <para>rng for random number generator</para> | ||
371 | </listitem> | ||
372 | <listitem> | ||
373 | <para>givcipher for cipher with associated IV generator | ||
374 | (see the geniv entry below for the specification of the | ||
375 | IV generator type used by the cipher implementation)</para> | ||
376 | </listitem> | ||
377 | </itemizedlist> | ||
378 | </para> | ||
379 | </listitem> | ||
380 | <listitem> | ||
381 | <para>blocksize: blocksize of cipher in bytes</para> | ||
382 | </listitem> | ||
383 | <listitem> | ||
384 | <para>keysize: key size in bytes</para> | ||
385 | </listitem> | ||
386 | <listitem> | ||
387 | <para>ivsize: IV size in bytes</para> | ||
388 | </listitem> | ||
389 | <listitem> | ||
390 | <para>seedsize: required size of seed data for random number | ||
391 | generator</para> | ||
392 | </listitem> | ||
393 | <listitem> | ||
394 | <para>digestsize: output size of the message digest</para> | ||
395 | </listitem> | ||
396 | <listitem> | ||
397 | <para>geniv: IV generation type: | ||
398 | <itemizedlist> | ||
399 | <listitem> | ||
400 | <para>eseqiv for encrypted sequence number based IV | ||
401 | generation</para> | ||
402 | </listitem> | ||
403 | <listitem> | ||
404 | <para>seqiv for sequence number based IV generation</para> | ||
405 | </listitem> | ||
406 | <listitem> | ||
407 | <para>chainiv for chain iv generation</para> | ||
408 | </listitem> | ||
409 | <listitem> | ||
410 | <para><builtin> is a marker that the cipher implements | ||
411 | IV generation and handling as it is specific to the given | ||
412 | cipher</para> | ||
413 | </listitem> | ||
414 | </itemizedlist> | ||
415 | </para> | ||
416 | </listitem> | ||
417 | </itemizedlist> | ||
418 | </sect1> | ||
419 | |||
420 | <sect1><title>Key Sizes</title> | ||
421 | <para> | ||
422 | When allocating a cipher handle, the caller only specifies the | ||
423 | cipher type. Symmetric ciphers, however, typically support | ||
424 | multiple key sizes (e.g. AES-128 vs. AES-192 vs. AES-256). | ||
425 | These key sizes are determined with the length of the provided | ||
426 | key. Thus, the kernel crypto API does not provide a separate | ||
427 | way to select the particular symmetric cipher key size. | ||
428 | </para> | ||
429 | </sect1> | ||
430 | |||
431 | <sect1><title>Cipher Allocation Type And Masks</title> | ||
432 | <para> | ||
433 | The different cipher handle allocation functions allow the | ||
434 | specification of a type and mask flag. Both parameters have | ||
435 | the following meaning (and are therefore not covered in the | ||
436 | subsequent sections). | ||
437 | </para> | ||
438 | |||
439 | <para> | ||
440 | The type flag specifies the type of the cipher algorithm. | ||
441 | The caller usually provides a 0 when the caller wants the | ||
442 | default handling. Otherwise, the caller may provide the | ||
443 | following selections which match the aforementioned cipher | ||
444 | types: | ||
445 | </para> | ||
446 | |||
447 | <itemizedlist> | ||
448 | <listitem> | ||
449 | <para>CRYPTO_ALG_TYPE_CIPHER Single block cipher</para> | ||
450 | </listitem> | ||
451 | <listitem> | ||
452 | <para>CRYPTO_ALG_TYPE_COMPRESS Compression</para> | ||
453 | </listitem> | ||
454 | <listitem> | ||
455 | <para>CRYPTO_ALG_TYPE_AEAD Authenticated Encryption with | ||
456 | Associated Data (MAC)</para> | ||
457 | </listitem> | ||
458 | <listitem> | ||
459 | <para>CRYPTO_ALG_TYPE_BLKCIPHER Synchronous multi-block cipher</para> | ||
460 | </listitem> | ||
461 | <listitem> | ||
462 | <para>CRYPTO_ALG_TYPE_ABLKCIPHER Asynchronous multi-block cipher</para> | ||
463 | </listitem> | ||
464 | <listitem> | ||
465 | <para>CRYPTO_ALG_TYPE_GIVCIPHER Asynchronous multi-block | ||
466 | cipher packed together with an IV generator (see geniv field | ||
467 | in the /proc/crypto listing for the known IV generators)</para> | ||
468 | </listitem> | ||
469 | <listitem> | ||
470 | <para>CRYPTO_ALG_TYPE_DIGEST Raw message digest</para> | ||
471 | </listitem> | ||
472 | <listitem> | ||
473 | <para>CRYPTO_ALG_TYPE_HASH Alias for CRYPTO_ALG_TYPE_DIGEST</para> | ||
474 | </listitem> | ||
475 | <listitem> | ||
476 | <para>CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash</para> | ||
477 | </listitem> | ||
478 | <listitem> | ||
479 | <para>CRYPTO_ALG_TYPE_AHASH Asynchronous multi-block hash</para> | ||
480 | </listitem> | ||
481 | <listitem> | ||
482 | <para>CRYPTO_ALG_TYPE_RNG Random Number Generation</para> | ||
483 | </listitem> | ||
484 | <listitem> | ||
485 | <para>CRYPTO_ALG_TYPE_AKCIPHER Asymmetric cipher</para> | ||
486 | </listitem> | ||
487 | <listitem> | ||
488 | <para>CRYPTO_ALG_TYPE_PCOMPRESS Enhanced version of | ||
489 | CRYPTO_ALG_TYPE_COMPRESS allowing for segmented compression / | ||
490 | decompression instead of performing the operation on one | ||
491 | segment only. CRYPTO_ALG_TYPE_PCOMPRESS is intended to replace | ||
492 | CRYPTO_ALG_TYPE_COMPRESS once existing consumers are converted.</para> | ||
493 | </listitem> | ||
494 | </itemizedlist> | ||
495 | |||
496 | <para> | ||
497 | The mask flag restricts the type of cipher. The only allowed | ||
498 | flag is CRYPTO_ALG_ASYNC to restrict the cipher lookup function | ||
499 | to asynchronous ciphers. Usually, a caller provides a 0 for the | ||
500 | mask flag. | ||
501 | </para> | ||
502 | |||
503 | <para> | ||
504 | When the caller provides a mask and type specification, the | ||
505 | caller limits the search the kernel crypto API can perform for | ||
506 | a suitable cipher implementation for the given cipher name. | ||
507 | That means, even when a caller uses a cipher name that exists | ||
508 | during its initialization call, the kernel crypto API may not | ||
509 | select it due to the used type and mask field. | ||
510 | </para> | ||
511 | </sect1> | ||
512 | |||
513 | <sect1><title>Internal Structure of Kernel Crypto API</title> | ||
514 | |||
515 | <para> | ||
516 | The kernel crypto API has an internal structure where a cipher | ||
517 | implementation may use many layers and indirections. This section | ||
518 | shall help to clarify how the kernel crypto API uses | ||
519 | various components to implement the complete cipher. | ||
520 | </para> | ||
521 | |||
522 | <para> | ||
523 | The following subsections explain the internal structure based | ||
524 | on existing cipher implementations. The first section addresses | ||
525 | the most complex scenario where all other scenarios form a logical | ||
526 | subset. | ||
527 | </para> | ||
528 | |||
529 | <sect2><title>Generic AEAD Cipher Structure</title> | ||
530 | |||
531 | <para> | ||
532 | The following ASCII art decomposes the kernel crypto API layers | ||
533 | when using the AEAD cipher with the automated IV generation. The | ||
534 | shown example is used by the IPSEC layer. | ||
535 | </para> | ||
536 | |||
537 | <para> | ||
538 | For other use cases of AEAD ciphers, the ASCII art applies as | ||
539 | well, but the caller may not use the AEAD cipher with a separate | ||
540 | IV generator. In this case, the caller must generate the IV. | ||
541 | </para> | ||
542 | |||
543 | <para> | ||
544 | The depicted example decomposes the AEAD cipher of GCM(AES) based | ||
545 | on the generic C implementations (gcm.c, aes-generic.c, ctr.c, | ||
546 | ghash-generic.c, seqiv.c). The generic implementation serves as an | ||
547 | example showing the complete logic of the kernel crypto API. | ||
548 | </para> | ||
549 | |||
550 | <para> | ||
551 | It is possible that some streamlined cipher implementations (like | ||
552 | AES-NI) provide implementations merging aspects which in the view | ||
553 | of the kernel crypto API cannot be decomposed into layers any more. | ||
554 | In case of the AES-NI implementation, the CTR mode, the GHASH | ||
555 | implementation and the AES cipher are all merged into one cipher | ||
556 | implementation registered with the kernel crypto API. In this case, | ||
557 | the concept described by the following ASCII art applies too. However, | ||
558 | the decomposition of GCM into the individual sub-components | ||
559 | by the kernel crypto API is not done any more. | ||
560 | </para> | ||
561 | |||
562 | <para> | ||
563 | Each block in the following ASCII art is an independent cipher | ||
564 | instance obtained from the kernel crypto API. Each block | ||
565 | is accessed by the caller or by other blocks using the API functions | ||
566 | defined by the kernel crypto API for the cipher implementation type. | ||
567 | </para> | ||
568 | |||
569 | <para> | ||
570 | The blocks below indicate the cipher type as well as the specific | ||
571 | logic implemented in the cipher. | ||
572 | </para> | ||
573 | |||
574 | <para> | ||
575 | The ASCII art picture also indicates the call structure, i.e. who | ||
576 | calls which component. The arrows point to the invoked block | ||
577 | where the caller uses the API applicable to the cipher type | ||
578 | specified for the block. | ||
579 | </para> | ||
580 | |||
581 | <programlisting> | ||
582 | <![CDATA[ | ||
583 | kernel crypto API | IPSEC Layer | ||
584 | | | ||
585 | +-----------+ | | ||
586 | | | (1) | ||
587 | | aead | <----------------------------------- esp_output | ||
588 | | (seqiv) | ---+ | ||
589 | +-----------+ | | ||
590 | | (2) | ||
591 | +-----------+ | | ||
592 | | | <--+ (2) | ||
593 | | aead | <----------------------------------- esp_input | ||
594 | | (gcm) | ------------+ | ||
595 | +-----------+ | | ||
596 | | (3) | (5) | ||
597 | v v | ||
598 | +-----------+ +-----------+ | ||
599 | | | | | | ||
600 | | skcipher | | ahash | | ||
601 | | (ctr) | ---+ | (ghash) | | ||
602 | +-----------+ | +-----------+ | ||
603 | | | ||
604 | +-----------+ | (4) | ||
605 | | | <--+ | ||
606 | | cipher | | ||
607 | | (aes) | | ||
608 | +-----------+ | ||
609 | ]]> | ||
610 | </programlisting> | ||
611 | |||
612 | <para> | ||
613 | The following call sequence is applicable when the IPSEC layer | ||
614 | triggers an encryption operation with the esp_output function. During | ||
615 | configuration, the administrator set up the use of rfc4106(gcm(aes)) as | ||
616 | the cipher for ESP. The following call sequence is now depicted in the | ||
617 | ASCII art above: | ||
618 | </para> | ||
619 | |||
620 | <orderedlist> | ||
621 | <listitem> | ||
622 | <para> | ||
623 | esp_output() invokes crypto_aead_encrypt() to trigger an encryption | ||
624 | operation of the AEAD cipher with IV generator. | ||
625 | </para> | ||
626 | |||
627 | <para> | ||
628 | In case of GCM, the SEQIV implementation is registered as GIVCIPHER | ||
629 | in crypto_rfc4106_alloc(). | ||
630 | </para> | ||
631 | |||
632 | <para> | ||
633 | The SEQIV performs its operation to generate an IV where the core | ||
634 | function is seqiv_geniv(). | ||
635 | </para> | ||
636 | </listitem> | ||
637 | |||
638 | <listitem> | ||
639 | <para> | ||
640 | Now, SEQIV uses the AEAD API function calls to invoke the associated | ||
641 | AEAD cipher. In our case, during the instantiation of SEQIV, the | ||
642 | cipher handle for GCM is provided to SEQIV. This means that SEQIV | ||
643 | invokes AEAD cipher operations with the GCM cipher handle. | ||
644 | </para> | ||
645 | |||
646 | <para> | ||
647 | During instantiation of the GCM handle, the CTR(AES) and GHASH | ||
648 | ciphers are instantiated. The cipher handles for CTR(AES) and GHASH | ||
649 | are retained for later use. | ||
650 | </para> | ||
651 | |||
652 | <para> | ||
653 | The GCM implementation is responsible to invoke the CTR mode AES and | ||
654 | the GHASH cipher in the right manner to implement the GCM | ||
655 | specification. | ||
656 | </para> | ||
657 | </listitem> | ||
658 | |||
659 | <listitem> | ||
660 | <para> | ||
661 | The GCM AEAD cipher type implementation now invokes the SKCIPHER API | ||
662 | with the instantiated CTR(AES) cipher handle. | ||
663 | </para> | ||
664 | |||
665 | <para> | ||
666 | During instantiation of the CTR(AES) cipher, the CIPHER type | ||
667 | implementation of AES is instantiated. The cipher handle for AES is | ||
668 | retained. | ||
669 | </para> | ||
670 | |||
671 | <para> | ||
672 | That means that the SKCIPHER implementation of CTR(AES) only | ||
673 | implements the CTR block chaining mode. After performing the block | ||
674 | chaining operation, the CIPHER implementation of AES is invoked. | ||
675 | </para> | ||
676 | </listitem> | ||
677 | |||
678 | <listitem> | ||
679 | <para> | ||
680 | The SKCIPHER of CTR(AES) now invokes the CIPHER API with the AES | ||
681 | cipher handle to encrypt one block. | ||
682 | </para> | ||
683 | </listitem> | ||
684 | |||
685 | <listitem> | ||
686 | <para> | ||
687 | The GCM AEAD implementation also invokes the GHASH cipher | ||
688 | implementation via the AHASH API. | ||
689 | </para> | ||
690 | </listitem> | ||
691 | </orderedlist> | ||
692 | |||
693 | <para> | ||
694 | When the IPSEC layer triggers the esp_input() function, the same call | ||
695 | sequence is followed with the only difference that the operation starts | ||
696 | with step (2). | ||
697 | </para> | ||
698 | </sect2> | ||
699 | |||
700 | <sect2><title>Generic Block Cipher Structure</title> | ||
701 | <para> | ||
702 | Generic block ciphers follow the same concept as depicted with the ASCII | ||
703 | art picture above. | ||
704 | </para> | ||
705 | |||
706 | <para> | ||
707 | For example, CBC(AES) is implemented with cbc.c, and aes-generic.c. The | ||
708 | ASCII art picture above applies as well with the difference that only | ||
709 | step (4) is used and the SKCIPHER block chaining mode is CBC. | ||
710 | </para> | ||
711 | </sect2> | ||
712 | |||
713 | <sect2><title>Generic Keyed Message Digest Structure</title> | ||
714 | <para> | ||
715 | Keyed message digest implementations again follow the same concept as | ||
716 | depicted in the ASCII art picture above. | ||
717 | </para> | ||
718 | |||
719 | <para> | ||
720 | For example, HMAC(SHA256) is implemented with hmac.c and | ||
721 | sha256_generic.c. The following ASCII art illustrates the | ||
722 | implementation: | ||
723 | </para> | ||
724 | |||
725 | <programlisting> | ||
726 | <![CDATA[ | ||
727 | kernel crypto API | Caller | ||
728 | | | ||
729 | +-----------+ (1) | | ||
730 | | | <------------------ some_function | ||
731 | | ahash | | ||
732 | | (hmac) | ---+ | ||
733 | +-----------+ | | ||
734 | | (2) | ||
735 | +-----------+ | | ||
736 | | | <--+ | ||
737 | | shash | | ||
738 | | (sha256) | | ||
739 | +-----------+ | ||
740 | ]]> | ||
741 | </programlisting> | ||
742 | |||
743 | <para> | ||
744 | The following call sequence is applicable when a caller triggers | ||
745 | an HMAC operation: | ||
746 | </para> | ||
747 | |||
748 | <orderedlist> | ||
749 | <listitem> | ||
750 | <para> | ||
751 | The AHASH API functions are invoked by the caller. The HMAC | ||
752 | implementation performs its operation as needed. | ||
753 | </para> | ||
754 | |||
755 | <para> | ||
756 | During initialization of the HMAC cipher, the SHASH cipher type of | ||
757 | SHA256 is instantiated. The cipher handle for the SHA256 instance is | ||
758 | retained. | ||
759 | </para> | ||
760 | |||
761 | <para> | ||
762 | At one time, the HMAC implementation requires a SHA256 operation | ||
763 | where the SHA256 cipher handle is used. | ||
764 | </para> | ||
765 | </listitem> | ||
766 | |||
767 | <listitem> | ||
768 | <para> | ||
769 | The HMAC instance now invokes the SHASH API with the SHA256 | ||
770 | cipher handle to calculate the message digest. | ||
771 | </para> | ||
772 | </listitem> | ||
773 | </orderedlist> | ||
774 | </sect2> | ||
775 | </sect1> | ||
776 | </chapter> | ||
777 | |||
778 | <chapter id="Development"><title>Developing Cipher Algorithms</title> | ||
779 | <sect1><title>Registering And Unregistering Transformation</title> | ||
780 | <para> | ||
781 | There are three distinct types of registration functions in | ||
782 | the Crypto API. One is used to register a generic cryptographic | ||
783 | transformation, while the other two are specific to HASH | ||
784 | transformations and COMPRESSion. We will discuss the latter | ||
785 | two in a separate chapter, here we will only look at the | ||
786 | generic ones. | ||
787 | </para> | ||
788 | |||
789 | <para> | ||
790 | Before discussing the register functions, the data structure | ||
791 | to be filled with each, struct crypto_alg, must be considered | ||
792 | -- see below for a description of this data structure. | ||
793 | </para> | ||
794 | |||
795 | <para> | ||
796 | The generic registration functions can be found in | ||
797 | include/linux/crypto.h and their definition can be seen below. | ||
798 | The former function registers a single transformation, while | ||
799 | the latter works on an array of transformation descriptions. | ||
800 | The latter is useful when registering transformations in bulk, | ||
801 | for example when a driver implements multiple transformations. | ||
802 | </para> | ||
803 | |||
804 | <programlisting> | ||
805 | int crypto_register_alg(struct crypto_alg *alg); | ||
806 | int crypto_register_algs(struct crypto_alg *algs, int count); | ||
807 | </programlisting> | ||
808 | |||
809 | <para> | ||
810 | The counterparts to those functions are listed below. | ||
811 | </para> | ||
812 | |||
813 | <programlisting> | ||
814 | int crypto_unregister_alg(struct crypto_alg *alg); | ||
815 | int crypto_unregister_algs(struct crypto_alg *algs, int count); | ||
816 | </programlisting> | ||
817 | |||
818 | <para> | ||
819 | Notice that both registration and unregistration functions | ||
820 | do return a value, so make sure to handle errors. A return | ||
821 | code of zero implies success. Any return code < 0 implies | ||
822 | an error. | ||
823 | </para> | ||
824 | |||
825 | <para> | ||
826 | The bulk registration/unregistration functions | ||
827 | register/unregister each transformation in the given array of | ||
828 | length count. They handle errors as follows: | ||
829 | </para> | ||
830 | <itemizedlist> | ||
831 | <listitem> | ||
832 | <para> | ||
833 | crypto_register_algs() succeeds if and only if it | ||
834 | successfully registers all the given transformations. If an | ||
835 | error occurs partway through, then it rolls back successful | ||
836 | registrations before returning the error code. Note that if | ||
837 | a driver needs to handle registration errors for individual | ||
838 | transformations, then it will need to use the non-bulk | ||
839 | function crypto_register_alg() instead. | ||
840 | </para> | ||
841 | </listitem> | ||
842 | <listitem> | ||
843 | <para> | ||
844 | crypto_unregister_algs() tries to unregister all the given | ||
845 | transformations, continuing on error. It logs errors and | ||
846 | always returns zero. | ||
847 | </para> | ||
848 | </listitem> | ||
849 | </itemizedlist> | ||
850 | |||
851 | </sect1> | ||
852 | |||
853 | <sect1><title>Single-Block Symmetric Ciphers [CIPHER]</title> | ||
854 | <para> | ||
855 | Example of transformations: aes, arc4, ... | ||
856 | </para> | ||
857 | |||
858 | <para> | ||
859 | This section describes the simplest of all transformation | ||
860 | implementations, that being the CIPHER type used for symmetric | ||
861 | ciphers. The CIPHER type is used for transformations which | ||
862 | operate on exactly one block at a time and there are no | ||
863 | dependencies between blocks at all. | ||
864 | </para> | ||
865 | |||
866 | <sect2><title>Registration specifics</title> | ||
867 | <para> | ||
868 | The registration of [CIPHER] algorithm is specific in that | ||
869 | struct crypto_alg field .cra_type is empty. The .cra_u.cipher | ||
870 | has to be filled in with proper callbacks to implement this | ||
871 | transformation. | ||
872 | </para> | ||
873 | |||
874 | <para> | ||
875 | See struct cipher_alg below. | ||
876 | </para> | ||
877 | </sect2> | ||
878 | |||
879 | <sect2><title>Cipher Definition With struct cipher_alg</title> | ||
880 | <para> | ||
881 | Struct cipher_alg defines a single block cipher. | ||
882 | </para> | ||
883 | |||
884 | <para> | ||
885 | Here are schematics of how these functions are called when | ||
886 | operated from other part of the kernel. Note that the | ||
887 | .cia_setkey() call might happen before or after any of these | ||
888 | schematics happen, but must not happen during any of these | ||
889 | are in-flight. | ||
890 | </para> | ||
891 | |||
892 | <para> | ||
893 | <programlisting> | ||
894 | KEY ---. PLAINTEXT ---. | ||
895 | v v | ||
896 | .cia_setkey() -> .cia_encrypt() | ||
897 | | | ||
898 | '-----> CIPHERTEXT | ||
899 | </programlisting> | ||
900 | </para> | ||
901 | |||
902 | <para> | ||
903 | Please note that a pattern where .cia_setkey() is called | ||
904 | multiple times is also valid: | ||
905 | </para> | ||
906 | |||
907 | <para> | ||
908 | <programlisting> | ||
909 | |||
910 | KEY1 --. PLAINTEXT1 --. KEY2 --. PLAINTEXT2 --. | ||
911 | v v v v | ||
912 | .cia_setkey() -> .cia_encrypt() -> .cia_setkey() -> .cia_encrypt() | ||
913 | | | | ||
914 | '---> CIPHERTEXT1 '---> CIPHERTEXT2 | ||
915 | </programlisting> | ||
916 | </para> | ||
917 | |||
918 | </sect2> | ||
919 | </sect1> | ||
920 | |||
921 | <sect1><title>Multi-Block Ciphers</title> | ||
922 | <para> | ||
923 | Example of transformations: cbc(aes), ecb(arc4), ... | ||
924 | </para> | ||
925 | |||
926 | <para> | ||
927 | This section describes the multi-block cipher transformation | ||
928 | implementations. The multi-block ciphers are | ||
929 | used for transformations which operate on scatterlists of | ||
930 | data supplied to the transformation functions. They output | ||
931 | the result into a scatterlist of data as well. | ||
932 | </para> | ||
933 | |||
934 | <sect2><title>Registration Specifics</title> | ||
935 | |||
936 | <para> | ||
937 | The registration of multi-block cipher algorithms | ||
938 | is one of the most standard procedures throughout the crypto API. | ||
939 | </para> | ||
940 | |||
941 | <para> | ||
942 | Note, if a cipher implementation requires a proper alignment | ||
943 | of data, the caller should use the functions of | ||
944 | crypto_skcipher_alignmask() to identify a memory alignment mask. | ||
945 | The kernel crypto API is able to process requests that are unaligned. | ||
946 | This implies, however, additional overhead as the kernel | ||
947 | crypto API needs to perform the realignment of the data which | ||
948 | may imply moving of data. | ||
949 | </para> | ||
950 | </sect2> | ||
951 | |||
952 | <sect2><title>Cipher Definition With struct blkcipher_alg and ablkcipher_alg</title> | ||
953 | <para> | ||
954 | Struct blkcipher_alg defines a synchronous block cipher whereas | ||
955 | struct ablkcipher_alg defines an asynchronous block cipher. | ||
956 | </para> | ||
957 | |||
958 | <para> | ||
959 | Please refer to the single block cipher description for schematics | ||
960 | of the block cipher usage. | ||
961 | </para> | ||
962 | </sect2> | ||
963 | |||
964 | <sect2><title>Specifics Of Asynchronous Multi-Block Cipher</title> | ||
965 | <para> | ||
966 | There are a couple of specifics to the asynchronous interface. | ||
967 | </para> | ||
968 | |||
969 | <para> | ||
970 | First of all, some of the drivers will want to use the | ||
971 | Generic ScatterWalk in case the hardware needs to be fed | ||
972 | separate chunks of the scatterlist which contains the | ||
973 | plaintext and will contain the ciphertext. Please refer | ||
974 | to the ScatterWalk interface offered by the Linux kernel | ||
975 | scatter / gather list implementation. | ||
976 | </para> | ||
977 | </sect2> | ||
978 | </sect1> | ||
979 | |||
980 | <sect1><title>Hashing [HASH]</title> | ||
981 | |||
982 | <para> | ||
983 | Example of transformations: crc32, md5, sha1, sha256,... | ||
984 | </para> | ||
985 | |||
986 | <sect2><title>Registering And Unregistering The Transformation</title> | ||
987 | |||
988 | <para> | ||
989 | There are multiple ways to register a HASH transformation, | ||
990 | depending on whether the transformation is synchronous [SHASH] | ||
991 | or asynchronous [AHASH] and the amount of HASH transformations | ||
992 | we are registering. You can find the prototypes defined in | ||
993 | include/crypto/internal/hash.h: | ||
994 | </para> | ||
995 | |||
996 | <programlisting> | ||
997 | int crypto_register_ahash(struct ahash_alg *alg); | ||
998 | |||
999 | int crypto_register_shash(struct shash_alg *alg); | ||
1000 | int crypto_register_shashes(struct shash_alg *algs, int count); | ||
1001 | </programlisting> | ||
1002 | |||
1003 | <para> | ||
1004 | The respective counterparts for unregistering the HASH | ||
1005 | transformation are as follows: | ||
1006 | </para> | ||
1007 | |||
1008 | <programlisting> | ||
1009 | int crypto_unregister_ahash(struct ahash_alg *alg); | ||
1010 | |||
1011 | int crypto_unregister_shash(struct shash_alg *alg); | ||
1012 | int crypto_unregister_shashes(struct shash_alg *algs, int count); | ||
1013 | </programlisting> | ||
1014 | </sect2> | ||
1015 | |||
1016 | <sect2><title>Cipher Definition With struct shash_alg and ahash_alg</title> | ||
1017 | <para> | ||
1018 | Here are schematics of how these functions are called when | ||
1019 | operated from other part of the kernel. Note that the .setkey() | ||
1020 | call might happen before or after any of these schematics happen, | ||
1021 | but must not happen during any of these are in-flight. Please note | ||
1022 | that calling .init() followed immediately by .finish() is also a | ||
1023 | perfectly valid transformation. | ||
1024 | </para> | ||
1025 | |||
1026 | <programlisting> | ||
1027 | I) DATA -----------. | ||
1028 | v | ||
1029 | .init() -> .update() -> .final() ! .update() might not be called | ||
1030 | ^ | | at all in this scenario. | ||
1031 | '----' '---> HASH | ||
1032 | |||
1033 | II) DATA -----------.-----------. | ||
1034 | v v | ||
1035 | .init() -> .update() -> .finup() ! .update() may not be called | ||
1036 | ^ | | at all in this scenario. | ||
1037 | '----' '---> HASH | ||
1038 | |||
1039 | III) DATA -----------. | ||
1040 | v | ||
1041 | .digest() ! The entire process is handled | ||
1042 | | by the .digest() call. | ||
1043 | '---------------> HASH | ||
1044 | </programlisting> | ||
1045 | |||
1046 | <para> | ||
1047 | Here is a schematic of how the .export()/.import() functions are | ||
1048 | called when used from another part of the kernel. | ||
1049 | </para> | ||
1050 | |||
1051 | <programlisting> | ||
1052 | KEY--. DATA--. | ||
1053 | v v ! .update() may not be called | ||
1054 | .setkey() -> .init() -> .update() -> .export() at all in this scenario. | ||
1055 | ^ | | | ||
1056 | '-----' '--> PARTIAL_HASH | ||
1057 | |||
1058 | ----------- other transformations happen here ----------- | ||
1059 | |||
1060 | PARTIAL_HASH--. DATA1--. | ||
1061 | v v | ||
1062 | .import -> .update() -> .final() ! .update() may not be called | ||
1063 | ^ | | at all in this scenario. | ||
1064 | '----' '--> HASH1 | ||
1065 | |||
1066 | PARTIAL_HASH--. DATA2-. | ||
1067 | v v | ||
1068 | .import -> .finup() | ||
1069 | | | ||
1070 | '---------------> HASH2 | ||
1071 | </programlisting> | ||
1072 | </sect2> | ||
1073 | |||
1074 | <sect2><title>Specifics Of Asynchronous HASH Transformation</title> | ||
1075 | <para> | ||
1076 | Some of the drivers will want to use the Generic ScatterWalk | ||
1077 | in case the implementation needs to be fed separate chunks of the | ||
1078 | scatterlist which contains the input data. The buffer containing | ||
1079 | the resulting hash will always be properly aligned to | ||
1080 | .cra_alignmask so there is no need to worry about this. | ||
1081 | </para> | ||
1082 | </sect2> | ||
1083 | </sect1> | ||
1084 | </chapter> | ||
1085 | |||
1086 | <chapter id="User"><title>User Space Interface</title> | ||
1087 | <sect1><title>Introduction</title> | ||
1088 | <para> | ||
1089 | The concepts of the kernel crypto API visible to kernel space is fully | ||
1090 | applicable to the user space interface as well. Therefore, the kernel | ||
1091 | crypto API high level discussion for the in-kernel use cases applies | ||
1092 | here as well. | ||
1093 | </para> | ||
1094 | |||
1095 | <para> | ||
1096 | The major difference, however, is that user space can only act as a | ||
1097 | consumer and never as a provider of a transformation or cipher algorithm. | ||
1098 | </para> | ||
1099 | |||
1100 | <para> | ||
1101 | The following covers the user space interface exported by the kernel | ||
1102 | crypto API. A working example of this description is libkcapi that | ||
1103 | can be obtained from [1]. That library can be used by user space | ||
1104 | applications that require cryptographic services from the kernel. | ||
1105 | </para> | ||
1106 | |||
1107 | <para> | ||
1108 | Some details of the in-kernel kernel crypto API aspects do not | ||
1109 | apply to user space, however. This includes the difference between | ||
1110 | synchronous and asynchronous invocations. The user space API call | ||
1111 | is fully synchronous. | ||
1112 | </para> | ||
1113 | |||
1114 | <para> | ||
1115 | [1] <ulink url="http://www.chronox.de/libkcapi.html">http://www.chronox.de/libkcapi.html</ulink> | ||
1116 | </para> | ||
1117 | |||
1118 | </sect1> | ||
1119 | |||
1120 | <sect1><title>User Space API General Remarks</title> | ||
1121 | <para> | ||
1122 | The kernel crypto API is accessible from user space. Currently, | ||
1123 | the following ciphers are accessible: | ||
1124 | </para> | ||
1125 | |||
1126 | <itemizedlist> | ||
1127 | <listitem> | ||
1128 | <para>Message digest including keyed message digest (HMAC, CMAC)</para> | ||
1129 | </listitem> | ||
1130 | |||
1131 | <listitem> | ||
1132 | <para>Symmetric ciphers</para> | ||
1133 | </listitem> | ||
1134 | |||
1135 | <listitem> | ||
1136 | <para>AEAD ciphers</para> | ||
1137 | </listitem> | ||
1138 | |||
1139 | <listitem> | ||
1140 | <para>Random Number Generators</para> | ||
1141 | </listitem> | ||
1142 | </itemizedlist> | ||
1143 | |||
1144 | <para> | ||
1145 | The interface is provided via socket type using the type AF_ALG. | ||
1146 | In addition, the setsockopt option type is SOL_ALG. In case the | ||
1147 | user space header files do not export these flags yet, use the | ||
1148 | following macros: | ||
1149 | </para> | ||
1150 | |||
1151 | <programlisting> | ||
1152 | #ifndef AF_ALG | ||
1153 | #define AF_ALG 38 | ||
1154 | #endif | ||
1155 | #ifndef SOL_ALG | ||
1156 | #define SOL_ALG 279 | ||
1157 | #endif | ||
1158 | </programlisting> | ||
1159 | |||
1160 | <para> | ||
1161 | A cipher is accessed with the same name as done for the in-kernel | ||
1162 | API calls. This includes the generic vs. unique naming schema for | ||
1163 | ciphers as well as the enforcement of priorities for generic names. | ||
1164 | </para> | ||
1165 | |||
1166 | <para> | ||
1167 | To interact with the kernel crypto API, a socket must be | ||
1168 | created by the user space application. User space invokes the cipher | ||
1169 | operation with the send()/write() system call family. The result of the | ||
1170 | cipher operation is obtained with the read()/recv() system call family. | ||
1171 | </para> | ||
1172 | |||
1173 | <para> | ||
1174 | The following API calls assume that the socket descriptor | ||
1175 | is already opened by the user space application and discusses only | ||
1176 | the kernel crypto API specific invocations. | ||
1177 | </para> | ||
1178 | |||
1179 | <para> | ||
1180 | To initialize the socket interface, the following sequence has to | ||
1181 | be performed by the consumer: | ||
1182 | </para> | ||
1183 | |||
1184 | <orderedlist> | ||
1185 | <listitem> | ||
1186 | <para> | ||
1187 | Create a socket of type AF_ALG with the struct sockaddr_alg | ||
1188 | parameter specified below for the different cipher types. | ||
1189 | </para> | ||
1190 | </listitem> | ||
1191 | |||
1192 | <listitem> | ||
1193 | <para> | ||
1194 | Invoke bind with the socket descriptor | ||
1195 | </para> | ||
1196 | </listitem> | ||
1197 | |||
1198 | <listitem> | ||
1199 | <para> | ||
1200 | Invoke accept with the socket descriptor. The accept system call | ||
1201 | returns a new file descriptor that is to be used to interact with | ||
1202 | the particular cipher instance. When invoking send/write or recv/read | ||
1203 | system calls to send data to the kernel or obtain data from the | ||
1204 | kernel, the file descriptor returned by accept must be used. | ||
1205 | </para> | ||
1206 | </listitem> | ||
1207 | </orderedlist> | ||
1208 | </sect1> | ||
1209 | |||
1210 | <sect1><title>In-place Cipher operation</title> | ||
1211 | <para> | ||
1212 | Just like the in-kernel operation of the kernel crypto API, the user | ||
1213 | space interface allows the cipher operation in-place. That means that | ||
1214 | the input buffer used for the send/write system call and the output | ||
1215 | buffer used by the read/recv system call may be one and the same. | ||
1216 | This is of particular interest for symmetric cipher operations where a | ||
1217 | copying of the output data to its final destination can be avoided. | ||
1218 | </para> | ||
1219 | |||
1220 | <para> | ||
1221 | If a consumer on the other hand wants to maintain the plaintext and | ||
1222 | the ciphertext in different memory locations, all a consumer needs | ||
1223 | to do is to provide different memory pointers for the encryption and | ||
1224 | decryption operation. | ||
1225 | </para> | ||
1226 | </sect1> | ||
1227 | |||
1228 | <sect1><title>Message Digest API</title> | ||
1229 | <para> | ||
1230 | The message digest type to be used for the cipher operation is | ||
1231 | selected when invoking the bind syscall. bind requires the caller | ||
1232 | to provide a filled struct sockaddr data structure. This data | ||
1233 | structure must be filled as follows: | ||
1234 | </para> | ||
1235 | |||
1236 | <programlisting> | ||
1237 | struct sockaddr_alg sa = { | ||
1238 | .salg_family = AF_ALG, | ||
1239 | .salg_type = "hash", /* this selects the hash logic in the kernel */ | ||
1240 | .salg_name = "sha1" /* this is the cipher name */ | ||
1241 | }; | ||
1242 | </programlisting> | ||
1243 | |||
1244 | <para> | ||
1245 | The salg_type value "hash" applies to message digests and keyed | ||
1246 | message digests. Though, a keyed message digest is referenced by | ||
1247 | the appropriate salg_name. Please see below for the setsockopt | ||
1248 | interface that explains how the key can be set for a keyed message | ||
1249 | digest. | ||
1250 | </para> | ||
1251 | |||
1252 | <para> | ||
1253 | Using the send() system call, the application provides the data that | ||
1254 | should be processed with the message digest. The send system call | ||
1255 | allows the following flags to be specified: | ||
1256 | </para> | ||
1257 | |||
1258 | <itemizedlist> | ||
1259 | <listitem> | ||
1260 | <para> | ||
1261 | MSG_MORE: If this flag is set, the send system call acts like a | ||
1262 | message digest update function where the final hash is not | ||
1263 | yet calculated. If the flag is not set, the send system call | ||
1264 | calculates the final message digest immediately. | ||
1265 | </para> | ||
1266 | </listitem> | ||
1267 | </itemizedlist> | ||
1268 | |||
1269 | <para> | ||
1270 | With the recv() system call, the application can read the message | ||
1271 | digest from the kernel crypto API. If the buffer is too small for the | ||
1272 | message digest, the flag MSG_TRUNC is set by the kernel. | ||
1273 | </para> | ||
1274 | |||
1275 | <para> | ||
1276 | In order to set a message digest key, the calling application must use | ||
1277 | the setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC | ||
1278 | operation is performed without the initial HMAC state change caused by | ||
1279 | the key. | ||
1280 | </para> | ||
1281 | </sect1> | ||
1282 | |||
1283 | <sect1><title>Symmetric Cipher API</title> | ||
1284 | <para> | ||
1285 | The operation is very similar to the message digest discussion. | ||
1286 | During initialization, the struct sockaddr data structure must be | ||
1287 | filled as follows: | ||
1288 | </para> | ||
1289 | |||
1290 | <programlisting> | ||
1291 | struct sockaddr_alg sa = { | ||
1292 | .salg_family = AF_ALG, | ||
1293 | .salg_type = "skcipher", /* this selects the symmetric cipher */ | ||
1294 | .salg_name = "cbc(aes)" /* this is the cipher name */ | ||
1295 | }; | ||
1296 | </programlisting> | ||
1297 | |||
1298 | <para> | ||
1299 | Before data can be sent to the kernel using the write/send system | ||
1300 | call family, the consumer must set the key. The key setting is | ||
1301 | described with the setsockopt invocation below. | ||
1302 | </para> | ||
1303 | |||
1304 | <para> | ||
1305 | Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is | ||
1306 | specified with the data structure provided by the sendmsg() system call. | ||
1307 | </para> | ||
1308 | |||
1309 | <para> | ||
1310 | The sendmsg system call parameter of struct msghdr is embedded into the | ||
1311 | struct cmsghdr data structure. See recv(2) and cmsg(3) for more | ||
1312 | information on how the cmsghdr data structure is used together with the | ||
1313 | send/recv system call family. That cmsghdr data structure holds the | ||
1314 | following information specified with a separate header instances: | ||
1315 | </para> | ||
1316 | |||
1317 | <itemizedlist> | ||
1318 | <listitem> | ||
1319 | <para> | ||
1320 | specification of the cipher operation type with one of these flags: | ||
1321 | </para> | ||
1322 | <itemizedlist> | ||
1323 | <listitem> | ||
1324 | <para>ALG_OP_ENCRYPT - encryption of data</para> | ||
1325 | </listitem> | ||
1326 | <listitem> | ||
1327 | <para>ALG_OP_DECRYPT - decryption of data</para> | ||
1328 | </listitem> | ||
1329 | </itemizedlist> | ||
1330 | </listitem> | ||
1331 | |||
1332 | <listitem> | ||
1333 | <para> | ||
1334 | specification of the IV information marked with the flag ALG_SET_IV | ||
1335 | </para> | ||
1336 | </listitem> | ||
1337 | </itemizedlist> | ||
1338 | |||
1339 | <para> | ||
1340 | The send system call family allows the following flag to be specified: | ||
1341 | </para> | ||
1342 | |||
1343 | <itemizedlist> | ||
1344 | <listitem> | ||
1345 | <para> | ||
1346 | MSG_MORE: If this flag is set, the send system call acts like a | ||
1347 | cipher update function where more input data is expected | ||
1348 | with a subsequent invocation of the send system call. | ||
1349 | </para> | ||
1350 | </listitem> | ||
1351 | </itemizedlist> | ||
1352 | |||
1353 | <para> | ||
1354 | Note: The kernel reports -EINVAL for any unexpected data. The caller | ||
1355 | must make sure that all data matches the constraints given in | ||
1356 | /proc/crypto for the selected cipher. | ||
1357 | </para> | ||
1358 | |||
1359 | <para> | ||
1360 | With the recv() system call, the application can read the result of | ||
1361 | the cipher operation from the kernel crypto API. The output buffer | ||
1362 | must be at least as large as to hold all blocks of the encrypted or | ||
1363 | decrypted data. If the output data size is smaller, only as many | ||
1364 | blocks are returned that fit into that output buffer size. | ||
1365 | </para> | ||
1366 | </sect1> | ||
1367 | |||
1368 | <sect1><title>AEAD Cipher API</title> | ||
1369 | <para> | ||
1370 | The operation is very similar to the symmetric cipher discussion. | ||
1371 | During initialization, the struct sockaddr data structure must be | ||
1372 | filled as follows: | ||
1373 | </para> | ||
1374 | |||
1375 | <programlisting> | ||
1376 | struct sockaddr_alg sa = { | ||
1377 | .salg_family = AF_ALG, | ||
1378 | .salg_type = "aead", /* this selects the symmetric cipher */ | ||
1379 | .salg_name = "gcm(aes)" /* this is the cipher name */ | ||
1380 | }; | ||
1381 | </programlisting> | ||
1382 | |||
1383 | <para> | ||
1384 | Before data can be sent to the kernel using the write/send system | ||
1385 | call family, the consumer must set the key. The key setting is | ||
1386 | described with the setsockopt invocation below. | ||
1387 | </para> | ||
1388 | |||
1389 | <para> | ||
1390 | In addition, before data can be sent to the kernel using the | ||
1391 | write/send system call family, the consumer must set the authentication | ||
1392 | tag size. To set the authentication tag size, the caller must use the | ||
1393 | setsockopt invocation described below. | ||
1394 | </para> | ||
1395 | |||
1396 | <para> | ||
1397 | Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is | ||
1398 | specified with the data structure provided by the sendmsg() system call. | ||
1399 | </para> | ||
1400 | |||
1401 | <para> | ||
1402 | The sendmsg system call parameter of struct msghdr is embedded into the | ||
1403 | struct cmsghdr data structure. See recv(2) and cmsg(3) for more | ||
1404 | information on how the cmsghdr data structure is used together with the | ||
1405 | send/recv system call family. That cmsghdr data structure holds the | ||
1406 | following information specified with a separate header instances: | ||
1407 | </para> | ||
1408 | |||
1409 | <itemizedlist> | ||
1410 | <listitem> | ||
1411 | <para> | ||
1412 | specification of the cipher operation type with one of these flags: | ||
1413 | </para> | ||
1414 | <itemizedlist> | ||
1415 | <listitem> | ||
1416 | <para>ALG_OP_ENCRYPT - encryption of data</para> | ||
1417 | </listitem> | ||
1418 | <listitem> | ||
1419 | <para>ALG_OP_DECRYPT - decryption of data</para> | ||
1420 | </listitem> | ||
1421 | </itemizedlist> | ||
1422 | </listitem> | ||
1423 | |||
1424 | <listitem> | ||
1425 | <para> | ||
1426 | specification of the IV information marked with the flag ALG_SET_IV | ||
1427 | </para> | ||
1428 | </listitem> | ||
1429 | |||
1430 | <listitem> | ||
1431 | <para> | ||
1432 | specification of the associated authentication data (AAD) with the | ||
1433 | flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together | ||
1434 | with the plaintext / ciphertext. See below for the memory structure. | ||
1435 | </para> | ||
1436 | </listitem> | ||
1437 | </itemizedlist> | ||
1438 | |||
1439 | <para> | ||
1440 | The send system call family allows the following flag to be specified: | ||
1441 | </para> | ||
1442 | |||
1443 | <itemizedlist> | ||
1444 | <listitem> | ||
1445 | <para> | ||
1446 | MSG_MORE: If this flag is set, the send system call acts like a | ||
1447 | cipher update function where more input data is expected | ||
1448 | with a subsequent invocation of the send system call. | ||
1449 | </para> | ||
1450 | </listitem> | ||
1451 | </itemizedlist> | ||
1452 | |||
1453 | <para> | ||
1454 | Note: The kernel reports -EINVAL for any unexpected data. The caller | ||
1455 | must make sure that all data matches the constraints given in | ||
1456 | /proc/crypto for the selected cipher. | ||
1457 | </para> | ||
1458 | |||
1459 | <para> | ||
1460 | With the recv() system call, the application can read the result of | ||
1461 | the cipher operation from the kernel crypto API. The output buffer | ||
1462 | must be at least as large as defined with the memory structure below. | ||
1463 | If the output data size is smaller, the cipher operation is not performed. | ||
1464 | </para> | ||
1465 | |||
1466 | <para> | ||
1467 | The authenticated decryption operation may indicate an integrity error. | ||
1468 | Such breach in integrity is marked with the -EBADMSG error code. | ||
1469 | </para> | ||
1470 | |||
1471 | <sect2><title>AEAD Memory Structure</title> | ||
1472 | <para> | ||
1473 | The AEAD cipher operates with the following information that | ||
1474 | is communicated between user and kernel space as one data stream: | ||
1475 | </para> | ||
1476 | |||
1477 | <itemizedlist> | ||
1478 | <listitem> | ||
1479 | <para>plaintext or ciphertext</para> | ||
1480 | </listitem> | ||
1481 | |||
1482 | <listitem> | ||
1483 | <para>associated authentication data (AAD)</para> | ||
1484 | </listitem> | ||
1485 | |||
1486 | <listitem> | ||
1487 | <para>authentication tag</para> | ||
1488 | </listitem> | ||
1489 | </itemizedlist> | ||
1490 | |||
1491 | <para> | ||
1492 | The sizes of the AAD and the authentication tag are provided with | ||
1493 | the sendmsg and setsockopt calls (see there). As the kernel knows | ||
1494 | the size of the entire data stream, the kernel is now able to | ||
1495 | calculate the right offsets of the data components in the data | ||
1496 | stream. | ||
1497 | </para> | ||
1498 | |||
1499 | <para> | ||
1500 | The user space caller must arrange the aforementioned information | ||
1501 | in the following order: | ||
1502 | </para> | ||
1503 | |||
1504 | <itemizedlist> | ||
1505 | <listitem> | ||
1506 | <para> | ||
1507 | AEAD encryption input: AAD || plaintext | ||
1508 | </para> | ||
1509 | </listitem> | ||
1510 | |||
1511 | <listitem> | ||
1512 | <para> | ||
1513 | AEAD decryption input: AAD || ciphertext || authentication tag | ||
1514 | </para> | ||
1515 | </listitem> | ||
1516 | </itemizedlist> | ||
1517 | |||
1518 | <para> | ||
1519 | The output buffer the user space caller provides must be at least as | ||
1520 | large to hold the following data: | ||
1521 | </para> | ||
1522 | |||
1523 | <itemizedlist> | ||
1524 | <listitem> | ||
1525 | <para> | ||
1526 | AEAD encryption output: ciphertext || authentication tag | ||
1527 | </para> | ||
1528 | </listitem> | ||
1529 | |||
1530 | <listitem> | ||
1531 | <para> | ||
1532 | AEAD decryption output: plaintext | ||
1533 | </para> | ||
1534 | </listitem> | ||
1535 | </itemizedlist> | ||
1536 | </sect2> | ||
1537 | </sect1> | ||
1538 | |||
1539 | <sect1><title>Random Number Generator API</title> | ||
1540 | <para> | ||
1541 | Again, the operation is very similar to the other APIs. | ||
1542 | During initialization, the struct sockaddr data structure must be | ||
1543 | filled as follows: | ||
1544 | </para> | ||
1545 | |||
1546 | <programlisting> | ||
1547 | struct sockaddr_alg sa = { | ||
1548 | .salg_family = AF_ALG, | ||
1549 | .salg_type = "rng", /* this selects the symmetric cipher */ | ||
1550 | .salg_name = "drbg_nopr_sha256" /* this is the cipher name */ | ||
1551 | }; | ||
1552 | </programlisting> | ||
1553 | |||
1554 | <para> | ||
1555 | Depending on the RNG type, the RNG must be seeded. The seed is provided | ||
1556 | using the setsockopt interface to set the key. For example, the | ||
1557 | ansi_cprng requires a seed. The DRBGs do not require a seed, but | ||
1558 | may be seeded. | ||
1559 | </para> | ||
1560 | |||
1561 | <para> | ||
1562 | Using the read()/recvmsg() system calls, random numbers can be obtained. | ||
1563 | The kernel generates at most 128 bytes in one call. If user space | ||
1564 | requires more data, multiple calls to read()/recvmsg() must be made. | ||
1565 | </para> | ||
1566 | |||
1567 | <para> | ||
1568 | WARNING: The user space caller may invoke the initially mentioned | ||
1569 | accept system call multiple times. In this case, the returned file | ||
1570 | descriptors have the same state. | ||
1571 | </para> | ||
1572 | |||
1573 | </sect1> | ||
1574 | |||
1575 | <sect1><title>Zero-Copy Interface</title> | ||
1576 | <para> | ||
1577 | In addition to the send/write/read/recv system call family, the AF_ALG | ||
1578 | interface can be accessed with the zero-copy interface of splice/vmsplice. | ||
1579 | As the name indicates, the kernel tries to avoid a copy operation into | ||
1580 | kernel space. | ||
1581 | </para> | ||
1582 | |||
1583 | <para> | ||
1584 | The zero-copy operation requires data to be aligned at the page boundary. | ||
1585 | Non-aligned data can be used as well, but may require more operations of | ||
1586 | the kernel which would defeat the speed gains obtained from the zero-copy | ||
1587 | interface. | ||
1588 | </para> | ||
1589 | |||
1590 | <para> | ||
1591 | The system-interent limit for the size of one zero-copy operation is | ||
1592 | 16 pages. If more data is to be sent to AF_ALG, user space must slice | ||
1593 | the input into segments with a maximum size of 16 pages. | ||
1594 | </para> | ||
1595 | |||
1596 | <para> | ||
1597 | Zero-copy can be used with the following code example (a complete working | ||
1598 | example is provided with libkcapi): | ||
1599 | </para> | ||
1600 | |||
1601 | <programlisting> | ||
1602 | int pipes[2]; | ||
1603 | |||
1604 | pipe(pipes); | ||
1605 | /* input data in iov */ | ||
1606 | vmsplice(pipes[1], iov, iovlen, SPLICE_F_GIFT); | ||
1607 | /* opfd is the file descriptor returned from accept() system call */ | ||
1608 | splice(pipes[0], NULL, opfd, NULL, ret, 0); | ||
1609 | read(opfd, out, outlen); | ||
1610 | </programlisting> | ||
1611 | |||
1612 | </sect1> | ||
1613 | |||
1614 | <sect1><title>Setsockopt Interface</title> | ||
1615 | <para> | ||
1616 | In addition to the read/recv and send/write system call handling | ||
1617 | to send and retrieve data subject to the cipher operation, a consumer | ||
1618 | also needs to set the additional information for the cipher operation. | ||
1619 | This additional information is set using the setsockopt system call | ||
1620 | that must be invoked with the file descriptor of the open cipher | ||
1621 | (i.e. the file descriptor returned by the accept system call). | ||
1622 | </para> | ||
1623 | |||
1624 | <para> | ||
1625 | Each setsockopt invocation must use the level SOL_ALG. | ||
1626 | </para> | ||
1627 | |||
1628 | <para> | ||
1629 | The setsockopt interface allows setting the following data using | ||
1630 | the mentioned optname: | ||
1631 | </para> | ||
1632 | |||
1633 | <itemizedlist> | ||
1634 | <listitem> | ||
1635 | <para> | ||
1636 | ALG_SET_KEY -- Setting the key. Key setting is applicable to: | ||
1637 | </para> | ||
1638 | <itemizedlist> | ||
1639 | <listitem> | ||
1640 | <para>the skcipher cipher type (symmetric ciphers)</para> | ||
1641 | </listitem> | ||
1642 | <listitem> | ||
1643 | <para>the hash cipher type (keyed message digests)</para> | ||
1644 | </listitem> | ||
1645 | <listitem> | ||
1646 | <para>the AEAD cipher type</para> | ||
1647 | </listitem> | ||
1648 | <listitem> | ||
1649 | <para>the RNG cipher type to provide the seed</para> | ||
1650 | </listitem> | ||
1651 | </itemizedlist> | ||
1652 | </listitem> | ||
1653 | |||
1654 | <listitem> | ||
1655 | <para> | ||
1656 | ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size | ||
1657 | for AEAD ciphers. For a encryption operation, the authentication | ||
1658 | tag of the given size will be generated. For a decryption operation, | ||
1659 | the provided ciphertext is assumed to contain an authentication tag | ||
1660 | of the given size (see section about AEAD memory layout below). | ||
1661 | </para> | ||
1662 | </listitem> | ||
1663 | </itemizedlist> | ||
1664 | |||
1665 | </sect1> | ||
1666 | |||
1667 | <sect1><title>User space API example</title> | ||
1668 | <para> | ||
1669 | Please see [1] for libkcapi which provides an easy-to-use wrapper | ||
1670 | around the aforementioned Netlink kernel interface. [1] also contains | ||
1671 | a test application that invokes all libkcapi API calls. | ||
1672 | </para> | ||
1673 | |||
1674 | <para> | ||
1675 | [1] <ulink url="http://www.chronox.de/libkcapi.html">http://www.chronox.de/libkcapi.html</ulink> | ||
1676 | </para> | ||
1677 | |||
1678 | </sect1> | ||
1679 | |||
1680 | </chapter> | ||
1681 | |||
1682 | <chapter id="API"><title>Programming Interface</title> | ||
1683 | <para> | ||
1684 | Please note that the kernel crypto API contains the AEAD givcrypt | ||
1685 | API (crypto_aead_giv* and aead_givcrypt_* function calls in | ||
1686 | include/crypto/aead.h). This API is obsolete and will be removed | ||
1687 | in the future. To obtain the functionality of an AEAD cipher with | ||
1688 | internal IV generation, use the IV generator as a regular cipher. | ||
1689 | For example, rfc4106(gcm(aes)) is the AEAD cipher with external | ||
1690 | IV generation and seqniv(rfc4106(gcm(aes))) implies that the kernel | ||
1691 | crypto API generates the IV. Different IV generators are available. | ||
1692 | </para> | ||
1693 | <sect1><title>Block Cipher Context Data Structures</title> | ||
1694 | !Pinclude/linux/crypto.h Block Cipher Context Data Structures | ||
1695 | !Finclude/crypto/aead.h aead_request | ||
1696 | </sect1> | ||
1697 | <sect1><title>Block Cipher Algorithm Definitions</title> | ||
1698 | !Pinclude/linux/crypto.h Block Cipher Algorithm Definitions | ||
1699 | !Finclude/linux/crypto.h crypto_alg | ||
1700 | !Finclude/linux/crypto.h ablkcipher_alg | ||
1701 | !Finclude/crypto/aead.h aead_alg | ||
1702 | !Finclude/linux/crypto.h blkcipher_alg | ||
1703 | !Finclude/linux/crypto.h cipher_alg | ||
1704 | !Finclude/crypto/rng.h rng_alg | ||
1705 | </sect1> | ||
1706 | <sect1><title>Symmetric Key Cipher API</title> | ||
1707 | !Pinclude/crypto/skcipher.h Symmetric Key Cipher API | ||
1708 | !Finclude/crypto/skcipher.h crypto_alloc_skcipher | ||
1709 | !Finclude/crypto/skcipher.h crypto_free_skcipher | ||
1710 | !Finclude/crypto/skcipher.h crypto_has_skcipher | ||
1711 | !Finclude/crypto/skcipher.h crypto_skcipher_ivsize | ||
1712 | !Finclude/crypto/skcipher.h crypto_skcipher_blocksize | ||
1713 | !Finclude/crypto/skcipher.h crypto_skcipher_setkey | ||
1714 | !Finclude/crypto/skcipher.h crypto_skcipher_reqtfm | ||
1715 | !Finclude/crypto/skcipher.h crypto_skcipher_encrypt | ||
1716 | !Finclude/crypto/skcipher.h crypto_skcipher_decrypt | ||
1717 | </sect1> | ||
1718 | <sect1><title>Symmetric Key Cipher Request Handle</title> | ||
1719 | !Pinclude/crypto/skcipher.h Symmetric Key Cipher Request Handle | ||
1720 | !Finclude/crypto/skcipher.h crypto_skcipher_reqsize | ||
1721 | !Finclude/crypto/skcipher.h skcipher_request_set_tfm | ||
1722 | !Finclude/crypto/skcipher.h skcipher_request_alloc | ||
1723 | !Finclude/crypto/skcipher.h skcipher_request_free | ||
1724 | !Finclude/crypto/skcipher.h skcipher_request_set_callback | ||
1725 | !Finclude/crypto/skcipher.h skcipher_request_set_crypt | ||
1726 | </sect1> | ||
1727 | <sect1><title>Asynchronous Block Cipher API - Deprecated</title> | ||
1728 | !Pinclude/linux/crypto.h Asynchronous Block Cipher API | ||
1729 | !Finclude/linux/crypto.h crypto_alloc_ablkcipher | ||
1730 | !Finclude/linux/crypto.h crypto_free_ablkcipher | ||
1731 | !Finclude/linux/crypto.h crypto_has_ablkcipher | ||
1732 | !Finclude/linux/crypto.h crypto_ablkcipher_ivsize | ||
1733 | !Finclude/linux/crypto.h crypto_ablkcipher_blocksize | ||
1734 | !Finclude/linux/crypto.h crypto_ablkcipher_setkey | ||
1735 | !Finclude/linux/crypto.h crypto_ablkcipher_reqtfm | ||
1736 | !Finclude/linux/crypto.h crypto_ablkcipher_encrypt | ||
1737 | !Finclude/linux/crypto.h crypto_ablkcipher_decrypt | ||
1738 | </sect1> | ||
1739 | <sect1><title>Asynchronous Cipher Request Handle - Deprecated</title> | ||
1740 | !Pinclude/linux/crypto.h Asynchronous Cipher Request Handle | ||
1741 | !Finclude/linux/crypto.h crypto_ablkcipher_reqsize | ||
1742 | !Finclude/linux/crypto.h ablkcipher_request_set_tfm | ||
1743 | !Finclude/linux/crypto.h ablkcipher_request_alloc | ||
1744 | !Finclude/linux/crypto.h ablkcipher_request_free | ||
1745 | !Finclude/linux/crypto.h ablkcipher_request_set_callback | ||
1746 | !Finclude/linux/crypto.h ablkcipher_request_set_crypt | ||
1747 | </sect1> | ||
1748 | <sect1><title>Authenticated Encryption With Associated Data (AEAD) Cipher API</title> | ||
1749 | !Pinclude/crypto/aead.h Authenticated Encryption With Associated Data (AEAD) Cipher API | ||
1750 | !Finclude/crypto/aead.h crypto_alloc_aead | ||
1751 | !Finclude/crypto/aead.h crypto_free_aead | ||
1752 | !Finclude/crypto/aead.h crypto_aead_ivsize | ||
1753 | !Finclude/crypto/aead.h crypto_aead_authsize | ||
1754 | !Finclude/crypto/aead.h crypto_aead_blocksize | ||
1755 | !Finclude/crypto/aead.h crypto_aead_setkey | ||
1756 | !Finclude/crypto/aead.h crypto_aead_setauthsize | ||
1757 | !Finclude/crypto/aead.h crypto_aead_encrypt | ||
1758 | !Finclude/crypto/aead.h crypto_aead_decrypt | ||
1759 | </sect1> | ||
1760 | <sect1><title>Asynchronous AEAD Request Handle</title> | ||
1761 | !Pinclude/crypto/aead.h Asynchronous AEAD Request Handle | ||
1762 | !Finclude/crypto/aead.h crypto_aead_reqsize | ||
1763 | !Finclude/crypto/aead.h aead_request_set_tfm | ||
1764 | !Finclude/crypto/aead.h aead_request_alloc | ||
1765 | !Finclude/crypto/aead.h aead_request_free | ||
1766 | !Finclude/crypto/aead.h aead_request_set_callback | ||
1767 | !Finclude/crypto/aead.h aead_request_set_crypt | ||
1768 | !Finclude/crypto/aead.h aead_request_set_ad | ||
1769 | </sect1> | ||
1770 | <sect1><title>Synchronous Block Cipher API - Deprecated</title> | ||
1771 | !Pinclude/linux/crypto.h Synchronous Block Cipher API | ||
1772 | !Finclude/linux/crypto.h crypto_alloc_blkcipher | ||
1773 | !Finclude/linux/crypto.h crypto_free_blkcipher | ||
1774 | !Finclude/linux/crypto.h crypto_has_blkcipher | ||
1775 | !Finclude/linux/crypto.h crypto_blkcipher_name | ||
1776 | !Finclude/linux/crypto.h crypto_blkcipher_ivsize | ||
1777 | !Finclude/linux/crypto.h crypto_blkcipher_blocksize | ||
1778 | !Finclude/linux/crypto.h crypto_blkcipher_setkey | ||
1779 | !Finclude/linux/crypto.h crypto_blkcipher_encrypt | ||
1780 | !Finclude/linux/crypto.h crypto_blkcipher_encrypt_iv | ||
1781 | !Finclude/linux/crypto.h crypto_blkcipher_decrypt | ||
1782 | !Finclude/linux/crypto.h crypto_blkcipher_decrypt_iv | ||
1783 | !Finclude/linux/crypto.h crypto_blkcipher_set_iv | ||
1784 | !Finclude/linux/crypto.h crypto_blkcipher_get_iv | ||
1785 | </sect1> | ||
1786 | <sect1><title>Single Block Cipher API</title> | ||
1787 | !Pinclude/linux/crypto.h Single Block Cipher API | ||
1788 | !Finclude/linux/crypto.h crypto_alloc_cipher | ||
1789 | !Finclude/linux/crypto.h crypto_free_cipher | ||
1790 | !Finclude/linux/crypto.h crypto_has_cipher | ||
1791 | !Finclude/linux/crypto.h crypto_cipher_blocksize | ||
1792 | !Finclude/linux/crypto.h crypto_cipher_setkey | ||
1793 | !Finclude/linux/crypto.h crypto_cipher_encrypt_one | ||
1794 | !Finclude/linux/crypto.h crypto_cipher_decrypt_one | ||
1795 | </sect1> | ||
1796 | <sect1><title>Message Digest Algorithm Definitions</title> | ||
1797 | !Pinclude/crypto/hash.h Message Digest Algorithm Definitions | ||
1798 | !Finclude/crypto/hash.h hash_alg_common | ||
1799 | !Finclude/crypto/hash.h ahash_alg | ||
1800 | !Finclude/crypto/hash.h shash_alg | ||
1801 | </sect1> | ||
1802 | <sect1><title>Asynchronous Message Digest API</title> | ||
1803 | !Pinclude/crypto/hash.h Asynchronous Message Digest API | ||
1804 | !Finclude/crypto/hash.h crypto_alloc_ahash | ||
1805 | !Finclude/crypto/hash.h crypto_free_ahash | ||
1806 | !Finclude/crypto/hash.h crypto_ahash_init | ||
1807 | !Finclude/crypto/hash.h crypto_ahash_digestsize | ||
1808 | !Finclude/crypto/hash.h crypto_ahash_reqtfm | ||
1809 | !Finclude/crypto/hash.h crypto_ahash_reqsize | ||
1810 | !Finclude/crypto/hash.h crypto_ahash_setkey | ||
1811 | !Finclude/crypto/hash.h crypto_ahash_finup | ||
1812 | !Finclude/crypto/hash.h crypto_ahash_final | ||
1813 | !Finclude/crypto/hash.h crypto_ahash_digest | ||
1814 | !Finclude/crypto/hash.h crypto_ahash_export | ||
1815 | !Finclude/crypto/hash.h crypto_ahash_import | ||
1816 | </sect1> | ||
1817 | <sect1><title>Asynchronous Hash Request Handle</title> | ||
1818 | !Pinclude/crypto/hash.h Asynchronous Hash Request Handle | ||
1819 | !Finclude/crypto/hash.h ahash_request_set_tfm | ||
1820 | !Finclude/crypto/hash.h ahash_request_alloc | ||
1821 | !Finclude/crypto/hash.h ahash_request_free | ||
1822 | !Finclude/crypto/hash.h ahash_request_set_callback | ||
1823 | !Finclude/crypto/hash.h ahash_request_set_crypt | ||
1824 | </sect1> | ||
1825 | <sect1><title>Synchronous Message Digest API</title> | ||
1826 | !Pinclude/crypto/hash.h Synchronous Message Digest API | ||
1827 | !Finclude/crypto/hash.h crypto_alloc_shash | ||
1828 | !Finclude/crypto/hash.h crypto_free_shash | ||
1829 | !Finclude/crypto/hash.h crypto_shash_blocksize | ||
1830 | !Finclude/crypto/hash.h crypto_shash_digestsize | ||
1831 | !Finclude/crypto/hash.h crypto_shash_descsize | ||
1832 | !Finclude/crypto/hash.h crypto_shash_setkey | ||
1833 | !Finclude/crypto/hash.h crypto_shash_digest | ||
1834 | !Finclude/crypto/hash.h crypto_shash_export | ||
1835 | !Finclude/crypto/hash.h crypto_shash_import | ||
1836 | !Finclude/crypto/hash.h crypto_shash_init | ||
1837 | !Finclude/crypto/hash.h crypto_shash_update | ||
1838 | !Finclude/crypto/hash.h crypto_shash_final | ||
1839 | !Finclude/crypto/hash.h crypto_shash_finup | ||
1840 | </sect1> | ||
1841 | <sect1><title>Crypto API Random Number API</title> | ||
1842 | !Pinclude/crypto/rng.h Random number generator API | ||
1843 | !Finclude/crypto/rng.h crypto_alloc_rng | ||
1844 | !Finclude/crypto/rng.h crypto_rng_alg | ||
1845 | !Finclude/crypto/rng.h crypto_free_rng | ||
1846 | !Finclude/crypto/rng.h crypto_rng_generate | ||
1847 | !Finclude/crypto/rng.h crypto_rng_get_bytes | ||
1848 | !Finclude/crypto/rng.h crypto_rng_reset | ||
1849 | !Finclude/crypto/rng.h crypto_rng_seedsize | ||
1850 | !Cinclude/crypto/rng.h | ||
1851 | </sect1> | ||
1852 | <sect1><title>Asymmetric Cipher API</title> | ||
1853 | !Pinclude/crypto/akcipher.h Generic Public Key API | ||
1854 | !Finclude/crypto/akcipher.h akcipher_alg | ||
1855 | !Finclude/crypto/akcipher.h akcipher_request | ||
1856 | !Finclude/crypto/akcipher.h crypto_alloc_akcipher | ||
1857 | !Finclude/crypto/akcipher.h crypto_free_akcipher | ||
1858 | !Finclude/crypto/akcipher.h crypto_akcipher_set_pub_key | ||
1859 | !Finclude/crypto/akcipher.h crypto_akcipher_set_priv_key | ||
1860 | </sect1> | ||
1861 | <sect1><title>Asymmetric Cipher Request Handle</title> | ||
1862 | !Finclude/crypto/akcipher.h akcipher_request_alloc | ||
1863 | !Finclude/crypto/akcipher.h akcipher_request_free | ||
1864 | !Finclude/crypto/akcipher.h akcipher_request_set_callback | ||
1865 | !Finclude/crypto/akcipher.h akcipher_request_set_crypt | ||
1866 | !Finclude/crypto/akcipher.h crypto_akcipher_maxsize | ||
1867 | !Finclude/crypto/akcipher.h crypto_akcipher_encrypt | ||
1868 | !Finclude/crypto/akcipher.h crypto_akcipher_decrypt | ||
1869 | !Finclude/crypto/akcipher.h crypto_akcipher_sign | ||
1870 | !Finclude/crypto/akcipher.h crypto_akcipher_verify | ||
1871 | </sect1> | ||
1872 | </chapter> | ||
1873 | |||
1874 | <chapter id="Code"><title>Code Examples</title> | ||
1875 | <sect1><title>Code Example For Symmetric Key Cipher Operation</title> | ||
1876 | <programlisting> | ||
1877 | |||
1878 | struct tcrypt_result { | ||
1879 | struct completion completion; | ||
1880 | int err; | ||
1881 | }; | ||
1882 | |||
1883 | /* tie all data structures together */ | ||
1884 | struct skcipher_def { | ||
1885 | struct scatterlist sg; | ||
1886 | struct crypto_skcipher *tfm; | ||
1887 | struct skcipher_request *req; | ||
1888 | struct tcrypt_result result; | ||
1889 | }; | ||
1890 | |||
1891 | /* Callback function */ | ||
1892 | static void test_skcipher_cb(struct crypto_async_request *req, int error) | ||
1893 | { | ||
1894 | struct tcrypt_result *result = req->data; | ||
1895 | |||
1896 | if (error == -EINPROGRESS) | ||
1897 | return; | ||
1898 | result->err = error; | ||
1899 | complete(&result->completion); | ||
1900 | pr_info("Encryption finished successfully\n"); | ||
1901 | } | ||
1902 | |||
1903 | /* Perform cipher operation */ | ||
1904 | static unsigned int test_skcipher_encdec(struct skcipher_def *sk, | ||
1905 | int enc) | ||
1906 | { | ||
1907 | int rc = 0; | ||
1908 | |||
1909 | if (enc) | ||
1910 | rc = crypto_skcipher_encrypt(sk->req); | ||
1911 | else | ||
1912 | rc = crypto_skcipher_decrypt(sk->req); | ||
1913 | |||
1914 | switch (rc) { | ||
1915 | case 0: | ||
1916 | break; | ||
1917 | case -EINPROGRESS: | ||
1918 | case -EBUSY: | ||
1919 | rc = wait_for_completion_interruptible( | ||
1920 | &sk->result.completion); | ||
1921 | if (!rc && !sk->result.err) { | ||
1922 | reinit_completion(&sk->result.completion); | ||
1923 | break; | ||
1924 | } | ||
1925 | default: | ||
1926 | pr_info("skcipher encrypt returned with %d result %d\n", | ||
1927 | rc, sk->result.err); | ||
1928 | break; | ||
1929 | } | ||
1930 | init_completion(&sk->result.completion); | ||
1931 | |||
1932 | return rc; | ||
1933 | } | ||
1934 | |||
1935 | /* Initialize and trigger cipher operation */ | ||
1936 | static int test_skcipher(void) | ||
1937 | { | ||
1938 | struct skcipher_def sk; | ||
1939 | struct crypto_skcipher *skcipher = NULL; | ||
1940 | struct skcipher_request *req = NULL; | ||
1941 | char *scratchpad = NULL; | ||
1942 | char *ivdata = NULL; | ||
1943 | unsigned char key[32]; | ||
1944 | int ret = -EFAULT; | ||
1945 | |||
1946 | skcipher = crypto_alloc_skcipher("cbc-aes-aesni", 0, 0); | ||
1947 | if (IS_ERR(skcipher)) { | ||
1948 | pr_info("could not allocate skcipher handle\n"); | ||
1949 | return PTR_ERR(skcipher); | ||
1950 | } | ||
1951 | |||
1952 | req = skcipher_request_alloc(skcipher, GFP_KERNEL); | ||
1953 | if (!req) { | ||
1954 | pr_info("could not allocate skcipher request\n"); | ||
1955 | ret = -ENOMEM; | ||
1956 | goto out; | ||
1957 | } | ||
1958 | |||
1959 | skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, | ||
1960 | test_skcipher_cb, | ||
1961 | &sk.result); | ||
1962 | |||
1963 | /* AES 256 with random key */ | ||
1964 | get_random_bytes(&key, 32); | ||
1965 | if (crypto_skcipher_setkey(skcipher, key, 32)) { | ||
1966 | pr_info("key could not be set\n"); | ||
1967 | ret = -EAGAIN; | ||
1968 | goto out; | ||
1969 | } | ||
1970 | |||
1971 | /* IV will be random */ | ||
1972 | ivdata = kmalloc(16, GFP_KERNEL); | ||
1973 | if (!ivdata) { | ||
1974 | pr_info("could not allocate ivdata\n"); | ||
1975 | goto out; | ||
1976 | } | ||
1977 | get_random_bytes(ivdata, 16); | ||
1978 | |||
1979 | /* Input data will be random */ | ||
1980 | scratchpad = kmalloc(16, GFP_KERNEL); | ||
1981 | if (!scratchpad) { | ||
1982 | pr_info("could not allocate scratchpad\n"); | ||
1983 | goto out; | ||
1984 | } | ||
1985 | get_random_bytes(scratchpad, 16); | ||
1986 | |||
1987 | sk.tfm = skcipher; | ||
1988 | sk.req = req; | ||
1989 | |||
1990 | /* We encrypt one block */ | ||
1991 | sg_init_one(&sk.sg, scratchpad, 16); | ||
1992 | skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata); | ||
1993 | init_completion(&sk.result.completion); | ||
1994 | |||
1995 | /* encrypt data */ | ||
1996 | ret = test_skcipher_encdec(&sk, 1); | ||
1997 | if (ret) | ||
1998 | goto out; | ||
1999 | |||
2000 | pr_info("Encryption triggered successfully\n"); | ||
2001 | |||
2002 | out: | ||
2003 | if (skcipher) | ||
2004 | crypto_free_skcipher(skcipher); | ||
2005 | if (req) | ||
2006 | skcipher_request_free(req); | ||
2007 | if (ivdata) | ||
2008 | kfree(ivdata); | ||
2009 | if (scratchpad) | ||
2010 | kfree(scratchpad); | ||
2011 | return ret; | ||
2012 | } | ||
2013 | </programlisting> | ||
2014 | </sect1> | ||
2015 | |||
2016 | <sect1><title>Code Example For Use of Operational State Memory With SHASH</title> | ||
2017 | <programlisting> | ||
2018 | |||
2019 | struct sdesc { | ||
2020 | struct shash_desc shash; | ||
2021 | char ctx[]; | ||
2022 | }; | ||
2023 | |||
2024 | static struct sdescinit_sdesc(struct crypto_shash *alg) | ||
2025 | { | ||
2026 | struct sdescsdesc; | ||
2027 | int size; | ||
2028 | |||
2029 | size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); | ||
2030 | sdesc = kmalloc(size, GFP_KERNEL); | ||
2031 | if (!sdesc) | ||
2032 | return ERR_PTR(-ENOMEM); | ||
2033 | sdesc->shash.tfm = alg; | ||
2034 | sdesc->shash.flags = 0x0; | ||
2035 | return sdesc; | ||
2036 | } | ||
2037 | |||
2038 | static int calc_hash(struct crypto_shashalg, | ||
2039 | const unsigned chardata, unsigned int datalen, | ||
2040 | unsigned chardigest) { | ||
2041 | struct sdescsdesc; | ||
2042 | int ret; | ||
2043 | |||
2044 | sdesc = init_sdesc(alg); | ||
2045 | if (IS_ERR(sdesc)) { | ||
2046 | pr_info("trusted_key: can't alloc %s\n", hash_alg); | ||
2047 | return PTR_ERR(sdesc); | ||
2048 | } | ||
2049 | |||
2050 | ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest); | ||
2051 | kfree(sdesc); | ||
2052 | return ret; | ||
2053 | } | ||
2054 | </programlisting> | ||
2055 | </sect1> | ||
2056 | |||
2057 | <sect1><title>Code Example For Random Number Generator Usage</title> | ||
2058 | <programlisting> | ||
2059 | |||
2060 | static int get_random_numbers(u8 *buf, unsigned int len) | ||
2061 | { | ||
2062 | struct crypto_rngrng = NULL; | ||
2063 | chardrbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */ | ||
2064 | int ret; | ||
2065 | |||
2066 | if (!buf || !len) { | ||
2067 | pr_debug("No output buffer provided\n"); | ||
2068 | return -EINVAL; | ||
2069 | } | ||
2070 | |||
2071 | rng = crypto_alloc_rng(drbg, 0, 0); | ||
2072 | if (IS_ERR(rng)) { | ||
2073 | pr_debug("could not allocate RNG handle for %s\n", drbg); | ||
2074 | return -PTR_ERR(rng); | ||
2075 | } | ||
2076 | |||
2077 | ret = crypto_rng_get_bytes(rng, buf, len); | ||
2078 | if (ret < 0) | ||
2079 | pr_debug("generation of random numbers failed\n"); | ||
2080 | else if (ret == 0) | ||
2081 | pr_debug("RNG returned no data"); | ||
2082 | else | ||
2083 | pr_debug("RNG returned %d bytes of data\n", ret); | ||
2084 | |||
2085 | out: | ||
2086 | crypto_free_rng(rng); | ||
2087 | return ret; | ||
2088 | } | ||
2089 | </programlisting> | ||
2090 | </sect1> | ||
2091 | </chapter> | ||
2092 | </book> | ||