blob: fd929889e8dc68a583e3cf138ef650c0cd9a90b4 [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
Herbert Xu5f7082e2008-08-31 22:21:09 +100039#define CRYPTO_ALG_TYPE_HASH 0x00000008
40#define CRYPTO_ALG_TYPE_SHASH 0x00000009
Loc Ho004a4032008-05-14 20:41:47 +080041#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Neil Horman17f0f4a2008-08-14 22:15:52 +100042#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Geert Uytterhoevena1d2f092009-03-04 15:05:33 +080043#define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100044
45#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080046#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080047#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Herbert Xu28259822006-08-06 21:23:26 +100049#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100050#define CRYPTO_ALG_DEAD 0x00000020
51#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100052#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
Herbert Xu60104392006-08-26 18:34:10 +100055 * Set this bit if and only if the algorithm requires another algorithm of
56 * the same type to handle corner cases.
57 */
58#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
59
60/*
Herbert Xuecfc4322007-12-05 21:08:36 +110061 * This bit is set for symmetric key ciphers that have already been wrapped
62 * with a generic IV generator to prevent them from being wrapped again.
63 */
64#define CRYPTO_ALG_GENIV 0x00000200
65
66/*
Herbert Xu73d38642008-08-03 21:15:23 +080067 * Set if the algorithm has passed automated run-time testing. Note that
68 * if there is no run-time testing for a given algorithm it is considered
69 * to have passed.
70 */
71
72#define CRYPTO_ALG_TESTED 0x00000400
73
74/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Transform masks and values (for crt_flags).
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define CRYPTO_TFM_REQ_MASK 0x000fff00
78#define CRYPTO_TFM_RES_MASK 0xfff00000
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070081#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110082#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
84#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
85#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
86#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
87#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
88
89/*
90 * Miscellaneous stuff.
91 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define CRYPTO_MAX_ALG_NAME 64
93
Herbert Xu79911102006-08-21 21:03:52 +100094/*
95 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
96 * declaration) is used to ensure that the crypto_tfm context structure is
97 * aligned correctly for the given architecture so that there are no alignment
98 * faults for C data types. In particular, this is required on platforms such
99 * as arm where pointers are 32-bit aligned but there are data types such as
100 * u64 which require 64-bit alignment.
101 */
102#if defined(ARCH_KMALLOC_MINALIGN)
103#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
104#elif defined(ARCH_SLAB_MINALIGN)
105#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
Herbert Xu6eb72282008-01-08 17:16:44 +1100106#else
107#define CRYPTO_MINALIGN __alignof__(unsigned long long)
Herbert Xu79911102006-08-21 21:03:52 +1000108#endif
109
Herbert Xu79911102006-08-21 21:03:52 +1000110#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100113struct crypto_ablkcipher;
114struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800115struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000116struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000117struct crypto_hash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000118struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700119struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000120struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800121struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800122struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700123
Herbert Xu32e39832007-03-24 14:35:34 +1100124typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
125
126struct crypto_async_request {
127 struct list_head list;
128 crypto_completion_t complete;
129 void *data;
130 struct crypto_tfm *tfm;
131
132 u32 flags;
133};
134
135struct ablkcipher_request {
136 struct crypto_async_request base;
137
138 unsigned int nbytes;
139
140 void *info;
141
142 struct scatterlist *src;
143 struct scatterlist *dst;
144
145 void *__ctx[] CRYPTO_MINALIGN_ATTR;
146};
147
Herbert Xu1ae97822007-08-30 15:36:14 +0800148/**
149 * struct aead_request - AEAD request
150 * @base: Common attributes for async crypto requests
151 * @assoclen: Length in bytes of associated data for authentication
152 * @cryptlen: Length of data to be encrypted or decrypted
153 * @iv: Initialisation vector
154 * @assoc: Associated data
155 * @src: Source data
156 * @dst: Destination data
157 * @__ctx: Start of private context data
158 */
159struct aead_request {
160 struct crypto_async_request base;
161
162 unsigned int assoclen;
163 unsigned int cryptlen;
164
165 u8 *iv;
166
167 struct scatterlist *assoc;
168 struct scatterlist *src;
169 struct scatterlist *dst;
170
171 void *__ctx[] CRYPTO_MINALIGN_ATTR;
172};
173
Herbert Xu5cde0af2006-08-22 00:07:53 +1000174struct blkcipher_desc {
175 struct crypto_blkcipher *tfm;
176 void *info;
177 u32 flags;
178};
179
Herbert Xu40725182005-07-06 13:51:52 -0700180struct cipher_desc {
181 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000182 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700183 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
184 const u8 *src, unsigned int nbytes);
185 void *info;
186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Herbert Xu055bcee2006-08-19 22:24:23 +1000188struct hash_desc {
189 struct crypto_hash *tfm;
190 u32 flags;
191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * Algorithms: modular crypto algorithm implementations, managed
195 * via crypto_register_alg() and crypto_unregister_alg().
196 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000197struct ablkcipher_alg {
198 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
199 unsigned int keylen);
200 int (*encrypt)(struct ablkcipher_request *req);
201 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800202 int (*givencrypt)(struct skcipher_givcrypt_request *req);
203 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000204
Herbert Xu23508e12007-11-27 21:33:24 +0800205 const char *geniv;
206
Herbert Xub5b7f082007-04-16 20:48:54 +1000207 unsigned int min_keysize;
208 unsigned int max_keysize;
209 unsigned int ivsize;
210};
211
Herbert Xu1ae97822007-08-30 15:36:14 +0800212struct aead_alg {
213 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
214 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100215 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800216 int (*encrypt)(struct aead_request *req);
217 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800218 int (*givencrypt)(struct aead_givcrypt_request *req);
219 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800220
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800221 const char *geniv;
222
Herbert Xu1ae97822007-08-30 15:36:14 +0800223 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100224 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800225};
226
Herbert Xu5cde0af2006-08-22 00:07:53 +1000227struct blkcipher_alg {
228 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
229 unsigned int keylen);
230 int (*encrypt)(struct blkcipher_desc *desc,
231 struct scatterlist *dst, struct scatterlist *src,
232 unsigned int nbytes);
233 int (*decrypt)(struct blkcipher_desc *desc,
234 struct scatterlist *dst, struct scatterlist *src,
235 unsigned int nbytes);
236
Herbert Xu23508e12007-11-27 21:33:24 +0800237 const char *geniv;
238
Herbert Xu5cde0af2006-08-22 00:07:53 +1000239 unsigned int min_keysize;
240 unsigned int max_keysize;
241 unsigned int ivsize;
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244struct cipher_alg {
245 unsigned int cia_min_keysize;
246 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000247 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000248 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000249 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
250 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253struct digest_alg {
254 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000255 void (*dia_init)(struct crypto_tfm *tfm);
256 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
257 unsigned int len);
258 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
259 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000260 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Herbert Xu055bcee2006-08-19 22:24:23 +1000263struct hash_alg {
264 int (*init)(struct hash_desc *desc);
265 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
266 unsigned int nbytes);
267 int (*final)(struct hash_desc *desc, u8 *out);
268 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
269 unsigned int nbytes, u8 *out);
270 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
271 unsigned int keylen);
272
273 unsigned int digestsize;
274};
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000277 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
278 unsigned int slen, u8 *dst, unsigned int *dlen);
279 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
280 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
Neil Horman17f0f4a2008-08-14 22:15:52 +1000283struct rng_alg {
284 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
285 unsigned int dlen);
286 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
287
288 unsigned int seedsize;
289};
290
291
Herbert Xub5b7f082007-04-16 20:48:54 +1000292#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800293#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000294#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#define cra_cipher cra_u.cipher
296#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000297#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000299#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301struct crypto_alg {
302 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000303 struct list_head cra_users;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u32 cra_flags;
306 unsigned int cra_blocksize;
307 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700308 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100309
310 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000311 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100312
Herbert Xud913ea02006-05-21 08:45:26 +1000313 char cra_name[CRYPTO_MAX_ALG_NAME];
314 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Herbert Xue853c3c2006-08-22 00:06:54 +1000316 const struct crypto_type *cra_type;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000319 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800320 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000321 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct cipher_alg cipher;
323 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000324 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000326 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000328
329 int (*cra_init)(struct crypto_tfm *tfm);
330 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000331 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 struct module *cra_module;
334};
335
336/*
337 * Algorithm registration interface.
338 */
339int crypto_register_alg(struct crypto_alg *alg);
340int crypto_unregister_alg(struct crypto_alg *alg);
341
342/*
343 * Algorithm query interface.
344 */
Herbert Xufce32d72006-08-26 17:35:45 +1000345int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347/*
348 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000349 * and core processing logic. Managed via crypto_alloc_*() and
350 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Herbert Xu32e39832007-03-24 14:35:34 +1100353struct ablkcipher_tfm {
354 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
355 unsigned int keylen);
356 int (*encrypt)(struct ablkcipher_request *req);
357 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800358 int (*givencrypt)(struct skcipher_givcrypt_request *req);
359 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
360
Herbert Xuecfc4322007-12-05 21:08:36 +1100361 struct crypto_ablkcipher *base;
362
Herbert Xu32e39832007-03-24 14:35:34 +1100363 unsigned int ivsize;
364 unsigned int reqsize;
365};
366
Herbert Xu1ae97822007-08-30 15:36:14 +0800367struct aead_tfm {
368 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
369 unsigned int keylen);
370 int (*encrypt)(struct aead_request *req);
371 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800372 int (*givencrypt)(struct aead_givcrypt_request *req);
373 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800374
375 struct crypto_aead *base;
376
Herbert Xu1ae97822007-08-30 15:36:14 +0800377 unsigned int ivsize;
378 unsigned int authsize;
379 unsigned int reqsize;
380};
381
Herbert Xu5cde0af2006-08-22 00:07:53 +1000382struct blkcipher_tfm {
383 void *iv;
384 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
385 unsigned int keylen);
386 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
387 struct scatterlist *src, unsigned int nbytes);
388 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
389 struct scatterlist *src, unsigned int nbytes);
390};
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int (*cit_setkey)(struct crypto_tfm *tfm,
394 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000395 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
396 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
Herbert Xu055bcee2006-08-19 22:24:23 +1000399struct hash_tfm {
400 int (*init)(struct hash_desc *desc);
401 int (*update)(struct hash_desc *desc,
402 struct scatterlist *sg, unsigned int nsg);
403 int (*final)(struct hash_desc *desc, u8 *out);
404 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
405 unsigned int nsg, u8 *out);
406 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
407 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000408 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
411struct compress_tfm {
412 int (*cot_compress)(struct crypto_tfm *tfm,
413 const u8 *src, unsigned int slen,
414 u8 *dst, unsigned int *dlen);
415 int (*cot_decompress)(struct crypto_tfm *tfm,
416 const u8 *src, unsigned int slen,
417 u8 *dst, unsigned int *dlen);
418};
419
Neil Horman17f0f4a2008-08-14 22:15:52 +1000420struct rng_tfm {
421 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
422 unsigned int dlen);
423 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
424};
425
Herbert Xu32e39832007-03-24 14:35:34 +1100426#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800427#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000428#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000430#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000432#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434struct crypto_tfm {
435
436 u32 crt_flags;
437
438 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100439 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800440 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000441 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000443 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000445 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700447
448 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100451
Herbert Xu79911102006-08-21 21:03:52 +1000452 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453};
454
Herbert Xu32e39832007-03-24 14:35:34 +1100455struct crypto_ablkcipher {
456 struct crypto_tfm base;
457};
458
Herbert Xu1ae97822007-08-30 15:36:14 +0800459struct crypto_aead {
460 struct crypto_tfm base;
461};
462
Herbert Xu5cde0af2006-08-22 00:07:53 +1000463struct crypto_blkcipher {
464 struct crypto_tfm base;
465};
466
Herbert Xu78a1fe42006-12-24 10:02:00 +1100467struct crypto_cipher {
468 struct crypto_tfm base;
469};
470
471struct crypto_comp {
472 struct crypto_tfm base;
473};
474
Herbert Xu055bcee2006-08-19 22:24:23 +1000475struct crypto_hash {
476 struct crypto_tfm base;
477};
478
Neil Horman17f0f4a2008-08-14 22:15:52 +1000479struct crypto_rng {
480 struct crypto_tfm base;
481};
482
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000483enum {
484 CRYPTOA_UNSPEC,
485 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100486 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800487 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100488 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000489};
490
Herbert Xuebc610e2007-01-01 18:37:02 +1100491#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
492
Herbert Xu39e1ee012007-08-29 19:27:26 +0800493/* Maximum number of (rtattr) parameters for each template. */
494#define CRYPTO_MAX_ATTRS 32
495
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000496struct crypto_attr_alg {
497 char name[CRYPTO_MAX_ALG_NAME];
498};
499
Herbert Xuebc610e2007-01-01 18:37:02 +1100500struct crypto_attr_type {
501 u32 type;
502 u32 mask;
503};
504
Herbert Xu39e1ee012007-08-29 19:27:26 +0800505struct crypto_attr_u32 {
506 u32 num;
507};
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * Transform user interface.
511 */
512
Herbert Xu6d7d6842006-07-30 11:53:01 +1000513struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100514void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
515
516static inline void crypto_free_tfm(struct crypto_tfm *tfm)
517{
518 return crypto_destroy_tfm(tfm, tfm);
519}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Herbert Xuda7f0332008-07-31 17:08:25 +0800521int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
524 * Transform helpers which query the underlying algorithm.
525 */
526static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
527{
528 return tfm->__crt_alg->cra_name;
529}
530
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000531static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
532{
533 return tfm->__crt_alg->cra_driver_name;
534}
535
536static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
537{
538 return tfm->__crt_alg->cra_priority;
539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
542{
543 return module_name(tfm->__crt_alg->cra_module);
544}
545
546static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
547{
548 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
552{
553 return tfm->__crt_alg->cra_blocksize;
554}
555
Herbert Xufbdae9f2005-07-06 13:53:29 -0700556static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
557{
558 return tfm->__crt_alg->cra_alignmask;
559}
560
Herbert Xuf28776a2006-08-13 20:58:18 +1000561static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
562{
563 return tfm->crt_flags;
564}
565
566static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
567{
568 tfm->crt_flags |= flags;
569}
570
571static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
572{
573 tfm->crt_flags &= ~flags;
574}
575
Herbert Xu40725182005-07-06 13:51:52 -0700576static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
577{
Herbert Xuf10b7892006-01-25 22:34:01 +1100578 return tfm->__crt_ctx;
579}
580
581static inline unsigned int crypto_tfm_ctx_alignment(void)
582{
583 struct crypto_tfm *tfm;
584 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/*
588 * API wrappers.
589 */
Herbert Xu32e39832007-03-24 14:35:34 +1100590static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
591 struct crypto_tfm *tfm)
592{
593 return (struct crypto_ablkcipher *)tfm;
594}
595
Herbert Xu378f4f52007-12-17 20:07:31 +0800596static inline u32 crypto_skcipher_type(u32 type)
597{
Herbert Xuecfc4322007-12-05 21:08:36 +1100598 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800599 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
600 return type;
601}
602
603static inline u32 crypto_skcipher_mask(u32 mask)
604{
Herbert Xuecfc4322007-12-05 21:08:36 +1100605 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800606 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
607 return mask;
608}
609
Herbert Xub9c55aa2007-12-04 12:46:48 +1100610struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
611 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100612
613static inline struct crypto_tfm *crypto_ablkcipher_tfm(
614 struct crypto_ablkcipher *tfm)
615{
616 return &tfm->base;
617}
618
619static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
620{
621 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
622}
623
624static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
625 u32 mask)
626{
Herbert Xu378f4f52007-12-17 20:07:31 +0800627 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
628 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100629}
630
631static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
632 struct crypto_ablkcipher *tfm)
633{
634 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
635}
636
637static inline unsigned int crypto_ablkcipher_ivsize(
638 struct crypto_ablkcipher *tfm)
639{
640 return crypto_ablkcipher_crt(tfm)->ivsize;
641}
642
643static inline unsigned int crypto_ablkcipher_blocksize(
644 struct crypto_ablkcipher *tfm)
645{
646 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
647}
648
649static inline unsigned int crypto_ablkcipher_alignmask(
650 struct crypto_ablkcipher *tfm)
651{
652 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
653}
654
655static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
656{
657 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
658}
659
660static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
661 u32 flags)
662{
663 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
664}
665
666static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
667 u32 flags)
668{
669 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
670}
671
672static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
673 const u8 *key, unsigned int keylen)
674{
Herbert Xuecfc4322007-12-05 21:08:36 +1100675 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
676
677 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100678}
679
680static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
681 struct ablkcipher_request *req)
682{
683 return __crypto_ablkcipher_cast(req->base.tfm);
684}
685
686static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
687{
688 struct ablkcipher_tfm *crt =
689 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
690 return crt->encrypt(req);
691}
692
693static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
694{
695 struct ablkcipher_tfm *crt =
696 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
697 return crt->decrypt(req);
698}
699
Herbert Xub16c3a22007-08-29 19:02:04 +0800700static inline unsigned int crypto_ablkcipher_reqsize(
701 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100702{
703 return crypto_ablkcipher_crt(tfm)->reqsize;
704}
705
Herbert Xue196d622007-04-14 16:09:14 +1000706static inline void ablkcipher_request_set_tfm(
707 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
708{
Herbert Xuecfc4322007-12-05 21:08:36 +1100709 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000710}
711
Herbert Xub5b7f082007-04-16 20:48:54 +1000712static inline struct ablkcipher_request *ablkcipher_request_cast(
713 struct crypto_async_request *req)
714{
715 return container_of(req, struct ablkcipher_request, base);
716}
717
Herbert Xu32e39832007-03-24 14:35:34 +1100718static inline struct ablkcipher_request *ablkcipher_request_alloc(
719 struct crypto_ablkcipher *tfm, gfp_t gfp)
720{
721 struct ablkcipher_request *req;
722
723 req = kmalloc(sizeof(struct ablkcipher_request) +
724 crypto_ablkcipher_reqsize(tfm), gfp);
725
726 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000727 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100728
729 return req;
730}
731
732static inline void ablkcipher_request_free(struct ablkcipher_request *req)
733{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800734 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100735}
736
737static inline void ablkcipher_request_set_callback(
738 struct ablkcipher_request *req,
739 u32 flags, crypto_completion_t complete, void *data)
740{
741 req->base.complete = complete;
742 req->base.data = data;
743 req->base.flags = flags;
744}
745
746static inline void ablkcipher_request_set_crypt(
747 struct ablkcipher_request *req,
748 struct scatterlist *src, struct scatterlist *dst,
749 unsigned int nbytes, void *iv)
750{
751 req->src = src;
752 req->dst = dst;
753 req->nbytes = nbytes;
754 req->info = iv;
755}
756
Herbert Xu1ae97822007-08-30 15:36:14 +0800757static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
758{
759 return (struct crypto_aead *)tfm;
760}
761
Herbert Xud29ce982007-12-12 19:24:27 +0800762struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800763
764static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
765{
766 return &tfm->base;
767}
768
769static inline void crypto_free_aead(struct crypto_aead *tfm)
770{
771 crypto_free_tfm(crypto_aead_tfm(tfm));
772}
773
774static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
775{
776 return &crypto_aead_tfm(tfm)->crt_aead;
777}
778
779static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
780{
781 return crypto_aead_crt(tfm)->ivsize;
782}
783
784static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
785{
786 return crypto_aead_crt(tfm)->authsize;
787}
788
789static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
790{
791 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
792}
793
794static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
795{
796 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
797}
798
799static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
800{
801 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
802}
803
804static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
805{
806 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
807}
808
809static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
810{
811 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
812}
813
814static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
815 unsigned int keylen)
816{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800817 struct aead_tfm *crt = crypto_aead_crt(tfm);
818
819 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800820}
821
Herbert Xu7ba683a2007-12-02 18:49:21 +1100822int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
823
Herbert Xu1ae97822007-08-30 15:36:14 +0800824static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
825{
826 return __crypto_aead_cast(req->base.tfm);
827}
828
829static inline int crypto_aead_encrypt(struct aead_request *req)
830{
831 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
832}
833
834static inline int crypto_aead_decrypt(struct aead_request *req)
835{
836 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
837}
838
Herbert Xub16c3a22007-08-29 19:02:04 +0800839static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800840{
841 return crypto_aead_crt(tfm)->reqsize;
842}
843
844static inline void aead_request_set_tfm(struct aead_request *req,
845 struct crypto_aead *tfm)
846{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800847 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800848}
849
850static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
851 gfp_t gfp)
852{
853 struct aead_request *req;
854
855 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
856
857 if (likely(req))
858 aead_request_set_tfm(req, tfm);
859
860 return req;
861}
862
863static inline void aead_request_free(struct aead_request *req)
864{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800865 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800866}
867
868static inline void aead_request_set_callback(struct aead_request *req,
869 u32 flags,
870 crypto_completion_t complete,
871 void *data)
872{
873 req->base.complete = complete;
874 req->base.data = data;
875 req->base.flags = flags;
876}
877
878static inline void aead_request_set_crypt(struct aead_request *req,
879 struct scatterlist *src,
880 struct scatterlist *dst,
881 unsigned int cryptlen, u8 *iv)
882{
883 req->src = src;
884 req->dst = dst;
885 req->cryptlen = cryptlen;
886 req->iv = iv;
887}
888
889static inline void aead_request_set_assoc(struct aead_request *req,
890 struct scatterlist *assoc,
891 unsigned int assoclen)
892{
893 req->assoc = assoc;
894 req->assoclen = assoclen;
895}
896
Herbert Xu5cde0af2006-08-22 00:07:53 +1000897static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
898 struct crypto_tfm *tfm)
899{
900 return (struct crypto_blkcipher *)tfm;
901}
902
903static inline struct crypto_blkcipher *crypto_blkcipher_cast(
904 struct crypto_tfm *tfm)
905{
906 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
907 return __crypto_blkcipher_cast(tfm);
908}
909
910static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
911 const char *alg_name, u32 type, u32 mask)
912{
Herbert Xu332f88402007-11-15 22:36:07 +0800913 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000914 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800915 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000916
917 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
918}
919
920static inline struct crypto_tfm *crypto_blkcipher_tfm(
921 struct crypto_blkcipher *tfm)
922{
923 return &tfm->base;
924}
925
926static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
927{
928 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
929}
930
Herbert Xufce32d72006-08-26 17:35:45 +1000931static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
932{
Herbert Xu332f88402007-11-15 22:36:07 +0800933 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000934 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800935 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000936
937 return crypto_has_alg(alg_name, type, mask);
938}
939
Herbert Xu5cde0af2006-08-22 00:07:53 +1000940static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
941{
942 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
943}
944
945static inline struct blkcipher_tfm *crypto_blkcipher_crt(
946 struct crypto_blkcipher *tfm)
947{
948 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
949}
950
951static inline struct blkcipher_alg *crypto_blkcipher_alg(
952 struct crypto_blkcipher *tfm)
953{
954 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
955}
956
957static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
958{
959 return crypto_blkcipher_alg(tfm)->ivsize;
960}
961
962static inline unsigned int crypto_blkcipher_blocksize(
963 struct crypto_blkcipher *tfm)
964{
965 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
966}
967
968static inline unsigned int crypto_blkcipher_alignmask(
969 struct crypto_blkcipher *tfm)
970{
971 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
972}
973
974static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
975{
976 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
977}
978
979static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
980 u32 flags)
981{
982 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
983}
984
985static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
986 u32 flags)
987{
988 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
989}
990
991static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
992 const u8 *key, unsigned int keylen)
993{
994 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
995 key, keylen);
996}
997
998static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
999 struct scatterlist *dst,
1000 struct scatterlist *src,
1001 unsigned int nbytes)
1002{
1003 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1004 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1005}
1006
1007static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1008 struct scatterlist *dst,
1009 struct scatterlist *src,
1010 unsigned int nbytes)
1011{
1012 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1013}
1014
1015static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1016 struct scatterlist *dst,
1017 struct scatterlist *src,
1018 unsigned int nbytes)
1019{
1020 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1021 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1022}
1023
1024static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1025 struct scatterlist *dst,
1026 struct scatterlist *src,
1027 unsigned int nbytes)
1028{
1029 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1030}
1031
1032static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1033 const u8 *src, unsigned int len)
1034{
1035 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1036}
1037
1038static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1039 u8 *dst, unsigned int len)
1040{
1041 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1042}
1043
Herbert Xuf28776a2006-08-13 20:58:18 +10001044static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1045{
1046 return (struct crypto_cipher *)tfm;
1047}
1048
1049static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1050{
1051 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1052 return __crypto_cipher_cast(tfm);
1053}
1054
1055static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1056 u32 type, u32 mask)
1057{
1058 type &= ~CRYPTO_ALG_TYPE_MASK;
1059 type |= CRYPTO_ALG_TYPE_CIPHER;
1060 mask |= CRYPTO_ALG_TYPE_MASK;
1061
1062 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1063}
1064
1065static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1066{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001067 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001068}
1069
1070static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1071{
1072 crypto_free_tfm(crypto_cipher_tfm(tfm));
1073}
1074
Herbert Xufce32d72006-08-26 17:35:45 +10001075static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1076{
1077 type &= ~CRYPTO_ALG_TYPE_MASK;
1078 type |= CRYPTO_ALG_TYPE_CIPHER;
1079 mask |= CRYPTO_ALG_TYPE_MASK;
1080
1081 return crypto_has_alg(alg_name, type, mask);
1082}
1083
Herbert Xuf28776a2006-08-13 20:58:18 +10001084static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1085{
1086 return &crypto_cipher_tfm(tfm)->crt_cipher;
1087}
1088
1089static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1090{
1091 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1092}
1093
1094static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1095{
1096 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1097}
1098
1099static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1100{
1101 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1102}
1103
1104static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1105 u32 flags)
1106{
1107 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1108}
1109
1110static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1111 u32 flags)
1112{
1113 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1114}
1115
Herbert Xu7226bc872006-08-21 21:40:49 +10001116static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1117 const u8 *key, unsigned int keylen)
1118{
1119 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1120 key, keylen);
1121}
1122
Herbert Xuf28776a2006-08-13 20:58:18 +10001123static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1124 u8 *dst, const u8 *src)
1125{
1126 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1127 dst, src);
1128}
1129
1130static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1131 u8 *dst, const u8 *src)
1132{
1133 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1134 dst, src);
1135}
1136
Herbert Xu055bcee2006-08-19 22:24:23 +10001137static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Herbert Xu055bcee2006-08-19 22:24:23 +10001139 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Herbert Xu055bcee2006-08-19 22:24:23 +10001142static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Herbert Xu055bcee2006-08-19 22:24:23 +10001144 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1145 CRYPTO_ALG_TYPE_HASH_MASK);
1146 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Herbert Xu055bcee2006-08-19 22:24:23 +10001149static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1150 u32 type, u32 mask)
1151{
1152 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001153 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001154 type |= CRYPTO_ALG_TYPE_HASH;
1155 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1156
1157 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1158}
1159
1160static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1161{
1162 return &tfm->base;
1163}
1164
1165static inline void crypto_free_hash(struct crypto_hash *tfm)
1166{
1167 crypto_free_tfm(crypto_hash_tfm(tfm));
1168}
1169
Herbert Xufce32d72006-08-26 17:35:45 +10001170static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1171{
1172 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001173 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001174 type |= CRYPTO_ALG_TYPE_HASH;
1175 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1176
1177 return crypto_has_alg(alg_name, type, mask);
1178}
1179
Herbert Xu055bcee2006-08-19 22:24:23 +10001180static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1181{
1182 return &crypto_hash_tfm(tfm)->crt_hash;
1183}
1184
1185static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1186{
1187 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1188}
1189
1190static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1191{
1192 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1193}
1194
1195static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1196{
1197 return crypto_hash_crt(tfm)->digestsize;
1198}
1199
1200static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1201{
1202 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1203}
1204
1205static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1206{
1207 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1208}
1209
1210static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1211{
1212 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1213}
1214
1215static inline int crypto_hash_init(struct hash_desc *desc)
1216{
1217 return crypto_hash_crt(desc->tfm)->init(desc);
1218}
1219
1220static inline int crypto_hash_update(struct hash_desc *desc,
1221 struct scatterlist *sg,
1222 unsigned int nbytes)
1223{
1224 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1225}
1226
1227static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1228{
1229 return crypto_hash_crt(desc->tfm)->final(desc, out);
1230}
1231
1232static inline int crypto_hash_digest(struct hash_desc *desc,
1233 struct scatterlist *sg,
1234 unsigned int nbytes, u8 *out)
1235{
1236 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1237}
1238
1239static inline int crypto_hash_setkey(struct crypto_hash *hash,
1240 const u8 *key, unsigned int keylen)
1241{
1242 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Herbert Xufce32d72006-08-26 17:35:45 +10001245static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1246{
1247 return (struct crypto_comp *)tfm;
1248}
1249
1250static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1251{
1252 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1253 CRYPTO_ALG_TYPE_MASK);
1254 return __crypto_comp_cast(tfm);
1255}
1256
1257static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1258 u32 type, u32 mask)
1259{
1260 type &= ~CRYPTO_ALG_TYPE_MASK;
1261 type |= CRYPTO_ALG_TYPE_COMPRESS;
1262 mask |= CRYPTO_ALG_TYPE_MASK;
1263
1264 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1265}
1266
1267static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1268{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001269 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001270}
1271
1272static inline void crypto_free_comp(struct crypto_comp *tfm)
1273{
1274 crypto_free_tfm(crypto_comp_tfm(tfm));
1275}
1276
1277static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1278{
1279 type &= ~CRYPTO_ALG_TYPE_MASK;
1280 type |= CRYPTO_ALG_TYPE_COMPRESS;
1281 mask |= CRYPTO_ALG_TYPE_MASK;
1282
1283 return crypto_has_alg(alg_name, type, mask);
1284}
1285
Herbert Xue4d5b792006-08-26 18:12:40 +10001286static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1287{
1288 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1289}
1290
Herbert Xufce32d72006-08-26 17:35:45 +10001291static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1292{
1293 return &crypto_comp_tfm(tfm)->crt_compress;
1294}
1295
1296static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 const u8 *src, unsigned int slen,
1298 u8 *dst, unsigned int *dlen)
1299{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001300 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1301 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Herbert Xufce32d72006-08-26 17:35:45 +10001304static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 const u8 *src, unsigned int slen,
1306 u8 *dst, unsigned int *dlen)
1307{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001308 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1309 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#endif /* _LINUX_CRYPTO_H */
1313