blob: 39c8fc6d33130e8b9f5b658509e1f7b4044269c1 [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 Xu5cb1454b2005-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu5cb1454b2005-11-05 16:58:14 +1100528
529 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000530 atomic_t cra_refcnt;
Herbert Xu5cb1454b2005-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 Xu6d7d684d2006-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu32e3983f2007-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 Xu6d7d684d2006-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 Xu32e3983f2007-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
Stephan Muellerf13ec332014-11-12 05:28:22 +0100824/**
825 * DOC: Asynchronous Block Cipher API
826 *
827 * Asynchronous block cipher API is used with the ciphers of type
828 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
829 *
830 * Asynchronous cipher operations imply that the function invocation for a
831 * cipher request returns immediately before the completion of the operation.
832 * The cipher request is scheduled as a separate kernel thread and therefore
833 * load-balanced on the different CPUs via the process scheduler. To allow
834 * the kernel crypto API to inform the caller about the completion of a cipher
835 * request, the caller must provide a callback function. That function is
836 * invoked with the cipher handle when the request completes.
837 *
838 * To support the asynchronous operation, additional information than just the
839 * cipher handle must be supplied to the kernel crypto API. That additional
840 * information is given by filling in the ablkcipher_request data structure.
841 *
842 * For the asynchronous block cipher API, the state is maintained with the tfm
843 * cipher handle. A single tfm can be used across multiple calls and in
844 * parallel. For asynchronous block cipher calls, context data supplied and
845 * only used by the caller can be referenced the request data structure in
846 * addition to the IV used for the cipher request. The maintenance of such
847 * state information would be important for a crypto driver implementer to
848 * have, because when calling the callback function upon completion of the
849 * cipher operation, that callback function may need some information about
850 * which operation just finished if it invoked multiple in parallel. This
851 * state information is unused by the kernel crypto API.
852 */
853
854/**
855 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
856 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
857 * ablkcipher cipher
858 * @type: specifies the type of the cipher
859 * @mask: specifies the mask for the cipher
860 *
861 * Allocate a cipher handle for an ablkcipher. The returned struct
862 * crypto_ablkcipher is the cipher handle that is required for any subsequent
863 * API invocation for that ablkcipher.
864 *
865 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
866 * of an error, PTR_ERR() returns the error code.
867 */
Herbert Xub9c55aa2007-12-04 12:46:48 +1100868struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
869 u32 type, u32 mask);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100870
871static inline struct crypto_tfm *crypto_ablkcipher_tfm(
872 struct crypto_ablkcipher *tfm)
873{
874 return &tfm->base;
875}
876
Stephan Muellerf13ec332014-11-12 05:28:22 +0100877/**
878 * crypto_free_ablkcipher() - zeroize and free cipher handle
879 * @tfm: cipher handle to be freed
880 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100881static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
882{
883 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
884}
885
Stephan Muellerf13ec332014-11-12 05:28:22 +0100886/**
887 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
888 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
889 * ablkcipher
890 * @type: specifies the type of the cipher
891 * @mask: specifies the mask for the cipher
892 *
893 * Return: true when the ablkcipher is known to the kernel crypto API; false
894 * otherwise
895 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100896static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
897 u32 mask)
898{
Herbert Xu378f4f52007-12-17 20:07:31 +0800899 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
900 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +1100901}
902
903static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
904 struct crypto_ablkcipher *tfm)
905{
906 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
907}
908
Stephan Muellerf13ec332014-11-12 05:28:22 +0100909/**
910 * crypto_ablkcipher_ivsize() - obtain IV size
911 * @tfm: cipher handle
912 *
913 * The size of the IV for the ablkcipher referenced by the cipher handle is
914 * returned. This IV size may be zero if the cipher does not need an IV.
915 *
916 * Return: IV size in bytes
917 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100918static inline unsigned int crypto_ablkcipher_ivsize(
919 struct crypto_ablkcipher *tfm)
920{
921 return crypto_ablkcipher_crt(tfm)->ivsize;
922}
923
Stephan Muellerf13ec332014-11-12 05:28:22 +0100924/**
925 * crypto_ablkcipher_blocksize() - obtain block size of cipher
926 * @tfm: cipher handle
927 *
928 * The block size for the ablkcipher referenced with the cipher handle is
929 * returned. The caller may use that information to allocate appropriate
930 * memory for the data returned by the encryption or decryption operation
931 *
932 * Return: block size of cipher
933 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100934static inline unsigned int crypto_ablkcipher_blocksize(
935 struct crypto_ablkcipher *tfm)
936{
937 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
938}
939
940static inline unsigned int crypto_ablkcipher_alignmask(
941 struct crypto_ablkcipher *tfm)
942{
943 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
944}
945
946static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
947{
948 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
949}
950
951static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
952 u32 flags)
953{
954 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
955}
956
957static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
958 u32 flags)
959{
960 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
961}
962
Stephan Muellerf13ec332014-11-12 05:28:22 +0100963/**
964 * crypto_ablkcipher_setkey() - set key for cipher
965 * @tfm: cipher handle
966 * @key: buffer holding the key
967 * @keylen: length of the key in bytes
968 *
969 * The caller provided key is set for the ablkcipher referenced by the cipher
970 * handle.
971 *
972 * Note, the key length determines the cipher type. Many block ciphers implement
973 * different cipher modes depending on the key size, such as AES-128 vs AES-192
974 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
975 * is performed.
976 *
977 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
978 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100979static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
980 const u8 *key, unsigned int keylen)
981{
Herbert Xuecfc4322007-12-05 21:08:36 +1100982 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
983
984 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100985}
986
Stephan Muellerf13ec332014-11-12 05:28:22 +0100987/**
988 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
989 * @req: ablkcipher_request out of which the cipher handle is to be obtained
990 *
991 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
992 * data structure.
993 *
994 * Return: crypto_ablkcipher handle
995 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100996static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
997 struct ablkcipher_request *req)
998{
999 return __crypto_ablkcipher_cast(req->base.tfm);
1000}
1001
Stephan Muellerf13ec332014-11-12 05:28:22 +01001002/**
1003 * crypto_ablkcipher_encrypt() - encrypt plaintext
1004 * @req: reference to the ablkcipher_request handle that holds all information
1005 * needed to perform the cipher operation
1006 *
1007 * Encrypt plaintext data using the ablkcipher_request handle. That data
1008 * structure and how it is filled with data is discussed with the
1009 * ablkcipher_request_* functions.
1010 *
1011 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1012 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001013static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1014{
1015 struct ablkcipher_tfm *crt =
1016 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1017 return crt->encrypt(req);
1018}
1019
Stephan Muellerf13ec332014-11-12 05:28:22 +01001020/**
1021 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1022 * @req: reference to the ablkcipher_request handle that holds all information
1023 * needed to perform the cipher operation
1024 *
1025 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1026 * structure and how it is filled with data is discussed with the
1027 * ablkcipher_request_* functions.
1028 *
1029 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1030 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001031static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1032{
1033 struct ablkcipher_tfm *crt =
1034 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1035 return crt->decrypt(req);
1036}
1037
Stephan Muellerf13ec332014-11-12 05:28:22 +01001038/**
1039 * DOC: Asynchronous Cipher Request Handle
1040 *
1041 * The ablkcipher_request data structure contains all pointers to data
1042 * required for the asynchronous cipher operation. This includes the cipher
1043 * handle (which can be used by multiple ablkcipher_request instances), pointer
1044 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1045 * as a handle to the ablkcipher_request_* API calls in a similar way as
1046 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1047 */
1048
1049/**
1050 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1051 * @tfm: cipher handle
1052 *
1053 * Return: number of bytes
1054 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001055static inline unsigned int crypto_ablkcipher_reqsize(
1056 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001057{
1058 return crypto_ablkcipher_crt(tfm)->reqsize;
1059}
1060
Stephan Muellerf13ec332014-11-12 05:28:22 +01001061/**
1062 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1063 * @req: request handle to be modified
1064 * @tfm: cipher handle that shall be added to the request handle
1065 *
1066 * Allow the caller to replace the existing ablkcipher handle in the request
1067 * data structure with a different one.
1068 */
Herbert Xue196d622007-04-14 16:09:14 +10001069static inline void ablkcipher_request_set_tfm(
1070 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1071{
Herbert Xuecfc4322007-12-05 21:08:36 +11001072 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001073}
1074
Herbert Xub5b7f082007-04-16 20:48:54 +10001075static inline struct ablkcipher_request *ablkcipher_request_cast(
1076 struct crypto_async_request *req)
1077{
1078 return container_of(req, struct ablkcipher_request, base);
1079}
1080
Stephan Muellerf13ec332014-11-12 05:28:22 +01001081/**
1082 * ablkcipher_request_alloc() - allocate request data structure
1083 * @tfm: cipher handle to be registered with the request
1084 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1085 *
1086 * Allocate the request data structure that must be used with the ablkcipher
1087 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1088 * handle is registered in the request data structure.
1089 *
1090 * Return: allocated request handle in case of success; IS_ERR() is true in case
1091 * of an error, PTR_ERR() returns the error code.
1092 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001093static inline struct ablkcipher_request *ablkcipher_request_alloc(
1094 struct crypto_ablkcipher *tfm, gfp_t gfp)
1095{
1096 struct ablkcipher_request *req;
1097
1098 req = kmalloc(sizeof(struct ablkcipher_request) +
1099 crypto_ablkcipher_reqsize(tfm), gfp);
1100
1101 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001102 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001103
1104 return req;
1105}
1106
Stephan Muellerf13ec332014-11-12 05:28:22 +01001107/**
1108 * ablkcipher_request_free() - zeroize and free request data structure
1109 * @req: request data structure cipher handle to be freed
1110 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001111static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1112{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001113 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001114}
1115
Stephan Muellerf13ec332014-11-12 05:28:22 +01001116/**
1117 * ablkcipher_request_set_callback() - set asynchronous callback function
1118 * @req: request handle
1119 * @flags: specify zero or an ORing of the flags
1120 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1121 * increase the wait queue beyond the initial maximum size;
1122 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1123 * @compl: callback function pointer to be registered with the request handle
1124 * @data: The data pointer refers to memory that is not used by the kernel
1125 * crypto API, but provided to the callback function for it to use. Here,
1126 * the caller can provide a reference to memory the callback function can
1127 * operate on. As the callback function is invoked asynchronously to the
1128 * related functionality, it may need to access data structures of the
1129 * related functionality which can be referenced using this pointer. The
1130 * callback function can access the memory via the "data" field in the
1131 * crypto_async_request data structure provided to the callback function.
1132 *
1133 * This function allows setting the callback function that is triggered once the
1134 * cipher operation completes.
1135 *
1136 * The callback function is registered with the ablkcipher_request handle and
1137 * must comply with the following template:
1138 *
1139 * void callback_function(struct crypto_async_request *req, int error)
1140 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001141static inline void ablkcipher_request_set_callback(
1142 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001143 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001144{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001145 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001146 req->base.data = data;
1147 req->base.flags = flags;
1148}
1149
Stephan Muellerf13ec332014-11-12 05:28:22 +01001150/**
1151 * ablkcipher_request_set_crypt() - set data buffers
1152 * @req: request handle
1153 * @src: source scatter / gather list
1154 * @dst: destination scatter / gather list
1155 * @nbytes: number of bytes to process from @src
1156 * @iv: IV for the cipher operation which must comply with the IV size defined
1157 * by crypto_ablkcipher_ivsize
1158 *
1159 * This function allows setting of the source data and destination data
1160 * scatter / gather lists.
1161 *
1162 * For encryption, the source is treated as the plaintext and the
1163 * destination is the ciphertext. For a decryption operation, the use is
1164 * reversed: the source is the ciphertext and the destination is the plaintext.
1165 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001166static inline void ablkcipher_request_set_crypt(
1167 struct ablkcipher_request *req,
1168 struct scatterlist *src, struct scatterlist *dst,
1169 unsigned int nbytes, void *iv)
1170{
1171 req->src = src;
1172 req->dst = dst;
1173 req->nbytes = nbytes;
1174 req->info = iv;
1175}
1176
Herbert Xu1ae97822007-08-30 15:36:14 +08001177static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1178{
1179 return (struct crypto_aead *)tfm;
1180}
1181
Herbert Xud29ce982007-12-12 19:24:27 +08001182struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +08001183
1184static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1185{
1186 return &tfm->base;
1187}
1188
1189static inline void crypto_free_aead(struct crypto_aead *tfm)
1190{
1191 crypto_free_tfm(crypto_aead_tfm(tfm));
1192}
1193
1194static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1195{
1196 return &crypto_aead_tfm(tfm)->crt_aead;
1197}
1198
1199static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1200{
1201 return crypto_aead_crt(tfm)->ivsize;
1202}
1203
1204static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1205{
1206 return crypto_aead_crt(tfm)->authsize;
1207}
1208
1209static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1210{
1211 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1212}
1213
1214static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1215{
1216 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1217}
1218
1219static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1220{
1221 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1222}
1223
1224static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1225{
1226 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1227}
1228
1229static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1230{
1231 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1232}
1233
1234static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1235 unsigned int keylen)
1236{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001237 struct aead_tfm *crt = crypto_aead_crt(tfm);
1238
1239 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +08001240}
1241
Herbert Xu7ba683a2007-12-02 18:49:21 +11001242int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1243
Herbert Xu1ae97822007-08-30 15:36:14 +08001244static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1245{
1246 return __crypto_aead_cast(req->base.tfm);
1247}
1248
1249static inline int crypto_aead_encrypt(struct aead_request *req)
1250{
1251 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1252}
1253
1254static inline int crypto_aead_decrypt(struct aead_request *req)
1255{
1256 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1257}
1258
Herbert Xub16c3a22007-08-29 19:02:04 +08001259static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +08001260{
1261 return crypto_aead_crt(tfm)->reqsize;
1262}
1263
1264static inline void aead_request_set_tfm(struct aead_request *req,
1265 struct crypto_aead *tfm)
1266{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001267 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +08001268}
1269
1270static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1271 gfp_t gfp)
1272{
1273 struct aead_request *req;
1274
1275 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1276
1277 if (likely(req))
1278 aead_request_set_tfm(req, tfm);
1279
1280 return req;
1281}
1282
1283static inline void aead_request_free(struct aead_request *req)
1284{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001285 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +08001286}
1287
1288static inline void aead_request_set_callback(struct aead_request *req,
1289 u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001290 crypto_completion_t compl,
Herbert Xu1ae97822007-08-30 15:36:14 +08001291 void *data)
1292{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001293 req->base.complete = compl;
Herbert Xu1ae97822007-08-30 15:36:14 +08001294 req->base.data = data;
1295 req->base.flags = flags;
1296}
1297
1298static inline void aead_request_set_crypt(struct aead_request *req,
1299 struct scatterlist *src,
1300 struct scatterlist *dst,
1301 unsigned int cryptlen, u8 *iv)
1302{
1303 req->src = src;
1304 req->dst = dst;
1305 req->cryptlen = cryptlen;
1306 req->iv = iv;
1307}
1308
1309static inline void aead_request_set_assoc(struct aead_request *req,
1310 struct scatterlist *assoc,
1311 unsigned int assoclen)
1312{
1313 req->assoc = assoc;
1314 req->assoclen = assoclen;
1315}
1316
Herbert Xu5cde0af2006-08-22 00:07:53 +10001317static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1318 struct crypto_tfm *tfm)
1319{
1320 return (struct crypto_blkcipher *)tfm;
1321}
1322
1323static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1324 struct crypto_tfm *tfm)
1325{
1326 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1327 return __crypto_blkcipher_cast(tfm);
1328}
1329
1330static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1331 const char *alg_name, u32 type, u32 mask)
1332{
Herbert Xu332f88402007-11-15 22:36:07 +08001333 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001334 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001335 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001336
1337 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1338}
1339
1340static inline struct crypto_tfm *crypto_blkcipher_tfm(
1341 struct crypto_blkcipher *tfm)
1342{
1343 return &tfm->base;
1344}
1345
1346static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1347{
1348 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1349}
1350
Herbert Xufce32d72006-08-26 17:35:45 +10001351static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1352{
Herbert Xu332f88402007-11-15 22:36:07 +08001353 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001354 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001355 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001356
1357 return crypto_has_alg(alg_name, type, mask);
1358}
1359
Herbert Xu5cde0af2006-08-22 00:07:53 +10001360static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1361{
1362 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1363}
1364
1365static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1366 struct crypto_blkcipher *tfm)
1367{
1368 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1369}
1370
1371static inline struct blkcipher_alg *crypto_blkcipher_alg(
1372 struct crypto_blkcipher *tfm)
1373{
1374 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1375}
1376
1377static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1378{
1379 return crypto_blkcipher_alg(tfm)->ivsize;
1380}
1381
1382static inline unsigned int crypto_blkcipher_blocksize(
1383 struct crypto_blkcipher *tfm)
1384{
1385 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1386}
1387
1388static inline unsigned int crypto_blkcipher_alignmask(
1389 struct crypto_blkcipher *tfm)
1390{
1391 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1392}
1393
1394static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1395{
1396 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1397}
1398
1399static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1400 u32 flags)
1401{
1402 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1403}
1404
1405static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1406 u32 flags)
1407{
1408 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1409}
1410
1411static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1412 const u8 *key, unsigned int keylen)
1413{
1414 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1415 key, keylen);
1416}
1417
1418static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1419 struct scatterlist *dst,
1420 struct scatterlist *src,
1421 unsigned int nbytes)
1422{
1423 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1424 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1425}
1426
1427static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1428 struct scatterlist *dst,
1429 struct scatterlist *src,
1430 unsigned int nbytes)
1431{
1432 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1433}
1434
1435static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1436 struct scatterlist *dst,
1437 struct scatterlist *src,
1438 unsigned int nbytes)
1439{
1440 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1441 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1442}
1443
1444static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1445 struct scatterlist *dst,
1446 struct scatterlist *src,
1447 unsigned int nbytes)
1448{
1449 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1450}
1451
1452static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1453 const u8 *src, unsigned int len)
1454{
1455 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1456}
1457
1458static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1459 u8 *dst, unsigned int len)
1460{
1461 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1462}
1463
Herbert Xuf28776a2006-08-13 20:58:18 +10001464static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1465{
1466 return (struct crypto_cipher *)tfm;
1467}
1468
1469static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1470{
1471 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1472 return __crypto_cipher_cast(tfm);
1473}
1474
1475static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1476 u32 type, u32 mask)
1477{
1478 type &= ~CRYPTO_ALG_TYPE_MASK;
1479 type |= CRYPTO_ALG_TYPE_CIPHER;
1480 mask |= CRYPTO_ALG_TYPE_MASK;
1481
1482 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1483}
1484
1485static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1486{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001487 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001488}
1489
1490static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1491{
1492 crypto_free_tfm(crypto_cipher_tfm(tfm));
1493}
1494
Herbert Xufce32d72006-08-26 17:35:45 +10001495static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1496{
1497 type &= ~CRYPTO_ALG_TYPE_MASK;
1498 type |= CRYPTO_ALG_TYPE_CIPHER;
1499 mask |= CRYPTO_ALG_TYPE_MASK;
1500
1501 return crypto_has_alg(alg_name, type, mask);
1502}
1503
Herbert Xuf28776a2006-08-13 20:58:18 +10001504static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1505{
1506 return &crypto_cipher_tfm(tfm)->crt_cipher;
1507}
1508
1509static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1510{
1511 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1512}
1513
1514static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1515{
1516 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1517}
1518
1519static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1520{
1521 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1522}
1523
1524static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1525 u32 flags)
1526{
1527 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1528}
1529
1530static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1531 u32 flags)
1532{
1533 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1534}
1535
Herbert Xu7226bc872006-08-21 21:40:49 +10001536static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1537 const u8 *key, unsigned int keylen)
1538{
1539 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1540 key, keylen);
1541}
1542
Herbert Xuf28776a2006-08-13 20:58:18 +10001543static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1544 u8 *dst, const u8 *src)
1545{
1546 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1547 dst, src);
1548}
1549
1550static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1551 u8 *dst, const u8 *src)
1552{
1553 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1554 dst, src);
1555}
1556
Herbert Xu055bcee2006-08-19 22:24:23 +10001557static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Herbert Xu055bcee2006-08-19 22:24:23 +10001559 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Herbert Xu055bcee2006-08-19 22:24:23 +10001562static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Herbert Xu055bcee2006-08-19 22:24:23 +10001564 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1565 CRYPTO_ALG_TYPE_HASH_MASK);
1566 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Herbert Xu055bcee2006-08-19 22:24:23 +10001569static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1570 u32 type, u32 mask)
1571{
1572 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001573 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001574 type |= CRYPTO_ALG_TYPE_HASH;
1575 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1576
1577 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1578}
1579
1580static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1581{
1582 return &tfm->base;
1583}
1584
1585static inline void crypto_free_hash(struct crypto_hash *tfm)
1586{
1587 crypto_free_tfm(crypto_hash_tfm(tfm));
1588}
1589
Herbert Xufce32d72006-08-26 17:35:45 +10001590static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1591{
1592 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001593 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001594 type |= CRYPTO_ALG_TYPE_HASH;
1595 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1596
1597 return crypto_has_alg(alg_name, type, mask);
1598}
1599
Herbert Xu055bcee2006-08-19 22:24:23 +10001600static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1601{
1602 return &crypto_hash_tfm(tfm)->crt_hash;
1603}
1604
1605static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1606{
1607 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1608}
1609
1610static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1611{
1612 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1613}
1614
1615static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1616{
1617 return crypto_hash_crt(tfm)->digestsize;
1618}
1619
1620static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1621{
1622 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1623}
1624
1625static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1626{
1627 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1628}
1629
1630static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1631{
1632 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1633}
1634
1635static inline int crypto_hash_init(struct hash_desc *desc)
1636{
1637 return crypto_hash_crt(desc->tfm)->init(desc);
1638}
1639
1640static inline int crypto_hash_update(struct hash_desc *desc,
1641 struct scatterlist *sg,
1642 unsigned int nbytes)
1643{
1644 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1645}
1646
1647static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1648{
1649 return crypto_hash_crt(desc->tfm)->final(desc, out);
1650}
1651
1652static inline int crypto_hash_digest(struct hash_desc *desc,
1653 struct scatterlist *sg,
1654 unsigned int nbytes, u8 *out)
1655{
1656 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1657}
1658
1659static inline int crypto_hash_setkey(struct crypto_hash *hash,
1660 const u8 *key, unsigned int keylen)
1661{
1662 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Herbert Xufce32d72006-08-26 17:35:45 +10001665static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1666{
1667 return (struct crypto_comp *)tfm;
1668}
1669
1670static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1671{
1672 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1673 CRYPTO_ALG_TYPE_MASK);
1674 return __crypto_comp_cast(tfm);
1675}
1676
1677static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1678 u32 type, u32 mask)
1679{
1680 type &= ~CRYPTO_ALG_TYPE_MASK;
1681 type |= CRYPTO_ALG_TYPE_COMPRESS;
1682 mask |= CRYPTO_ALG_TYPE_MASK;
1683
1684 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1685}
1686
1687static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1688{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001689 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001690}
1691
1692static inline void crypto_free_comp(struct crypto_comp *tfm)
1693{
1694 crypto_free_tfm(crypto_comp_tfm(tfm));
1695}
1696
1697static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1698{
1699 type &= ~CRYPTO_ALG_TYPE_MASK;
1700 type |= CRYPTO_ALG_TYPE_COMPRESS;
1701 mask |= CRYPTO_ALG_TYPE_MASK;
1702
1703 return crypto_has_alg(alg_name, type, mask);
1704}
1705
Herbert Xue4d5b792006-08-26 18:12:40 +10001706static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1707{
1708 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1709}
1710
Herbert Xufce32d72006-08-26 17:35:45 +10001711static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1712{
1713 return &crypto_comp_tfm(tfm)->crt_compress;
1714}
1715
1716static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 const u8 *src, unsigned int slen,
1718 u8 *dst, unsigned int *dlen)
1719{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001720 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1721 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Herbert Xufce32d72006-08-26 17:35:45 +10001724static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 const u8 *src, unsigned int slen,
1726 u8 *dst, unsigned int *dlen)
1727{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001728 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1729 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#endif /* _LINUX_CRYPTO_H */
1733