blob: b6efe569128d8cf5a786100346a65789ad8fdd08 [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
140 void *info;
141
142 unsigned int nbytes;
143 struct scatterlist *src;
144 u8 *result;
145
146 void *__ctx[] CRYPTO_MINALIGN_ATTR;
147};
148
Herbert Xu1ae97822007-08-30 15:36:14 +0800149/**
150 * struct aead_request - AEAD request
151 * @base: Common attributes for async crypto requests
152 * @assoclen: Length in bytes of associated data for authentication
153 * @cryptlen: Length of data to be encrypted or decrypted
154 * @iv: Initialisation vector
155 * @assoc: Associated data
156 * @src: Source data
157 * @dst: Destination data
158 * @__ctx: Start of private context data
159 */
160struct aead_request {
161 struct crypto_async_request base;
162
163 unsigned int assoclen;
164 unsigned int cryptlen;
165
166 u8 *iv;
167
168 struct scatterlist *assoc;
169 struct scatterlist *src;
170 struct scatterlist *dst;
171
172 void *__ctx[] CRYPTO_MINALIGN_ATTR;
173};
174
Herbert Xu5cde0af2006-08-22 00:07:53 +1000175struct blkcipher_desc {
176 struct crypto_blkcipher *tfm;
177 void *info;
178 u32 flags;
179};
180
Herbert Xu40725182005-07-06 13:51:52 -0700181struct cipher_desc {
182 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000183 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700184 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
185 const u8 *src, unsigned int nbytes);
186 void *info;
187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Herbert Xu055bcee2006-08-19 22:24:23 +1000189struct hash_desc {
190 struct crypto_hash *tfm;
191 u32 flags;
192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * Algorithms: modular crypto algorithm implementations, managed
196 * via crypto_register_alg() and crypto_unregister_alg().
197 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000198struct ablkcipher_alg {
199 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
200 unsigned int keylen);
201 int (*encrypt)(struct ablkcipher_request *req);
202 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800203 int (*givencrypt)(struct skcipher_givcrypt_request *req);
204 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000205
Herbert Xu23508e12007-11-27 21:33:24 +0800206 const char *geniv;
207
Herbert Xub5b7f082007-04-16 20:48:54 +1000208 unsigned int min_keysize;
209 unsigned int max_keysize;
210 unsigned int ivsize;
211};
212
Loc Ho004a4032008-05-14 20:41:47 +0800213struct ahash_alg {
214 int (*init)(struct ahash_request *req);
215 int (*update)(struct ahash_request *req);
216 int (*final)(struct ahash_request *req);
217 int (*digest)(struct ahash_request *req);
218 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
219 unsigned int keylen);
220
221 unsigned int digestsize;
222};
223
Herbert Xu1ae97822007-08-30 15:36:14 +0800224struct aead_alg {
225 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
226 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100227 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800228 int (*encrypt)(struct aead_request *req);
229 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800230 int (*givencrypt)(struct aead_givcrypt_request *req);
231 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800232
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800233 const char *geniv;
234
Herbert Xu1ae97822007-08-30 15:36:14 +0800235 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100236 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800237};
238
Herbert Xu5cde0af2006-08-22 00:07:53 +1000239struct blkcipher_alg {
240 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
241 unsigned int keylen);
242 int (*encrypt)(struct blkcipher_desc *desc,
243 struct scatterlist *dst, struct scatterlist *src,
244 unsigned int nbytes);
245 int (*decrypt)(struct blkcipher_desc *desc,
246 struct scatterlist *dst, struct scatterlist *src,
247 unsigned int nbytes);
248
Herbert Xu23508e12007-11-27 21:33:24 +0800249 const char *geniv;
250
Herbert Xu5cde0af2006-08-22 00:07:53 +1000251 unsigned int min_keysize;
252 unsigned int max_keysize;
253 unsigned int ivsize;
254};
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256struct cipher_alg {
257 unsigned int cia_min_keysize;
258 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000259 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000260 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000261 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
262 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
265struct digest_alg {
266 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000267 void (*dia_init)(struct crypto_tfm *tfm);
268 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
269 unsigned int len);
270 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
271 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000272 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
Herbert Xu055bcee2006-08-19 22:24:23 +1000275struct hash_alg {
276 int (*init)(struct hash_desc *desc);
277 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
278 unsigned int nbytes);
279 int (*final)(struct hash_desc *desc, u8 *out);
280 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
281 unsigned int nbytes, u8 *out);
282 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
283 unsigned int keylen);
284
285 unsigned int digestsize;
286};
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000289 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
290 unsigned int slen, u8 *dst, unsigned int *dlen);
291 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
292 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Herbert Xub5b7f082007-04-16 20:48:54 +1000295#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800296#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000297#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#define cra_cipher cra_u.cipher
299#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000300#define cra_hash cra_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800301#define cra_ahash cra_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#define cra_compress cra_u.compress
303
304struct crypto_alg {
305 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000306 struct list_head cra_users;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 u32 cra_flags;
309 unsigned int cra_blocksize;
310 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700311 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100312
313 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000314 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100315
Herbert Xud913ea02006-05-21 08:45:26 +1000316 char cra_name[CRYPTO_MAX_ALG_NAME];
317 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Herbert Xue853c3c2006-08-22 00:06:54 +1000319 const struct crypto_type *cra_type;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000322 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800323 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000324 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct cipher_alg cipher;
326 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000327 struct hash_alg hash;
Loc Ho004a4032008-05-14 20:41:47 +0800328 struct ahash_alg ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct compress_alg compress;
330 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000331
332 int (*cra_init)(struct crypto_tfm *tfm);
333 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000334 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 struct module *cra_module;
337};
338
339/*
340 * Algorithm registration interface.
341 */
342int crypto_register_alg(struct crypto_alg *alg);
343int crypto_unregister_alg(struct crypto_alg *alg);
344
345/*
346 * Algorithm query interface.
347 */
Herbert Xufce32d72006-08-26 17:35:45 +1000348int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350/*
351 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000352 * and core processing logic. Managed via crypto_alloc_*() and
353 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Herbert Xu32e39832007-03-24 14:35:34 +1100356struct ablkcipher_tfm {
357 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
358 unsigned int keylen);
359 int (*encrypt)(struct ablkcipher_request *req);
360 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800361 int (*givencrypt)(struct skcipher_givcrypt_request *req);
362 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
363
Herbert Xuecfc4322007-12-05 21:08:36 +1100364 struct crypto_ablkcipher *base;
365
Herbert Xu32e39832007-03-24 14:35:34 +1100366 unsigned int ivsize;
367 unsigned int reqsize;
368};
369
Herbert Xu1ae97822007-08-30 15:36:14 +0800370struct aead_tfm {
371 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
372 unsigned int keylen);
373 int (*encrypt)(struct aead_request *req);
374 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800375 int (*givencrypt)(struct aead_givcrypt_request *req);
376 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800377
378 struct crypto_aead *base;
379
Herbert Xu1ae97822007-08-30 15:36:14 +0800380 unsigned int ivsize;
381 unsigned int authsize;
382 unsigned int reqsize;
383};
384
Herbert Xu5cde0af2006-08-22 00:07:53 +1000385struct blkcipher_tfm {
386 void *iv;
387 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
388 unsigned int keylen);
389 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
390 struct scatterlist *src, unsigned int nbytes);
391 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
392 struct scatterlist *src, unsigned int nbytes);
393};
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int (*cit_setkey)(struct crypto_tfm *tfm,
397 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000398 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
399 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Herbert Xu055bcee2006-08-19 22:24:23 +1000402struct hash_tfm {
403 int (*init)(struct hash_desc *desc);
404 int (*update)(struct hash_desc *desc,
405 struct scatterlist *sg, unsigned int nsg);
406 int (*final)(struct hash_desc *desc, u8 *out);
407 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
408 unsigned int nsg, u8 *out);
409 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
410 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000411 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412};
413
Loc Ho004a4032008-05-14 20:41:47 +0800414struct ahash_tfm {
415 int (*init)(struct ahash_request *req);
416 int (*update)(struct ahash_request *req);
417 int (*final)(struct ahash_request *req);
418 int (*digest)(struct ahash_request *req);
419 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
420 unsigned int keylen);
421
422 unsigned int digestsize;
423 struct crypto_ahash *base;
424 unsigned int reqsize;
425};
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427struct compress_tfm {
428 int (*cot_compress)(struct crypto_tfm *tfm,
429 const u8 *src, unsigned int slen,
430 u8 *dst, unsigned int *dlen);
431 int (*cot_decompress)(struct crypto_tfm *tfm,
432 const u8 *src, unsigned int slen,
433 u8 *dst, unsigned int *dlen);
434};
435
Herbert Xu32e39832007-03-24 14:35:34 +1100436#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800437#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000438#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000440#define crt_hash crt_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800441#define crt_ahash crt_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#define crt_compress crt_u.compress
443
444struct crypto_tfm {
445
446 u32 crt_flags;
447
448 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100449 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800450 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000451 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000453 struct hash_tfm hash;
Loc Ho004a4032008-05-14 20:41:47 +0800454 struct ahash_tfm ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct compress_tfm compress;
456 } crt_u;
457
458 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100459
Herbert Xu79911102006-08-21 21:03:52 +1000460 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
Herbert Xu32e39832007-03-24 14:35:34 +1100463struct crypto_ablkcipher {
464 struct crypto_tfm base;
465};
466
Herbert Xu1ae97822007-08-30 15:36:14 +0800467struct crypto_aead {
468 struct crypto_tfm base;
469};
470
Herbert Xu5cde0af2006-08-22 00:07:53 +1000471struct crypto_blkcipher {
472 struct crypto_tfm base;
473};
474
Herbert Xu78a1fe42006-12-24 10:02:00 +1100475struct crypto_cipher {
476 struct crypto_tfm base;
477};
478
479struct crypto_comp {
480 struct crypto_tfm base;
481};
482
Herbert Xu055bcee2006-08-19 22:24:23 +1000483struct crypto_hash {
484 struct crypto_tfm base;
485};
486
Loc Ho004a4032008-05-14 20:41:47 +0800487struct crypto_ahash {
488 struct crypto_tfm base;
489};
490
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000491enum {
492 CRYPTOA_UNSPEC,
493 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100494 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800495 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100496 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000497};
498
Herbert Xuebc610e2007-01-01 18:37:02 +1100499#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
500
Herbert Xu39e1ee012007-08-29 19:27:26 +0800501/* Maximum number of (rtattr) parameters for each template. */
502#define CRYPTO_MAX_ATTRS 32
503
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000504struct crypto_attr_alg {
505 char name[CRYPTO_MAX_ALG_NAME];
506};
507
Herbert Xuebc610e2007-01-01 18:37:02 +1100508struct crypto_attr_type {
509 u32 type;
510 u32 mask;
511};
512
Herbert Xu39e1ee012007-08-29 19:27:26 +0800513struct crypto_attr_u32 {
514 u32 num;
515};
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/*
518 * Transform user interface.
519 */
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000522struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523void crypto_free_tfm(struct crypto_tfm *tfm);
524
525/*
526 * Transform helpers which query the underlying algorithm.
527 */
528static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
529{
530 return tfm->__crt_alg->cra_name;
531}
532
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000533static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
534{
535 return tfm->__crt_alg->cra_driver_name;
536}
537
538static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
539{
540 return tfm->__crt_alg->cra_priority;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
544{
545 return module_name(tfm->__crt_alg->cra_module);
546}
547
548static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
549{
550 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
554{
555 return tfm->__crt_alg->cra_blocksize;
556}
557
Herbert Xufbdae9f2005-07-06 13:53:29 -0700558static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
559{
560 return tfm->__crt_alg->cra_alignmask;
561}
562
Herbert Xuf28776a2006-08-13 20:58:18 +1000563static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
564{
565 return tfm->crt_flags;
566}
567
568static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
569{
570 tfm->crt_flags |= flags;
571}
572
573static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
574{
575 tfm->crt_flags &= ~flags;
576}
577
Herbert Xu40725182005-07-06 13:51:52 -0700578static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
579{
Herbert Xuf10b7892006-01-25 22:34:01 +1100580 return tfm->__crt_ctx;
581}
582
583static inline unsigned int crypto_tfm_ctx_alignment(void)
584{
585 struct crypto_tfm *tfm;
586 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
590 * API wrappers.
591 */
Herbert Xu32e39832007-03-24 14:35:34 +1100592static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
593 struct crypto_tfm *tfm)
594{
595 return (struct crypto_ablkcipher *)tfm;
596}
597
Herbert Xu378f4f52007-12-17 20:07:31 +0800598static inline u32 crypto_skcipher_type(u32 type)
599{
Herbert Xuecfc4322007-12-05 21:08:36 +1100600 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800601 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
602 return type;
603}
604
605static inline u32 crypto_skcipher_mask(u32 mask)
606{
Herbert Xuecfc4322007-12-05 21:08:36 +1100607 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800608 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
609 return mask;
610}
611
Herbert Xub9c55aa2007-12-04 12:46:48 +1100612struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
613 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100614
615static inline struct crypto_tfm *crypto_ablkcipher_tfm(
616 struct crypto_ablkcipher *tfm)
617{
618 return &tfm->base;
619}
620
621static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
622{
623 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
624}
625
626static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
627 u32 mask)
628{
Herbert Xu378f4f52007-12-17 20:07:31 +0800629 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
630 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100631}
632
633static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
634 struct crypto_ablkcipher *tfm)
635{
636 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
637}
638
639static inline unsigned int crypto_ablkcipher_ivsize(
640 struct crypto_ablkcipher *tfm)
641{
642 return crypto_ablkcipher_crt(tfm)->ivsize;
643}
644
645static inline unsigned int crypto_ablkcipher_blocksize(
646 struct crypto_ablkcipher *tfm)
647{
648 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
649}
650
651static inline unsigned int crypto_ablkcipher_alignmask(
652 struct crypto_ablkcipher *tfm)
653{
654 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
655}
656
657static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
658{
659 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
660}
661
662static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
663 u32 flags)
664{
665 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
666}
667
668static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
669 u32 flags)
670{
671 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
672}
673
674static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
675 const u8 *key, unsigned int keylen)
676{
Herbert Xuecfc4322007-12-05 21:08:36 +1100677 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
678
679 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100680}
681
682static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
683 struct ablkcipher_request *req)
684{
685 return __crypto_ablkcipher_cast(req->base.tfm);
686}
687
688static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
689{
690 struct ablkcipher_tfm *crt =
691 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
692 return crt->encrypt(req);
693}
694
695static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
696{
697 struct ablkcipher_tfm *crt =
698 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
699 return crt->decrypt(req);
700}
701
Herbert Xub16c3a22007-08-29 19:02:04 +0800702static inline unsigned int crypto_ablkcipher_reqsize(
703 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100704{
705 return crypto_ablkcipher_crt(tfm)->reqsize;
706}
707
Herbert Xue196d622007-04-14 16:09:14 +1000708static inline void ablkcipher_request_set_tfm(
709 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
710{
Herbert Xuecfc4322007-12-05 21:08:36 +1100711 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000712}
713
Herbert Xub5b7f082007-04-16 20:48:54 +1000714static inline struct ablkcipher_request *ablkcipher_request_cast(
715 struct crypto_async_request *req)
716{
717 return container_of(req, struct ablkcipher_request, base);
718}
719
Herbert Xu32e39832007-03-24 14:35:34 +1100720static inline struct ablkcipher_request *ablkcipher_request_alloc(
721 struct crypto_ablkcipher *tfm, gfp_t gfp)
722{
723 struct ablkcipher_request *req;
724
725 req = kmalloc(sizeof(struct ablkcipher_request) +
726 crypto_ablkcipher_reqsize(tfm), gfp);
727
728 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000729 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100730
731 return req;
732}
733
734static inline void ablkcipher_request_free(struct ablkcipher_request *req)
735{
736 kfree(req);
737}
738
739static inline void ablkcipher_request_set_callback(
740 struct ablkcipher_request *req,
741 u32 flags, crypto_completion_t complete, void *data)
742{
743 req->base.complete = complete;
744 req->base.data = data;
745 req->base.flags = flags;
746}
747
748static inline void ablkcipher_request_set_crypt(
749 struct ablkcipher_request *req,
750 struct scatterlist *src, struct scatterlist *dst,
751 unsigned int nbytes, void *iv)
752{
753 req->src = src;
754 req->dst = dst;
755 req->nbytes = nbytes;
756 req->info = iv;
757}
758
Herbert Xu1ae97822007-08-30 15:36:14 +0800759static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
760{
761 return (struct crypto_aead *)tfm;
762}
763
Herbert Xud29ce982007-12-12 19:24:27 +0800764struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800765
766static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
767{
768 return &tfm->base;
769}
770
771static inline void crypto_free_aead(struct crypto_aead *tfm)
772{
773 crypto_free_tfm(crypto_aead_tfm(tfm));
774}
775
776static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
777{
778 return &crypto_aead_tfm(tfm)->crt_aead;
779}
780
781static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
782{
783 return crypto_aead_crt(tfm)->ivsize;
784}
785
786static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
787{
788 return crypto_aead_crt(tfm)->authsize;
789}
790
791static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
792{
793 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
794}
795
796static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
797{
798 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
799}
800
801static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
802{
803 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
804}
805
806static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
807{
808 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
809}
810
811static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
812{
813 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
814}
815
816static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
817 unsigned int keylen)
818{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800819 struct aead_tfm *crt = crypto_aead_crt(tfm);
820
821 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800822}
823
Herbert Xu7ba683a2007-12-02 18:49:21 +1100824int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
825
Herbert Xu1ae97822007-08-30 15:36:14 +0800826static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
827{
828 return __crypto_aead_cast(req->base.tfm);
829}
830
831static inline int crypto_aead_encrypt(struct aead_request *req)
832{
833 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
834}
835
836static inline int crypto_aead_decrypt(struct aead_request *req)
837{
838 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
839}
840
Herbert Xub16c3a22007-08-29 19:02:04 +0800841static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800842{
843 return crypto_aead_crt(tfm)->reqsize;
844}
845
846static inline void aead_request_set_tfm(struct aead_request *req,
847 struct crypto_aead *tfm)
848{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800849 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800850}
851
852static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
853 gfp_t gfp)
854{
855 struct aead_request *req;
856
857 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
858
859 if (likely(req))
860 aead_request_set_tfm(req, tfm);
861
862 return req;
863}
864
865static inline void aead_request_free(struct aead_request *req)
866{
867 kfree(req);
868}
869
870static inline void aead_request_set_callback(struct aead_request *req,
871 u32 flags,
872 crypto_completion_t complete,
873 void *data)
874{
875 req->base.complete = complete;
876 req->base.data = data;
877 req->base.flags = flags;
878}
879
880static inline void aead_request_set_crypt(struct aead_request *req,
881 struct scatterlist *src,
882 struct scatterlist *dst,
883 unsigned int cryptlen, u8 *iv)
884{
885 req->src = src;
886 req->dst = dst;
887 req->cryptlen = cryptlen;
888 req->iv = iv;
889}
890
891static inline void aead_request_set_assoc(struct aead_request *req,
892 struct scatterlist *assoc,
893 unsigned int assoclen)
894{
895 req->assoc = assoc;
896 req->assoclen = assoclen;
897}
898
Herbert Xu5cde0af2006-08-22 00:07:53 +1000899static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
900 struct crypto_tfm *tfm)
901{
902 return (struct crypto_blkcipher *)tfm;
903}
904
905static inline struct crypto_blkcipher *crypto_blkcipher_cast(
906 struct crypto_tfm *tfm)
907{
908 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
909 return __crypto_blkcipher_cast(tfm);
910}
911
912static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
913 const char *alg_name, u32 type, u32 mask)
914{
Herbert Xu332f88402007-11-15 22:36:07 +0800915 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000916 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800917 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000918
919 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
920}
921
922static inline struct crypto_tfm *crypto_blkcipher_tfm(
923 struct crypto_blkcipher *tfm)
924{
925 return &tfm->base;
926}
927
928static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
929{
930 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
931}
932
Herbert Xufce32d72006-08-26 17:35:45 +1000933static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
934{
Herbert Xu332f88402007-11-15 22:36:07 +0800935 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000936 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800937 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000938
939 return crypto_has_alg(alg_name, type, mask);
940}
941
Herbert Xu5cde0af2006-08-22 00:07:53 +1000942static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
943{
944 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
945}
946
947static inline struct blkcipher_tfm *crypto_blkcipher_crt(
948 struct crypto_blkcipher *tfm)
949{
950 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
951}
952
953static inline struct blkcipher_alg *crypto_blkcipher_alg(
954 struct crypto_blkcipher *tfm)
955{
956 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
957}
958
959static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
960{
961 return crypto_blkcipher_alg(tfm)->ivsize;
962}
963
964static inline unsigned int crypto_blkcipher_blocksize(
965 struct crypto_blkcipher *tfm)
966{
967 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
968}
969
970static inline unsigned int crypto_blkcipher_alignmask(
971 struct crypto_blkcipher *tfm)
972{
973 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
974}
975
976static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
977{
978 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
979}
980
981static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
982 u32 flags)
983{
984 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
985}
986
987static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
988 u32 flags)
989{
990 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
991}
992
993static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
994 const u8 *key, unsigned int keylen)
995{
996 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
997 key, keylen);
998}
999
1000static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1001 struct scatterlist *dst,
1002 struct scatterlist *src,
1003 unsigned int nbytes)
1004{
1005 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1006 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1007}
1008
1009static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1010 struct scatterlist *dst,
1011 struct scatterlist *src,
1012 unsigned int nbytes)
1013{
1014 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1015}
1016
1017static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1018 struct scatterlist *dst,
1019 struct scatterlist *src,
1020 unsigned int nbytes)
1021{
1022 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1023 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1024}
1025
1026static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1027 struct scatterlist *dst,
1028 struct scatterlist *src,
1029 unsigned int nbytes)
1030{
1031 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1032}
1033
1034static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1035 const u8 *src, unsigned int len)
1036{
1037 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1038}
1039
1040static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1041 u8 *dst, unsigned int len)
1042{
1043 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1044}
1045
Herbert Xuf28776a2006-08-13 20:58:18 +10001046static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1047{
1048 return (struct crypto_cipher *)tfm;
1049}
1050
1051static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1052{
1053 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1054 return __crypto_cipher_cast(tfm);
1055}
1056
1057static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1058 u32 type, u32 mask)
1059{
1060 type &= ~CRYPTO_ALG_TYPE_MASK;
1061 type |= CRYPTO_ALG_TYPE_CIPHER;
1062 mask |= CRYPTO_ALG_TYPE_MASK;
1063
1064 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1065}
1066
1067static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1068{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001069 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001070}
1071
1072static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1073{
1074 crypto_free_tfm(crypto_cipher_tfm(tfm));
1075}
1076
Herbert Xufce32d72006-08-26 17:35:45 +10001077static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1078{
1079 type &= ~CRYPTO_ALG_TYPE_MASK;
1080 type |= CRYPTO_ALG_TYPE_CIPHER;
1081 mask |= CRYPTO_ALG_TYPE_MASK;
1082
1083 return crypto_has_alg(alg_name, type, mask);
1084}
1085
Herbert Xuf28776a2006-08-13 20:58:18 +10001086static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1087{
1088 return &crypto_cipher_tfm(tfm)->crt_cipher;
1089}
1090
1091static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1092{
1093 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1094}
1095
1096static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1097{
1098 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1099}
1100
1101static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1102{
1103 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1104}
1105
1106static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1107 u32 flags)
1108{
1109 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1110}
1111
1112static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1113 u32 flags)
1114{
1115 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1116}
1117
Herbert Xu7226bc872006-08-21 21:40:49 +10001118static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1119 const u8 *key, unsigned int keylen)
1120{
1121 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1122 key, keylen);
1123}
1124
Herbert Xuf28776a2006-08-13 20:58:18 +10001125static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1126 u8 *dst, const u8 *src)
1127{
1128 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1129 dst, src);
1130}
1131
1132static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1133 u8 *dst, const u8 *src)
1134{
1135 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1136 dst, src);
1137}
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 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Herbert Xu055bcee2006-08-19 22:24:23 +10001144static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Herbert Xu055bcee2006-08-19 22:24:23 +10001146 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1147 CRYPTO_ALG_TYPE_HASH_MASK);
1148 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Herbert Xu055bcee2006-08-19 22:24:23 +10001151static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1152 u32 type, u32 mask)
1153{
1154 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001155 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001156 type |= CRYPTO_ALG_TYPE_HASH;
1157 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1158
1159 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1160}
1161
1162static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1163{
1164 return &tfm->base;
1165}
1166
1167static inline void crypto_free_hash(struct crypto_hash *tfm)
1168{
1169 crypto_free_tfm(crypto_hash_tfm(tfm));
1170}
1171
Herbert Xufce32d72006-08-26 17:35:45 +10001172static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1173{
1174 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001175 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001176 type |= CRYPTO_ALG_TYPE_HASH;
1177 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1178
1179 return crypto_has_alg(alg_name, type, mask);
1180}
1181
Herbert Xu055bcee2006-08-19 22:24:23 +10001182static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1183{
1184 return &crypto_hash_tfm(tfm)->crt_hash;
1185}
1186
1187static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1188{
1189 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1190}
1191
1192static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1193{
1194 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1195}
1196
1197static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1198{
1199 return crypto_hash_crt(tfm)->digestsize;
1200}
1201
1202static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1203{
1204 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1205}
1206
1207static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1208{
1209 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1210}
1211
1212static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1213{
1214 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1215}
1216
1217static inline int crypto_hash_init(struct hash_desc *desc)
1218{
1219 return crypto_hash_crt(desc->tfm)->init(desc);
1220}
1221
1222static inline int crypto_hash_update(struct hash_desc *desc,
1223 struct scatterlist *sg,
1224 unsigned int nbytes)
1225{
1226 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1227}
1228
1229static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1230{
1231 return crypto_hash_crt(desc->tfm)->final(desc, out);
1232}
1233
1234static inline int crypto_hash_digest(struct hash_desc *desc,
1235 struct scatterlist *sg,
1236 unsigned int nbytes, u8 *out)
1237{
1238 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1239}
1240
1241static inline int crypto_hash_setkey(struct crypto_hash *hash,
1242 const u8 *key, unsigned int keylen)
1243{
1244 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Herbert Xufce32d72006-08-26 17:35:45 +10001247static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1248{
1249 return (struct crypto_comp *)tfm;
1250}
1251
1252static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1253{
1254 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1255 CRYPTO_ALG_TYPE_MASK);
1256 return __crypto_comp_cast(tfm);
1257}
1258
1259static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1260 u32 type, u32 mask)
1261{
1262 type &= ~CRYPTO_ALG_TYPE_MASK;
1263 type |= CRYPTO_ALG_TYPE_COMPRESS;
1264 mask |= CRYPTO_ALG_TYPE_MASK;
1265
1266 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1267}
1268
1269static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1270{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001271 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001272}
1273
1274static inline void crypto_free_comp(struct crypto_comp *tfm)
1275{
1276 crypto_free_tfm(crypto_comp_tfm(tfm));
1277}
1278
1279static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1280{
1281 type &= ~CRYPTO_ALG_TYPE_MASK;
1282 type |= CRYPTO_ALG_TYPE_COMPRESS;
1283 mask |= CRYPTO_ALG_TYPE_MASK;
1284
1285 return crypto_has_alg(alg_name, type, mask);
1286}
1287
Herbert Xue4d5b792006-08-26 18:12:40 +10001288static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1289{
1290 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1291}
1292
Herbert Xufce32d72006-08-26 17:35:45 +10001293static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1294{
1295 return &crypto_comp_tfm(tfm)->crt_compress;
1296}
1297
1298static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 const u8 *src, unsigned int slen,
1300 u8 *dst, unsigned int *dlen)
1301{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001302 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1303 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Herbert Xufce32d72006-08-26 17:35:45 +10001306static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 const u8 *src, unsigned int slen,
1308 u8 *dst, unsigned int *dlen)
1309{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001310 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1311 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Loc Ho004a4032008-05-14 20:41:47 +08001314static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
1315{
1316 return (struct crypto_ahash *)tfm;
1317}
1318
1319static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
1320 u32 type, u32 mask)
1321{
1322 type &= ~CRYPTO_ALG_TYPE_MASK;
1323 mask &= ~CRYPTO_ALG_TYPE_MASK;
1324 type |= CRYPTO_ALG_TYPE_AHASH;
1325 mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
1326
1327 return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
1328}
1329
1330static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
1331{
1332 return &tfm->base;
1333}
1334
1335static inline void crypto_free_ahash(struct crypto_ahash *tfm)
1336{
1337 crypto_free_tfm(crypto_ahash_tfm(tfm));
1338}
1339
1340static inline unsigned int crypto_ahash_alignmask(
1341 struct crypto_ahash *tfm)
1342{
1343 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
1344}
1345
1346static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
1347{
1348 return &crypto_ahash_tfm(tfm)->crt_ahash;
1349}
1350
1351static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
1352{
1353 return crypto_ahash_crt(tfm)->digestsize;
1354}
1355
1356static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
1357{
1358 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
1359}
1360
1361static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
1362{
1363 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
1364}
1365
1366static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
1367{
1368 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
1369}
1370
1371static inline struct crypto_ahash *crypto_ahash_reqtfm(
1372 struct ahash_request *req)
1373{
1374 return __crypto_ahash_cast(req->base.tfm);
1375}
1376
1377static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
1378{
1379 return crypto_ahash_crt(tfm)->reqsize;
1380}
1381
1382static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
1383 const u8 *key, unsigned int keylen)
1384{
1385 struct ahash_tfm *crt = crypto_ahash_crt(tfm);
1386
1387 return crt->setkey(crt->base, key, keylen);
1388}
1389
1390static inline int crypto_ahash_digest(struct ahash_request *req)
1391{
1392 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
1393 return crt->digest(req);
1394}
1395
1396static inline void ahash_request_set_tfm(struct ahash_request *req,
1397 struct crypto_ahash *tfm)
1398{
1399 req->base.tfm = crypto_ahash_tfm(crypto_ahash_crt(tfm)->base);
1400}
1401
1402static inline struct ahash_request *ahash_request_alloc(
1403 struct crypto_ahash *tfm, gfp_t gfp)
1404{
1405 struct ahash_request *req;
1406
1407 req = kmalloc(sizeof(struct ahash_request) +
1408 crypto_ahash_reqsize(tfm), gfp);
1409
1410 if (likely(req))
1411 ahash_request_set_tfm(req, tfm);
1412
1413 return req;
1414}
1415
1416static inline void ahash_request_free(struct ahash_request *req)
1417{
1418 kfree(req);
1419}
1420
1421static inline struct ahash_request *ahash_request_cast(
1422 struct crypto_async_request *req)
1423{
1424 return container_of(req, struct ahash_request, base);
1425}
1426
1427static inline void ahash_request_set_callback(struct ahash_request *req,
1428 u32 flags,
1429 crypto_completion_t complete,
1430 void *data)
1431{
1432 req->base.complete = complete;
1433 req->base.data = data;
1434 req->base.flags = flags;
1435}
1436
1437static inline void ahash_request_set_crypt(struct ahash_request *req,
1438 struct scatterlist *src, u8 *result,
1439 unsigned int nbytes)
1440{
1441 req->src = src;
1442 req->nbytes = nbytes;
1443 req->result = result;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#endif /* _LINUX_CRYPTO_H */
1447