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