blob: ffaaa418cf59f50a3a57c9ba74f729b150978398 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Herbert Xu6521f302006-08-06 20:28:44 +100020#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Algorithm masks and types.
30 */
Herbert Xu28259822006-08-06 21:23:26 +100031#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080033#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100035#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080036#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080037#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Loc Ho004a4032008-05-14 20:41:47 +080038#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
39#define CRYPTO_ALG_TYPE_HASH 0x00000009
40#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Neil Horman17f0f4a2008-08-14 22:15:52 +100041#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Herbert Xu055bcee2006-08-19 22:24:23 +100042
43#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080044#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080045#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Herbert Xu28259822006-08-06 21:23:26 +100047#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100048#define CRYPTO_ALG_DEAD 0x00000020
49#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100050#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Herbert Xu60104392006-08-26 18:34:10 +100053 * Set this bit if and only if the algorithm requires another algorithm of
54 * the same type to handle corner cases.
55 */
56#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
57
58/*
Herbert Xuecfc4322007-12-05 21:08:36 +110059 * This bit is set for symmetric key ciphers that have already been wrapped
60 * with a generic IV generator to prevent them from being wrapped again.
61 */
62#define CRYPTO_ALG_GENIV 0x00000200
63
64/*
Herbert Xu73d38642008-08-03 21:15:23 +080065 * Set if the algorithm has passed automated run-time testing. Note that
66 * if there is no run-time testing for a given algorithm it is considered
67 * to have passed.
68 */
69
70#define CRYPTO_ALG_TESTED 0x00000400
71
72/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * Transform masks and values (for crt_flags).
74 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define CRYPTO_TFM_REQ_MASK 0x000fff00
76#define CRYPTO_TFM_RES_MASK 0xfff00000
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070079#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110080#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
82#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
83#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
84#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
85#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
86
87/*
88 * Miscellaneous stuff.
89 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define CRYPTO_MAX_ALG_NAME 64
91
Herbert Xu79911102006-08-21 21:03:52 +100092/*
93 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
94 * declaration) is used to ensure that the crypto_tfm context structure is
95 * aligned correctly for the given architecture so that there are no alignment
96 * faults for C data types. In particular, this is required on platforms such
97 * as arm where pointers are 32-bit aligned but there are data types such as
98 * u64 which require 64-bit alignment.
99 */
100#if defined(ARCH_KMALLOC_MINALIGN)
101#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
102#elif defined(ARCH_SLAB_MINALIGN)
103#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
Herbert Xu6eb72282008-01-08 17:16:44 +1100104#else
105#define CRYPTO_MINALIGN __alignof__(unsigned long long)
Herbert Xu79911102006-08-21 21:03:52 +1000106#endif
107
Herbert Xu79911102006-08-21 21:03:52 +1000108#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100111struct crypto_ablkcipher;
112struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800113struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000114struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000115struct crypto_hash;
Loc Ho004a4032008-05-14 20:41:47 +0800116struct crypto_ahash;
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
Loc Ho004a4032008-05-14 20:41:47 +0800147struct ahash_request {
148 struct crypto_async_request base;
149
Loc Ho004a4032008-05-14 20:41:47 +0800150 unsigned int nbytes;
151 struct scatterlist *src;
152 u8 *result;
153
154 void *__ctx[] CRYPTO_MINALIGN_ATTR;
155};
156
Herbert Xu1ae97822007-08-30 15:36:14 +0800157/**
158 * struct aead_request - AEAD request
159 * @base: Common attributes for async crypto requests
160 * @assoclen: Length in bytes of associated data for authentication
161 * @cryptlen: Length of data to be encrypted or decrypted
162 * @iv: Initialisation vector
163 * @assoc: Associated data
164 * @src: Source data
165 * @dst: Destination data
166 * @__ctx: Start of private context data
167 */
168struct aead_request {
169 struct crypto_async_request base;
170
171 unsigned int assoclen;
172 unsigned int cryptlen;
173
174 u8 *iv;
175
176 struct scatterlist *assoc;
177 struct scatterlist *src;
178 struct scatterlist *dst;
179
180 void *__ctx[] CRYPTO_MINALIGN_ATTR;
181};
182
Herbert Xu5cde0af2006-08-22 00:07:53 +1000183struct blkcipher_desc {
184 struct crypto_blkcipher *tfm;
185 void *info;
186 u32 flags;
187};
188
Herbert Xu40725182005-07-06 13:51:52 -0700189struct cipher_desc {
190 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000191 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700192 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
193 const u8 *src, unsigned int nbytes);
194 void *info;
195};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Herbert Xu055bcee2006-08-19 22:24:23 +1000197struct hash_desc {
198 struct crypto_hash *tfm;
199 u32 flags;
200};
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * Algorithms: modular crypto algorithm implementations, managed
204 * via crypto_register_alg() and crypto_unregister_alg().
205 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000206struct ablkcipher_alg {
207 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
208 unsigned int keylen);
209 int (*encrypt)(struct ablkcipher_request *req);
210 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800211 int (*givencrypt)(struct skcipher_givcrypt_request *req);
212 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000213
Herbert Xu23508e12007-11-27 21:33:24 +0800214 const char *geniv;
215
Herbert Xub5b7f082007-04-16 20:48:54 +1000216 unsigned int min_keysize;
217 unsigned int max_keysize;
218 unsigned int ivsize;
219};
220
Loc Ho004a4032008-05-14 20:41:47 +0800221struct ahash_alg {
222 int (*init)(struct ahash_request *req);
223 int (*update)(struct ahash_request *req);
224 int (*final)(struct ahash_request *req);
225 int (*digest)(struct ahash_request *req);
226 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
227 unsigned int keylen);
228
229 unsigned int digestsize;
230};
231
Herbert Xu1ae97822007-08-30 15:36:14 +0800232struct aead_alg {
233 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
234 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100235 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800236 int (*encrypt)(struct aead_request *req);
237 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800238 int (*givencrypt)(struct aead_givcrypt_request *req);
239 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800240
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800241 const char *geniv;
242
Herbert Xu1ae97822007-08-30 15:36:14 +0800243 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100244 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800245};
246
Herbert Xu5cde0af2006-08-22 00:07:53 +1000247struct blkcipher_alg {
248 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
249 unsigned int keylen);
250 int (*encrypt)(struct blkcipher_desc *desc,
251 struct scatterlist *dst, struct scatterlist *src,
252 unsigned int nbytes);
253 int (*decrypt)(struct blkcipher_desc *desc,
254 struct scatterlist *dst, struct scatterlist *src,
255 unsigned int nbytes);
256
Herbert Xu23508e12007-11-27 21:33:24 +0800257 const char *geniv;
258
Herbert Xu5cde0af2006-08-22 00:07:53 +1000259 unsigned int min_keysize;
260 unsigned int max_keysize;
261 unsigned int ivsize;
262};
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264struct cipher_alg {
265 unsigned int cia_min_keysize;
266 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000267 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000268 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000269 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
270 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
273struct digest_alg {
274 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000275 void (*dia_init)(struct crypto_tfm *tfm);
276 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
277 unsigned int len);
278 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
279 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000280 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
Herbert Xu055bcee2006-08-19 22:24:23 +1000283struct hash_alg {
284 int (*init)(struct hash_desc *desc);
285 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
286 unsigned int nbytes);
287 int (*final)(struct hash_desc *desc, u8 *out);
288 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
289 unsigned int nbytes, u8 *out);
290 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
291 unsigned int keylen);
292
293 unsigned int digestsize;
294};
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000297 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
298 unsigned int slen, u8 *dst, unsigned int *dlen);
299 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
300 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301};
302
Neil Horman17f0f4a2008-08-14 22:15:52 +1000303struct rng_alg {
304 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
305 unsigned int dlen);
306 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
307
308 unsigned int seedsize;
309};
310
311
Herbert Xub5b7f082007-04-16 20:48:54 +1000312#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800313#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000314#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#define cra_cipher cra_u.cipher
316#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000317#define cra_hash cra_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800318#define cra_ahash cra_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000320#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322struct crypto_alg {
323 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000324 struct list_head cra_users;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u32 cra_flags;
327 unsigned int cra_blocksize;
328 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700329 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100330
331 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000332 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100333
Herbert Xud913ea02006-05-21 08:45:26 +1000334 char cra_name[CRYPTO_MAX_ALG_NAME];
335 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Herbert Xue853c3c2006-08-22 00:06:54 +1000337 const struct crypto_type *cra_type;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000340 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800341 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000342 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct cipher_alg cipher;
344 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000345 struct hash_alg hash;
Loc Ho004a4032008-05-14 20:41:47 +0800346 struct ahash_alg ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000348 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000350
351 int (*cra_init)(struct crypto_tfm *tfm);
352 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000353 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 struct module *cra_module;
356};
357
358/*
359 * Algorithm registration interface.
360 */
361int crypto_register_alg(struct crypto_alg *alg);
362int crypto_unregister_alg(struct crypto_alg *alg);
363
364/*
365 * Algorithm query interface.
366 */
Herbert Xufce32d72006-08-26 17:35:45 +1000367int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369/*
370 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000371 * and core processing logic. Managed via crypto_alloc_*() and
372 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Herbert Xu32e39832007-03-24 14:35:34 +1100375struct ablkcipher_tfm {
376 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
377 unsigned int keylen);
378 int (*encrypt)(struct ablkcipher_request *req);
379 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800380 int (*givencrypt)(struct skcipher_givcrypt_request *req);
381 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
382
Herbert Xuecfc4322007-12-05 21:08:36 +1100383 struct crypto_ablkcipher *base;
384
Herbert Xu32e39832007-03-24 14:35:34 +1100385 unsigned int ivsize;
386 unsigned int reqsize;
387};
388
Herbert Xu1ae97822007-08-30 15:36:14 +0800389struct aead_tfm {
390 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
391 unsigned int keylen);
392 int (*encrypt)(struct aead_request *req);
393 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800394 int (*givencrypt)(struct aead_givcrypt_request *req);
395 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800396
397 struct crypto_aead *base;
398
Herbert Xu1ae97822007-08-30 15:36:14 +0800399 unsigned int ivsize;
400 unsigned int authsize;
401 unsigned int reqsize;
402};
403
Herbert Xu5cde0af2006-08-22 00:07:53 +1000404struct blkcipher_tfm {
405 void *iv;
406 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
407 unsigned int keylen);
408 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
409 struct scatterlist *src, unsigned int nbytes);
410 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
411 struct scatterlist *src, unsigned int nbytes);
412};
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int (*cit_setkey)(struct crypto_tfm *tfm,
416 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000417 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
418 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419};
420
Herbert Xu055bcee2006-08-19 22:24:23 +1000421struct hash_tfm {
422 int (*init)(struct hash_desc *desc);
423 int (*update)(struct hash_desc *desc,
424 struct scatterlist *sg, unsigned int nsg);
425 int (*final)(struct hash_desc *desc, u8 *out);
426 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
427 unsigned int nsg, u8 *out);
428 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
429 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000430 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
Loc Ho004a4032008-05-14 20:41:47 +0800433struct ahash_tfm {
434 int (*init)(struct ahash_request *req);
435 int (*update)(struct ahash_request *req);
436 int (*final)(struct ahash_request *req);
437 int (*digest)(struct ahash_request *req);
438 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
439 unsigned int keylen);
440
441 unsigned int digestsize;
Loc Ho004a4032008-05-14 20:41:47 +0800442 unsigned int reqsize;
443};
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445struct compress_tfm {
446 int (*cot_compress)(struct crypto_tfm *tfm,
447 const u8 *src, unsigned int slen,
448 u8 *dst, unsigned int *dlen);
449 int (*cot_decompress)(struct crypto_tfm *tfm,
450 const u8 *src, unsigned int slen,
451 u8 *dst, unsigned int *dlen);
452};
453
Neil Horman17f0f4a2008-08-14 22:15:52 +1000454struct rng_tfm {
455 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
456 unsigned int dlen);
457 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
458};
459
Herbert Xu32e39832007-03-24 14:35:34 +1100460#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800461#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000462#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000464#define crt_hash crt_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800465#define crt_ahash crt_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000467#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469struct crypto_tfm {
470
471 u32 crt_flags;
472
473 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100474 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800475 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000476 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000478 struct hash_tfm hash;
Loc Ho004a4032008-05-14 20:41:47 +0800479 struct ahash_tfm ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000481 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700483
484 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100487
Herbert Xu79911102006-08-21 21:03:52 +1000488 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489};
490
Herbert Xu32e39832007-03-24 14:35:34 +1100491struct crypto_ablkcipher {
492 struct crypto_tfm base;
493};
494
Herbert Xu1ae97822007-08-30 15:36:14 +0800495struct crypto_aead {
496 struct crypto_tfm base;
497};
498
Herbert Xu5cde0af2006-08-22 00:07:53 +1000499struct crypto_blkcipher {
500 struct crypto_tfm base;
501};
502
Herbert Xu78a1fe42006-12-24 10:02:00 +1100503struct crypto_cipher {
504 struct crypto_tfm base;
505};
506
507struct crypto_comp {
508 struct crypto_tfm base;
509};
510
Herbert Xu055bcee2006-08-19 22:24:23 +1000511struct crypto_hash {
512 struct crypto_tfm base;
513};
514
Neil Horman17f0f4a2008-08-14 22:15:52 +1000515struct crypto_rng {
516 struct crypto_tfm base;
517};
518
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000519enum {
520 CRYPTOA_UNSPEC,
521 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100522 CRYPTOA_TYPE,
Herbert Xu39e1ee02007-08-29 19:27:26 +0800523 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100524 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000525};
526
Herbert Xuebc610e2007-01-01 18:37:02 +1100527#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
528
Herbert Xu39e1ee02007-08-29 19:27:26 +0800529/* Maximum number of (rtattr) parameters for each template. */
530#define CRYPTO_MAX_ATTRS 32
531
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000532struct crypto_attr_alg {
533 char name[CRYPTO_MAX_ALG_NAME];
534};
535
Herbert Xuebc610e2007-01-01 18:37:02 +1100536struct crypto_attr_type {
537 u32 type;
538 u32 mask;
539};
540
Herbert Xu39e1ee02007-08-29 19:27:26 +0800541struct crypto_attr_u32 {
542 u32 num;
543};
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/*
546 * Transform user interface.
547 */
548
Herbert Xu7b0bac62008-09-21 06:52:53 +0900549struct crypto_tfm *crypto_alloc_tfm(const char *alg_name,
550 const struct crypto_type *frontend,
551 u32 type, u32 mask);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000552struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553void crypto_free_tfm(struct crypto_tfm *tfm);
554
Herbert Xuda7f0332008-07-31 17:08:25 +0800555int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
558 * Transform helpers which query the underlying algorithm.
559 */
560static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
561{
562 return tfm->__crt_alg->cra_name;
563}
564
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000565static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
566{
567 return tfm->__crt_alg->cra_driver_name;
568}
569
570static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
571{
572 return tfm->__crt_alg->cra_priority;
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
576{
577 return module_name(tfm->__crt_alg->cra_module);
578}
579
580static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
581{
582 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
586{
587 return tfm->__crt_alg->cra_blocksize;
588}
589
Herbert Xufbdae9f2005-07-06 13:53:29 -0700590static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
591{
592 return tfm->__crt_alg->cra_alignmask;
593}
594
Herbert Xuf28776a2006-08-13 20:58:18 +1000595static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
596{
597 return tfm->crt_flags;
598}
599
600static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
601{
602 tfm->crt_flags |= flags;
603}
604
605static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
606{
607 tfm->crt_flags &= ~flags;
608}
609
Herbert Xu40725182005-07-06 13:51:52 -0700610static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
611{
Herbert Xuf10b7892006-01-25 22:34:01 +1100612 return tfm->__crt_ctx;
613}
614
615static inline unsigned int crypto_tfm_ctx_alignment(void)
616{
617 struct crypto_tfm *tfm;
618 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * API wrappers.
623 */
Herbert Xu32e39832007-03-24 14:35:34 +1100624static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
625 struct crypto_tfm *tfm)
626{
627 return (struct crypto_ablkcipher *)tfm;
628}
629
Herbert Xu378f4f52007-12-17 20:07:31 +0800630static inline u32 crypto_skcipher_type(u32 type)
631{
Herbert Xuecfc4322007-12-05 21:08:36 +1100632 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800633 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
634 return type;
635}
636
637static inline u32 crypto_skcipher_mask(u32 mask)
638{
Herbert Xuecfc4322007-12-05 21:08:36 +1100639 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800640 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
641 return mask;
642}
643
Herbert Xub9c55aa2007-12-04 12:46:48 +1100644struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
645 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100646
647static inline struct crypto_tfm *crypto_ablkcipher_tfm(
648 struct crypto_ablkcipher *tfm)
649{
650 return &tfm->base;
651}
652
653static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
654{
655 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
656}
657
658static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
659 u32 mask)
660{
Herbert Xu378f4f52007-12-17 20:07:31 +0800661 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
662 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100663}
664
665static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
666 struct crypto_ablkcipher *tfm)
667{
668 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
669}
670
671static inline unsigned int crypto_ablkcipher_ivsize(
672 struct crypto_ablkcipher *tfm)
673{
674 return crypto_ablkcipher_crt(tfm)->ivsize;
675}
676
677static inline unsigned int crypto_ablkcipher_blocksize(
678 struct crypto_ablkcipher *tfm)
679{
680 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
681}
682
683static inline unsigned int crypto_ablkcipher_alignmask(
684 struct crypto_ablkcipher *tfm)
685{
686 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
687}
688
689static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
690{
691 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
692}
693
694static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
695 u32 flags)
696{
697 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
698}
699
700static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
701 u32 flags)
702{
703 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
704}
705
706static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
707 const u8 *key, unsigned int keylen)
708{
Herbert Xuecfc4322007-12-05 21:08:36 +1100709 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
710
711 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100712}
713
714static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
715 struct ablkcipher_request *req)
716{
717 return __crypto_ablkcipher_cast(req->base.tfm);
718}
719
720static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
721{
722 struct ablkcipher_tfm *crt =
723 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
724 return crt->encrypt(req);
725}
726
727static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
728{
729 struct ablkcipher_tfm *crt =
730 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
731 return crt->decrypt(req);
732}
733
Herbert Xub16c3a22007-08-29 19:02:04 +0800734static inline unsigned int crypto_ablkcipher_reqsize(
735 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100736{
737 return crypto_ablkcipher_crt(tfm)->reqsize;
738}
739
Herbert Xue196d622007-04-14 16:09:14 +1000740static inline void ablkcipher_request_set_tfm(
741 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
742{
Herbert Xuecfc4322007-12-05 21:08:36 +1100743 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000744}
745
Herbert Xub5b7f082007-04-16 20:48:54 +1000746static inline struct ablkcipher_request *ablkcipher_request_cast(
747 struct crypto_async_request *req)
748{
749 return container_of(req, struct ablkcipher_request, base);
750}
751
Herbert Xu32e39832007-03-24 14:35:34 +1100752static inline struct ablkcipher_request *ablkcipher_request_alloc(
753 struct crypto_ablkcipher *tfm, gfp_t gfp)
754{
755 struct ablkcipher_request *req;
756
757 req = kmalloc(sizeof(struct ablkcipher_request) +
758 crypto_ablkcipher_reqsize(tfm), gfp);
759
760 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000761 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100762
763 return req;
764}
765
766static inline void ablkcipher_request_free(struct ablkcipher_request *req)
767{
768 kfree(req);
769}
770
771static inline void ablkcipher_request_set_callback(
772 struct ablkcipher_request *req,
773 u32 flags, crypto_completion_t complete, void *data)
774{
775 req->base.complete = complete;
776 req->base.data = data;
777 req->base.flags = flags;
778}
779
780static inline void ablkcipher_request_set_crypt(
781 struct ablkcipher_request *req,
782 struct scatterlist *src, struct scatterlist *dst,
783 unsigned int nbytes, void *iv)
784{
785 req->src = src;
786 req->dst = dst;
787 req->nbytes = nbytes;
788 req->info = iv;
789}
790
Herbert Xu1ae97822007-08-30 15:36:14 +0800791static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
792{
793 return (struct crypto_aead *)tfm;
794}
795
Herbert Xud29ce982007-12-12 19:24:27 +0800796struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800797
798static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
799{
800 return &tfm->base;
801}
802
803static inline void crypto_free_aead(struct crypto_aead *tfm)
804{
805 crypto_free_tfm(crypto_aead_tfm(tfm));
806}
807
808static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
809{
810 return &crypto_aead_tfm(tfm)->crt_aead;
811}
812
813static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
814{
815 return crypto_aead_crt(tfm)->ivsize;
816}
817
818static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
819{
820 return crypto_aead_crt(tfm)->authsize;
821}
822
823static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
824{
825 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
826}
827
828static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
829{
830 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
831}
832
833static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
834{
835 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
836}
837
838static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
839{
840 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
841}
842
843static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
844{
845 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
846}
847
848static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
849 unsigned int keylen)
850{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800851 struct aead_tfm *crt = crypto_aead_crt(tfm);
852
853 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800854}
855
Herbert Xu7ba683a2007-12-02 18:49:21 +1100856int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
857
Herbert Xu1ae97822007-08-30 15:36:14 +0800858static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
859{
860 return __crypto_aead_cast(req->base.tfm);
861}
862
863static inline int crypto_aead_encrypt(struct aead_request *req)
864{
865 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
866}
867
868static inline int crypto_aead_decrypt(struct aead_request *req)
869{
870 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
871}
872
Herbert Xub16c3a22007-08-29 19:02:04 +0800873static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800874{
875 return crypto_aead_crt(tfm)->reqsize;
876}
877
878static inline void aead_request_set_tfm(struct aead_request *req,
879 struct crypto_aead *tfm)
880{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800881 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800882}
883
884static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
885 gfp_t gfp)
886{
887 struct aead_request *req;
888
889 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
890
891 if (likely(req))
892 aead_request_set_tfm(req, tfm);
893
894 return req;
895}
896
897static inline void aead_request_free(struct aead_request *req)
898{
899 kfree(req);
900}
901
902static inline void aead_request_set_callback(struct aead_request *req,
903 u32 flags,
904 crypto_completion_t complete,
905 void *data)
906{
907 req->base.complete = complete;
908 req->base.data = data;
909 req->base.flags = flags;
910}
911
912static inline void aead_request_set_crypt(struct aead_request *req,
913 struct scatterlist *src,
914 struct scatterlist *dst,
915 unsigned int cryptlen, u8 *iv)
916{
917 req->src = src;
918 req->dst = dst;
919 req->cryptlen = cryptlen;
920 req->iv = iv;
921}
922
923static inline void aead_request_set_assoc(struct aead_request *req,
924 struct scatterlist *assoc,
925 unsigned int assoclen)
926{
927 req->assoc = assoc;
928 req->assoclen = assoclen;
929}
930
Herbert Xu5cde0af2006-08-22 00:07:53 +1000931static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
932 struct crypto_tfm *tfm)
933{
934 return (struct crypto_blkcipher *)tfm;
935}
936
937static inline struct crypto_blkcipher *crypto_blkcipher_cast(
938 struct crypto_tfm *tfm)
939{
940 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
941 return __crypto_blkcipher_cast(tfm);
942}
943
944static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
945 const char *alg_name, u32 type, u32 mask)
946{
Herbert Xu332f88402007-11-15 22:36:07 +0800947 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000948 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800949 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000950
951 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
952}
953
954static inline struct crypto_tfm *crypto_blkcipher_tfm(
955 struct crypto_blkcipher *tfm)
956{
957 return &tfm->base;
958}
959
960static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
961{
962 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
963}
964
Herbert Xufce32d72006-08-26 17:35:45 +1000965static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
966{
Herbert Xu332f88402007-11-15 22:36:07 +0800967 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000968 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800969 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000970
971 return crypto_has_alg(alg_name, type, mask);
972}
973
Herbert Xu5cde0af2006-08-22 00:07:53 +1000974static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
975{
976 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
977}
978
979static inline struct blkcipher_tfm *crypto_blkcipher_crt(
980 struct crypto_blkcipher *tfm)
981{
982 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
983}
984
985static inline struct blkcipher_alg *crypto_blkcipher_alg(
986 struct crypto_blkcipher *tfm)
987{
988 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
989}
990
991static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
992{
993 return crypto_blkcipher_alg(tfm)->ivsize;
994}
995
996static inline unsigned int crypto_blkcipher_blocksize(
997 struct crypto_blkcipher *tfm)
998{
999 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1000}
1001
1002static inline unsigned int crypto_blkcipher_alignmask(
1003 struct crypto_blkcipher *tfm)
1004{
1005 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1006}
1007
1008static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1009{
1010 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1011}
1012
1013static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1014 u32 flags)
1015{
1016 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1017}
1018
1019static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1020 u32 flags)
1021{
1022 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1023}
1024
1025static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1026 const u8 *key, unsigned int keylen)
1027{
1028 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1029 key, keylen);
1030}
1031
1032static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1033 struct scatterlist *dst,
1034 struct scatterlist *src,
1035 unsigned int nbytes)
1036{
1037 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1038 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1039}
1040
1041static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1042 struct scatterlist *dst,
1043 struct scatterlist *src,
1044 unsigned int nbytes)
1045{
1046 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1047}
1048
1049static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1050 struct scatterlist *dst,
1051 struct scatterlist *src,
1052 unsigned int nbytes)
1053{
1054 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1055 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1056}
1057
1058static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1059 struct scatterlist *dst,
1060 struct scatterlist *src,
1061 unsigned int nbytes)
1062{
1063 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1064}
1065
1066static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1067 const u8 *src, unsigned int len)
1068{
1069 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1070}
1071
1072static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1073 u8 *dst, unsigned int len)
1074{
1075 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1076}
1077
Herbert Xuf28776a2006-08-13 20:58:18 +10001078static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1079{
1080 return (struct crypto_cipher *)tfm;
1081}
1082
1083static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1084{
1085 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1086 return __crypto_cipher_cast(tfm);
1087}
1088
1089static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1090 u32 type, u32 mask)
1091{
1092 type &= ~CRYPTO_ALG_TYPE_MASK;
1093 type |= CRYPTO_ALG_TYPE_CIPHER;
1094 mask |= CRYPTO_ALG_TYPE_MASK;
1095
1096 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1097}
1098
1099static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1100{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001101 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001102}
1103
1104static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1105{
1106 crypto_free_tfm(crypto_cipher_tfm(tfm));
1107}
1108
Herbert Xufce32d72006-08-26 17:35:45 +10001109static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1110{
1111 type &= ~CRYPTO_ALG_TYPE_MASK;
1112 type |= CRYPTO_ALG_TYPE_CIPHER;
1113 mask |= CRYPTO_ALG_TYPE_MASK;
1114
1115 return crypto_has_alg(alg_name, type, mask);
1116}
1117
Herbert Xuf28776a2006-08-13 20:58:18 +10001118static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1119{
1120 return &crypto_cipher_tfm(tfm)->crt_cipher;
1121}
1122
1123static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1124{
1125 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1126}
1127
1128static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1129{
1130 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1131}
1132
1133static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1134{
1135 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1136}
1137
1138static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1139 u32 flags)
1140{
1141 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1142}
1143
1144static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1145 u32 flags)
1146{
1147 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1148}
1149
Herbert Xu7226bc82006-08-21 21:40:49 +10001150static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1151 const u8 *key, unsigned int keylen)
1152{
1153 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1154 key, keylen);
1155}
1156
Herbert Xuf28776a2006-08-13 20:58:18 +10001157static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1158 u8 *dst, const u8 *src)
1159{
1160 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1161 dst, src);
1162}
1163
1164static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1165 u8 *dst, const u8 *src)
1166{
1167 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1168 dst, src);
1169}
1170
Herbert Xu055bcee2006-08-19 22:24:23 +10001171static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Herbert Xu055bcee2006-08-19 22:24:23 +10001173 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
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 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1179 CRYPTO_ALG_TYPE_HASH_MASK);
1180 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Herbert Xu055bcee2006-08-19 22:24:23 +10001183static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1184 u32 type, u32 mask)
1185{
1186 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001187 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001188 type |= CRYPTO_ALG_TYPE_HASH;
1189 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1190
1191 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1192}
1193
1194static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1195{
1196 return &tfm->base;
1197}
1198
1199static inline void crypto_free_hash(struct crypto_hash *tfm)
1200{
1201 crypto_free_tfm(crypto_hash_tfm(tfm));
1202}
1203
Herbert Xufce32d72006-08-26 17:35:45 +10001204static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1205{
1206 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001207 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001208 type |= CRYPTO_ALG_TYPE_HASH;
1209 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1210
1211 return crypto_has_alg(alg_name, type, mask);
1212}
1213
Herbert Xu055bcee2006-08-19 22:24:23 +10001214static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1215{
1216 return &crypto_hash_tfm(tfm)->crt_hash;
1217}
1218
1219static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1220{
1221 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1222}
1223
1224static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1225{
1226 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1227}
1228
1229static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1230{
1231 return crypto_hash_crt(tfm)->digestsize;
1232}
1233
1234static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1235{
1236 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1237}
1238
1239static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1240{
1241 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1242}
1243
1244static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1245{
1246 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1247}
1248
1249static inline int crypto_hash_init(struct hash_desc *desc)
1250{
1251 return crypto_hash_crt(desc->tfm)->init(desc);
1252}
1253
1254static inline int crypto_hash_update(struct hash_desc *desc,
1255 struct scatterlist *sg,
1256 unsigned int nbytes)
1257{
1258 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1259}
1260
1261static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1262{
1263 return crypto_hash_crt(desc->tfm)->final(desc, out);
1264}
1265
1266static inline int crypto_hash_digest(struct hash_desc *desc,
1267 struct scatterlist *sg,
1268 unsigned int nbytes, u8 *out)
1269{
1270 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1271}
1272
1273static inline int crypto_hash_setkey(struct crypto_hash *hash,
1274 const u8 *key, unsigned int keylen)
1275{
1276 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Herbert Xufce32d72006-08-26 17:35:45 +10001279static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1280{
1281 return (struct crypto_comp *)tfm;
1282}
1283
1284static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1285{
1286 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1287 CRYPTO_ALG_TYPE_MASK);
1288 return __crypto_comp_cast(tfm);
1289}
1290
1291static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1292 u32 type, u32 mask)
1293{
1294 type &= ~CRYPTO_ALG_TYPE_MASK;
1295 type |= CRYPTO_ALG_TYPE_COMPRESS;
1296 mask |= CRYPTO_ALG_TYPE_MASK;
1297
1298 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1299}
1300
1301static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1302{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001303 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001304}
1305
1306static inline void crypto_free_comp(struct crypto_comp *tfm)
1307{
1308 crypto_free_tfm(crypto_comp_tfm(tfm));
1309}
1310
1311static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1312{
1313 type &= ~CRYPTO_ALG_TYPE_MASK;
1314 type |= CRYPTO_ALG_TYPE_COMPRESS;
1315 mask |= CRYPTO_ALG_TYPE_MASK;
1316
1317 return crypto_has_alg(alg_name, type, mask);
1318}
1319
Herbert Xue4d5b792006-08-26 18:12:40 +10001320static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1321{
1322 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1323}
1324
Herbert Xufce32d72006-08-26 17:35:45 +10001325static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1326{
1327 return &crypto_comp_tfm(tfm)->crt_compress;
1328}
1329
1330static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 const u8 *src, unsigned int slen,
1332 u8 *dst, unsigned int *dlen)
1333{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001334 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1335 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Herbert Xufce32d72006-08-26 17:35:45 +10001338static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 const u8 *src, unsigned int slen,
1340 u8 *dst, unsigned int *dlen)
1341{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001342 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1343 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346#endif /* _LINUX_CRYPTO_H */
1347