blob: fc32694287e21cee1b2508f0456bbd29552479a1 [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>
9 * and Nettle, by Niels Möller.
10 *
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
36#define CRYPTO_ALG_TYPE_COMPRESS 0x00000005
Herbert Xu1ae97822007-08-30 15:36:14 +080037#define CRYPTO_ALG_TYPE_AEAD 0x00000006
Herbert Xu055bcee2006-08-19 22:24:23 +100038
39#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Herbert Xu28259822006-08-06 21:23:26 +100041#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100042#define CRYPTO_ALG_DEAD 0x00000020
43#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100044#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Herbert Xu60104392006-08-26 18:34:10 +100047 * Set this bit if and only if the algorithm requires another algorithm of
48 * the same type to handle corner cases.
49 */
50#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
51
52/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * Transform masks and values (for crt_flags).
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define CRYPTO_TFM_REQ_MASK 0x000fff00
56#define CRYPTO_TFM_RES_MASK 0xfff00000
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070059#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110060#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
62#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
63#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
64#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
65#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
66
67/*
68 * Miscellaneous stuff.
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define CRYPTO_MAX_ALG_NAME 64
71
Herbert Xu79911102006-08-21 21:03:52 +100072/*
73 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
74 * declaration) is used to ensure that the crypto_tfm context structure is
75 * aligned correctly for the given architecture so that there are no alignment
76 * faults for C data types. In particular, this is required on platforms such
77 * as arm where pointers are 32-bit aligned but there are data types such as
78 * u64 which require 64-bit alignment.
79 */
80#if defined(ARCH_KMALLOC_MINALIGN)
81#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
82#elif defined(ARCH_SLAB_MINALIGN)
83#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
84#endif
85
86#ifdef CRYPTO_MINALIGN
87#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
88#else
89#define CRYPTO_MINALIGN_ATTR
90#endif
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +110093struct crypto_ablkcipher;
94struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +080095struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +100096struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +100097struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -070098struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +100099struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700100
Herbert Xu32e39832007-03-24 14:35:34 +1100101typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
102
103struct crypto_async_request {
104 struct list_head list;
105 crypto_completion_t complete;
106 void *data;
107 struct crypto_tfm *tfm;
108
109 u32 flags;
110};
111
112struct ablkcipher_request {
113 struct crypto_async_request base;
114
115 unsigned int nbytes;
116
117 void *info;
118
119 struct scatterlist *src;
120 struct scatterlist *dst;
121
122 void *__ctx[] CRYPTO_MINALIGN_ATTR;
123};
124
Herbert Xu1ae97822007-08-30 15:36:14 +0800125/**
126 * struct aead_request - AEAD request
127 * @base: Common attributes for async crypto requests
128 * @assoclen: Length in bytes of associated data for authentication
129 * @cryptlen: Length of data to be encrypted or decrypted
130 * @iv: Initialisation vector
131 * @assoc: Associated data
132 * @src: Source data
133 * @dst: Destination data
134 * @__ctx: Start of private context data
135 */
136struct aead_request {
137 struct crypto_async_request base;
138
139 unsigned int assoclen;
140 unsigned int cryptlen;
141
142 u8 *iv;
143
144 struct scatterlist *assoc;
145 struct scatterlist *src;
146 struct scatterlist *dst;
147
148 void *__ctx[] CRYPTO_MINALIGN_ATTR;
149};
150
Herbert Xu5cde0af2006-08-22 00:07:53 +1000151struct blkcipher_desc {
152 struct crypto_blkcipher *tfm;
153 void *info;
154 u32 flags;
155};
156
Herbert Xu40725182005-07-06 13:51:52 -0700157struct cipher_desc {
158 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000159 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700160 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
161 const u8 *src, unsigned int nbytes);
162 void *info;
163};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Herbert Xu055bcee2006-08-19 22:24:23 +1000165struct hash_desc {
166 struct crypto_hash *tfm;
167 u32 flags;
168};
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * Algorithms: modular crypto algorithm implementations, managed
172 * via crypto_register_alg() and crypto_unregister_alg().
173 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000174struct ablkcipher_alg {
175 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
176 unsigned int keylen);
177 int (*encrypt)(struct ablkcipher_request *req);
178 int (*decrypt)(struct ablkcipher_request *req);
179
Herbert Xub5b7f082007-04-16 20:48:54 +1000180 unsigned int min_keysize;
181 unsigned int max_keysize;
182 unsigned int ivsize;
183};
184
Herbert Xu1ae97822007-08-30 15:36:14 +0800185struct aead_alg {
186 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
187 unsigned int keylen);
188 int (*encrypt)(struct aead_request *req);
189 int (*decrypt)(struct aead_request *req);
190
191 unsigned int ivsize;
192 unsigned int authsize;
193};
194
Herbert Xu5cde0af2006-08-22 00:07:53 +1000195struct blkcipher_alg {
196 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
197 unsigned int keylen);
198 int (*encrypt)(struct blkcipher_desc *desc,
199 struct scatterlist *dst, struct scatterlist *src,
200 unsigned int nbytes);
201 int (*decrypt)(struct blkcipher_desc *desc,
202 struct scatterlist *dst, struct scatterlist *src,
203 unsigned int nbytes);
204
205 unsigned int min_keysize;
206 unsigned int max_keysize;
207 unsigned int ivsize;
208};
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210struct cipher_alg {
211 unsigned int cia_min_keysize;
212 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000213 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000214 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000215 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
216 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219struct digest_alg {
220 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000221 void (*dia_init)(struct crypto_tfm *tfm);
222 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
223 unsigned int len);
224 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
225 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000226 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
Herbert Xu055bcee2006-08-19 22:24:23 +1000229struct hash_alg {
230 int (*init)(struct hash_desc *desc);
231 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
232 unsigned int nbytes);
233 int (*final)(struct hash_desc *desc, u8 *out);
234 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
235 unsigned int nbytes, u8 *out);
236 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
237 unsigned int keylen);
238
239 unsigned int digestsize;
240};
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000243 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
244 unsigned int slen, u8 *dst, unsigned int *dlen);
245 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
246 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Herbert Xub5b7f082007-04-16 20:48:54 +1000249#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800250#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000251#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define cra_cipher cra_u.cipher
253#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000254#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#define cra_compress cra_u.compress
256
257struct crypto_alg {
258 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000259 struct list_head cra_users;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 u32 cra_flags;
262 unsigned int cra_blocksize;
263 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700264 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100265
266 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000267 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100268
Herbert Xud913ea02006-05-21 08:45:26 +1000269 char cra_name[CRYPTO_MAX_ALG_NAME];
270 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Herbert Xue853c3c2006-08-22 00:06:54 +1000272 const struct crypto_type *cra_type;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000275 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800276 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000277 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct cipher_alg cipher;
279 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000280 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct compress_alg compress;
282 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000283
284 int (*cra_init)(struct crypto_tfm *tfm);
285 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000286 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 struct module *cra_module;
289};
290
291/*
292 * Algorithm registration interface.
293 */
294int crypto_register_alg(struct crypto_alg *alg);
295int crypto_unregister_alg(struct crypto_alg *alg);
296
297/*
298 * Algorithm query interface.
299 */
300#ifdef CONFIG_CRYPTO
Herbert Xufce32d72006-08-26 17:35:45 +1000301int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#else
Herbert Xufce32d72006-08-26 17:35:45 +1000303static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
304{
305 return 0;
306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#endif
308
309/*
310 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000311 * and core processing logic. Managed via crypto_alloc_*() and
312 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Herbert Xu32e39832007-03-24 14:35:34 +1100315struct ablkcipher_tfm {
316 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
317 unsigned int keylen);
318 int (*encrypt)(struct ablkcipher_request *req);
319 int (*decrypt)(struct ablkcipher_request *req);
320 unsigned int ivsize;
321 unsigned int reqsize;
322};
323
Herbert Xu1ae97822007-08-30 15:36:14 +0800324struct aead_tfm {
325 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
326 unsigned int keylen);
327 int (*encrypt)(struct aead_request *req);
328 int (*decrypt)(struct aead_request *req);
329 unsigned int ivsize;
330 unsigned int authsize;
331 unsigned int reqsize;
332};
333
Herbert Xu5cde0af2006-08-22 00:07:53 +1000334struct blkcipher_tfm {
335 void *iv;
336 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
337 unsigned int keylen);
338 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
339 struct scatterlist *src, unsigned int nbytes);
340 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
341 struct scatterlist *src, unsigned int nbytes);
342};
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int (*cit_setkey)(struct crypto_tfm *tfm,
346 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000347 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
348 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
Herbert Xu055bcee2006-08-19 22:24:23 +1000351struct hash_tfm {
352 int (*init)(struct hash_desc *desc);
353 int (*update)(struct hash_desc *desc,
354 struct scatterlist *sg, unsigned int nsg);
355 int (*final)(struct hash_desc *desc, u8 *out);
356 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
357 unsigned int nsg, u8 *out);
358 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
359 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000360 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
363struct compress_tfm {
364 int (*cot_compress)(struct crypto_tfm *tfm,
365 const u8 *src, unsigned int slen,
366 u8 *dst, unsigned int *dlen);
367 int (*cot_decompress)(struct crypto_tfm *tfm,
368 const u8 *src, unsigned int slen,
369 u8 *dst, unsigned int *dlen);
370};
371
Herbert Xu32e39832007-03-24 14:35:34 +1100372#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800373#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000374#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000376#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define crt_compress crt_u.compress
378
379struct crypto_tfm {
380
381 u32 crt_flags;
382
383 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100384 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800385 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000386 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000388 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct compress_tfm compress;
390 } crt_u;
391
392 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100393
Herbert Xu79911102006-08-21 21:03:52 +1000394 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Herbert Xu32e39832007-03-24 14:35:34 +1100397struct crypto_ablkcipher {
398 struct crypto_tfm base;
399};
400
Herbert Xu1ae97822007-08-30 15:36:14 +0800401struct crypto_aead {
402 struct crypto_tfm base;
403};
404
Herbert Xu5cde0af2006-08-22 00:07:53 +1000405struct crypto_blkcipher {
406 struct crypto_tfm base;
407};
408
Herbert Xu78a1fe42006-12-24 10:02:00 +1100409struct crypto_cipher {
410 struct crypto_tfm base;
411};
412
413struct crypto_comp {
414 struct crypto_tfm base;
415};
416
Herbert Xu055bcee2006-08-19 22:24:23 +1000417struct crypto_hash {
418 struct crypto_tfm base;
419};
420
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000421enum {
422 CRYPTOA_UNSPEC,
423 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100424 CRYPTOA_TYPE,
Herbert Xu39e1ee02007-08-29 19:27:26 +0800425 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100426 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000427};
428
Herbert Xuebc610e2007-01-01 18:37:02 +1100429#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
430
Herbert Xu39e1ee02007-08-29 19:27:26 +0800431/* Maximum number of (rtattr) parameters for each template. */
432#define CRYPTO_MAX_ATTRS 32
433
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000434struct crypto_attr_alg {
435 char name[CRYPTO_MAX_ALG_NAME];
436};
437
Herbert Xuebc610e2007-01-01 18:37:02 +1100438struct crypto_attr_type {
439 u32 type;
440 u32 mask;
441};
442
Herbert Xu39e1ee02007-08-29 19:27:26 +0800443struct crypto_attr_u32 {
444 u32 num;
445};
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
448 * Transform user interface.
449 */
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000452struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453void crypto_free_tfm(struct crypto_tfm *tfm);
454
455/*
456 * Transform helpers which query the underlying algorithm.
457 */
458static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
459{
460 return tfm->__crt_alg->cra_name;
461}
462
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000463static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
464{
465 return tfm->__crt_alg->cra_driver_name;
466}
467
468static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
469{
470 return tfm->__crt_alg->cra_priority;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
474{
475 return module_name(tfm->__crt_alg->cra_module);
476}
477
478static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
479{
480 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
484{
485 return tfm->__crt_alg->cra_blocksize;
486}
487
Herbert Xufbdae9f2005-07-06 13:53:29 -0700488static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
489{
490 return tfm->__crt_alg->cra_alignmask;
491}
492
Herbert Xuf28776a2006-08-13 20:58:18 +1000493static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
494{
495 return tfm->crt_flags;
496}
497
498static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
499{
500 tfm->crt_flags |= flags;
501}
502
503static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
504{
505 tfm->crt_flags &= ~flags;
506}
507
Herbert Xu40725182005-07-06 13:51:52 -0700508static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
509{
Herbert Xuf10b7892006-01-25 22:34:01 +1100510 return tfm->__crt_ctx;
511}
512
513static inline unsigned int crypto_tfm_ctx_alignment(void)
514{
515 struct crypto_tfm *tfm;
516 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * API wrappers.
521 */
Herbert Xu32e39832007-03-24 14:35:34 +1100522static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
523 struct crypto_tfm *tfm)
524{
525 return (struct crypto_ablkcipher *)tfm;
526}
527
528static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
529 const char *alg_name, u32 type, u32 mask)
530{
531 type &= ~CRYPTO_ALG_TYPE_MASK;
532 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
533 mask |= CRYPTO_ALG_TYPE_MASK;
534
535 return __crypto_ablkcipher_cast(
536 crypto_alloc_base(alg_name, type, mask));
537}
538
539static inline struct crypto_tfm *crypto_ablkcipher_tfm(
540 struct crypto_ablkcipher *tfm)
541{
542 return &tfm->base;
543}
544
545static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
546{
547 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
548}
549
550static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
551 u32 mask)
552{
553 type &= ~CRYPTO_ALG_TYPE_MASK;
554 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
555 mask |= CRYPTO_ALG_TYPE_MASK;
556
557 return crypto_has_alg(alg_name, type, mask);
558}
559
560static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
561 struct crypto_ablkcipher *tfm)
562{
563 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
564}
565
566static inline unsigned int crypto_ablkcipher_ivsize(
567 struct crypto_ablkcipher *tfm)
568{
569 return crypto_ablkcipher_crt(tfm)->ivsize;
570}
571
572static inline unsigned int crypto_ablkcipher_blocksize(
573 struct crypto_ablkcipher *tfm)
574{
575 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
576}
577
578static inline unsigned int crypto_ablkcipher_alignmask(
579 struct crypto_ablkcipher *tfm)
580{
581 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
582}
583
584static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
585{
586 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
587}
588
589static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
590 u32 flags)
591{
592 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
593}
594
595static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
596 u32 flags)
597{
598 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
599}
600
601static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
602 const u8 *key, unsigned int keylen)
603{
604 return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
605}
606
607static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
608 struct ablkcipher_request *req)
609{
610 return __crypto_ablkcipher_cast(req->base.tfm);
611}
612
613static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
614{
615 struct ablkcipher_tfm *crt =
616 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
617 return crt->encrypt(req);
618}
619
620static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
621{
622 struct ablkcipher_tfm *crt =
623 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
624 return crt->decrypt(req);
625}
626
Herbert Xub16c3a22007-08-29 19:02:04 +0800627static inline unsigned int crypto_ablkcipher_reqsize(
628 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100629{
630 return crypto_ablkcipher_crt(tfm)->reqsize;
631}
632
Herbert Xue196d622007-04-14 16:09:14 +1000633static inline void ablkcipher_request_set_tfm(
634 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
635{
636 req->base.tfm = crypto_ablkcipher_tfm(tfm);
637}
638
Herbert Xub5b7f082007-04-16 20:48:54 +1000639static inline struct ablkcipher_request *ablkcipher_request_cast(
640 struct crypto_async_request *req)
641{
642 return container_of(req, struct ablkcipher_request, base);
643}
644
Herbert Xu32e39832007-03-24 14:35:34 +1100645static inline struct ablkcipher_request *ablkcipher_request_alloc(
646 struct crypto_ablkcipher *tfm, gfp_t gfp)
647{
648 struct ablkcipher_request *req;
649
650 req = kmalloc(sizeof(struct ablkcipher_request) +
651 crypto_ablkcipher_reqsize(tfm), gfp);
652
653 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000654 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100655
656 return req;
657}
658
659static inline void ablkcipher_request_free(struct ablkcipher_request *req)
660{
661 kfree(req);
662}
663
664static inline void ablkcipher_request_set_callback(
665 struct ablkcipher_request *req,
666 u32 flags, crypto_completion_t complete, void *data)
667{
668 req->base.complete = complete;
669 req->base.data = data;
670 req->base.flags = flags;
671}
672
673static inline void ablkcipher_request_set_crypt(
674 struct ablkcipher_request *req,
675 struct scatterlist *src, struct scatterlist *dst,
676 unsigned int nbytes, void *iv)
677{
678 req->src = src;
679 req->dst = dst;
680 req->nbytes = nbytes;
681 req->info = iv;
682}
683
Herbert Xu1ae97822007-08-30 15:36:14 +0800684static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
685{
686 return (struct crypto_aead *)tfm;
687}
688
689static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
690 u32 type, u32 mask)
691{
692 type &= ~CRYPTO_ALG_TYPE_MASK;
693 type |= CRYPTO_ALG_TYPE_AEAD;
694 mask |= CRYPTO_ALG_TYPE_MASK;
695
696 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
697}
698
699static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
700{
701 return &tfm->base;
702}
703
704static inline void crypto_free_aead(struct crypto_aead *tfm)
705{
706 crypto_free_tfm(crypto_aead_tfm(tfm));
707}
708
709static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
710{
711 return &crypto_aead_tfm(tfm)->crt_aead;
712}
713
714static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
715{
716 return crypto_aead_crt(tfm)->ivsize;
717}
718
719static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
720{
721 return crypto_aead_crt(tfm)->authsize;
722}
723
724static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
725{
726 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
727}
728
729static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
730{
731 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
732}
733
734static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
735{
736 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
737}
738
739static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
740{
741 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
742}
743
744static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
745{
746 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
747}
748
749static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
750 unsigned int keylen)
751{
752 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
753}
754
755static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
756{
757 return __crypto_aead_cast(req->base.tfm);
758}
759
760static inline int crypto_aead_encrypt(struct aead_request *req)
761{
762 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
763}
764
765static inline int crypto_aead_decrypt(struct aead_request *req)
766{
767 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
768}
769
Herbert Xub16c3a22007-08-29 19:02:04 +0800770static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800771{
772 return crypto_aead_crt(tfm)->reqsize;
773}
774
775static inline void aead_request_set_tfm(struct aead_request *req,
776 struct crypto_aead *tfm)
777{
778 req->base.tfm = crypto_aead_tfm(tfm);
779}
780
781static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
782 gfp_t gfp)
783{
784 struct aead_request *req;
785
786 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
787
788 if (likely(req))
789 aead_request_set_tfm(req, tfm);
790
791 return req;
792}
793
794static inline void aead_request_free(struct aead_request *req)
795{
796 kfree(req);
797}
798
799static inline void aead_request_set_callback(struct aead_request *req,
800 u32 flags,
801 crypto_completion_t complete,
802 void *data)
803{
804 req->base.complete = complete;
805 req->base.data = data;
806 req->base.flags = flags;
807}
808
809static inline void aead_request_set_crypt(struct aead_request *req,
810 struct scatterlist *src,
811 struct scatterlist *dst,
812 unsigned int cryptlen, u8 *iv)
813{
814 req->src = src;
815 req->dst = dst;
816 req->cryptlen = cryptlen;
817 req->iv = iv;
818}
819
820static inline void aead_request_set_assoc(struct aead_request *req,
821 struct scatterlist *assoc,
822 unsigned int assoclen)
823{
824 req->assoc = assoc;
825 req->assoclen = assoclen;
826}
827
Herbert Xu5cde0af2006-08-22 00:07:53 +1000828static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
829 struct crypto_tfm *tfm)
830{
831 return (struct crypto_blkcipher *)tfm;
832}
833
834static inline struct crypto_blkcipher *crypto_blkcipher_cast(
835 struct crypto_tfm *tfm)
836{
837 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
838 return __crypto_blkcipher_cast(tfm);
839}
840
841static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
842 const char *alg_name, u32 type, u32 mask)
843{
Herbert Xu32e39832007-03-24 14:35:34 +1100844 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xu5cde0af2006-08-22 00:07:53 +1000845 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu32e39832007-03-24 14:35:34 +1100846 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000847
848 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
849}
850
851static inline struct crypto_tfm *crypto_blkcipher_tfm(
852 struct crypto_blkcipher *tfm)
853{
854 return &tfm->base;
855}
856
857static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
858{
859 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
860}
861
Herbert Xufce32d72006-08-26 17:35:45 +1000862static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
863{
Herbert Xu32e39832007-03-24 14:35:34 +1100864 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xufce32d72006-08-26 17:35:45 +1000865 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu32e39832007-03-24 14:35:34 +1100866 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
Herbert Xufce32d72006-08-26 17:35:45 +1000867
868 return crypto_has_alg(alg_name, type, mask);
869}
870
Herbert Xu5cde0af2006-08-22 00:07:53 +1000871static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
872{
873 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
874}
875
876static inline struct blkcipher_tfm *crypto_blkcipher_crt(
877 struct crypto_blkcipher *tfm)
878{
879 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
880}
881
882static inline struct blkcipher_alg *crypto_blkcipher_alg(
883 struct crypto_blkcipher *tfm)
884{
885 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
886}
887
888static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
889{
890 return crypto_blkcipher_alg(tfm)->ivsize;
891}
892
893static inline unsigned int crypto_blkcipher_blocksize(
894 struct crypto_blkcipher *tfm)
895{
896 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
897}
898
899static inline unsigned int crypto_blkcipher_alignmask(
900 struct crypto_blkcipher *tfm)
901{
902 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
903}
904
905static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
906{
907 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
908}
909
910static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
911 u32 flags)
912{
913 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
914}
915
916static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
917 u32 flags)
918{
919 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
920}
921
922static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
923 const u8 *key, unsigned int keylen)
924{
925 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
926 key, keylen);
927}
928
929static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
930 struct scatterlist *dst,
931 struct scatterlist *src,
932 unsigned int nbytes)
933{
934 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
935 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
936}
937
938static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
939 struct scatterlist *dst,
940 struct scatterlist *src,
941 unsigned int nbytes)
942{
943 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
944}
945
946static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
947 struct scatterlist *dst,
948 struct scatterlist *src,
949 unsigned int nbytes)
950{
951 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
952 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
953}
954
955static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
956 struct scatterlist *dst,
957 struct scatterlist *src,
958 unsigned int nbytes)
959{
960 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
961}
962
963static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
964 const u8 *src, unsigned int len)
965{
966 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
967}
968
969static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
970 u8 *dst, unsigned int len)
971{
972 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
973}
974
Herbert Xuf28776a2006-08-13 20:58:18 +1000975static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
976{
977 return (struct crypto_cipher *)tfm;
978}
979
980static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
981{
982 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
983 return __crypto_cipher_cast(tfm);
984}
985
986static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
987 u32 type, u32 mask)
988{
989 type &= ~CRYPTO_ALG_TYPE_MASK;
990 type |= CRYPTO_ALG_TYPE_CIPHER;
991 mask |= CRYPTO_ALG_TYPE_MASK;
992
993 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
994}
995
996static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
997{
Herbert Xu78a1fe42006-12-24 10:02:00 +1100998 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +1000999}
1000
1001static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1002{
1003 crypto_free_tfm(crypto_cipher_tfm(tfm));
1004}
1005
Herbert Xufce32d72006-08-26 17:35:45 +10001006static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1007{
1008 type &= ~CRYPTO_ALG_TYPE_MASK;
1009 type |= CRYPTO_ALG_TYPE_CIPHER;
1010 mask |= CRYPTO_ALG_TYPE_MASK;
1011
1012 return crypto_has_alg(alg_name, type, mask);
1013}
1014
Herbert Xuf28776a2006-08-13 20:58:18 +10001015static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1016{
1017 return &crypto_cipher_tfm(tfm)->crt_cipher;
1018}
1019
1020static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1021{
1022 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1023}
1024
1025static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1026{
1027 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1028}
1029
1030static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1031{
1032 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1033}
1034
1035static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1036 u32 flags)
1037{
1038 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1039}
1040
1041static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1042 u32 flags)
1043{
1044 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1045}
1046
Herbert Xu7226bc82006-08-21 21:40:49 +10001047static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1048 const u8 *key, unsigned int keylen)
1049{
1050 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1051 key, keylen);
1052}
1053
Herbert Xuf28776a2006-08-13 20:58:18 +10001054static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1055 u8 *dst, const u8 *src)
1056{
1057 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1058 dst, src);
1059}
1060
1061static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1062 u8 *dst, const u8 *src)
1063{
1064 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1065 dst, src);
1066}
1067
Herbert Xu055bcee2006-08-19 22:24:23 +10001068static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Herbert Xu055bcee2006-08-19 22:24:23 +10001070 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
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 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1076 CRYPTO_ALG_TYPE_HASH_MASK);
1077 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Herbert Xu055bcee2006-08-19 22:24:23 +10001080static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1081 u32 type, u32 mask)
1082{
1083 type &= ~CRYPTO_ALG_TYPE_MASK;
1084 type |= CRYPTO_ALG_TYPE_HASH;
1085 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1086
1087 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1088}
1089
1090static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1091{
1092 return &tfm->base;
1093}
1094
1095static inline void crypto_free_hash(struct crypto_hash *tfm)
1096{
1097 crypto_free_tfm(crypto_hash_tfm(tfm));
1098}
1099
Herbert Xufce32d72006-08-26 17:35:45 +10001100static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1101{
1102 type &= ~CRYPTO_ALG_TYPE_MASK;
1103 type |= CRYPTO_ALG_TYPE_HASH;
1104 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1105
1106 return crypto_has_alg(alg_name, type, mask);
1107}
1108
Herbert Xu055bcee2006-08-19 22:24:23 +10001109static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1110{
1111 return &crypto_hash_tfm(tfm)->crt_hash;
1112}
1113
1114static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1115{
1116 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1117}
1118
1119static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1120{
1121 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1122}
1123
1124static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1125{
1126 return crypto_hash_crt(tfm)->digestsize;
1127}
1128
1129static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1130{
1131 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1132}
1133
1134static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1135{
1136 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1137}
1138
1139static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1140{
1141 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1142}
1143
1144static inline int crypto_hash_init(struct hash_desc *desc)
1145{
1146 return crypto_hash_crt(desc->tfm)->init(desc);
1147}
1148
1149static inline int crypto_hash_update(struct hash_desc *desc,
1150 struct scatterlist *sg,
1151 unsigned int nbytes)
1152{
1153 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1154}
1155
1156static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1157{
1158 return crypto_hash_crt(desc->tfm)->final(desc, out);
1159}
1160
1161static inline int crypto_hash_digest(struct hash_desc *desc,
1162 struct scatterlist *sg,
1163 unsigned int nbytes, u8 *out)
1164{
1165 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1166}
1167
1168static inline int crypto_hash_setkey(struct crypto_hash *hash,
1169 const u8 *key, unsigned int keylen)
1170{
1171 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Herbert Xufce32d72006-08-26 17:35:45 +10001174static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1175{
1176 return (struct crypto_comp *)tfm;
1177}
1178
1179static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1180{
1181 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1182 CRYPTO_ALG_TYPE_MASK);
1183 return __crypto_comp_cast(tfm);
1184}
1185
1186static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1187 u32 type, u32 mask)
1188{
1189 type &= ~CRYPTO_ALG_TYPE_MASK;
1190 type |= CRYPTO_ALG_TYPE_COMPRESS;
1191 mask |= CRYPTO_ALG_TYPE_MASK;
1192
1193 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1194}
1195
1196static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1197{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001198 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001199}
1200
1201static inline void crypto_free_comp(struct crypto_comp *tfm)
1202{
1203 crypto_free_tfm(crypto_comp_tfm(tfm));
1204}
1205
1206static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1207{
1208 type &= ~CRYPTO_ALG_TYPE_MASK;
1209 type |= CRYPTO_ALG_TYPE_COMPRESS;
1210 mask |= CRYPTO_ALG_TYPE_MASK;
1211
1212 return crypto_has_alg(alg_name, type, mask);
1213}
1214
Herbert Xue4d5b792006-08-26 18:12:40 +10001215static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1216{
1217 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1218}
1219
Herbert Xufce32d72006-08-26 17:35:45 +10001220static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1221{
1222 return &crypto_comp_tfm(tfm)->crt_compress;
1223}
1224
1225static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 const u8 *src, unsigned int slen,
1227 u8 *dst, unsigned int *dlen)
1228{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001229 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1230 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Herbert Xufce32d72006-08-26 17:35:45 +10001233static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 const u8 *src, unsigned int slen,
1235 u8 *dst, unsigned int *dlen)
1236{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001237 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1238 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241#endif /* _LINUX_CRYPTO_H */
1242