blob: de9adec5693c7fd17a9c738439c6d6dda6facf8c [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
Arun Sharma60063492011-07-26 16:09:06 -070020#include <linux/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 Xu332f8842007-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 Xu332f8842007-11-15 22:36:07 +080047#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Herbert Xu28259822006-08-06 21:23:26 +100049#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100050#define CRYPTO_ALG_DEAD 0x00000020
51#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100052#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
Herbert Xu60104392006-08-26 18:34:10 +100055 * Set this bit if and only if the algorithm requires another algorithm of
56 * the same type to handle corner cases.
57 */
58#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
59
60/*
Herbert Xuecfc4322007-12-05 21:08:36 +110061 * This bit is set for symmetric key ciphers that have already been wrapped
62 * with a generic IV generator to prevent them from being wrapped again.
63 */
64#define CRYPTO_ALG_GENIV 0x00000200
65
66/*
Herbert Xu73d38642008-08-03 21:15:23 +080067 * Set if the algorithm has passed automated run-time testing. Note that
68 * if there is no run-time testing for a given algorithm it is considered
69 * to have passed.
70 */
71
72#define CRYPTO_ALG_TESTED 0x00000400
73
74/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020075 * Set if the algorithm is an instance that is build from templates.
76 */
77#define CRYPTO_ALG_INSTANCE 0x00000800
78
79/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * Transform masks and values (for crt_flags).
81 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define CRYPTO_TFM_REQ_MASK 0x000fff00
83#define CRYPTO_TFM_RES_MASK 0xfff00000
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070086#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110087#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
89#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
90#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
91#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
92#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
93
94/*
95 * Miscellaneous stuff.
96 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define CRYPTO_MAX_ALG_NAME 64
98
Herbert Xu79911102006-08-21 21:03:52 +100099/*
100 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
101 * declaration) is used to ensure that the crypto_tfm context structure is
102 * aligned correctly for the given architecture so that there are no alignment
103 * faults for C data types. In particular, this is required on platforms such
104 * as arm where pointers are 32-bit aligned but there are data types such as
105 * u64 which require 64-bit alignment.
106 */
Herbert Xu79911102006-08-21 21:03:52 +1000107#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000108
Herbert Xu79911102006-08-21 21:03:52 +1000109#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100112struct crypto_ablkcipher;
113struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800114struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000115struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000116struct crypto_hash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000117struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700118struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000119struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800120struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800121struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700122
Herbert Xu32e39832007-03-24 14:35:34 +1100123typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
124
125struct crypto_async_request {
126 struct list_head list;
127 crypto_completion_t complete;
128 void *data;
129 struct crypto_tfm *tfm;
130
131 u32 flags;
132};
133
134struct ablkcipher_request {
135 struct crypto_async_request base;
136
137 unsigned int nbytes;
138
139 void *info;
140
141 struct scatterlist *src;
142 struct scatterlist *dst;
143
144 void *__ctx[] CRYPTO_MINALIGN_ATTR;
145};
146
Herbert Xu1ae97822007-08-30 15:36:14 +0800147/**
148 * struct aead_request - AEAD request
149 * @base: Common attributes for async crypto requests
150 * @assoclen: Length in bytes of associated data for authentication
151 * @cryptlen: Length of data to be encrypted or decrypted
152 * @iv: Initialisation vector
153 * @assoc: Associated data
154 * @src: Source data
155 * @dst: Destination data
156 * @__ctx: Start of private context data
157 */
158struct aead_request {
159 struct crypto_async_request base;
160
161 unsigned int assoclen;
162 unsigned int cryptlen;
163
164 u8 *iv;
165
166 struct scatterlist *assoc;
167 struct scatterlist *src;
168 struct scatterlist *dst;
169
170 void *__ctx[] CRYPTO_MINALIGN_ATTR;
171};
172
Herbert Xu5cde0af2006-08-22 00:07:53 +1000173struct blkcipher_desc {
174 struct crypto_blkcipher *tfm;
175 void *info;
176 u32 flags;
177};
178
Herbert Xu40725182005-07-06 13:51:52 -0700179struct cipher_desc {
180 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000181 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700182 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
183 const u8 *src, unsigned int nbytes);
184 void *info;
185};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Herbert Xu055bcee2006-08-19 22:24:23 +1000187struct hash_desc {
188 struct crypto_hash *tfm;
189 u32 flags;
190};
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * Algorithms: modular crypto algorithm implementations, managed
194 * via crypto_register_alg() and crypto_unregister_alg().
195 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000196struct ablkcipher_alg {
197 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
198 unsigned int keylen);
199 int (*encrypt)(struct ablkcipher_request *req);
200 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800201 int (*givencrypt)(struct skcipher_givcrypt_request *req);
202 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000203
Herbert Xu23508e12007-11-27 21:33:24 +0800204 const char *geniv;
205
Herbert Xub5b7f082007-04-16 20:48:54 +1000206 unsigned int min_keysize;
207 unsigned int max_keysize;
208 unsigned int ivsize;
209};
210
Herbert Xu1ae97822007-08-30 15:36:14 +0800211struct aead_alg {
212 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
213 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100214 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800215 int (*encrypt)(struct aead_request *req);
216 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800217 int (*givencrypt)(struct aead_givcrypt_request *req);
218 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800219
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800220 const char *geniv;
221
Herbert Xu1ae97822007-08-30 15:36:14 +0800222 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100223 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800224};
225
Herbert Xu5cde0af2006-08-22 00:07:53 +1000226struct blkcipher_alg {
227 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
228 unsigned int keylen);
229 int (*encrypt)(struct blkcipher_desc *desc,
230 struct scatterlist *dst, struct scatterlist *src,
231 unsigned int nbytes);
232 int (*decrypt)(struct blkcipher_desc *desc,
233 struct scatterlist *dst, struct scatterlist *src,
234 unsigned int nbytes);
235
Herbert Xu23508e12007-11-27 21:33:24 +0800236 const char *geniv;
237
Herbert Xu5cde0af2006-08-22 00:07:53 +1000238 unsigned int min_keysize;
239 unsigned int max_keysize;
240 unsigned int ivsize;
241};
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243struct cipher_alg {
244 unsigned int cia_min_keysize;
245 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000246 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000247 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000248 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
249 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000253 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
254 unsigned int slen, u8 *dst, unsigned int *dlen);
255 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
256 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257};
258
Neil Horman17f0f4a2008-08-14 22:15:52 +1000259struct rng_alg {
260 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
261 unsigned int dlen);
262 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
263
264 unsigned int seedsize;
265};
266
267
Herbert Xub5b7f082007-04-16 20:48:54 +1000268#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800269#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000270#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000273#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275struct crypto_alg {
276 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000277 struct list_head cra_users;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 u32 cra_flags;
280 unsigned int cra_blocksize;
281 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700282 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100283
284 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000285 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100286
Herbert Xud913ea02006-05-21 08:45:26 +1000287 char cra_name[CRYPTO_MAX_ALG_NAME];
288 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Herbert Xue853c3c2006-08-22 00:06:54 +1000290 const struct crypto_type *cra_type;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000293 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800294 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000295 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000298 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000300
301 int (*cra_init)(struct crypto_tfm *tfm);
302 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000303 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 struct module *cra_module;
306};
307
308/*
309 * Algorithm registration interface.
310 */
311int crypto_register_alg(struct crypto_alg *alg);
312int crypto_unregister_alg(struct crypto_alg *alg);
313
314/*
315 * Algorithm query interface.
316 */
Herbert Xufce32d72006-08-26 17:35:45 +1000317int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/*
320 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000321 * and core processing logic. Managed via crypto_alloc_*() and
322 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Herbert Xu32e39832007-03-24 14:35:34 +1100325struct ablkcipher_tfm {
326 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
327 unsigned int keylen);
328 int (*encrypt)(struct ablkcipher_request *req);
329 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800330 int (*givencrypt)(struct skcipher_givcrypt_request *req);
331 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
332
Herbert Xuecfc4322007-12-05 21:08:36 +1100333 struct crypto_ablkcipher *base;
334
Herbert Xu32e39832007-03-24 14:35:34 +1100335 unsigned int ivsize;
336 unsigned int reqsize;
337};
338
Herbert Xu1ae97822007-08-30 15:36:14 +0800339struct aead_tfm {
340 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
341 unsigned int keylen);
342 int (*encrypt)(struct aead_request *req);
343 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800344 int (*givencrypt)(struct aead_givcrypt_request *req);
345 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800346
347 struct crypto_aead *base;
348
Herbert Xu1ae97822007-08-30 15:36:14 +0800349 unsigned int ivsize;
350 unsigned int authsize;
351 unsigned int reqsize;
352};
353
Herbert Xu5cde0af2006-08-22 00:07:53 +1000354struct blkcipher_tfm {
355 void *iv;
356 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
357 unsigned int keylen);
358 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
359 struct scatterlist *src, unsigned int nbytes);
360 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
361 struct scatterlist *src, unsigned int nbytes);
362};
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int (*cit_setkey)(struct crypto_tfm *tfm,
366 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000367 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
368 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
Herbert Xu055bcee2006-08-19 22:24:23 +1000371struct hash_tfm {
372 int (*init)(struct hash_desc *desc);
373 int (*update)(struct hash_desc *desc,
374 struct scatterlist *sg, unsigned int nsg);
375 int (*final)(struct hash_desc *desc, u8 *out);
376 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
377 unsigned int nsg, u8 *out);
378 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
379 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000380 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381};
382
383struct compress_tfm {
384 int (*cot_compress)(struct crypto_tfm *tfm,
385 const u8 *src, unsigned int slen,
386 u8 *dst, unsigned int *dlen);
387 int (*cot_decompress)(struct crypto_tfm *tfm,
388 const u8 *src, unsigned int slen,
389 u8 *dst, unsigned int *dlen);
390};
391
Neil Horman17f0f4a2008-08-14 22:15:52 +1000392struct rng_tfm {
393 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
394 unsigned int dlen);
395 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
396};
397
Herbert Xu32e39832007-03-24 14:35:34 +1100398#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800399#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000400#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000402#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000404#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406struct crypto_tfm {
407
408 u32 crt_flags;
409
410 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100411 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800412 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000413 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000415 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000417 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700419
420 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100423
Herbert Xu79911102006-08-21 21:03:52 +1000424 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
Herbert Xu32e39832007-03-24 14:35:34 +1100427struct crypto_ablkcipher {
428 struct crypto_tfm base;
429};
430
Herbert Xu1ae97822007-08-30 15:36:14 +0800431struct crypto_aead {
432 struct crypto_tfm base;
433};
434
Herbert Xu5cde0af2006-08-22 00:07:53 +1000435struct crypto_blkcipher {
436 struct crypto_tfm base;
437};
438
Herbert Xu78a1fe42006-12-24 10:02:00 +1100439struct crypto_cipher {
440 struct crypto_tfm base;
441};
442
443struct crypto_comp {
444 struct crypto_tfm base;
445};
446
Herbert Xu055bcee2006-08-19 22:24:23 +1000447struct crypto_hash {
448 struct crypto_tfm base;
449};
450
Neil Horman17f0f4a2008-08-14 22:15:52 +1000451struct crypto_rng {
452 struct crypto_tfm base;
453};
454
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000455enum {
456 CRYPTOA_UNSPEC,
457 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100458 CRYPTOA_TYPE,
Herbert Xu39e1ee02007-08-29 19:27:26 +0800459 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100460 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000461};
462
Herbert Xuebc610e2007-01-01 18:37:02 +1100463#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
464
Herbert Xu39e1ee02007-08-29 19:27:26 +0800465/* Maximum number of (rtattr) parameters for each template. */
466#define CRYPTO_MAX_ATTRS 32
467
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000468struct crypto_attr_alg {
469 char name[CRYPTO_MAX_ALG_NAME];
470};
471
Herbert Xuebc610e2007-01-01 18:37:02 +1100472struct crypto_attr_type {
473 u32 type;
474 u32 mask;
475};
476
Herbert Xu39e1ee02007-08-29 19:27:26 +0800477struct crypto_attr_u32 {
478 u32 num;
479};
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Transform user interface.
483 */
484
Herbert Xu6d7d6842006-07-30 11:53:01 +1000485struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100486void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
487
488static inline void crypto_free_tfm(struct crypto_tfm *tfm)
489{
490 return crypto_destroy_tfm(tfm, tfm);
491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Herbert Xuda7f0332008-07-31 17:08:25 +0800493int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*
496 * Transform helpers which query the underlying algorithm.
497 */
498static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
499{
500 return tfm->__crt_alg->cra_name;
501}
502
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000503static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
504{
505 return tfm->__crt_alg->cra_driver_name;
506}
507
508static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
509{
510 return tfm->__crt_alg->cra_priority;
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
514{
515 return module_name(tfm->__crt_alg->cra_module);
516}
517
518static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
519{
520 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
524{
525 return tfm->__crt_alg->cra_blocksize;
526}
527
Herbert Xufbdae9f2005-07-06 13:53:29 -0700528static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
529{
530 return tfm->__crt_alg->cra_alignmask;
531}
532
Herbert Xuf28776a2006-08-13 20:58:18 +1000533static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
534{
535 return tfm->crt_flags;
536}
537
538static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
539{
540 tfm->crt_flags |= flags;
541}
542
543static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
544{
545 tfm->crt_flags &= ~flags;
546}
547
Herbert Xu40725182005-07-06 13:51:52 -0700548static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
549{
Herbert Xuf10b7892006-01-25 22:34:01 +1100550 return tfm->__crt_ctx;
551}
552
553static inline unsigned int crypto_tfm_ctx_alignment(void)
554{
555 struct crypto_tfm *tfm;
556 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
560 * API wrappers.
561 */
Herbert Xu32e39832007-03-24 14:35:34 +1100562static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
563 struct crypto_tfm *tfm)
564{
565 return (struct crypto_ablkcipher *)tfm;
566}
567
Herbert Xu378f4f52007-12-17 20:07:31 +0800568static inline u32 crypto_skcipher_type(u32 type)
569{
Herbert Xuecfc4322007-12-05 21:08:36 +1100570 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800571 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
572 return type;
573}
574
575static inline u32 crypto_skcipher_mask(u32 mask)
576{
Herbert Xuecfc4322007-12-05 21:08:36 +1100577 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800578 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
579 return mask;
580}
581
Herbert Xub9c55aa2007-12-04 12:46:48 +1100582struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
583 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100584
585static inline struct crypto_tfm *crypto_ablkcipher_tfm(
586 struct crypto_ablkcipher *tfm)
587{
588 return &tfm->base;
589}
590
591static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
592{
593 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
594}
595
596static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
597 u32 mask)
598{
Herbert Xu378f4f52007-12-17 20:07:31 +0800599 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
600 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100601}
602
603static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
604 struct crypto_ablkcipher *tfm)
605{
606 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
607}
608
609static inline unsigned int crypto_ablkcipher_ivsize(
610 struct crypto_ablkcipher *tfm)
611{
612 return crypto_ablkcipher_crt(tfm)->ivsize;
613}
614
615static inline unsigned int crypto_ablkcipher_blocksize(
616 struct crypto_ablkcipher *tfm)
617{
618 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
619}
620
621static inline unsigned int crypto_ablkcipher_alignmask(
622 struct crypto_ablkcipher *tfm)
623{
624 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
625}
626
627static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
628{
629 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
630}
631
632static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
633 u32 flags)
634{
635 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
636}
637
638static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
639 u32 flags)
640{
641 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
642}
643
644static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
645 const u8 *key, unsigned int keylen)
646{
Herbert Xuecfc4322007-12-05 21:08:36 +1100647 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
648
649 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100650}
651
652static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
653 struct ablkcipher_request *req)
654{
655 return __crypto_ablkcipher_cast(req->base.tfm);
656}
657
658static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
659{
660 struct ablkcipher_tfm *crt =
661 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
662 return crt->encrypt(req);
663}
664
665static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
666{
667 struct ablkcipher_tfm *crt =
668 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
669 return crt->decrypt(req);
670}
671
Herbert Xub16c3a22007-08-29 19:02:04 +0800672static inline unsigned int crypto_ablkcipher_reqsize(
673 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100674{
675 return crypto_ablkcipher_crt(tfm)->reqsize;
676}
677
Herbert Xue196d622007-04-14 16:09:14 +1000678static inline void ablkcipher_request_set_tfm(
679 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
680{
Herbert Xuecfc4322007-12-05 21:08:36 +1100681 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000682}
683
Herbert Xub5b7f082007-04-16 20:48:54 +1000684static inline struct ablkcipher_request *ablkcipher_request_cast(
685 struct crypto_async_request *req)
686{
687 return container_of(req, struct ablkcipher_request, base);
688}
689
Herbert Xu32e39832007-03-24 14:35:34 +1100690static inline struct ablkcipher_request *ablkcipher_request_alloc(
691 struct crypto_ablkcipher *tfm, gfp_t gfp)
692{
693 struct ablkcipher_request *req;
694
695 req = kmalloc(sizeof(struct ablkcipher_request) +
696 crypto_ablkcipher_reqsize(tfm), gfp);
697
698 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000699 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100700
701 return req;
702}
703
704static inline void ablkcipher_request_free(struct ablkcipher_request *req)
705{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800706 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100707}
708
709static inline void ablkcipher_request_set_callback(
710 struct ablkcipher_request *req,
711 u32 flags, crypto_completion_t complete, void *data)
712{
713 req->base.complete = complete;
714 req->base.data = data;
715 req->base.flags = flags;
716}
717
718static inline void ablkcipher_request_set_crypt(
719 struct ablkcipher_request *req,
720 struct scatterlist *src, struct scatterlist *dst,
721 unsigned int nbytes, void *iv)
722{
723 req->src = src;
724 req->dst = dst;
725 req->nbytes = nbytes;
726 req->info = iv;
727}
728
Herbert Xu1ae97822007-08-30 15:36:14 +0800729static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
730{
731 return (struct crypto_aead *)tfm;
732}
733
Herbert Xud29ce982007-12-12 19:24:27 +0800734struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800735
736static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
737{
738 return &tfm->base;
739}
740
741static inline void crypto_free_aead(struct crypto_aead *tfm)
742{
743 crypto_free_tfm(crypto_aead_tfm(tfm));
744}
745
746static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
747{
748 return &crypto_aead_tfm(tfm)->crt_aead;
749}
750
751static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
752{
753 return crypto_aead_crt(tfm)->ivsize;
754}
755
756static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
757{
758 return crypto_aead_crt(tfm)->authsize;
759}
760
761static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
762{
763 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
764}
765
766static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
767{
768 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
769}
770
771static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
772{
773 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
774}
775
776static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
777{
778 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
779}
780
781static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
782{
783 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
784}
785
786static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
787 unsigned int keylen)
788{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800789 struct aead_tfm *crt = crypto_aead_crt(tfm);
790
791 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800792}
793
Herbert Xu7ba683a2007-12-02 18:49:21 +1100794int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
795
Herbert Xu1ae97822007-08-30 15:36:14 +0800796static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
797{
798 return __crypto_aead_cast(req->base.tfm);
799}
800
801static inline int crypto_aead_encrypt(struct aead_request *req)
802{
803 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
804}
805
806static inline int crypto_aead_decrypt(struct aead_request *req)
807{
808 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
809}
810
Herbert Xub16c3a22007-08-29 19:02:04 +0800811static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800812{
813 return crypto_aead_crt(tfm)->reqsize;
814}
815
816static inline void aead_request_set_tfm(struct aead_request *req,
817 struct crypto_aead *tfm)
818{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800819 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800820}
821
822static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
823 gfp_t gfp)
824{
825 struct aead_request *req;
826
827 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
828
829 if (likely(req))
830 aead_request_set_tfm(req, tfm);
831
832 return req;
833}
834
835static inline void aead_request_free(struct aead_request *req)
836{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800837 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800838}
839
840static inline void aead_request_set_callback(struct aead_request *req,
841 u32 flags,
842 crypto_completion_t complete,
843 void *data)
844{
845 req->base.complete = complete;
846 req->base.data = data;
847 req->base.flags = flags;
848}
849
850static inline void aead_request_set_crypt(struct aead_request *req,
851 struct scatterlist *src,
852 struct scatterlist *dst,
853 unsigned int cryptlen, u8 *iv)
854{
855 req->src = src;
856 req->dst = dst;
857 req->cryptlen = cryptlen;
858 req->iv = iv;
859}
860
861static inline void aead_request_set_assoc(struct aead_request *req,
862 struct scatterlist *assoc,
863 unsigned int assoclen)
864{
865 req->assoc = assoc;
866 req->assoclen = assoclen;
867}
868
Herbert Xu5cde0af2006-08-22 00:07:53 +1000869static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
870 struct crypto_tfm *tfm)
871{
872 return (struct crypto_blkcipher *)tfm;
873}
874
875static inline struct crypto_blkcipher *crypto_blkcipher_cast(
876 struct crypto_tfm *tfm)
877{
878 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
879 return __crypto_blkcipher_cast(tfm);
880}
881
882static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
883 const char *alg_name, u32 type, u32 mask)
884{
Herbert Xu332f8842007-11-15 22:36:07 +0800885 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000886 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800887 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000888
889 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
890}
891
892static inline struct crypto_tfm *crypto_blkcipher_tfm(
893 struct crypto_blkcipher *tfm)
894{
895 return &tfm->base;
896}
897
898static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
899{
900 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
901}
902
Herbert Xufce32d72006-08-26 17:35:45 +1000903static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
904{
Herbert Xu332f8842007-11-15 22:36:07 +0800905 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000906 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800907 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000908
909 return crypto_has_alg(alg_name, type, mask);
910}
911
Herbert Xu5cde0af2006-08-22 00:07:53 +1000912static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
913{
914 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
915}
916
917static inline struct blkcipher_tfm *crypto_blkcipher_crt(
918 struct crypto_blkcipher *tfm)
919{
920 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
921}
922
923static inline struct blkcipher_alg *crypto_blkcipher_alg(
924 struct crypto_blkcipher *tfm)
925{
926 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
927}
928
929static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
930{
931 return crypto_blkcipher_alg(tfm)->ivsize;
932}
933
934static inline unsigned int crypto_blkcipher_blocksize(
935 struct crypto_blkcipher *tfm)
936{
937 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
938}
939
940static inline unsigned int crypto_blkcipher_alignmask(
941 struct crypto_blkcipher *tfm)
942{
943 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
944}
945
946static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
947{
948 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
949}
950
951static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
952 u32 flags)
953{
954 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
955}
956
957static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
958 u32 flags)
959{
960 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
961}
962
963static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
964 const u8 *key, unsigned int keylen)
965{
966 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
967 key, keylen);
968}
969
970static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
971 struct scatterlist *dst,
972 struct scatterlist *src,
973 unsigned int nbytes)
974{
975 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
976 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
977}
978
979static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
980 struct scatterlist *dst,
981 struct scatterlist *src,
982 unsigned int nbytes)
983{
984 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
985}
986
987static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
988 struct scatterlist *dst,
989 struct scatterlist *src,
990 unsigned int nbytes)
991{
992 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
993 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
994}
995
996static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
997 struct scatterlist *dst,
998 struct scatterlist *src,
999 unsigned int nbytes)
1000{
1001 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1002}
1003
1004static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1005 const u8 *src, unsigned int len)
1006{
1007 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1008}
1009
1010static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1011 u8 *dst, unsigned int len)
1012{
1013 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1014}
1015
Herbert Xuf28776a2006-08-13 20:58:18 +10001016static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1017{
1018 return (struct crypto_cipher *)tfm;
1019}
1020
1021static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1022{
1023 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1024 return __crypto_cipher_cast(tfm);
1025}
1026
1027static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1028 u32 type, u32 mask)
1029{
1030 type &= ~CRYPTO_ALG_TYPE_MASK;
1031 type |= CRYPTO_ALG_TYPE_CIPHER;
1032 mask |= CRYPTO_ALG_TYPE_MASK;
1033
1034 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1035}
1036
1037static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1038{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001039 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001040}
1041
1042static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1043{
1044 crypto_free_tfm(crypto_cipher_tfm(tfm));
1045}
1046
Herbert Xufce32d72006-08-26 17:35:45 +10001047static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1048{
1049 type &= ~CRYPTO_ALG_TYPE_MASK;
1050 type |= CRYPTO_ALG_TYPE_CIPHER;
1051 mask |= CRYPTO_ALG_TYPE_MASK;
1052
1053 return crypto_has_alg(alg_name, type, mask);
1054}
1055
Herbert Xuf28776a2006-08-13 20:58:18 +10001056static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1057{
1058 return &crypto_cipher_tfm(tfm)->crt_cipher;
1059}
1060
1061static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1062{
1063 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1064}
1065
1066static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1067{
1068 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1069}
1070
1071static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1072{
1073 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1074}
1075
1076static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1077 u32 flags)
1078{
1079 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1080}
1081
1082static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1083 u32 flags)
1084{
1085 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1086}
1087
Herbert Xu7226bc82006-08-21 21:40:49 +10001088static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1089 const u8 *key, unsigned int keylen)
1090{
1091 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1092 key, keylen);
1093}
1094
Herbert Xuf28776a2006-08-13 20:58:18 +10001095static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1096 u8 *dst, const u8 *src)
1097{
1098 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1099 dst, src);
1100}
1101
1102static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1103 u8 *dst, const u8 *src)
1104{
1105 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1106 dst, src);
1107}
1108
Herbert Xu055bcee2006-08-19 22:24:23 +10001109static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Herbert Xu055bcee2006-08-19 22:24:23 +10001111 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Herbert Xu055bcee2006-08-19 22:24:23 +10001114static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Herbert Xu055bcee2006-08-19 22:24:23 +10001116 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1117 CRYPTO_ALG_TYPE_HASH_MASK);
1118 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Herbert Xu055bcee2006-08-19 22:24:23 +10001121static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1122 u32 type, u32 mask)
1123{
1124 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001125 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001126 type |= CRYPTO_ALG_TYPE_HASH;
1127 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1128
1129 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1130}
1131
1132static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1133{
1134 return &tfm->base;
1135}
1136
1137static inline void crypto_free_hash(struct crypto_hash *tfm)
1138{
1139 crypto_free_tfm(crypto_hash_tfm(tfm));
1140}
1141
Herbert Xufce32d72006-08-26 17:35:45 +10001142static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1143{
1144 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001145 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001146 type |= CRYPTO_ALG_TYPE_HASH;
1147 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1148
1149 return crypto_has_alg(alg_name, type, mask);
1150}
1151
Herbert Xu055bcee2006-08-19 22:24:23 +10001152static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1153{
1154 return &crypto_hash_tfm(tfm)->crt_hash;
1155}
1156
1157static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1158{
1159 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1160}
1161
1162static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1163{
1164 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1165}
1166
1167static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1168{
1169 return crypto_hash_crt(tfm)->digestsize;
1170}
1171
1172static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1173{
1174 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1175}
1176
1177static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1178{
1179 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1180}
1181
1182static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1183{
1184 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1185}
1186
1187static inline int crypto_hash_init(struct hash_desc *desc)
1188{
1189 return crypto_hash_crt(desc->tfm)->init(desc);
1190}
1191
1192static inline int crypto_hash_update(struct hash_desc *desc,
1193 struct scatterlist *sg,
1194 unsigned int nbytes)
1195{
1196 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1197}
1198
1199static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1200{
1201 return crypto_hash_crt(desc->tfm)->final(desc, out);
1202}
1203
1204static inline int crypto_hash_digest(struct hash_desc *desc,
1205 struct scatterlist *sg,
1206 unsigned int nbytes, u8 *out)
1207{
1208 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1209}
1210
1211static inline int crypto_hash_setkey(struct crypto_hash *hash,
1212 const u8 *key, unsigned int keylen)
1213{
1214 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Herbert Xufce32d72006-08-26 17:35:45 +10001217static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1218{
1219 return (struct crypto_comp *)tfm;
1220}
1221
1222static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1223{
1224 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1225 CRYPTO_ALG_TYPE_MASK);
1226 return __crypto_comp_cast(tfm);
1227}
1228
1229static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1230 u32 type, u32 mask)
1231{
1232 type &= ~CRYPTO_ALG_TYPE_MASK;
1233 type |= CRYPTO_ALG_TYPE_COMPRESS;
1234 mask |= CRYPTO_ALG_TYPE_MASK;
1235
1236 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1237}
1238
1239static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1240{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001241 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001242}
1243
1244static inline void crypto_free_comp(struct crypto_comp *tfm)
1245{
1246 crypto_free_tfm(crypto_comp_tfm(tfm));
1247}
1248
1249static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1250{
1251 type &= ~CRYPTO_ALG_TYPE_MASK;
1252 type |= CRYPTO_ALG_TYPE_COMPRESS;
1253 mask |= CRYPTO_ALG_TYPE_MASK;
1254
1255 return crypto_has_alg(alg_name, type, mask);
1256}
1257
Herbert Xue4d5b792006-08-26 18:12:40 +10001258static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1259{
1260 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1261}
1262
Herbert Xufce32d72006-08-26 17:35:45 +10001263static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1264{
1265 return &crypto_comp_tfm(tfm)->crt_compress;
1266}
1267
1268static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 const u8 *src, unsigned int slen,
1270 u8 *dst, unsigned int *dlen)
1271{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001272 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1273 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Herbert Xufce32d72006-08-26 17:35:45 +10001276static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 const u8 *src, unsigned int slen,
1278 u8 *dst, unsigned int *dlen)
1279{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001280 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1281 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#endif /* _LINUX_CRYPTO_H */
1285