blob: ec29fa268b94e278e5125380f8e2b1faa20c10c9 [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;
Loc Ho004a4032008-05-14 20:41:47 +0800118struct crypto_ahash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000119struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700120struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000121struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800122struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800123struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700124
Herbert Xu32e39832007-03-24 14:35:34 +1100125typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
126
127struct crypto_async_request {
128 struct list_head list;
129 crypto_completion_t complete;
130 void *data;
131 struct crypto_tfm *tfm;
132
133 u32 flags;
134};
135
136struct ablkcipher_request {
137 struct crypto_async_request base;
138
139 unsigned int nbytes;
140
141 void *info;
142
143 struct scatterlist *src;
144 struct scatterlist *dst;
145
146 void *__ctx[] CRYPTO_MINALIGN_ATTR;
147};
148
Loc Ho004a4032008-05-14 20:41:47 +0800149struct ahash_request {
150 struct crypto_async_request base;
151
Loc Ho004a4032008-05-14 20:41:47 +0800152 unsigned int nbytes;
153 struct scatterlist *src;
154 u8 *result;
155
156 void *__ctx[] CRYPTO_MINALIGN_ATTR;
157};
158
Herbert Xu1ae97822007-08-30 15:36:14 +0800159/**
160 * struct aead_request - AEAD request
161 * @base: Common attributes for async crypto requests
162 * @assoclen: Length in bytes of associated data for authentication
163 * @cryptlen: Length of data to be encrypted or decrypted
164 * @iv: Initialisation vector
165 * @assoc: Associated data
166 * @src: Source data
167 * @dst: Destination data
168 * @__ctx: Start of private context data
169 */
170struct aead_request {
171 struct crypto_async_request base;
172
173 unsigned int assoclen;
174 unsigned int cryptlen;
175
176 u8 *iv;
177
178 struct scatterlist *assoc;
179 struct scatterlist *src;
180 struct scatterlist *dst;
181
182 void *__ctx[] CRYPTO_MINALIGN_ATTR;
183};
184
Herbert Xu5cde0af2006-08-22 00:07:53 +1000185struct blkcipher_desc {
186 struct crypto_blkcipher *tfm;
187 void *info;
188 u32 flags;
189};
190
Herbert Xu40725182005-07-06 13:51:52 -0700191struct cipher_desc {
192 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000193 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700194 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
195 const u8 *src, unsigned int nbytes);
196 void *info;
197};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Herbert Xu055bcee2006-08-19 22:24:23 +1000199struct hash_desc {
200 struct crypto_hash *tfm;
201 u32 flags;
202};
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * Algorithms: modular crypto algorithm implementations, managed
206 * via crypto_register_alg() and crypto_unregister_alg().
207 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000208struct ablkcipher_alg {
209 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
210 unsigned int keylen);
211 int (*encrypt)(struct ablkcipher_request *req);
212 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800213 int (*givencrypt)(struct skcipher_givcrypt_request *req);
214 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000215
Herbert Xu23508e12007-11-27 21:33:24 +0800216 const char *geniv;
217
Herbert Xub5b7f082007-04-16 20:48:54 +1000218 unsigned int min_keysize;
219 unsigned int max_keysize;
220 unsigned int ivsize;
221};
222
Loc Ho004a4032008-05-14 20:41:47 +0800223struct ahash_alg {
224 int (*init)(struct ahash_request *req);
Herbert Xudec8b782008-11-02 21:38:11 +0800225 int (*reinit)(struct ahash_request *req);
Loc Ho004a4032008-05-14 20:41:47 +0800226 int (*update)(struct ahash_request *req);
227 int (*final)(struct ahash_request *req);
228 int (*digest)(struct ahash_request *req);
229 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
230 unsigned int keylen);
231
232 unsigned int digestsize;
233};
234
Herbert Xu1ae97822007-08-30 15:36:14 +0800235struct aead_alg {
236 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
237 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100238 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800239 int (*encrypt)(struct aead_request *req);
240 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800241 int (*givencrypt)(struct aead_givcrypt_request *req);
242 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800243
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800244 const char *geniv;
245
Herbert Xu1ae97822007-08-30 15:36:14 +0800246 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100247 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800248};
249
Herbert Xu5cde0af2006-08-22 00:07:53 +1000250struct blkcipher_alg {
251 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
252 unsigned int keylen);
253 int (*encrypt)(struct blkcipher_desc *desc,
254 struct scatterlist *dst, struct scatterlist *src,
255 unsigned int nbytes);
256 int (*decrypt)(struct blkcipher_desc *desc,
257 struct scatterlist *dst, struct scatterlist *src,
258 unsigned int nbytes);
259
Herbert Xu23508e12007-11-27 21:33:24 +0800260 const char *geniv;
261
Herbert Xu5cde0af2006-08-22 00:07:53 +1000262 unsigned int min_keysize;
263 unsigned int max_keysize;
264 unsigned int ivsize;
265};
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267struct cipher_alg {
268 unsigned int cia_min_keysize;
269 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000270 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000271 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000272 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
273 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
276struct digest_alg {
277 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000278 void (*dia_init)(struct crypto_tfm *tfm);
279 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
280 unsigned int len);
281 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
282 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000283 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284};
285
Herbert Xu055bcee2006-08-19 22:24:23 +1000286struct hash_alg {
287 int (*init)(struct hash_desc *desc);
288 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
289 unsigned int nbytes);
290 int (*final)(struct hash_desc *desc, u8 *out);
291 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
292 unsigned int nbytes, u8 *out);
293 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
294 unsigned int keylen);
295
296 unsigned int digestsize;
297};
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000300 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
301 unsigned int slen, u8 *dst, unsigned int *dlen);
302 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
303 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Neil Horman17f0f4a2008-08-14 22:15:52 +1000306struct rng_alg {
307 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
308 unsigned int dlen);
309 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
310
311 unsigned int seedsize;
312};
313
314
Herbert Xub5b7f082007-04-16 20:48:54 +1000315#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800316#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000317#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define cra_cipher cra_u.cipher
319#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000320#define cra_hash cra_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800321#define cra_ahash cra_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000323#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325struct crypto_alg {
326 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000327 struct list_head cra_users;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 u32 cra_flags;
330 unsigned int cra_blocksize;
331 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700332 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100333
334 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000335 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100336
Herbert Xud913ea02006-05-21 08:45:26 +1000337 char cra_name[CRYPTO_MAX_ALG_NAME];
338 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Herbert Xue853c3c2006-08-22 00:06:54 +1000340 const struct crypto_type *cra_type;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000343 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800344 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000345 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct cipher_alg cipher;
347 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000348 struct hash_alg hash;
Loc Ho004a4032008-05-14 20:41:47 +0800349 struct ahash_alg ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000351 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000353
354 int (*cra_init)(struct crypto_tfm *tfm);
355 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000356 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 struct module *cra_module;
359};
360
361/*
362 * Algorithm registration interface.
363 */
364int crypto_register_alg(struct crypto_alg *alg);
365int crypto_unregister_alg(struct crypto_alg *alg);
366
367/*
368 * Algorithm query interface.
369 */
Herbert Xufce32d72006-08-26 17:35:45 +1000370int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/*
373 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000374 * and core processing logic. Managed via crypto_alloc_*() and
375 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Herbert Xu32e39832007-03-24 14:35:34 +1100378struct ablkcipher_tfm {
379 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
380 unsigned int keylen);
381 int (*encrypt)(struct ablkcipher_request *req);
382 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800383 int (*givencrypt)(struct skcipher_givcrypt_request *req);
384 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
385
Herbert Xuecfc4322007-12-05 21:08:36 +1100386 struct crypto_ablkcipher *base;
387
Herbert Xu32e39832007-03-24 14:35:34 +1100388 unsigned int ivsize;
389 unsigned int reqsize;
390};
391
Herbert Xu1ae97822007-08-30 15:36:14 +0800392struct aead_tfm {
393 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
394 unsigned int keylen);
395 int (*encrypt)(struct aead_request *req);
396 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800397 int (*givencrypt)(struct aead_givcrypt_request *req);
398 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800399
400 struct crypto_aead *base;
401
Herbert Xu1ae97822007-08-30 15:36:14 +0800402 unsigned int ivsize;
403 unsigned int authsize;
404 unsigned int reqsize;
405};
406
Herbert Xu5cde0af2006-08-22 00:07:53 +1000407struct blkcipher_tfm {
408 void *iv;
409 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
410 unsigned int keylen);
411 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
412 struct scatterlist *src, unsigned int nbytes);
413 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
414 struct scatterlist *src, unsigned int nbytes);
415};
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int (*cit_setkey)(struct crypto_tfm *tfm,
419 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000420 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
421 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
Herbert Xu055bcee2006-08-19 22:24:23 +1000424struct hash_tfm {
425 int (*init)(struct hash_desc *desc);
426 int (*update)(struct hash_desc *desc,
427 struct scatterlist *sg, unsigned int nsg);
428 int (*final)(struct hash_desc *desc, u8 *out);
429 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
430 unsigned int nsg, u8 *out);
431 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
432 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000433 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
Loc Ho004a4032008-05-14 20:41:47 +0800436struct ahash_tfm {
437 int (*init)(struct ahash_request *req);
438 int (*update)(struct ahash_request *req);
439 int (*final)(struct ahash_request *req);
440 int (*digest)(struct ahash_request *req);
441 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
442 unsigned int keylen);
443
444 unsigned int digestsize;
Loc Ho004a4032008-05-14 20:41:47 +0800445 unsigned int reqsize;
446};
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448struct compress_tfm {
449 int (*cot_compress)(struct crypto_tfm *tfm,
450 const u8 *src, unsigned int slen,
451 u8 *dst, unsigned int *dlen);
452 int (*cot_decompress)(struct crypto_tfm *tfm,
453 const u8 *src, unsigned int slen,
454 u8 *dst, unsigned int *dlen);
455};
456
Neil Horman17f0f4a2008-08-14 22:15:52 +1000457struct rng_tfm {
458 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
459 unsigned int dlen);
460 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
461};
462
Herbert Xu32e39832007-03-24 14:35:34 +1100463#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800464#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000465#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000467#define crt_hash crt_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800468#define crt_ahash crt_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000470#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472struct crypto_tfm {
473
474 u32 crt_flags;
475
476 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100477 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800478 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000479 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000481 struct hash_tfm hash;
Loc Ho004a4032008-05-14 20:41:47 +0800482 struct ahash_tfm ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000484 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700486
487 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100490
Herbert Xu79911102006-08-21 21:03:52 +1000491 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492};
493
Herbert Xu32e39832007-03-24 14:35:34 +1100494struct crypto_ablkcipher {
495 struct crypto_tfm base;
496};
497
Herbert Xu1ae97822007-08-30 15:36:14 +0800498struct crypto_aead {
499 struct crypto_tfm base;
500};
501
Herbert Xu5cde0af2006-08-22 00:07:53 +1000502struct crypto_blkcipher {
503 struct crypto_tfm base;
504};
505
Herbert Xu78a1fe42006-12-24 10:02:00 +1100506struct crypto_cipher {
507 struct crypto_tfm base;
508};
509
510struct crypto_comp {
511 struct crypto_tfm base;
512};
513
Herbert Xu055bcee2006-08-19 22:24:23 +1000514struct crypto_hash {
515 struct crypto_tfm base;
516};
517
Neil Horman17f0f4a2008-08-14 22:15:52 +1000518struct crypto_rng {
519 struct crypto_tfm base;
520};
521
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000522enum {
523 CRYPTOA_UNSPEC,
524 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100525 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800526 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100527 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000528};
529
Herbert Xuebc610e2007-01-01 18:37:02 +1100530#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
531
Herbert Xu39e1ee012007-08-29 19:27:26 +0800532/* Maximum number of (rtattr) parameters for each template. */
533#define CRYPTO_MAX_ATTRS 32
534
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000535struct crypto_attr_alg {
536 char name[CRYPTO_MAX_ALG_NAME];
537};
538
Herbert Xuebc610e2007-01-01 18:37:02 +1100539struct crypto_attr_type {
540 u32 type;
541 u32 mask;
542};
543
Herbert Xu39e1ee012007-08-29 19:27:26 +0800544struct crypto_attr_u32 {
545 u32 num;
546};
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
549 * Transform user interface.
550 */
551
Herbert Xu6d7d6842006-07-30 11:53:01 +1000552struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100553void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
554
555static inline void crypto_free_tfm(struct crypto_tfm *tfm)
556{
557 return crypto_destroy_tfm(tfm, tfm);
558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Herbert Xuda7f0332008-07-31 17:08:25 +0800560int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
563 * Transform helpers which query the underlying algorithm.
564 */
565static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
566{
567 return tfm->__crt_alg->cra_name;
568}
569
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000570static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
571{
572 return tfm->__crt_alg->cra_driver_name;
573}
574
575static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
576{
577 return tfm->__crt_alg->cra_priority;
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
581{
582 return module_name(tfm->__crt_alg->cra_module);
583}
584
585static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
586{
587 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
591{
592 return tfm->__crt_alg->cra_blocksize;
593}
594
Herbert Xufbdae9f2005-07-06 13:53:29 -0700595static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
596{
597 return tfm->__crt_alg->cra_alignmask;
598}
599
Herbert Xuf28776a2006-08-13 20:58:18 +1000600static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
601{
602 return tfm->crt_flags;
603}
604
605static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
606{
607 tfm->crt_flags |= flags;
608}
609
610static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
611{
612 tfm->crt_flags &= ~flags;
613}
614
Herbert Xu40725182005-07-06 13:51:52 -0700615static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
616{
Herbert Xuf10b7892006-01-25 22:34:01 +1100617 return tfm->__crt_ctx;
618}
619
620static inline unsigned int crypto_tfm_ctx_alignment(void)
621{
622 struct crypto_tfm *tfm;
623 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * API wrappers.
628 */
Herbert Xu32e39832007-03-24 14:35:34 +1100629static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
630 struct crypto_tfm *tfm)
631{
632 return (struct crypto_ablkcipher *)tfm;
633}
634
Herbert Xu378f4f52007-12-17 20:07:31 +0800635static inline u32 crypto_skcipher_type(u32 type)
636{
Herbert Xuecfc4322007-12-05 21:08:36 +1100637 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800638 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
639 return type;
640}
641
642static inline u32 crypto_skcipher_mask(u32 mask)
643{
Herbert Xuecfc4322007-12-05 21:08:36 +1100644 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800645 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
646 return mask;
647}
648
Herbert Xub9c55aa2007-12-04 12:46:48 +1100649struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
650 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100651
652static inline struct crypto_tfm *crypto_ablkcipher_tfm(
653 struct crypto_ablkcipher *tfm)
654{
655 return &tfm->base;
656}
657
658static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
659{
660 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
661}
662
663static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
664 u32 mask)
665{
Herbert Xu378f4f52007-12-17 20:07:31 +0800666 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
667 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100668}
669
670static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
671 struct crypto_ablkcipher *tfm)
672{
673 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
674}
675
676static inline unsigned int crypto_ablkcipher_ivsize(
677 struct crypto_ablkcipher *tfm)
678{
679 return crypto_ablkcipher_crt(tfm)->ivsize;
680}
681
682static inline unsigned int crypto_ablkcipher_blocksize(
683 struct crypto_ablkcipher *tfm)
684{
685 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
686}
687
688static inline unsigned int crypto_ablkcipher_alignmask(
689 struct crypto_ablkcipher *tfm)
690{
691 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
692}
693
694static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
695{
696 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
697}
698
699static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
700 u32 flags)
701{
702 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
703}
704
705static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
706 u32 flags)
707{
708 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
709}
710
711static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
712 const u8 *key, unsigned int keylen)
713{
Herbert Xuecfc4322007-12-05 21:08:36 +1100714 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
715
716 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100717}
718
719static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
720 struct ablkcipher_request *req)
721{
722 return __crypto_ablkcipher_cast(req->base.tfm);
723}
724
725static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
726{
727 struct ablkcipher_tfm *crt =
728 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
729 return crt->encrypt(req);
730}
731
732static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
733{
734 struct ablkcipher_tfm *crt =
735 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
736 return crt->decrypt(req);
737}
738
Herbert Xub16c3a22007-08-29 19:02:04 +0800739static inline unsigned int crypto_ablkcipher_reqsize(
740 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100741{
742 return crypto_ablkcipher_crt(tfm)->reqsize;
743}
744
Herbert Xue196d622007-04-14 16:09:14 +1000745static inline void ablkcipher_request_set_tfm(
746 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
747{
Herbert Xuecfc4322007-12-05 21:08:36 +1100748 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000749}
750
Herbert Xub5b7f082007-04-16 20:48:54 +1000751static inline struct ablkcipher_request *ablkcipher_request_cast(
752 struct crypto_async_request *req)
753{
754 return container_of(req, struct ablkcipher_request, base);
755}
756
Herbert Xu32e39832007-03-24 14:35:34 +1100757static inline struct ablkcipher_request *ablkcipher_request_alloc(
758 struct crypto_ablkcipher *tfm, gfp_t gfp)
759{
760 struct ablkcipher_request *req;
761
762 req = kmalloc(sizeof(struct ablkcipher_request) +
763 crypto_ablkcipher_reqsize(tfm), gfp);
764
765 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000766 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100767
768 return req;
769}
770
771static inline void ablkcipher_request_free(struct ablkcipher_request *req)
772{
773 kfree(req);
774}
775
776static inline void ablkcipher_request_set_callback(
777 struct ablkcipher_request *req,
778 u32 flags, crypto_completion_t complete, void *data)
779{
780 req->base.complete = complete;
781 req->base.data = data;
782 req->base.flags = flags;
783}
784
785static inline void ablkcipher_request_set_crypt(
786 struct ablkcipher_request *req,
787 struct scatterlist *src, struct scatterlist *dst,
788 unsigned int nbytes, void *iv)
789{
790 req->src = src;
791 req->dst = dst;
792 req->nbytes = nbytes;
793 req->info = iv;
794}
795
Herbert Xu1ae97822007-08-30 15:36:14 +0800796static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
797{
798 return (struct crypto_aead *)tfm;
799}
800
Herbert Xud29ce982007-12-12 19:24:27 +0800801struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800802
803static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
804{
805 return &tfm->base;
806}
807
808static inline void crypto_free_aead(struct crypto_aead *tfm)
809{
810 crypto_free_tfm(crypto_aead_tfm(tfm));
811}
812
813static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
814{
815 return &crypto_aead_tfm(tfm)->crt_aead;
816}
817
818static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
819{
820 return crypto_aead_crt(tfm)->ivsize;
821}
822
823static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
824{
825 return crypto_aead_crt(tfm)->authsize;
826}
827
828static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
829{
830 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
831}
832
833static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
834{
835 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
836}
837
838static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
839{
840 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
841}
842
843static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
844{
845 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
846}
847
848static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
849{
850 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
851}
852
853static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
854 unsigned int keylen)
855{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800856 struct aead_tfm *crt = crypto_aead_crt(tfm);
857
858 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800859}
860
Herbert Xu7ba683a2007-12-02 18:49:21 +1100861int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
862
Herbert Xu1ae97822007-08-30 15:36:14 +0800863static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
864{
865 return __crypto_aead_cast(req->base.tfm);
866}
867
868static inline int crypto_aead_encrypt(struct aead_request *req)
869{
870 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
871}
872
873static inline int crypto_aead_decrypt(struct aead_request *req)
874{
875 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
876}
877
Herbert Xub16c3a22007-08-29 19:02:04 +0800878static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800879{
880 return crypto_aead_crt(tfm)->reqsize;
881}
882
883static inline void aead_request_set_tfm(struct aead_request *req,
884 struct crypto_aead *tfm)
885{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800886 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800887}
888
889static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
890 gfp_t gfp)
891{
892 struct aead_request *req;
893
894 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
895
896 if (likely(req))
897 aead_request_set_tfm(req, tfm);
898
899 return req;
900}
901
902static inline void aead_request_free(struct aead_request *req)
903{
904 kfree(req);
905}
906
907static inline void aead_request_set_callback(struct aead_request *req,
908 u32 flags,
909 crypto_completion_t complete,
910 void *data)
911{
912 req->base.complete = complete;
913 req->base.data = data;
914 req->base.flags = flags;
915}
916
917static inline void aead_request_set_crypt(struct aead_request *req,
918 struct scatterlist *src,
919 struct scatterlist *dst,
920 unsigned int cryptlen, u8 *iv)
921{
922 req->src = src;
923 req->dst = dst;
924 req->cryptlen = cryptlen;
925 req->iv = iv;
926}
927
928static inline void aead_request_set_assoc(struct aead_request *req,
929 struct scatterlist *assoc,
930 unsigned int assoclen)
931{
932 req->assoc = assoc;
933 req->assoclen = assoclen;
934}
935
Herbert Xu5cde0af2006-08-22 00:07:53 +1000936static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
937 struct crypto_tfm *tfm)
938{
939 return (struct crypto_blkcipher *)tfm;
940}
941
942static inline struct crypto_blkcipher *crypto_blkcipher_cast(
943 struct crypto_tfm *tfm)
944{
945 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
946 return __crypto_blkcipher_cast(tfm);
947}
948
949static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
950 const char *alg_name, u32 type, u32 mask)
951{
Herbert Xu332f88402007-11-15 22:36:07 +0800952 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000953 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800954 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000955
956 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
957}
958
959static inline struct crypto_tfm *crypto_blkcipher_tfm(
960 struct crypto_blkcipher *tfm)
961{
962 return &tfm->base;
963}
964
965static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
966{
967 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
968}
969
Herbert Xufce32d72006-08-26 17:35:45 +1000970static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
971{
Herbert Xu332f88402007-11-15 22:36:07 +0800972 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000973 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800974 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000975
976 return crypto_has_alg(alg_name, type, mask);
977}
978
Herbert Xu5cde0af2006-08-22 00:07:53 +1000979static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
980{
981 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
982}
983
984static inline struct blkcipher_tfm *crypto_blkcipher_crt(
985 struct crypto_blkcipher *tfm)
986{
987 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
988}
989
990static inline struct blkcipher_alg *crypto_blkcipher_alg(
991 struct crypto_blkcipher *tfm)
992{
993 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
994}
995
996static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
997{
998 return crypto_blkcipher_alg(tfm)->ivsize;
999}
1000
1001static inline unsigned int crypto_blkcipher_blocksize(
1002 struct crypto_blkcipher *tfm)
1003{
1004 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1005}
1006
1007static inline unsigned int crypto_blkcipher_alignmask(
1008 struct crypto_blkcipher *tfm)
1009{
1010 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1011}
1012
1013static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1014{
1015 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1016}
1017
1018static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1019 u32 flags)
1020{
1021 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1022}
1023
1024static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1025 u32 flags)
1026{
1027 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1028}
1029
1030static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1031 const u8 *key, unsigned int keylen)
1032{
1033 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1034 key, keylen);
1035}
1036
1037static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1038 struct scatterlist *dst,
1039 struct scatterlist *src,
1040 unsigned int nbytes)
1041{
1042 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1043 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1044}
1045
1046static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1047 struct scatterlist *dst,
1048 struct scatterlist *src,
1049 unsigned int nbytes)
1050{
1051 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1052}
1053
1054static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1055 struct scatterlist *dst,
1056 struct scatterlist *src,
1057 unsigned int nbytes)
1058{
1059 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1060 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1061}
1062
1063static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1064 struct scatterlist *dst,
1065 struct scatterlist *src,
1066 unsigned int nbytes)
1067{
1068 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1069}
1070
1071static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1072 const u8 *src, unsigned int len)
1073{
1074 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1075}
1076
1077static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1078 u8 *dst, unsigned int len)
1079{
1080 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1081}
1082
Herbert Xuf28776a2006-08-13 20:58:18 +10001083static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1084{
1085 return (struct crypto_cipher *)tfm;
1086}
1087
1088static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1089{
1090 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1091 return __crypto_cipher_cast(tfm);
1092}
1093
1094static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1095 u32 type, u32 mask)
1096{
1097 type &= ~CRYPTO_ALG_TYPE_MASK;
1098 type |= CRYPTO_ALG_TYPE_CIPHER;
1099 mask |= CRYPTO_ALG_TYPE_MASK;
1100
1101 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1102}
1103
1104static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1105{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001106 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001107}
1108
1109static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1110{
1111 crypto_free_tfm(crypto_cipher_tfm(tfm));
1112}
1113
Herbert Xufce32d72006-08-26 17:35:45 +10001114static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1115{
1116 type &= ~CRYPTO_ALG_TYPE_MASK;
1117 type |= CRYPTO_ALG_TYPE_CIPHER;
1118 mask |= CRYPTO_ALG_TYPE_MASK;
1119
1120 return crypto_has_alg(alg_name, type, mask);
1121}
1122
Herbert Xuf28776a2006-08-13 20:58:18 +10001123static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1124{
1125 return &crypto_cipher_tfm(tfm)->crt_cipher;
1126}
1127
1128static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1129{
1130 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1131}
1132
1133static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1134{
1135 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1136}
1137
1138static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1139{
1140 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1141}
1142
1143static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1144 u32 flags)
1145{
1146 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1147}
1148
1149static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1150 u32 flags)
1151{
1152 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1153}
1154
Herbert Xu7226bc872006-08-21 21:40:49 +10001155static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1156 const u8 *key, unsigned int keylen)
1157{
1158 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1159 key, keylen);
1160}
1161
Herbert Xuf28776a2006-08-13 20:58:18 +10001162static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1163 u8 *dst, const u8 *src)
1164{
1165 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1166 dst, src);
1167}
1168
1169static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1170 u8 *dst, const u8 *src)
1171{
1172 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1173 dst, src);
1174}
1175
Herbert Xu055bcee2006-08-19 22:24:23 +10001176static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Herbert Xu055bcee2006-08-19 22:24:23 +10001178 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Herbert Xu055bcee2006-08-19 22:24:23 +10001181static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Herbert Xu055bcee2006-08-19 22:24:23 +10001183 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1184 CRYPTO_ALG_TYPE_HASH_MASK);
1185 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Herbert Xu055bcee2006-08-19 22:24:23 +10001188static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1189 u32 type, u32 mask)
1190{
1191 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001192 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001193 type |= CRYPTO_ALG_TYPE_HASH;
1194 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1195
1196 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1197}
1198
1199static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1200{
1201 return &tfm->base;
1202}
1203
1204static inline void crypto_free_hash(struct crypto_hash *tfm)
1205{
1206 crypto_free_tfm(crypto_hash_tfm(tfm));
1207}
1208
Herbert Xufce32d72006-08-26 17:35:45 +10001209static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1210{
1211 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001212 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001213 type |= CRYPTO_ALG_TYPE_HASH;
1214 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1215
1216 return crypto_has_alg(alg_name, type, mask);
1217}
1218
Herbert Xu055bcee2006-08-19 22:24:23 +10001219static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1220{
1221 return &crypto_hash_tfm(tfm)->crt_hash;
1222}
1223
1224static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1225{
1226 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1227}
1228
1229static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1230{
1231 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1232}
1233
1234static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1235{
1236 return crypto_hash_crt(tfm)->digestsize;
1237}
1238
1239static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1240{
1241 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1242}
1243
1244static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1245{
1246 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1247}
1248
1249static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1250{
1251 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1252}
1253
1254static inline int crypto_hash_init(struct hash_desc *desc)
1255{
1256 return crypto_hash_crt(desc->tfm)->init(desc);
1257}
1258
1259static inline int crypto_hash_update(struct hash_desc *desc,
1260 struct scatterlist *sg,
1261 unsigned int nbytes)
1262{
1263 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1264}
1265
1266static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1267{
1268 return crypto_hash_crt(desc->tfm)->final(desc, out);
1269}
1270
1271static inline int crypto_hash_digest(struct hash_desc *desc,
1272 struct scatterlist *sg,
1273 unsigned int nbytes, u8 *out)
1274{
1275 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1276}
1277
1278static inline int crypto_hash_setkey(struct crypto_hash *hash,
1279 const u8 *key, unsigned int keylen)
1280{
1281 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Herbert Xufce32d72006-08-26 17:35:45 +10001284static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1285{
1286 return (struct crypto_comp *)tfm;
1287}
1288
1289static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1290{
1291 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1292 CRYPTO_ALG_TYPE_MASK);
1293 return __crypto_comp_cast(tfm);
1294}
1295
1296static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1297 u32 type, u32 mask)
1298{
1299 type &= ~CRYPTO_ALG_TYPE_MASK;
1300 type |= CRYPTO_ALG_TYPE_COMPRESS;
1301 mask |= CRYPTO_ALG_TYPE_MASK;
1302
1303 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1304}
1305
1306static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1307{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001308 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001309}
1310
1311static inline void crypto_free_comp(struct crypto_comp *tfm)
1312{
1313 crypto_free_tfm(crypto_comp_tfm(tfm));
1314}
1315
1316static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1317{
1318 type &= ~CRYPTO_ALG_TYPE_MASK;
1319 type |= CRYPTO_ALG_TYPE_COMPRESS;
1320 mask |= CRYPTO_ALG_TYPE_MASK;
1321
1322 return crypto_has_alg(alg_name, type, mask);
1323}
1324
Herbert Xue4d5b792006-08-26 18:12:40 +10001325static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1326{
1327 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1328}
1329
Herbert Xufce32d72006-08-26 17:35:45 +10001330static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1331{
1332 return &crypto_comp_tfm(tfm)->crt_compress;
1333}
1334
1335static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 const u8 *src, unsigned int slen,
1337 u8 *dst, unsigned int *dlen)
1338{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001339 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1340 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Herbert Xufce32d72006-08-26 17:35:45 +10001343static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 const u8 *src, unsigned int slen,
1345 u8 *dst, unsigned int *dlen)
1346{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001347 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1348 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#endif /* _LINUX_CRYPTO_H */
1352