xref: /linux/Documentation/crypto/libcrypto.rst (revision 0fc8f6200d2313278fbf4539bbab74677c685531)
1*e9af4f47SEric Biggers.. SPDX-License-Identifier: GPL-2.0-or-later
2*e9af4f47SEric Biggers
3*e9af4f47SEric Biggers==============
4*e9af4f47SEric BiggersCrypto library
5*e9af4f47SEric Biggers==============
6*e9af4f47SEric Biggers
7*e9af4f47SEric Biggers``lib/crypto/`` provides faster and easier access to cryptographic algorithms
8*e9af4f47SEric Biggersthan the traditional crypto API.
9*e9af4f47SEric Biggers
10*e9af4f47SEric BiggersEach cryptographic algorithm is supported via a set of dedicated functions.
11*e9af4f47SEric Biggers"Crypto agility", where needed, is left to calling code.
12*e9af4f47SEric Biggers
13*e9af4f47SEric BiggersThe crypto library functions are intended to be boring and straightforward, and
14*e9af4f47SEric Biggersto follow familiar conventions.  Their primary documentation is their (fairly
15*e9af4f47SEric Biggersextensive) kernel-doc.  This page just provides some extra high-level context.
16*e9af4f47SEric Biggers
17*e9af4f47SEric BiggersNote that the crypto library isn't entirely new.  ``lib/`` has contained some
18*e9af4f47SEric Biggerscrypto functions since 2005.  Rather, it's just an approach that's been expanded
19*e9af4f47SEric Biggersover time as it's been found to work well.  It also largely just matches how the
20*e9af4f47SEric Biggerskernel already does things elsewhere.
21*e9af4f47SEric Biggers
22*e9af4f47SEric BiggersScope and intended audience
23*e9af4f47SEric Biggers===========================
24*e9af4f47SEric Biggers
25*e9af4f47SEric BiggersThe crypto library documentation is primarily meant for kernel developers who
26*e9af4f47SEric Biggersneed to use a particular cryptographic algorithm(s) in kernel code.  For
27*e9af4f47SEric Biggersexample, "I just need to compute a SHA-256 hash."  A secondary audience is
28*e9af4f47SEric Biggersdevelopers working on the crypto algorithm implementations themselves.
29*e9af4f47SEric Biggers
30*e9af4f47SEric BiggersIf you're looking for more general information about cryptography, like the
31*e9af4f47SEric Biggersdifferences between the different crypto algorithms or how to select an
32*e9af4f47SEric Biggersappropriate algorithm, you should refer to external sources which cover that
33*e9af4f47SEric Biggerstype of information much more comprehensively.  If you need help selecting
34*e9af4f47SEric Biggersalgorithms for a new kernel feature that doesn't already have its algorithms
35*e9af4f47SEric Biggerspredefined, please reach out to ``linux-crypto@vger.kernel.org`` for advice.
36*e9af4f47SEric Biggers
37*e9af4f47SEric BiggersCode organization
38*e9af4f47SEric Biggers=================
39*e9af4f47SEric Biggers
40*e9af4f47SEric Biggers- ``lib/crypto/*.c``: the crypto algorithm implementations
41*e9af4f47SEric Biggers
42*e9af4f47SEric Biggers- ``lib/crypto/$(SRCARCH)/``: architecture-specific code for crypto algorithms.
43*e9af4f47SEric Biggers  It is here rather than somewhere in ``arch/`` partly because this allows
44*e9af4f47SEric Biggers  generic and architecture-optimized code to be easily built into a single
45*e9af4f47SEric Biggers  loadable module (when the algorithm is set to 'm' in the kconfig).
46*e9af4f47SEric Biggers
47*e9af4f47SEric Biggers- ``lib/crypto/tests/``: KUnit tests for the crypto algorithms
48*e9af4f47SEric Biggers
49*e9af4f47SEric Biggers- ``include/crypto/``: crypto headers, for both the crypto library and the
50*e9af4f47SEric Biggers  traditional crypto API
51*e9af4f47SEric Biggers
52*e9af4f47SEric BiggersGenerally, there is one kernel module per algorithm.  Sometimes related
53*e9af4f47SEric Biggersalgorithms are grouped into one module.  There is intentionally no common
54*e9af4f47SEric Biggersframework, though there are some utility functions that multiple algorithms use.
55*e9af4f47SEric Biggers
56*e9af4f47SEric BiggersEach algorithm module is controlled by a tristate kconfig symbol
57*e9af4f47SEric Biggers``CRYPTO_LIB_$(ALGORITHM)``.  As is the norm for library functions in the
58*e9af4f47SEric Biggerskernel, these are hidden symbols which don't show up in the kconfig menu.
59*e9af4f47SEric BiggersInstead, they are just selected by all the kconfig symbols that need them.
60*e9af4f47SEric Biggers
61*e9af4f47SEric BiggersMany of the algorithms have multiple implementations: a generic implementation
62*e9af4f47SEric Biggersand architecture-optimized implementation(s).  Each module initialization
63*e9af4f47SEric Biggersfunction, or initcall in the built-in case, automatically enables the best
64*e9af4f47SEric Biggersimplementation based on the available CPU features.
65*e9af4f47SEric Biggers
66*e9af4f47SEric BiggersNote that the crypto library doesn't use the ``crypto/``,
67*e9af4f47SEric Biggers``arch/$(SRCARCH)/crypto/``, or ``drivers/crypto/`` directories.  These
68*e9af4f47SEric Biggersdirectories are used by the traditional crypto API.  When possible, algorithms
69*e9af4f47SEric Biggersin the traditional crypto API are implemented by calls into the library.
70*e9af4f47SEric Biggers
71*e9af4f47SEric BiggersAdvantages
72*e9af4f47SEric Biggers==========
73*e9af4f47SEric Biggers
74*e9af4f47SEric BiggersSome of the advantages of the library over the traditional crypto API are:
75*e9af4f47SEric Biggers
76*e9af4f47SEric Biggers- The library functions tend to be much easier to use.  For example, a hash
77*e9af4f47SEric Biggers  value can be computed using only a single function call.  Most of the library
78*e9af4f47SEric Biggers  functions always succeed and return void, eliminating the need to write
79*e9af4f47SEric Biggers  error-handling code.  Most also accept standard virtual addresses, rather than
80*e9af4f47SEric Biggers  scatterlists which are difficult and less efficient to work with.
81*e9af4f47SEric Biggers
82*e9af4f47SEric Biggers- The library functions are usually faster, especially for short inputs.  They
83*e9af4f47SEric Biggers  call the crypto algorithms directly without inefficient indirect calls, memory
84*e9af4f47SEric Biggers  allocations, string parsing, lookups in an algorithm registry, and other
85*e9af4f47SEric Biggers  unnecessary API overhead.  Architecture-optimized code is enabled by default.
86*e9af4f47SEric Biggers
87*e9af4f47SEric Biggers- The library functions use standard link-time dependencies instead of
88*e9af4f47SEric Biggers  error-prone dynamic loading by name.  There's no need for workarounds such as
89*e9af4f47SEric Biggers  forcing algorithms to be built-in or adding module soft dependencies.
90*e9af4f47SEric Biggers
91*e9af4f47SEric Biggers- The library focuses on the approach that works the best on the vast majority
92*e9af4f47SEric Biggers  of systems: CPU-based implementations of the crypto algorithms, utilizing
93*e9af4f47SEric Biggers  on-CPU acceleration (such as AES instructions) when available.
94*e9af4f47SEric Biggers
95*e9af4f47SEric Biggers- The library uses standard KUnit tests, rather than custom ad-hoc tests.
96*e9af4f47SEric Biggers
97*e9af4f47SEric Biggers- The library tends to have higher assurance implementations of the crypto
98*e9af4f47SEric Biggers  algorithms.  This is both due to its simpler design and because more of its
99*e9af4f47SEric Biggers  code is being regularly tested.
100*e9af4f47SEric Biggers
101*e9af4f47SEric Biggers- The library supports features that don't fit into the rigid framework of the
102*e9af4f47SEric Biggers  traditional crypto API, for example interleaved hashing and XOFs.
103*e9af4f47SEric Biggers
104*e9af4f47SEric BiggersWhen to use it
105*e9af4f47SEric Biggers==============
106*e9af4f47SEric Biggers
107*e9af4f47SEric BiggersIn-kernel users should use the library (rather than the traditional crypto API)
108*e9af4f47SEric Biggerswhenever possible.  Many subsystems have already been converted.  It usually
109*e9af4f47SEric Biggerssimplifies their code significantly and improves performance.
110*e9af4f47SEric Biggers
111*e9af4f47SEric BiggersSome kernel features allow userspace to provide an arbitrary string that selects
112*e9af4f47SEric Biggersan arbitrary algorithm from the traditional crypto API by name.  These features
113*e9af4f47SEric Biggersgenerally will have to keep using the traditional crypto API for backwards
114*e9af4f47SEric Biggerscompatibility.
115*e9af4f47SEric Biggers
116*e9af4f47SEric BiggersNote: new kernel features shouldn't support every algorithm, but rather make a
117*e9af4f47SEric Biggersdeliberate choice about what algorithm(s) to support.  History has shown that
118*e9af4f47SEric Biggersmaking a deliberate, thoughtful choice greatly simplifies code maintenance,
119*e9af4f47SEric Biggersreduces the chance for mistakes (such as using an obsolete, insecure, or
120*e9af4f47SEric Biggersinappropriate algorithm), and makes your feature easier to use.
121*e9af4f47SEric Biggers
122*e9af4f47SEric BiggersTesting
123*e9af4f47SEric Biggers=======
124*e9af4f47SEric Biggers
125*e9af4f47SEric BiggersThe crypto library uses standard KUnit tests.  Like many of the kernel's other
126*e9af4f47SEric BiggersKUnit tests, they are included in the set of tests that is run by
127*e9af4f47SEric Biggers``tools/testing/kunit/kunit.py run --alltests``.
128*e9af4f47SEric Biggers
129*e9af4f47SEric BiggersA ``.kunitconfig`` file is also provided to run just the crypto library tests.
130*e9af4f47SEric BiggersFor example, here's how to run them in user-mode Linux:
131*e9af4f47SEric Biggers
132*e9af4f47SEric Biggers.. code-block:: sh
133*e9af4f47SEric Biggers
134*e9af4f47SEric Biggers    tools/testing/kunit/kunit.py run --kunitconfig=lib/crypto/
135*e9af4f47SEric Biggers
136*e9af4f47SEric BiggersMany of the crypto algorithms have architecture-optimized implementations.
137*e9af4f47SEric BiggersTesting those requires building an appropriate kernel and running the tests
138*e9af4f47SEric Biggerseither in QEMU or on appropriate hardware.  Here's one example with QEMU:
139*e9af4f47SEric Biggers
140*e9af4f47SEric Biggers.. code-block:: sh
141*e9af4f47SEric Biggers
142*e9af4f47SEric Biggers    tools/testing/kunit/kunit.py run --kunitconfig=lib/crypto/ --arch=arm64 --make_options LLVM=1
143*e9af4f47SEric Biggers
144*e9af4f47SEric BiggersDepending on the code being tested, flags may need to be passed to QEMU to
145*e9af4f47SEric Biggersemulate the correct type of hardware for the code to be reached.
146*e9af4f47SEric Biggers
147*e9af4f47SEric BiggersSince correctness is essential in cryptographic code, new architecture-optimized
148*e9af4f47SEric Biggerscode is accepted only if it can be tested in QEMU.
149*e9af4f47SEric Biggers
150*e9af4f47SEric BiggersNote: the crypto library also includes FIPS 140 self-tests.  These are
151*e9af4f47SEric Biggerslightweight, are designed specifically to meet FIPS 140 requirements, and exist
152*e9af4f47SEric Biggers*only* to meet those requirements.  Normal testing done by kernel developers and
153*e9af4f47SEric Biggersintegrators should use the much more comprehensive KUnit tests instead.
154*e9af4f47SEric Biggers
155*e9af4f47SEric BiggersAPI documentation
156*e9af4f47SEric Biggers=================
157*e9af4f47SEric Biggers
158*e9af4f47SEric Biggers.. toctree::
159*e9af4f47SEric Biggers   :maxdepth: 2
160*e9af4f47SEric Biggers
161*e9af4f47SEric Biggers   libcrypto-blockcipher
162*e9af4f47SEric Biggers   libcrypto-hash
163*e9af4f47SEric Biggers   libcrypto-signature
164*e9af4f47SEric Biggers   libcrypto-utils
165*e9af4f47SEric Biggers   sha3
166