208a63290b231e43e1d909eed30044031ed178f7
[cascardo/linux.git] / include / linux / crypto.h
1 /*
2  * Scatterlist Cryptographic API.
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7  *
8  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9  * and Nettle, by Niels Möller.
10  * 
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option) 
14  * any later version.
15  *
16  */
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
19
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27
28 /*
29  * Algorithm masks and types.
30  */
31 #define CRYPTO_ALG_TYPE_MASK            0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
33 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000002
34 #define CRYPTO_ALG_TYPE_AEAD            0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER       0x00000004
36 #define CRYPTO_ALG_TYPE_ABLKCIPHER      0x00000005
37 #define CRYPTO_ALG_TYPE_GIVCIPHER       0x00000006
38 #define CRYPTO_ALG_TYPE_DIGEST          0x00000008
39 #define CRYPTO_ALG_TYPE_HASH            0x00000008
40 #define CRYPTO_ALG_TYPE_SHASH           0x00000009
41 #define CRYPTO_ALG_TYPE_AHASH           0x0000000a
42 #define CRYPTO_ALG_TYPE_RNG             0x0000000c
43 #define CRYPTO_ALG_TYPE_PCOMPRESS       0x0000000f
44
45 #define CRYPTO_ALG_TYPE_HASH_MASK       0x0000000e
46 #define CRYPTO_ALG_TYPE_AHASH_MASK      0x0000000c
47 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK  0x0000000c
48
49 #define CRYPTO_ALG_LARVAL               0x00000010
50 #define CRYPTO_ALG_DEAD                 0x00000020
51 #define CRYPTO_ALG_DYING                0x00000040
52 #define CRYPTO_ALG_ASYNC                0x00000080
53
54 /*
55  * Set this bit if and only if the algorithm requires another algorithm of
56  * the same type to handle corner cases.
57  */
58 #define CRYPTO_ALG_NEED_FALLBACK        0x00000100
59
60 /*
61  * This bit is set for symmetric key ciphers that have already been wrapped
62  * with a generic IV generator to prevent them from being wrapped again.
63  */
64 #define CRYPTO_ALG_GENIV                0x00000200
65
66 /*
67  * Set if the algorithm has passed automated run-time testing.  Note that
68  * if there is no run-time testing for a given algorithm it is considered
69  * to have passed.
70  */
71
72 #define CRYPTO_ALG_TESTED               0x00000400
73
74 /*
75  * Set if the algorithm is an instance that is build from templates.
76  */
77 #define CRYPTO_ALG_INSTANCE             0x00000800
78
79 /* Set this bit if the algorithm provided is hardware accelerated but
80  * not available to userspace via instruction set or so.
81  */
82 #define CRYPTO_ALG_KERN_DRIVER_ONLY     0x00001000
83
84 /*
85  * Transform masks and values (for crt_flags).
86  */
87 #define CRYPTO_TFM_REQ_MASK             0x000fff00
88 #define CRYPTO_TFM_RES_MASK             0xfff00000
89
90 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
91 #define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
92 #define CRYPTO_TFM_REQ_MAY_BACKLOG      0x00000400
93 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
94 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
95 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
96 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
97 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
98
99 /*
100  * Miscellaneous stuff.
101  */
102 #define CRYPTO_MAX_ALG_NAME             64
103
104 /*
105  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
106  * declaration) is used to ensure that the crypto_tfm context structure is
107  * aligned correctly for the given architecture so that there are no alignment
108  * faults for C data types.  In particular, this is required on platforms such
109  * as arm where pointers are 32-bit aligned but there are data types such as
110  * u64 which require 64-bit alignment.
111  */
112 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
113
114 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
115
116 struct scatterlist;
117 struct crypto_ablkcipher;
118 struct crypto_async_request;
119 struct crypto_aead;
120 struct crypto_blkcipher;
121 struct crypto_hash;
122 struct crypto_rng;
123 struct crypto_tfm;
124 struct crypto_type;
125 struct aead_givcrypt_request;
126 struct skcipher_givcrypt_request;
127
128 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
129
130 /**
131  * DOC: Block Cipher Context Data Structures
132  *
133  * These data structures define the operating context for each block cipher
134  * type.
135  */
136
137 struct crypto_async_request {
138         struct list_head list;
139         crypto_completion_t complete;
140         void *data;
141         struct crypto_tfm *tfm;
142
143         u32 flags;
144 };
145
146 struct ablkcipher_request {
147         struct crypto_async_request base;
148
149         unsigned int nbytes;
150
151         void *info;
152
153         struct scatterlist *src;
154         struct scatterlist *dst;
155
156         void *__ctx[] CRYPTO_MINALIGN_ATTR;
157 };
158
159 /**
160  *      struct aead_request - AEAD request
161  *      @base: Common attributes for async crypto requests
162  *      @assoclen: Length in bytes of associated data for authentication
163  *      @cryptlen: Length of data to be encrypted or decrypted
164  *      @iv: Initialisation vector
165  *      @assoc: Associated data
166  *      @src: Source data
167  *      @dst: Destination data
168  *      @__ctx: Start of private context data
169  */
170 struct aead_request {
171         struct crypto_async_request base;
172
173         unsigned int assoclen;
174         unsigned int cryptlen;
175
176         u8 *iv;
177
178         struct scatterlist *assoc;
179         struct scatterlist *src;
180         struct scatterlist *dst;
181
182         void *__ctx[] CRYPTO_MINALIGN_ATTR;
183 };
184
185 struct blkcipher_desc {
186         struct crypto_blkcipher *tfm;
187         void *info;
188         u32 flags;
189 };
190
191 struct cipher_desc {
192         struct crypto_tfm *tfm;
193         void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
194         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
195                              const u8 *src, unsigned int nbytes);
196         void *info;
197 };
198
199 struct hash_desc {
200         struct crypto_hash *tfm;
201         u32 flags;
202 };
203
204 /**
205  * DOC: Block Cipher Algorithm Definitions
206  *
207  * These data structures define modular crypto algorithm implementations,
208  * managed via crypto_register_alg() and crypto_unregister_alg().
209  */
210
211 /**
212  * struct ablkcipher_alg - asynchronous block cipher definition
213  * @min_keysize: Minimum key size supported by the transformation. This is the
214  *               smallest key length supported by this transformation algorithm.
215  *               This must be set to one of the pre-defined values as this is
216  *               not hardware specific. Possible values for this field can be
217  *               found via git grep "_MIN_KEY_SIZE" include/crypto/
218  * @max_keysize: Maximum key size supported by the transformation. This is the
219  *               largest key length supported by this transformation algorithm.
220  *               This must be set to one of the pre-defined values as this is
221  *               not hardware specific. Possible values for this field can be
222  *               found via git grep "_MAX_KEY_SIZE" include/crypto/
223  * @setkey: Set key for the transformation. This function is used to either
224  *          program a supplied key into the hardware or store the key in the
225  *          transformation context for programming it later. Note that this
226  *          function does modify the transformation context. This function can
227  *          be called multiple times during the existence of the transformation
228  *          object, so one must make sure the key is properly reprogrammed into
229  *          the hardware. This function is also responsible for checking the key
230  *          length for validity. In case a software fallback was put in place in
231  *          the @cra_init call, this function might need to use the fallback if
232  *          the algorithm doesn't support all of the key sizes.
233  * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
234  *           the supplied scatterlist containing the blocks of data. The crypto
235  *           API consumer is responsible for aligning the entries of the
236  *           scatterlist properly and making sure the chunks are correctly
237  *           sized. In case a software fallback was put in place in the
238  *           @cra_init call, this function might need to use the fallback if
239  *           the algorithm doesn't support all of the key sizes. In case the
240  *           key was stored in transformation context, the key might need to be
241  *           re-programmed into the hardware in this function. This function
242  *           shall not modify the transformation context, as this function may
243  *           be called in parallel with the same transformation object.
244  * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
245  *           and the conditions are exactly the same.
246  * @givencrypt: Update the IV for encryption. With this function, a cipher
247  *              implementation may provide the function on how to update the IV
248  *              for encryption.
249  * @givdecrypt: Update the IV for decryption. This is the reverse of
250  *              @givencrypt .
251  * @geniv: The transformation implementation may use an "IV generator" provided
252  *         by the kernel crypto API. Several use cases have a predefined
253  *         approach how IVs are to be updated. For such use cases, the kernel
254  *         crypto API provides ready-to-use implementations that can be
255  *         referenced with this variable.
256  * @ivsize: IV size applicable for transformation. The consumer must provide an
257  *          IV of exactly that size to perform the encrypt or decrypt operation.
258  *
259  * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
260  * mandatory and must be filled.
261  */
262 struct ablkcipher_alg {
263         int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
264                       unsigned int keylen);
265         int (*encrypt)(struct ablkcipher_request *req);
266         int (*decrypt)(struct ablkcipher_request *req);
267         int (*givencrypt)(struct skcipher_givcrypt_request *req);
268         int (*givdecrypt)(struct skcipher_givcrypt_request *req);
269
270         const char *geniv;
271
272         unsigned int min_keysize;
273         unsigned int max_keysize;
274         unsigned int ivsize;
275 };
276
277 /**
278  * struct aead_alg - AEAD cipher definition
279  * @maxauthsize: Set the maximum authentication tag size supported by the
280  *               transformation. A transformation may support smaller tag sizes.
281  *               As the authentication tag is a message digest to ensure the
282  *               integrity of the encrypted data, a consumer typically wants the
283  *               largest authentication tag possible as defined by this
284  *               variable.
285  * @setauthsize: Set authentication size for the AEAD transformation. This
286  *               function is used to specify the consumer requested size of the
287  *               authentication tag to be either generated by the transformation
288  *               during encryption or the size of the authentication tag to be
289  *               supplied during the decryption operation. This function is also
290  *               responsible for checking the authentication tag size for
291  *               validity.
292  * @setkey: see struct ablkcipher_alg
293  * @encrypt: see struct ablkcipher_alg
294  * @decrypt: see struct ablkcipher_alg
295  * @givencrypt: see struct ablkcipher_alg
296  * @givdecrypt: see struct ablkcipher_alg
297  * @geniv: see struct ablkcipher_alg
298  * @ivsize: see struct ablkcipher_alg
299  *
300  * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
301  * mandatory and must be filled.
302  */
303 struct aead_alg {
304         int (*setkey)(struct crypto_aead *tfm, const u8 *key,
305                       unsigned int keylen);
306         int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
307         int (*encrypt)(struct aead_request *req);
308         int (*decrypt)(struct aead_request *req);
309         int (*givencrypt)(struct aead_givcrypt_request *req);
310         int (*givdecrypt)(struct aead_givcrypt_request *req);
311
312         const char *geniv;
313
314         unsigned int ivsize;
315         unsigned int maxauthsize;
316 };
317
318 /**
319  * struct blkcipher_alg - synchronous block cipher definition
320  * @min_keysize: see struct ablkcipher_alg
321  * @max_keysize: see struct ablkcipher_alg
322  * @setkey: see struct ablkcipher_alg
323  * @encrypt: see struct ablkcipher_alg
324  * @decrypt: see struct ablkcipher_alg
325  * @geniv: see struct ablkcipher_alg
326  * @ivsize: see struct ablkcipher_alg
327  *
328  * All fields except @geniv and @ivsize are mandatory and must be filled.
329  */
330 struct blkcipher_alg {
331         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
332                       unsigned int keylen);
333         int (*encrypt)(struct blkcipher_desc *desc,
334                        struct scatterlist *dst, struct scatterlist *src,
335                        unsigned int nbytes);
336         int (*decrypt)(struct blkcipher_desc *desc,
337                        struct scatterlist *dst, struct scatterlist *src,
338                        unsigned int nbytes);
339
340         const char *geniv;
341
342         unsigned int min_keysize;
343         unsigned int max_keysize;
344         unsigned int ivsize;
345 };
346
347 /**
348  * struct cipher_alg - single-block symmetric ciphers definition
349  * @cia_min_keysize: Minimum key size supported by the transformation. This is
350  *                   the smallest key length supported by this transformation
351  *                   algorithm. This must be set to one of the pre-defined
352  *                   values as this is not hardware specific. Possible values
353  *                   for this field can be found via git grep "_MIN_KEY_SIZE"
354  *                   include/crypto/
355  * @cia_max_keysize: Maximum key size supported by the transformation. This is
356  *                  the largest key length supported by this transformation
357  *                  algorithm. This must be set to one of the pre-defined values
358  *                  as this is not hardware specific. Possible values for this
359  *                  field can be found via git grep "_MAX_KEY_SIZE"
360  *                  include/crypto/
361  * @cia_setkey: Set key for the transformation. This function is used to either
362  *              program a supplied key into the hardware or store the key in the
363  *              transformation context for programming it later. Note that this
364  *              function does modify the transformation context. This function
365  *              can be called multiple times during the existence of the
366  *              transformation object, so one must make sure the key is properly
367  *              reprogrammed into the hardware. This function is also
368  *              responsible for checking the key length for validity.
369  * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
370  *               single block of data, which must be @cra_blocksize big. This
371  *               always operates on a full @cra_blocksize and it is not possible
372  *               to encrypt a block of smaller size. The supplied buffers must
373  *               therefore also be at least of @cra_blocksize size. Both the
374  *               input and output buffers are always aligned to @cra_alignmask.
375  *               In case either of the input or output buffer supplied by user
376  *               of the crypto API is not aligned to @cra_alignmask, the crypto
377  *               API will re-align the buffers. The re-alignment means that a
378  *               new buffer will be allocated, the data will be copied into the
379  *               new buffer, then the processing will happen on the new buffer,
380  *               then the data will be copied back into the original buffer and
381  *               finally the new buffer will be freed. In case a software
382  *               fallback was put in place in the @cra_init call, this function
383  *               might need to use the fallback if the algorithm doesn't support
384  *               all of the key sizes. In case the key was stored in
385  *               transformation context, the key might need to be re-programmed
386  *               into the hardware in this function. This function shall not
387  *               modify the transformation context, as this function may be
388  *               called in parallel with the same transformation object.
389  * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
390  *               @cia_encrypt, and the conditions are exactly the same.
391  *
392  * All fields are mandatory and must be filled.
393  */
394 struct cipher_alg {
395         unsigned int cia_min_keysize;
396         unsigned int cia_max_keysize;
397         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
398                           unsigned int keylen);
399         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
400         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
401 };
402
403 struct compress_alg {
404         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
405                             unsigned int slen, u8 *dst, unsigned int *dlen);
406         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
407                               unsigned int slen, u8 *dst, unsigned int *dlen);
408 };
409
410 /**
411  * struct rng_alg - random number generator definition
412  * @rng_make_random: The function defined by this variable obtains a random
413  *                   number. The random number generator transform must generate
414  *                   the random number out of the context provided with this
415  *                   call.
416  * @rng_reset: Reset of the random number generator by clearing the entire state.
417  *             With the invocation of this function call, the random number
418  *             generator shall completely reinitialize its state. If the random
419  *             number generator requires a seed for setting up a new state,
420  *             the seed must be provided by the consumer while invoking this
421  *             function. The required size of the seed is defined with
422  *             @seedsize .
423  * @seedsize: The seed size required for a random number generator
424  *            initialization defined with this variable. Some random number
425  *            generators like the SP800-90A DRBG does not require a seed as the
426  *            seeding is implemented internally without the need of support by
427  *            the consumer. In this case, the seed size is set to zero.
428  */
429 struct rng_alg {
430         int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
431                                unsigned int dlen);
432         int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
433
434         unsigned int seedsize;
435 };
436
437
438 #define cra_ablkcipher  cra_u.ablkcipher
439 #define cra_aead        cra_u.aead
440 #define cra_blkcipher   cra_u.blkcipher
441 #define cra_cipher      cra_u.cipher
442 #define cra_compress    cra_u.compress
443 #define cra_rng         cra_u.rng
444
445 /**
446  * struct crypto_alg - definition of a cryptograpic cipher algorithm
447  * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
448  *             CRYPTO_ALG_* flags for the flags which go in here. Those are
449  *             used for fine-tuning the description of the transformation
450  *             algorithm.
451  * @cra_blocksize: Minimum block size of this transformation. The size in bytes
452  *                 of the smallest possible unit which can be transformed with
453  *                 this algorithm. The users must respect this value.
454  *                 In case of HASH transformation, it is possible for a smaller
455  *                 block than @cra_blocksize to be passed to the crypto API for
456  *                 transformation, in case of any other transformation type, an
457  *                 error will be returned upon any attempt to transform smaller
458  *                 than @cra_blocksize chunks.
459  * @cra_ctxsize: Size of the operational context of the transformation. This
460  *               value informs the kernel crypto API about the memory size
461  *               needed to be allocated for the transformation context.
462  * @cra_alignmask: Alignment mask for the input and output data buffer. The data
463  *                 buffer containing the input data for the algorithm must be
464  *                 aligned to this alignment mask. The data buffer for the
465  *                 output data must be aligned to this alignment mask. Note that
466  *                 the Crypto API will do the re-alignment in software, but
467  *                 only under special conditions and there is a performance hit.
468  *                 The re-alignment happens at these occasions for different
469  *                 @cra_u types: cipher -- For both input data and output data
470  *                 buffer; ahash -- For output hash destination buf; shash --
471  *                 For output hash destination buf.
472  *                 This is needed on hardware which is flawed by design and
473  *                 cannot pick data from arbitrary addresses.
474  * @cra_priority: Priority of this transformation implementation. In case
475  *                multiple transformations with same @cra_name are available to
476  *                the Crypto API, the kernel will use the one with highest
477  *                @cra_priority.
478  * @cra_name: Generic name (usable by multiple implementations) of the
479  *            transformation algorithm. This is the name of the transformation
480  *            itself. This field is used by the kernel when looking up the
481  *            providers of particular transformation.
482  * @cra_driver_name: Unique name of the transformation provider. This is the
483  *                   name of the provider of the transformation. This can be any
484  *                   arbitrary value, but in the usual case, this contains the
485  *                   name of the chip or provider and the name of the
486  *                   transformation algorithm.
487  * @cra_type: Type of the cryptographic transformation. This is a pointer to
488  *            struct crypto_type, which implements callbacks common for all
489  *            trasnformation types. There are multiple options:
490  *            &crypto_blkcipher_type, &crypto_ablkcipher_type,
491  *            &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
492  *            This field might be empty. In that case, there are no common
493  *            callbacks. This is the case for: cipher, compress, shash.
494  * @cra_u: Callbacks implementing the transformation. This is a union of
495  *         multiple structures. Depending on the type of transformation selected
496  *         by @cra_type and @cra_flags above, the associated structure must be
497  *         filled with callbacks. This field might be empty. This is the case
498  *         for ahash, shash.
499  * @cra_init: Initialize the cryptographic transformation object. This function
500  *            is used to initialize the cryptographic transformation object.
501  *            This function is called only once at the instantiation time, right
502  *            after the transformation context was allocated. In case the
503  *            cryptographic hardware has some special requirements which need to
504  *            be handled by software, this function shall check for the precise
505  *            requirement of the transformation and put any software fallbacks
506  *            in place.
507  * @cra_exit: Deinitialize the cryptographic transformation object. This is a
508  *            counterpart to @cra_init, used to remove various changes set in
509  *            @cra_init.
510  * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
511  * @cra_list: internally used
512  * @cra_users: internally used
513  * @cra_refcnt: internally used
514  * @cra_destroy: internally used
515  *
516  * The struct crypto_alg describes a generic Crypto API algorithm and is common
517  * for all of the transformations. Any variable not documented here shall not
518  * be used by a cipher implementation as it is internal to the Crypto API.
519  */
520 struct crypto_alg {
521         struct list_head cra_list;
522         struct list_head cra_users;
523
524         u32 cra_flags;
525         unsigned int cra_blocksize;
526         unsigned int cra_ctxsize;
527         unsigned int cra_alignmask;
528
529         int cra_priority;
530         atomic_t cra_refcnt;
531
532         char cra_name[CRYPTO_MAX_ALG_NAME];
533         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
534
535         const struct crypto_type *cra_type;
536
537         union {
538                 struct ablkcipher_alg ablkcipher;
539                 struct aead_alg aead;
540                 struct blkcipher_alg blkcipher;
541                 struct cipher_alg cipher;
542                 struct compress_alg compress;
543                 struct rng_alg rng;
544         } cra_u;
545
546         int (*cra_init)(struct crypto_tfm *tfm);
547         void (*cra_exit)(struct crypto_tfm *tfm);
548         void (*cra_destroy)(struct crypto_alg *alg);
549         
550         struct module *cra_module;
551 };
552
553 /*
554  * Algorithm registration interface.
555  */
556 int crypto_register_alg(struct crypto_alg *alg);
557 int crypto_unregister_alg(struct crypto_alg *alg);
558 int crypto_register_algs(struct crypto_alg *algs, int count);
559 int crypto_unregister_algs(struct crypto_alg *algs, int count);
560
561 /*
562  * Algorithm query interface.
563  */
564 int crypto_has_alg(const char *name, u32 type, u32 mask);
565
566 /*
567  * Transforms: user-instantiated objects which encapsulate algorithms
568  * and core processing logic.  Managed via crypto_alloc_*() and
569  * crypto_free_*(), as well as the various helpers below.
570  */
571
572 struct ablkcipher_tfm {
573         int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
574                       unsigned int keylen);
575         int (*encrypt)(struct ablkcipher_request *req);
576         int (*decrypt)(struct ablkcipher_request *req);
577         int (*givencrypt)(struct skcipher_givcrypt_request *req);
578         int (*givdecrypt)(struct skcipher_givcrypt_request *req);
579
580         struct crypto_ablkcipher *base;
581
582         unsigned int ivsize;
583         unsigned int reqsize;
584 };
585
586 struct aead_tfm {
587         int (*setkey)(struct crypto_aead *tfm, const u8 *key,
588                       unsigned int keylen);
589         int (*encrypt)(struct aead_request *req);
590         int (*decrypt)(struct aead_request *req);
591         int (*givencrypt)(struct aead_givcrypt_request *req);
592         int (*givdecrypt)(struct aead_givcrypt_request *req);
593
594         struct crypto_aead *base;
595
596         unsigned int ivsize;
597         unsigned int authsize;
598         unsigned int reqsize;
599 };
600
601 struct blkcipher_tfm {
602         void *iv;
603         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
604                       unsigned int keylen);
605         int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
606                        struct scatterlist *src, unsigned int nbytes);
607         int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
608                        struct scatterlist *src, unsigned int nbytes);
609 };
610
611 struct cipher_tfm {
612         int (*cit_setkey)(struct crypto_tfm *tfm,
613                           const u8 *key, unsigned int keylen);
614         void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
615         void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
616 };
617
618 struct hash_tfm {
619         int (*init)(struct hash_desc *desc);
620         int (*update)(struct hash_desc *desc,
621                       struct scatterlist *sg, unsigned int nsg);
622         int (*final)(struct hash_desc *desc, u8 *out);
623         int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
624                       unsigned int nsg, u8 *out);
625         int (*setkey)(struct crypto_hash *tfm, const u8 *key,
626                       unsigned int keylen);
627         unsigned int digestsize;
628 };
629
630 struct compress_tfm {
631         int (*cot_compress)(struct crypto_tfm *tfm,
632                             const u8 *src, unsigned int slen,
633                             u8 *dst, unsigned int *dlen);
634         int (*cot_decompress)(struct crypto_tfm *tfm,
635                               const u8 *src, unsigned int slen,
636                               u8 *dst, unsigned int *dlen);
637 };
638
639 struct rng_tfm {
640         int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
641                               unsigned int dlen);
642         int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
643 };
644
645 #define crt_ablkcipher  crt_u.ablkcipher
646 #define crt_aead        crt_u.aead
647 #define crt_blkcipher   crt_u.blkcipher
648 #define crt_cipher      crt_u.cipher
649 #define crt_hash        crt_u.hash
650 #define crt_compress    crt_u.compress
651 #define crt_rng         crt_u.rng
652
653 struct crypto_tfm {
654
655         u32 crt_flags;
656         
657         union {
658                 struct ablkcipher_tfm ablkcipher;
659                 struct aead_tfm aead;
660                 struct blkcipher_tfm blkcipher;
661                 struct cipher_tfm cipher;
662                 struct hash_tfm hash;
663                 struct compress_tfm compress;
664                 struct rng_tfm rng;
665         } crt_u;
666
667         void (*exit)(struct crypto_tfm *tfm);
668         
669         struct crypto_alg *__crt_alg;
670
671         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
672 };
673
674 struct crypto_ablkcipher {
675         struct crypto_tfm base;
676 };
677
678 struct crypto_aead {
679         struct crypto_tfm base;
680 };
681
682 struct crypto_blkcipher {
683         struct crypto_tfm base;
684 };
685
686 struct crypto_cipher {
687         struct crypto_tfm base;
688 };
689
690 struct crypto_comp {
691         struct crypto_tfm base;
692 };
693
694 struct crypto_hash {
695         struct crypto_tfm base;
696 };
697
698 struct crypto_rng {
699         struct crypto_tfm base;
700 };
701
702 enum {
703         CRYPTOA_UNSPEC,
704         CRYPTOA_ALG,
705         CRYPTOA_TYPE,
706         CRYPTOA_U32,
707         __CRYPTOA_MAX,
708 };
709
710 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
711
712 /* Maximum number of (rtattr) parameters for each template. */
713 #define CRYPTO_MAX_ATTRS 32
714
715 struct crypto_attr_alg {
716         char name[CRYPTO_MAX_ALG_NAME];
717 };
718
719 struct crypto_attr_type {
720         u32 type;
721         u32 mask;
722 };
723
724 struct crypto_attr_u32 {
725         u32 num;
726 };
727
728 /* 
729  * Transform user interface.
730  */
731  
732 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
733 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
734
735 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
736 {
737         return crypto_destroy_tfm(tfm, tfm);
738 }
739
740 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
741
742 /*
743  * Transform helpers which query the underlying algorithm.
744  */
745 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
746 {
747         return tfm->__crt_alg->cra_name;
748 }
749
750 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
751 {
752         return tfm->__crt_alg->cra_driver_name;
753 }
754
755 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
756 {
757         return tfm->__crt_alg->cra_priority;
758 }
759
760 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
761 {
762         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
763 }
764
765 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
766 {
767         return tfm->__crt_alg->cra_blocksize;
768 }
769
770 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
771 {
772         return tfm->__crt_alg->cra_alignmask;
773 }
774
775 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
776 {
777         return tfm->crt_flags;
778 }
779
780 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
781 {
782         tfm->crt_flags |= flags;
783 }
784
785 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
786 {
787         tfm->crt_flags &= ~flags;
788 }
789
790 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
791 {
792         return tfm->__crt_ctx;
793 }
794
795 static inline unsigned int crypto_tfm_ctx_alignment(void)
796 {
797         struct crypto_tfm *tfm;
798         return __alignof__(tfm->__crt_ctx);
799 }
800
801 /*
802  * API wrappers.
803  */
804 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
805         struct crypto_tfm *tfm)
806 {
807         return (struct crypto_ablkcipher *)tfm;
808 }
809
810 static inline u32 crypto_skcipher_type(u32 type)
811 {
812         type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
813         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
814         return type;
815 }
816
817 static inline u32 crypto_skcipher_mask(u32 mask)
818 {
819         mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
820         mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
821         return mask;
822 }
823
824 /**
825  * DOC: Asynchronous Block Cipher API
826  *
827  * Asynchronous block cipher API is used with the ciphers of type
828  * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
829  *
830  * Asynchronous cipher operations imply that the function invocation for a
831  * cipher request returns immediately before the completion of the operation.
832  * The cipher request is scheduled as a separate kernel thread and therefore
833  * load-balanced on the different CPUs via the process scheduler. To allow
834  * the kernel crypto API to inform the caller about the completion of a cipher
835  * request, the caller must provide a callback function. That function is
836  * invoked with the cipher handle when the request completes.
837  *
838  * To support the asynchronous operation, additional information than just the
839  * cipher handle must be supplied to the kernel crypto API. That additional
840  * information is given by filling in the ablkcipher_request data structure.
841  *
842  * For the asynchronous block cipher API, the state is maintained with the tfm
843  * cipher handle. A single tfm can be used across multiple calls and in
844  * parallel. For asynchronous block cipher calls, context data supplied and
845  * only used by the caller can be referenced the request data structure in
846  * addition to the IV used for the cipher request. The maintenance of such
847  * state information would be important for a crypto driver implementer to
848  * have, because when calling the callback function upon completion of the
849  * cipher operation, that callback function may need some information about
850  * which operation just finished if it invoked multiple in parallel. This
851  * state information is unused by the kernel crypto API.
852  */
853
854 /**
855  * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
856  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
857  *            ablkcipher cipher
858  * @type: specifies the type of the cipher
859  * @mask: specifies the mask for the cipher
860  *
861  * Allocate a cipher handle for an ablkcipher. The returned struct
862  * crypto_ablkcipher is the cipher handle that is required for any subsequent
863  * API invocation for that ablkcipher.
864  *
865  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
866  *         of an error, PTR_ERR() returns the error code.
867  */
868 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
869                                                   u32 type, u32 mask);
870
871 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
872         struct crypto_ablkcipher *tfm)
873 {
874         return &tfm->base;
875 }
876
877 /**
878  * crypto_free_ablkcipher() - zeroize and free cipher handle
879  * @tfm: cipher handle to be freed
880  */
881 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
882 {
883         crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
884 }
885
886 /**
887  * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
888  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
889  *            ablkcipher
890  * @type: specifies the type of the cipher
891  * @mask: specifies the mask for the cipher
892  *
893  * Return: true when the ablkcipher is known to the kernel crypto API; false
894  *         otherwise
895  */
896 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
897                                         u32 mask)
898 {
899         return crypto_has_alg(alg_name, crypto_skcipher_type(type),
900                               crypto_skcipher_mask(mask));
901 }
902
903 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
904         struct crypto_ablkcipher *tfm)
905 {
906         return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
907 }
908
909 /**
910  * crypto_ablkcipher_ivsize() - obtain IV size
911  * @tfm: cipher handle
912  *
913  * The size of the IV for the ablkcipher referenced by the cipher handle is
914  * returned. This IV size may be zero if the cipher does not need an IV.
915  *
916  * Return: IV size in bytes
917  */
918 static inline unsigned int crypto_ablkcipher_ivsize(
919         struct crypto_ablkcipher *tfm)
920 {
921         return crypto_ablkcipher_crt(tfm)->ivsize;
922 }
923
924 /**
925  * crypto_ablkcipher_blocksize() - obtain block size of cipher
926  * @tfm: cipher handle
927  *
928  * The block size for the ablkcipher referenced with the cipher handle is
929  * returned. The caller may use that information to allocate appropriate
930  * memory for the data returned by the encryption or decryption operation
931  *
932  * Return: block size of cipher
933  */
934 static inline unsigned int crypto_ablkcipher_blocksize(
935         struct crypto_ablkcipher *tfm)
936 {
937         return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
938 }
939
940 static inline unsigned int crypto_ablkcipher_alignmask(
941         struct crypto_ablkcipher *tfm)
942 {
943         return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
944 }
945
946 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
947 {
948         return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
949 }
950
951 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
952                                                u32 flags)
953 {
954         crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
955 }
956
957 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
958                                                  u32 flags)
959 {
960         crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
961 }
962
963 /**
964  * crypto_ablkcipher_setkey() - set key for cipher
965  * @tfm: cipher handle
966  * @key: buffer holding the key
967  * @keylen: length of the key in bytes
968  *
969  * The caller provided key is set for the ablkcipher referenced by the cipher
970  * handle.
971  *
972  * Note, the key length determines the cipher type. Many block ciphers implement
973  * different cipher modes depending on the key size, such as AES-128 vs AES-192
974  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
975  * is performed.
976  *
977  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
978  */
979 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
980                                            const u8 *key, unsigned int keylen)
981 {
982         struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
983
984         return crt->setkey(crt->base, key, keylen);
985 }
986
987 /**
988  * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
989  * @req: ablkcipher_request out of which the cipher handle is to be obtained
990  *
991  * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
992  * data structure.
993  *
994  * Return: crypto_ablkcipher handle
995  */
996 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
997         struct ablkcipher_request *req)
998 {
999         return __crypto_ablkcipher_cast(req->base.tfm);
1000 }
1001
1002 /**
1003  * crypto_ablkcipher_encrypt() - encrypt plaintext
1004  * @req: reference to the ablkcipher_request handle that holds all information
1005  *       needed to perform the cipher operation
1006  *
1007  * Encrypt plaintext data using the ablkcipher_request handle. That data
1008  * structure and how it is filled with data is discussed with the
1009  * ablkcipher_request_* functions.
1010  *
1011  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1012  */
1013 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1014 {
1015         struct ablkcipher_tfm *crt =
1016                 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1017         return crt->encrypt(req);
1018 }
1019
1020 /**
1021  * crypto_ablkcipher_decrypt() - decrypt ciphertext
1022  * @req: reference to the ablkcipher_request handle that holds all information
1023  *       needed to perform the cipher operation
1024  *
1025  * Decrypt ciphertext data using the ablkcipher_request handle. That data
1026  * structure and how it is filled with data is discussed with the
1027  * ablkcipher_request_* functions.
1028  *
1029  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1030  */
1031 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1032 {
1033         struct ablkcipher_tfm *crt =
1034                 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1035         return crt->decrypt(req);
1036 }
1037
1038 /**
1039  * DOC: Asynchronous Cipher Request Handle
1040  *
1041  * The ablkcipher_request data structure contains all pointers to data
1042  * required for the asynchronous cipher operation. This includes the cipher
1043  * handle (which can be used by multiple ablkcipher_request instances), pointer
1044  * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1045  * as a handle to the ablkcipher_request_* API calls in a similar way as
1046  * ablkcipher handle to the crypto_ablkcipher_* API calls.
1047  */
1048
1049 /**
1050  * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1051  * @tfm: cipher handle
1052  *
1053  * Return: number of bytes
1054  */
1055 static inline unsigned int crypto_ablkcipher_reqsize(
1056         struct crypto_ablkcipher *tfm)
1057 {
1058         return crypto_ablkcipher_crt(tfm)->reqsize;
1059 }
1060
1061 /**
1062  * ablkcipher_request_set_tfm() - update cipher handle reference in request
1063  * @req: request handle to be modified
1064  * @tfm: cipher handle that shall be added to the request handle
1065  *
1066  * Allow the caller to replace the existing ablkcipher handle in the request
1067  * data structure with a different one.
1068  */
1069 static inline void ablkcipher_request_set_tfm(
1070         struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1071 {
1072         req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
1073 }
1074
1075 static inline struct ablkcipher_request *ablkcipher_request_cast(
1076         struct crypto_async_request *req)
1077 {
1078         return container_of(req, struct ablkcipher_request, base);
1079 }
1080
1081 /**
1082  * ablkcipher_request_alloc() - allocate request data structure
1083  * @tfm: cipher handle to be registered with the request
1084  * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1085  *
1086  * Allocate the request data structure that must be used with the ablkcipher
1087  * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1088  * handle is registered in the request data structure.
1089  *
1090  * Return: allocated request handle in case of success; IS_ERR() is true in case
1091  *         of an error, PTR_ERR() returns the error code.
1092  */
1093 static inline struct ablkcipher_request *ablkcipher_request_alloc(
1094         struct crypto_ablkcipher *tfm, gfp_t gfp)
1095 {
1096         struct ablkcipher_request *req;
1097
1098         req = kmalloc(sizeof(struct ablkcipher_request) +
1099                       crypto_ablkcipher_reqsize(tfm), gfp);
1100
1101         if (likely(req))
1102                 ablkcipher_request_set_tfm(req, tfm);
1103
1104         return req;
1105 }
1106
1107 /**
1108  * ablkcipher_request_free() - zeroize and free request data structure
1109  * @req: request data structure cipher handle to be freed
1110  */
1111 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1112 {
1113         kzfree(req);
1114 }
1115
1116 /**
1117  * ablkcipher_request_set_callback() - set asynchronous callback function
1118  * @req: request handle
1119  * @flags: specify zero or an ORing of the flags
1120  *         CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1121  *         increase the wait queue beyond the initial maximum size;
1122  *         CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1123  * @compl: callback function pointer to be registered with the request handle
1124  * @data: The data pointer refers to memory that is not used by the kernel
1125  *        crypto API, but provided to the callback function for it to use. Here,
1126  *        the caller can provide a reference to memory the callback function can
1127  *        operate on. As the callback function is invoked asynchronously to the
1128  *        related functionality, it may need to access data structures of the
1129  *        related functionality which can be referenced using this pointer. The
1130  *        callback function can access the memory via the "data" field in the
1131  *        crypto_async_request data structure provided to the callback function.
1132  *
1133  * This function allows setting the callback function that is triggered once the
1134  * cipher operation completes.
1135  *
1136  * The callback function is registered with the ablkcipher_request handle and
1137  * must comply with the following template:
1138  *
1139  *      void callback_function(struct crypto_async_request *req, int error)
1140  */
1141 static inline void ablkcipher_request_set_callback(
1142         struct ablkcipher_request *req,
1143         u32 flags, crypto_completion_t compl, void *data)
1144 {
1145         req->base.complete = compl;
1146         req->base.data = data;
1147         req->base.flags = flags;
1148 }
1149
1150 /**
1151  * ablkcipher_request_set_crypt() - set data buffers
1152  * @req: request handle
1153  * @src: source scatter / gather list
1154  * @dst: destination scatter / gather list
1155  * @nbytes: number of bytes to process from @src
1156  * @iv: IV for the cipher operation which must comply with the IV size defined
1157  *      by crypto_ablkcipher_ivsize
1158  *
1159  * This function allows setting of the source data and destination data
1160  * scatter / gather lists.
1161  *
1162  * For encryption, the source is treated as the plaintext and the
1163  * destination is the ciphertext. For a decryption operation, the use is
1164  * reversed: the source is the ciphertext and the destination is the plaintext.
1165  */
1166 static inline void ablkcipher_request_set_crypt(
1167         struct ablkcipher_request *req,
1168         struct scatterlist *src, struct scatterlist *dst,
1169         unsigned int nbytes, void *iv)
1170 {
1171         req->src = src;
1172         req->dst = dst;
1173         req->nbytes = nbytes;
1174         req->info = iv;
1175 }
1176
1177 /**
1178  * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1179  *
1180  * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1181  * (listed as type "aead" in /proc/crypto)
1182  *
1183  * The most prominent examples for this type of encryption is GCM and CCM.
1184  * However, the kernel supports other types of AEAD ciphers which are defined
1185  * with the following cipher string:
1186  *
1187  *      authenc(keyed message digest, block cipher)
1188  *
1189  * For example: authenc(hmac(sha256), cbc(aes))
1190  *
1191  * The example code provided for the asynchronous block cipher operation
1192  * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1193  * the *aead* pendants discussed in the following. In addtion, for the AEAD
1194  * operation, the aead_request_set_assoc function must be used to set the
1195  * pointer to the associated data memory location before performing the
1196  * encryption or decryption operation. In case of an encryption, the associated
1197  * data memory is filled during the encryption operation. For decryption, the
1198  * associated data memory must contain data that is used to verify the integrity
1199  * of the decrypted data. Another deviation from the asynchronous block cipher
1200  * operation is that the caller should explicitly check for -EBADMSG of the
1201  * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1202  * a breach in the integrity of the message. In essence, that -EBADMSG error
1203  * code is the key bonus an AEAD cipher has over "standard" block chaining
1204  * modes.
1205  */
1206
1207 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1208 {
1209         return (struct crypto_aead *)tfm;
1210 }
1211
1212 /**
1213  * crypto_alloc_aead() - allocate AEAD cipher handle
1214  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1215  *           AEAD cipher
1216  * @type: specifies the type of the cipher
1217  * @mask: specifies the mask for the cipher
1218  *
1219  * Allocate a cipher handle for an AEAD. The returned struct
1220  * crypto_aead is the cipher handle that is required for any subsequent
1221  * API invocation for that AEAD.
1222  *
1223  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1224  *         of an error, PTR_ERR() returns the error code.
1225  */
1226 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
1227
1228 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1229 {
1230         return &tfm->base;
1231 }
1232
1233 /**
1234  * crypto_free_aead() - zeroize and free aead handle
1235  * @tfm: cipher handle to be freed
1236  */
1237 static inline void crypto_free_aead(struct crypto_aead *tfm)
1238 {
1239         crypto_free_tfm(crypto_aead_tfm(tfm));
1240 }
1241
1242 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1243 {
1244         return &crypto_aead_tfm(tfm)->crt_aead;
1245 }
1246
1247 /**
1248  * crypto_aead_ivsize() - obtain IV size
1249  * @tfm: cipher handle
1250  *
1251  * The size of the IV for the aead referenced by the cipher handle is
1252  * returned. This IV size may be zero if the cipher does not need an IV.
1253  *
1254  * Return: IV size in bytes
1255  */
1256 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1257 {
1258         return crypto_aead_crt(tfm)->ivsize;
1259 }
1260
1261 /**
1262  * crypto_aead_authsize() - obtain maximum authentication data size
1263  * @tfm: cipher handle
1264  *
1265  * The maximum size of the authentication data for the AEAD cipher referenced
1266  * by the AEAD cipher handle is returned. The authentication data size may be
1267  * zero if the cipher implements a hard-coded maximum.
1268  *
1269  * The authentication data may also be known as "tag value".
1270  *
1271  * Return: authentication data size / tag size in bytes
1272  */
1273 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1274 {
1275         return crypto_aead_crt(tfm)->authsize;
1276 }
1277
1278 /**
1279  * crypto_aead_blocksize() - obtain block size of cipher
1280  * @tfm: cipher handle
1281  *
1282  * The block size for the AEAD referenced with the cipher handle is returned.
1283  * The caller may use that information to allocate appropriate memory for the
1284  * data returned by the encryption or decryption operation
1285  *
1286  * Return: block size of cipher
1287  */
1288 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1289 {
1290         return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1291 }
1292
1293 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1294 {
1295         return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1296 }
1297
1298 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1299 {
1300         return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1301 }
1302
1303 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1304 {
1305         crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1306 }
1307
1308 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1309 {
1310         crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1311 }
1312
1313 /**
1314  * crypto_aead_setkey() - set key for cipher
1315  * @tfm: cipher handle
1316  * @key: buffer holding the key
1317  * @keylen: length of the key in bytes
1318  *
1319  * The caller provided key is set for the AEAD referenced by the cipher
1320  * handle.
1321  *
1322  * Note, the key length determines the cipher type. Many block ciphers implement
1323  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1324  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1325  * is performed.
1326  *
1327  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1328  */
1329 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1330                                      unsigned int keylen)
1331 {
1332         struct aead_tfm *crt = crypto_aead_crt(tfm);
1333
1334         return crt->setkey(crt->base, key, keylen);
1335 }
1336
1337 /**
1338  * crypto_aead_setauthsize() - set authentication data size
1339  * @tfm: cipher handle
1340  * @authsize: size of the authentication data / tag in bytes
1341  *
1342  * Set the authentication data size / tag size. AEAD requires an authentication
1343  * tag (or MAC) in addition to the associated data.
1344  *
1345  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1346  */
1347 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1348
1349 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1350 {
1351         return __crypto_aead_cast(req->base.tfm);
1352 }
1353
1354 /**
1355  * crypto_aead_encrypt() - encrypt plaintext
1356  * @req: reference to the aead_request handle that holds all information
1357  *       needed to perform the cipher operation
1358  *
1359  * Encrypt plaintext data using the aead_request handle. That data structure
1360  * and how it is filled with data is discussed with the aead_request_*
1361  * functions.
1362  *
1363  * IMPORTANT NOTE The encryption operation creates the authentication data /
1364  *                tag. That data is concatenated with the created ciphertext.
1365  *                The ciphertext memory size is therefore the given number of
1366  *                block cipher blocks + the size defined by the
1367  *                crypto_aead_setauthsize invocation. The caller must ensure
1368  *                that sufficient memory is available for the ciphertext and
1369  *                the authentication tag.
1370  *
1371  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1372  */
1373 static inline int crypto_aead_encrypt(struct aead_request *req)
1374 {
1375         return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1376 }
1377
1378 /**
1379  * crypto_aead_decrypt() - decrypt ciphertext
1380  * @req: reference to the ablkcipher_request handle that holds all information
1381  *       needed to perform the cipher operation
1382  *
1383  * Decrypt ciphertext data using the aead_request handle. That data structure
1384  * and how it is filled with data is discussed with the aead_request_*
1385  * functions.
1386  *
1387  * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1388  *                authentication data / tag. That authentication data / tag
1389  *                must have the size defined by the crypto_aead_setauthsize
1390  *                invocation.
1391  *
1392  *
1393  * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1394  *         cipher operation performs the authentication of the data during the
1395  *         decryption operation. Therefore, the function returns this error if
1396  *         the authentication of the ciphertext was unsuccessful (i.e. the
1397  *         integrity of the ciphertext or the associated data was violated);
1398  *         < 0 if an error occurred.
1399  */
1400 static inline int crypto_aead_decrypt(struct aead_request *req)
1401 {
1402         return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1403 }
1404
1405 /**
1406  * DOC: Asynchronous AEAD Request Handle
1407  *
1408  * The aead_request data structure contains all pointers to data required for
1409  * the AEAD cipher operation. This includes the cipher handle (which can be
1410  * used by multiple aead_request instances), pointer to plaintext and
1411  * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1412  * aead_request_* API calls in a similar way as AEAD handle to the
1413  * crypto_aead_* API calls.
1414  */
1415
1416 /**
1417  * crypto_aead_reqsize() - obtain size of the request data structure
1418  * @tfm: cipher handle
1419  *
1420  * Return: number of bytes
1421  */
1422 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
1423 {
1424         return crypto_aead_crt(tfm)->reqsize;
1425 }
1426
1427 /**
1428  * aead_request_set_tfm() - update cipher handle reference in request
1429  * @req: request handle to be modified
1430  * @tfm: cipher handle that shall be added to the request handle
1431  *
1432  * Allow the caller to replace the existing aead handle in the request
1433  * data structure with a different one.
1434  */
1435 static inline void aead_request_set_tfm(struct aead_request *req,
1436                                         struct crypto_aead *tfm)
1437 {
1438         req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
1439 }
1440
1441 /**
1442  * aead_request_alloc() - allocate request data structure
1443  * @tfm: cipher handle to be registered with the request
1444  * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1445  *
1446  * Allocate the request data structure that must be used with the AEAD
1447  * encrypt and decrypt API calls. During the allocation, the provided aead
1448  * handle is registered in the request data structure.
1449  *
1450  * Return: allocated request handle in case of success; IS_ERR() is true in case
1451  *         of an error, PTR_ERR() returns the error code.
1452  */
1453 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1454                                                       gfp_t gfp)
1455 {
1456         struct aead_request *req;
1457
1458         req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1459
1460         if (likely(req))
1461                 aead_request_set_tfm(req, tfm);
1462
1463         return req;
1464 }
1465
1466 /**
1467  * aead_request_free() - zeroize and free request data structure
1468  * @req: request data structure cipher handle to be freed
1469  */
1470 static inline void aead_request_free(struct aead_request *req)
1471 {
1472         kzfree(req);
1473 }
1474
1475 /**
1476  * aead_request_set_callback() - set asynchronous callback function
1477  * @req: request handle
1478  * @flags: specify zero or an ORing of the flags
1479  *         CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1480  *         increase the wait queue beyond the initial maximum size;
1481  *         CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1482  * @compl: callback function pointer to be registered with the request handle
1483  * @data: The data pointer refers to memory that is not used by the kernel
1484  *        crypto API, but provided to the callback function for it to use. Here,
1485  *        the caller can provide a reference to memory the callback function can
1486  *        operate on. As the callback function is invoked asynchronously to the
1487  *        related functionality, it may need to access data structures of the
1488  *        related functionality which can be referenced using this pointer. The
1489  *        callback function can access the memory via the "data" field in the
1490  *        crypto_async_request data structure provided to the callback function.
1491  *
1492  * Setting the callback function that is triggered once the cipher operation
1493  * completes
1494  *
1495  * The callback function is registered with the aead_request handle and
1496  * must comply with the following template:
1497  *
1498  *      void callback_function(struct crypto_async_request *req, int error)
1499  */
1500 static inline void aead_request_set_callback(struct aead_request *req,
1501                                              u32 flags,
1502                                              crypto_completion_t compl,
1503                                              void *data)
1504 {
1505         req->base.complete = compl;
1506         req->base.data = data;
1507         req->base.flags = flags;
1508 }
1509
1510 /**
1511  * aead_request_set_crypt - set data buffers
1512  * @req: request handle
1513  * @src: source scatter / gather list
1514  * @dst: destination scatter / gather list
1515  * @cryptlen: number of bytes to process from @src
1516  * @iv: IV for the cipher operation which must comply with the IV size defined
1517  *      by crypto_aead_ivsize()
1518  *
1519  * Setting the source data and destination data scatter / gather lists.
1520  *
1521  * For encryption, the source is treated as the plaintext and the
1522  * destination is the ciphertext. For a decryption operation, the use is
1523  * reversed: the source is the ciphertext and the destination is the plaintext.
1524  *
1525  * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1526  *                the caller must concatenate the ciphertext followed by the
1527  *                authentication tag and provide the entire data stream to the
1528  *                decryption operation (i.e. the data length used for the
1529  *                initialization of the scatterlist and the data length for the
1530  *                decryption operation is identical). For encryption, however,
1531  *                the authentication tag is created while encrypting the data.
1532  *                The destination buffer must hold sufficient space for the
1533  *                ciphertext and the authentication tag while the encryption
1534  *                invocation must only point to the plaintext data size. The
1535  *                following code snippet illustrates the memory usage
1536  *                buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1537  *                sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1538  *                aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1539  */
1540 static inline void aead_request_set_crypt(struct aead_request *req,
1541                                           struct scatterlist *src,
1542                                           struct scatterlist *dst,
1543                                           unsigned int cryptlen, u8 *iv)
1544 {
1545         req->src = src;
1546         req->dst = dst;
1547         req->cryptlen = cryptlen;
1548         req->iv = iv;
1549 }
1550
1551 /**
1552  * aead_request_set_assoc() - set the associated data scatter / gather list
1553  * @req: request handle
1554  * @assoc: associated data scatter / gather list
1555  * @assoclen: number of bytes to process from @assoc
1556  *
1557  * For encryption, the memory is filled with the associated data. For
1558  * decryption, the memory must point to the associated data.
1559  */
1560 static inline void aead_request_set_assoc(struct aead_request *req,
1561                                           struct scatterlist *assoc,
1562                                           unsigned int assoclen)
1563 {
1564         req->assoc = assoc;
1565         req->assoclen = assoclen;
1566 }
1567
1568 /**
1569  * DOC: Synchronous Block Cipher API
1570  *
1571  * The synchronous block cipher API is used with the ciphers of type
1572  * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1573  *
1574  * Synchronous calls, have a context in the tfm. But since a single tfm can be
1575  * used in multiple calls and in parallel, this info should not be changeable
1576  * (unless a lock is used). This applies, for example, to the symmetric key.
1577  * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1578  * structure for synchronous blkcipher api. So, its the only state info that can
1579  * be kept for synchronous calls without using a big lock across a tfm.
1580  *
1581  * The block cipher API allows the use of a complete cipher, i.e. a cipher
1582  * consisting of a template (a block chaining mode) and a single block cipher
1583  * primitive (e.g. AES).
1584  *
1585  * The plaintext data buffer and the ciphertext data buffer are pointed to
1586  * by using scatter/gather lists. The cipher operation is performed
1587  * on all segments of the provided scatter/gather lists.
1588  *
1589  * The kernel crypto API supports a cipher operation "in-place" which means that
1590  * the caller may provide the same scatter/gather list for the plaintext and
1591  * cipher text. After the completion of the cipher operation, the plaintext
1592  * data is replaced with the ciphertext data in case of an encryption and vice
1593  * versa for a decryption. The caller must ensure that the scatter/gather lists
1594  * for the output data point to sufficiently large buffers, i.e. multiples of
1595  * the block size of the cipher.
1596  */
1597
1598 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1599         struct crypto_tfm *tfm)
1600 {
1601         return (struct crypto_blkcipher *)tfm;
1602 }
1603
1604 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1605         struct crypto_tfm *tfm)
1606 {
1607         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1608         return __crypto_blkcipher_cast(tfm);
1609 }
1610
1611 /**
1612  * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1613  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1614  *            blkcipher cipher
1615  * @type: specifies the type of the cipher
1616  * @mask: specifies the mask for the cipher
1617  *
1618  * Allocate a cipher handle for a block cipher. The returned struct
1619  * crypto_blkcipher is the cipher handle that is required for any subsequent
1620  * API invocation for that block cipher.
1621  *
1622  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1623  *         of an error, PTR_ERR() returns the error code.
1624  */
1625 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1626         const char *alg_name, u32 type, u32 mask)
1627 {
1628         type &= ~CRYPTO_ALG_TYPE_MASK;
1629         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1630         mask |= CRYPTO_ALG_TYPE_MASK;
1631
1632         return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1633 }
1634
1635 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1636         struct crypto_blkcipher *tfm)
1637 {
1638         return &tfm->base;
1639 }
1640
1641 /**
1642  * crypto_free_blkcipher() - zeroize and free the block cipher handle
1643  * @tfm: cipher handle to be freed
1644  */
1645 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1646 {
1647         crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1648 }
1649
1650 /**
1651  * crypto_has_blkcipher() - Search for the availability of a block cipher
1652  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1653  *            block cipher
1654  * @type: specifies the type of the cipher
1655  * @mask: specifies the mask for the cipher
1656  *
1657  * Return: true when the block cipher is known to the kernel crypto API; false
1658  *         otherwise
1659  */
1660 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1661 {
1662         type &= ~CRYPTO_ALG_TYPE_MASK;
1663         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1664         mask |= CRYPTO_ALG_TYPE_MASK;
1665
1666         return crypto_has_alg(alg_name, type, mask);
1667 }
1668
1669 /**
1670  * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1671  * @tfm: cipher handle
1672  *
1673  * Return: The character string holding the name of the cipher
1674  */
1675 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1676 {
1677         return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1678 }
1679
1680 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1681         struct crypto_blkcipher *tfm)
1682 {
1683         return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1684 }
1685
1686 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1687         struct crypto_blkcipher *tfm)
1688 {
1689         return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1690 }
1691
1692 /**
1693  * crypto_blkcipher_ivsize() - obtain IV size
1694  * @tfm: cipher handle
1695  *
1696  * The size of the IV for the block cipher referenced by the cipher handle is
1697  * returned. This IV size may be zero if the cipher does not need an IV.
1698  *
1699  * Return: IV size in bytes
1700  */
1701 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1702 {
1703         return crypto_blkcipher_alg(tfm)->ivsize;
1704 }
1705
1706 /**
1707  * crypto_blkcipher_blocksize() - obtain block size of cipher
1708  * @tfm: cipher handle
1709  *
1710  * The block size for the block cipher referenced with the cipher handle is
1711  * returned. The caller may use that information to allocate appropriate
1712  * memory for the data returned by the encryption or decryption operation.
1713  *
1714  * Return: block size of cipher
1715  */
1716 static inline unsigned int crypto_blkcipher_blocksize(
1717         struct crypto_blkcipher *tfm)
1718 {
1719         return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1720 }
1721
1722 static inline unsigned int crypto_blkcipher_alignmask(
1723         struct crypto_blkcipher *tfm)
1724 {
1725         return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1726 }
1727
1728 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1729 {
1730         return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1731 }
1732
1733 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1734                                               u32 flags)
1735 {
1736         crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1737 }
1738
1739 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1740                                                 u32 flags)
1741 {
1742         crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1743 }
1744
1745 /**
1746  * crypto_blkcipher_setkey() - set key for cipher
1747  * @tfm: cipher handle
1748  * @key: buffer holding the key
1749  * @keylen: length of the key in bytes
1750  *
1751  * The caller provided key is set for the block cipher referenced by the cipher
1752  * handle.
1753  *
1754  * Note, the key length determines the cipher type. Many block ciphers implement
1755  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1756  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1757  * is performed.
1758  *
1759  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1760  */
1761 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1762                                           const u8 *key, unsigned int keylen)
1763 {
1764         return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1765                                                  key, keylen);
1766 }
1767
1768 /**
1769  * crypto_blkcipher_encrypt() - encrypt plaintext
1770  * @desc: reference to the block cipher handle with meta data
1771  * @dst: scatter/gather list that is filled by the cipher operation with the
1772  *      ciphertext
1773  * @src: scatter/gather list that holds the plaintext
1774  * @nbytes: number of bytes of the plaintext to encrypt.
1775  *
1776  * Encrypt plaintext data using the IV set by the caller with a preceding
1777  * call of crypto_blkcipher_set_iv.
1778  *
1779  * The blkcipher_desc data structure must be filled by the caller and can
1780  * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1781  * with the block cipher handle; desc.flags is filled with either
1782  * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1783  *
1784  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1785  */
1786 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1787                                            struct scatterlist *dst,
1788                                            struct scatterlist *src,
1789                                            unsigned int nbytes)
1790 {
1791         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1792         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1793 }
1794
1795 /**
1796  * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1797  * @desc: reference to the block cipher handle with meta data
1798  * @dst: scatter/gather list that is filled by the cipher operation with the
1799  *      ciphertext
1800  * @src: scatter/gather list that holds the plaintext
1801  * @nbytes: number of bytes of the plaintext to encrypt.
1802  *
1803  * Encrypt plaintext data with the use of an IV that is solely used for this
1804  * cipher operation. Any previously set IV is not used.
1805  *
1806  * The blkcipher_desc data structure must be filled by the caller and can
1807  * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1808  * with the block cipher handle; desc.info is filled with the IV to be used for
1809  * the current operation; desc.flags is filled with either
1810  * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1811  *
1812  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1813  */
1814 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1815                                               struct scatterlist *dst,
1816                                               struct scatterlist *src,
1817                                               unsigned int nbytes)
1818 {
1819         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1820 }
1821
1822 /**
1823  * crypto_blkcipher_decrypt() - decrypt ciphertext
1824  * @desc: reference to the block cipher handle with meta data
1825  * @dst: scatter/gather list that is filled by the cipher operation with the
1826  *      plaintext
1827  * @src: scatter/gather list that holds the ciphertext
1828  * @nbytes: number of bytes of the ciphertext to decrypt.
1829  *
1830  * Decrypt ciphertext data using the IV set by the caller with a preceding
1831  * call of crypto_blkcipher_set_iv.
1832  *
1833  * The blkcipher_desc data structure must be filled by the caller as documented
1834  * for the crypto_blkcipher_encrypt call above.
1835  *
1836  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1837  *
1838  */
1839 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1840                                            struct scatterlist *dst,
1841                                            struct scatterlist *src,
1842                                            unsigned int nbytes)
1843 {
1844         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1845         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1846 }
1847
1848 /**
1849  * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1850  * @desc: reference to the block cipher handle with meta data
1851  * @dst: scatter/gather list that is filled by the cipher operation with the
1852  *      plaintext
1853  * @src: scatter/gather list that holds the ciphertext
1854  * @nbytes: number of bytes of the ciphertext to decrypt.
1855  *
1856  * Decrypt ciphertext data with the use of an IV that is solely used for this
1857  * cipher operation. Any previously set IV is not used.
1858  *
1859  * The blkcipher_desc data structure must be filled by the caller as documented
1860  * for the crypto_blkcipher_encrypt_iv call above.
1861  *
1862  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1863  */
1864 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1865                                               struct scatterlist *dst,
1866                                               struct scatterlist *src,
1867                                               unsigned int nbytes)
1868 {
1869         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1870 }
1871
1872 /**
1873  * crypto_blkcipher_set_iv() - set IV for cipher
1874  * @tfm: cipher handle
1875  * @src: buffer holding the IV
1876  * @len: length of the IV in bytes
1877  *
1878  * The caller provided IV is set for the block cipher referenced by the cipher
1879  * handle.
1880  */
1881 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1882                                            const u8 *src, unsigned int len)
1883 {
1884         memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1885 }
1886
1887 /**
1888  * crypto_blkcipher_get_iv() - obtain IV from cipher
1889  * @tfm: cipher handle
1890  * @dst: buffer filled with the IV
1891  * @len: length of the buffer dst
1892  *
1893  * The caller can obtain the IV set for the block cipher referenced by the
1894  * cipher handle and store it into the user-provided buffer. If the buffer
1895  * has an insufficient space, the IV is truncated to fit the buffer.
1896  */
1897 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1898                                            u8 *dst, unsigned int len)
1899 {
1900         memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1901 }
1902
1903 /**
1904  * DOC: Single Block Cipher API
1905  *
1906  * The single block cipher API is used with the ciphers of type
1907  * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1908  *
1909  * Using the single block cipher API calls, operations with the basic cipher
1910  * primitive can be implemented. These cipher primitives exclude any block
1911  * chaining operations including IV handling.
1912  *
1913  * The purpose of this single block cipher API is to support the implementation
1914  * of templates or other concepts that only need to perform the cipher operation
1915  * on one block at a time. Templates invoke the underlying cipher primitive
1916  * block-wise and process either the input or the output data of these cipher
1917  * operations.
1918  */
1919
1920 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1921 {
1922         return (struct crypto_cipher *)tfm;
1923 }
1924
1925 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1926 {
1927         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1928         return __crypto_cipher_cast(tfm);
1929 }
1930
1931 /**
1932  * crypto_alloc_cipher() - allocate single block cipher handle
1933  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1934  *           single block cipher
1935  * @type: specifies the type of the cipher
1936  * @mask: specifies the mask for the cipher
1937  *
1938  * Allocate a cipher handle for a single block cipher. The returned struct
1939  * crypto_cipher is the cipher handle that is required for any subsequent API
1940  * invocation for that single block cipher.
1941  *
1942  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1943  *         of an error, PTR_ERR() returns the error code.
1944  */
1945 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1946                                                         u32 type, u32 mask)
1947 {
1948         type &= ~CRYPTO_ALG_TYPE_MASK;
1949         type |= CRYPTO_ALG_TYPE_CIPHER;
1950         mask |= CRYPTO_ALG_TYPE_MASK;
1951
1952         return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1953 }
1954
1955 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1956 {
1957         return &tfm->base;
1958 }
1959
1960 /**
1961  * crypto_free_cipher() - zeroize and free the single block cipher handle
1962  * @tfm: cipher handle to be freed
1963  */
1964 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1965 {
1966         crypto_free_tfm(crypto_cipher_tfm(tfm));
1967 }
1968
1969 /**
1970  * crypto_has_cipher() - Search for the availability of a single block cipher
1971  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1972  *           single block cipher
1973  * @type: specifies the type of the cipher
1974  * @mask: specifies the mask for the cipher
1975  *
1976  * Return: true when the single block cipher is known to the kernel crypto API;
1977  *         false otherwise
1978  */
1979 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1980 {
1981         type &= ~CRYPTO_ALG_TYPE_MASK;
1982         type |= CRYPTO_ALG_TYPE_CIPHER;
1983         mask |= CRYPTO_ALG_TYPE_MASK;
1984
1985         return crypto_has_alg(alg_name, type, mask);
1986 }
1987
1988 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1989 {
1990         return &crypto_cipher_tfm(tfm)->crt_cipher;
1991 }
1992
1993 /**
1994  * crypto_cipher_blocksize() - obtain block size for cipher
1995  * @tfm: cipher handle
1996  *
1997  * The block size for the single block cipher referenced with the cipher handle
1998  * tfm is returned. The caller may use that information to allocate appropriate
1999  * memory for the data returned by the encryption or decryption operation
2000  *
2001  * Return: block size of cipher
2002  */
2003 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
2004 {
2005         return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
2006 }
2007
2008 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
2009 {
2010         return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
2011 }
2012
2013 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
2014 {
2015         return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
2016 }
2017
2018 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
2019                                            u32 flags)
2020 {
2021         crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
2022 }
2023
2024 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
2025                                              u32 flags)
2026 {
2027         crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
2028 }
2029
2030 /**
2031  * crypto_cipher_setkey() - set key for cipher
2032  * @tfm: cipher handle
2033  * @key: buffer holding the key
2034  * @keylen: length of the key in bytes
2035  *
2036  * The caller provided key is set for the single block cipher referenced by the
2037  * cipher handle.
2038  *
2039  * Note, the key length determines the cipher type. Many block ciphers implement
2040  * different cipher modes depending on the key size, such as AES-128 vs AES-192
2041  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
2042  * is performed.
2043  *
2044  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2045  */
2046 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
2047                                        const u8 *key, unsigned int keylen)
2048 {
2049         return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
2050                                                   key, keylen);
2051 }
2052
2053 /**
2054  * crypto_cipher_encrypt_one() - encrypt one block of plaintext
2055  * @tfm: cipher handle
2056  * @dst: points to the buffer that will be filled with the ciphertext
2057  * @src: buffer holding the plaintext to be encrypted
2058  *
2059  * Invoke the encryption operation of one block. The caller must ensure that
2060  * the plaintext and ciphertext buffers are at least one block in size.
2061  */
2062 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
2063                                              u8 *dst, const u8 *src)
2064 {
2065         crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
2066                                                 dst, src);
2067 }
2068
2069 /**
2070  * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
2071  * @tfm: cipher handle
2072  * @dst: points to the buffer that will be filled with the plaintext
2073  * @src: buffer holding the ciphertext to be decrypted
2074  *
2075  * Invoke the decryption operation of one block. The caller must ensure that
2076  * the plaintext and ciphertext buffers are at least one block in size.
2077  */
2078 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
2079                                              u8 *dst, const u8 *src)
2080 {
2081         crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
2082                                                 dst, src);
2083 }
2084
2085 /**
2086  * DOC: Synchronous Message Digest API
2087  *
2088  * The synchronous message digest API is used with the ciphers of type
2089  * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
2090  */
2091
2092 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
2093 {
2094         return (struct crypto_hash *)tfm;
2095 }
2096
2097 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
2098 {
2099         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
2100                CRYPTO_ALG_TYPE_HASH_MASK);
2101         return __crypto_hash_cast(tfm);
2102 }
2103
2104 /**
2105  * crypto_alloc_hash() - allocate synchronous message digest handle
2106  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2107  *            message digest cipher
2108  * @type: specifies the type of the cipher
2109  * @mask: specifies the mask for the cipher
2110  *
2111  * Allocate a cipher handle for a message digest. The returned struct
2112  * crypto_hash is the cipher handle that is required for any subsequent
2113  * API invocation for that message digest.
2114  *
2115  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
2116  * of an error, PTR_ERR() returns the error code.
2117  */
2118 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
2119                                                     u32 type, u32 mask)
2120 {
2121         type &= ~CRYPTO_ALG_TYPE_MASK;
2122         mask &= ~CRYPTO_ALG_TYPE_MASK;
2123         type |= CRYPTO_ALG_TYPE_HASH;
2124         mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2125
2126         return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
2127 }
2128
2129 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
2130 {
2131         return &tfm->base;
2132 }
2133
2134 /**
2135  * crypto_free_hash() - zeroize and free message digest handle
2136  * @tfm: cipher handle to be freed
2137  */
2138 static inline void crypto_free_hash(struct crypto_hash *tfm)
2139 {
2140         crypto_free_tfm(crypto_hash_tfm(tfm));
2141 }
2142
2143 /**
2144  * crypto_has_hash() - Search for the availability of a message digest
2145  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2146  *            message digest cipher
2147  * @type: specifies the type of the cipher
2148  * @mask: specifies the mask for the cipher
2149  *
2150  * Return: true when the message digest cipher is known to the kernel crypto
2151  *         API; false otherwise
2152  */
2153 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
2154 {
2155         type &= ~CRYPTO_ALG_TYPE_MASK;
2156         mask &= ~CRYPTO_ALG_TYPE_MASK;
2157         type |= CRYPTO_ALG_TYPE_HASH;
2158         mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2159
2160         return crypto_has_alg(alg_name, type, mask);
2161 }
2162
2163 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
2164 {
2165         return &crypto_hash_tfm(tfm)->crt_hash;
2166 }
2167
2168 /**
2169  * crypto_hash_blocksize() - obtain block size for message digest
2170  * @tfm: cipher handle
2171  *
2172  * The block size for the message digest cipher referenced with the cipher
2173  * handle is returned.
2174  *
2175  * Return: block size of cipher
2176  */
2177 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
2178 {
2179         return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
2180 }
2181
2182 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
2183 {
2184         return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
2185 }
2186
2187 /**
2188  * crypto_hash_digestsize() - obtain message digest size
2189  * @tfm: cipher handle
2190  *
2191  * The size for the message digest created by the message digest cipher
2192  * referenced with the cipher handle is returned.
2193  *
2194  * Return: message digest size
2195  */
2196 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
2197 {
2198         return crypto_hash_crt(tfm)->digestsize;
2199 }
2200
2201 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
2202 {
2203         return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
2204 }
2205
2206 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
2207 {
2208         crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
2209 }
2210
2211 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
2212 {
2213         crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
2214 }
2215
2216 /**
2217  * crypto_hash_init() - (re)initialize message digest handle
2218  * @desc: cipher request handle that to be filled by caller --
2219  *        desc.tfm is filled with the hash cipher handle;
2220  *        desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
2221  *
2222  * The call (re-)initializes the message digest referenced by the hash cipher
2223  * request handle. Any potentially existing state created by previous
2224  * operations is discarded.
2225  *
2226  * Return: 0 if the message digest initialization was successful; < 0 if an
2227  *         error occurred
2228  */
2229 static inline int crypto_hash_init(struct hash_desc *desc)
2230 {
2231         return crypto_hash_crt(desc->tfm)->init(desc);
2232 }
2233
2234 /**
2235  * crypto_hash_update() - add data to message digest for processing
2236  * @desc: cipher request handle
2237  * @sg: scatter / gather list pointing to the data to be added to the message
2238  *      digest
2239  * @nbytes: number of bytes to be processed from @sg
2240  *
2241  * Updates the message digest state of the cipher handle pointed to by the
2242  * hash cipher request handle with the input data pointed to by the
2243  * scatter/gather list.
2244  *
2245  * Return: 0 if the message digest update was successful; < 0 if an error
2246  *         occurred
2247  */
2248 static inline int crypto_hash_update(struct hash_desc *desc,
2249                                      struct scatterlist *sg,
2250                                      unsigned int nbytes)
2251 {
2252         return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
2253 }
2254
2255 /**
2256  * crypto_hash_final() - calculate message digest
2257  * @desc: cipher request handle
2258  * @out: message digest output buffer -- The caller must ensure that the out
2259  *       buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
2260  *       function).
2261  *
2262  * Finalize the message digest operation and create the message digest
2263  * based on all data added to the cipher handle. The message digest is placed
2264  * into the output buffer.
2265  *
2266  * Return: 0 if the message digest creation was successful; < 0 if an error
2267  *         occurred
2268  */
2269 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
2270 {
2271         return crypto_hash_crt(desc->tfm)->final(desc, out);
2272 }
2273
2274 /**
2275  * crypto_hash_digest() - calculate message digest for a buffer
2276  * @desc: see crypto_hash_final()
2277  * @sg: see crypto_hash_update()
2278  * @nbytes:  see crypto_hash_update()
2279  * @out: see crypto_hash_final()
2280  *
2281  * This function is a "short-hand" for the function calls of crypto_hash_init,
2282  * crypto_hash_update and crypto_hash_final. The parameters have the same
2283  * meaning as discussed for those separate three functions.
2284  *
2285  * Return: 0 if the message digest creation was successful; < 0 if an error
2286  *         occurred
2287  */
2288 static inline int crypto_hash_digest(struct hash_desc *desc,
2289                                      struct scatterlist *sg,
2290                                      unsigned int nbytes, u8 *out)
2291 {
2292         return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
2293 }
2294
2295 /**
2296  * crypto_hash_setkey() - set key for message digest
2297  * @hash: cipher handle
2298  * @key: buffer holding the key
2299  * @keylen: length of the key in bytes
2300  *
2301  * The caller provided key is set for the message digest cipher. The cipher
2302  * handle must point to a keyed hash in order for this function to succeed.
2303  *
2304  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2305  */
2306 static inline int crypto_hash_setkey(struct crypto_hash *hash,
2307                                      const u8 *key, unsigned int keylen)
2308 {
2309         return crypto_hash_crt(hash)->setkey(hash, key, keylen);
2310 }
2311
2312 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
2313 {
2314         return (struct crypto_comp *)tfm;
2315 }
2316
2317 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
2318 {
2319         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
2320                CRYPTO_ALG_TYPE_MASK);
2321         return __crypto_comp_cast(tfm);
2322 }
2323
2324 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
2325                                                     u32 type, u32 mask)
2326 {
2327         type &= ~CRYPTO_ALG_TYPE_MASK;
2328         type |= CRYPTO_ALG_TYPE_COMPRESS;
2329         mask |= CRYPTO_ALG_TYPE_MASK;
2330
2331         return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
2332 }
2333
2334 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
2335 {
2336         return &tfm->base;
2337 }
2338
2339 static inline void crypto_free_comp(struct crypto_comp *tfm)
2340 {
2341         crypto_free_tfm(crypto_comp_tfm(tfm));
2342 }
2343
2344 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
2345 {
2346         type &= ~CRYPTO_ALG_TYPE_MASK;
2347         type |= CRYPTO_ALG_TYPE_COMPRESS;
2348         mask |= CRYPTO_ALG_TYPE_MASK;
2349
2350         return crypto_has_alg(alg_name, type, mask);
2351 }
2352
2353 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
2354 {
2355         return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
2356 }
2357
2358 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
2359 {
2360         return &crypto_comp_tfm(tfm)->crt_compress;
2361 }
2362
2363 static inline int crypto_comp_compress(struct crypto_comp *tfm,
2364                                        const u8 *src, unsigned int slen,
2365                                        u8 *dst, unsigned int *dlen)
2366 {
2367         return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
2368                                                   src, slen, dst, dlen);
2369 }
2370
2371 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
2372                                          const u8 *src, unsigned int slen,
2373                                          u8 *dst, unsigned int *dlen)
2374 {
2375         return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
2376                                                     src, slen, dst, dlen);
2377 }
2378
2379 #endif  /* _LINUX_CRYPTO_H */
2380