Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[linux-2.6-microblaze.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 #include <linux/completion.h>
28
29 /*
30  * Autoloaded crypto modules should only use a prefixed name to avoid allowing
31  * arbitrary modules to be loaded. Loading from userspace may still need the
32  * unprefixed names, so retains those aliases as well.
33  * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
34  * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
35  * expands twice on the same line. Instead, use a separate base name for the
36  * alias.
37  */
38 #define MODULE_ALIAS_CRYPTO(name)       \
39                 __MODULE_INFO(alias, alias_userspace, name);    \
40                 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
41
42 /*
43  * Algorithm masks and types.
44  */
45 #define CRYPTO_ALG_TYPE_MASK            0x0000000f
46 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
47 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000002
48 #define CRYPTO_ALG_TYPE_AEAD            0x00000003
49 #define CRYPTO_ALG_TYPE_BLKCIPHER       0x00000004
50 #define CRYPTO_ALG_TYPE_ABLKCIPHER      0x00000005
51 #define CRYPTO_ALG_TYPE_SKCIPHER        0x00000005
52 #define CRYPTO_ALG_TYPE_KPP             0x00000008
53 #define CRYPTO_ALG_TYPE_ACOMPRESS       0x0000000a
54 #define CRYPTO_ALG_TYPE_SCOMPRESS       0x0000000b
55 #define CRYPTO_ALG_TYPE_RNG             0x0000000c
56 #define CRYPTO_ALG_TYPE_AKCIPHER        0x0000000d
57 #define CRYPTO_ALG_TYPE_DIGEST          0x0000000e
58 #define CRYPTO_ALG_TYPE_HASH            0x0000000e
59 #define CRYPTO_ALG_TYPE_SHASH           0x0000000e
60 #define CRYPTO_ALG_TYPE_AHASH           0x0000000f
61
62 #define CRYPTO_ALG_TYPE_HASH_MASK       0x0000000e
63 #define CRYPTO_ALG_TYPE_AHASH_MASK      0x0000000e
64 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK  0x0000000c
65 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK  0x0000000e
66
67 #define CRYPTO_ALG_LARVAL               0x00000010
68 #define CRYPTO_ALG_DEAD                 0x00000020
69 #define CRYPTO_ALG_DYING                0x00000040
70 #define CRYPTO_ALG_ASYNC                0x00000080
71
72 /*
73  * Set this bit if and only if the algorithm requires another algorithm of
74  * the same type to handle corner cases.
75  */
76 #define CRYPTO_ALG_NEED_FALLBACK        0x00000100
77
78 /*
79  * Set if the algorithm has passed automated run-time testing.  Note that
80  * if there is no run-time testing for a given algorithm it is considered
81  * to have passed.
82  */
83
84 #define CRYPTO_ALG_TESTED               0x00000400
85
86 /*
87  * Set if the algorithm is an instance that is built from templates.
88  */
89 #define CRYPTO_ALG_INSTANCE             0x00000800
90
91 /* Set this bit if the algorithm provided is hardware accelerated but
92  * not available to userspace via instruction set or so.
93  */
94 #define CRYPTO_ALG_KERN_DRIVER_ONLY     0x00001000
95
96 /*
97  * Mark a cipher as a service implementation only usable by another
98  * cipher and never by a normal user of the kernel crypto API
99  */
100 #define CRYPTO_ALG_INTERNAL             0x00002000
101
102 /*
103  * Set if the algorithm has a ->setkey() method but can be used without
104  * calling it first, i.e. there is a default key.
105  */
106 #define CRYPTO_ALG_OPTIONAL_KEY         0x00004000
107
108 /*
109  * Don't trigger module loading
110  */
111 #define CRYPTO_NOLOAD                   0x00008000
112
113 /*
114  * Transform masks and values (for crt_flags).
115  */
116 #define CRYPTO_TFM_NEED_KEY             0x00000001
117
118 #define CRYPTO_TFM_REQ_MASK             0x000fff00
119 #define CRYPTO_TFM_RES_MASK             0xfff00000
120
121 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
122 #define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
123 #define CRYPTO_TFM_REQ_MAY_BACKLOG      0x00000400
124 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
125 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
126 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
127 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
128 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
129
130 /*
131  * Miscellaneous stuff.
132  */
133 #define CRYPTO_MAX_ALG_NAME             128
134
135 /*
136  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
137  * declaration) is used to ensure that the crypto_tfm context structure is
138  * aligned correctly for the given architecture so that there are no alignment
139  * faults for C data types.  In particular, this is required on platforms such
140  * as arm where pointers are 32-bit aligned but there are data types such as
141  * u64 which require 64-bit alignment.
142  */
143 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
144
145 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
146
147 struct scatterlist;
148 struct crypto_ablkcipher;
149 struct crypto_async_request;
150 struct crypto_blkcipher;
151 struct crypto_tfm;
152 struct crypto_type;
153
154 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
155
156 /**
157  * DOC: Block Cipher Context Data Structures
158  *
159  * These data structures define the operating context for each block cipher
160  * type.
161  */
162
163 struct crypto_async_request {
164         struct list_head list;
165         crypto_completion_t complete;
166         void *data;
167         struct crypto_tfm *tfm;
168
169         u32 flags;
170 };
171
172 struct ablkcipher_request {
173         struct crypto_async_request base;
174
175         unsigned int nbytes;
176
177         void *info;
178
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 /**
200  * DOC: Block Cipher Algorithm Definitions
201  *
202  * These data structures define modular crypto algorithm implementations,
203  * managed via crypto_register_alg() and crypto_unregister_alg().
204  */
205
206 /**
207  * struct ablkcipher_alg - asynchronous block cipher definition
208  * @min_keysize: Minimum key size supported by the transformation. This is the
209  *               smallest key length supported by this transformation algorithm.
210  *               This must be set to one of the pre-defined values as this is
211  *               not hardware specific. Possible values for this field can be
212  *               found via git grep "_MIN_KEY_SIZE" include/crypto/
213  * @max_keysize: Maximum key size supported by the transformation. This is the
214  *               largest 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 "_MAX_KEY_SIZE" include/crypto/
218  * @setkey: Set key for the transformation. This function is used to either
219  *          program a supplied key into the hardware or store the key in the
220  *          transformation context for programming it later. Note that this
221  *          function does modify the transformation context. This function can
222  *          be called multiple times during the existence of the transformation
223  *          object, so one must make sure the key is properly reprogrammed into
224  *          the hardware. This function is also responsible for checking the key
225  *          length for validity. In case a software fallback was put in place in
226  *          the @cra_init call, this function might need to use the fallback if
227  *          the algorithm doesn't support all of the key sizes.
228  * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
229  *           the supplied scatterlist containing the blocks of data. The crypto
230  *           API consumer is responsible for aligning the entries of the
231  *           scatterlist properly and making sure the chunks are correctly
232  *           sized. In case a software fallback was put in place in the
233  *           @cra_init call, this function might need to use the fallback if
234  *           the algorithm doesn't support all of the key sizes. In case the
235  *           key was stored in transformation context, the key might need to be
236  *           re-programmed into the hardware in this function. This function
237  *           shall not modify the transformation context, as this function may
238  *           be called in parallel with the same transformation object.
239  * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
240  *           and the conditions are exactly the same.
241  * @ivsize: IV size applicable for transformation. The consumer must provide an
242  *          IV of exactly that size to perform the encrypt or decrypt operation.
243  *
244  * All fields except @ivsize are mandatory and must be filled.
245  */
246 struct ablkcipher_alg {
247         int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
248                       unsigned int keylen);
249         int (*encrypt)(struct ablkcipher_request *req);
250         int (*decrypt)(struct ablkcipher_request *req);
251
252         unsigned int min_keysize;
253         unsigned int max_keysize;
254         unsigned int ivsize;
255 };
256
257 /**
258  * struct blkcipher_alg - synchronous block cipher definition
259  * @min_keysize: see struct ablkcipher_alg
260  * @max_keysize: see struct ablkcipher_alg
261  * @setkey: see struct ablkcipher_alg
262  * @encrypt: see struct ablkcipher_alg
263  * @decrypt: see struct ablkcipher_alg
264  * @ivsize: see struct ablkcipher_alg
265  *
266  * All fields except @ivsize are mandatory and must be filled.
267  */
268 struct blkcipher_alg {
269         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
270                       unsigned int keylen);
271         int (*encrypt)(struct blkcipher_desc *desc,
272                        struct scatterlist *dst, struct scatterlist *src,
273                        unsigned int nbytes);
274         int (*decrypt)(struct blkcipher_desc *desc,
275                        struct scatterlist *dst, struct scatterlist *src,
276                        unsigned int nbytes);
277
278         unsigned int min_keysize;
279         unsigned int max_keysize;
280         unsigned int ivsize;
281 };
282
283 /**
284  * struct cipher_alg - single-block symmetric ciphers definition
285  * @cia_min_keysize: Minimum key size supported by the transformation. This is
286  *                   the smallest key length supported by this transformation
287  *                   algorithm. This must be set to one of the pre-defined
288  *                   values as this is not hardware specific. Possible values
289  *                   for this field can be found via git grep "_MIN_KEY_SIZE"
290  *                   include/crypto/
291  * @cia_max_keysize: Maximum key size supported by the transformation. This is
292  *                  the largest key length supported by this transformation
293  *                  algorithm. This must be set to one of the pre-defined values
294  *                  as this is not hardware specific. Possible values for this
295  *                  field can be found via git grep "_MAX_KEY_SIZE"
296  *                  include/crypto/
297  * @cia_setkey: Set key for the transformation. This function is used to either
298  *              program a supplied key into the hardware or store the key in the
299  *              transformation context for programming it later. Note that this
300  *              function does modify the transformation context. This function
301  *              can be called multiple times during the existence of the
302  *              transformation object, so one must make sure the key is properly
303  *              reprogrammed into the hardware. This function is also
304  *              responsible for checking the key length for validity.
305  * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
306  *               single block of data, which must be @cra_blocksize big. This
307  *               always operates on a full @cra_blocksize and it is not possible
308  *               to encrypt a block of smaller size. The supplied buffers must
309  *               therefore also be at least of @cra_blocksize size. Both the
310  *               input and output buffers are always aligned to @cra_alignmask.
311  *               In case either of the input or output buffer supplied by user
312  *               of the crypto API is not aligned to @cra_alignmask, the crypto
313  *               API will re-align the buffers. The re-alignment means that a
314  *               new buffer will be allocated, the data will be copied into the
315  *               new buffer, then the processing will happen on the new buffer,
316  *               then the data will be copied back into the original buffer and
317  *               finally the new buffer will be freed. In case a software
318  *               fallback was put in place in the @cra_init call, this function
319  *               might need to use the fallback if the algorithm doesn't support
320  *               all of the key sizes. In case the key was stored in
321  *               transformation context, the key might need to be re-programmed
322  *               into the hardware in this function. This function shall not
323  *               modify the transformation context, as this function may be
324  *               called in parallel with the same transformation object.
325  * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
326  *               @cia_encrypt, and the conditions are exactly the same.
327  *
328  * All fields are mandatory and must be filled.
329  */
330 struct cipher_alg {
331         unsigned int cia_min_keysize;
332         unsigned int cia_max_keysize;
333         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
334                           unsigned int keylen);
335         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
336         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
337 };
338
339 struct compress_alg {
340         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
341                             unsigned int slen, u8 *dst, unsigned int *dlen);
342         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
343                               unsigned int slen, u8 *dst, unsigned int *dlen);
344 };
345
346 #ifdef CONFIG_CRYPTO_STATS
347 /*
348  * struct crypto_istat_aead - statistics for AEAD algorithm
349  * @encrypt_cnt:        number of encrypt requests
350  * @encrypt_tlen:       total data size handled by encrypt requests
351  * @decrypt_cnt:        number of decrypt requests
352  * @decrypt_tlen:       total data size handled by decrypt requests
353  * @err_cnt:            number of error for AEAD requests
354  */
355 struct crypto_istat_aead {
356         atomic64_t encrypt_cnt;
357         atomic64_t encrypt_tlen;
358         atomic64_t decrypt_cnt;
359         atomic64_t decrypt_tlen;
360         atomic64_t err_cnt;
361 };
362
363 /*
364  * struct crypto_istat_akcipher - statistics for akcipher algorithm
365  * @encrypt_cnt:        number of encrypt requests
366  * @encrypt_tlen:       total data size handled by encrypt requests
367  * @decrypt_cnt:        number of decrypt requests
368  * @decrypt_tlen:       total data size handled by decrypt requests
369  * @verify_cnt:         number of verify operation
370  * @sign_cnt:           number of sign requests
371  * @err_cnt:            number of error for akcipher requests
372  */
373 struct crypto_istat_akcipher {
374         atomic64_t encrypt_cnt;
375         atomic64_t encrypt_tlen;
376         atomic64_t decrypt_cnt;
377         atomic64_t decrypt_tlen;
378         atomic64_t verify_cnt;
379         atomic64_t sign_cnt;
380         atomic64_t err_cnt;
381 };
382
383 /*
384  * struct crypto_istat_cipher - statistics for cipher algorithm
385  * @encrypt_cnt:        number of encrypt requests
386  * @encrypt_tlen:       total data size handled by encrypt requests
387  * @decrypt_cnt:        number of decrypt requests
388  * @decrypt_tlen:       total data size handled by decrypt requests
389  * @err_cnt:            number of error for cipher requests
390  */
391 struct crypto_istat_cipher {
392         atomic64_t encrypt_cnt;
393         atomic64_t encrypt_tlen;
394         atomic64_t decrypt_cnt;
395         atomic64_t decrypt_tlen;
396         atomic64_t err_cnt;
397 };
398
399 /*
400  * struct crypto_istat_compress - statistics for compress algorithm
401  * @compress_cnt:       number of compress requests
402  * @compress_tlen:      total data size handled by compress requests
403  * @decompress_cnt:     number of decompress requests
404  * @decompress_tlen:    total data size handled by decompress requests
405  * @err_cnt:            number of error for compress requests
406  */
407 struct crypto_istat_compress {
408         atomic64_t compress_cnt;
409         atomic64_t compress_tlen;
410         atomic64_t decompress_cnt;
411         atomic64_t decompress_tlen;
412         atomic64_t err_cnt;
413 };
414
415 /*
416  * struct crypto_istat_hash - statistics for has algorithm
417  * @hash_cnt:           number of hash requests
418  * @hash_tlen:          total data size hashed
419  * @err_cnt:            number of error for hash requests
420  */
421 struct crypto_istat_hash {
422         atomic64_t hash_cnt;
423         atomic64_t hash_tlen;
424         atomic64_t err_cnt;
425 };
426
427 /*
428  * struct crypto_istat_kpp - statistics for KPP algorithm
429  * @setsecret_cnt:              number of setsecrey operation
430  * @generate_public_key_cnt:    number of generate_public_key operation
431  * @compute_shared_secret_cnt:  number of compute_shared_secret operation
432  * @err_cnt:                    number of error for KPP requests
433  */
434 struct crypto_istat_kpp {
435         atomic64_t setsecret_cnt;
436         atomic64_t generate_public_key_cnt;
437         atomic64_t compute_shared_secret_cnt;
438         atomic64_t err_cnt;
439 };
440
441 /*
442  * struct crypto_istat_rng: statistics for RNG algorithm
443  * @generate_cnt:       number of RNG generate requests
444  * @generate_tlen:      total data size of generated data by the RNG
445  * @seed_cnt:           number of times the RNG was seeded
446  * @err_cnt:            number of error for RNG requests
447  */
448 struct crypto_istat_rng {
449         atomic64_t generate_cnt;
450         atomic64_t generate_tlen;
451         atomic64_t seed_cnt;
452         atomic64_t err_cnt;
453 };
454 #endif /* CONFIG_CRYPTO_STATS */
455
456 #define cra_ablkcipher  cra_u.ablkcipher
457 #define cra_blkcipher   cra_u.blkcipher
458 #define cra_cipher      cra_u.cipher
459 #define cra_compress    cra_u.compress
460
461 /**
462  * struct crypto_alg - definition of a cryptograpic cipher algorithm
463  * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
464  *             CRYPTO_ALG_* flags for the flags which go in here. Those are
465  *             used for fine-tuning the description of the transformation
466  *             algorithm.
467  * @cra_blocksize: Minimum block size of this transformation. The size in bytes
468  *                 of the smallest possible unit which can be transformed with
469  *                 this algorithm. The users must respect this value.
470  *                 In case of HASH transformation, it is possible for a smaller
471  *                 block than @cra_blocksize to be passed to the crypto API for
472  *                 transformation, in case of any other transformation type, an
473  *                 error will be returned upon any attempt to transform smaller
474  *                 than @cra_blocksize chunks.
475  * @cra_ctxsize: Size of the operational context of the transformation. This
476  *               value informs the kernel crypto API about the memory size
477  *               needed to be allocated for the transformation context.
478  * @cra_alignmask: Alignment mask for the input and output data buffer. The data
479  *                 buffer containing the input data for the algorithm must be
480  *                 aligned to this alignment mask. The data buffer for the
481  *                 output data must be aligned to this alignment mask. Note that
482  *                 the Crypto API will do the re-alignment in software, but
483  *                 only under special conditions and there is a performance hit.
484  *                 The re-alignment happens at these occasions for different
485  *                 @cra_u types: cipher -- For both input data and output data
486  *                 buffer; ahash -- For output hash destination buf; shash --
487  *                 For output hash destination buf.
488  *                 This is needed on hardware which is flawed by design and
489  *                 cannot pick data from arbitrary addresses.
490  * @cra_priority: Priority of this transformation implementation. In case
491  *                multiple transformations with same @cra_name are available to
492  *                the Crypto API, the kernel will use the one with highest
493  *                @cra_priority.
494  * @cra_name: Generic name (usable by multiple implementations) of the
495  *            transformation algorithm. This is the name of the transformation
496  *            itself. This field is used by the kernel when looking up the
497  *            providers of particular transformation.
498  * @cra_driver_name: Unique name of the transformation provider. This is the
499  *                   name of the provider of the transformation. This can be any
500  *                   arbitrary value, but in the usual case, this contains the
501  *                   name of the chip or provider and the name of the
502  *                   transformation algorithm.
503  * @cra_type: Type of the cryptographic transformation. This is a pointer to
504  *            struct crypto_type, which implements callbacks common for all
505  *            transformation types. There are multiple options:
506  *            &crypto_blkcipher_type, &crypto_ablkcipher_type,
507  *            &crypto_ahash_type, &crypto_rng_type.
508  *            This field might be empty. In that case, there are no common
509  *            callbacks. This is the case for: cipher, compress, shash.
510  * @cra_u: Callbacks implementing the transformation. This is a union of
511  *         multiple structures. Depending on the type of transformation selected
512  *         by @cra_type and @cra_flags above, the associated structure must be
513  *         filled with callbacks. This field might be empty. This is the case
514  *         for ahash, shash.
515  * @cra_init: Initialize the cryptographic transformation object. This function
516  *            is used to initialize the cryptographic transformation object.
517  *            This function is called only once at the instantiation time, right
518  *            after the transformation context was allocated. In case the
519  *            cryptographic hardware has some special requirements which need to
520  *            be handled by software, this function shall check for the precise
521  *            requirement of the transformation and put any software fallbacks
522  *            in place.
523  * @cra_exit: Deinitialize the cryptographic transformation object. This is a
524  *            counterpart to @cra_init, used to remove various changes set in
525  *            @cra_init.
526  * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher
527  *                    definition. See @struct @ablkcipher_alg.
528  * @cra_u.blkcipher: Union member which contains a synchronous block cipher
529  *                   definition See @struct @blkcipher_alg.
530  * @cra_u.cipher: Union member which contains a single-block symmetric cipher
531  *                definition. See @struct @cipher_alg.
532  * @cra_u.compress: Union member which contains a (de)compression algorithm.
533  *                  See @struct @compress_alg.
534  * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
535  * @cra_list: internally used
536  * @cra_users: internally used
537  * @cra_refcnt: internally used
538  * @cra_destroy: internally used
539  *
540  * @stats: union of all possible crypto_istat_xxx structures
541  * @stats.aead:         statistics for AEAD algorithm
542  * @stats.akcipher:     statistics for akcipher algorithm
543  * @stats.cipher:       statistics for cipher algorithm
544  * @stats.compress:     statistics for compress algorithm
545  * @stats.hash:         statistics for hash algorithm
546  * @stats.rng:          statistics for rng algorithm
547  * @stats.kpp:          statistics for KPP algorithm
548  *
549  * The struct crypto_alg describes a generic Crypto API algorithm and is common
550  * for all of the transformations. Any variable not documented here shall not
551  * be used by a cipher implementation as it is internal to the Crypto API.
552  */
553 struct crypto_alg {
554         struct list_head cra_list;
555         struct list_head cra_users;
556
557         u32 cra_flags;
558         unsigned int cra_blocksize;
559         unsigned int cra_ctxsize;
560         unsigned int cra_alignmask;
561
562         int cra_priority;
563         refcount_t cra_refcnt;
564
565         char cra_name[CRYPTO_MAX_ALG_NAME];
566         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
567
568         const struct crypto_type *cra_type;
569
570         union {
571                 struct ablkcipher_alg ablkcipher;
572                 struct blkcipher_alg blkcipher;
573                 struct cipher_alg cipher;
574                 struct compress_alg compress;
575         } cra_u;
576
577         int (*cra_init)(struct crypto_tfm *tfm);
578         void (*cra_exit)(struct crypto_tfm *tfm);
579         void (*cra_destroy)(struct crypto_alg *alg);
580         
581         struct module *cra_module;
582
583 #ifdef CONFIG_CRYPTO_STATS
584         union {
585                 struct crypto_istat_aead aead;
586                 struct crypto_istat_akcipher akcipher;
587                 struct crypto_istat_cipher cipher;
588                 struct crypto_istat_compress compress;
589                 struct crypto_istat_hash hash;
590                 struct crypto_istat_rng rng;
591                 struct crypto_istat_kpp kpp;
592         } stats;
593 #endif /* CONFIG_CRYPTO_STATS */
594
595 } CRYPTO_MINALIGN_ATTR;
596
597 #ifdef CONFIG_CRYPTO_STATS
598 void crypto_stats_init(struct crypto_alg *alg);
599 void crypto_stats_get(struct crypto_alg *alg);
600 void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
601 void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
602 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
603 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
604 void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
605 void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
606 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
607 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
608 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
609 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
610 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
611 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
612 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
613 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
614 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
615 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
616 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
617 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
618 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
619 #else
620 static inline void crypto_stats_init(struct crypto_alg *alg)
621 {}
622 static inline void crypto_stats_get(struct crypto_alg *alg)
623 {}
624 static inline void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
625 {}
626 static inline void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
627 {}
628 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
629 {}
630 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
631 {}
632 static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
633 {}
634 static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
635 {}
636 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
637 {}
638 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
639 {}
640 static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
641 {}
642 static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
643 {}
644 static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
645 {}
646 static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
647 {}
648 static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
649 {}
650 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
651 {}
652 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
653 {}
654 static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
655 {}
656 static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
657 {}
658 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
659 {}
660 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
661 {}
662 #endif
663 /*
664  * A helper struct for waiting for completion of async crypto ops
665  */
666 struct crypto_wait {
667         struct completion completion;
668         int err;
669 };
670
671 /*
672  * Macro for declaring a crypto op async wait object on stack
673  */
674 #define DECLARE_CRYPTO_WAIT(_wait) \
675         struct crypto_wait _wait = { \
676                 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
677
678 /*
679  * Async ops completion helper functioons
680  */
681 void crypto_req_done(struct crypto_async_request *req, int err);
682
683 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
684 {
685         switch (err) {
686         case -EINPROGRESS:
687         case -EBUSY:
688                 wait_for_completion(&wait->completion);
689                 reinit_completion(&wait->completion);
690                 err = wait->err;
691                 break;
692         };
693
694         return err;
695 }
696
697 static inline void crypto_init_wait(struct crypto_wait *wait)
698 {
699         init_completion(&wait->completion);
700 }
701
702 /*
703  * Algorithm registration interface.
704  */
705 int crypto_register_alg(struct crypto_alg *alg);
706 int crypto_unregister_alg(struct crypto_alg *alg);
707 int crypto_register_algs(struct crypto_alg *algs, int count);
708 int crypto_unregister_algs(struct crypto_alg *algs, int count);
709
710 /*
711  * Algorithm query interface.
712  */
713 int crypto_has_alg(const char *name, u32 type, u32 mask);
714
715 /*
716  * Transforms: user-instantiated objects which encapsulate algorithms
717  * and core processing logic.  Managed via crypto_alloc_*() and
718  * crypto_free_*(), as well as the various helpers below.
719  */
720
721 struct ablkcipher_tfm {
722         int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
723                       unsigned int keylen);
724         int (*encrypt)(struct ablkcipher_request *req);
725         int (*decrypt)(struct ablkcipher_request *req);
726
727         struct crypto_ablkcipher *base;
728
729         unsigned int ivsize;
730         unsigned int reqsize;
731 };
732
733 struct blkcipher_tfm {
734         void *iv;
735         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
736                       unsigned int keylen);
737         int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
738                        struct scatterlist *src, unsigned int nbytes);
739         int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
740                        struct scatterlist *src, unsigned int nbytes);
741 };
742
743 struct cipher_tfm {
744         int (*cit_setkey)(struct crypto_tfm *tfm,
745                           const u8 *key, unsigned int keylen);
746         void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
747         void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
748 };
749
750 struct compress_tfm {
751         int (*cot_compress)(struct crypto_tfm *tfm,
752                             const u8 *src, unsigned int slen,
753                             u8 *dst, unsigned int *dlen);
754         int (*cot_decompress)(struct crypto_tfm *tfm,
755                               const u8 *src, unsigned int slen,
756                               u8 *dst, unsigned int *dlen);
757 };
758
759 #define crt_ablkcipher  crt_u.ablkcipher
760 #define crt_blkcipher   crt_u.blkcipher
761 #define crt_cipher      crt_u.cipher
762 #define crt_compress    crt_u.compress
763
764 struct crypto_tfm {
765
766         u32 crt_flags;
767         
768         union {
769                 struct ablkcipher_tfm ablkcipher;
770                 struct blkcipher_tfm blkcipher;
771                 struct cipher_tfm cipher;
772                 struct compress_tfm compress;
773         } crt_u;
774
775         void (*exit)(struct crypto_tfm *tfm);
776         
777         struct crypto_alg *__crt_alg;
778
779         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
780 };
781
782 struct crypto_ablkcipher {
783         struct crypto_tfm base;
784 };
785
786 struct crypto_blkcipher {
787         struct crypto_tfm base;
788 };
789
790 struct crypto_cipher {
791         struct crypto_tfm base;
792 };
793
794 struct crypto_comp {
795         struct crypto_tfm base;
796 };
797
798 enum {
799         CRYPTOA_UNSPEC,
800         CRYPTOA_ALG,
801         CRYPTOA_TYPE,
802         CRYPTOA_U32,
803         __CRYPTOA_MAX,
804 };
805
806 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
807
808 /* Maximum number of (rtattr) parameters for each template. */
809 #define CRYPTO_MAX_ATTRS 32
810
811 struct crypto_attr_alg {
812         char name[CRYPTO_MAX_ALG_NAME];
813 };
814
815 struct crypto_attr_type {
816         u32 type;
817         u32 mask;
818 };
819
820 struct crypto_attr_u32 {
821         u32 num;
822 };
823
824 /* 
825  * Transform user interface.
826  */
827  
828 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
829 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
830
831 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
832 {
833         return crypto_destroy_tfm(tfm, tfm);
834 }
835
836 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
837
838 /*
839  * Transform helpers which query the underlying algorithm.
840  */
841 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
842 {
843         return tfm->__crt_alg->cra_name;
844 }
845
846 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
847 {
848         return tfm->__crt_alg->cra_driver_name;
849 }
850
851 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
852 {
853         return tfm->__crt_alg->cra_priority;
854 }
855
856 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
857 {
858         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
859 }
860
861 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
862 {
863         return tfm->__crt_alg->cra_blocksize;
864 }
865
866 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
867 {
868         return tfm->__crt_alg->cra_alignmask;
869 }
870
871 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
872 {
873         return tfm->crt_flags;
874 }
875
876 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
877 {
878         tfm->crt_flags |= flags;
879 }
880
881 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
882 {
883         tfm->crt_flags &= ~flags;
884 }
885
886 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
887 {
888         return tfm->__crt_ctx;
889 }
890
891 static inline unsigned int crypto_tfm_ctx_alignment(void)
892 {
893         struct crypto_tfm *tfm;
894         return __alignof__(tfm->__crt_ctx);
895 }
896
897 /*
898  * API wrappers.
899  */
900 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
901         struct crypto_tfm *tfm)
902 {
903         return (struct crypto_ablkcipher *)tfm;
904 }
905
906 static inline u32 crypto_skcipher_type(u32 type)
907 {
908         type &= ~CRYPTO_ALG_TYPE_MASK;
909         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
910         return type;
911 }
912
913 static inline u32 crypto_skcipher_mask(u32 mask)
914 {
915         mask &= ~CRYPTO_ALG_TYPE_MASK;
916         mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
917         return mask;
918 }
919
920 /**
921  * DOC: Asynchronous Block Cipher API
922  *
923  * Asynchronous block cipher API is used with the ciphers of type
924  * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
925  *
926  * Asynchronous cipher operations imply that the function invocation for a
927  * cipher request returns immediately before the completion of the operation.
928  * The cipher request is scheduled as a separate kernel thread and therefore
929  * load-balanced on the different CPUs via the process scheduler. To allow
930  * the kernel crypto API to inform the caller about the completion of a cipher
931  * request, the caller must provide a callback function. That function is
932  * invoked with the cipher handle when the request completes.
933  *
934  * To support the asynchronous operation, additional information than just the
935  * cipher handle must be supplied to the kernel crypto API. That additional
936  * information is given by filling in the ablkcipher_request data structure.
937  *
938  * For the asynchronous block cipher API, the state is maintained with the tfm
939  * cipher handle. A single tfm can be used across multiple calls and in
940  * parallel. For asynchronous block cipher calls, context data supplied and
941  * only used by the caller can be referenced the request data structure in
942  * addition to the IV used for the cipher request. The maintenance of such
943  * state information would be important for a crypto driver implementer to
944  * have, because when calling the callback function upon completion of the
945  * cipher operation, that callback function may need some information about
946  * which operation just finished if it invoked multiple in parallel. This
947  * state information is unused by the kernel crypto API.
948  */
949
950 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
951         struct crypto_ablkcipher *tfm)
952 {
953         return &tfm->base;
954 }
955
956 /**
957  * crypto_free_ablkcipher() - zeroize and free cipher handle
958  * @tfm: cipher handle to be freed
959  */
960 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
961 {
962         crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
963 }
964
965 /**
966  * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
967  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
968  *            ablkcipher
969  * @type: specifies the type of the cipher
970  * @mask: specifies the mask for the cipher
971  *
972  * Return: true when the ablkcipher is known to the kernel crypto API; false
973  *         otherwise
974  */
975 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
976                                         u32 mask)
977 {
978         return crypto_has_alg(alg_name, crypto_skcipher_type(type),
979                               crypto_skcipher_mask(mask));
980 }
981
982 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
983         struct crypto_ablkcipher *tfm)
984 {
985         return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
986 }
987
988 /**
989  * crypto_ablkcipher_ivsize() - obtain IV size
990  * @tfm: cipher handle
991  *
992  * The size of the IV for the ablkcipher referenced by the cipher handle is
993  * returned. This IV size may be zero if the cipher does not need an IV.
994  *
995  * Return: IV size in bytes
996  */
997 static inline unsigned int crypto_ablkcipher_ivsize(
998         struct crypto_ablkcipher *tfm)
999 {
1000         return crypto_ablkcipher_crt(tfm)->ivsize;
1001 }
1002
1003 /**
1004  * crypto_ablkcipher_blocksize() - obtain block size of cipher
1005  * @tfm: cipher handle
1006  *
1007  * The block size for the ablkcipher referenced with the cipher handle is
1008  * returned. The caller may use that information to allocate appropriate
1009  * memory for the data returned by the encryption or decryption operation
1010  *
1011  * Return: block size of cipher
1012  */
1013 static inline unsigned int crypto_ablkcipher_blocksize(
1014         struct crypto_ablkcipher *tfm)
1015 {
1016         return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
1017 }
1018
1019 static inline unsigned int crypto_ablkcipher_alignmask(
1020         struct crypto_ablkcipher *tfm)
1021 {
1022         return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
1023 }
1024
1025 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
1026 {
1027         return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
1028 }
1029
1030 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
1031                                                u32 flags)
1032 {
1033         crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
1034 }
1035
1036 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
1037                                                  u32 flags)
1038 {
1039         crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
1040 }
1041
1042 /**
1043  * crypto_ablkcipher_setkey() - set key for cipher
1044  * @tfm: cipher handle
1045  * @key: buffer holding the key
1046  * @keylen: length of the key in bytes
1047  *
1048  * The caller provided key is set for the ablkcipher referenced by the cipher
1049  * handle.
1050  *
1051  * Note, the key length determines the cipher type. Many block ciphers implement
1052  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1053  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1054  * is performed.
1055  *
1056  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1057  */
1058 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
1059                                            const u8 *key, unsigned int keylen)
1060 {
1061         struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
1062
1063         return crt->setkey(crt->base, key, keylen);
1064 }
1065
1066 /**
1067  * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
1068  * @req: ablkcipher_request out of which the cipher handle is to be obtained
1069  *
1070  * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1071  * data structure.
1072  *
1073  * Return: crypto_ablkcipher handle
1074  */
1075 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1076         struct ablkcipher_request *req)
1077 {
1078         return __crypto_ablkcipher_cast(req->base.tfm);
1079 }
1080
1081 /**
1082  * crypto_ablkcipher_encrypt() - encrypt plaintext
1083  * @req: reference to the ablkcipher_request handle that holds all information
1084  *       needed to perform the cipher operation
1085  *
1086  * Encrypt plaintext data using the ablkcipher_request handle. That data
1087  * structure and how it is filled with data is discussed with the
1088  * ablkcipher_request_* functions.
1089  *
1090  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1091  */
1092 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1093 {
1094         struct ablkcipher_tfm *crt =
1095                 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1096         struct crypto_alg *alg = crt->base->base.__crt_alg;
1097         unsigned int nbytes = req->nbytes;
1098         int ret;
1099
1100         crypto_stats_get(alg);
1101         ret = crt->encrypt(req);
1102         crypto_stats_ablkcipher_encrypt(nbytes, ret, alg);
1103         return ret;
1104 }
1105
1106 /**
1107  * crypto_ablkcipher_decrypt() - decrypt ciphertext
1108  * @req: reference to the ablkcipher_request handle that holds all information
1109  *       needed to perform the cipher operation
1110  *
1111  * Decrypt ciphertext data using the ablkcipher_request handle. That data
1112  * structure and how it is filled with data is discussed with the
1113  * ablkcipher_request_* functions.
1114  *
1115  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1116  */
1117 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1118 {
1119         struct ablkcipher_tfm *crt =
1120                 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1121         struct crypto_alg *alg = crt->base->base.__crt_alg;
1122         unsigned int nbytes = req->nbytes;
1123         int ret;
1124
1125         crypto_stats_get(alg);
1126         ret = crt->decrypt(req);
1127         crypto_stats_ablkcipher_decrypt(nbytes, ret, alg);
1128         return ret;
1129 }
1130
1131 /**
1132  * DOC: Asynchronous Cipher Request Handle
1133  *
1134  * The ablkcipher_request data structure contains all pointers to data
1135  * required for the asynchronous cipher operation. This includes the cipher
1136  * handle (which can be used by multiple ablkcipher_request instances), pointer
1137  * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1138  * as a handle to the ablkcipher_request_* API calls in a similar way as
1139  * ablkcipher handle to the crypto_ablkcipher_* API calls.
1140  */
1141
1142 /**
1143  * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1144  * @tfm: cipher handle
1145  *
1146  * Return: number of bytes
1147  */
1148 static inline unsigned int crypto_ablkcipher_reqsize(
1149         struct crypto_ablkcipher *tfm)
1150 {
1151         return crypto_ablkcipher_crt(tfm)->reqsize;
1152 }
1153
1154 /**
1155  * ablkcipher_request_set_tfm() - update cipher handle reference in request
1156  * @req: request handle to be modified
1157  * @tfm: cipher handle that shall be added to the request handle
1158  *
1159  * Allow the caller to replace the existing ablkcipher handle in the request
1160  * data structure with a different one.
1161  */
1162 static inline void ablkcipher_request_set_tfm(
1163         struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1164 {
1165         req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
1166 }
1167
1168 static inline struct ablkcipher_request *ablkcipher_request_cast(
1169         struct crypto_async_request *req)
1170 {
1171         return container_of(req, struct ablkcipher_request, base);
1172 }
1173
1174 /**
1175  * ablkcipher_request_alloc() - allocate request data structure
1176  * @tfm: cipher handle to be registered with the request
1177  * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1178  *
1179  * Allocate the request data structure that must be used with the ablkcipher
1180  * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1181  * handle is registered in the request data structure.
1182  *
1183  * Return: allocated request handle in case of success, or NULL if out of memory
1184  */
1185 static inline struct ablkcipher_request *ablkcipher_request_alloc(
1186         struct crypto_ablkcipher *tfm, gfp_t gfp)
1187 {
1188         struct ablkcipher_request *req;
1189
1190         req = kmalloc(sizeof(struct ablkcipher_request) +
1191                       crypto_ablkcipher_reqsize(tfm), gfp);
1192
1193         if (likely(req))
1194                 ablkcipher_request_set_tfm(req, tfm);
1195
1196         return req;
1197 }
1198
1199 /**
1200  * ablkcipher_request_free() - zeroize and free request data structure
1201  * @req: request data structure cipher handle to be freed
1202  */
1203 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1204 {
1205         kzfree(req);
1206 }
1207
1208 /**
1209  * ablkcipher_request_set_callback() - set asynchronous callback function
1210  * @req: request handle
1211  * @flags: specify zero or an ORing of the flags
1212  *         CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1213  *         increase the wait queue beyond the initial maximum size;
1214  *         CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1215  * @compl: callback function pointer to be registered with the request handle
1216  * @data: The data pointer refers to memory that is not used by the kernel
1217  *        crypto API, but provided to the callback function for it to use. Here,
1218  *        the caller can provide a reference to memory the callback function can
1219  *        operate on. As the callback function is invoked asynchronously to the
1220  *        related functionality, it may need to access data structures of the
1221  *        related functionality which can be referenced using this pointer. The
1222  *        callback function can access the memory via the "data" field in the
1223  *        crypto_async_request data structure provided to the callback function.
1224  *
1225  * This function allows setting the callback function that is triggered once the
1226  * cipher operation completes.
1227  *
1228  * The callback function is registered with the ablkcipher_request handle and
1229  * must comply with the following template::
1230  *
1231  *      void callback_function(struct crypto_async_request *req, int error)
1232  */
1233 static inline void ablkcipher_request_set_callback(
1234         struct ablkcipher_request *req,
1235         u32 flags, crypto_completion_t compl, void *data)
1236 {
1237         req->base.complete = compl;
1238         req->base.data = data;
1239         req->base.flags = flags;
1240 }
1241
1242 /**
1243  * ablkcipher_request_set_crypt() - set data buffers
1244  * @req: request handle
1245  * @src: source scatter / gather list
1246  * @dst: destination scatter / gather list
1247  * @nbytes: number of bytes to process from @src
1248  * @iv: IV for the cipher operation which must comply with the IV size defined
1249  *      by crypto_ablkcipher_ivsize
1250  *
1251  * This function allows setting of the source data and destination data
1252  * scatter / gather lists.
1253  *
1254  * For encryption, the source is treated as the plaintext and the
1255  * destination is the ciphertext. For a decryption operation, the use is
1256  * reversed - the source is the ciphertext and the destination is the plaintext.
1257  */
1258 static inline void ablkcipher_request_set_crypt(
1259         struct ablkcipher_request *req,
1260         struct scatterlist *src, struct scatterlist *dst,
1261         unsigned int nbytes, void *iv)
1262 {
1263         req->src = src;
1264         req->dst = dst;
1265         req->nbytes = nbytes;
1266         req->info = iv;
1267 }
1268
1269 /**
1270  * DOC: Synchronous Block Cipher API
1271  *
1272  * The synchronous block cipher API is used with the ciphers of type
1273  * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1274  *
1275  * Synchronous calls, have a context in the tfm. But since a single tfm can be
1276  * used in multiple calls and in parallel, this info should not be changeable
1277  * (unless a lock is used). This applies, for example, to the symmetric key.
1278  * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1279  * structure for synchronous blkcipher api. So, its the only state info that can
1280  * be kept for synchronous calls without using a big lock across a tfm.
1281  *
1282  * The block cipher API allows the use of a complete cipher, i.e. a cipher
1283  * consisting of a template (a block chaining mode) and a single block cipher
1284  * primitive (e.g. AES).
1285  *
1286  * The plaintext data buffer and the ciphertext data buffer are pointed to
1287  * by using scatter/gather lists. The cipher operation is performed
1288  * on all segments of the provided scatter/gather lists.
1289  *
1290  * The kernel crypto API supports a cipher operation "in-place" which means that
1291  * the caller may provide the same scatter/gather list for the plaintext and
1292  * cipher text. After the completion of the cipher operation, the plaintext
1293  * data is replaced with the ciphertext data in case of an encryption and vice
1294  * versa for a decryption. The caller must ensure that the scatter/gather lists
1295  * for the output data point to sufficiently large buffers, i.e. multiples of
1296  * the block size of the cipher.
1297  */
1298
1299 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1300         struct crypto_tfm *tfm)
1301 {
1302         return (struct crypto_blkcipher *)tfm;
1303 }
1304
1305 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1306         struct crypto_tfm *tfm)
1307 {
1308         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1309         return __crypto_blkcipher_cast(tfm);
1310 }
1311
1312 /**
1313  * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1314  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1315  *            blkcipher cipher
1316  * @type: specifies the type of the cipher
1317  * @mask: specifies the mask for the cipher
1318  *
1319  * Allocate a cipher handle for a block cipher. The returned struct
1320  * crypto_blkcipher is the cipher handle that is required for any subsequent
1321  * API invocation for that block cipher.
1322  *
1323  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1324  *         of an error, PTR_ERR() returns the error code.
1325  */
1326 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1327         const char *alg_name, u32 type, u32 mask)
1328 {
1329         type &= ~CRYPTO_ALG_TYPE_MASK;
1330         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1331         mask |= CRYPTO_ALG_TYPE_MASK;
1332
1333         return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1334 }
1335
1336 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1337         struct crypto_blkcipher *tfm)
1338 {
1339         return &tfm->base;
1340 }
1341
1342 /**
1343  * crypto_free_blkcipher() - zeroize and free the block cipher handle
1344  * @tfm: cipher handle to be freed
1345  */
1346 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1347 {
1348         crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1349 }
1350
1351 /**
1352  * crypto_has_blkcipher() - Search for the availability of a block cipher
1353  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1354  *            block cipher
1355  * @type: specifies the type of the cipher
1356  * @mask: specifies the mask for the cipher
1357  *
1358  * Return: true when the block cipher is known to the kernel crypto API; false
1359  *         otherwise
1360  */
1361 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1362 {
1363         type &= ~CRYPTO_ALG_TYPE_MASK;
1364         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1365         mask |= CRYPTO_ALG_TYPE_MASK;
1366
1367         return crypto_has_alg(alg_name, type, mask);
1368 }
1369
1370 /**
1371  * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1372  * @tfm: cipher handle
1373  *
1374  * Return: The character string holding the name of the cipher
1375  */
1376 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1377 {
1378         return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1379 }
1380
1381 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1382         struct crypto_blkcipher *tfm)
1383 {
1384         return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1385 }
1386
1387 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1388         struct crypto_blkcipher *tfm)
1389 {
1390         return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1391 }
1392
1393 /**
1394  * crypto_blkcipher_ivsize() - obtain IV size
1395  * @tfm: cipher handle
1396  *
1397  * The size of the IV for the block cipher referenced by the cipher handle is
1398  * returned. This IV size may be zero if the cipher does not need an IV.
1399  *
1400  * Return: IV size in bytes
1401  */
1402 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1403 {
1404         return crypto_blkcipher_alg(tfm)->ivsize;
1405 }
1406
1407 /**
1408  * crypto_blkcipher_blocksize() - obtain block size of cipher
1409  * @tfm: cipher handle
1410  *
1411  * The block size for the block cipher referenced with the cipher handle is
1412  * returned. The caller may use that information to allocate appropriate
1413  * memory for the data returned by the encryption or decryption operation.
1414  *
1415  * Return: block size of cipher
1416  */
1417 static inline unsigned int crypto_blkcipher_blocksize(
1418         struct crypto_blkcipher *tfm)
1419 {
1420         return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1421 }
1422
1423 static inline unsigned int crypto_blkcipher_alignmask(
1424         struct crypto_blkcipher *tfm)
1425 {
1426         return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1427 }
1428
1429 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1430 {
1431         return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1432 }
1433
1434 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1435                                               u32 flags)
1436 {
1437         crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1438 }
1439
1440 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1441                                                 u32 flags)
1442 {
1443         crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1444 }
1445
1446 /**
1447  * crypto_blkcipher_setkey() - set key for cipher
1448  * @tfm: cipher handle
1449  * @key: buffer holding the key
1450  * @keylen: length of the key in bytes
1451  *
1452  * The caller provided key is set for the block cipher referenced by the cipher
1453  * handle.
1454  *
1455  * Note, the key length determines the cipher type. Many block ciphers implement
1456  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1457  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1458  * is performed.
1459  *
1460  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1461  */
1462 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1463                                           const u8 *key, unsigned int keylen)
1464 {
1465         return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1466                                                  key, keylen);
1467 }
1468
1469 /**
1470  * crypto_blkcipher_encrypt() - encrypt plaintext
1471  * @desc: reference to the block cipher handle with meta data
1472  * @dst: scatter/gather list that is filled by the cipher operation with the
1473  *      ciphertext
1474  * @src: scatter/gather list that holds the plaintext
1475  * @nbytes: number of bytes of the plaintext to encrypt.
1476  *
1477  * Encrypt plaintext data using the IV set by the caller with a preceding
1478  * call of crypto_blkcipher_set_iv.
1479  *
1480  * The blkcipher_desc data structure must be filled by the caller and can
1481  * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1482  * with the block cipher handle; desc.flags is filled with either
1483  * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1484  *
1485  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1486  */
1487 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1488                                            struct scatterlist *dst,
1489                                            struct scatterlist *src,
1490                                            unsigned int nbytes)
1491 {
1492         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1493         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1494 }
1495
1496 /**
1497  * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1498  * @desc: reference to the block cipher handle with meta data
1499  * @dst: scatter/gather list that is filled by the cipher operation with the
1500  *      ciphertext
1501  * @src: scatter/gather list that holds the plaintext
1502  * @nbytes: number of bytes of the plaintext to encrypt.
1503  *
1504  * Encrypt plaintext data with the use of an IV that is solely used for this
1505  * cipher operation. Any previously set IV is not used.
1506  *
1507  * The blkcipher_desc data structure must be filled by the caller and can
1508  * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1509  * with the block cipher handle; desc.info is filled with the IV to be used for
1510  * the current operation; desc.flags is filled with either
1511  * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1512  *
1513  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1514  */
1515 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1516                                               struct scatterlist *dst,
1517                                               struct scatterlist *src,
1518                                               unsigned int nbytes)
1519 {
1520         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1521 }
1522
1523 /**
1524  * crypto_blkcipher_decrypt() - decrypt ciphertext
1525  * @desc: reference to the block cipher handle with meta data
1526  * @dst: scatter/gather list that is filled by the cipher operation with the
1527  *      plaintext
1528  * @src: scatter/gather list that holds the ciphertext
1529  * @nbytes: number of bytes of the ciphertext to decrypt.
1530  *
1531  * Decrypt ciphertext data using the IV set by the caller with a preceding
1532  * call of crypto_blkcipher_set_iv.
1533  *
1534  * The blkcipher_desc data structure must be filled by the caller as documented
1535  * for the crypto_blkcipher_encrypt call above.
1536  *
1537  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1538  *
1539  */
1540 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1541                                            struct scatterlist *dst,
1542                                            struct scatterlist *src,
1543                                            unsigned int nbytes)
1544 {
1545         desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1546         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1547 }
1548
1549 /**
1550  * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1551  * @desc: reference to the block cipher handle with meta data
1552  * @dst: scatter/gather list that is filled by the cipher operation with the
1553  *      plaintext
1554  * @src: scatter/gather list that holds the ciphertext
1555  * @nbytes: number of bytes of the ciphertext to decrypt.
1556  *
1557  * Decrypt ciphertext data with the use of an IV that is solely used for this
1558  * cipher operation. Any previously set IV is not used.
1559  *
1560  * The blkcipher_desc data structure must be filled by the caller as documented
1561  * for the crypto_blkcipher_encrypt_iv call above.
1562  *
1563  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1564  */
1565 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1566                                               struct scatterlist *dst,
1567                                               struct scatterlist *src,
1568                                               unsigned int nbytes)
1569 {
1570         return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1571 }
1572
1573 /**
1574  * crypto_blkcipher_set_iv() - set IV for cipher
1575  * @tfm: cipher handle
1576  * @src: buffer holding the IV
1577  * @len: length of the IV in bytes
1578  *
1579  * The caller provided IV is set for the block cipher referenced by the cipher
1580  * handle.
1581  */
1582 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1583                                            const u8 *src, unsigned int len)
1584 {
1585         memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1586 }
1587
1588 /**
1589  * crypto_blkcipher_get_iv() - obtain IV from cipher
1590  * @tfm: cipher handle
1591  * @dst: buffer filled with the IV
1592  * @len: length of the buffer dst
1593  *
1594  * The caller can obtain the IV set for the block cipher referenced by the
1595  * cipher handle and store it into the user-provided buffer. If the buffer
1596  * has an insufficient space, the IV is truncated to fit the buffer.
1597  */
1598 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1599                                            u8 *dst, unsigned int len)
1600 {
1601         memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1602 }
1603
1604 /**
1605  * DOC: Single Block Cipher API
1606  *
1607  * The single block cipher API is used with the ciphers of type
1608  * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1609  *
1610  * Using the single block cipher API calls, operations with the basic cipher
1611  * primitive can be implemented. These cipher primitives exclude any block
1612  * chaining operations including IV handling.
1613  *
1614  * The purpose of this single block cipher API is to support the implementation
1615  * of templates or other concepts that only need to perform the cipher operation
1616  * on one block at a time. Templates invoke the underlying cipher primitive
1617  * block-wise and process either the input or the output data of these cipher
1618  * operations.
1619  */
1620
1621 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1622 {
1623         return (struct crypto_cipher *)tfm;
1624 }
1625
1626 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1627 {
1628         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1629         return __crypto_cipher_cast(tfm);
1630 }
1631
1632 /**
1633  * crypto_alloc_cipher() - allocate single block cipher handle
1634  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1635  *           single block cipher
1636  * @type: specifies the type of the cipher
1637  * @mask: specifies the mask for the cipher
1638  *
1639  * Allocate a cipher handle for a single block cipher. The returned struct
1640  * crypto_cipher is the cipher handle that is required for any subsequent API
1641  * invocation for that single block cipher.
1642  *
1643  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1644  *         of an error, PTR_ERR() returns the error code.
1645  */
1646 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1647                                                         u32 type, u32 mask)
1648 {
1649         type &= ~CRYPTO_ALG_TYPE_MASK;
1650         type |= CRYPTO_ALG_TYPE_CIPHER;
1651         mask |= CRYPTO_ALG_TYPE_MASK;
1652
1653         return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1654 }
1655
1656 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1657 {
1658         return &tfm->base;
1659 }
1660
1661 /**
1662  * crypto_free_cipher() - zeroize and free the single block cipher handle
1663  * @tfm: cipher handle to be freed
1664  */
1665 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1666 {
1667         crypto_free_tfm(crypto_cipher_tfm(tfm));
1668 }
1669
1670 /**
1671  * crypto_has_cipher() - Search for the availability of a single block cipher
1672  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1673  *           single block cipher
1674  * @type: specifies the type of the cipher
1675  * @mask: specifies the mask for the cipher
1676  *
1677  * Return: true when the single block cipher is known to the kernel crypto API;
1678  *         false otherwise
1679  */
1680 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1681 {
1682         type &= ~CRYPTO_ALG_TYPE_MASK;
1683         type |= CRYPTO_ALG_TYPE_CIPHER;
1684         mask |= CRYPTO_ALG_TYPE_MASK;
1685
1686         return crypto_has_alg(alg_name, type, mask);
1687 }
1688
1689 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1690 {
1691         return &crypto_cipher_tfm(tfm)->crt_cipher;
1692 }
1693
1694 /**
1695  * crypto_cipher_blocksize() - obtain block size for cipher
1696  * @tfm: cipher handle
1697  *
1698  * The block size for the single block cipher referenced with the cipher handle
1699  * tfm is returned. The caller may use that information to allocate appropriate
1700  * memory for the data returned by the encryption or decryption operation
1701  *
1702  * Return: block size of cipher
1703  */
1704 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1705 {
1706         return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1707 }
1708
1709 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1710 {
1711         return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1712 }
1713
1714 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1715 {
1716         return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1717 }
1718
1719 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1720                                            u32 flags)
1721 {
1722         crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1723 }
1724
1725 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1726                                              u32 flags)
1727 {
1728         crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1729 }
1730
1731 /**
1732  * crypto_cipher_setkey() - set key for cipher
1733  * @tfm: cipher handle
1734  * @key: buffer holding the key
1735  * @keylen: length of the key in bytes
1736  *
1737  * The caller provided key is set for the single block cipher referenced by the
1738  * cipher handle.
1739  *
1740  * Note, the key length determines the cipher type. Many block ciphers implement
1741  * different cipher modes depending on the key size, such as AES-128 vs AES-192
1742  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1743  * is performed.
1744  *
1745  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1746  */
1747 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1748                                        const u8 *key, unsigned int keylen)
1749 {
1750         return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1751                                                   key, keylen);
1752 }
1753
1754 /**
1755  * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1756  * @tfm: cipher handle
1757  * @dst: points to the buffer that will be filled with the ciphertext
1758  * @src: buffer holding the plaintext to be encrypted
1759  *
1760  * Invoke the encryption operation of one block. The caller must ensure that
1761  * the plaintext and ciphertext buffers are at least one block in size.
1762  */
1763 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1764                                              u8 *dst, const u8 *src)
1765 {
1766         crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1767                                                 dst, src);
1768 }
1769
1770 /**
1771  * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1772  * @tfm: cipher handle
1773  * @dst: points to the buffer that will be filled with the plaintext
1774  * @src: buffer holding the ciphertext to be decrypted
1775  *
1776  * Invoke the decryption operation of one block. The caller must ensure that
1777  * the plaintext and ciphertext buffers are at least one block in size.
1778  */
1779 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1780                                              u8 *dst, const u8 *src)
1781 {
1782         crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1783                                                 dst, src);
1784 }
1785
1786 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1787 {
1788         return (struct crypto_comp *)tfm;
1789 }
1790
1791 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1792 {
1793         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1794                CRYPTO_ALG_TYPE_MASK);
1795         return __crypto_comp_cast(tfm);
1796 }
1797
1798 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1799                                                     u32 type, u32 mask)
1800 {
1801         type &= ~CRYPTO_ALG_TYPE_MASK;
1802         type |= CRYPTO_ALG_TYPE_COMPRESS;
1803         mask |= CRYPTO_ALG_TYPE_MASK;
1804
1805         return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1806 }
1807
1808 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1809 {
1810         return &tfm->base;
1811 }
1812
1813 static inline void crypto_free_comp(struct crypto_comp *tfm)
1814 {
1815         crypto_free_tfm(crypto_comp_tfm(tfm));
1816 }
1817
1818 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1819 {
1820         type &= ~CRYPTO_ALG_TYPE_MASK;
1821         type |= CRYPTO_ALG_TYPE_COMPRESS;
1822         mask |= CRYPTO_ALG_TYPE_MASK;
1823
1824         return crypto_has_alg(alg_name, type, mask);
1825 }
1826
1827 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1828 {
1829         return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1830 }
1831
1832 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1833 {
1834         return &crypto_comp_tfm(tfm)->crt_compress;
1835 }
1836
1837 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1838                                        const u8 *src, unsigned int slen,
1839                                        u8 *dst, unsigned int *dlen)
1840 {
1841         return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1842                                                   src, slen, dst, dlen);
1843 }
1844
1845 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1846                                          const u8 *src, unsigned int slen,
1847                                          u8 *dst, unsigned int *dlen)
1848 {
1849         return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1850                                                     src, slen, dst, dlen);
1851 }
1852
1853 #endif  /* _LINUX_CRYPTO_H */
1854