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