blob: da09b4ac3ae9b419d85b526df8baa6ef2cc7d5a6 [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 Xub5b7f082007-04-16 20:48:54 +100098struct crypto_queue;
Herbert Xu40725182005-07-06 13:51:52 -070099struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000100struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700101
Herbert Xu32e39832007-03-24 14:35:34 +1100102typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
103
104struct crypto_async_request {
105 struct list_head list;
106 crypto_completion_t complete;
107 void *data;
108 struct crypto_tfm *tfm;
109
110 u32 flags;
111};
112
113struct ablkcipher_request {
114 struct crypto_async_request base;
115
116 unsigned int nbytes;
117
118 void *info;
119
120 struct scatterlist *src;
121 struct scatterlist *dst;
122
123 void *__ctx[] CRYPTO_MINALIGN_ATTR;
124};
125
Herbert Xu1ae97822007-08-30 15:36:14 +0800126/**
127 * struct aead_request - AEAD request
128 * @base: Common attributes for async crypto requests
129 * @assoclen: Length in bytes of associated data for authentication
130 * @cryptlen: Length of data to be encrypted or decrypted
131 * @iv: Initialisation vector
132 * @assoc: Associated data
133 * @src: Source data
134 * @dst: Destination data
135 * @__ctx: Start of private context data
136 */
137struct aead_request {
138 struct crypto_async_request base;
139
140 unsigned int assoclen;
141 unsigned int cryptlen;
142
143 u8 *iv;
144
145 struct scatterlist *assoc;
146 struct scatterlist *src;
147 struct scatterlist *dst;
148
149 void *__ctx[] CRYPTO_MINALIGN_ATTR;
150};
151
Herbert Xu5cde0af2006-08-22 00:07:53 +1000152struct blkcipher_desc {
153 struct crypto_blkcipher *tfm;
154 void *info;
155 u32 flags;
156};
157
Herbert Xu40725182005-07-06 13:51:52 -0700158struct cipher_desc {
159 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000160 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700161 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
162 const u8 *src, unsigned int nbytes);
163 void *info;
164};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Herbert Xu055bcee2006-08-19 22:24:23 +1000166struct hash_desc {
167 struct crypto_hash *tfm;
168 u32 flags;
169};
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * Algorithms: modular crypto algorithm implementations, managed
173 * via crypto_register_alg() and crypto_unregister_alg().
174 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000175struct ablkcipher_alg {
176 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
177 unsigned int keylen);
178 int (*encrypt)(struct ablkcipher_request *req);
179 int (*decrypt)(struct ablkcipher_request *req);
180
181 struct crypto_queue *queue;
182
183 unsigned int min_keysize;
184 unsigned int max_keysize;
185 unsigned int ivsize;
186};
187
Herbert Xu1ae97822007-08-30 15:36:14 +0800188struct aead_alg {
189 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
190 unsigned int keylen);
191 int (*encrypt)(struct aead_request *req);
192 int (*decrypt)(struct aead_request *req);
193
194 unsigned int ivsize;
195 unsigned int authsize;
196};
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 Xu39e1ee012007-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 Xu39e1ee012007-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 Xu39e1ee012007-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;
536 mask |= CRYPTO_ALG_TYPE_MASK;
537
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;
558 mask |= CRYPTO_ALG_TYPE_MASK;
559
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
630static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm)
631{
632 return crypto_ablkcipher_crt(tfm)->reqsize;
633}
634
Herbert Xue196d622007-04-14 16:09:14 +1000635static inline void ablkcipher_request_set_tfm(
636 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
637{
638 req->base.tfm = crypto_ablkcipher_tfm(tfm);
639}
640
Herbert Xub5b7f082007-04-16 20:48:54 +1000641static inline struct ablkcipher_request *ablkcipher_request_cast(
642 struct crypto_async_request *req)
643{
644 return container_of(req, struct ablkcipher_request, base);
645}
646
Herbert Xu32e39832007-03-24 14:35:34 +1100647static inline struct ablkcipher_request *ablkcipher_request_alloc(
648 struct crypto_ablkcipher *tfm, gfp_t gfp)
649{
650 struct ablkcipher_request *req;
651
652 req = kmalloc(sizeof(struct ablkcipher_request) +
653 crypto_ablkcipher_reqsize(tfm), gfp);
654
655 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000656 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100657
658 return req;
659}
660
661static inline void ablkcipher_request_free(struct ablkcipher_request *req)
662{
663 kfree(req);
664}
665
666static inline void ablkcipher_request_set_callback(
667 struct ablkcipher_request *req,
668 u32 flags, crypto_completion_t complete, void *data)
669{
670 req->base.complete = complete;
671 req->base.data = data;
672 req->base.flags = flags;
673}
674
675static inline void ablkcipher_request_set_crypt(
676 struct ablkcipher_request *req,
677 struct scatterlist *src, struct scatterlist *dst,
678 unsigned int nbytes, void *iv)
679{
680 req->src = src;
681 req->dst = dst;
682 req->nbytes = nbytes;
683 req->info = iv;
684}
685
Herbert Xu1ae97822007-08-30 15:36:14 +0800686static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
687{
688 return (struct crypto_aead *)tfm;
689}
690
691static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
692 u32 type, u32 mask)
693{
694 type &= ~CRYPTO_ALG_TYPE_MASK;
695 type |= CRYPTO_ALG_TYPE_AEAD;
696 mask |= CRYPTO_ALG_TYPE_MASK;
697
698 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
699}
700
701static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
702{
703 return &tfm->base;
704}
705
706static inline void crypto_free_aead(struct crypto_aead *tfm)
707{
708 crypto_free_tfm(crypto_aead_tfm(tfm));
709}
710
711static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
712{
713 return &crypto_aead_tfm(tfm)->crt_aead;
714}
715
716static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
717{
718 return crypto_aead_crt(tfm)->ivsize;
719}
720
721static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
722{
723 return crypto_aead_crt(tfm)->authsize;
724}
725
726static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
727{
728 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
729}
730
731static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
732{
733 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
734}
735
736static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
737{
738 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
739}
740
741static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
742{
743 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
744}
745
746static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
747{
748 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
749}
750
751static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
752 unsigned int keylen)
753{
754 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
755}
756
757static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
758{
759 return __crypto_aead_cast(req->base.tfm);
760}
761
762static inline int crypto_aead_encrypt(struct aead_request *req)
763{
764 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
765}
766
767static inline int crypto_aead_decrypt(struct aead_request *req)
768{
769 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
770}
771
772static inline int crypto_aead_reqsize(struct crypto_aead *tfm)
773{
774 return crypto_aead_crt(tfm)->reqsize;
775}
776
777static inline void aead_request_set_tfm(struct aead_request *req,
778 struct crypto_aead *tfm)
779{
780 req->base.tfm = crypto_aead_tfm(tfm);
781}
782
783static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
784 gfp_t gfp)
785{
786 struct aead_request *req;
787
788 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
789
790 if (likely(req))
791 aead_request_set_tfm(req, tfm);
792
793 return req;
794}
795
796static inline void aead_request_free(struct aead_request *req)
797{
798 kfree(req);
799}
800
801static inline void aead_request_set_callback(struct aead_request *req,
802 u32 flags,
803 crypto_completion_t complete,
804 void *data)
805{
806 req->base.complete = complete;
807 req->base.data = data;
808 req->base.flags = flags;
809}
810
811static inline void aead_request_set_crypt(struct aead_request *req,
812 struct scatterlist *src,
813 struct scatterlist *dst,
814 unsigned int cryptlen, u8 *iv)
815{
816 req->src = src;
817 req->dst = dst;
818 req->cryptlen = cryptlen;
819 req->iv = iv;
820}
821
822static inline void aead_request_set_assoc(struct aead_request *req,
823 struct scatterlist *assoc,
824 unsigned int assoclen)
825{
826 req->assoc = assoc;
827 req->assoclen = assoclen;
828}
829
Herbert Xu5cde0af2006-08-22 00:07:53 +1000830static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
831 struct crypto_tfm *tfm)
832{
833 return (struct crypto_blkcipher *)tfm;
834}
835
836static inline struct crypto_blkcipher *crypto_blkcipher_cast(
837 struct crypto_tfm *tfm)
838{
839 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
840 return __crypto_blkcipher_cast(tfm);
841}
842
843static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
844 const char *alg_name, u32 type, u32 mask)
845{
Herbert Xu32e39832007-03-24 14:35:34 +1100846 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xu5cde0af2006-08-22 00:07:53 +1000847 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu32e39832007-03-24 14:35:34 +1100848 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000849
850 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
851}
852
853static inline struct crypto_tfm *crypto_blkcipher_tfm(
854 struct crypto_blkcipher *tfm)
855{
856 return &tfm->base;
857}
858
859static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
860{
861 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
862}
863
Herbert Xufce32d72006-08-26 17:35:45 +1000864static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
865{
Herbert Xu32e39832007-03-24 14:35:34 +1100866 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xufce32d72006-08-26 17:35:45 +1000867 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu32e39832007-03-24 14:35:34 +1100868 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
Herbert Xufce32d72006-08-26 17:35:45 +1000869
870 return crypto_has_alg(alg_name, type, mask);
871}
872
Herbert Xu5cde0af2006-08-22 00:07:53 +1000873static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
874{
875 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
876}
877
878static inline struct blkcipher_tfm *crypto_blkcipher_crt(
879 struct crypto_blkcipher *tfm)
880{
881 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
882}
883
884static inline struct blkcipher_alg *crypto_blkcipher_alg(
885 struct crypto_blkcipher *tfm)
886{
887 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
888}
889
890static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
891{
892 return crypto_blkcipher_alg(tfm)->ivsize;
893}
894
895static inline unsigned int crypto_blkcipher_blocksize(
896 struct crypto_blkcipher *tfm)
897{
898 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
899}
900
901static inline unsigned int crypto_blkcipher_alignmask(
902 struct crypto_blkcipher *tfm)
903{
904 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
905}
906
907static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
908{
909 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
910}
911
912static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
913 u32 flags)
914{
915 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
916}
917
918static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
919 u32 flags)
920{
921 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
922}
923
924static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
925 const u8 *key, unsigned int keylen)
926{
927 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
928 key, keylen);
929}
930
931static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
932 struct scatterlist *dst,
933 struct scatterlist *src,
934 unsigned int nbytes)
935{
936 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
937 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
938}
939
940static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
941 struct scatterlist *dst,
942 struct scatterlist *src,
943 unsigned int nbytes)
944{
945 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
946}
947
948static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
949 struct scatterlist *dst,
950 struct scatterlist *src,
951 unsigned int nbytes)
952{
953 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
954 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
955}
956
957static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
958 struct scatterlist *dst,
959 struct scatterlist *src,
960 unsigned int nbytes)
961{
962 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
963}
964
965static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
966 const u8 *src, unsigned int len)
967{
968 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
969}
970
971static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
972 u8 *dst, unsigned int len)
973{
974 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
975}
976
Herbert Xuf28776a2006-08-13 20:58:18 +1000977static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
978{
979 return (struct crypto_cipher *)tfm;
980}
981
982static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
983{
984 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
985 return __crypto_cipher_cast(tfm);
986}
987
988static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
989 u32 type, u32 mask)
990{
991 type &= ~CRYPTO_ALG_TYPE_MASK;
992 type |= CRYPTO_ALG_TYPE_CIPHER;
993 mask |= CRYPTO_ALG_TYPE_MASK;
994
995 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
996}
997
998static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
999{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001000 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001001}
1002
1003static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1004{
1005 crypto_free_tfm(crypto_cipher_tfm(tfm));
1006}
1007
Herbert Xufce32d72006-08-26 17:35:45 +10001008static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1009{
1010 type &= ~CRYPTO_ALG_TYPE_MASK;
1011 type |= CRYPTO_ALG_TYPE_CIPHER;
1012 mask |= CRYPTO_ALG_TYPE_MASK;
1013
1014 return crypto_has_alg(alg_name, type, mask);
1015}
1016
Herbert Xuf28776a2006-08-13 20:58:18 +10001017static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1018{
1019 return &crypto_cipher_tfm(tfm)->crt_cipher;
1020}
1021
1022static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1023{
1024 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1025}
1026
1027static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1028{
1029 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1030}
1031
1032static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1033{
1034 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1035}
1036
1037static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1038 u32 flags)
1039{
1040 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1041}
1042
1043static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1044 u32 flags)
1045{
1046 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1047}
1048
Herbert Xu7226bc872006-08-21 21:40:49 +10001049static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1050 const u8 *key, unsigned int keylen)
1051{
1052 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1053 key, keylen);
1054}
1055
Herbert Xuf28776a2006-08-13 20:58:18 +10001056static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1057 u8 *dst, const u8 *src)
1058{
1059 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1060 dst, src);
1061}
1062
1063static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1064 u8 *dst, const u8 *src)
1065{
1066 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1067 dst, src);
1068}
1069
Herbert Xu055bcee2006-08-19 22:24:23 +10001070static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Herbert Xu055bcee2006-08-19 22:24:23 +10001072 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Herbert Xu055bcee2006-08-19 22:24:23 +10001075static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Herbert Xu055bcee2006-08-19 22:24:23 +10001077 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1078 CRYPTO_ALG_TYPE_HASH_MASK);
1079 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Herbert Xu055bcee2006-08-19 22:24:23 +10001082static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1083 u32 type, u32 mask)
1084{
1085 type &= ~CRYPTO_ALG_TYPE_MASK;
1086 type |= CRYPTO_ALG_TYPE_HASH;
1087 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1088
1089 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1090}
1091
1092static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1093{
1094 return &tfm->base;
1095}
1096
1097static inline void crypto_free_hash(struct crypto_hash *tfm)
1098{
1099 crypto_free_tfm(crypto_hash_tfm(tfm));
1100}
1101
Herbert Xufce32d72006-08-26 17:35:45 +10001102static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1103{
1104 type &= ~CRYPTO_ALG_TYPE_MASK;
1105 type |= CRYPTO_ALG_TYPE_HASH;
1106 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1107
1108 return crypto_has_alg(alg_name, type, mask);
1109}
1110
Herbert Xu055bcee2006-08-19 22:24:23 +10001111static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1112{
1113 return &crypto_hash_tfm(tfm)->crt_hash;
1114}
1115
1116static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1117{
1118 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1119}
1120
1121static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1122{
1123 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1124}
1125
1126static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1127{
1128 return crypto_hash_crt(tfm)->digestsize;
1129}
1130
1131static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1132{
1133 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1134}
1135
1136static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1137{
1138 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1139}
1140
1141static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1142{
1143 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1144}
1145
1146static inline int crypto_hash_init(struct hash_desc *desc)
1147{
1148 return crypto_hash_crt(desc->tfm)->init(desc);
1149}
1150
1151static inline int crypto_hash_update(struct hash_desc *desc,
1152 struct scatterlist *sg,
1153 unsigned int nbytes)
1154{
1155 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1156}
1157
1158static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1159{
1160 return crypto_hash_crt(desc->tfm)->final(desc, out);
1161}
1162
1163static inline int crypto_hash_digest(struct hash_desc *desc,
1164 struct scatterlist *sg,
1165 unsigned int nbytes, u8 *out)
1166{
1167 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1168}
1169
1170static inline int crypto_hash_setkey(struct crypto_hash *hash,
1171 const u8 *key, unsigned int keylen)
1172{
1173 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Herbert Xufce32d72006-08-26 17:35:45 +10001176static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1177{
1178 return (struct crypto_comp *)tfm;
1179}
1180
1181static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1182{
1183 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1184 CRYPTO_ALG_TYPE_MASK);
1185 return __crypto_comp_cast(tfm);
1186}
1187
1188static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1189 u32 type, u32 mask)
1190{
1191 type &= ~CRYPTO_ALG_TYPE_MASK;
1192 type |= CRYPTO_ALG_TYPE_COMPRESS;
1193 mask |= CRYPTO_ALG_TYPE_MASK;
1194
1195 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1196}
1197
1198static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1199{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001200 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001201}
1202
1203static inline void crypto_free_comp(struct crypto_comp *tfm)
1204{
1205 crypto_free_tfm(crypto_comp_tfm(tfm));
1206}
1207
1208static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1209{
1210 type &= ~CRYPTO_ALG_TYPE_MASK;
1211 type |= CRYPTO_ALG_TYPE_COMPRESS;
1212 mask |= CRYPTO_ALG_TYPE_MASK;
1213
1214 return crypto_has_alg(alg_name, type, mask);
1215}
1216
Herbert Xue4d5b792006-08-26 18:12:40 +10001217static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1218{
1219 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1220}
1221
Herbert Xufce32d72006-08-26 17:35:45 +10001222static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1223{
1224 return &crypto_comp_tfm(tfm)->crt_compress;
1225}
1226
1227static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 const u8 *src, unsigned int slen,
1229 u8 *dst, unsigned int *dlen)
1230{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001231 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1232 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Herbert Xufce32d72006-08-26 17:35:45 +10001235static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 const u8 *src, unsigned int slen,
1237 u8 *dst, unsigned int *dlen)
1238{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001239 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1240 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#endif /* _LINUX_CRYPTO_H */
1244