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