blob: 7ea0a4bc4ced07a54d313446916707113e599c06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Herbert Xu6521f302006-08-06 20:28:44 +100020#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/list.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
39#define CRYPTO_ALG_TYPE_HASH 0x00000009
40#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Herbert Xu055bcee2006-08-19 22:24:23 +100041
42#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080043#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080044#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Herbert Xu28259822006-08-06 21:23:26 +100046#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100047#define CRYPTO_ALG_DEAD 0x00000020
48#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100049#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
Herbert Xu60104392006-08-26 18:34:10 +100052 * Set this bit if and only if the algorithm requires another algorithm of
53 * the same type to handle corner cases.
54 */
55#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
56
57/*
Herbert Xuecfc4322007-12-05 21:08:36 +110058 * This bit is set for symmetric key ciphers that have already been wrapped
59 * with a generic IV generator to prevent them from being wrapped again.
60 */
61#define CRYPTO_ALG_GENIV 0x00000200
62
63/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * Transform masks and values (for crt_flags).
65 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define CRYPTO_TFM_REQ_MASK 0x000fff00
67#define CRYPTO_TFM_RES_MASK 0xfff00000
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070070#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110071#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
73#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
74#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
75#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
76#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
77
78/*
79 * Miscellaneous stuff.
80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define CRYPTO_MAX_ALG_NAME 64
82
Herbert Xu79911102006-08-21 21:03:52 +100083/*
84 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
85 * declaration) is used to ensure that the crypto_tfm context structure is
86 * aligned correctly for the given architecture so that there are no alignment
87 * faults for C data types. In particular, this is required on platforms such
88 * as arm where pointers are 32-bit aligned but there are data types such as
89 * u64 which require 64-bit alignment.
90 */
91#if defined(ARCH_KMALLOC_MINALIGN)
92#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
93#elif defined(ARCH_SLAB_MINALIGN)
94#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
Herbert Xu6eb72282008-01-08 17:16:44 +110095#else
96#define CRYPTO_MINALIGN __alignof__(unsigned long long)
Herbert Xu79911102006-08-21 21:03:52 +100097#endif
98
Herbert Xu79911102006-08-21 21:03:52 +100099#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100102struct crypto_ablkcipher;
103struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800104struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000105struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000106struct crypto_hash;
Loc Ho004a4032008-05-14 20:41:47 +0800107struct crypto_ahash;
Herbert Xu40725182005-07-06 13:51:52 -0700108struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000109struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800110struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800111struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700112
Herbert Xu32e39832007-03-24 14:35:34 +1100113typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
114
115struct crypto_async_request {
116 struct list_head list;
117 crypto_completion_t complete;
118 void *data;
119 struct crypto_tfm *tfm;
120
121 u32 flags;
122};
123
124struct ablkcipher_request {
125 struct crypto_async_request base;
126
127 unsigned int nbytes;
128
129 void *info;
130
131 struct scatterlist *src;
132 struct scatterlist *dst;
133
134 void *__ctx[] CRYPTO_MINALIGN_ATTR;
135};
136
Loc Ho004a4032008-05-14 20:41:47 +0800137struct ahash_request {
138 struct crypto_async_request base;
139
Loc Ho004a4032008-05-14 20:41:47 +0800140 unsigned int nbytes;
141 struct scatterlist *src;
142 u8 *result;
143
144 void *__ctx[] CRYPTO_MINALIGN_ATTR;
145};
146
Herbert Xu1ae97822007-08-30 15:36:14 +0800147/**
148 * struct aead_request - AEAD request
149 * @base: Common attributes for async crypto requests
150 * @assoclen: Length in bytes of associated data for authentication
151 * @cryptlen: Length of data to be encrypted or decrypted
152 * @iv: Initialisation vector
153 * @assoc: Associated data
154 * @src: Source data
155 * @dst: Destination data
156 * @__ctx: Start of private context data
157 */
158struct aead_request {
159 struct crypto_async_request base;
160
161 unsigned int assoclen;
162 unsigned int cryptlen;
163
164 u8 *iv;
165
166 struct scatterlist *assoc;
167 struct scatterlist *src;
168 struct scatterlist *dst;
169
170 void *__ctx[] CRYPTO_MINALIGN_ATTR;
171};
172
Herbert Xu5cde0af2006-08-22 00:07:53 +1000173struct blkcipher_desc {
174 struct crypto_blkcipher *tfm;
175 void *info;
176 u32 flags;
177};
178
Herbert Xu40725182005-07-06 13:51:52 -0700179struct cipher_desc {
180 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000181 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700182 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
183 const u8 *src, unsigned int nbytes);
184 void *info;
185};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Herbert Xu055bcee2006-08-19 22:24:23 +1000187struct hash_desc {
188 struct crypto_hash *tfm;
189 u32 flags;
190};
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * Algorithms: modular crypto algorithm implementations, managed
194 * via crypto_register_alg() and crypto_unregister_alg().
195 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000196struct ablkcipher_alg {
197 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
198 unsigned int keylen);
199 int (*encrypt)(struct ablkcipher_request *req);
200 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800201 int (*givencrypt)(struct skcipher_givcrypt_request *req);
202 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000203
Herbert Xu23508e12007-11-27 21:33:24 +0800204 const char *geniv;
205
Herbert Xub5b7f082007-04-16 20:48:54 +1000206 unsigned int min_keysize;
207 unsigned int max_keysize;
208 unsigned int ivsize;
209};
210
Loc Ho004a4032008-05-14 20:41:47 +0800211struct ahash_alg {
212 int (*init)(struct ahash_request *req);
213 int (*update)(struct ahash_request *req);
214 int (*final)(struct ahash_request *req);
215 int (*digest)(struct ahash_request *req);
216 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
217 unsigned int keylen);
218
219 unsigned int digestsize;
220};
221
Herbert Xu1ae97822007-08-30 15:36:14 +0800222struct aead_alg {
223 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
224 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100225 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800226 int (*encrypt)(struct aead_request *req);
227 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800228 int (*givencrypt)(struct aead_givcrypt_request *req);
229 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800230
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800231 const char *geniv;
232
Herbert Xu1ae97822007-08-30 15:36:14 +0800233 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100234 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800235};
236
Herbert Xu5cde0af2006-08-22 00:07:53 +1000237struct blkcipher_alg {
238 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
239 unsigned int keylen);
240 int (*encrypt)(struct blkcipher_desc *desc,
241 struct scatterlist *dst, struct scatterlist *src,
242 unsigned int nbytes);
243 int (*decrypt)(struct blkcipher_desc *desc,
244 struct scatterlist *dst, struct scatterlist *src,
245 unsigned int nbytes);
246
Herbert Xu23508e12007-11-27 21:33:24 +0800247 const char *geniv;
248
Herbert Xu5cde0af2006-08-22 00:07:53 +1000249 unsigned int min_keysize;
250 unsigned int max_keysize;
251 unsigned int ivsize;
252};
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254struct cipher_alg {
255 unsigned int cia_min_keysize;
256 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000257 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000258 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000259 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
260 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
263struct digest_alg {
264 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000265 void (*dia_init)(struct crypto_tfm *tfm);
266 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
267 unsigned int len);
268 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
269 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000270 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Herbert Xu055bcee2006-08-19 22:24:23 +1000273struct hash_alg {
274 int (*init)(struct hash_desc *desc);
275 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
276 unsigned int nbytes);
277 int (*final)(struct hash_desc *desc, u8 *out);
278 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
279 unsigned int nbytes, u8 *out);
280 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
281 unsigned int keylen);
282
283 unsigned int digestsize;
284};
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000287 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
288 unsigned int slen, u8 *dst, unsigned int *dlen);
289 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
290 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292
Herbert Xub5b7f082007-04-16 20:48:54 +1000293#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800294#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000295#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#define cra_cipher cra_u.cipher
297#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000298#define cra_hash cra_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800299#define cra_ahash cra_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#define cra_compress cra_u.compress
301
302struct crypto_alg {
303 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000304 struct list_head cra_users;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 u32 cra_flags;
307 unsigned int cra_blocksize;
308 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700309 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100310
311 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000312 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100313
Herbert Xud913ea02006-05-21 08:45:26 +1000314 char cra_name[CRYPTO_MAX_ALG_NAME];
315 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Herbert Xue853c3c2006-08-22 00:06:54 +1000317 const struct crypto_type *cra_type;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000320 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800321 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000322 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct cipher_alg cipher;
324 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000325 struct hash_alg hash;
Loc Ho004a4032008-05-14 20:41:47 +0800326 struct ahash_alg ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct compress_alg compress;
328 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000329
330 int (*cra_init)(struct crypto_tfm *tfm);
331 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000332 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 struct module *cra_module;
335};
336
337/*
338 * Algorithm registration interface.
339 */
340int crypto_register_alg(struct crypto_alg *alg);
341int crypto_unregister_alg(struct crypto_alg *alg);
342
343/*
344 * Algorithm query interface.
345 */
Herbert Xufce32d72006-08-26 17:35:45 +1000346int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348/*
349 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000350 * and core processing logic. Managed via crypto_alloc_*() and
351 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Herbert Xu32e39832007-03-24 14:35:34 +1100354struct ablkcipher_tfm {
355 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
356 unsigned int keylen);
357 int (*encrypt)(struct ablkcipher_request *req);
358 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800359 int (*givencrypt)(struct skcipher_givcrypt_request *req);
360 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
361
Herbert Xuecfc4322007-12-05 21:08:36 +1100362 struct crypto_ablkcipher *base;
363
Herbert Xu32e39832007-03-24 14:35:34 +1100364 unsigned int ivsize;
365 unsigned int reqsize;
366};
367
Herbert Xu1ae97822007-08-30 15:36:14 +0800368struct aead_tfm {
369 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
370 unsigned int keylen);
371 int (*encrypt)(struct aead_request *req);
372 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800373 int (*givencrypt)(struct aead_givcrypt_request *req);
374 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800375
376 struct crypto_aead *base;
377
Herbert Xu1ae97822007-08-30 15:36:14 +0800378 unsigned int ivsize;
379 unsigned int authsize;
380 unsigned int reqsize;
381};
382
Herbert Xu5cde0af2006-08-22 00:07:53 +1000383struct blkcipher_tfm {
384 void *iv;
385 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
386 unsigned int keylen);
387 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
388 struct scatterlist *src, unsigned int nbytes);
389 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
390 struct scatterlist *src, unsigned int nbytes);
391};
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int (*cit_setkey)(struct crypto_tfm *tfm,
395 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000396 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
397 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Herbert Xu055bcee2006-08-19 22:24:23 +1000400struct hash_tfm {
401 int (*init)(struct hash_desc *desc);
402 int (*update)(struct hash_desc *desc,
403 struct scatterlist *sg, unsigned int nsg);
404 int (*final)(struct hash_desc *desc, u8 *out);
405 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
406 unsigned int nsg, u8 *out);
407 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
408 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000409 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410};
411
Loc Ho004a4032008-05-14 20:41:47 +0800412struct ahash_tfm {
413 int (*init)(struct ahash_request *req);
414 int (*update)(struct ahash_request *req);
415 int (*final)(struct ahash_request *req);
416 int (*digest)(struct ahash_request *req);
417 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
418 unsigned int keylen);
419
420 unsigned int digestsize;
Loc Ho004a4032008-05-14 20:41:47 +0800421 unsigned int reqsize;
422};
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424struct compress_tfm {
425 int (*cot_compress)(struct crypto_tfm *tfm,
426 const u8 *src, unsigned int slen,
427 u8 *dst, unsigned int *dlen);
428 int (*cot_decompress)(struct crypto_tfm *tfm,
429 const u8 *src, unsigned int slen,
430 u8 *dst, unsigned int *dlen);
431};
432
Herbert Xu32e39832007-03-24 14:35:34 +1100433#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800434#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000435#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000437#define crt_hash crt_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800438#define crt_ahash crt_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#define crt_compress crt_u.compress
440
441struct crypto_tfm {
442
443 u32 crt_flags;
444
445 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100446 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800447 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000448 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000450 struct hash_tfm hash;
Loc Ho004a4032008-05-14 20:41:47 +0800451 struct ahash_tfm ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct compress_tfm compress;
453 } crt_u;
454
455 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100456
Herbert Xu79911102006-08-21 21:03:52 +1000457 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458};
459
Herbert Xu32e39832007-03-24 14:35:34 +1100460struct crypto_ablkcipher {
461 struct crypto_tfm base;
462};
463
Herbert Xu1ae97822007-08-30 15:36:14 +0800464struct crypto_aead {
465 struct crypto_tfm base;
466};
467
Herbert Xu5cde0af2006-08-22 00:07:53 +1000468struct crypto_blkcipher {
469 struct crypto_tfm base;
470};
471
Herbert Xu78a1fe42006-12-24 10:02:00 +1100472struct crypto_cipher {
473 struct crypto_tfm base;
474};
475
476struct crypto_comp {
477 struct crypto_tfm base;
478};
479
Herbert Xu055bcee2006-08-19 22:24:23 +1000480struct crypto_hash {
481 struct crypto_tfm base;
482};
483
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000484enum {
485 CRYPTOA_UNSPEC,
486 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100487 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800488 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100489 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000490};
491
Herbert Xuebc610e2007-01-01 18:37:02 +1100492#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
493
Herbert Xu39e1ee012007-08-29 19:27:26 +0800494/* Maximum number of (rtattr) parameters for each template. */
495#define CRYPTO_MAX_ATTRS 32
496
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000497struct crypto_attr_alg {
498 char name[CRYPTO_MAX_ALG_NAME];
499};
500
Herbert Xuebc610e2007-01-01 18:37:02 +1100501struct crypto_attr_type {
502 u32 type;
503 u32 mask;
504};
505
Herbert Xu39e1ee012007-08-29 19:27:26 +0800506struct crypto_attr_u32 {
507 u32 num;
508};
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/*
511 * Transform user interface.
512 */
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000515struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516void crypto_free_tfm(struct crypto_tfm *tfm);
517
Herbert Xuda7f0332008-07-31 17:08:25 +0800518int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
521 * Transform helpers which query the underlying algorithm.
522 */
523static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
524{
525 return tfm->__crt_alg->cra_name;
526}
527
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000528static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
529{
530 return tfm->__crt_alg->cra_driver_name;
531}
532
533static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
534{
535 return tfm->__crt_alg->cra_priority;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
539{
540 return module_name(tfm->__crt_alg->cra_module);
541}
542
543static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
544{
545 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
549{
550 return tfm->__crt_alg->cra_blocksize;
551}
552
Herbert Xufbdae9f2005-07-06 13:53:29 -0700553static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
554{
555 return tfm->__crt_alg->cra_alignmask;
556}
557
Herbert Xuf28776a2006-08-13 20:58:18 +1000558static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
559{
560 return tfm->crt_flags;
561}
562
563static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
564{
565 tfm->crt_flags |= flags;
566}
567
568static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
569{
570 tfm->crt_flags &= ~flags;
571}
572
Herbert Xu40725182005-07-06 13:51:52 -0700573static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
574{
Herbert Xuf10b7892006-01-25 22:34:01 +1100575 return tfm->__crt_ctx;
576}
577
578static inline unsigned int crypto_tfm_ctx_alignment(void)
579{
580 struct crypto_tfm *tfm;
581 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/*
585 * API wrappers.
586 */
Herbert Xu32e39832007-03-24 14:35:34 +1100587static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
588 struct crypto_tfm *tfm)
589{
590 return (struct crypto_ablkcipher *)tfm;
591}
592
Herbert Xu378f4f52007-12-17 20:07:31 +0800593static inline u32 crypto_skcipher_type(u32 type)
594{
Herbert Xuecfc4322007-12-05 21:08:36 +1100595 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800596 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
597 return type;
598}
599
600static inline u32 crypto_skcipher_mask(u32 mask)
601{
Herbert Xuecfc4322007-12-05 21:08:36 +1100602 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800603 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
604 return mask;
605}
606
Herbert Xub9c55aa2007-12-04 12:46:48 +1100607struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
608 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100609
610static inline struct crypto_tfm *crypto_ablkcipher_tfm(
611 struct crypto_ablkcipher *tfm)
612{
613 return &tfm->base;
614}
615
616static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
617{
618 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
619}
620
621static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
622 u32 mask)
623{
Herbert Xu378f4f52007-12-17 20:07:31 +0800624 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
625 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100626}
627
628static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
629 struct crypto_ablkcipher *tfm)
630{
631 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
632}
633
634static inline unsigned int crypto_ablkcipher_ivsize(
635 struct crypto_ablkcipher *tfm)
636{
637 return crypto_ablkcipher_crt(tfm)->ivsize;
638}
639
640static inline unsigned int crypto_ablkcipher_blocksize(
641 struct crypto_ablkcipher *tfm)
642{
643 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
644}
645
646static inline unsigned int crypto_ablkcipher_alignmask(
647 struct crypto_ablkcipher *tfm)
648{
649 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
650}
651
652static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
653{
654 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
655}
656
657static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
658 u32 flags)
659{
660 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
661}
662
663static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
664 u32 flags)
665{
666 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
667}
668
669static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
670 const u8 *key, unsigned int keylen)
671{
Herbert Xuecfc4322007-12-05 21:08:36 +1100672 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
673
674 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100675}
676
677static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
678 struct ablkcipher_request *req)
679{
680 return __crypto_ablkcipher_cast(req->base.tfm);
681}
682
683static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
684{
685 struct ablkcipher_tfm *crt =
686 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
687 return crt->encrypt(req);
688}
689
690static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
691{
692 struct ablkcipher_tfm *crt =
693 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
694 return crt->decrypt(req);
695}
696
Herbert Xub16c3a22007-08-29 19:02:04 +0800697static inline unsigned int crypto_ablkcipher_reqsize(
698 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100699{
700 return crypto_ablkcipher_crt(tfm)->reqsize;
701}
702
Herbert Xue196d622007-04-14 16:09:14 +1000703static inline void ablkcipher_request_set_tfm(
704 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
705{
Herbert Xuecfc4322007-12-05 21:08:36 +1100706 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000707}
708
Herbert Xub5b7f082007-04-16 20:48:54 +1000709static inline struct ablkcipher_request *ablkcipher_request_cast(
710 struct crypto_async_request *req)
711{
712 return container_of(req, struct ablkcipher_request, base);
713}
714
Herbert Xu32e39832007-03-24 14:35:34 +1100715static inline struct ablkcipher_request *ablkcipher_request_alloc(
716 struct crypto_ablkcipher *tfm, gfp_t gfp)
717{
718 struct ablkcipher_request *req;
719
720 req = kmalloc(sizeof(struct ablkcipher_request) +
721 crypto_ablkcipher_reqsize(tfm), gfp);
722
723 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000724 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100725
726 return req;
727}
728
729static inline void ablkcipher_request_free(struct ablkcipher_request *req)
730{
731 kfree(req);
732}
733
734static inline void ablkcipher_request_set_callback(
735 struct ablkcipher_request *req,
736 u32 flags, crypto_completion_t complete, void *data)
737{
738 req->base.complete = complete;
739 req->base.data = data;
740 req->base.flags = flags;
741}
742
743static inline void ablkcipher_request_set_crypt(
744 struct ablkcipher_request *req,
745 struct scatterlist *src, struct scatterlist *dst,
746 unsigned int nbytes, void *iv)
747{
748 req->src = src;
749 req->dst = dst;
750 req->nbytes = nbytes;
751 req->info = iv;
752}
753
Herbert Xu1ae97822007-08-30 15:36:14 +0800754static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
755{
756 return (struct crypto_aead *)tfm;
757}
758
Herbert Xud29ce982007-12-12 19:24:27 +0800759struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800760
761static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
762{
763 return &tfm->base;
764}
765
766static inline void crypto_free_aead(struct crypto_aead *tfm)
767{
768 crypto_free_tfm(crypto_aead_tfm(tfm));
769}
770
771static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
772{
773 return &crypto_aead_tfm(tfm)->crt_aead;
774}
775
776static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
777{
778 return crypto_aead_crt(tfm)->ivsize;
779}
780
781static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
782{
783 return crypto_aead_crt(tfm)->authsize;
784}
785
786static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
787{
788 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
789}
790
791static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
792{
793 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
794}
795
796static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
797{
798 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
799}
800
801static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
802{
803 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
804}
805
806static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
807{
808 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
809}
810
811static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
812 unsigned int keylen)
813{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800814 struct aead_tfm *crt = crypto_aead_crt(tfm);
815
816 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800817}
818
Herbert Xu7ba683a2007-12-02 18:49:21 +1100819int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
820
Herbert Xu1ae97822007-08-30 15:36:14 +0800821static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
822{
823 return __crypto_aead_cast(req->base.tfm);
824}
825
826static inline int crypto_aead_encrypt(struct aead_request *req)
827{
828 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
829}
830
831static inline int crypto_aead_decrypt(struct aead_request *req)
832{
833 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
834}
835
Herbert Xub16c3a22007-08-29 19:02:04 +0800836static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800837{
838 return crypto_aead_crt(tfm)->reqsize;
839}
840
841static inline void aead_request_set_tfm(struct aead_request *req,
842 struct crypto_aead *tfm)
843{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800844 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800845}
846
847static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
848 gfp_t gfp)
849{
850 struct aead_request *req;
851
852 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
853
854 if (likely(req))
855 aead_request_set_tfm(req, tfm);
856
857 return req;
858}
859
860static inline void aead_request_free(struct aead_request *req)
861{
862 kfree(req);
863}
864
865static inline void aead_request_set_callback(struct aead_request *req,
866 u32 flags,
867 crypto_completion_t complete,
868 void *data)
869{
870 req->base.complete = complete;
871 req->base.data = data;
872 req->base.flags = flags;
873}
874
875static inline void aead_request_set_crypt(struct aead_request *req,
876 struct scatterlist *src,
877 struct scatterlist *dst,
878 unsigned int cryptlen, u8 *iv)
879{
880 req->src = src;
881 req->dst = dst;
882 req->cryptlen = cryptlen;
883 req->iv = iv;
884}
885
886static inline void aead_request_set_assoc(struct aead_request *req,
887 struct scatterlist *assoc,
888 unsigned int assoclen)
889{
890 req->assoc = assoc;
891 req->assoclen = assoclen;
892}
893
Herbert Xu5cde0af2006-08-22 00:07:53 +1000894static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
895 struct crypto_tfm *tfm)
896{
897 return (struct crypto_blkcipher *)tfm;
898}
899
900static inline struct crypto_blkcipher *crypto_blkcipher_cast(
901 struct crypto_tfm *tfm)
902{
903 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
904 return __crypto_blkcipher_cast(tfm);
905}
906
907static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
908 const char *alg_name, u32 type, u32 mask)
909{
Herbert Xu332f88402007-11-15 22:36:07 +0800910 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000911 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800912 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000913
914 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
915}
916
917static inline struct crypto_tfm *crypto_blkcipher_tfm(
918 struct crypto_blkcipher *tfm)
919{
920 return &tfm->base;
921}
922
923static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
924{
925 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
926}
927
Herbert Xufce32d72006-08-26 17:35:45 +1000928static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
929{
Herbert Xu332f88402007-11-15 22:36:07 +0800930 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000931 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800932 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000933
934 return crypto_has_alg(alg_name, type, mask);
935}
936
Herbert Xu5cde0af2006-08-22 00:07:53 +1000937static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
938{
939 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
940}
941
942static inline struct blkcipher_tfm *crypto_blkcipher_crt(
943 struct crypto_blkcipher *tfm)
944{
945 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
946}
947
948static inline struct blkcipher_alg *crypto_blkcipher_alg(
949 struct crypto_blkcipher *tfm)
950{
951 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
952}
953
954static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
955{
956 return crypto_blkcipher_alg(tfm)->ivsize;
957}
958
959static inline unsigned int crypto_blkcipher_blocksize(
960 struct crypto_blkcipher *tfm)
961{
962 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
963}
964
965static inline unsigned int crypto_blkcipher_alignmask(
966 struct crypto_blkcipher *tfm)
967{
968 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
969}
970
971static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
972{
973 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
974}
975
976static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
977 u32 flags)
978{
979 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
980}
981
982static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
983 u32 flags)
984{
985 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
986}
987
988static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
989 const u8 *key, unsigned int keylen)
990{
991 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
992 key, keylen);
993}
994
995static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
996 struct scatterlist *dst,
997 struct scatterlist *src,
998 unsigned int nbytes)
999{
1000 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1001 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1002}
1003
1004static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1005 struct scatterlist *dst,
1006 struct scatterlist *src,
1007 unsigned int nbytes)
1008{
1009 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1010}
1011
1012static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1013 struct scatterlist *dst,
1014 struct scatterlist *src,
1015 unsigned int nbytes)
1016{
1017 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1018 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1019}
1020
1021static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1022 struct scatterlist *dst,
1023 struct scatterlist *src,
1024 unsigned int nbytes)
1025{
1026 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1027}
1028
1029static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1030 const u8 *src, unsigned int len)
1031{
1032 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1033}
1034
1035static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1036 u8 *dst, unsigned int len)
1037{
1038 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1039}
1040
Herbert Xuf28776a2006-08-13 20:58:18 +10001041static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1042{
1043 return (struct crypto_cipher *)tfm;
1044}
1045
1046static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1047{
1048 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1049 return __crypto_cipher_cast(tfm);
1050}
1051
1052static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1053 u32 type, u32 mask)
1054{
1055 type &= ~CRYPTO_ALG_TYPE_MASK;
1056 type |= CRYPTO_ALG_TYPE_CIPHER;
1057 mask |= CRYPTO_ALG_TYPE_MASK;
1058
1059 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1060}
1061
1062static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1063{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001064 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001065}
1066
1067static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1068{
1069 crypto_free_tfm(crypto_cipher_tfm(tfm));
1070}
1071
Herbert Xufce32d72006-08-26 17:35:45 +10001072static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1073{
1074 type &= ~CRYPTO_ALG_TYPE_MASK;
1075 type |= CRYPTO_ALG_TYPE_CIPHER;
1076 mask |= CRYPTO_ALG_TYPE_MASK;
1077
1078 return crypto_has_alg(alg_name, type, mask);
1079}
1080
Herbert Xuf28776a2006-08-13 20:58:18 +10001081static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1082{
1083 return &crypto_cipher_tfm(tfm)->crt_cipher;
1084}
1085
1086static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1087{
1088 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1089}
1090
1091static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1092{
1093 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1094}
1095
1096static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1097{
1098 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1099}
1100
1101static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1102 u32 flags)
1103{
1104 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1105}
1106
1107static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1108 u32 flags)
1109{
1110 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1111}
1112
Herbert Xu7226bc872006-08-21 21:40:49 +10001113static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1114 const u8 *key, unsigned int keylen)
1115{
1116 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1117 key, keylen);
1118}
1119
Herbert Xuf28776a2006-08-13 20:58:18 +10001120static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1121 u8 *dst, const u8 *src)
1122{
1123 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1124 dst, src);
1125}
1126
1127static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1128 u8 *dst, const u8 *src)
1129{
1130 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1131 dst, src);
1132}
1133
Herbert Xu055bcee2006-08-19 22:24:23 +10001134static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Herbert Xu055bcee2006-08-19 22:24:23 +10001136 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Herbert Xu055bcee2006-08-19 22:24:23 +10001139static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Herbert Xu055bcee2006-08-19 22:24:23 +10001141 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1142 CRYPTO_ALG_TYPE_HASH_MASK);
1143 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Herbert Xu055bcee2006-08-19 22:24:23 +10001146static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1147 u32 type, u32 mask)
1148{
1149 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001150 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001151 type |= CRYPTO_ALG_TYPE_HASH;
1152 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1153
1154 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1155}
1156
1157static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1158{
1159 return &tfm->base;
1160}
1161
1162static inline void crypto_free_hash(struct crypto_hash *tfm)
1163{
1164 crypto_free_tfm(crypto_hash_tfm(tfm));
1165}
1166
Herbert Xufce32d72006-08-26 17:35:45 +10001167static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1168{
1169 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001170 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001171 type |= CRYPTO_ALG_TYPE_HASH;
1172 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1173
1174 return crypto_has_alg(alg_name, type, mask);
1175}
1176
Herbert Xu055bcee2006-08-19 22:24:23 +10001177static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1178{
1179 return &crypto_hash_tfm(tfm)->crt_hash;
1180}
1181
1182static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1183{
1184 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1185}
1186
1187static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1188{
1189 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1190}
1191
1192static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1193{
1194 return crypto_hash_crt(tfm)->digestsize;
1195}
1196
1197static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1198{
1199 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1200}
1201
1202static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1203{
1204 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1205}
1206
1207static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1208{
1209 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1210}
1211
1212static inline int crypto_hash_init(struct hash_desc *desc)
1213{
1214 return crypto_hash_crt(desc->tfm)->init(desc);
1215}
1216
1217static inline int crypto_hash_update(struct hash_desc *desc,
1218 struct scatterlist *sg,
1219 unsigned int nbytes)
1220{
1221 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1222}
1223
1224static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1225{
1226 return crypto_hash_crt(desc->tfm)->final(desc, out);
1227}
1228
1229static inline int crypto_hash_digest(struct hash_desc *desc,
1230 struct scatterlist *sg,
1231 unsigned int nbytes, u8 *out)
1232{
1233 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1234}
1235
1236static inline int crypto_hash_setkey(struct crypto_hash *hash,
1237 const u8 *key, unsigned int keylen)
1238{
1239 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Herbert Xufce32d72006-08-26 17:35:45 +10001242static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1243{
1244 return (struct crypto_comp *)tfm;
1245}
1246
1247static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1248{
1249 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1250 CRYPTO_ALG_TYPE_MASK);
1251 return __crypto_comp_cast(tfm);
1252}
1253
1254static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1255 u32 type, u32 mask)
1256{
1257 type &= ~CRYPTO_ALG_TYPE_MASK;
1258 type |= CRYPTO_ALG_TYPE_COMPRESS;
1259 mask |= CRYPTO_ALG_TYPE_MASK;
1260
1261 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1262}
1263
1264static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1265{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001266 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001267}
1268
1269static inline void crypto_free_comp(struct crypto_comp *tfm)
1270{
1271 crypto_free_tfm(crypto_comp_tfm(tfm));
1272}
1273
1274static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1275{
1276 type &= ~CRYPTO_ALG_TYPE_MASK;
1277 type |= CRYPTO_ALG_TYPE_COMPRESS;
1278 mask |= CRYPTO_ALG_TYPE_MASK;
1279
1280 return crypto_has_alg(alg_name, type, mask);
1281}
1282
Herbert Xue4d5b792006-08-26 18:12:40 +10001283static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1284{
1285 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1286}
1287
Herbert Xufce32d72006-08-26 17:35:45 +10001288static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1289{
1290 return &crypto_comp_tfm(tfm)->crt_compress;
1291}
1292
1293static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 const u8 *src, unsigned int slen,
1295 u8 *dst, unsigned int *dlen)
1296{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001297 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1298 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Herbert Xufce32d72006-08-26 17:35:45 +10001301static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 const u8 *src, unsigned int slen,
1303 u8 *dst, unsigned int *dlen)
1304{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001305 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1306 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309#endif /* _LINUX_CRYPTO_H */
1310