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