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