aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/DocBook
diff options
context:
space:
mode:
authorStephan Mueller <smueller@chronox.de>2015-02-27 14:00:00 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2015-03-04 04:12:39 -0500
commit7b24d97f16f561cc90eab1658100598d54a414fd (patch)
tree2fa00f1b217a27099f6c00c62c4a0fc0fda9f245 /Documentation/DocBook
parentd9850fc529efc0c1b3b938aab6916698dcf31f01 (diff)
crypto: doc - describe internal structure
The kernel crypto API has many indirections which warrant a description as otherwise one can get easily lost. The description explains the layers of the kernel crypto API based on examples. Signed-off-by: Stephan Mueller <smueller@chronox.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'Documentation/DocBook')
-rw-r--r--Documentation/DocBook/crypto-API.tmpl264
1 files changed, 264 insertions, 0 deletions
diff --git a/Documentation/DocBook/crypto-API.tmpl b/Documentation/DocBook/crypto-API.tmpl
index 04a8c24ead47..33f63cfc00ca 100644
--- a/Documentation/DocBook/crypto-API.tmpl
+++ b/Documentation/DocBook/crypto-API.tmpl
@@ -509,6 +509,270 @@
509 select it due to the used type and mask field. 509 select it due to the used type and mask field.
510 </para> 510 </para>
511 </sect1> 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[
583kernel 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[
727kernel 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>
512 </chapter> 776 </chapter>
513 777
514 <chapter id="Development"><title>Developing Cipher Algorithms</title> 778 <chapter id="Development"><title>Developing Cipher Algorithms</title>