blob: 752360e1e8fe48c90e69c853426fc48ea5233afc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Algorithm masks and types.
30 */
Herbert Xu28259822006-08-06 21:23:26 +100031#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080033#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100035#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080036#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080037#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Loc Ho004a4032008-05-14 20:41:47 +080038#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
Herbert Xu5f7082e2008-08-31 22:21:09 +100039#define CRYPTO_ALG_TYPE_HASH 0x00000008
40#define CRYPTO_ALG_TYPE_SHASH 0x00000009
Loc Ho004a4032008-05-14 20:41:47 +080041#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Neil Horman17f0f4a2008-08-14 22:15:52 +100042#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Geert Uytterhoevena1d2f092009-03-04 15:05:33 +080043#define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100044
45#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080046#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080047#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Herbert Xu28259822006-08-06 21:23:26 +100049#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100050#define CRYPTO_ALG_DEAD 0x00000020
51#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100052#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
Herbert Xu60104392006-08-26 18:34:10 +100055 * 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
60/*
Herbert Xuecfc4322007-12-05 21:08:36 +110061 * 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
66/*
Herbert Xu73d38642008-08-03 21:15:23 +080067 * 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
74/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020075 * Set if the algorithm is an instance that is build from templates.
76 */
77#define CRYPTO_ALG_INSTANCE 0x00000800
78
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010079/* 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
Steffen Klassert64a947b2011-09-27 07:21:26 +020084/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Transform masks and values (for crt_flags).
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define CRYPTO_TFM_REQ_MASK 0x000fff00
88#define CRYPTO_TFM_RES_MASK 0xfff00000
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070091#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110092#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define CRYPTO_MAX_ALG_NAME 64
103
Herbert Xu79911102006-08-21 21:03:52 +1000104/*
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 */
Herbert Xu79911102006-08-21 21:03:52 +1000112#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000113
Herbert Xu79911102006-08-21 21:03:52 +1000114#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100117struct crypto_ablkcipher;
118struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800119struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000120struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000121struct crypto_hash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000122struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700123struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000124struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800125struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800126struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700127
Herbert Xu32e39832007-03-24 14:35:34 +1100128typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
129
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100130/**
131 * DOC: Block Cipher Context Data Structures
132 *
133 * These data structures define the operating context for each block cipher
134 * type.
135 */
136
Herbert Xu32e39832007-03-24 14:35:34 +1100137struct 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
Herbert Xu1ae97822007-08-30 15:36:14 +0800159/**
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
Herbert Xu5cde0af2006-08-22 00:07:53 +1000185struct blkcipher_desc {
186 struct crypto_blkcipher *tfm;
187 void *info;
188 u32 flags;
189};
190
Herbert Xu40725182005-07-06 13:51:52 -0700191struct cipher_desc {
192 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000193 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700194 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
195 const u8 *src, unsigned int nbytes);
196 void *info;
197};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Herbert Xu055bcee2006-08-19 22:24:23 +1000199struct hash_desc {
200 struct crypto_hash *tfm;
201 u32 flags;
202};
203
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100204/**
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000262struct 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);
Herbert Xu61da88e2007-12-17 21:51:27 +0800267 int (*givencrypt)(struct skcipher_givcrypt_request *req);
268 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000269
Herbert Xu23508e12007-11-27 21:33:24 +0800270 const char *geniv;
271
Herbert Xub5b7f082007-04-16 20:48:54 +1000272 unsigned int min_keysize;
273 unsigned int max_keysize;
274 unsigned int ivsize;
275};
276
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100277/**
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 */
Herbert Xu1ae97822007-08-30 15:36:14 +0800303struct aead_alg {
304 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
305 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100306 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800307 int (*encrypt)(struct aead_request *req);
308 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800309 int (*givencrypt)(struct aead_givcrypt_request *req);
310 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800311
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800312 const char *geniv;
313
Herbert Xu1ae97822007-08-30 15:36:14 +0800314 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100315 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800316};
317
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100318/**
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 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000330struct 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
Herbert Xu23508e12007-11-27 21:33:24 +0800340 const char *geniv;
341
Herbert Xu5cde0af2006-08-22 00:07:53 +1000342 unsigned int min_keysize;
343 unsigned int max_keysize;
344 unsigned int ivsize;
345};
346
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100347/**
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394struct cipher_alg {
395 unsigned int cia_min_keysize;
396 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000397 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000398 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000399 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000404 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408};
409
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100410/**
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 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000429struct 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
Herbert Xub5b7f082007-04-16 20:48:54 +1000438#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800439#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000440#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000443#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100445/**
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520struct crypto_alg {
521 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000522 struct list_head cra_users;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 u32 cra_flags;
525 unsigned int cra_blocksize;
526 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700527 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100528
529 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000530 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100531
Herbert Xud913ea02006-05-21 08:45:26 +1000532 char cra_name[CRYPTO_MAX_ALG_NAME];
533 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Herbert Xue853c3c2006-08-22 00:06:54 +1000535 const struct crypto_type *cra_type;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000538 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800539 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000540 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000543 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000545
546 int (*cra_init)(struct crypto_tfm *tfm);
547 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000548 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
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);
Mark Brown4b004342012-01-17 23:34:26 +0000558int crypto_register_algs(struct crypto_alg *algs, int count);
559int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/*
562 * Algorithm query interface.
563 */
Herbert Xufce32d72006-08-26 17:35:45 +1000564int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/*
567 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000568 * and core processing logic. Managed via crypto_alloc_*() and
569 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Herbert Xu32e39832007-03-24 14:35:34 +1100572struct 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);
Herbert Xu61da88e2007-12-17 21:51:27 +0800577 int (*givencrypt)(struct skcipher_givcrypt_request *req);
578 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
579
Herbert Xuecfc4322007-12-05 21:08:36 +1100580 struct crypto_ablkcipher *base;
581
Herbert Xu32e39832007-03-24 14:35:34 +1100582 unsigned int ivsize;
583 unsigned int reqsize;
584};
585
Herbert Xu1ae97822007-08-30 15:36:14 +0800586struct 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);
Herbert Xu743edf52007-12-10 16:18:01 +0800591 int (*givencrypt)(struct aead_givcrypt_request *req);
592 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800593
594 struct crypto_aead *base;
595
Herbert Xu1ae97822007-08-30 15:36:14 +0800596 unsigned int ivsize;
597 unsigned int authsize;
598 unsigned int reqsize;
599};
600
Herbert Xu5cde0af2006-08-22 00:07:53 +1000601struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int (*cit_setkey)(struct crypto_tfm *tfm,
613 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000614 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616};
617
Herbert Xu055bcee2006-08-19 22:24:23 +1000618struct 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);
Herbert Xu055bcee2006-08-19 22:24:23 +1000627 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628};
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
Neil Horman17f0f4a2008-08-14 22:15:52 +1000639struct 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
Herbert Xu32e39832007-03-24 14:35:34 +1100645#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800646#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000647#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000649#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000651#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653struct crypto_tfm {
654
655 u32 crt_flags;
656
657 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100658 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800659 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000660 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000662 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000664 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700666
667 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100670
Herbert Xu79911102006-08-21 21:03:52 +1000671 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
Herbert Xu32e39832007-03-24 14:35:34 +1100674struct crypto_ablkcipher {
675 struct crypto_tfm base;
676};
677
Herbert Xu1ae97822007-08-30 15:36:14 +0800678struct crypto_aead {
679 struct crypto_tfm base;
680};
681
Herbert Xu5cde0af2006-08-22 00:07:53 +1000682struct crypto_blkcipher {
683 struct crypto_tfm base;
684};
685
Herbert Xu78a1fe42006-12-24 10:02:00 +1100686struct crypto_cipher {
687 struct crypto_tfm base;
688};
689
690struct crypto_comp {
691 struct crypto_tfm base;
692};
693
Herbert Xu055bcee2006-08-19 22:24:23 +1000694struct crypto_hash {
695 struct crypto_tfm base;
696};
697
Neil Horman17f0f4a2008-08-14 22:15:52 +1000698struct crypto_rng {
699 struct crypto_tfm base;
700};
701
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000702enum {
703 CRYPTOA_UNSPEC,
704 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100705 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800706 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100707 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000708};
709
Herbert Xuebc610e2007-01-01 18:37:02 +1100710#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
711
Herbert Xu39e1ee012007-08-29 19:27:26 +0800712/* Maximum number of (rtattr) parameters for each template. */
713#define CRYPTO_MAX_ATTRS 32
714
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000715struct crypto_attr_alg {
716 char name[CRYPTO_MAX_ALG_NAME];
717};
718
Herbert Xuebc610e2007-01-01 18:37:02 +1100719struct crypto_attr_type {
720 u32 type;
721 u32 mask;
722};
723
Herbert Xu39e1ee012007-08-29 19:27:26 +0800724struct crypto_attr_u32 {
725 u32 num;
726};
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
729 * Transform user interface.
730 */
731
Herbert Xu6d7d6842006-07-30 11:53:01 +1000732struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100733void 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Herbert Xuda7f0332008-07-31 17:08:25 +0800740int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/*
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
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000750static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
761{
762 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
766{
767 return tfm->__crt_alg->cra_blocksize;
768}
769
Herbert Xufbdae9f2005-07-06 13:53:29 -0700770static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
771{
772 return tfm->__crt_alg->cra_alignmask;
773}
774
Herbert Xuf28776a2006-08-13 20:58:18 +1000775static 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
Herbert Xu40725182005-07-06 13:51:52 -0700790static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
791{
Herbert Xuf10b7892006-01-25 22:34:01 +1100792 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);
Herbert Xu40725182005-07-06 13:51:52 -0700799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * API wrappers.
803 */
Herbert Xu32e39832007-03-24 14:35:34 +1100804static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
805 struct crypto_tfm *tfm)
806{
807 return (struct crypto_ablkcipher *)tfm;
808}
809
Herbert Xu378f4f52007-12-17 20:07:31 +0800810static inline u32 crypto_skcipher_type(u32 type)
811{
Herbert Xuecfc4322007-12-05 21:08:36 +1100812 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800813 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
814 return type;
815}
816
817static inline u32 crypto_skcipher_mask(u32 mask)
818{
Herbert Xuecfc4322007-12-05 21:08:36 +1100819 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800820 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
821 return mask;
822}
823
Herbert Xub9c55aa2007-12-04 12:46:48 +1100824struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
825 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100826
827static inline struct crypto_tfm *crypto_ablkcipher_tfm(
828 struct crypto_ablkcipher *tfm)
829{
830 return &tfm->base;
831}
832
833static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
834{
835 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
836}
837
838static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
839 u32 mask)
840{
Herbert Xu378f4f52007-12-17 20:07:31 +0800841 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
842 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100843}
844
845static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
846 struct crypto_ablkcipher *tfm)
847{
848 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
849}
850
851static inline unsigned int crypto_ablkcipher_ivsize(
852 struct crypto_ablkcipher *tfm)
853{
854 return crypto_ablkcipher_crt(tfm)->ivsize;
855}
856
857static inline unsigned int crypto_ablkcipher_blocksize(
858 struct crypto_ablkcipher *tfm)
859{
860 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
861}
862
863static inline unsigned int crypto_ablkcipher_alignmask(
864 struct crypto_ablkcipher *tfm)
865{
866 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
867}
868
869static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
870{
871 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
872}
873
874static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
875 u32 flags)
876{
877 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
878}
879
880static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
881 u32 flags)
882{
883 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
884}
885
886static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
887 const u8 *key, unsigned int keylen)
888{
Herbert Xuecfc4322007-12-05 21:08:36 +1100889 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
890
891 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100892}
893
894static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
895 struct ablkcipher_request *req)
896{
897 return __crypto_ablkcipher_cast(req->base.tfm);
898}
899
900static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
901{
902 struct ablkcipher_tfm *crt =
903 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
904 return crt->encrypt(req);
905}
906
907static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
908{
909 struct ablkcipher_tfm *crt =
910 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
911 return crt->decrypt(req);
912}
913
Herbert Xub16c3a22007-08-29 19:02:04 +0800914static inline unsigned int crypto_ablkcipher_reqsize(
915 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100916{
917 return crypto_ablkcipher_crt(tfm)->reqsize;
918}
919
Herbert Xue196d622007-04-14 16:09:14 +1000920static inline void ablkcipher_request_set_tfm(
921 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
922{
Herbert Xuecfc4322007-12-05 21:08:36 +1100923 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000924}
925
Herbert Xub5b7f082007-04-16 20:48:54 +1000926static inline struct ablkcipher_request *ablkcipher_request_cast(
927 struct crypto_async_request *req)
928{
929 return container_of(req, struct ablkcipher_request, base);
930}
931
Herbert Xu32e39832007-03-24 14:35:34 +1100932static inline struct ablkcipher_request *ablkcipher_request_alloc(
933 struct crypto_ablkcipher *tfm, gfp_t gfp)
934{
935 struct ablkcipher_request *req;
936
937 req = kmalloc(sizeof(struct ablkcipher_request) +
938 crypto_ablkcipher_reqsize(tfm), gfp);
939
940 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000941 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100942
943 return req;
944}
945
946static inline void ablkcipher_request_free(struct ablkcipher_request *req)
947{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800948 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100949}
950
951static inline void ablkcipher_request_set_callback(
952 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700953 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +1100954{
Mark Rustad3e3dc252014-07-25 02:53:38 -0700955 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +1100956 req->base.data = data;
957 req->base.flags = flags;
958}
959
960static inline void ablkcipher_request_set_crypt(
961 struct ablkcipher_request *req,
962 struct scatterlist *src, struct scatterlist *dst,
963 unsigned int nbytes, void *iv)
964{
965 req->src = src;
966 req->dst = dst;
967 req->nbytes = nbytes;
968 req->info = iv;
969}
970
Herbert Xu1ae97822007-08-30 15:36:14 +0800971static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
972{
973 return (struct crypto_aead *)tfm;
974}
975
Herbert Xud29ce982007-12-12 19:24:27 +0800976struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800977
978static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
979{
980 return &tfm->base;
981}
982
983static inline void crypto_free_aead(struct crypto_aead *tfm)
984{
985 crypto_free_tfm(crypto_aead_tfm(tfm));
986}
987
988static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
989{
990 return &crypto_aead_tfm(tfm)->crt_aead;
991}
992
993static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
994{
995 return crypto_aead_crt(tfm)->ivsize;
996}
997
998static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
999{
1000 return crypto_aead_crt(tfm)->authsize;
1001}
1002
1003static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1004{
1005 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1006}
1007
1008static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1009{
1010 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1011}
1012
1013static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1014{
1015 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1016}
1017
1018static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1019{
1020 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1021}
1022
1023static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1024{
1025 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1026}
1027
1028static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1029 unsigned int keylen)
1030{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001031 struct aead_tfm *crt = crypto_aead_crt(tfm);
1032
1033 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +08001034}
1035
Herbert Xu7ba683a2007-12-02 18:49:21 +11001036int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1037
Herbert Xu1ae97822007-08-30 15:36:14 +08001038static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1039{
1040 return __crypto_aead_cast(req->base.tfm);
1041}
1042
1043static inline int crypto_aead_encrypt(struct aead_request *req)
1044{
1045 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1046}
1047
1048static inline int crypto_aead_decrypt(struct aead_request *req)
1049{
1050 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1051}
1052
Herbert Xub16c3a22007-08-29 19:02:04 +08001053static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +08001054{
1055 return crypto_aead_crt(tfm)->reqsize;
1056}
1057
1058static inline void aead_request_set_tfm(struct aead_request *req,
1059 struct crypto_aead *tfm)
1060{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001061 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +08001062}
1063
1064static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1065 gfp_t gfp)
1066{
1067 struct aead_request *req;
1068
1069 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1070
1071 if (likely(req))
1072 aead_request_set_tfm(req, tfm);
1073
1074 return req;
1075}
1076
1077static inline void aead_request_free(struct aead_request *req)
1078{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001079 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +08001080}
1081
1082static inline void aead_request_set_callback(struct aead_request *req,
1083 u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001084 crypto_completion_t compl,
Herbert Xu1ae97822007-08-30 15:36:14 +08001085 void *data)
1086{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001087 req->base.complete = compl;
Herbert Xu1ae97822007-08-30 15:36:14 +08001088 req->base.data = data;
1089 req->base.flags = flags;
1090}
1091
1092static inline void aead_request_set_crypt(struct aead_request *req,
1093 struct scatterlist *src,
1094 struct scatterlist *dst,
1095 unsigned int cryptlen, u8 *iv)
1096{
1097 req->src = src;
1098 req->dst = dst;
1099 req->cryptlen = cryptlen;
1100 req->iv = iv;
1101}
1102
1103static inline void aead_request_set_assoc(struct aead_request *req,
1104 struct scatterlist *assoc,
1105 unsigned int assoclen)
1106{
1107 req->assoc = assoc;
1108 req->assoclen = assoclen;
1109}
1110
Herbert Xu5cde0af2006-08-22 00:07:53 +10001111static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1112 struct crypto_tfm *tfm)
1113{
1114 return (struct crypto_blkcipher *)tfm;
1115}
1116
1117static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1118 struct crypto_tfm *tfm)
1119{
1120 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1121 return __crypto_blkcipher_cast(tfm);
1122}
1123
1124static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1125 const char *alg_name, u32 type, u32 mask)
1126{
Herbert Xu332f88402007-11-15 22:36:07 +08001127 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001128 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001129 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001130
1131 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1132}
1133
1134static inline struct crypto_tfm *crypto_blkcipher_tfm(
1135 struct crypto_blkcipher *tfm)
1136{
1137 return &tfm->base;
1138}
1139
1140static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1141{
1142 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1143}
1144
Herbert Xufce32d72006-08-26 17:35:45 +10001145static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1146{
Herbert Xu332f88402007-11-15 22:36:07 +08001147 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001148 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001149 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001150
1151 return crypto_has_alg(alg_name, type, mask);
1152}
1153
Herbert Xu5cde0af2006-08-22 00:07:53 +10001154static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1155{
1156 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1157}
1158
1159static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1160 struct crypto_blkcipher *tfm)
1161{
1162 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1163}
1164
1165static inline struct blkcipher_alg *crypto_blkcipher_alg(
1166 struct crypto_blkcipher *tfm)
1167{
1168 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1169}
1170
1171static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1172{
1173 return crypto_blkcipher_alg(tfm)->ivsize;
1174}
1175
1176static inline unsigned int crypto_blkcipher_blocksize(
1177 struct crypto_blkcipher *tfm)
1178{
1179 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1180}
1181
1182static inline unsigned int crypto_blkcipher_alignmask(
1183 struct crypto_blkcipher *tfm)
1184{
1185 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1186}
1187
1188static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1189{
1190 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1191}
1192
1193static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1194 u32 flags)
1195{
1196 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1197}
1198
1199static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1200 u32 flags)
1201{
1202 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1203}
1204
1205static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1206 const u8 *key, unsigned int keylen)
1207{
1208 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1209 key, keylen);
1210}
1211
1212static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1213 struct scatterlist *dst,
1214 struct scatterlist *src,
1215 unsigned int nbytes)
1216{
1217 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1218 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1219}
1220
1221static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1222 struct scatterlist *dst,
1223 struct scatterlist *src,
1224 unsigned int nbytes)
1225{
1226 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1227}
1228
1229static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1230 struct scatterlist *dst,
1231 struct scatterlist *src,
1232 unsigned int nbytes)
1233{
1234 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1235 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1236}
1237
1238static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1239 struct scatterlist *dst,
1240 struct scatterlist *src,
1241 unsigned int nbytes)
1242{
1243 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1244}
1245
1246static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1247 const u8 *src, unsigned int len)
1248{
1249 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1250}
1251
1252static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1253 u8 *dst, unsigned int len)
1254{
1255 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1256}
1257
Herbert Xuf28776a2006-08-13 20:58:18 +10001258static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1259{
1260 return (struct crypto_cipher *)tfm;
1261}
1262
1263static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1264{
1265 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1266 return __crypto_cipher_cast(tfm);
1267}
1268
1269static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1270 u32 type, u32 mask)
1271{
1272 type &= ~CRYPTO_ALG_TYPE_MASK;
1273 type |= CRYPTO_ALG_TYPE_CIPHER;
1274 mask |= CRYPTO_ALG_TYPE_MASK;
1275
1276 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1277}
1278
1279static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1280{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001281 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001282}
1283
1284static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1285{
1286 crypto_free_tfm(crypto_cipher_tfm(tfm));
1287}
1288
Herbert Xufce32d72006-08-26 17:35:45 +10001289static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1290{
1291 type &= ~CRYPTO_ALG_TYPE_MASK;
1292 type |= CRYPTO_ALG_TYPE_CIPHER;
1293 mask |= CRYPTO_ALG_TYPE_MASK;
1294
1295 return crypto_has_alg(alg_name, type, mask);
1296}
1297
Herbert Xuf28776a2006-08-13 20:58:18 +10001298static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1299{
1300 return &crypto_cipher_tfm(tfm)->crt_cipher;
1301}
1302
1303static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1304{
1305 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1306}
1307
1308static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1309{
1310 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1311}
1312
1313static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1314{
1315 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1316}
1317
1318static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1319 u32 flags)
1320{
1321 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1322}
1323
1324static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1325 u32 flags)
1326{
1327 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1328}
1329
Herbert Xu7226bc872006-08-21 21:40:49 +10001330static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1331 const u8 *key, unsigned int keylen)
1332{
1333 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1334 key, keylen);
1335}
1336
Herbert Xuf28776a2006-08-13 20:58:18 +10001337static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1338 u8 *dst, const u8 *src)
1339{
1340 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1341 dst, src);
1342}
1343
1344static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1345 u8 *dst, const u8 *src)
1346{
1347 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1348 dst, src);
1349}
1350
Herbert Xu055bcee2006-08-19 22:24:23 +10001351static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Herbert Xu055bcee2006-08-19 22:24:23 +10001353 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Herbert Xu055bcee2006-08-19 22:24:23 +10001356static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Herbert Xu055bcee2006-08-19 22:24:23 +10001358 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1359 CRYPTO_ALG_TYPE_HASH_MASK);
1360 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Herbert Xu055bcee2006-08-19 22:24:23 +10001363static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1364 u32 type, u32 mask)
1365{
1366 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001367 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001368 type |= CRYPTO_ALG_TYPE_HASH;
1369 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1370
1371 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1372}
1373
1374static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1375{
1376 return &tfm->base;
1377}
1378
1379static inline void crypto_free_hash(struct crypto_hash *tfm)
1380{
1381 crypto_free_tfm(crypto_hash_tfm(tfm));
1382}
1383
Herbert Xufce32d72006-08-26 17:35:45 +10001384static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1385{
1386 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001387 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001388 type |= CRYPTO_ALG_TYPE_HASH;
1389 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1390
1391 return crypto_has_alg(alg_name, type, mask);
1392}
1393
Herbert Xu055bcee2006-08-19 22:24:23 +10001394static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1395{
1396 return &crypto_hash_tfm(tfm)->crt_hash;
1397}
1398
1399static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1400{
1401 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1402}
1403
1404static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1405{
1406 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1407}
1408
1409static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1410{
1411 return crypto_hash_crt(tfm)->digestsize;
1412}
1413
1414static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1415{
1416 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1417}
1418
1419static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1420{
1421 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1422}
1423
1424static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1425{
1426 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1427}
1428
1429static inline int crypto_hash_init(struct hash_desc *desc)
1430{
1431 return crypto_hash_crt(desc->tfm)->init(desc);
1432}
1433
1434static inline int crypto_hash_update(struct hash_desc *desc,
1435 struct scatterlist *sg,
1436 unsigned int nbytes)
1437{
1438 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1439}
1440
1441static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1442{
1443 return crypto_hash_crt(desc->tfm)->final(desc, out);
1444}
1445
1446static inline int crypto_hash_digest(struct hash_desc *desc,
1447 struct scatterlist *sg,
1448 unsigned int nbytes, u8 *out)
1449{
1450 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1451}
1452
1453static inline int crypto_hash_setkey(struct crypto_hash *hash,
1454 const u8 *key, unsigned int keylen)
1455{
1456 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Herbert Xufce32d72006-08-26 17:35:45 +10001459static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1460{
1461 return (struct crypto_comp *)tfm;
1462}
1463
1464static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1465{
1466 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1467 CRYPTO_ALG_TYPE_MASK);
1468 return __crypto_comp_cast(tfm);
1469}
1470
1471static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1472 u32 type, u32 mask)
1473{
1474 type &= ~CRYPTO_ALG_TYPE_MASK;
1475 type |= CRYPTO_ALG_TYPE_COMPRESS;
1476 mask |= CRYPTO_ALG_TYPE_MASK;
1477
1478 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1479}
1480
1481static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1482{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001483 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001484}
1485
1486static inline void crypto_free_comp(struct crypto_comp *tfm)
1487{
1488 crypto_free_tfm(crypto_comp_tfm(tfm));
1489}
1490
1491static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1492{
1493 type &= ~CRYPTO_ALG_TYPE_MASK;
1494 type |= CRYPTO_ALG_TYPE_COMPRESS;
1495 mask |= CRYPTO_ALG_TYPE_MASK;
1496
1497 return crypto_has_alg(alg_name, type, mask);
1498}
1499
Herbert Xue4d5b792006-08-26 18:12:40 +10001500static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1501{
1502 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1503}
1504
Herbert Xufce32d72006-08-26 17:35:45 +10001505static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1506{
1507 return &crypto_comp_tfm(tfm)->crt_compress;
1508}
1509
1510static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 const u8 *src, unsigned int slen,
1512 u8 *dst, unsigned int *dlen)
1513{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001514 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1515 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Herbert Xufce32d72006-08-26 17:35:45 +10001518static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 const u8 *src, unsigned int slen,
1520 u8 *dst, unsigned int *dlen)
1521{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001522 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1523 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#endif /* _LINUX_CRYPTO_H */
1527