]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - Documentation/DocBook/crypto-API.tmpl
crypto: doc - add akcipher API
[mirror_ubuntu-artful-kernel.git] / Documentation / DocBook / crypto-API.tmpl
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>blkcipher for synchronous block ciphers</para>
352 </listitem>
353 <listitem>
354 <para>ablkcipher for asynchronous block ciphers</para>
355 </listitem>
356 <listitem>
357 <para>cipher for single block ciphers that may be used with
358 an additional template</para>
359 </listitem>
360 <listitem>
361 <para>shash for synchronous message digest</para>
362 </listitem>
363 <listitem>
364 <para>ahash for asynchronous message digest</para>
365 </listitem>
366 <listitem>
367 <para>aead for AEAD cipher type</para>
368 </listitem>
369 <listitem>
370 <para>compression for compression type transformations</para>
371 </listitem>
372 <listitem>
373 <para>rng for random number generator</para>
374 </listitem>
375 <listitem>
376 <para>givcipher for cipher with associated IV generator
377 (see the geniv entry below for the specification of the
378 IV generator type used by the cipher implementation)</para>
379 </listitem>
380 </itemizedlist>
381 </para>
382 </listitem>
383 <listitem>
384 <para>blocksize: blocksize of cipher in bytes</para>
385 </listitem>
386 <listitem>
387 <para>keysize: key size in bytes</para>
388 </listitem>
389 <listitem>
390 <para>ivsize: IV size in bytes</para>
391 </listitem>
392 <listitem>
393 <para>seedsize: required size of seed data for random number
394 generator</para>
395 </listitem>
396 <listitem>
397 <para>digestsize: output size of the message digest</para>
398 </listitem>
399 <listitem>
400 <para>geniv: IV generation type:
401 <itemizedlist>
402 <listitem>
403 <para>eseqiv for encrypted sequence number based IV
404 generation</para>
405 </listitem>
406 <listitem>
407 <para>seqiv for sequence number based IV generation</para>
408 </listitem>
409 <listitem>
410 <para>chainiv for chain iv generation</para>
411 </listitem>
412 <listitem>
413 <para>&lt;builtin&gt; is a marker that the cipher implements
414 IV generation and handling as it is specific to the given
415 cipher</para>
416 </listitem>
417 </itemizedlist>
418 </para>
419 </listitem>
420 </itemizedlist>
421 </sect1>
422
423 <sect1><title>Key Sizes</title>
424 <para>
425 When allocating a cipher handle, the caller only specifies the
426 cipher type. Symmetric ciphers, however, typically support
427 multiple key sizes (e.g. AES-128 vs. AES-192 vs. AES-256).
428 These key sizes are determined with the length of the provided
429 key. Thus, the kernel crypto API does not provide a separate
430 way to select the particular symmetric cipher key size.
431 </para>
432 </sect1>
433
434 <sect1><title>Cipher Allocation Type And Masks</title>
435 <para>
436 The different cipher handle allocation functions allow the
437 specification of a type and mask flag. Both parameters have
438 the following meaning (and are therefore not covered in the
439 subsequent sections).
440 </para>
441
442 <para>
443 The type flag specifies the type of the cipher algorithm.
444 The caller usually provides a 0 when the caller wants the
445 default handling. Otherwise, the caller may provide the
446 following selections which match the the aforementioned
447 cipher types:
448 </para>
449
450 <itemizedlist>
451 <listitem>
452 <para>CRYPTO_ALG_TYPE_CIPHER Single block cipher</para>
453 </listitem>
454 <listitem>
455 <para>CRYPTO_ALG_TYPE_COMPRESS Compression</para>
456 </listitem>
457 <listitem>
458 <para>CRYPTO_ALG_TYPE_AEAD Authenticated Encryption with
459 Associated Data (MAC)</para>
460 </listitem>
461 <listitem>
462 <para>CRYPTO_ALG_TYPE_BLKCIPHER Synchronous multi-block cipher</para>
463 </listitem>
464 <listitem>
465 <para>CRYPTO_ALG_TYPE_ABLKCIPHER Asynchronous multi-block cipher</para>
466 </listitem>
467 <listitem>
468 <para>CRYPTO_ALG_TYPE_GIVCIPHER Asynchronous multi-block
469 cipher packed together with an IV generator (see geniv field
470 in the /proc/crypto listing for the known IV generators)</para>
471 </listitem>
472 <listitem>
473 <para>CRYPTO_ALG_TYPE_DIGEST Raw message digest</para>
474 </listitem>
475 <listitem>
476 <para>CRYPTO_ALG_TYPE_HASH Alias for CRYPTO_ALG_TYPE_DIGEST</para>
477 </listitem>
478 <listitem>
479 <para>CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash</para>
480 </listitem>
481 <listitem>
482 <para>CRYPTO_ALG_TYPE_AHASH Asynchronous multi-block hash</para>
483 </listitem>
484 <listitem>
485 <para>CRYPTO_ALG_TYPE_RNG Random Number Generation</para>
486 </listitem>
487 <listitem>
488 <para>CRYPTO_ALG_TYPE_AKCIPHER Asymmetric cipher</para>
489 </listitem>
490 <listitem>
491 <para>CRYPTO_ALG_TYPE_PCOMPRESS Enhanced version of
492 CRYPTO_ALG_TYPE_COMPRESS allowing for segmented compression /
493 decompression instead of performing the operation on one
494 segment only. CRYPTO_ALG_TYPE_PCOMPRESS is intended to replace
495 CRYPTO_ALG_TYPE_COMPRESS once existing consumers are converted.</para>
496 </listitem>
497 </itemizedlist>
498
499 <para>
500 The mask flag restricts the type of cipher. The only allowed
501 flag is CRYPTO_ALG_ASYNC to restrict the cipher lookup function
502 to asynchronous ciphers. Usually, a caller provides a 0 for the
503 mask flag.
504 </para>
505
506 <para>
507 When the caller provides a mask and type specification, the
508 caller limits the search the kernel crypto API can perform for
509 a suitable cipher implementation for the given cipher name.
510 That means, even when a caller uses a cipher name that exists
511 during its initialization call, the kernel crypto API may not
512 select it due to the used type and mask field.
513 </para>
514 </sect1>
515
516 <sect1><title>Internal Structure of Kernel Crypto API</title>
517
518 <para>
519 The kernel crypto API has an internal structure where a cipher
520 implementation may use many layers and indirections. This section
521 shall help to clarify how the kernel crypto API uses
522 various components to implement the complete cipher.
523 </para>
524
525 <para>
526 The following subsections explain the internal structure based
527 on existing cipher implementations. The first section addresses
528 the most complex scenario where all other scenarios form a logical
529 subset.
530 </para>
531
532 <sect2><title>Generic AEAD Cipher Structure</title>
533
534 <para>
535 The following ASCII art decomposes the kernel crypto API layers
536 when using the AEAD cipher with the automated IV generation. The
537 shown example is used by the IPSEC layer.
538 </para>
539
540 <para>
541 For other use cases of AEAD ciphers, the ASCII art applies as
542 well, but the caller may not use the AEAD cipher with a separate
543 IV generator. In this case, the caller must generate the IV.
544 </para>
545
546 <para>
547 The depicted example decomposes the AEAD cipher of GCM(AES) based
548 on the generic C implementations (gcm.c, aes-generic.c, ctr.c,
549 ghash-generic.c, seqiv.c). The generic implementation serves as an
550 example showing the complete logic of the kernel crypto API.
551 </para>
552
553 <para>
554 It is possible that some streamlined cipher implementations (like
555 AES-NI) provide implementations merging aspects which in the view
556 of the kernel crypto API cannot be decomposed into layers any more.
557 In case of the AES-NI implementation, the CTR mode, the GHASH
558 implementation and the AES cipher are all merged into one cipher
559 implementation registered with the kernel crypto API. In this case,
560 the concept described by the following ASCII art applies too. However,
561 the decomposition of GCM into the individual sub-components
562 by the kernel crypto API is not done any more.
563 </para>
564
565 <para>
566 Each block in the following ASCII art is an independent cipher
567 instance obtained from the kernel crypto API. Each block
568 is accessed by the caller or by other blocks using the API functions
569 defined by the kernel crypto API for the cipher implementation type.
570 </para>
571
572 <para>
573 The blocks below indicate the cipher type as well as the specific
574 logic implemented in the cipher.
575 </para>
576
577 <para>
578 The ASCII art picture also indicates the call structure, i.e. who
579 calls which component. The arrows point to the invoked block
580 where the caller uses the API applicable to the cipher type
581 specified for the block.
582 </para>
583
584 <programlisting>
585 <![CDATA[
586 kernel crypto API | IPSEC Layer
587 |
588 +-----------+ |
589 | | (1)
590 | aead | <----------------------------------- esp_output
591 | (seqiv) | ---+
592 +-----------+ |
593 | (2)
594 +-----------+ |
595 | | <--+ (2)
596 | aead | <----------------------------------- esp_input
597 | (gcm) | ------------+
598 +-----------+ |
599 | (3) | (5)
600 v v
601 +-----------+ +-----------+
602 | | | |
603 | ablkcipher| | ahash |
604 | (ctr) | ---+ | (ghash) |
605 +-----------+ | +-----------+
606 |
607 +-----------+ | (4)
608 | | <--+
609 | cipher |
610 | (aes) |
611 +-----------+
612 ]]>
613 </programlisting>
614
615 <para>
616 The following call sequence is applicable when the IPSEC layer
617 triggers an encryption operation with the esp_output function. During
618 configuration, the administrator set up the use of rfc4106(gcm(aes)) as
619 the cipher for ESP. The following call sequence is now depicted in the
620 ASCII art above:
621 </para>
622
623 <orderedlist>
624 <listitem>
625 <para>
626 esp_output() invokes crypto_aead_encrypt() to trigger an encryption
627 operation of the AEAD cipher with IV generator.
628 </para>
629
630 <para>
631 In case of GCM, the SEQIV implementation is registered as GIVCIPHER
632 in crypto_rfc4106_alloc().
633 </para>
634
635 <para>
636 The SEQIV performs its operation to generate an IV where the core
637 function is seqiv_geniv().
638 </para>
639 </listitem>
640
641 <listitem>
642 <para>
643 Now, SEQIV uses the AEAD API function calls to invoke the associated
644 AEAD cipher. In our case, during the instantiation of SEQIV, the
645 cipher handle for GCM is provided to SEQIV. This means that SEQIV
646 invokes AEAD cipher operations with the GCM cipher handle.
647 </para>
648
649 <para>
650 During instantiation of the GCM handle, the CTR(AES) and GHASH
651 ciphers are instantiated. The cipher handles for CTR(AES) and GHASH
652 are retained for later use.
653 </para>
654
655 <para>
656 The GCM implementation is responsible to invoke the CTR mode AES and
657 the GHASH cipher in the right manner to implement the GCM
658 specification.
659 </para>
660 </listitem>
661
662 <listitem>
663 <para>
664 The GCM AEAD cipher type implementation now invokes the ABLKCIPHER API
665 with the instantiated CTR(AES) cipher handle.
666 </para>
667
668 <para>
669 During instantiation of the CTR(AES) cipher, the CIPHER type
670 implementation of AES is instantiated. The cipher handle for AES is
671 retained.
672 </para>
673
674 <para>
675 That means that the ABLKCIPHER implementation of CTR(AES) only
676 implements the CTR block chaining mode. After performing the block
677 chaining operation, the CIPHER implementation of AES is invoked.
678 </para>
679 </listitem>
680
681 <listitem>
682 <para>
683 The ABLKCIPHER of CTR(AES) now invokes the CIPHER API with the AES
684 cipher handle to encrypt one block.
685 </para>
686 </listitem>
687
688 <listitem>
689 <para>
690 The GCM AEAD implementation also invokes the GHASH cipher
691 implementation via the AHASH API.
692 </para>
693 </listitem>
694 </orderedlist>
695
696 <para>
697 When the IPSEC layer triggers the esp_input() function, the same call
698 sequence is followed with the only difference that the operation starts
699 with step (2).
700 </para>
701 </sect2>
702
703 <sect2><title>Generic Block Cipher Structure</title>
704 <para>
705 Generic block ciphers follow the same concept as depicted with the ASCII
706 art picture above.
707 </para>
708
709 <para>
710 For example, CBC(AES) is implemented with cbc.c, and aes-generic.c. The
711 ASCII art picture above applies as well with the difference that only
712 step (4) is used and the ABLKCIPHER block chaining mode is CBC.
713 </para>
714 </sect2>
715
716 <sect2><title>Generic Keyed Message Digest Structure</title>
717 <para>
718 Keyed message digest implementations again follow the same concept as
719 depicted in the ASCII art picture above.
720 </para>
721
722 <para>
723 For example, HMAC(SHA256) is implemented with hmac.c and
724 sha256_generic.c. The following ASCII art illustrates the
725 implementation:
726 </para>
727
728 <programlisting>
729 <![CDATA[
730 kernel crypto API | Caller
731 |
732 +-----------+ (1) |
733 | | <------------------ some_function
734 | ahash |
735 | (hmac) | ---+
736 +-----------+ |
737 | (2)
738 +-----------+ |
739 | | <--+
740 | shash |
741 | (sha256) |
742 +-----------+
743 ]]>
744 </programlisting>
745
746 <para>
747 The following call sequence is applicable when a caller triggers
748 an HMAC operation:
749 </para>
750
751 <orderedlist>
752 <listitem>
753 <para>
754 The AHASH API functions are invoked by the caller. The HMAC
755 implementation performs its operation as needed.
756 </para>
757
758 <para>
759 During initialization of the HMAC cipher, the SHASH cipher type of
760 SHA256 is instantiated. The cipher handle for the SHA256 instance is
761 retained.
762 </para>
763
764 <para>
765 At one time, the HMAC implementation requires a SHA256 operation
766 where the SHA256 cipher handle is used.
767 </para>
768 </listitem>
769
770 <listitem>
771 <para>
772 The HMAC instance now invokes the SHASH API with the SHA256
773 cipher handle to calculate the message digest.
774 </para>
775 </listitem>
776 </orderedlist>
777 </sect2>
778 </sect1>
779 </chapter>
780
781 <chapter id="Development"><title>Developing Cipher Algorithms</title>
782 <sect1><title>Registering And Unregistering Transformation</title>
783 <para>
784 There are three distinct types of registration functions in
785 the Crypto API. One is used to register a generic cryptographic
786 transformation, while the other two are specific to HASH
787 transformations and COMPRESSion. We will discuss the latter
788 two in a separate chapter, here we will only look at the
789 generic ones.
790 </para>
791
792 <para>
793 Before discussing the register functions, the data structure
794 to be filled with each, struct crypto_alg, must be considered
795 -- see below for a description of this data structure.
796 </para>
797
798 <para>
799 The generic registration functions can be found in
800 include/linux/crypto.h and their definition can be seen below.
801 The former function registers a single transformation, while
802 the latter works on an array of transformation descriptions.
803 The latter is useful when registering transformations in bulk.
804 </para>
805
806 <programlisting>
807 int crypto_register_alg(struct crypto_alg *alg);
808 int crypto_register_algs(struct crypto_alg *algs, int count);
809 </programlisting>
810
811 <para>
812 The counterparts to those functions are listed below.
813 </para>
814
815 <programlisting>
816 int crypto_unregister_alg(struct crypto_alg *alg);
817 int crypto_unregister_algs(struct crypto_alg *algs, int count);
818 </programlisting>
819
820 <para>
821 Notice that both registration and unregistration functions
822 do return a value, so make sure to handle errors. A return
823 code of zero implies success. Any return code &lt; 0 implies
824 an error.
825 </para>
826
827 <para>
828 The bulk registration / unregistration functions require
829 that struct crypto_alg is an array of count size. These
830 functions simply loop over that array and register /
831 unregister each individual algorithm. If an error occurs,
832 the loop is terminated at the offending algorithm definition.
833 That means, the algorithms prior to the offending algorithm
834 are successfully registered. Note, the caller has no way of
835 knowing which cipher implementations have successfully
836 registered. If this is important to know, the caller should
837 loop through the different implementations using the single
838 instance *_alg functions for each individual implementation.
839 </para>
840 </sect1>
841
842 <sect1><title>Single-Block Symmetric Ciphers [CIPHER]</title>
843 <para>
844 Example of transformations: aes, arc4, ...
845 </para>
846
847 <para>
848 This section describes the simplest of all transformation
849 implementations, that being the CIPHER type used for symmetric
850 ciphers. The CIPHER type is used for transformations which
851 operate on exactly one block at a time and there are no
852 dependencies between blocks at all.
853 </para>
854
855 <sect2><title>Registration specifics</title>
856 <para>
857 The registration of [CIPHER] algorithm is specific in that
858 struct crypto_alg field .cra_type is empty. The .cra_u.cipher
859 has to be filled in with proper callbacks to implement this
860 transformation.
861 </para>
862
863 <para>
864 See struct cipher_alg below.
865 </para>
866 </sect2>
867
868 <sect2><title>Cipher Definition With struct cipher_alg</title>
869 <para>
870 Struct cipher_alg defines a single block cipher.
871 </para>
872
873 <para>
874 Here are schematics of how these functions are called when
875 operated from other part of the kernel. Note that the
876 .cia_setkey() call might happen before or after any of these
877 schematics happen, but must not happen during any of these
878 are in-flight.
879 </para>
880
881 <para>
882 <programlisting>
883 KEY ---. PLAINTEXT ---.
884 v v
885 .cia_setkey() -&gt; .cia_encrypt()
886 |
887 '-----&gt; CIPHERTEXT
888 </programlisting>
889 </para>
890
891 <para>
892 Please note that a pattern where .cia_setkey() is called
893 multiple times is also valid:
894 </para>
895
896 <para>
897 <programlisting>
898
899 KEY1 --. PLAINTEXT1 --. KEY2 --. PLAINTEXT2 --.
900 v v v v
901 .cia_setkey() -&gt; .cia_encrypt() -&gt; .cia_setkey() -&gt; .cia_encrypt()
902 | |
903 '---&gt; CIPHERTEXT1 '---&gt; CIPHERTEXT2
904 </programlisting>
905 </para>
906
907 </sect2>
908 </sect1>
909
910 <sect1><title>Multi-Block Ciphers [BLKCIPHER] [ABLKCIPHER]</title>
911 <para>
912 Example of transformations: cbc(aes), ecb(arc4), ...
913 </para>
914
915 <para>
916 This section describes the multi-block cipher transformation
917 implementations for both synchronous [BLKCIPHER] and
918 asynchronous [ABLKCIPHER] case. The multi-block ciphers are
919 used for transformations which operate on scatterlists of
920 data supplied to the transformation functions. They output
921 the result into a scatterlist of data as well.
922 </para>
923
924 <sect2><title>Registration Specifics</title>
925
926 <para>
927 The registration of [BLKCIPHER] or [ABLKCIPHER] algorithms
928 is one of the most standard procedures throughout the crypto API.
929 </para>
930
931 <para>
932 Note, if a cipher implementation requires a proper alignment
933 of data, the caller should use the functions of
934 crypto_blkcipher_alignmask() or crypto_ablkcipher_alignmask()
935 respectively to identify a memory alignment mask. The kernel
936 crypto API is able to process requests that are unaligned.
937 This implies, however, additional overhead as the kernel
938 crypto API needs to perform the realignment of the data which
939 may imply moving of data.
940 </para>
941 </sect2>
942
943 <sect2><title>Cipher Definition With struct blkcipher_alg and ablkcipher_alg</title>
944 <para>
945 Struct blkcipher_alg defines a synchronous block cipher whereas
946 struct ablkcipher_alg defines an asynchronous block cipher.
947 </para>
948
949 <para>
950 Please refer to the single block cipher description for schematics
951 of the block cipher usage. The usage patterns are exactly the same
952 for [ABLKCIPHER] and [BLKCIPHER] as they are for plain [CIPHER].
953 </para>
954 </sect2>
955
956 <sect2><title>Specifics Of Asynchronous Multi-Block Cipher</title>
957 <para>
958 There are a couple of specifics to the [ABLKCIPHER] interface.
959 </para>
960
961 <para>
962 First of all, some of the drivers will want to use the
963 Generic ScatterWalk in case the hardware needs to be fed
964 separate chunks of the scatterlist which contains the
965 plaintext and will contain the ciphertext. Please refer
966 to the ScatterWalk interface offered by the Linux kernel
967 scatter / gather list implementation.
968 </para>
969 </sect2>
970 </sect1>
971
972 <sect1><title>Hashing [HASH]</title>
973
974 <para>
975 Example of transformations: crc32, md5, sha1, sha256,...
976 </para>
977
978 <sect2><title>Registering And Unregistering The Transformation</title>
979
980 <para>
981 There are multiple ways to register a HASH transformation,
982 depending on whether the transformation is synchronous [SHASH]
983 or asynchronous [AHASH] and the amount of HASH transformations
984 we are registering. You can find the prototypes defined in
985 include/crypto/internal/hash.h:
986 </para>
987
988 <programlisting>
989 int crypto_register_ahash(struct ahash_alg *alg);
990
991 int crypto_register_shash(struct shash_alg *alg);
992 int crypto_register_shashes(struct shash_alg *algs, int count);
993 </programlisting>
994
995 <para>
996 The respective counterparts for unregistering the HASH
997 transformation are as follows:
998 </para>
999
1000 <programlisting>
1001 int crypto_unregister_ahash(struct ahash_alg *alg);
1002
1003 int crypto_unregister_shash(struct shash_alg *alg);
1004 int crypto_unregister_shashes(struct shash_alg *algs, int count);
1005 </programlisting>
1006 </sect2>
1007
1008 <sect2><title>Cipher Definition With struct shash_alg and ahash_alg</title>
1009 <para>
1010 Here are schematics of how these functions are called when
1011 operated from other part of the kernel. Note that the .setkey()
1012 call might happen before or after any of these schematics happen,
1013 but must not happen during any of these are in-flight. Please note
1014 that calling .init() followed immediately by .finish() is also a
1015 perfectly valid transformation.
1016 </para>
1017
1018 <programlisting>
1019 I) DATA -----------.
1020 v
1021 .init() -&gt; .update() -&gt; .final() ! .update() might not be called
1022 ^ | | at all in this scenario.
1023 '----' '---&gt; HASH
1024
1025 II) DATA -----------.-----------.
1026 v v
1027 .init() -&gt; .update() -&gt; .finup() ! .update() may not be called
1028 ^ | | at all in this scenario.
1029 '----' '---&gt; HASH
1030
1031 III) DATA -----------.
1032 v
1033 .digest() ! The entire process is handled
1034 | by the .digest() call.
1035 '---------------&gt; HASH
1036 </programlisting>
1037
1038 <para>
1039 Here is a schematic of how the .export()/.import() functions are
1040 called when used from another part of the kernel.
1041 </para>
1042
1043 <programlisting>
1044 KEY--. DATA--.
1045 v v ! .update() may not be called
1046 .setkey() -&gt; .init() -&gt; .update() -&gt; .export() at all in this scenario.
1047 ^ | |
1048 '-----' '--&gt; PARTIAL_HASH
1049
1050 ----------- other transformations happen here -----------
1051
1052 PARTIAL_HASH--. DATA1--.
1053 v v
1054 .import -&gt; .update() -&gt; .final() ! .update() may not be called
1055 ^ | | at all in this scenario.
1056 '----' '--&gt; HASH1
1057
1058 PARTIAL_HASH--. DATA2-.
1059 v v
1060 .import -&gt; .finup()
1061 |
1062 '---------------&gt; HASH2
1063 </programlisting>
1064 </sect2>
1065
1066 <sect2><title>Specifics Of Asynchronous HASH Transformation</title>
1067 <para>
1068 Some of the drivers will want to use the Generic ScatterWalk
1069 in case the implementation needs to be fed separate chunks of the
1070 scatterlist which contains the input data. The buffer containing
1071 the resulting hash will always be properly aligned to
1072 .cra_alignmask so there is no need to worry about this.
1073 </para>
1074 </sect2>
1075 </sect1>
1076 </chapter>
1077
1078 <chapter id="User"><title>User Space Interface</title>
1079 <sect1><title>Introduction</title>
1080 <para>
1081 The concepts of the kernel crypto API visible to kernel space is fully
1082 applicable to the user space interface as well. Therefore, the kernel
1083 crypto API high level discussion for the in-kernel use cases applies
1084 here as well.
1085 </para>
1086
1087 <para>
1088 The major difference, however, is that user space can only act as a
1089 consumer and never as a provider of a transformation or cipher algorithm.
1090 </para>
1091
1092 <para>
1093 The following covers the user space interface exported by the kernel
1094 crypto API. A working example of this description is libkcapi that
1095 can be obtained from [1]. That library can be used by user space
1096 applications that require cryptographic services from the kernel.
1097 </para>
1098
1099 <para>
1100 Some details of the in-kernel kernel crypto API aspects do not
1101 apply to user space, however. This includes the difference between
1102 synchronous and asynchronous invocations. The user space API call
1103 is fully synchronous.
1104 </para>
1105
1106 <para>
1107 [1] <ulink url="http://www.chronox.de/libkcapi.html">http://www.chronox.de/libkcapi.html</ulink>
1108 </para>
1109
1110 </sect1>
1111
1112 <sect1><title>User Space API General Remarks</title>
1113 <para>
1114 The kernel crypto API is accessible from user space. Currently,
1115 the following ciphers are accessible:
1116 </para>
1117
1118 <itemizedlist>
1119 <listitem>
1120 <para>Message digest including keyed message digest (HMAC, CMAC)</para>
1121 </listitem>
1122
1123 <listitem>
1124 <para>Symmetric ciphers</para>
1125 </listitem>
1126
1127 <listitem>
1128 <para>AEAD ciphers</para>
1129 </listitem>
1130
1131 <listitem>
1132 <para>Random Number Generators</para>
1133 </listitem>
1134 </itemizedlist>
1135
1136 <para>
1137 The interface is provided via socket type using the type AF_ALG.
1138 In addition, the setsockopt option type is SOL_ALG. In case the
1139 user space header files do not export these flags yet, use the
1140 following macros:
1141 </para>
1142
1143 <programlisting>
1144 #ifndef AF_ALG
1145 #define AF_ALG 38
1146 #endif
1147 #ifndef SOL_ALG
1148 #define SOL_ALG 279
1149 #endif
1150 </programlisting>
1151
1152 <para>
1153 A cipher is accessed with the same name as done for the in-kernel
1154 API calls. This includes the generic vs. unique naming schema for
1155 ciphers as well as the enforcement of priorities for generic names.
1156 </para>
1157
1158 <para>
1159 To interact with the kernel crypto API, a socket must be
1160 created by the user space application. User space invokes the cipher
1161 operation with the send()/write() system call family. The result of the
1162 cipher operation is obtained with the read()/recv() system call family.
1163 </para>
1164
1165 <para>
1166 The following API calls assume that the socket descriptor
1167 is already opened by the user space application and discusses only
1168 the kernel crypto API specific invocations.
1169 </para>
1170
1171 <para>
1172 To initialize the socket interface, the following sequence has to
1173 be performed by the consumer:
1174 </para>
1175
1176 <orderedlist>
1177 <listitem>
1178 <para>
1179 Create a socket of type AF_ALG with the struct sockaddr_alg
1180 parameter specified below for the different cipher types.
1181 </para>
1182 </listitem>
1183
1184 <listitem>
1185 <para>
1186 Invoke bind with the socket descriptor
1187 </para>
1188 </listitem>
1189
1190 <listitem>
1191 <para>
1192 Invoke accept with the socket descriptor. The accept system call
1193 returns a new file descriptor that is to be used to interact with
1194 the particular cipher instance. When invoking send/write or recv/read
1195 system calls to send data to the kernel or obtain data from the
1196 kernel, the file descriptor returned by accept must be used.
1197 </para>
1198 </listitem>
1199 </orderedlist>
1200 </sect1>
1201
1202 <sect1><title>In-place Cipher operation</title>
1203 <para>
1204 Just like the in-kernel operation of the kernel crypto API, the user
1205 space interface allows the cipher operation in-place. That means that
1206 the input buffer used for the send/write system call and the output
1207 buffer used by the read/recv system call may be one and the same.
1208 This is of particular interest for symmetric cipher operations where a
1209 copying of the output data to its final destination can be avoided.
1210 </para>
1211
1212 <para>
1213 If a consumer on the other hand wants to maintain the plaintext and
1214 the ciphertext in different memory locations, all a consumer needs
1215 to do is to provide different memory pointers for the encryption and
1216 decryption operation.
1217 </para>
1218 </sect1>
1219
1220 <sect1><title>Message Digest API</title>
1221 <para>
1222 The message digest type to be used for the cipher operation is
1223 selected when invoking the bind syscall. bind requires the caller
1224 to provide a filled struct sockaddr data structure. This data
1225 structure must be filled as follows:
1226 </para>
1227
1228 <programlisting>
1229 struct sockaddr_alg sa = {
1230 .salg_family = AF_ALG,
1231 .salg_type = "hash", /* this selects the hash logic in the kernel */
1232 .salg_name = "sha1" /* this is the cipher name */
1233 };
1234 </programlisting>
1235
1236 <para>
1237 The salg_type value "hash" applies to message digests and keyed
1238 message digests. Though, a keyed message digest is referenced by
1239 the appropriate salg_name. Please see below for the setsockopt
1240 interface that explains how the key can be set for a keyed message
1241 digest.
1242 </para>
1243
1244 <para>
1245 Using the send() system call, the application provides the data that
1246 should be processed with the message digest. The send system call
1247 allows the following flags to be specified:
1248 </para>
1249
1250 <itemizedlist>
1251 <listitem>
1252 <para>
1253 MSG_MORE: If this flag is set, the send system call acts like a
1254 message digest update function where the final hash is not
1255 yet calculated. If the flag is not set, the send system call
1256 calculates the final message digest immediately.
1257 </para>
1258 </listitem>
1259 </itemizedlist>
1260
1261 <para>
1262 With the recv() system call, the application can read the message
1263 digest from the kernel crypto API. If the buffer is too small for the
1264 message digest, the flag MSG_TRUNC is set by the kernel.
1265 </para>
1266
1267 <para>
1268 In order to set a message digest key, the calling application must use
1269 the setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC
1270 operation is performed without the initial HMAC state change caused by
1271 the key.
1272 </para>
1273 </sect1>
1274
1275 <sect1><title>Symmetric Cipher API</title>
1276 <para>
1277 The operation is very similar to the message digest discussion.
1278 During initialization, the struct sockaddr data structure must be
1279 filled as follows:
1280 </para>
1281
1282 <programlisting>
1283 struct sockaddr_alg sa = {
1284 .salg_family = AF_ALG,
1285 .salg_type = "skcipher", /* this selects the symmetric cipher */
1286 .salg_name = "cbc(aes)" /* this is the cipher name */
1287 };
1288 </programlisting>
1289
1290 <para>
1291 Before data can be sent to the kernel using the write/send system
1292 call family, the consumer must set the key. The key setting is
1293 described with the setsockopt invocation below.
1294 </para>
1295
1296 <para>
1297 Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is
1298 specified with the data structure provided by the sendmsg() system call.
1299 </para>
1300
1301 <para>
1302 The sendmsg system call parameter of struct msghdr is embedded into the
1303 struct cmsghdr data structure. See recv(2) and cmsg(3) for more
1304 information on how the cmsghdr data structure is used together with the
1305 send/recv system call family. That cmsghdr data structure holds the
1306 following information specified with a separate header instances:
1307 </para>
1308
1309 <itemizedlist>
1310 <listitem>
1311 <para>
1312 specification of the cipher operation type with one of these flags:
1313 </para>
1314 <itemizedlist>
1315 <listitem>
1316 <para>ALG_OP_ENCRYPT - encryption of data</para>
1317 </listitem>
1318 <listitem>
1319 <para>ALG_OP_DECRYPT - decryption of data</para>
1320 </listitem>
1321 </itemizedlist>
1322 </listitem>
1323
1324 <listitem>
1325 <para>
1326 specification of the IV information marked with the flag ALG_SET_IV
1327 </para>
1328 </listitem>
1329 </itemizedlist>
1330
1331 <para>
1332 The send system call family allows the following flag to be specified:
1333 </para>
1334
1335 <itemizedlist>
1336 <listitem>
1337 <para>
1338 MSG_MORE: If this flag is set, the send system call acts like a
1339 cipher update function where more input data is expected
1340 with a subsequent invocation of the send system call.
1341 </para>
1342 </listitem>
1343 </itemizedlist>
1344
1345 <para>
1346 Note: The kernel reports -EINVAL for any unexpected data. The caller
1347 must make sure that all data matches the constraints given in
1348 /proc/crypto for the selected cipher.
1349 </para>
1350
1351 <para>
1352 With the recv() system call, the application can read the result of
1353 the cipher operation from the kernel crypto API. The output buffer
1354 must be at least as large as to hold all blocks of the encrypted or
1355 decrypted data. If the output data size is smaller, only as many
1356 blocks are returned that fit into that output buffer size.
1357 </para>
1358 </sect1>
1359
1360 <sect1><title>AEAD Cipher API</title>
1361 <para>
1362 The operation is very similar to the symmetric cipher discussion.
1363 During initialization, the struct sockaddr data structure must be
1364 filled as follows:
1365 </para>
1366
1367 <programlisting>
1368 struct sockaddr_alg sa = {
1369 .salg_family = AF_ALG,
1370 .salg_type = "aead", /* this selects the symmetric cipher */
1371 .salg_name = "gcm(aes)" /* this is the cipher name */
1372 };
1373 </programlisting>
1374
1375 <para>
1376 Before data can be sent to the kernel using the write/send system
1377 call family, the consumer must set the key. The key setting is
1378 described with the setsockopt invocation below.
1379 </para>
1380
1381 <para>
1382 In addition, before data can be sent to the kernel using the
1383 write/send system call family, the consumer must set the authentication
1384 tag size. To set the authentication tag size, the caller must use the
1385 setsockopt invocation described below.
1386 </para>
1387
1388 <para>
1389 Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is
1390 specified with the data structure provided by the sendmsg() system call.
1391 </para>
1392
1393 <para>
1394 The sendmsg system call parameter of struct msghdr is embedded into the
1395 struct cmsghdr data structure. See recv(2) and cmsg(3) for more
1396 information on how the cmsghdr data structure is used together with the
1397 send/recv system call family. That cmsghdr data structure holds the
1398 following information specified with a separate header instances:
1399 </para>
1400
1401 <itemizedlist>
1402 <listitem>
1403 <para>
1404 specification of the cipher operation type with one of these flags:
1405 </para>
1406 <itemizedlist>
1407 <listitem>
1408 <para>ALG_OP_ENCRYPT - encryption of data</para>
1409 </listitem>
1410 <listitem>
1411 <para>ALG_OP_DECRYPT - decryption of data</para>
1412 </listitem>
1413 </itemizedlist>
1414 </listitem>
1415
1416 <listitem>
1417 <para>
1418 specification of the IV information marked with the flag ALG_SET_IV
1419 </para>
1420 </listitem>
1421
1422 <listitem>
1423 <para>
1424 specification of the associated authentication data (AAD) with the
1425 flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together
1426 with the plaintext / ciphertext. See below for the memory structure.
1427 </para>
1428 </listitem>
1429 </itemizedlist>
1430
1431 <para>
1432 The send system call family allows the following flag to be specified:
1433 </para>
1434
1435 <itemizedlist>
1436 <listitem>
1437 <para>
1438 MSG_MORE: If this flag is set, the send system call acts like a
1439 cipher update function where more input data is expected
1440 with a subsequent invocation of the send system call.
1441 </para>
1442 </listitem>
1443 </itemizedlist>
1444
1445 <para>
1446 Note: The kernel reports -EINVAL for any unexpected data. The caller
1447 must make sure that all data matches the constraints given in
1448 /proc/crypto for the selected cipher.
1449 </para>
1450
1451 <para>
1452 With the recv() system call, the application can read the result of
1453 the cipher operation from the kernel crypto API. The output buffer
1454 must be at least as large as defined with the memory structure below.
1455 If the output data size is smaller, the cipher operation is not performed.
1456 </para>
1457
1458 <para>
1459 The authenticated decryption operation may indicate an integrity error.
1460 Such breach in integrity is marked with the -EBADMSG error code.
1461 </para>
1462
1463 <sect2><title>AEAD Memory Structure</title>
1464 <para>
1465 The AEAD cipher operates with the following information that
1466 is communicated between user and kernel space as one data stream:
1467 </para>
1468
1469 <itemizedlist>
1470 <listitem>
1471 <para>plaintext or ciphertext</para>
1472 </listitem>
1473
1474 <listitem>
1475 <para>associated authentication data (AAD)</para>
1476 </listitem>
1477
1478 <listitem>
1479 <para>authentication tag</para>
1480 </listitem>
1481 </itemizedlist>
1482
1483 <para>
1484 The sizes of the AAD and the authentication tag are provided with
1485 the sendmsg and setsockopt calls (see there). As the kernel knows
1486 the size of the entire data stream, the kernel is now able to
1487 calculate the right offsets of the data components in the data
1488 stream.
1489 </para>
1490
1491 <para>
1492 The user space caller must arrange the aforementioned information
1493 in the following order:
1494 </para>
1495
1496 <itemizedlist>
1497 <listitem>
1498 <para>
1499 AEAD encryption input: AAD || plaintext
1500 </para>
1501 </listitem>
1502
1503 <listitem>
1504 <para>
1505 AEAD decryption input: AAD || ciphertext || authentication tag
1506 </para>
1507 </listitem>
1508 </itemizedlist>
1509
1510 <para>
1511 The output buffer the user space caller provides must be at least as
1512 large to hold the following data:
1513 </para>
1514
1515 <itemizedlist>
1516 <listitem>
1517 <para>
1518 AEAD encryption output: ciphertext || authentication tag
1519 </para>
1520 </listitem>
1521
1522 <listitem>
1523 <para>
1524 AEAD decryption output: plaintext
1525 </para>
1526 </listitem>
1527 </itemizedlist>
1528 </sect2>
1529 </sect1>
1530
1531 <sect1><title>Random Number Generator API</title>
1532 <para>
1533 Again, the operation is very similar to the other APIs.
1534 During initialization, the struct sockaddr data structure must be
1535 filled as follows:
1536 </para>
1537
1538 <programlisting>
1539 struct sockaddr_alg sa = {
1540 .salg_family = AF_ALG,
1541 .salg_type = "rng", /* this selects the symmetric cipher */
1542 .salg_name = "drbg_nopr_sha256" /* this is the cipher name */
1543 };
1544 </programlisting>
1545
1546 <para>
1547 Depending on the RNG type, the RNG must be seeded. The seed is provided
1548 using the setsockopt interface to set the key. For example, the
1549 ansi_cprng requires a seed. The DRBGs do not require a seed, but
1550 may be seeded.
1551 </para>
1552
1553 <para>
1554 Using the read()/recvmsg() system calls, random numbers can be obtained.
1555 The kernel generates at most 128 bytes in one call. If user space
1556 requires more data, multiple calls to read()/recvmsg() must be made.
1557 </para>
1558
1559 <para>
1560 WARNING: The user space caller may invoke the initially mentioned
1561 accept system call multiple times. In this case, the returned file
1562 descriptors have the same state.
1563 </para>
1564
1565 </sect1>
1566
1567 <sect1><title>Zero-Copy Interface</title>
1568 <para>
1569 In addition to the send/write/read/recv system call family, the AF_ALG
1570 interface can be accessed with the zero-copy interface of splice/vmsplice.
1571 As the name indicates, the kernel tries to avoid a copy operation into
1572 kernel space.
1573 </para>
1574
1575 <para>
1576 The zero-copy operation requires data to be aligned at the page boundary.
1577 Non-aligned data can be used as well, but may require more operations of
1578 the kernel which would defeat the speed gains obtained from the zero-copy
1579 interface.
1580 </para>
1581
1582 <para>
1583 The system-interent limit for the size of one zero-copy operation is
1584 16 pages. If more data is to be sent to AF_ALG, user space must slice
1585 the input into segments with a maximum size of 16 pages.
1586 </para>
1587
1588 <para>
1589 Zero-copy can be used with the following code example (a complete working
1590 example is provided with libkcapi):
1591 </para>
1592
1593 <programlisting>
1594 int pipes[2];
1595
1596 pipe(pipes);
1597 /* input data in iov */
1598 vmsplice(pipes[1], iov, iovlen, SPLICE_F_GIFT);
1599 /* opfd is the file descriptor returned from accept() system call */
1600 splice(pipes[0], NULL, opfd, NULL, ret, 0);
1601 read(opfd, out, outlen);
1602 </programlisting>
1603
1604 </sect1>
1605
1606 <sect1><title>Setsockopt Interface</title>
1607 <para>
1608 In addition to the read/recv and send/write system call handling
1609 to send and retrieve data subject to the cipher operation, a consumer
1610 also needs to set the additional information for the cipher operation.
1611 This additional information is set using the setsockopt system call
1612 that must be invoked with the file descriptor of the open cipher
1613 (i.e. the file descriptor returned by the accept system call).
1614 </para>
1615
1616 <para>
1617 Each setsockopt invocation must use the level SOL_ALG.
1618 </para>
1619
1620 <para>
1621 The setsockopt interface allows setting the following data using
1622 the mentioned optname:
1623 </para>
1624
1625 <itemizedlist>
1626 <listitem>
1627 <para>
1628 ALG_SET_KEY -- Setting the key. Key setting is applicable to:
1629 </para>
1630 <itemizedlist>
1631 <listitem>
1632 <para>the skcipher cipher type (symmetric ciphers)</para>
1633 </listitem>
1634 <listitem>
1635 <para>the hash cipher type (keyed message digests)</para>
1636 </listitem>
1637 <listitem>
1638 <para>the AEAD cipher type</para>
1639 </listitem>
1640 <listitem>
1641 <para>the RNG cipher type to provide the seed</para>
1642 </listitem>
1643 </itemizedlist>
1644 </listitem>
1645
1646 <listitem>
1647 <para>
1648 ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size
1649 for AEAD ciphers. For a encryption operation, the authentication
1650 tag of the given size will be generated. For a decryption operation,
1651 the provided ciphertext is assumed to contain an authentication tag
1652 of the given size (see section about AEAD memory layout below).
1653 </para>
1654 </listitem>
1655 </itemizedlist>
1656
1657 </sect1>
1658
1659 <sect1><title>User space API example</title>
1660 <para>
1661 Please see [1] for libkcapi which provides an easy-to-use wrapper
1662 around the aforementioned Netlink kernel interface. [1] also contains
1663 a test application that invokes all libkcapi API calls.
1664 </para>
1665
1666 <para>
1667 [1] <ulink url="http://www.chronox.de/libkcapi.html">http://www.chronox.de/libkcapi.html</ulink>
1668 </para>
1669
1670 </sect1>
1671
1672 </chapter>
1673
1674 <chapter id="API"><title>Programming Interface</title>
1675 <para>
1676 Please note that the kernel crypto API contains the AEAD givcrypt
1677 API (crypto_aead_giv* and aead_givcrypt_* function calls in
1678 include/crypto/aead.h). This API is obsolete and will be removed
1679 in the future. To obtain the functionality of an AEAD cipher with
1680 internal IV generation, use the IV generator as a regular cipher.
1681 For example, rfc4106(gcm(aes)) is the AEAD cipher with external
1682 IV generation and seqniv(rfc4106(gcm(aes))) implies that the kernel
1683 crypto API generates the IV. Different IV generators are available.
1684 </para>
1685 <sect1><title>Block Cipher Context Data Structures</title>
1686 !Pinclude/linux/crypto.h Block Cipher Context Data Structures
1687 !Finclude/crypto/aead.h aead_request
1688 </sect1>
1689 <sect1><title>Block Cipher Algorithm Definitions</title>
1690 !Pinclude/linux/crypto.h Block Cipher Algorithm Definitions
1691 !Finclude/linux/crypto.h crypto_alg
1692 !Finclude/linux/crypto.h ablkcipher_alg
1693 !Finclude/crypto/aead.h aead_alg
1694 !Finclude/linux/crypto.h blkcipher_alg
1695 !Finclude/linux/crypto.h cipher_alg
1696 !Finclude/crypto/rng.h rng_alg
1697 </sect1>
1698 <sect1><title>Asynchronous Block Cipher API</title>
1699 !Pinclude/linux/crypto.h Asynchronous Block Cipher API
1700 !Finclude/linux/crypto.h crypto_alloc_ablkcipher
1701 !Finclude/linux/crypto.h crypto_free_ablkcipher
1702 !Finclude/linux/crypto.h crypto_has_ablkcipher
1703 !Finclude/linux/crypto.h crypto_ablkcipher_ivsize
1704 !Finclude/linux/crypto.h crypto_ablkcipher_blocksize
1705 !Finclude/linux/crypto.h crypto_ablkcipher_setkey
1706 !Finclude/linux/crypto.h crypto_ablkcipher_reqtfm
1707 !Finclude/linux/crypto.h crypto_ablkcipher_encrypt
1708 !Finclude/linux/crypto.h crypto_ablkcipher_decrypt
1709 </sect1>
1710 <sect1><title>Asynchronous Cipher Request Handle</title>
1711 !Pinclude/linux/crypto.h Asynchronous Cipher Request Handle
1712 !Finclude/linux/crypto.h crypto_ablkcipher_reqsize
1713 !Finclude/linux/crypto.h ablkcipher_request_set_tfm
1714 !Finclude/linux/crypto.h ablkcipher_request_alloc
1715 !Finclude/linux/crypto.h ablkcipher_request_free
1716 !Finclude/linux/crypto.h ablkcipher_request_set_callback
1717 !Finclude/linux/crypto.h ablkcipher_request_set_crypt
1718 </sect1>
1719 <sect1><title>Authenticated Encryption With Associated Data (AEAD) Cipher API</title>
1720 !Pinclude/crypto/aead.h Authenticated Encryption With Associated Data (AEAD) Cipher API
1721 !Finclude/crypto/aead.h crypto_alloc_aead
1722 !Finclude/crypto/aead.h crypto_free_aead
1723 !Finclude/crypto/aead.h crypto_aead_ivsize
1724 !Finclude/crypto/aead.h crypto_aead_authsize
1725 !Finclude/crypto/aead.h crypto_aead_blocksize
1726 !Finclude/crypto/aead.h crypto_aead_setkey
1727 !Finclude/crypto/aead.h crypto_aead_setauthsize
1728 !Finclude/crypto/aead.h crypto_aead_encrypt
1729 !Finclude/crypto/aead.h crypto_aead_decrypt
1730 </sect1>
1731 <sect1><title>Asynchronous AEAD Request Handle</title>
1732 !Pinclude/crypto/aead.h Asynchronous AEAD Request Handle
1733 !Finclude/crypto/aead.h crypto_aead_reqsize
1734 !Finclude/crypto/aead.h aead_request_set_tfm
1735 !Finclude/crypto/aead.h aead_request_alloc
1736 !Finclude/crypto/aead.h aead_request_free
1737 !Finclude/crypto/aead.h aead_request_set_callback
1738 !Finclude/crypto/aead.h aead_request_set_crypt
1739 !Finclude/crypto/aead.h aead_request_set_assoc
1740 !Finclude/crypto/aead.h aead_request_set_ad
1741 </sect1>
1742 <sect1><title>Synchronous Block Cipher API</title>
1743 !Pinclude/linux/crypto.h Synchronous Block Cipher API
1744 !Finclude/linux/crypto.h crypto_alloc_blkcipher
1745 !Finclude/linux/crypto.h crypto_free_blkcipher
1746 !Finclude/linux/crypto.h crypto_has_blkcipher
1747 !Finclude/linux/crypto.h crypto_blkcipher_name
1748 !Finclude/linux/crypto.h crypto_blkcipher_ivsize
1749 !Finclude/linux/crypto.h crypto_blkcipher_blocksize
1750 !Finclude/linux/crypto.h crypto_blkcipher_setkey
1751 !Finclude/linux/crypto.h crypto_blkcipher_encrypt
1752 !Finclude/linux/crypto.h crypto_blkcipher_encrypt_iv
1753 !Finclude/linux/crypto.h crypto_blkcipher_decrypt
1754 !Finclude/linux/crypto.h crypto_blkcipher_decrypt_iv
1755 !Finclude/linux/crypto.h crypto_blkcipher_set_iv
1756 !Finclude/linux/crypto.h crypto_blkcipher_get_iv
1757 </sect1>
1758 <sect1><title>Single Block Cipher API</title>
1759 !Pinclude/linux/crypto.h Single Block Cipher API
1760 !Finclude/linux/crypto.h crypto_alloc_cipher
1761 !Finclude/linux/crypto.h crypto_free_cipher
1762 !Finclude/linux/crypto.h crypto_has_cipher
1763 !Finclude/linux/crypto.h crypto_cipher_blocksize
1764 !Finclude/linux/crypto.h crypto_cipher_setkey
1765 !Finclude/linux/crypto.h crypto_cipher_encrypt_one
1766 !Finclude/linux/crypto.h crypto_cipher_decrypt_one
1767 </sect1>
1768 <sect1><title>Message Digest Algorithm Definitions</title>
1769 !Pinclude/crypto/hash.h Message Digest Algorithm Definitions
1770 !Finclude/crypto/hash.h hash_alg_common
1771 !Finclude/crypto/hash.h ahash_alg
1772 !Finclude/crypto/hash.h shash_alg
1773 </sect1>
1774 <sect1><title>Asynchronous Message Digest API</title>
1775 !Pinclude/crypto/hash.h Asynchronous Message Digest API
1776 !Finclude/crypto/hash.h crypto_alloc_ahash
1777 !Finclude/crypto/hash.h crypto_free_ahash
1778 !Finclude/crypto/hash.h crypto_ahash_init
1779 !Finclude/crypto/hash.h crypto_ahash_digestsize
1780 !Finclude/crypto/hash.h crypto_ahash_reqtfm
1781 !Finclude/crypto/hash.h crypto_ahash_reqsize
1782 !Finclude/crypto/hash.h crypto_ahash_setkey
1783 !Finclude/crypto/hash.h crypto_ahash_finup
1784 !Finclude/crypto/hash.h crypto_ahash_final
1785 !Finclude/crypto/hash.h crypto_ahash_digest
1786 !Finclude/crypto/hash.h crypto_ahash_export
1787 !Finclude/crypto/hash.h crypto_ahash_import
1788 </sect1>
1789 <sect1><title>Asynchronous Hash Request Handle</title>
1790 !Pinclude/crypto/hash.h Asynchronous Hash Request Handle
1791 !Finclude/crypto/hash.h ahash_request_set_tfm
1792 !Finclude/crypto/hash.h ahash_request_alloc
1793 !Finclude/crypto/hash.h ahash_request_free
1794 !Finclude/crypto/hash.h ahash_request_set_callback
1795 !Finclude/crypto/hash.h ahash_request_set_crypt
1796 </sect1>
1797 <sect1><title>Synchronous Message Digest API</title>
1798 !Pinclude/crypto/hash.h Synchronous Message Digest API
1799 !Finclude/crypto/hash.h crypto_alloc_shash
1800 !Finclude/crypto/hash.h crypto_free_shash
1801 !Finclude/crypto/hash.h crypto_shash_blocksize
1802 !Finclude/crypto/hash.h crypto_shash_digestsize
1803 !Finclude/crypto/hash.h crypto_shash_descsize
1804 !Finclude/crypto/hash.h crypto_shash_setkey
1805 !Finclude/crypto/hash.h crypto_shash_digest
1806 !Finclude/crypto/hash.h crypto_shash_export
1807 !Finclude/crypto/hash.h crypto_shash_import
1808 !Finclude/crypto/hash.h crypto_shash_init
1809 !Finclude/crypto/hash.h crypto_shash_update
1810 !Finclude/crypto/hash.h crypto_shash_final
1811 !Finclude/crypto/hash.h crypto_shash_finup
1812 </sect1>
1813 <sect1><title>Crypto API Random Number API</title>
1814 !Pinclude/crypto/rng.h Random number generator API
1815 !Finclude/crypto/rng.h crypto_alloc_rng
1816 !Finclude/crypto/rng.h crypto_rng_alg
1817 !Finclude/crypto/rng.h crypto_free_rng
1818 !Finclude/crypto/rng.h crypto_rng_get_bytes
1819 !Finclude/crypto/rng.h crypto_rng_reset
1820 !Finclude/crypto/rng.h crypto_rng_seedsize
1821 !Cinclude/crypto/rng.h
1822 </sect1>
1823 <sect1><title>Asymmetric Cipher API</title>
1824 !Pinclude/crypto/akcipher.h Generic Public Key API
1825 !Finclude/crypto/akcipher.h akcipher_alg
1826 !Finclude/crypto/akcipher.h akcipher_request
1827 !Finclude/crypto/akcipher.h crypto_alloc_akcipher
1828 !Finclude/crypto/akcipher.h crypto_free_akcipher
1829 !Finclude/crypto/akcipher.h crypto_akcipher_set_pub_key
1830 !Finclude/crypto/akcipher.h crypto_akcipher_set_priv_key
1831 </sect1>
1832 <sect1><title>Asymmetric Cipher Request Handle</title>
1833 !Finclude/crypto/akcipher.h akcipher_request_alloc
1834 !Finclude/crypto/akcipher.h akcipher_request_free
1835 !Finclude/crypto/akcipher.h akcipher_request_set_callback
1836 !Finclude/crypto/akcipher.h akcipher_request_set_crypt
1837 !Finclude/crypto/akcipher.h crypto_akcipher_maxsize
1838 !Finclude/crypto/akcipher.h crypto_akcipher_encrypt
1839 !Finclude/crypto/akcipher.h crypto_akcipher_decrypt
1840 !Finclude/crypto/akcipher.h crypto_akcipher_sign
1841 !Finclude/crypto/akcipher.h crypto_akcipher_verify
1842 </sect1>
1843 </chapter>
1844
1845 <chapter id="Code"><title>Code Examples</title>
1846 <sect1><title>Code Example For Asynchronous Block Cipher Operation</title>
1847 <programlisting>
1848
1849 struct tcrypt_result {
1850 struct completion completion;
1851 int err;
1852 };
1853
1854 /* tie all data structures together */
1855 struct ablkcipher_def {
1856 struct scatterlist sg;
1857 struct crypto_ablkcipher *tfm;
1858 struct ablkcipher_request *req;
1859 struct tcrypt_result result;
1860 };
1861
1862 /* Callback function */
1863 static void test_ablkcipher_cb(struct crypto_async_request *req, int error)
1864 {
1865 struct tcrypt_result *result = req-&gt;data;
1866
1867 if (error == -EINPROGRESS)
1868 return;
1869 result-&gt;err = error;
1870 complete(&amp;result-&gt;completion);
1871 pr_info("Encryption finished successfully\n");
1872 }
1873
1874 /* Perform cipher operation */
1875 static unsigned int test_ablkcipher_encdec(struct ablkcipher_def *ablk,
1876 int enc)
1877 {
1878 int rc = 0;
1879
1880 if (enc)
1881 rc = crypto_ablkcipher_encrypt(ablk-&gt;req);
1882 else
1883 rc = crypto_ablkcipher_decrypt(ablk-&gt;req);
1884
1885 switch (rc) {
1886 case 0:
1887 break;
1888 case -EINPROGRESS:
1889 case -EBUSY:
1890 rc = wait_for_completion_interruptible(
1891 &amp;ablk-&gt;result.completion);
1892 if (!rc &amp;&amp; !ablk-&gt;result.err) {
1893 reinit_completion(&amp;ablk-&gt;result.completion);
1894 break;
1895 }
1896 default:
1897 pr_info("ablkcipher encrypt returned with %d result %d\n",
1898 rc, ablk-&gt;result.err);
1899 break;
1900 }
1901 init_completion(&amp;ablk-&gt;result.completion);
1902
1903 return rc;
1904 }
1905
1906 /* Initialize and trigger cipher operation */
1907 static int test_ablkcipher(void)
1908 {
1909 struct ablkcipher_def ablk;
1910 struct crypto_ablkcipher *ablkcipher = NULL;
1911 struct ablkcipher_request *req = NULL;
1912 char *scratchpad = NULL;
1913 char *ivdata = NULL;
1914 unsigned char key[32];
1915 int ret = -EFAULT;
1916
1917 ablkcipher = crypto_alloc_ablkcipher("cbc-aes-aesni", 0, 0);
1918 if (IS_ERR(ablkcipher)) {
1919 pr_info("could not allocate ablkcipher handle\n");
1920 return PTR_ERR(ablkcipher);
1921 }
1922
1923 req = ablkcipher_request_alloc(ablkcipher, GFP_KERNEL);
1924 if (IS_ERR(req)) {
1925 pr_info("could not allocate request queue\n");
1926 ret = PTR_ERR(req);
1927 goto out;
1928 }
1929
1930 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1931 test_ablkcipher_cb,
1932 &amp;ablk.result);
1933
1934 /* AES 256 with random key */
1935 get_random_bytes(&amp;key, 32);
1936 if (crypto_ablkcipher_setkey(ablkcipher, key, 32)) {
1937 pr_info("key could not be set\n");
1938 ret = -EAGAIN;
1939 goto out;
1940 }
1941
1942 /* IV will be random */
1943 ivdata = kmalloc(16, GFP_KERNEL);
1944 if (!ivdata) {
1945 pr_info("could not allocate ivdata\n");
1946 goto out;
1947 }
1948 get_random_bytes(ivdata, 16);
1949
1950 /* Input data will be random */
1951 scratchpad = kmalloc(16, GFP_KERNEL);
1952 if (!scratchpad) {
1953 pr_info("could not allocate scratchpad\n");
1954 goto out;
1955 }
1956 get_random_bytes(scratchpad, 16);
1957
1958 ablk.tfm = ablkcipher;
1959 ablk.req = req;
1960
1961 /* We encrypt one block */
1962 sg_init_one(&amp;ablk.sg, scratchpad, 16);
1963 ablkcipher_request_set_crypt(req, &amp;ablk.sg, &amp;ablk.sg, 16, ivdata);
1964 init_completion(&amp;ablk.result.completion);
1965
1966 /* encrypt data */
1967 ret = test_ablkcipher_encdec(&amp;ablk, 1);
1968 if (ret)
1969 goto out;
1970
1971 pr_info("Encryption triggered successfully\n");
1972
1973 out:
1974 if (ablkcipher)
1975 crypto_free_ablkcipher(ablkcipher);
1976 if (req)
1977 ablkcipher_request_free(req);
1978 if (ivdata)
1979 kfree(ivdata);
1980 if (scratchpad)
1981 kfree(scratchpad);
1982 return ret;
1983 }
1984 </programlisting>
1985 </sect1>
1986
1987 <sect1><title>Code Example For Synchronous Block Cipher Operation</title>
1988 <programlisting>
1989
1990 static int test_blkcipher(void)
1991 {
1992 struct crypto_blkcipher *blkcipher = NULL;
1993 char *cipher = "cbc(aes)";
1994 // AES 128
1995 charkey =
1996 "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
1997 chariv =
1998 "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
1999 unsigned int ivsize = 0;
2000 char *scratchpad = NULL; // holds plaintext and ciphertext
2001 struct scatterlist sg;
2002 struct blkcipher_desc desc;
2003 int ret = -EFAULT;
2004
2005 blkcipher = crypto_alloc_blkcipher(cipher, 0, 0);
2006 if (IS_ERR(blkcipher)) {
2007 printk("could not allocate blkcipher handle for %s\n", cipher);
2008 return -PTR_ERR(blkcipher);
2009 }
2010
2011 if (crypto_blkcipher_setkey(blkcipher, key, strlen(key))) {
2012 printk("key could not be set\n");
2013 ret = -EAGAIN;
2014 goto out;
2015 }
2016
2017 ivsize = crypto_blkcipher_ivsize(blkcipher);
2018 if (ivsize) {
2019 if (ivsize != strlen(iv))
2020 printk("IV length differs from expected length\n");
2021 crypto_blkcipher_set_iv(blkcipher, iv, ivsize);
2022 }
2023
2024 scratchpad = kmalloc(crypto_blkcipher_blocksize(blkcipher), GFP_KERNEL);
2025 if (!scratchpad) {
2026 printk("could not allocate scratchpad for %s\n", cipher);
2027 goto out;
2028 }
2029 /* get some random data that we want to encrypt */
2030 get_random_bytes(scratchpad, crypto_blkcipher_blocksize(blkcipher));
2031
2032 desc.flags = 0;
2033 desc.tfm = blkcipher;
2034 sg_init_one(&amp;sg, scratchpad, crypto_blkcipher_blocksize(blkcipher));
2035
2036 /* encrypt data in place */
2037 crypto_blkcipher_encrypt(&amp;desc, &amp;sg, &amp;sg,
2038 crypto_blkcipher_blocksize(blkcipher));
2039
2040 /* decrypt data in place
2041 * crypto_blkcipher_decrypt(&amp;desc, &amp;sg, &amp;sg,
2042 */ crypto_blkcipher_blocksize(blkcipher));
2043
2044
2045 printk("Cipher operation completed\n");
2046 return 0;
2047
2048 out:
2049 if (blkcipher)
2050 crypto_free_blkcipher(blkcipher);
2051 if (scratchpad)
2052 kzfree(scratchpad);
2053 return ret;
2054 }
2055 </programlisting>
2056 </sect1>
2057
2058 <sect1><title>Code Example For Use of Operational State Memory With SHASH</title>
2059 <programlisting>
2060
2061 struct sdesc {
2062 struct shash_desc shash;
2063 char ctx[];
2064 };
2065
2066 static struct sdescinit_sdesc(struct crypto_shash *alg)
2067 {
2068 struct sdescsdesc;
2069 int size;
2070
2071 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
2072 sdesc = kmalloc(size, GFP_KERNEL);
2073 if (!sdesc)
2074 return ERR_PTR(-ENOMEM);
2075 sdesc-&gt;shash.tfm = alg;
2076 sdesc-&gt;shash.flags = 0x0;
2077 return sdesc;
2078 }
2079
2080 static int calc_hash(struct crypto_shashalg,
2081 const unsigned chardata, unsigned int datalen,
2082 unsigned chardigest) {
2083 struct sdescsdesc;
2084 int ret;
2085
2086 sdesc = init_sdesc(alg);
2087 if (IS_ERR(sdesc)) {
2088 pr_info("trusted_key: can't alloc %s\n", hash_alg);
2089 return PTR_ERR(sdesc);
2090 }
2091
2092 ret = crypto_shash_digest(&amp;sdesc-&gt;shash, data, datalen, digest);
2093 kfree(sdesc);
2094 return ret;
2095 }
2096 </programlisting>
2097 </sect1>
2098
2099 <sect1><title>Code Example For Random Number Generator Usage</title>
2100 <programlisting>
2101
2102 static int get_random_numbers(u8 *buf, unsigned int len)
2103 {
2104 struct crypto_rngrng = NULL;
2105 chardrbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
2106 int ret;
2107
2108 if (!buf || !len) {
2109 pr_debug("No output buffer provided\n");
2110 return -EINVAL;
2111 }
2112
2113 rng = crypto_alloc_rng(drbg, 0, 0);
2114 if (IS_ERR(rng)) {
2115 pr_debug("could not allocate RNG handle for %s\n", drbg);
2116 return -PTR_ERR(rng);
2117 }
2118
2119 ret = crypto_rng_get_bytes(rng, buf, len);
2120 if (ret &lt; 0)
2121 pr_debug("generation of random numbers failed\n");
2122 else if (ret == 0)
2123 pr_debug("RNG returned no data");
2124 else
2125 pr_debug("RNG returned %d bytes of data\n", ret);
2126
2127 out:
2128 crypto_free_rng(rng);
2129 return ret;
2130 }
2131 </programlisting>
2132 </sect1>
2133 </chapter>
2134 </book>