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