OSDN Git Service

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