blob: 4704e71a31fecfadaceed37a66260f128c3ab5db [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 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 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 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 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
Stephan Muellerfced7b02014-11-12 05:29:00 +01001177/**
1178 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1179 *
1180 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1181 * (listed as type "aead" in /proc/crypto)
1182 *
1183 * The most prominent examples for this type of encryption is GCM and CCM.
1184 * However, the kernel supports other types of AEAD ciphers which are defined
1185 * with the following cipher string:
1186 *
1187 * authenc(keyed message digest, block cipher)
1188 *
1189 * For example: authenc(hmac(sha256), cbc(aes))
1190 *
1191 * The example code provided for the asynchronous block cipher operation
1192 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1193 * the *aead* pendants discussed in the following. In addtion, for the AEAD
1194 * operation, the aead_request_set_assoc function must be used to set the
1195 * pointer to the associated data memory location before performing the
1196 * encryption or decryption operation. In case of an encryption, the associated
1197 * data memory is filled during the encryption operation. For decryption, the
1198 * associated data memory must contain data that is used to verify the integrity
1199 * of the decrypted data. Another deviation from the asynchronous block cipher
1200 * operation is that the caller should explicitly check for -EBADMSG of the
1201 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1202 * a breach in the integrity of the message. In essence, that -EBADMSG error
1203 * code is the key bonus an AEAD cipher has over "standard" block chaining
1204 * modes.
1205 */
1206
Herbert Xu1ae97822007-08-30 15:36:14 +08001207static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1208{
1209 return (struct crypto_aead *)tfm;
1210}
1211
Stephan Muellerfced7b02014-11-12 05:29:00 +01001212/**
1213 * crypto_alloc_aead() - allocate AEAD cipher handle
1214 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1215 * AEAD cipher
1216 * @type: specifies the type of the cipher
1217 * @mask: specifies the mask for the cipher
1218 *
1219 * Allocate a cipher handle for an AEAD. The returned struct
1220 * crypto_aead is the cipher handle that is required for any subsequent
1221 * API invocation for that AEAD.
1222 *
1223 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1224 * of an error, PTR_ERR() returns the error code.
1225 */
Herbert Xud29ce982007-12-12 19:24:27 +08001226struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +08001227
1228static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1229{
1230 return &tfm->base;
1231}
1232
Stephan Muellerfced7b02014-11-12 05:29:00 +01001233/**
1234 * crypto_free_aead() - zeroize and free aead handle
1235 * @tfm: cipher handle to be freed
1236 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001237static inline void crypto_free_aead(struct crypto_aead *tfm)
1238{
1239 crypto_free_tfm(crypto_aead_tfm(tfm));
1240}
1241
1242static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1243{
1244 return &crypto_aead_tfm(tfm)->crt_aead;
1245}
1246
Stephan Muellerfced7b02014-11-12 05:29:00 +01001247/**
1248 * crypto_aead_ivsize() - obtain IV size
1249 * @tfm: cipher handle
1250 *
1251 * The size of the IV for the aead referenced by the cipher handle is
1252 * returned. This IV size may be zero if the cipher does not need an IV.
1253 *
1254 * Return: IV size in bytes
1255 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001256static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1257{
1258 return crypto_aead_crt(tfm)->ivsize;
1259}
1260
Stephan Muellerfced7b02014-11-12 05:29:00 +01001261/**
1262 * crypto_aead_authsize() - obtain maximum authentication data size
1263 * @tfm: cipher handle
1264 *
1265 * The maximum size of the authentication data for the AEAD cipher referenced
1266 * by the AEAD cipher handle is returned. The authentication data size may be
1267 * zero if the cipher implements a hard-coded maximum.
1268 *
1269 * The authentication data may also be known as "tag value".
1270 *
1271 * Return: authentication data size / tag size in bytes
1272 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001273static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1274{
1275 return crypto_aead_crt(tfm)->authsize;
1276}
1277
Stephan Muellerfced7b02014-11-12 05:29:00 +01001278/**
1279 * crypto_aead_blocksize() - obtain block size of cipher
1280 * @tfm: cipher handle
1281 *
1282 * The block size for the AEAD referenced with the cipher handle is returned.
1283 * The caller may use that information to allocate appropriate memory for the
1284 * data returned by the encryption or decryption operation
1285 *
1286 * Return: block size of cipher
1287 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001288static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1289{
1290 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1291}
1292
1293static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1294{
1295 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1296}
1297
1298static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1299{
1300 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1301}
1302
1303static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1304{
1305 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1306}
1307
1308static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1309{
1310 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1311}
1312
Stephan Muellerfced7b02014-11-12 05:29:00 +01001313/**
1314 * crypto_aead_setkey() - set key for cipher
1315 * @tfm: cipher handle
1316 * @key: buffer holding the key
1317 * @keylen: length of the key in bytes
1318 *
1319 * The caller provided key is set for the AEAD referenced by the cipher
1320 * handle.
1321 *
1322 * Note, the key length determines the cipher type. Many block ciphers implement
1323 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1324 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1325 * is performed.
1326 *
1327 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1328 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001329static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1330 unsigned int keylen)
1331{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001332 struct aead_tfm *crt = crypto_aead_crt(tfm);
1333
1334 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +08001335}
1336
Stephan Muellerfced7b02014-11-12 05:29:00 +01001337/**
1338 * crypto_aead_setauthsize() - set authentication data size
1339 * @tfm: cipher handle
1340 * @authsize: size of the authentication data / tag in bytes
1341 *
1342 * Set the authentication data size / tag size. AEAD requires an authentication
1343 * tag (or MAC) in addition to the associated data.
1344 *
1345 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1346 */
Herbert Xu7ba683a2007-12-02 18:49:21 +11001347int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1348
Herbert Xu1ae97822007-08-30 15:36:14 +08001349static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1350{
1351 return __crypto_aead_cast(req->base.tfm);
1352}
1353
Stephan Muellerfced7b02014-11-12 05:29:00 +01001354/**
1355 * crypto_aead_encrypt() - encrypt plaintext
1356 * @req: reference to the aead_request handle that holds all information
1357 * needed to perform the cipher operation
1358 *
1359 * Encrypt plaintext data using the aead_request handle. That data structure
1360 * and how it is filled with data is discussed with the aead_request_*
1361 * functions.
1362 *
1363 * IMPORTANT NOTE The encryption operation creates the authentication data /
1364 * tag. That data is concatenated with the created ciphertext.
1365 * The ciphertext memory size is therefore the given number of
1366 * block cipher blocks + the size defined by the
1367 * crypto_aead_setauthsize invocation. The caller must ensure
1368 * that sufficient memory is available for the ciphertext and
1369 * the authentication tag.
1370 *
1371 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1372 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001373static inline int crypto_aead_encrypt(struct aead_request *req)
1374{
1375 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1376}
1377
Stephan Muellerfced7b02014-11-12 05:29:00 +01001378/**
1379 * crypto_aead_decrypt() - decrypt ciphertext
1380 * @req: reference to the ablkcipher_request handle that holds all information
1381 * needed to perform the cipher operation
1382 *
1383 * Decrypt ciphertext data using the aead_request handle. That data structure
1384 * and how it is filled with data is discussed with the aead_request_*
1385 * functions.
1386 *
1387 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1388 * authentication data / tag. That authentication data / tag
1389 * must have the size defined by the crypto_aead_setauthsize
1390 * invocation.
1391 *
1392 *
1393 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1394 * cipher operation performs the authentication of the data during the
1395 * decryption operation. Therefore, the function returns this error if
1396 * the authentication of the ciphertext was unsuccessful (i.e. the
1397 * integrity of the ciphertext or the associated data was violated);
1398 * < 0 if an error occurred.
1399 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001400static inline int crypto_aead_decrypt(struct aead_request *req)
1401{
1402 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1403}
1404
Stephan Muellerfced7b02014-11-12 05:29:00 +01001405/**
1406 * DOC: Asynchronous AEAD Request Handle
1407 *
1408 * The aead_request data structure contains all pointers to data required for
1409 * the AEAD cipher operation. This includes the cipher handle (which can be
1410 * used by multiple aead_request instances), pointer to plaintext and
1411 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1412 * aead_request_* API calls in a similar way as AEAD handle to the
1413 * crypto_aead_* API calls.
1414 */
1415
1416/**
1417 * crypto_aead_reqsize() - obtain size of the request data structure
1418 * @tfm: cipher handle
1419 *
1420 * Return: number of bytes
1421 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001422static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +08001423{
1424 return crypto_aead_crt(tfm)->reqsize;
1425}
1426
Stephan Muellerfced7b02014-11-12 05:29:00 +01001427/**
1428 * aead_request_set_tfm() - update cipher handle reference in request
1429 * @req: request handle to be modified
1430 * @tfm: cipher handle that shall be added to the request handle
1431 *
1432 * Allow the caller to replace the existing aead handle in the request
1433 * data structure with a different one.
1434 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001435static inline void aead_request_set_tfm(struct aead_request *req,
1436 struct crypto_aead *tfm)
1437{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001438 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +08001439}
1440
Stephan Muellerfced7b02014-11-12 05:29:00 +01001441/**
1442 * aead_request_alloc() - allocate request data structure
1443 * @tfm: cipher handle to be registered with the request
1444 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1445 *
1446 * Allocate the request data structure that must be used with the AEAD
1447 * encrypt and decrypt API calls. During the allocation, the provided aead
1448 * handle is registered in the request data structure.
1449 *
1450 * Return: allocated request handle in case of success; IS_ERR() is true in case
1451 * of an error, PTR_ERR() returns the error code.
1452 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001453static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1454 gfp_t gfp)
1455{
1456 struct aead_request *req;
1457
1458 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1459
1460 if (likely(req))
1461 aead_request_set_tfm(req, tfm);
1462
1463 return req;
1464}
1465
Stephan Muellerfced7b02014-11-12 05:29:00 +01001466/**
1467 * aead_request_free() - zeroize and free request data structure
1468 * @req: request data structure cipher handle to be freed
1469 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001470static inline void aead_request_free(struct aead_request *req)
1471{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001472 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +08001473}
1474
Stephan Muellerfced7b02014-11-12 05:29:00 +01001475/**
1476 * aead_request_set_callback() - set asynchronous callback function
1477 * @req: request handle
1478 * @flags: specify zero or an ORing of the flags
1479 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1480 * increase the wait queue beyond the initial maximum size;
1481 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1482 * @compl: callback function pointer to be registered with the request handle
1483 * @data: The data pointer refers to memory that is not used by the kernel
1484 * crypto API, but provided to the callback function for it to use. Here,
1485 * the caller can provide a reference to memory the callback function can
1486 * operate on. As the callback function is invoked asynchronously to the
1487 * related functionality, it may need to access data structures of the
1488 * related functionality which can be referenced using this pointer. The
1489 * callback function can access the memory via the "data" field in the
1490 * crypto_async_request data structure provided to the callback function.
1491 *
1492 * Setting the callback function that is triggered once the cipher operation
1493 * completes
1494 *
1495 * The callback function is registered with the aead_request handle and
1496 * must comply with the following template:
1497 *
1498 * void callback_function(struct crypto_async_request *req, int error)
1499 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001500static inline void aead_request_set_callback(struct aead_request *req,
1501 u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001502 crypto_completion_t compl,
Herbert Xu1ae97822007-08-30 15:36:14 +08001503 void *data)
1504{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001505 req->base.complete = compl;
Herbert Xu1ae97822007-08-30 15:36:14 +08001506 req->base.data = data;
1507 req->base.flags = flags;
1508}
1509
Stephan Muellerfced7b02014-11-12 05:29:00 +01001510/**
1511 * aead_request_set_crypt - set data buffers
1512 * @req: request handle
1513 * @src: source scatter / gather list
1514 * @dst: destination scatter / gather list
1515 * @cryptlen: number of bytes to process from @src
1516 * @iv: IV for the cipher operation which must comply with the IV size defined
1517 * by crypto_aead_ivsize()
1518 *
1519 * Setting the source data and destination data scatter / gather lists.
1520 *
1521 * For encryption, the source is treated as the plaintext and the
1522 * destination is the ciphertext. For a decryption operation, the use is
1523 * reversed: the source is the ciphertext and the destination is the plaintext.
1524 *
1525 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1526 * the caller must concatenate the ciphertext followed by the
1527 * authentication tag and provide the entire data stream to the
1528 * decryption operation (i.e. the data length used for the
1529 * initialization of the scatterlist and the data length for the
1530 * decryption operation is identical). For encryption, however,
1531 * the authentication tag is created while encrypting the data.
1532 * The destination buffer must hold sufficient space for the
1533 * ciphertext and the authentication tag while the encryption
1534 * invocation must only point to the plaintext data size. The
1535 * following code snippet illustrates the memory usage
1536 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1537 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1538 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1539 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001540static inline void aead_request_set_crypt(struct aead_request *req,
1541 struct scatterlist *src,
1542 struct scatterlist *dst,
1543 unsigned int cryptlen, u8 *iv)
1544{
1545 req->src = src;
1546 req->dst = dst;
1547 req->cryptlen = cryptlen;
1548 req->iv = iv;
1549}
1550
Stephan Muellerfced7b02014-11-12 05:29:00 +01001551/**
1552 * aead_request_set_assoc() - set the associated data scatter / gather list
1553 * @req: request handle
1554 * @assoc: associated data scatter / gather list
1555 * @assoclen: number of bytes to process from @assoc
1556 *
1557 * For encryption, the memory is filled with the associated data. For
1558 * decryption, the memory must point to the associated data.
1559 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001560static inline void aead_request_set_assoc(struct aead_request *req,
1561 struct scatterlist *assoc,
1562 unsigned int assoclen)
1563{
1564 req->assoc = assoc;
1565 req->assoclen = assoclen;
1566}
1567
Herbert Xu5cde0af2006-08-22 00:07:53 +10001568static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1569 struct crypto_tfm *tfm)
1570{
1571 return (struct crypto_blkcipher *)tfm;
1572}
1573
1574static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1575 struct crypto_tfm *tfm)
1576{
1577 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1578 return __crypto_blkcipher_cast(tfm);
1579}
1580
1581static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1582 const char *alg_name, u32 type, u32 mask)
1583{
Herbert Xu332f88402007-11-15 22:36:07 +08001584 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001585 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001586 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001587
1588 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1589}
1590
1591static inline struct crypto_tfm *crypto_blkcipher_tfm(
1592 struct crypto_blkcipher *tfm)
1593{
1594 return &tfm->base;
1595}
1596
1597static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1598{
1599 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1600}
1601
Herbert Xufce32d72006-08-26 17:35:45 +10001602static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1603{
Herbert Xu332f88402007-11-15 22:36:07 +08001604 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001605 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001606 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001607
1608 return crypto_has_alg(alg_name, type, mask);
1609}
1610
Herbert Xu5cde0af2006-08-22 00:07:53 +10001611static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1612{
1613 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1614}
1615
1616static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1617 struct crypto_blkcipher *tfm)
1618{
1619 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1620}
1621
1622static inline struct blkcipher_alg *crypto_blkcipher_alg(
1623 struct crypto_blkcipher *tfm)
1624{
1625 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1626}
1627
1628static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1629{
1630 return crypto_blkcipher_alg(tfm)->ivsize;
1631}
1632
1633static inline unsigned int crypto_blkcipher_blocksize(
1634 struct crypto_blkcipher *tfm)
1635{
1636 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1637}
1638
1639static inline unsigned int crypto_blkcipher_alignmask(
1640 struct crypto_blkcipher *tfm)
1641{
1642 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1643}
1644
1645static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1646{
1647 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1648}
1649
1650static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1651 u32 flags)
1652{
1653 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1654}
1655
1656static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1657 u32 flags)
1658{
1659 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1660}
1661
1662static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1663 const u8 *key, unsigned int keylen)
1664{
1665 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1666 key, keylen);
1667}
1668
1669static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1670 struct scatterlist *dst,
1671 struct scatterlist *src,
1672 unsigned int nbytes)
1673{
1674 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1675 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1676}
1677
1678static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1679 struct scatterlist *dst,
1680 struct scatterlist *src,
1681 unsigned int nbytes)
1682{
1683 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1684}
1685
1686static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1687 struct scatterlist *dst,
1688 struct scatterlist *src,
1689 unsigned int nbytes)
1690{
1691 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1692 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1693}
1694
1695static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1696 struct scatterlist *dst,
1697 struct scatterlist *src,
1698 unsigned int nbytes)
1699{
1700 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1701}
1702
1703static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1704 const u8 *src, unsigned int len)
1705{
1706 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1707}
1708
1709static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1710 u8 *dst, unsigned int len)
1711{
1712 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1713}
1714
Herbert Xuf28776a2006-08-13 20:58:18 +10001715static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1716{
1717 return (struct crypto_cipher *)tfm;
1718}
1719
1720static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1721{
1722 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1723 return __crypto_cipher_cast(tfm);
1724}
1725
1726static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1727 u32 type, u32 mask)
1728{
1729 type &= ~CRYPTO_ALG_TYPE_MASK;
1730 type |= CRYPTO_ALG_TYPE_CIPHER;
1731 mask |= CRYPTO_ALG_TYPE_MASK;
1732
1733 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1734}
1735
1736static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1737{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001738 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001739}
1740
1741static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1742{
1743 crypto_free_tfm(crypto_cipher_tfm(tfm));
1744}
1745
Herbert Xufce32d72006-08-26 17:35:45 +10001746static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1747{
1748 type &= ~CRYPTO_ALG_TYPE_MASK;
1749 type |= CRYPTO_ALG_TYPE_CIPHER;
1750 mask |= CRYPTO_ALG_TYPE_MASK;
1751
1752 return crypto_has_alg(alg_name, type, mask);
1753}
1754
Herbert Xuf28776a2006-08-13 20:58:18 +10001755static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1756{
1757 return &crypto_cipher_tfm(tfm)->crt_cipher;
1758}
1759
1760static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1761{
1762 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1763}
1764
1765static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1766{
1767 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1768}
1769
1770static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1771{
1772 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1773}
1774
1775static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1776 u32 flags)
1777{
1778 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1779}
1780
1781static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1782 u32 flags)
1783{
1784 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1785}
1786
Herbert Xu7226bc872006-08-21 21:40:49 +10001787static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1788 const u8 *key, unsigned int keylen)
1789{
1790 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1791 key, keylen);
1792}
1793
Herbert Xuf28776a2006-08-13 20:58:18 +10001794static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1795 u8 *dst, const u8 *src)
1796{
1797 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1798 dst, src);
1799}
1800
1801static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1802 u8 *dst, const u8 *src)
1803{
1804 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1805 dst, src);
1806}
1807
Herbert Xu055bcee2006-08-19 22:24:23 +10001808static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Herbert Xu055bcee2006-08-19 22:24:23 +10001810 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Herbert Xu055bcee2006-08-19 22:24:23 +10001813static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Herbert Xu055bcee2006-08-19 22:24:23 +10001815 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1816 CRYPTO_ALG_TYPE_HASH_MASK);
1817 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Herbert Xu055bcee2006-08-19 22:24:23 +10001820static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1821 u32 type, u32 mask)
1822{
1823 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001824 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001825 type |= CRYPTO_ALG_TYPE_HASH;
1826 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1827
1828 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1829}
1830
1831static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1832{
1833 return &tfm->base;
1834}
1835
1836static inline void crypto_free_hash(struct crypto_hash *tfm)
1837{
1838 crypto_free_tfm(crypto_hash_tfm(tfm));
1839}
1840
Herbert Xufce32d72006-08-26 17:35:45 +10001841static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1842{
1843 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001844 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001845 type |= CRYPTO_ALG_TYPE_HASH;
1846 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1847
1848 return crypto_has_alg(alg_name, type, mask);
1849}
1850
Herbert Xu055bcee2006-08-19 22:24:23 +10001851static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1852{
1853 return &crypto_hash_tfm(tfm)->crt_hash;
1854}
1855
1856static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1857{
1858 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1859}
1860
1861static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1862{
1863 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1864}
1865
1866static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1867{
1868 return crypto_hash_crt(tfm)->digestsize;
1869}
1870
1871static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1872{
1873 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1874}
1875
1876static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1877{
1878 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1879}
1880
1881static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1882{
1883 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1884}
1885
1886static inline int crypto_hash_init(struct hash_desc *desc)
1887{
1888 return crypto_hash_crt(desc->tfm)->init(desc);
1889}
1890
1891static inline int crypto_hash_update(struct hash_desc *desc,
1892 struct scatterlist *sg,
1893 unsigned int nbytes)
1894{
1895 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1896}
1897
1898static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1899{
1900 return crypto_hash_crt(desc->tfm)->final(desc, out);
1901}
1902
1903static inline int crypto_hash_digest(struct hash_desc *desc,
1904 struct scatterlist *sg,
1905 unsigned int nbytes, u8 *out)
1906{
1907 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1908}
1909
1910static inline int crypto_hash_setkey(struct crypto_hash *hash,
1911 const u8 *key, unsigned int keylen)
1912{
1913 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Herbert Xufce32d72006-08-26 17:35:45 +10001916static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1917{
1918 return (struct crypto_comp *)tfm;
1919}
1920
1921static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1922{
1923 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1924 CRYPTO_ALG_TYPE_MASK);
1925 return __crypto_comp_cast(tfm);
1926}
1927
1928static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1929 u32 type, u32 mask)
1930{
1931 type &= ~CRYPTO_ALG_TYPE_MASK;
1932 type |= CRYPTO_ALG_TYPE_COMPRESS;
1933 mask |= CRYPTO_ALG_TYPE_MASK;
1934
1935 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1936}
1937
1938static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1939{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001940 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001941}
1942
1943static inline void crypto_free_comp(struct crypto_comp *tfm)
1944{
1945 crypto_free_tfm(crypto_comp_tfm(tfm));
1946}
1947
1948static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1949{
1950 type &= ~CRYPTO_ALG_TYPE_MASK;
1951 type |= CRYPTO_ALG_TYPE_COMPRESS;
1952 mask |= CRYPTO_ALG_TYPE_MASK;
1953
1954 return crypto_has_alg(alg_name, type, mask);
1955}
1956
Herbert Xue4d5b792006-08-26 18:12:40 +10001957static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1958{
1959 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1960}
1961
Herbert Xufce32d72006-08-26 17:35:45 +10001962static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1963{
1964 return &crypto_comp_tfm(tfm)->crt_compress;
1965}
1966
1967static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 const u8 *src, unsigned int slen,
1969 u8 *dst, unsigned int *dlen)
1970{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001971 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1972 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Herbert Xufce32d72006-08-26 17:35:45 +10001975static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 const u8 *src, unsigned int slen,
1977 u8 *dst, unsigned int *dlen)
1978{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001979 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1980 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983#endif /* _LINUX_CRYPTO_H */
1984