blob: 48aa5959abbb5b4b237330ebf920308e33c6efed [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
33#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
Herbert Xu055bcee2006-08-19 22:24:23 +100034#define CRYPTO_ALG_TYPE_HASH 0x00000003
35#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f8842007-11-15 22:36:07 +080036#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
37#define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
38#define CRYPTO_ALG_TYPE_AEAD 0x00000009
Herbert Xu055bcee2006-08-19 22:24:23 +100039
40#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Herbert Xu332f8842007-11-15 22:36:07 +080041#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Herbert Xu28259822006-08-06 21:23:26 +100043#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100044#define CRYPTO_ALG_DEAD 0x00000020
45#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100046#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
Herbert Xu60104392006-08-26 18:34:10 +100049 * Set this bit if and only if the algorithm requires another algorithm of
50 * the same type to handle corner cases.
51 */
52#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
53
54/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * Transform masks and values (for crt_flags).
56 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define CRYPTO_TFM_REQ_MASK 0x000fff00
58#define CRYPTO_TFM_RES_MASK 0xfff00000
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070061#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110062#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
64#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
65#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
66#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
67#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
68
69/*
70 * Miscellaneous stuff.
71 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define CRYPTO_MAX_ALG_NAME 64
73
Herbert Xu79911102006-08-21 21:03:52 +100074/*
75 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
76 * declaration) is used to ensure that the crypto_tfm context structure is
77 * aligned correctly for the given architecture so that there are no alignment
78 * faults for C data types. In particular, this is required on platforms such
79 * as arm where pointers are 32-bit aligned but there are data types such as
80 * u64 which require 64-bit alignment.
81 */
82#if defined(ARCH_KMALLOC_MINALIGN)
83#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
84#elif defined(ARCH_SLAB_MINALIGN)
85#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
86#endif
87
88#ifdef CRYPTO_MINALIGN
89#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
90#else
91#define CRYPTO_MINALIGN_ATTR
92#endif
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +110095struct crypto_ablkcipher;
96struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +080097struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +100098struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +100099struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -0700100struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000101struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700102
Herbert Xu32e39832007-03-24 14:35:34 +1100103typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
104
105struct crypto_async_request {
106 struct list_head list;
107 crypto_completion_t complete;
108 void *data;
109 struct crypto_tfm *tfm;
110
111 u32 flags;
112};
113
114struct ablkcipher_request {
115 struct crypto_async_request base;
116
117 unsigned int nbytes;
118
119 void *info;
120
121 struct scatterlist *src;
122 struct scatterlist *dst;
123
124 void *__ctx[] CRYPTO_MINALIGN_ATTR;
125};
126
Herbert Xu1ae97822007-08-30 15:36:14 +0800127/**
128 * struct aead_request - AEAD request
129 * @base: Common attributes for async crypto requests
130 * @assoclen: Length in bytes of associated data for authentication
131 * @cryptlen: Length of data to be encrypted or decrypted
132 * @iv: Initialisation vector
133 * @assoc: Associated data
134 * @src: Source data
135 * @dst: Destination data
136 * @__ctx: Start of private context data
137 */
138struct aead_request {
139 struct crypto_async_request base;
140
141 unsigned int assoclen;
142 unsigned int cryptlen;
143
144 u8 *iv;
145
146 struct scatterlist *assoc;
147 struct scatterlist *src;
148 struct scatterlist *dst;
149
150 void *__ctx[] CRYPTO_MINALIGN_ATTR;
151};
152
Herbert Xu5cde0af2006-08-22 00:07:53 +1000153struct blkcipher_desc {
154 struct crypto_blkcipher *tfm;
155 void *info;
156 u32 flags;
157};
158
Herbert Xu40725182005-07-06 13:51:52 -0700159struct cipher_desc {
160 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000161 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700162 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
163 const u8 *src, unsigned int nbytes);
164 void *info;
165};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Herbert Xu055bcee2006-08-19 22:24:23 +1000167struct hash_desc {
168 struct crypto_hash *tfm;
169 u32 flags;
170};
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * Algorithms: modular crypto algorithm implementations, managed
174 * via crypto_register_alg() and crypto_unregister_alg().
175 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000176struct ablkcipher_alg {
177 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
178 unsigned int keylen);
179 int (*encrypt)(struct ablkcipher_request *req);
180 int (*decrypt)(struct ablkcipher_request *req);
181
Herbert Xub5b7f082007-04-16 20:48:54 +1000182 unsigned int min_keysize;
183 unsigned int max_keysize;
184 unsigned int ivsize;
185};
186
Herbert Xu1ae97822007-08-30 15:36:14 +0800187struct aead_alg {
188 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
189 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100190 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800191 int (*encrypt)(struct aead_request *req);
192 int (*decrypt)(struct aead_request *req);
193
194 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100195 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800196};
197
Herbert Xu5cde0af2006-08-22 00:07:53 +1000198struct blkcipher_alg {
199 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
200 unsigned int keylen);
201 int (*encrypt)(struct blkcipher_desc *desc,
202 struct scatterlist *dst, struct scatterlist *src,
203 unsigned int nbytes);
204 int (*decrypt)(struct blkcipher_desc *desc,
205 struct scatterlist *dst, struct scatterlist *src,
206 unsigned int nbytes);
207
208 unsigned int min_keysize;
209 unsigned int max_keysize;
210 unsigned int ivsize;
211};
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213struct cipher_alg {
214 unsigned int cia_min_keysize;
215 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000216 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000217 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000218 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
219 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
222struct digest_alg {
223 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000224 void (*dia_init)(struct crypto_tfm *tfm);
225 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
226 unsigned int len);
227 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
228 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000229 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
Herbert Xu055bcee2006-08-19 22:24:23 +1000232struct hash_alg {
233 int (*init)(struct hash_desc *desc);
234 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
235 unsigned int nbytes);
236 int (*final)(struct hash_desc *desc, u8 *out);
237 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
238 unsigned int nbytes, u8 *out);
239 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
240 unsigned int keylen);
241
242 unsigned int digestsize;
243};
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000246 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
247 unsigned int slen, u8 *dst, unsigned int *dlen);
248 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
249 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Herbert Xub5b7f082007-04-16 20:48:54 +1000252#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800253#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000254#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#define cra_cipher cra_u.cipher
256#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000257#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#define cra_compress cra_u.compress
259
260struct crypto_alg {
261 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000262 struct list_head cra_users;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 u32 cra_flags;
265 unsigned int cra_blocksize;
266 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700267 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100268
269 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000270 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100271
Herbert Xud913ea02006-05-21 08:45:26 +1000272 char cra_name[CRYPTO_MAX_ALG_NAME];
273 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Herbert Xue853c3c2006-08-22 00:06:54 +1000275 const struct crypto_type *cra_type;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000278 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800279 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000280 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct cipher_alg cipher;
282 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000283 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct compress_alg compress;
285 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000286
287 int (*cra_init)(struct crypto_tfm *tfm);
288 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000289 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 struct module *cra_module;
292};
293
294/*
295 * Algorithm registration interface.
296 */
297int crypto_register_alg(struct crypto_alg *alg);
298int crypto_unregister_alg(struct crypto_alg *alg);
299
300/*
301 * Algorithm query interface.
302 */
303#ifdef CONFIG_CRYPTO
Herbert Xufce32d72006-08-26 17:35:45 +1000304int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#else
Herbert Xufce32d72006-08-26 17:35:45 +1000306static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
307{
308 return 0;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#endif
311
312/*
313 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000314 * and core processing logic. Managed via crypto_alloc_*() and
315 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Herbert Xu32e39832007-03-24 14:35:34 +1100318struct ablkcipher_tfm {
319 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
320 unsigned int keylen);
321 int (*encrypt)(struct ablkcipher_request *req);
322 int (*decrypt)(struct ablkcipher_request *req);
323 unsigned int ivsize;
324 unsigned int reqsize;
325};
326
Herbert Xu1ae97822007-08-30 15:36:14 +0800327struct aead_tfm {
328 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
329 unsigned int keylen);
330 int (*encrypt)(struct aead_request *req);
331 int (*decrypt)(struct aead_request *req);
332 unsigned int ivsize;
333 unsigned int authsize;
334 unsigned int reqsize;
335};
336
Herbert Xu5cde0af2006-08-22 00:07:53 +1000337struct blkcipher_tfm {
338 void *iv;
339 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
340 unsigned int keylen);
341 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
342 struct scatterlist *src, unsigned int nbytes);
343 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
344 struct scatterlist *src, unsigned int nbytes);
345};
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int (*cit_setkey)(struct crypto_tfm *tfm,
349 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000350 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
351 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
Herbert Xu055bcee2006-08-19 22:24:23 +1000354struct hash_tfm {
355 int (*init)(struct hash_desc *desc);
356 int (*update)(struct hash_desc *desc,
357 struct scatterlist *sg, unsigned int nsg);
358 int (*final)(struct hash_desc *desc, u8 *out);
359 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
360 unsigned int nsg, u8 *out);
361 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
362 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000363 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
366struct compress_tfm {
367 int (*cot_compress)(struct crypto_tfm *tfm,
368 const u8 *src, unsigned int slen,
369 u8 *dst, unsigned int *dlen);
370 int (*cot_decompress)(struct crypto_tfm *tfm,
371 const u8 *src, unsigned int slen,
372 u8 *dst, unsigned int *dlen);
373};
374
Herbert Xu32e39832007-03-24 14:35:34 +1100375#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800376#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000377#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000379#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#define crt_compress crt_u.compress
381
382struct crypto_tfm {
383
384 u32 crt_flags;
385
386 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100387 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800388 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000389 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000391 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct compress_tfm compress;
393 } crt_u;
394
395 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100396
Herbert Xu79911102006-08-21 21:03:52 +1000397 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Herbert Xu32e39832007-03-24 14:35:34 +1100400struct crypto_ablkcipher {
401 struct crypto_tfm base;
402};
403
Herbert Xu1ae97822007-08-30 15:36:14 +0800404struct crypto_aead {
405 struct crypto_tfm base;
406};
407
Herbert Xu5cde0af2006-08-22 00:07:53 +1000408struct crypto_blkcipher {
409 struct crypto_tfm base;
410};
411
Herbert Xu78a1fe42006-12-24 10:02:00 +1100412struct crypto_cipher {
413 struct crypto_tfm base;
414};
415
416struct crypto_comp {
417 struct crypto_tfm base;
418};
419
Herbert Xu055bcee2006-08-19 22:24:23 +1000420struct crypto_hash {
421 struct crypto_tfm base;
422};
423
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000424enum {
425 CRYPTOA_UNSPEC,
426 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100427 CRYPTOA_TYPE,
Herbert Xu39e1ee02007-08-29 19:27:26 +0800428 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100429 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000430};
431
Herbert Xuebc610e2007-01-01 18:37:02 +1100432#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
433
Herbert Xu39e1ee02007-08-29 19:27:26 +0800434/* Maximum number of (rtattr) parameters for each template. */
435#define CRYPTO_MAX_ATTRS 32
436
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000437struct crypto_attr_alg {
438 char name[CRYPTO_MAX_ALG_NAME];
439};
440
Herbert Xuebc610e2007-01-01 18:37:02 +1100441struct crypto_attr_type {
442 u32 type;
443 u32 mask;
444};
445
Herbert Xu39e1ee02007-08-29 19:27:26 +0800446struct crypto_attr_u32 {
447 u32 num;
448};
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
451 * Transform user interface.
452 */
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000455struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456void crypto_free_tfm(struct crypto_tfm *tfm);
457
458/*
459 * Transform helpers which query the underlying algorithm.
460 */
461static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
462{
463 return tfm->__crt_alg->cra_name;
464}
465
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000466static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
467{
468 return tfm->__crt_alg->cra_driver_name;
469}
470
471static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
472{
473 return tfm->__crt_alg->cra_priority;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
477{
478 return module_name(tfm->__crt_alg->cra_module);
479}
480
481static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
482{
483 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
487{
488 return tfm->__crt_alg->cra_blocksize;
489}
490
Herbert Xufbdae9f2005-07-06 13:53:29 -0700491static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
492{
493 return tfm->__crt_alg->cra_alignmask;
494}
495
Herbert Xuf28776a2006-08-13 20:58:18 +1000496static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
497{
498 return tfm->crt_flags;
499}
500
501static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
502{
503 tfm->crt_flags |= flags;
504}
505
506static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
507{
508 tfm->crt_flags &= ~flags;
509}
510
Herbert Xu40725182005-07-06 13:51:52 -0700511static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
512{
Herbert Xuf10b7892006-01-25 22:34:01 +1100513 return tfm->__crt_ctx;
514}
515
516static inline unsigned int crypto_tfm_ctx_alignment(void)
517{
518 struct crypto_tfm *tfm;
519 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * API wrappers.
524 */
Herbert Xu32e39832007-03-24 14:35:34 +1100525static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
526 struct crypto_tfm *tfm)
527{
528 return (struct crypto_ablkcipher *)tfm;
529}
530
531static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
532 const char *alg_name, u32 type, u32 mask)
533{
534 type &= ~CRYPTO_ALG_TYPE_MASK;
535 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800536 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
Herbert Xu32e39832007-03-24 14:35:34 +1100537
538 return __crypto_ablkcipher_cast(
539 crypto_alloc_base(alg_name, type, mask));
540}
541
542static inline struct crypto_tfm *crypto_ablkcipher_tfm(
543 struct crypto_ablkcipher *tfm)
544{
545 return &tfm->base;
546}
547
548static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
549{
550 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
551}
552
553static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
554 u32 mask)
555{
556 type &= ~CRYPTO_ALG_TYPE_MASK;
557 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800558 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
Herbert Xu32e39832007-03-24 14:35:34 +1100559
560 return crypto_has_alg(alg_name, type, mask);
561}
562
563static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
564 struct crypto_ablkcipher *tfm)
565{
566 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
567}
568
569static inline unsigned int crypto_ablkcipher_ivsize(
570 struct crypto_ablkcipher *tfm)
571{
572 return crypto_ablkcipher_crt(tfm)->ivsize;
573}
574
575static inline unsigned int crypto_ablkcipher_blocksize(
576 struct crypto_ablkcipher *tfm)
577{
578 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
579}
580
581static inline unsigned int crypto_ablkcipher_alignmask(
582 struct crypto_ablkcipher *tfm)
583{
584 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
585}
586
587static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
588{
589 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
590}
591
592static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
593 u32 flags)
594{
595 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
596}
597
598static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
599 u32 flags)
600{
601 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
602}
603
604static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
605 const u8 *key, unsigned int keylen)
606{
607 return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
608}
609
610static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
611 struct ablkcipher_request *req)
612{
613 return __crypto_ablkcipher_cast(req->base.tfm);
614}
615
616static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
617{
618 struct ablkcipher_tfm *crt =
619 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
620 return crt->encrypt(req);
621}
622
623static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
624{
625 struct ablkcipher_tfm *crt =
626 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
627 return crt->decrypt(req);
628}
629
Herbert Xub16c3a22007-08-29 19:02:04 +0800630static inline unsigned int crypto_ablkcipher_reqsize(
631 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100632{
633 return crypto_ablkcipher_crt(tfm)->reqsize;
634}
635
Herbert Xue196d622007-04-14 16:09:14 +1000636static inline void ablkcipher_request_set_tfm(
637 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
638{
639 req->base.tfm = crypto_ablkcipher_tfm(tfm);
640}
641
Herbert Xub5b7f082007-04-16 20:48:54 +1000642static inline struct ablkcipher_request *ablkcipher_request_cast(
643 struct crypto_async_request *req)
644{
645 return container_of(req, struct ablkcipher_request, base);
646}
647
Herbert Xu32e39832007-03-24 14:35:34 +1100648static inline struct ablkcipher_request *ablkcipher_request_alloc(
649 struct crypto_ablkcipher *tfm, gfp_t gfp)
650{
651 struct ablkcipher_request *req;
652
653 req = kmalloc(sizeof(struct ablkcipher_request) +
654 crypto_ablkcipher_reqsize(tfm), gfp);
655
656 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000657 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100658
659 return req;
660}
661
662static inline void ablkcipher_request_free(struct ablkcipher_request *req)
663{
664 kfree(req);
665}
666
667static inline void ablkcipher_request_set_callback(
668 struct ablkcipher_request *req,
669 u32 flags, crypto_completion_t complete, void *data)
670{
671 req->base.complete = complete;
672 req->base.data = data;
673 req->base.flags = flags;
674}
675
676static inline void ablkcipher_request_set_crypt(
677 struct ablkcipher_request *req,
678 struct scatterlist *src, struct scatterlist *dst,
679 unsigned int nbytes, void *iv)
680{
681 req->src = src;
682 req->dst = dst;
683 req->nbytes = nbytes;
684 req->info = iv;
685}
686
Herbert Xu1ae97822007-08-30 15:36:14 +0800687static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
688{
689 return (struct crypto_aead *)tfm;
690}
691
692static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
693 u32 type, u32 mask)
694{
695 type &= ~CRYPTO_ALG_TYPE_MASK;
696 type |= CRYPTO_ALG_TYPE_AEAD;
697 mask |= CRYPTO_ALG_TYPE_MASK;
698
699 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
700}
701
702static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
703{
704 return &tfm->base;
705}
706
707static inline void crypto_free_aead(struct crypto_aead *tfm)
708{
709 crypto_free_tfm(crypto_aead_tfm(tfm));
710}
711
712static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
713{
714 return &crypto_aead_tfm(tfm)->crt_aead;
715}
716
717static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
718{
719 return crypto_aead_crt(tfm)->ivsize;
720}
721
722static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
723{
724 return crypto_aead_crt(tfm)->authsize;
725}
726
727static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
728{
729 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
730}
731
732static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
733{
734 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
735}
736
737static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
738{
739 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
740}
741
742static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
743{
744 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
745}
746
747static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
748{
749 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
750}
751
752static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
753 unsigned int keylen)
754{
755 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
756}
757
Herbert Xu7ba683a2007-12-02 18:49:21 +1100758int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
759
Herbert Xu1ae97822007-08-30 15:36:14 +0800760static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
761{
762 return __crypto_aead_cast(req->base.tfm);
763}
764
765static inline int crypto_aead_encrypt(struct aead_request *req)
766{
767 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
768}
769
770static inline int crypto_aead_decrypt(struct aead_request *req)
771{
772 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
773}
774
Herbert Xub16c3a22007-08-29 19:02:04 +0800775static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800776{
777 return crypto_aead_crt(tfm)->reqsize;
778}
779
780static inline void aead_request_set_tfm(struct aead_request *req,
781 struct crypto_aead *tfm)
782{
783 req->base.tfm = crypto_aead_tfm(tfm);
784}
785
786static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
787 gfp_t gfp)
788{
789 struct aead_request *req;
790
791 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
792
793 if (likely(req))
794 aead_request_set_tfm(req, tfm);
795
796 return req;
797}
798
799static inline void aead_request_free(struct aead_request *req)
800{
801 kfree(req);
802}
803
804static inline void aead_request_set_callback(struct aead_request *req,
805 u32 flags,
806 crypto_completion_t complete,
807 void *data)
808{
809 req->base.complete = complete;
810 req->base.data = data;
811 req->base.flags = flags;
812}
813
814static inline void aead_request_set_crypt(struct aead_request *req,
815 struct scatterlist *src,
816 struct scatterlist *dst,
817 unsigned int cryptlen, u8 *iv)
818{
819 req->src = src;
820 req->dst = dst;
821 req->cryptlen = cryptlen;
822 req->iv = iv;
823}
824
825static inline void aead_request_set_assoc(struct aead_request *req,
826 struct scatterlist *assoc,
827 unsigned int assoclen)
828{
829 req->assoc = assoc;
830 req->assoclen = assoclen;
831}
832
Herbert Xu5cde0af2006-08-22 00:07:53 +1000833static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
834 struct crypto_tfm *tfm)
835{
836 return (struct crypto_blkcipher *)tfm;
837}
838
839static inline struct crypto_blkcipher *crypto_blkcipher_cast(
840 struct crypto_tfm *tfm)
841{
842 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
843 return __crypto_blkcipher_cast(tfm);
844}
845
846static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
847 const char *alg_name, u32 type, u32 mask)
848{
Herbert Xu332f8842007-11-15 22:36:07 +0800849 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000850 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800851 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000852
853 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
854}
855
856static inline struct crypto_tfm *crypto_blkcipher_tfm(
857 struct crypto_blkcipher *tfm)
858{
859 return &tfm->base;
860}
861
862static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
863{
864 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
865}
866
Herbert Xufce32d72006-08-26 17:35:45 +1000867static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
868{
Herbert Xu332f8842007-11-15 22:36:07 +0800869 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000870 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800871 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000872
873 return crypto_has_alg(alg_name, type, mask);
874}
875
Herbert Xu5cde0af2006-08-22 00:07:53 +1000876static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
877{
878 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
879}
880
881static inline struct blkcipher_tfm *crypto_blkcipher_crt(
882 struct crypto_blkcipher *tfm)
883{
884 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
885}
886
887static inline struct blkcipher_alg *crypto_blkcipher_alg(
888 struct crypto_blkcipher *tfm)
889{
890 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
891}
892
893static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
894{
895 return crypto_blkcipher_alg(tfm)->ivsize;
896}
897
898static inline unsigned int crypto_blkcipher_blocksize(
899 struct crypto_blkcipher *tfm)
900{
901 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
902}
903
904static inline unsigned int crypto_blkcipher_alignmask(
905 struct crypto_blkcipher *tfm)
906{
907 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
908}
909
910static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
911{
912 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
913}
914
915static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
916 u32 flags)
917{
918 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
919}
920
921static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
922 u32 flags)
923{
924 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
925}
926
927static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
928 const u8 *key, unsigned int keylen)
929{
930 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
931 key, keylen);
932}
933
934static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
935 struct scatterlist *dst,
936 struct scatterlist *src,
937 unsigned int nbytes)
938{
939 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
940 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
941}
942
943static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
944 struct scatterlist *dst,
945 struct scatterlist *src,
946 unsigned int nbytes)
947{
948 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
949}
950
951static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
952 struct scatterlist *dst,
953 struct scatterlist *src,
954 unsigned int nbytes)
955{
956 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
957 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
958}
959
960static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
961 struct scatterlist *dst,
962 struct scatterlist *src,
963 unsigned int nbytes)
964{
965 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
966}
967
968static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
969 const u8 *src, unsigned int len)
970{
971 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
972}
973
974static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
975 u8 *dst, unsigned int len)
976{
977 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
978}
979
Herbert Xuf28776a2006-08-13 20:58:18 +1000980static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
981{
982 return (struct crypto_cipher *)tfm;
983}
984
985static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
986{
987 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
988 return __crypto_cipher_cast(tfm);
989}
990
991static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
992 u32 type, u32 mask)
993{
994 type &= ~CRYPTO_ALG_TYPE_MASK;
995 type |= CRYPTO_ALG_TYPE_CIPHER;
996 mask |= CRYPTO_ALG_TYPE_MASK;
997
998 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
999}
1000
1001static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1002{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001003 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001004}
1005
1006static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1007{
1008 crypto_free_tfm(crypto_cipher_tfm(tfm));
1009}
1010
Herbert Xufce32d72006-08-26 17:35:45 +10001011static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1012{
1013 type &= ~CRYPTO_ALG_TYPE_MASK;
1014 type |= CRYPTO_ALG_TYPE_CIPHER;
1015 mask |= CRYPTO_ALG_TYPE_MASK;
1016
1017 return crypto_has_alg(alg_name, type, mask);
1018}
1019
Herbert Xuf28776a2006-08-13 20:58:18 +10001020static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1021{
1022 return &crypto_cipher_tfm(tfm)->crt_cipher;
1023}
1024
1025static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1026{
1027 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1028}
1029
1030static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1031{
1032 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1033}
1034
1035static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1036{
1037 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1038}
1039
1040static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1041 u32 flags)
1042{
1043 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1044}
1045
1046static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1047 u32 flags)
1048{
1049 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1050}
1051
Herbert Xu7226bc82006-08-21 21:40:49 +10001052static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1053 const u8 *key, unsigned int keylen)
1054{
1055 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1056 key, keylen);
1057}
1058
Herbert Xuf28776a2006-08-13 20:58:18 +10001059static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1060 u8 *dst, const u8 *src)
1061{
1062 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1063 dst, src);
1064}
1065
1066static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1067 u8 *dst, const u8 *src)
1068{
1069 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1070 dst, src);
1071}
1072
Herbert Xu055bcee2006-08-19 22:24:23 +10001073static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Herbert Xu055bcee2006-08-19 22:24:23 +10001075 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Herbert Xu055bcee2006-08-19 22:24:23 +10001078static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Herbert Xu055bcee2006-08-19 22:24:23 +10001080 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1081 CRYPTO_ALG_TYPE_HASH_MASK);
1082 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Herbert Xu055bcee2006-08-19 22:24:23 +10001085static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1086 u32 type, u32 mask)
1087{
1088 type &= ~CRYPTO_ALG_TYPE_MASK;
1089 type |= CRYPTO_ALG_TYPE_HASH;
1090 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1091
1092 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1093}
1094
1095static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1096{
1097 return &tfm->base;
1098}
1099
1100static inline void crypto_free_hash(struct crypto_hash *tfm)
1101{
1102 crypto_free_tfm(crypto_hash_tfm(tfm));
1103}
1104
Herbert Xufce32d72006-08-26 17:35:45 +10001105static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1106{
1107 type &= ~CRYPTO_ALG_TYPE_MASK;
1108 type |= CRYPTO_ALG_TYPE_HASH;
1109 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1110
1111 return crypto_has_alg(alg_name, type, mask);
1112}
1113
Herbert Xu055bcee2006-08-19 22:24:23 +10001114static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1115{
1116 return &crypto_hash_tfm(tfm)->crt_hash;
1117}
1118
1119static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1120{
1121 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1122}
1123
1124static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1125{
1126 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1127}
1128
1129static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1130{
1131 return crypto_hash_crt(tfm)->digestsize;
1132}
1133
1134static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1135{
1136 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1137}
1138
1139static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1140{
1141 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1142}
1143
1144static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1145{
1146 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1147}
1148
1149static inline int crypto_hash_init(struct hash_desc *desc)
1150{
1151 return crypto_hash_crt(desc->tfm)->init(desc);
1152}
1153
1154static inline int crypto_hash_update(struct hash_desc *desc,
1155 struct scatterlist *sg,
1156 unsigned int nbytes)
1157{
1158 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1159}
1160
1161static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1162{
1163 return crypto_hash_crt(desc->tfm)->final(desc, out);
1164}
1165
1166static inline int crypto_hash_digest(struct hash_desc *desc,
1167 struct scatterlist *sg,
1168 unsigned int nbytes, u8 *out)
1169{
1170 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1171}
1172
1173static inline int crypto_hash_setkey(struct crypto_hash *hash,
1174 const u8 *key, unsigned int keylen)
1175{
1176 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Herbert Xufce32d72006-08-26 17:35:45 +10001179static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1180{
1181 return (struct crypto_comp *)tfm;
1182}
1183
1184static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1185{
1186 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1187 CRYPTO_ALG_TYPE_MASK);
1188 return __crypto_comp_cast(tfm);
1189}
1190
1191static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1192 u32 type, u32 mask)
1193{
1194 type &= ~CRYPTO_ALG_TYPE_MASK;
1195 type |= CRYPTO_ALG_TYPE_COMPRESS;
1196 mask |= CRYPTO_ALG_TYPE_MASK;
1197
1198 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1199}
1200
1201static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1202{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001203 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001204}
1205
1206static inline void crypto_free_comp(struct crypto_comp *tfm)
1207{
1208 crypto_free_tfm(crypto_comp_tfm(tfm));
1209}
1210
1211static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1212{
1213 type &= ~CRYPTO_ALG_TYPE_MASK;
1214 type |= CRYPTO_ALG_TYPE_COMPRESS;
1215 mask |= CRYPTO_ALG_TYPE_MASK;
1216
1217 return crypto_has_alg(alg_name, type, mask);
1218}
1219
Herbert Xue4d5b792006-08-26 18:12:40 +10001220static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1221{
1222 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1223}
1224
Herbert Xufce32d72006-08-26 17:35:45 +10001225static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1226{
1227 return &crypto_comp_tfm(tfm)->crt_compress;
1228}
1229
1230static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 const u8 *src, unsigned int slen,
1232 u8 *dst, unsigned int *dlen)
1233{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001234 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1235 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Herbert Xufce32d72006-08-26 17:35:45 +10001238static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 const u8 *src, unsigned int slen,
1240 u8 *dst, unsigned int *dlen)
1241{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001242 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1243 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246#endif /* _LINUX_CRYPTO_H */
1247