xref: /OK3568_Linux_fs/kernel/Documentation/crypto/devel-algos.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunDeveloping Cipher Algorithms
2*4882a593Smuzhiyun============================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunRegistering And Unregistering Transformation
5*4882a593Smuzhiyun--------------------------------------------
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThere are three distinct types of registration functions in the Crypto
8*4882a593SmuzhiyunAPI. One is used to register a generic cryptographic transformation,
9*4882a593Smuzhiyunwhile the other two are specific to HASH transformations and
10*4882a593SmuzhiyunCOMPRESSion. We will discuss the latter two in a separate chapter, here
11*4882a593Smuzhiyunwe will only look at the generic ones.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunBefore discussing the register functions, the data structure to be
14*4882a593Smuzhiyunfilled with each, struct crypto_alg, must be considered -- see below
15*4882a593Smuzhiyunfor a description of this data structure.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunThe generic registration functions can be found in
18*4882a593Smuzhiyuninclude/linux/crypto.h and their definition can be seen below. The
19*4882a593Smuzhiyunformer function registers a single transformation, while the latter
20*4882a593Smuzhiyunworks on an array of transformation descriptions. The latter is useful
21*4882a593Smuzhiyunwhen registering transformations in bulk, for example when a driver
22*4882a593Smuzhiyunimplements multiple transformations.
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun::
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun       int crypto_register_alg(struct crypto_alg *alg);
27*4882a593Smuzhiyun       int crypto_register_algs(struct crypto_alg *algs, int count);
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunThe counterparts to those functions are listed below.
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun::
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun       void crypto_unregister_alg(struct crypto_alg *alg);
35*4882a593Smuzhiyun       void crypto_unregister_algs(struct crypto_alg *algs, int count);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunThe registration functions return 0 on success, or a negative errno
39*4882a593Smuzhiyunvalue on failure.  crypto_register_algs() succeeds only if it
40*4882a593Smuzhiyunsuccessfully registered all the given algorithms; if it fails partway
41*4882a593Smuzhiyunthrough, then any changes are rolled back.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunThe unregistration functions always succeed, so they don't have a
44*4882a593Smuzhiyunreturn value.  Don't try to unregister algorithms that aren't
45*4882a593Smuzhiyuncurrently registered.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunSingle-Block Symmetric Ciphers [CIPHER]
48*4882a593Smuzhiyun---------------------------------------
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunExample of transformations: aes, serpent, ...
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunThis section describes the simplest of all transformation
53*4882a593Smuzhiyunimplementations, that being the CIPHER type used for symmetric ciphers.
54*4882a593SmuzhiyunThe CIPHER type is used for transformations which operate on exactly one
55*4882a593Smuzhiyunblock at a time and there are no dependencies between blocks at all.
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunRegistration specifics
58*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunThe registration of [CIPHER] algorithm is specific in that struct
61*4882a593Smuzhiyuncrypto_alg field .cra_type is empty. The .cra_u.cipher has to be
62*4882a593Smuzhiyunfilled in with proper callbacks to implement this transformation.
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunSee struct cipher_alg below.
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunCipher Definition With struct cipher_alg
67*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunStruct cipher_alg defines a single block cipher.
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunHere are schematics of how these functions are called when operated from
72*4882a593Smuzhiyunother part of the kernel. Note that the .cia_setkey() call might happen
73*4882a593Smuzhiyunbefore or after any of these schematics happen, but must not happen
74*4882a593Smuzhiyunduring any of these are in-flight.
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun::
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun             KEY ---.    PLAINTEXT ---.
79*4882a593Smuzhiyun                    v                 v
80*4882a593Smuzhiyun              .cia_setkey() -> .cia_encrypt()
81*4882a593Smuzhiyun                                      |
82*4882a593Smuzhiyun                                      '-----> CIPHERTEXT
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun
85*4882a593SmuzhiyunPlease note that a pattern where .cia_setkey() is called multiple times
86*4882a593Smuzhiyunis also valid:
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun::
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun      KEY1 --.    PLAINTEXT1 --.         KEY2 --.    PLAINTEXT2 --.
92*4882a593Smuzhiyun             v                 v                v                 v
93*4882a593Smuzhiyun       .cia_setkey() -> .cia_encrypt() -> .cia_setkey() -> .cia_encrypt()
94*4882a593Smuzhiyun                               |                                  |
95*4882a593Smuzhiyun                               '---> CIPHERTEXT1                  '---> CIPHERTEXT2
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunMulti-Block Ciphers
99*4882a593Smuzhiyun-------------------
100*4882a593Smuzhiyun
101*4882a593SmuzhiyunExample of transformations: cbc(aes), chacha20, ...
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunThis section describes the multi-block cipher transformation
104*4882a593Smuzhiyunimplementations. The multi-block ciphers are used for transformations
105*4882a593Smuzhiyunwhich operate on scatterlists of data supplied to the transformation
106*4882a593Smuzhiyunfunctions. They output the result into a scatterlist of data as well.
107*4882a593Smuzhiyun
108*4882a593SmuzhiyunRegistration Specifics
109*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunThe registration of multi-block cipher algorithms is one of the most
112*4882a593Smuzhiyunstandard procedures throughout the crypto API.
113*4882a593Smuzhiyun
114*4882a593SmuzhiyunNote, if a cipher implementation requires a proper alignment of data,
115*4882a593Smuzhiyunthe caller should use the functions of crypto_skcipher_alignmask() to
116*4882a593Smuzhiyunidentify a memory alignment mask. The kernel crypto API is able to
117*4882a593Smuzhiyunprocess requests that are unaligned. This implies, however, additional
118*4882a593Smuzhiyunoverhead as the kernel crypto API needs to perform the realignment of
119*4882a593Smuzhiyunthe data which may imply moving of data.
120*4882a593Smuzhiyun
121*4882a593SmuzhiyunCipher Definition With struct skcipher_alg
122*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunStruct skcipher_alg defines a multi-block cipher, or more generally, a
125*4882a593Smuzhiyunlength-preserving symmetric cipher algorithm.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunScatterlist handling
128*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunSome drivers will want to use the Generic ScatterWalk in case the
131*4882a593Smuzhiyunhardware needs to be fed separate chunks of the scatterlist which
132*4882a593Smuzhiyuncontains the plaintext and will contain the ciphertext. Please refer
133*4882a593Smuzhiyunto the ScatterWalk interface offered by the Linux kernel scatter /
134*4882a593Smuzhiyungather list implementation.
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunHashing [HASH]
137*4882a593Smuzhiyun--------------
138*4882a593Smuzhiyun
139*4882a593SmuzhiyunExample of transformations: crc32, md5, sha1, sha256,...
140*4882a593Smuzhiyun
141*4882a593SmuzhiyunRegistering And Unregistering The Transformation
142*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunThere are multiple ways to register a HASH transformation, depending on
145*4882a593Smuzhiyunwhether the transformation is synchronous [SHASH] or asynchronous
146*4882a593Smuzhiyun[AHASH] and the amount of HASH transformations we are registering. You
147*4882a593Smuzhiyuncan find the prototypes defined in include/crypto/internal/hash.h:
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun::
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun       int crypto_register_ahash(struct ahash_alg *alg);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun       int crypto_register_shash(struct shash_alg *alg);
154*4882a593Smuzhiyun       int crypto_register_shashes(struct shash_alg *algs, int count);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunThe respective counterparts for unregistering the HASH transformation
158*4882a593Smuzhiyunare as follows:
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun::
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun       void crypto_unregister_ahash(struct ahash_alg *alg);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun       void crypto_unregister_shash(struct shash_alg *alg);
165*4882a593Smuzhiyun       void crypto_unregister_shashes(struct shash_alg *algs, int count);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunCipher Definition With struct shash_alg and ahash_alg
169*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunHere are schematics of how these functions are called when operated from
172*4882a593Smuzhiyunother part of the kernel. Note that the .setkey() call might happen
173*4882a593Smuzhiyunbefore or after any of these schematics happen, but must not happen
174*4882a593Smuzhiyunduring any of these are in-flight. Please note that calling .init()
175*4882a593Smuzhiyunfollowed immediately by .finish() is also a perfectly valid
176*4882a593Smuzhiyuntransformation.
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun::
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun       I)   DATA -----------.
181*4882a593Smuzhiyun                            v
182*4882a593Smuzhiyun             .init() -> .update() -> .final()      ! .update() might not be called
183*4882a593Smuzhiyun                         ^    |         |            at all in this scenario.
184*4882a593Smuzhiyun                         '----'         '---> HASH
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun       II)  DATA -----------.-----------.
187*4882a593Smuzhiyun                            v           v
188*4882a593Smuzhiyun             .init() -> .update() -> .finup()      ! .update() may not be called
189*4882a593Smuzhiyun                         ^    |         |            at all in this scenario.
190*4882a593Smuzhiyun                         '----'         '---> HASH
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun       III) DATA -----------.
193*4882a593Smuzhiyun                            v
194*4882a593Smuzhiyun                        .digest()                  ! The entire process is handled
195*4882a593Smuzhiyun                            |                        by the .digest() call.
196*4882a593Smuzhiyun                            '---------------> HASH
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun
199*4882a593SmuzhiyunHere is a schematic of how the .export()/.import() functions are called
200*4882a593Smuzhiyunwhen used from another part of the kernel.
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun::
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun       KEY--.                 DATA--.
205*4882a593Smuzhiyun            v                       v                  ! .update() may not be called
206*4882a593Smuzhiyun        .setkey() -> .init() -> .update() -> .export()   at all in this scenario.
207*4882a593Smuzhiyun                                 ^     |         |
208*4882a593Smuzhiyun                                 '-----'         '--> PARTIAL_HASH
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun       ----------- other transformations happen here -----------
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun       PARTIAL_HASH--.   DATA1--.
213*4882a593Smuzhiyun                     v          v
214*4882a593Smuzhiyun                 .import -> .update() -> .final()     ! .update() may not be called
215*4882a593Smuzhiyun                             ^    |         |           at all in this scenario.
216*4882a593Smuzhiyun                             '----'         '--> HASH1
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun       PARTIAL_HASH--.   DATA2-.
219*4882a593Smuzhiyun                     v         v
220*4882a593Smuzhiyun                 .import -> .finup()
221*4882a593Smuzhiyun                               |
222*4882a593Smuzhiyun                               '---------------> HASH2
223*4882a593Smuzhiyun
224*4882a593SmuzhiyunNote that it is perfectly legal to "abandon" a request object:
225*4882a593Smuzhiyun- call .init() and then (as many times) .update()
226*4882a593Smuzhiyun- _not_ call any of .final(), .finup() or .export() at any point in future
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunIn other words implementations should mind the resource allocation and clean-up.
229*4882a593SmuzhiyunNo resources related to request objects should remain allocated after a call
230*4882a593Smuzhiyunto .init() or .update(), since there might be no chance to free them.
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun
233*4882a593SmuzhiyunSpecifics Of Asynchronous HASH Transformation
234*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235*4882a593Smuzhiyun
236*4882a593SmuzhiyunSome of the drivers will want to use the Generic ScatterWalk in case the
237*4882a593Smuzhiyunimplementation needs to be fed separate chunks of the scatterlist which
238*4882a593Smuzhiyuncontains the input data. The buffer containing the resulting hash will
239*4882a593Smuzhiyunalways be properly aligned to .cra_alignmask so there is no need to
240*4882a593Smuzhiyunworry about this.
241