Merge drm-intel-next-queued into gvt-next
[sfrench/cifs-2.6.git] / include / linux / crypto.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Scatterlist Cryptographic API.
4  *
5  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
7  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8  *
9  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
10  * and Nettle, by Niels Möller.
11  */
12 #ifndef _LINUX_CRYPTO_H
13 #define _LINUX_CRYPTO_H
14
15 #include <linux/atomic.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/bug.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/uaccess.h>
22 #include <linux/completion.h>
23
24 /*
25  * Autoloaded crypto modules should only use a prefixed name to avoid allowing
26  * arbitrary modules to be loaded. Loading from userspace may still need the
27  * unprefixed names, so retains those aliases as well.
28  * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
29  * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
30  * expands twice on the same line. Instead, use a separate base name for the
31  * alias.
32  */
33 #define MODULE_ALIAS_CRYPTO(name)       \
34                 __MODULE_INFO(alias, alias_userspace, name);    \
35                 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
36
37 /*
38  * Algorithm masks and types.
39  */
40 #define CRYPTO_ALG_TYPE_MASK            0x0000000f
41 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
42 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000002
43 #define CRYPTO_ALG_TYPE_AEAD            0x00000003
44 #define CRYPTO_ALG_TYPE_SKCIPHER        0x00000005
45 #define CRYPTO_ALG_TYPE_KPP             0x00000008
46 #define CRYPTO_ALG_TYPE_ACOMPRESS       0x0000000a
47 #define CRYPTO_ALG_TYPE_SCOMPRESS       0x0000000b
48 #define CRYPTO_ALG_TYPE_RNG             0x0000000c
49 #define CRYPTO_ALG_TYPE_AKCIPHER        0x0000000d
50 #define CRYPTO_ALG_TYPE_HASH            0x0000000e
51 #define CRYPTO_ALG_TYPE_SHASH           0x0000000e
52 #define CRYPTO_ALG_TYPE_AHASH           0x0000000f
53
54 #define CRYPTO_ALG_TYPE_HASH_MASK       0x0000000e
55 #define CRYPTO_ALG_TYPE_AHASH_MASK      0x0000000e
56 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK  0x0000000e
57
58 #define CRYPTO_ALG_LARVAL               0x00000010
59 #define CRYPTO_ALG_DEAD                 0x00000020
60 #define CRYPTO_ALG_DYING                0x00000040
61 #define CRYPTO_ALG_ASYNC                0x00000080
62
63 /*
64  * Set this bit if and only if the algorithm requires another algorithm of
65  * the same type to handle corner cases.
66  */
67 #define CRYPTO_ALG_NEED_FALLBACK        0x00000100
68
69 /*
70  * Set if the algorithm has passed automated run-time testing.  Note that
71  * if there is no run-time testing for a given algorithm it is considered
72  * to have passed.
73  */
74
75 #define CRYPTO_ALG_TESTED               0x00000400
76
77 /*
78  * Set if the algorithm is an instance that is built from templates.
79  */
80 #define CRYPTO_ALG_INSTANCE             0x00000800
81
82 /* Set this bit if the algorithm provided is hardware accelerated but
83  * not available to userspace via instruction set or so.
84  */
85 #define CRYPTO_ALG_KERN_DRIVER_ONLY     0x00001000
86
87 /*
88  * Mark a cipher as a service implementation only usable by another
89  * cipher and never by a normal user of the kernel crypto API
90  */
91 #define CRYPTO_ALG_INTERNAL             0x00002000
92
93 /*
94  * Set if the algorithm has a ->setkey() method but can be used without
95  * calling it first, i.e. there is a default key.
96  */
97 #define CRYPTO_ALG_OPTIONAL_KEY         0x00004000
98
99 /*
100  * Don't trigger module loading
101  */
102 #define CRYPTO_NOLOAD                   0x00008000
103
104 /*
105  * Transform masks and values (for crt_flags).
106  */
107 #define CRYPTO_TFM_NEED_KEY             0x00000001
108
109 #define CRYPTO_TFM_REQ_MASK             0x000fff00
110 #define CRYPTO_TFM_RES_MASK             0xfff00000
111
112 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 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             128
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_async_request;
140 struct crypto_tfm;
141 struct crypto_type;
142
143 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
144
145 /**
146  * DOC: Block Cipher Context Data Structures
147  *
148  * These data structures define the operating context for each block cipher
149  * type.
150  */
151
152 struct crypto_async_request {
153         struct list_head list;
154         crypto_completion_t complete;
155         void *data;
156         struct crypto_tfm *tfm;
157
158         u32 flags;
159 };
160
161 /**
162  * DOC: Block Cipher Algorithm Definitions
163  *
164  * These data structures define modular crypto algorithm implementations,
165  * managed via crypto_register_alg() and crypto_unregister_alg().
166  */
167
168 /**
169  * struct cipher_alg - single-block symmetric ciphers definition
170  * @cia_min_keysize: Minimum key size supported by the transformation. This is
171  *                   the smallest key length supported by this transformation
172  *                   algorithm. This must be set to one of the pre-defined
173  *                   values as this is not hardware specific. Possible values
174  *                   for this field can be found via git grep "_MIN_KEY_SIZE"
175  *                   include/crypto/
176  * @cia_max_keysize: Maximum key size supported by the transformation. This is
177  *                  the largest key length supported by this transformation
178  *                  algorithm. This must be set to one of the pre-defined values
179  *                  as this is not hardware specific. Possible values for this
180  *                  field can be found via git grep "_MAX_KEY_SIZE"
181  *                  include/crypto/
182  * @cia_setkey: Set key for the transformation. This function is used to either
183  *              program a supplied key into the hardware or store the key in the
184  *              transformation context for programming it later. Note that this
185  *              function does modify the transformation context. This function
186  *              can be called multiple times during the existence of the
187  *              transformation object, so one must make sure the key is properly
188  *              reprogrammed into the hardware. This function is also
189  *              responsible for checking the key length for validity.
190  * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
191  *               single block of data, which must be @cra_blocksize big. This
192  *               always operates on a full @cra_blocksize and it is not possible
193  *               to encrypt a block of smaller size. The supplied buffers must
194  *               therefore also be at least of @cra_blocksize size. Both the
195  *               input and output buffers are always aligned to @cra_alignmask.
196  *               In case either of the input or output buffer supplied by user
197  *               of the crypto API is not aligned to @cra_alignmask, the crypto
198  *               API will re-align the buffers. The re-alignment means that a
199  *               new buffer will be allocated, the data will be copied into the
200  *               new buffer, then the processing will happen on the new buffer,
201  *               then the data will be copied back into the original buffer and
202  *               finally the new buffer will be freed. In case a software
203  *               fallback was put in place in the @cra_init call, this function
204  *               might need to use the fallback if the algorithm doesn't support
205  *               all of the key sizes. In case the key was stored in
206  *               transformation context, the key might need to be re-programmed
207  *               into the hardware in this function. This function shall not
208  *               modify the transformation context, as this function may be
209  *               called in parallel with the same transformation object.
210  * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
211  *               @cia_encrypt, and the conditions are exactly the same.
212  *
213  * All fields are mandatory and must be filled.
214  */
215 struct cipher_alg {
216         unsigned int cia_min_keysize;
217         unsigned int cia_max_keysize;
218         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
219                           unsigned int keylen);
220         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
221         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
222 };
223
224 /**
225  * struct compress_alg - compression/decompression algorithm
226  * @coa_compress: Compress a buffer of specified length, storing the resulting
227  *                data in the specified buffer. Return the length of the
228  *                compressed data in dlen.
229  * @coa_decompress: Decompress the source buffer, storing the uncompressed
230  *                  data in the specified buffer. The length of the data is
231  *                  returned in dlen.
232  *
233  * All fields are mandatory.
234  */
235 struct compress_alg {
236         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
237                             unsigned int slen, u8 *dst, unsigned int *dlen);
238         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
239                               unsigned int slen, u8 *dst, unsigned int *dlen);
240 };
241
242 #ifdef CONFIG_CRYPTO_STATS
243 /*
244  * struct crypto_istat_aead - statistics for AEAD algorithm
245  * @encrypt_cnt:        number of encrypt requests
246  * @encrypt_tlen:       total data size handled by encrypt requests
247  * @decrypt_cnt:        number of decrypt requests
248  * @decrypt_tlen:       total data size handled by decrypt requests
249  * @err_cnt:            number of error for AEAD requests
250  */
251 struct crypto_istat_aead {
252         atomic64_t encrypt_cnt;
253         atomic64_t encrypt_tlen;
254         atomic64_t decrypt_cnt;
255         atomic64_t decrypt_tlen;
256         atomic64_t err_cnt;
257 };
258
259 /*
260  * struct crypto_istat_akcipher - statistics for akcipher algorithm
261  * @encrypt_cnt:        number of encrypt requests
262  * @encrypt_tlen:       total data size handled by encrypt requests
263  * @decrypt_cnt:        number of decrypt requests
264  * @decrypt_tlen:       total data size handled by decrypt requests
265  * @verify_cnt:         number of verify operation
266  * @sign_cnt:           number of sign requests
267  * @err_cnt:            number of error for akcipher requests
268  */
269 struct crypto_istat_akcipher {
270         atomic64_t encrypt_cnt;
271         atomic64_t encrypt_tlen;
272         atomic64_t decrypt_cnt;
273         atomic64_t decrypt_tlen;
274         atomic64_t verify_cnt;
275         atomic64_t sign_cnt;
276         atomic64_t err_cnt;
277 };
278
279 /*
280  * struct crypto_istat_cipher - statistics for cipher algorithm
281  * @encrypt_cnt:        number of encrypt requests
282  * @encrypt_tlen:       total data size handled by encrypt requests
283  * @decrypt_cnt:        number of decrypt requests
284  * @decrypt_tlen:       total data size handled by decrypt requests
285  * @err_cnt:            number of error for cipher requests
286  */
287 struct crypto_istat_cipher {
288         atomic64_t encrypt_cnt;
289         atomic64_t encrypt_tlen;
290         atomic64_t decrypt_cnt;
291         atomic64_t decrypt_tlen;
292         atomic64_t err_cnt;
293 };
294
295 /*
296  * struct crypto_istat_compress - statistics for compress algorithm
297  * @compress_cnt:       number of compress requests
298  * @compress_tlen:      total data size handled by compress requests
299  * @decompress_cnt:     number of decompress requests
300  * @decompress_tlen:    total data size handled by decompress requests
301  * @err_cnt:            number of error for compress requests
302  */
303 struct crypto_istat_compress {
304         atomic64_t compress_cnt;
305         atomic64_t compress_tlen;
306         atomic64_t decompress_cnt;
307         atomic64_t decompress_tlen;
308         atomic64_t err_cnt;
309 };
310
311 /*
312  * struct crypto_istat_hash - statistics for has algorithm
313  * @hash_cnt:           number of hash requests
314  * @hash_tlen:          total data size hashed
315  * @err_cnt:            number of error for hash requests
316  */
317 struct crypto_istat_hash {
318         atomic64_t hash_cnt;
319         atomic64_t hash_tlen;
320         atomic64_t err_cnt;
321 };
322
323 /*
324  * struct crypto_istat_kpp - statistics for KPP algorithm
325  * @setsecret_cnt:              number of setsecrey operation
326  * @generate_public_key_cnt:    number of generate_public_key operation
327  * @compute_shared_secret_cnt:  number of compute_shared_secret operation
328  * @err_cnt:                    number of error for KPP requests
329  */
330 struct crypto_istat_kpp {
331         atomic64_t setsecret_cnt;
332         atomic64_t generate_public_key_cnt;
333         atomic64_t compute_shared_secret_cnt;
334         atomic64_t err_cnt;
335 };
336
337 /*
338  * struct crypto_istat_rng: statistics for RNG algorithm
339  * @generate_cnt:       number of RNG generate requests
340  * @generate_tlen:      total data size of generated data by the RNG
341  * @seed_cnt:           number of times the RNG was seeded
342  * @err_cnt:            number of error for RNG requests
343  */
344 struct crypto_istat_rng {
345         atomic64_t generate_cnt;
346         atomic64_t generate_tlen;
347         atomic64_t seed_cnt;
348         atomic64_t err_cnt;
349 };
350 #endif /* CONFIG_CRYPTO_STATS */
351
352 #define cra_cipher      cra_u.cipher
353 #define cra_compress    cra_u.compress
354
355 /**
356  * struct crypto_alg - definition of a cryptograpic cipher algorithm
357  * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
358  *             CRYPTO_ALG_* flags for the flags which go in here. Those are
359  *             used for fine-tuning the description of the transformation
360  *             algorithm.
361  * @cra_blocksize: Minimum block size of this transformation. The size in bytes
362  *                 of the smallest possible unit which can be transformed with
363  *                 this algorithm. The users must respect this value.
364  *                 In case of HASH transformation, it is possible for a smaller
365  *                 block than @cra_blocksize to be passed to the crypto API for
366  *                 transformation, in case of any other transformation type, an
367  *                 error will be returned upon any attempt to transform smaller
368  *                 than @cra_blocksize chunks.
369  * @cra_ctxsize: Size of the operational context of the transformation. This
370  *               value informs the kernel crypto API about the memory size
371  *               needed to be allocated for the transformation context.
372  * @cra_alignmask: Alignment mask for the input and output data buffer. The data
373  *                 buffer containing the input data for the algorithm must be
374  *                 aligned to this alignment mask. The data buffer for the
375  *                 output data must be aligned to this alignment mask. Note that
376  *                 the Crypto API will do the re-alignment in software, but
377  *                 only under special conditions and there is a performance hit.
378  *                 The re-alignment happens at these occasions for different
379  *                 @cra_u types: cipher -- For both input data and output data
380  *                 buffer; ahash -- For output hash destination buf; shash --
381  *                 For output hash destination buf.
382  *                 This is needed on hardware which is flawed by design and
383  *                 cannot pick data from arbitrary addresses.
384  * @cra_priority: Priority of this transformation implementation. In case
385  *                multiple transformations with same @cra_name are available to
386  *                the Crypto API, the kernel will use the one with highest
387  *                @cra_priority.
388  * @cra_name: Generic name (usable by multiple implementations) of the
389  *            transformation algorithm. This is the name of the transformation
390  *            itself. This field is used by the kernel when looking up the
391  *            providers of particular transformation.
392  * @cra_driver_name: Unique name of the transformation provider. This is the
393  *                   name of the provider of the transformation. This can be any
394  *                   arbitrary value, but in the usual case, this contains the
395  *                   name of the chip or provider and the name of the
396  *                   transformation algorithm.
397  * @cra_type: Type of the cryptographic transformation. This is a pointer to
398  *            struct crypto_type, which implements callbacks common for all
399  *            transformation types. There are multiple options, such as
400  *            &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type.
401  *            This field might be empty. In that case, there are no common
402  *            callbacks. This is the case for: cipher, compress, shash.
403  * @cra_u: Callbacks implementing the transformation. This is a union of
404  *         multiple structures. Depending on the type of transformation selected
405  *         by @cra_type and @cra_flags above, the associated structure must be
406  *         filled with callbacks. This field might be empty. This is the case
407  *         for ahash, shash.
408  * @cra_init: Initialize the cryptographic transformation object. This function
409  *            is used to initialize the cryptographic transformation object.
410  *            This function is called only once at the instantiation time, right
411  *            after the transformation context was allocated. In case the
412  *            cryptographic hardware has some special requirements which need to
413  *            be handled by software, this function shall check for the precise
414  *            requirement of the transformation and put any software fallbacks
415  *            in place.
416  * @cra_exit: Deinitialize the cryptographic transformation object. This is a
417  *            counterpart to @cra_init, used to remove various changes set in
418  *            @cra_init.
419  * @cra_u.cipher: Union member which contains a single-block symmetric cipher
420  *                definition. See @struct @cipher_alg.
421  * @cra_u.compress: Union member which contains a (de)compression algorithm.
422  *                  See @struct @compress_alg.
423  * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
424  * @cra_list: internally used
425  * @cra_users: internally used
426  * @cra_refcnt: internally used
427  * @cra_destroy: internally used
428  *
429  * @stats: union of all possible crypto_istat_xxx structures
430  * @stats.aead:         statistics for AEAD algorithm
431  * @stats.akcipher:     statistics for akcipher algorithm
432  * @stats.cipher:       statistics for cipher algorithm
433  * @stats.compress:     statistics for compress algorithm
434  * @stats.hash:         statistics for hash algorithm
435  * @stats.rng:          statistics for rng algorithm
436  * @stats.kpp:          statistics for KPP algorithm
437  *
438  * The struct crypto_alg describes a generic Crypto API algorithm and is common
439  * for all of the transformations. Any variable not documented here shall not
440  * be used by a cipher implementation as it is internal to the Crypto API.
441  */
442 struct crypto_alg {
443         struct list_head cra_list;
444         struct list_head cra_users;
445
446         u32 cra_flags;
447         unsigned int cra_blocksize;
448         unsigned int cra_ctxsize;
449         unsigned int cra_alignmask;
450
451         int cra_priority;
452         refcount_t cra_refcnt;
453
454         char cra_name[CRYPTO_MAX_ALG_NAME];
455         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
456
457         const struct crypto_type *cra_type;
458
459         union {
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
470 #ifdef CONFIG_CRYPTO_STATS
471         union {
472                 struct crypto_istat_aead aead;
473                 struct crypto_istat_akcipher akcipher;
474                 struct crypto_istat_cipher cipher;
475                 struct crypto_istat_compress compress;
476                 struct crypto_istat_hash hash;
477                 struct crypto_istat_rng rng;
478                 struct crypto_istat_kpp kpp;
479         } stats;
480 #endif /* CONFIG_CRYPTO_STATS */
481
482 } CRYPTO_MINALIGN_ATTR;
483
484 #ifdef CONFIG_CRYPTO_STATS
485 void crypto_stats_init(struct crypto_alg *alg);
486 void crypto_stats_get(struct crypto_alg *alg);
487 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
488 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
489 void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
490 void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
491 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
492 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
493 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
494 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
495 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
496 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
497 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
498 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
499 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
500 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
501 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
502 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
503 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
504 #else
505 static inline void crypto_stats_init(struct crypto_alg *alg)
506 {}
507 static inline void crypto_stats_get(struct crypto_alg *alg)
508 {}
509 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
510 {}
511 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
512 {}
513 static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
514 {}
515 static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
516 {}
517 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
518 {}
519 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
520 {}
521 static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
522 {}
523 static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
524 {}
525 static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
526 {}
527 static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
528 {}
529 static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
530 {}
531 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
532 {}
533 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
534 {}
535 static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
536 {}
537 static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
538 {}
539 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
540 {}
541 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
542 {}
543 #endif
544 /*
545  * A helper struct for waiting for completion of async crypto ops
546  */
547 struct crypto_wait {
548         struct completion completion;
549         int err;
550 };
551
552 /*
553  * Macro for declaring a crypto op async wait object on stack
554  */
555 #define DECLARE_CRYPTO_WAIT(_wait) \
556         struct crypto_wait _wait = { \
557                 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
558
559 /*
560  * Async ops completion helper functioons
561  */
562 void crypto_req_done(struct crypto_async_request *req, int err);
563
564 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
565 {
566         switch (err) {
567         case -EINPROGRESS:
568         case -EBUSY:
569                 wait_for_completion(&wait->completion);
570                 reinit_completion(&wait->completion);
571                 err = wait->err;
572                 break;
573         };
574
575         return err;
576 }
577
578 static inline void crypto_init_wait(struct crypto_wait *wait)
579 {
580         init_completion(&wait->completion);
581 }
582
583 /*
584  * Algorithm registration interface.
585  */
586 int crypto_register_alg(struct crypto_alg *alg);
587 int crypto_unregister_alg(struct crypto_alg *alg);
588 int crypto_register_algs(struct crypto_alg *algs, int count);
589 int crypto_unregister_algs(struct crypto_alg *algs, int count);
590
591 /*
592  * Algorithm query interface.
593  */
594 int crypto_has_alg(const char *name, u32 type, u32 mask);
595
596 /*
597  * Transforms: user-instantiated objects which encapsulate algorithms
598  * and core processing logic.  Managed via crypto_alloc_*() and
599  * crypto_free_*(), as well as the various helpers below.
600  */
601
602 struct cipher_tfm {
603         int (*cit_setkey)(struct crypto_tfm *tfm,
604                           const u8 *key, unsigned int keylen);
605         void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
606         void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
607 };
608
609 struct compress_tfm {
610         int (*cot_compress)(struct crypto_tfm *tfm,
611                             const u8 *src, unsigned int slen,
612                             u8 *dst, unsigned int *dlen);
613         int (*cot_decompress)(struct crypto_tfm *tfm,
614                               const u8 *src, unsigned int slen,
615                               u8 *dst, unsigned int *dlen);
616 };
617
618 #define crt_cipher      crt_u.cipher
619 #define crt_compress    crt_u.compress
620
621 struct crypto_tfm {
622
623         u32 crt_flags;
624         
625         union {
626                 struct cipher_tfm cipher;
627                 struct compress_tfm compress;
628         } crt_u;
629
630         void (*exit)(struct crypto_tfm *tfm);
631         
632         struct crypto_alg *__crt_alg;
633
634         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
635 };
636
637 struct crypto_cipher {
638         struct crypto_tfm base;
639 };
640
641 struct crypto_comp {
642         struct crypto_tfm base;
643 };
644
645 enum {
646         CRYPTOA_UNSPEC,
647         CRYPTOA_ALG,
648         CRYPTOA_TYPE,
649         CRYPTOA_U32,
650         __CRYPTOA_MAX,
651 };
652
653 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
654
655 /* Maximum number of (rtattr) parameters for each template. */
656 #define CRYPTO_MAX_ATTRS 32
657
658 struct crypto_attr_alg {
659         char name[CRYPTO_MAX_ALG_NAME];
660 };
661
662 struct crypto_attr_type {
663         u32 type;
664         u32 mask;
665 };
666
667 struct crypto_attr_u32 {
668         u32 num;
669 };
670
671 /* 
672  * Transform user interface.
673  */
674  
675 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
676 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
677
678 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
679 {
680         return crypto_destroy_tfm(tfm, tfm);
681 }
682
683 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
684
685 /*
686  * Transform helpers which query the underlying algorithm.
687  */
688 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
689 {
690         return tfm->__crt_alg->cra_name;
691 }
692
693 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
694 {
695         return tfm->__crt_alg->cra_driver_name;
696 }
697
698 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
699 {
700         return tfm->__crt_alg->cra_priority;
701 }
702
703 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
704 {
705         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
706 }
707
708 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
709 {
710         return tfm->__crt_alg->cra_blocksize;
711 }
712
713 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
714 {
715         return tfm->__crt_alg->cra_alignmask;
716 }
717
718 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
719 {
720         return tfm->crt_flags;
721 }
722
723 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
724 {
725         tfm->crt_flags |= flags;
726 }
727
728 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
729 {
730         tfm->crt_flags &= ~flags;
731 }
732
733 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
734 {
735         return tfm->__crt_ctx;
736 }
737
738 static inline unsigned int crypto_tfm_ctx_alignment(void)
739 {
740         struct crypto_tfm *tfm;
741         return __alignof__(tfm->__crt_ctx);
742 }
743
744 /**
745  * DOC: Single Block Cipher API
746  *
747  * The single block cipher API is used with the ciphers of type
748  * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
749  *
750  * Using the single block cipher API calls, operations with the basic cipher
751  * primitive can be implemented. These cipher primitives exclude any block
752  * chaining operations including IV handling.
753  *
754  * The purpose of this single block cipher API is to support the implementation
755  * of templates or other concepts that only need to perform the cipher operation
756  * on one block at a time. Templates invoke the underlying cipher primitive
757  * block-wise and process either the input or the output data of these cipher
758  * operations.
759  */
760
761 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
762 {
763         return (struct crypto_cipher *)tfm;
764 }
765
766 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
767 {
768         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
769         return __crypto_cipher_cast(tfm);
770 }
771
772 /**
773  * crypto_alloc_cipher() - allocate single block cipher handle
774  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
775  *           single block cipher
776  * @type: specifies the type of the cipher
777  * @mask: specifies the mask for the cipher
778  *
779  * Allocate a cipher handle for a single block cipher. The returned struct
780  * crypto_cipher is the cipher handle that is required for any subsequent API
781  * invocation for that single block cipher.
782  *
783  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
784  *         of an error, PTR_ERR() returns the error code.
785  */
786 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
787                                                         u32 type, u32 mask)
788 {
789         type &= ~CRYPTO_ALG_TYPE_MASK;
790         type |= CRYPTO_ALG_TYPE_CIPHER;
791         mask |= CRYPTO_ALG_TYPE_MASK;
792
793         return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
794 }
795
796 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
797 {
798         return &tfm->base;
799 }
800
801 /**
802  * crypto_free_cipher() - zeroize and free the single block cipher handle
803  * @tfm: cipher handle to be freed
804  */
805 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
806 {
807         crypto_free_tfm(crypto_cipher_tfm(tfm));
808 }
809
810 /**
811  * crypto_has_cipher() - Search for the availability of a single block cipher
812  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
813  *           single block cipher
814  * @type: specifies the type of the cipher
815  * @mask: specifies the mask for the cipher
816  *
817  * Return: true when the single block cipher is known to the kernel crypto API;
818  *         false otherwise
819  */
820 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
821 {
822         type &= ~CRYPTO_ALG_TYPE_MASK;
823         type |= CRYPTO_ALG_TYPE_CIPHER;
824         mask |= CRYPTO_ALG_TYPE_MASK;
825
826         return crypto_has_alg(alg_name, type, mask);
827 }
828
829 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
830 {
831         return &crypto_cipher_tfm(tfm)->crt_cipher;
832 }
833
834 /**
835  * crypto_cipher_blocksize() - obtain block size for cipher
836  * @tfm: cipher handle
837  *
838  * The block size for the single block cipher referenced with the cipher handle
839  * tfm is returned. The caller may use that information to allocate appropriate
840  * memory for the data returned by the encryption or decryption operation
841  *
842  * Return: block size of cipher
843  */
844 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
845 {
846         return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
847 }
848
849 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
850 {
851         return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
852 }
853
854 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
855 {
856         return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
857 }
858
859 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
860                                            u32 flags)
861 {
862         crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
863 }
864
865 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
866                                              u32 flags)
867 {
868         crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
869 }
870
871 /**
872  * crypto_cipher_setkey() - set key for cipher
873  * @tfm: cipher handle
874  * @key: buffer holding the key
875  * @keylen: length of the key in bytes
876  *
877  * The caller provided key is set for the single block cipher referenced by the
878  * cipher handle.
879  *
880  * Note, the key length determines the cipher type. Many block ciphers implement
881  * different cipher modes depending on the key size, such as AES-128 vs AES-192
882  * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
883  * is performed.
884  *
885  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
886  */
887 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
888                                        const u8 *key, unsigned int keylen)
889 {
890         return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
891                                                   key, keylen);
892 }
893
894 /**
895  * crypto_cipher_encrypt_one() - encrypt one block of plaintext
896  * @tfm: cipher handle
897  * @dst: points to the buffer that will be filled with the ciphertext
898  * @src: buffer holding the plaintext to be encrypted
899  *
900  * Invoke the encryption operation of one block. The caller must ensure that
901  * the plaintext and ciphertext buffers are at least one block in size.
902  */
903 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
904                                              u8 *dst, const u8 *src)
905 {
906         crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
907                                                 dst, src);
908 }
909
910 /**
911  * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
912  * @tfm: cipher handle
913  * @dst: points to the buffer that will be filled with the plaintext
914  * @src: buffer holding the ciphertext to be decrypted
915  *
916  * Invoke the decryption operation of one block. The caller must ensure that
917  * the plaintext and ciphertext buffers are at least one block in size.
918  */
919 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
920                                              u8 *dst, const u8 *src)
921 {
922         crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
923                                                 dst, src);
924 }
925
926 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
927 {
928         return (struct crypto_comp *)tfm;
929 }
930
931 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
932 {
933         BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
934                CRYPTO_ALG_TYPE_MASK);
935         return __crypto_comp_cast(tfm);
936 }
937
938 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
939                                                     u32 type, u32 mask)
940 {
941         type &= ~CRYPTO_ALG_TYPE_MASK;
942         type |= CRYPTO_ALG_TYPE_COMPRESS;
943         mask |= CRYPTO_ALG_TYPE_MASK;
944
945         return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
946 }
947
948 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
949 {
950         return &tfm->base;
951 }
952
953 static inline void crypto_free_comp(struct crypto_comp *tfm)
954 {
955         crypto_free_tfm(crypto_comp_tfm(tfm));
956 }
957
958 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
959 {
960         type &= ~CRYPTO_ALG_TYPE_MASK;
961         type |= CRYPTO_ALG_TYPE_COMPRESS;
962         mask |= CRYPTO_ALG_TYPE_MASK;
963
964         return crypto_has_alg(alg_name, type, mask);
965 }
966
967 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
968 {
969         return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
970 }
971
972 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
973 {
974         return &crypto_comp_tfm(tfm)->crt_compress;
975 }
976
977 static inline int crypto_comp_compress(struct crypto_comp *tfm,
978                                        const u8 *src, unsigned int slen,
979                                        u8 *dst, unsigned int *dlen)
980 {
981         return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
982                                                   src, slen, dst, dlen);
983 }
984
985 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
986                                          const u8 *src, unsigned int slen,
987                                          u8 *dst, unsigned int *dlen)
988 {
989         return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
990                                                     src, slen, dst, dlen);
991 }
992
993 #endif  /* _LINUX_CRYPTO_H */
994