crypto: doc - describe internal structure
[deliverable/linux.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 interchangably.
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_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 GIVCIPHER interface. In
540 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 | givcipher | <----------------------------------- 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 | ablkcipher| | 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_givencrypt() to trigger an encryption
624 operation of the GIVCIPHER implementation.
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 ABLKCIPHER 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 ABLKCIPHER 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 ABLKCIPHER 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 ABLKCIPHER 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 </para>
802
803 <programlisting>
804 int crypto_register_alg(struct crypto_alg *alg);
805 int crypto_register_algs(struct crypto_alg *algs, int count);
806 </programlisting>
807
808 <para>
809 The counterparts to those functions are listed below.
810 </para>
811
812 <programlisting>
813 int crypto_unregister_alg(struct crypto_alg *alg);
814 int crypto_unregister_algs(struct crypto_alg *algs, int count);
815 </programlisting>
816
817 <para>
818 Notice that both registration and unregistration functions
819 do return a value, so make sure to handle errors. A return
820 code of zero implies success. Any return code &lt; 0 implies
821 an error.
822 </para>
823
824 <para>
825 The bulk registration / unregistration functions require
826 that struct crypto_alg is an array of count size. These
827 functions simply loop over that array and register /
828 unregister each individual algorithm. If an error occurs,
829 the loop is terminated at the offending algorithm definition.
830 That means, the algorithms prior to the offending algorithm
831 are successfully registered. Note, the caller has no way of
832 knowing which cipher implementations have successfully
833 registered. If this is important to know, the caller should
834 loop through the different implementations using the single
835 instance *_alg functions for each individual implementation.
836 </para>
837 </sect1>
838
839 <sect1><title>Single-Block Symmetric Ciphers [CIPHER]</title>
840 <para>
841 Example of transformations: aes, arc4, ...
842 </para>
843
844 <para>
845 This section describes the simplest of all transformation
846 implementations, that being the CIPHER type used for symmetric
847 ciphers. The CIPHER type is used for transformations which
848 operate on exactly one block at a time and there are no
849 dependencies between blocks at all.
850 </para>
851
852 <sect2><title>Registration specifics</title>
853 <para>
854 The registration of [CIPHER] algorithm is specific in that
855 struct crypto_alg field .cra_type is empty. The .cra_u.cipher
856 has to be filled in with proper callbacks to implement this
857 transformation.
858 </para>
859
860 <para>
861 See struct cipher_alg below.
862 </para>
863 </sect2>
864
865 <sect2><title>Cipher Definition With struct cipher_alg</title>
866 <para>
867 Struct cipher_alg defines a single block cipher.
868 </para>
869
870 <para>
871 Here are schematics of how these functions are called when
872 operated from other part of the kernel. Note that the
873 .cia_setkey() call might happen before or after any of these
874 schematics happen, but must not happen during any of these
875 are in-flight.
876 </para>
877
878 <para>
879 <programlisting>
880 KEY ---. PLAINTEXT ---.
881 v v
882 .cia_setkey() -&gt; .cia_encrypt()
883 |
884 '-----&gt; CIPHERTEXT
885 </programlisting>
886 </para>
887
888 <para>
889 Please note that a pattern where .cia_setkey() is called
890 multiple times is also valid:
891 </para>
892
893 <para>
894 <programlisting>
895
896 KEY1 --. PLAINTEXT1 --. KEY2 --. PLAINTEXT2 --.
897 v v v v
898 .cia_setkey() -&gt; .cia_encrypt() -&gt; .cia_setkey() -&gt; .cia_encrypt()
899 | |
900 '---&gt; CIPHERTEXT1 '---&gt; CIPHERTEXT2
901 </programlisting>
902 </para>
903
904 </sect2>
905 </sect1>
906
907 <sect1><title>Multi-Block Ciphers [BLKCIPHER] [ABLKCIPHER]</title>
908 <para>
909 Example of transformations: cbc(aes), ecb(arc4), ...
910 </para>
911
912 <para>
913 This section describes the multi-block cipher transformation
914 implementations for both synchronous [BLKCIPHER] and
915 asynchronous [ABLKCIPHER] case. The multi-block ciphers are
916 used for transformations which operate on scatterlists of
917 data supplied to the transformation functions. They output
918 the result into a scatterlist of data as well.
919 </para>
920
921 <sect2><title>Registration Specifics</title>
922
923 <para>
924 The registration of [BLKCIPHER] or [ABLKCIPHER] algorithms
925 is one of the most standard procedures throughout the crypto API.
926 </para>
927
928 <para>
929 Note, if a cipher implementation requires a proper alignment
930 of data, the caller should use the functions of
931 crypto_blkcipher_alignmask() or crypto_ablkcipher_alignmask()
932 respectively to identify a memory alignment mask. The kernel
933 crypto API is able to process requests that are unaligned.
934 This implies, however, additional overhead as the kernel
935 crypto API needs to perform the realignment of the data which
936 may imply moving of data.
937 </para>
938 </sect2>
939
940 <sect2><title>Cipher Definition With struct blkcipher_alg and ablkcipher_alg</title>
941 <para>
942 Struct blkcipher_alg defines a synchronous block cipher whereas
943 struct ablkcipher_alg defines an asynchronous block cipher.
944 </para>
945
946 <para>
947 Please refer to the single block cipher description for schematics
948 of the block cipher usage. The usage patterns are exactly the same
949 for [ABLKCIPHER] and [BLKCIPHER] as they are for plain [CIPHER].
950 </para>
951 </sect2>
952
953 <sect2><title>Specifics Of Asynchronous Multi-Block Cipher</title>
954 <para>
955 There are a couple of specifics to the [ABLKCIPHER] interface.
956 </para>
957
958 <para>
959 First of all, some of the drivers will want to use the
960 Generic ScatterWalk in case the hardware needs to be fed
961 separate chunks of the scatterlist which contains the
962 plaintext and will contain the ciphertext. Please refer
963 to the ScatterWalk interface offered by the Linux kernel
964 scatter / gather list implementation.
965 </para>
966 </sect2>
967 </sect1>
968
969 <sect1><title>Hashing [HASH]</title>
970
971 <para>
972 Example of transformations: crc32, md5, sha1, sha256,...
973 </para>
974
975 <sect2><title>Registering And Unregistering The Transformation</title>
976
977 <para>
978 There are multiple ways to register a HASH transformation,
979 depending on whether the transformation is synchronous [SHASH]
980 or asynchronous [AHASH] and the amount of HASH transformations
981 we are registering. You can find the prototypes defined in
982 include/crypto/internal/hash.h:
983 </para>
984
985 <programlisting>
986 int crypto_register_ahash(struct ahash_alg *alg);
987
988 int crypto_register_shash(struct shash_alg *alg);
989 int crypto_register_shashes(struct shash_alg *algs, int count);
990 </programlisting>
991
992 <para>
993 The respective counterparts for unregistering the HASH
994 transformation are as follows:
995 </para>
996
997 <programlisting>
998 int crypto_unregister_ahash(struct ahash_alg *alg);
999
1000 int crypto_unregister_shash(struct shash_alg *alg);
1001 int crypto_unregister_shashes(struct shash_alg *algs, int count);
1002 </programlisting>
1003 </sect2>
1004
1005 <sect2><title>Cipher Definition With struct shash_alg and ahash_alg</title>
1006 <para>
1007 Here are schematics of how these functions are called when
1008 operated from other part of the kernel. Note that the .setkey()
1009 call might happen before or after any of these schematics happen,
1010 but must not happen during any of these are in-flight. Please note
1011 that calling .init() followed immediately by .finish() is also a
1012 perfectly valid transformation.
1013 </para>
1014
1015 <programlisting>
1016 I) DATA -----------.
1017 v
1018 .init() -&gt; .update() -&gt; .final() ! .update() might not be called
1019 ^ | | at all in this scenario.
1020 '----' '---&gt; HASH
1021
1022 II) DATA -----------.-----------.
1023 v v
1024 .init() -&gt; .update() -&gt; .finup() ! .update() may not be called
1025 ^ | | at all in this scenario.
1026 '----' '---&gt; HASH
1027
1028 III) DATA -----------.
1029 v
1030 .digest() ! The entire process is handled
1031 | by the .digest() call.
1032 '---------------&gt; HASH
1033 </programlisting>
1034
1035 <para>
1036 Here is a schematic of how the .export()/.import() functions are
1037 called when used from another part of the kernel.
1038 </para>
1039
1040 <programlisting>
1041 KEY--. DATA--.
1042 v v ! .update() may not be called
1043 .setkey() -&gt; .init() -&gt; .update() -&gt; .export() at all in this scenario.
1044 ^ | |
1045 '-----' '--&gt; PARTIAL_HASH
1046
1047 ----------- other transformations happen here -----------
1048
1049 PARTIAL_HASH--. DATA1--.
1050 v v
1051 .import -&gt; .update() -&gt; .final() ! .update() may not be called
1052 ^ | | at all in this scenario.
1053 '----' '--&gt; HASH1
1054
1055 PARTIAL_HASH--. DATA2-.
1056 v v
1057 .import -&gt; .finup()
1058 |
1059 '---------------&gt; HASH2
1060 </programlisting>
1061 </sect2>
1062
1063 <sect2><title>Specifics Of Asynchronous HASH Transformation</title>
1064 <para>
1065 Some of the drivers will want to use the Generic ScatterWalk
1066 in case the implementation needs to be fed separate chunks of the
1067 scatterlist which contains the input data. The buffer containing
1068 the resulting hash will always be properly aligned to
1069 .cra_alignmask so there is no need to worry about this.
1070 </para>
1071 </sect2>
1072 </sect1>
1073 </chapter>
1074
1075 <chapter id="API"><title>Programming Interface</title>
1076 <sect1><title>Block Cipher Context Data Structures</title>
1077 !Pinclude/linux/crypto.h Block Cipher Context Data Structures
1078 !Finclude/linux/crypto.h aead_request
1079 </sect1>
1080 <sect1><title>Block Cipher Algorithm Definitions</title>
1081 !Pinclude/linux/crypto.h Block Cipher Algorithm Definitions
1082 !Finclude/linux/crypto.h crypto_alg
1083 !Finclude/linux/crypto.h ablkcipher_alg
1084 !Finclude/linux/crypto.h aead_alg
1085 !Finclude/linux/crypto.h blkcipher_alg
1086 !Finclude/linux/crypto.h cipher_alg
1087 !Finclude/linux/crypto.h rng_alg
1088 </sect1>
1089 <sect1><title>Asynchronous Block Cipher API</title>
1090 !Pinclude/linux/crypto.h Asynchronous Block Cipher API
1091 !Finclude/linux/crypto.h crypto_alloc_ablkcipher
1092 !Finclude/linux/crypto.h crypto_free_ablkcipher
1093 !Finclude/linux/crypto.h crypto_has_ablkcipher
1094 !Finclude/linux/crypto.h crypto_ablkcipher_ivsize
1095 !Finclude/linux/crypto.h crypto_ablkcipher_blocksize
1096 !Finclude/linux/crypto.h crypto_ablkcipher_setkey
1097 !Finclude/linux/crypto.h crypto_ablkcipher_reqtfm
1098 !Finclude/linux/crypto.h crypto_ablkcipher_encrypt
1099 !Finclude/linux/crypto.h crypto_ablkcipher_decrypt
1100 </sect1>
1101 <sect1><title>Asynchronous Cipher Request Handle</title>
1102 !Pinclude/linux/crypto.h Asynchronous Cipher Request Handle
1103 !Finclude/linux/crypto.h crypto_ablkcipher_reqsize
1104 !Finclude/linux/crypto.h ablkcipher_request_set_tfm
1105 !Finclude/linux/crypto.h ablkcipher_request_alloc
1106 !Finclude/linux/crypto.h ablkcipher_request_free
1107 !Finclude/linux/crypto.h ablkcipher_request_set_callback
1108 !Finclude/linux/crypto.h ablkcipher_request_set_crypt
1109 </sect1>
1110 <sect1><title>Authenticated Encryption With Associated Data (AEAD) Cipher API</title>
1111 !Pinclude/linux/crypto.h Authenticated Encryption With Associated Data (AEAD) Cipher API
1112 !Finclude/linux/crypto.h crypto_alloc_aead
1113 !Finclude/linux/crypto.h crypto_free_aead
1114 !Finclude/linux/crypto.h crypto_aead_ivsize
1115 !Finclude/linux/crypto.h crypto_aead_authsize
1116 !Finclude/linux/crypto.h crypto_aead_blocksize
1117 !Finclude/linux/crypto.h crypto_aead_setkey
1118 !Finclude/linux/crypto.h crypto_aead_setauthsize
1119 !Finclude/linux/crypto.h crypto_aead_encrypt
1120 !Finclude/linux/crypto.h crypto_aead_decrypt
1121 </sect1>
1122 <sect1><title>Asynchronous AEAD Request Handle</title>
1123 !Pinclude/linux/crypto.h Asynchronous AEAD Request Handle
1124 !Finclude/linux/crypto.h crypto_aead_reqsize
1125 !Finclude/linux/crypto.h aead_request_set_tfm
1126 !Finclude/linux/crypto.h aead_request_alloc
1127 !Finclude/linux/crypto.h aead_request_free
1128 !Finclude/linux/crypto.h aead_request_set_callback
1129 !Finclude/linux/crypto.h aead_request_set_crypt
1130 !Finclude/linux/crypto.h aead_request_set_assoc
1131 </sect1>
1132 <sect1><title>Synchronous Block Cipher API</title>
1133 !Pinclude/linux/crypto.h Synchronous Block Cipher API
1134 !Finclude/linux/crypto.h crypto_alloc_blkcipher
1135 !Finclude/linux/crypto.h crypto_free_blkcipher
1136 !Finclude/linux/crypto.h crypto_has_blkcipher
1137 !Finclude/linux/crypto.h crypto_blkcipher_name
1138 !Finclude/linux/crypto.h crypto_blkcipher_ivsize
1139 !Finclude/linux/crypto.h crypto_blkcipher_blocksize
1140 !Finclude/linux/crypto.h crypto_blkcipher_setkey
1141 !Finclude/linux/crypto.h crypto_blkcipher_encrypt
1142 !Finclude/linux/crypto.h crypto_blkcipher_encrypt_iv
1143 !Finclude/linux/crypto.h crypto_blkcipher_decrypt
1144 !Finclude/linux/crypto.h crypto_blkcipher_decrypt_iv
1145 !Finclude/linux/crypto.h crypto_blkcipher_set_iv
1146 !Finclude/linux/crypto.h crypto_blkcipher_get_iv
1147 </sect1>
1148 <sect1><title>Single Block Cipher API</title>
1149 !Pinclude/linux/crypto.h Single Block Cipher API
1150 !Finclude/linux/crypto.h crypto_alloc_cipher
1151 !Finclude/linux/crypto.h crypto_free_cipher
1152 !Finclude/linux/crypto.h crypto_has_cipher
1153 !Finclude/linux/crypto.h crypto_cipher_blocksize
1154 !Finclude/linux/crypto.h crypto_cipher_setkey
1155 !Finclude/linux/crypto.h crypto_cipher_encrypt_one
1156 !Finclude/linux/crypto.h crypto_cipher_decrypt_one
1157 </sect1>
1158 <sect1><title>Synchronous Message Digest API</title>
1159 !Pinclude/linux/crypto.h Synchronous Message Digest API
1160 !Finclude/linux/crypto.h crypto_alloc_hash
1161 !Finclude/linux/crypto.h crypto_free_hash
1162 !Finclude/linux/crypto.h crypto_has_hash
1163 !Finclude/linux/crypto.h crypto_hash_blocksize
1164 !Finclude/linux/crypto.h crypto_hash_digestsize
1165 !Finclude/linux/crypto.h crypto_hash_init
1166 !Finclude/linux/crypto.h crypto_hash_update
1167 !Finclude/linux/crypto.h crypto_hash_final
1168 !Finclude/linux/crypto.h crypto_hash_digest
1169 !Finclude/linux/crypto.h crypto_hash_setkey
1170 </sect1>
1171 <sect1><title>Message Digest Algorithm Definitions</title>
1172 !Pinclude/crypto/hash.h Message Digest Algorithm Definitions
1173 !Finclude/crypto/hash.h hash_alg_common
1174 !Finclude/crypto/hash.h ahash_alg
1175 !Finclude/crypto/hash.h shash_alg
1176 </sect1>
1177 <sect1><title>Asynchronous Message Digest API</title>
1178 !Pinclude/crypto/hash.h Asynchronous Message Digest API
1179 !Finclude/crypto/hash.h crypto_alloc_ahash
1180 !Finclude/crypto/hash.h crypto_free_ahash
1181 !Finclude/crypto/hash.h crypto_ahash_init
1182 !Finclude/crypto/hash.h crypto_ahash_digestsize
1183 !Finclude/crypto/hash.h crypto_ahash_reqtfm
1184 !Finclude/crypto/hash.h crypto_ahash_reqsize
1185 !Finclude/crypto/hash.h crypto_ahash_setkey
1186 !Finclude/crypto/hash.h crypto_ahash_finup
1187 !Finclude/crypto/hash.h crypto_ahash_final
1188 !Finclude/crypto/hash.h crypto_ahash_digest
1189 !Finclude/crypto/hash.h crypto_ahash_export
1190 !Finclude/crypto/hash.h crypto_ahash_import
1191 </sect1>
1192 <sect1><title>Asynchronous Hash Request Handle</title>
1193 !Pinclude/crypto/hash.h Asynchronous Hash Request Handle
1194 !Finclude/crypto/hash.h ahash_request_set_tfm
1195 !Finclude/crypto/hash.h ahash_request_alloc
1196 !Finclude/crypto/hash.h ahash_request_free
1197 !Finclude/crypto/hash.h ahash_request_set_callback
1198 !Finclude/crypto/hash.h ahash_request_set_crypt
1199 </sect1>
1200 <sect1><title>Synchronous Message Digest API</title>
1201 !Pinclude/crypto/hash.h Synchronous Message Digest API
1202 !Finclude/crypto/hash.h crypto_alloc_shash
1203 !Finclude/crypto/hash.h crypto_free_shash
1204 !Finclude/crypto/hash.h crypto_shash_blocksize
1205 !Finclude/crypto/hash.h crypto_shash_digestsize
1206 !Finclude/crypto/hash.h crypto_shash_descsize
1207 !Finclude/crypto/hash.h crypto_shash_setkey
1208 !Finclude/crypto/hash.h crypto_shash_digest
1209 !Finclude/crypto/hash.h crypto_shash_export
1210 !Finclude/crypto/hash.h crypto_shash_import
1211 !Finclude/crypto/hash.h crypto_shash_init
1212 !Finclude/crypto/hash.h crypto_shash_update
1213 !Finclude/crypto/hash.h crypto_shash_final
1214 !Finclude/crypto/hash.h crypto_shash_finup
1215 </sect1>
1216 <sect1><title>Crypto API Random Number API</title>
1217 !Pinclude/crypto/rng.h Random number generator API
1218 !Finclude/crypto/rng.h crypto_alloc_rng
1219 !Finclude/crypto/rng.h crypto_rng_alg
1220 !Finclude/crypto/rng.h crypto_free_rng
1221 !Finclude/crypto/rng.h crypto_rng_get_bytes
1222 !Finclude/crypto/rng.h crypto_rng_reset
1223 !Finclude/crypto/rng.h crypto_rng_seedsize
1224 !Cinclude/crypto/rng.h
1225 </sect1>
1226 </chapter>
1227
1228 <chapter id="Code"><title>Code Examples</title>
1229 <sect1><title>Code Example For Asynchronous Block Cipher Operation</title>
1230 <programlisting>
1231
1232 struct tcrypt_result {
1233 struct completion completion;
1234 int err;
1235 };
1236
1237 /* tie all data structures together */
1238 struct ablkcipher_def {
1239 struct scatterlist sg;
1240 struct crypto_ablkcipher *tfm;
1241 struct ablkcipher_request *req;
1242 struct tcrypt_result result;
1243 };
1244
1245 /* Callback function */
1246 static void test_ablkcipher_cb(struct crypto_async_request *req, int error)
1247 {
1248 struct tcrypt_result *result = req-&gt;data;
1249
1250 if (error == -EINPROGRESS)
1251 return;
1252 result-&gt;err = error;
1253 complete(&amp;result-&gt;completion);
1254 pr_info("Encryption finished successfully\n");
1255 }
1256
1257 /* Perform cipher operation */
1258 static unsigned int test_ablkcipher_encdec(struct ablkcipher_def *ablk,
1259 int enc)
1260 {
1261 int rc = 0;
1262
1263 if (enc)
1264 rc = crypto_ablkcipher_encrypt(ablk-&gt;req);
1265 else
1266 rc = crypto_ablkcipher_decrypt(ablk-&gt;req);
1267
1268 switch (rc) {
1269 case 0:
1270 break;
1271 case -EINPROGRESS:
1272 case -EBUSY:
1273 rc = wait_for_completion_interruptible(
1274 &amp;ablk-&gt;result.completion);
1275 if (!rc &amp;&amp; !ablk-&gt;result.err) {
1276 reinit_completion(&amp;ablk-&gt;result.completion);
1277 break;
1278 }
1279 default:
1280 pr_info("ablkcipher encrypt returned with %d result %d\n",
1281 rc, ablk-&gt;result.err);
1282 break;
1283 }
1284 init_completion(&amp;ablk-&gt;result.completion);
1285
1286 return rc;
1287 }
1288
1289 /* Initialize and trigger cipher operation */
1290 static int test_ablkcipher(void)
1291 {
1292 struct ablkcipher_def ablk;
1293 struct crypto_ablkcipher *ablkcipher = NULL;
1294 struct ablkcipher_request *req = NULL;
1295 char *scratchpad = NULL;
1296 char *ivdata = NULL;
1297 unsigned char key[32];
1298 int ret = -EFAULT;
1299
1300 ablkcipher = crypto_alloc_ablkcipher("cbc-aes-aesni", 0, 0);
1301 if (IS_ERR(ablkcipher)) {
1302 pr_info("could not allocate ablkcipher handle\n");
1303 return PTR_ERR(ablkcipher);
1304 }
1305
1306 req = ablkcipher_request_alloc(ablkcipher, GFP_KERNEL);
1307 if (IS_ERR(req)) {
1308 pr_info("could not allocate request queue\n");
1309 ret = PTR_ERR(req);
1310 goto out;
1311 }
1312
1313 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1314 test_ablkcipher_cb,
1315 &amp;ablk.result);
1316
1317 /* AES 256 with random key */
1318 get_random_bytes(&amp;key, 32);
1319 if (crypto_ablkcipher_setkey(ablkcipher, key, 32)) {
1320 pr_info("key could not be set\n");
1321 ret = -EAGAIN;
1322 goto out;
1323 }
1324
1325 /* IV will be random */
1326 ivdata = kmalloc(16, GFP_KERNEL);
1327 if (!ivdata) {
1328 pr_info("could not allocate ivdata\n");
1329 goto out;
1330 }
1331 get_random_bytes(ivdata, 16);
1332
1333 /* Input data will be random */
1334 scratchpad = kmalloc(16, GFP_KERNEL);
1335 if (!scratchpad) {
1336 pr_info("could not allocate scratchpad\n");
1337 goto out;
1338 }
1339 get_random_bytes(scratchpad, 16);
1340
1341 ablk.tfm = ablkcipher;
1342 ablk.req = req;
1343
1344 /* We encrypt one block */
1345 sg_init_one(&amp;ablk.sg, scratchpad, 16);
1346 ablkcipher_request_set_crypt(req, &amp;ablk.sg, &amp;ablk.sg, 16, ivdata);
1347 init_completion(&amp;ablk.result.completion);
1348
1349 /* encrypt data */
1350 ret = test_ablkcipher_encdec(&amp;ablk, 1);
1351 if (ret)
1352 goto out;
1353
1354 pr_info("Encryption triggered successfully\n");
1355
1356 out:
1357 if (ablkcipher)
1358 crypto_free_ablkcipher(ablkcipher);
1359 if (req)
1360 ablkcipher_request_free(req);
1361 if (ivdata)
1362 kfree(ivdata);
1363 if (scratchpad)
1364 kfree(scratchpad);
1365 return ret;
1366 }
1367 </programlisting>
1368 </sect1>
1369
1370 <sect1><title>Code Example For Synchronous Block Cipher Operation</title>
1371 <programlisting>
1372
1373 static int test_blkcipher(void)
1374 {
1375 struct crypto_blkcipher *blkcipher = NULL;
1376 char *cipher = "cbc(aes)";
1377 // AES 128
1378 charkey =
1379 "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
1380 chariv =
1381 "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
1382 unsigned int ivsize = 0;
1383 char *scratchpad = NULL; // holds plaintext and ciphertext
1384 struct scatterlist sg;
1385 struct blkcipher_desc desc;
1386 int ret = -EFAULT;
1387
1388 blkcipher = crypto_alloc_blkcipher(cipher, 0, 0);
1389 if (IS_ERR(blkcipher)) {
1390 printk("could not allocate blkcipher handle for %s\n", cipher);
1391 return -PTR_ERR(blkcipher);
1392 }
1393
1394 if (crypto_blkcipher_setkey(blkcipher, key, strlen(key))) {
1395 printk("key could not be set\n");
1396 ret = -EAGAIN;
1397 goto out;
1398 }
1399
1400 ivsize = crypto_blkcipher_ivsize(blkcipher);
1401 if (ivsize) {
1402 if (ivsize != strlen(iv))
1403 printk("IV length differs from expected length\n");
1404 crypto_blkcipher_set_iv(blkcipher, iv, ivsize);
1405 }
1406
1407 scratchpad = kmalloc(crypto_blkcipher_blocksize(blkcipher), GFP_KERNEL);
1408 if (!scratchpad) {
1409 printk("could not allocate scratchpad for %s\n", cipher);
1410 goto out;
1411 }
1412 /* get some random data that we want to encrypt */
1413 get_random_bytes(scratchpad, crypto_blkcipher_blocksize(blkcipher));
1414
1415 desc.flags = 0;
1416 desc.tfm = blkcipher;
1417 sg_init_one(&amp;sg, scratchpad, crypto_blkcipher_blocksize(blkcipher));
1418
1419 /* encrypt data in place */
1420 crypto_blkcipher_encrypt(&amp;desc, &amp;sg, &amp;sg,
1421 crypto_blkcipher_blocksize(blkcipher));
1422
1423 /* decrypt data in place
1424 * crypto_blkcipher_decrypt(&amp;desc, &amp;sg, &amp;sg,
1425 */ crypto_blkcipher_blocksize(blkcipher));
1426
1427
1428 printk("Cipher operation completed\n");
1429 return 0;
1430
1431 out:
1432 if (blkcipher)
1433 crypto_free_blkcipher(blkcipher);
1434 if (scratchpad)
1435 kzfree(scratchpad);
1436 return ret;
1437 }
1438 </programlisting>
1439 </sect1>
1440
1441 <sect1><title>Code Example For Use of Operational State Memory With SHASH</title>
1442 <programlisting>
1443
1444 struct sdesc {
1445 struct shash_desc shash;
1446 char ctx[];
1447 };
1448
1449 static struct sdescinit_sdesc(struct crypto_shash *alg)
1450 {
1451 struct sdescsdesc;
1452 int size;
1453
1454 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
1455 sdesc = kmalloc(size, GFP_KERNEL);
1456 if (!sdesc)
1457 return ERR_PTR(-ENOMEM);
1458 sdesc-&gt;shash.tfm = alg;
1459 sdesc-&gt;shash.flags = 0x0;
1460 return sdesc;
1461 }
1462
1463 static int calc_hash(struct crypto_shashalg,
1464 const unsigned chardata, unsigned int datalen,
1465 unsigned chardigest) {
1466 struct sdescsdesc;
1467 int ret;
1468
1469 sdesc = init_sdesc(alg);
1470 if (IS_ERR(sdesc)) {
1471 pr_info("trusted_key: can't alloc %s\n", hash_alg);
1472 return PTR_ERR(sdesc);
1473 }
1474
1475 ret = crypto_shash_digest(&amp;sdesc-&gt;shash, data, datalen, digest);
1476 kfree(sdesc);
1477 return ret;
1478 }
1479 </programlisting>
1480 </sect1>
1481
1482 <sect1><title>Code Example For Random Number Generator Usage</title>
1483 <programlisting>
1484
1485 static int get_random_numbers(u8 *buf, unsigned int len)
1486 {
1487 struct crypto_rngrng = NULL;
1488 chardrbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
1489 int ret;
1490
1491 if (!buf || !len) {
1492 pr_debug("No output buffer provided\n");
1493 return -EINVAL;
1494 }
1495
1496 rng = crypto_alloc_rng(drbg, 0, 0);
1497 if (IS_ERR(rng)) {
1498 pr_debug("could not allocate RNG handle for %s\n", drbg);
1499 return -PTR_ERR(rng);
1500 }
1501
1502 ret = crypto_rng_get_bytes(rng, buf, len);
1503 if (ret &lt; 0)
1504 pr_debug("generation of random numbers failed\n");
1505 else if (ret == 0)
1506 pr_debug("RNG returned no data");
1507 else
1508 pr_debug("RNG returned %d bytes of data\n", ret);
1509
1510 out:
1511 crypto_free_rng(rng);
1512 return ret;
1513 }
1514 </programlisting>
1515 </sect1>
1516 </chapter>
1517 </book>
This page took 0.097297 seconds and 5 git commands to generate.