blob: 81d994a3bdaf1395db837a4ce2454ae733cc12c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Herbert Xu6521f302006-08-06 20:28:44 +100020#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Algorithm masks and types.
30 */
Herbert Xu28259822006-08-06 21:23:26 +100031#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080033#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100035#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080036#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080037#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Loc Ho004a4032008-05-14 20:41:47 +080038#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
39#define CRYPTO_ALG_TYPE_HASH 0x00000009
40#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Herbert Xu055bcee2006-08-19 22:24:23 +100041
42#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080043#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080044#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Herbert Xu28259822006-08-06 21:23:26 +100046#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100047#define CRYPTO_ALG_DEAD 0x00000020
48#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100049#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
Herbert Xu60104392006-08-26 18:34:10 +100052 * Set this bit if and only if the algorithm requires another algorithm of
53 * the same type to handle corner cases.
54 */
55#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
56
57/*
Herbert Xuecfc4322007-12-05 21:08:36 +110058 * This bit is set for symmetric key ciphers that have already been wrapped
59 * with a generic IV generator to prevent them from being wrapped again.
60 */
61#define CRYPTO_ALG_GENIV 0x00000200
62
63/*
Herbert Xu73d38642008-08-03 21:15:23 +080064 * Set if the algorithm has passed automated run-time testing. Note that
65 * if there is no run-time testing for a given algorithm it is considered
66 * to have passed.
67 */
68
69#define CRYPTO_ALG_TESTED 0x00000400
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * Transform masks and values (for crt_flags).
73 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define CRYPTO_TFM_REQ_MASK 0x000fff00
75#define CRYPTO_TFM_RES_MASK 0xfff00000
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070078#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110079#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
81#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
82#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
83#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
84#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
85
86/*
87 * Miscellaneous stuff.
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define CRYPTO_MAX_ALG_NAME 64
90
Herbert Xu79911102006-08-21 21:03:52 +100091/*
92 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
93 * declaration) is used to ensure that the crypto_tfm context structure is
94 * aligned correctly for the given architecture so that there are no alignment
95 * faults for C data types. In particular, this is required on platforms such
96 * as arm where pointers are 32-bit aligned but there are data types such as
97 * u64 which require 64-bit alignment.
98 */
99#if defined(ARCH_KMALLOC_MINALIGN)
100#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
101#elif defined(ARCH_SLAB_MINALIGN)
102#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
Herbert Xu6eb72282008-01-08 17:16:44 +1100103#else
104#define CRYPTO_MINALIGN __alignof__(unsigned long long)
Herbert Xu79911102006-08-21 21:03:52 +1000105#endif
106
Herbert Xu79911102006-08-21 21:03:52 +1000107#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100110struct crypto_ablkcipher;
111struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800112struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000113struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000114struct crypto_hash;
Loc Ho004a4032008-05-14 20:41:47 +0800115struct crypto_ahash;
Herbert Xu40725182005-07-06 13:51:52 -0700116struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000117struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800118struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800119struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700120
Herbert Xu32e39832007-03-24 14:35:34 +1100121typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
122
123struct crypto_async_request {
124 struct list_head list;
125 crypto_completion_t complete;
126 void *data;
127 struct crypto_tfm *tfm;
128
129 u32 flags;
130};
131
132struct ablkcipher_request {
133 struct crypto_async_request base;
134
135 unsigned int nbytes;
136
137 void *info;
138
139 struct scatterlist *src;
140 struct scatterlist *dst;
141
142 void *__ctx[] CRYPTO_MINALIGN_ATTR;
143};
144
Loc Ho004a4032008-05-14 20:41:47 +0800145struct ahash_request {
146 struct crypto_async_request base;
147
Loc Ho004a4032008-05-14 20:41:47 +0800148 unsigned int nbytes;
149 struct scatterlist *src;
150 u8 *result;
151
152 void *__ctx[] CRYPTO_MINALIGN_ATTR;
153};
154
Herbert Xu1ae97822007-08-30 15:36:14 +0800155/**
156 * struct aead_request - AEAD request
157 * @base: Common attributes for async crypto requests
158 * @assoclen: Length in bytes of associated data for authentication
159 * @cryptlen: Length of data to be encrypted or decrypted
160 * @iv: Initialisation vector
161 * @assoc: Associated data
162 * @src: Source data
163 * @dst: Destination data
164 * @__ctx: Start of private context data
165 */
166struct aead_request {
167 struct crypto_async_request base;
168
169 unsigned int assoclen;
170 unsigned int cryptlen;
171
172 u8 *iv;
173
174 struct scatterlist *assoc;
175 struct scatterlist *src;
176 struct scatterlist *dst;
177
178 void *__ctx[] CRYPTO_MINALIGN_ATTR;
179};
180
Herbert Xu5cde0af2006-08-22 00:07:53 +1000181struct blkcipher_desc {
182 struct crypto_blkcipher *tfm;
183 void *info;
184 u32 flags;
185};
186
Herbert Xu40725182005-07-06 13:51:52 -0700187struct cipher_desc {
188 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000189 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700190 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
191 const u8 *src, unsigned int nbytes);
192 void *info;
193};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Herbert Xu055bcee2006-08-19 22:24:23 +1000195struct hash_desc {
196 struct crypto_hash *tfm;
197 u32 flags;
198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * Algorithms: modular crypto algorithm implementations, managed
202 * via crypto_register_alg() and crypto_unregister_alg().
203 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000204struct ablkcipher_alg {
205 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
206 unsigned int keylen);
207 int (*encrypt)(struct ablkcipher_request *req);
208 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800209 int (*givencrypt)(struct skcipher_givcrypt_request *req);
210 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000211
Herbert Xu23508e12007-11-27 21:33:24 +0800212 const char *geniv;
213
Herbert Xub5b7f082007-04-16 20:48:54 +1000214 unsigned int min_keysize;
215 unsigned int max_keysize;
216 unsigned int ivsize;
217};
218
Loc Ho004a4032008-05-14 20:41:47 +0800219struct ahash_alg {
220 int (*init)(struct ahash_request *req);
221 int (*update)(struct ahash_request *req);
222 int (*final)(struct ahash_request *req);
223 int (*digest)(struct ahash_request *req);
224 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
225 unsigned int keylen);
226
227 unsigned int digestsize;
228};
229
Herbert Xu1ae97822007-08-30 15:36:14 +0800230struct aead_alg {
231 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
232 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100233 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800234 int (*encrypt)(struct aead_request *req);
235 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800236 int (*givencrypt)(struct aead_givcrypt_request *req);
237 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800238
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800239 const char *geniv;
240
Herbert Xu1ae97822007-08-30 15:36:14 +0800241 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100242 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800243};
244
Herbert Xu5cde0af2006-08-22 00:07:53 +1000245struct blkcipher_alg {
246 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
247 unsigned int keylen);
248 int (*encrypt)(struct blkcipher_desc *desc,
249 struct scatterlist *dst, struct scatterlist *src,
250 unsigned int nbytes);
251 int (*decrypt)(struct blkcipher_desc *desc,
252 struct scatterlist *dst, struct scatterlist *src,
253 unsigned int nbytes);
254
Herbert Xu23508e12007-11-27 21:33:24 +0800255 const char *geniv;
256
Herbert Xu5cde0af2006-08-22 00:07:53 +1000257 unsigned int min_keysize;
258 unsigned int max_keysize;
259 unsigned int ivsize;
260};
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262struct cipher_alg {
263 unsigned int cia_min_keysize;
264 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000265 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000266 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000267 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
268 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
271struct digest_alg {
272 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000273 void (*dia_init)(struct crypto_tfm *tfm);
274 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
275 unsigned int len);
276 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
277 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000278 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Herbert Xu055bcee2006-08-19 22:24:23 +1000281struct hash_alg {
282 int (*init)(struct hash_desc *desc);
283 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
284 unsigned int nbytes);
285 int (*final)(struct hash_desc *desc, u8 *out);
286 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
287 unsigned int nbytes, u8 *out);
288 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
289 unsigned int keylen);
290
291 unsigned int digestsize;
292};
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000295 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
296 unsigned int slen, u8 *dst, unsigned int *dlen);
297 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
298 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
Herbert Xub5b7f082007-04-16 20:48:54 +1000301#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800302#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000303#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#define cra_cipher cra_u.cipher
305#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000306#define cra_hash cra_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800307#define cra_ahash cra_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#define cra_compress cra_u.compress
309
310struct crypto_alg {
311 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000312 struct list_head cra_users;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 u32 cra_flags;
315 unsigned int cra_blocksize;
316 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700317 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100318
319 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000320 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100321
Herbert Xud913ea02006-05-21 08:45:26 +1000322 char cra_name[CRYPTO_MAX_ALG_NAME];
323 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Herbert Xue853c3c2006-08-22 00:06:54 +1000325 const struct crypto_type *cra_type;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000328 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800329 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000330 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct cipher_alg cipher;
332 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000333 struct hash_alg hash;
Loc Ho004a4032008-05-14 20:41:47 +0800334 struct ahash_alg ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct compress_alg compress;
336 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000337
338 int (*cra_init)(struct crypto_tfm *tfm);
339 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000340 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 struct module *cra_module;
343};
344
345/*
346 * Algorithm registration interface.
347 */
348int crypto_register_alg(struct crypto_alg *alg);
349int crypto_unregister_alg(struct crypto_alg *alg);
350
351/*
352 * Algorithm query interface.
353 */
Herbert Xufce32d72006-08-26 17:35:45 +1000354int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356/*
357 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000358 * and core processing logic. Managed via crypto_alloc_*() and
359 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Herbert Xu32e39832007-03-24 14:35:34 +1100362struct ablkcipher_tfm {
363 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
364 unsigned int keylen);
365 int (*encrypt)(struct ablkcipher_request *req);
366 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800367 int (*givencrypt)(struct skcipher_givcrypt_request *req);
368 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
369
Herbert Xuecfc4322007-12-05 21:08:36 +1100370 struct crypto_ablkcipher *base;
371
Herbert Xu32e39832007-03-24 14:35:34 +1100372 unsigned int ivsize;
373 unsigned int reqsize;
374};
375
Herbert Xu1ae97822007-08-30 15:36:14 +0800376struct aead_tfm {
377 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
378 unsigned int keylen);
379 int (*encrypt)(struct aead_request *req);
380 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800381 int (*givencrypt)(struct aead_givcrypt_request *req);
382 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800383
384 struct crypto_aead *base;
385
Herbert Xu1ae97822007-08-30 15:36:14 +0800386 unsigned int ivsize;
387 unsigned int authsize;
388 unsigned int reqsize;
389};
390
Herbert Xu5cde0af2006-08-22 00:07:53 +1000391struct blkcipher_tfm {
392 void *iv;
393 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
394 unsigned int keylen);
395 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
396 struct scatterlist *src, unsigned int nbytes);
397 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
398 struct scatterlist *src, unsigned int nbytes);
399};
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int (*cit_setkey)(struct crypto_tfm *tfm,
403 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000404 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
405 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406};
407
Herbert Xu055bcee2006-08-19 22:24:23 +1000408struct hash_tfm {
409 int (*init)(struct hash_desc *desc);
410 int (*update)(struct hash_desc *desc,
411 struct scatterlist *sg, unsigned int nsg);
412 int (*final)(struct hash_desc *desc, u8 *out);
413 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
414 unsigned int nsg, u8 *out);
415 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
416 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000417 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418};
419
Loc Ho004a4032008-05-14 20:41:47 +0800420struct ahash_tfm {
421 int (*init)(struct ahash_request *req);
422 int (*update)(struct ahash_request *req);
423 int (*final)(struct ahash_request *req);
424 int (*digest)(struct ahash_request *req);
425 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
426 unsigned int keylen);
427
428 unsigned int digestsize;
Loc Ho004a4032008-05-14 20:41:47 +0800429 unsigned int reqsize;
430};
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432struct compress_tfm {
433 int (*cot_compress)(struct crypto_tfm *tfm,
434 const u8 *src, unsigned int slen,
435 u8 *dst, unsigned int *dlen);
436 int (*cot_decompress)(struct crypto_tfm *tfm,
437 const u8 *src, unsigned int slen,
438 u8 *dst, unsigned int *dlen);
439};
440
Herbert Xu32e39832007-03-24 14:35:34 +1100441#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800442#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000443#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000445#define crt_hash crt_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800446#define crt_ahash crt_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#define crt_compress crt_u.compress
448
449struct crypto_tfm {
450
451 u32 crt_flags;
452
453 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100454 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800455 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000456 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000458 struct hash_tfm hash;
Loc Ho004a4032008-05-14 20:41:47 +0800459 struct ahash_tfm ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct compress_tfm compress;
461 } crt_u;
462
463 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100464
Herbert Xu79911102006-08-21 21:03:52 +1000465 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466};
467
Herbert Xu32e39832007-03-24 14:35:34 +1100468struct crypto_ablkcipher {
469 struct crypto_tfm base;
470};
471
Herbert Xu1ae97822007-08-30 15:36:14 +0800472struct crypto_aead {
473 struct crypto_tfm base;
474};
475
Herbert Xu5cde0af2006-08-22 00:07:53 +1000476struct crypto_blkcipher {
477 struct crypto_tfm base;
478};
479
Herbert Xu78a1fe42006-12-24 10:02:00 +1100480struct crypto_cipher {
481 struct crypto_tfm base;
482};
483
484struct crypto_comp {
485 struct crypto_tfm base;
486};
487
Herbert Xu055bcee2006-08-19 22:24:23 +1000488struct crypto_hash {
489 struct crypto_tfm base;
490};
491
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000492enum {
493 CRYPTOA_UNSPEC,
494 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100495 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800496 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100497 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000498};
499
Herbert Xuebc610e2007-01-01 18:37:02 +1100500#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
501
Herbert Xu39e1ee012007-08-29 19:27:26 +0800502/* Maximum number of (rtattr) parameters for each template. */
503#define CRYPTO_MAX_ATTRS 32
504
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000505struct crypto_attr_alg {
506 char name[CRYPTO_MAX_ALG_NAME];
507};
508
Herbert Xuebc610e2007-01-01 18:37:02 +1100509struct crypto_attr_type {
510 u32 type;
511 u32 mask;
512};
513
Herbert Xu39e1ee012007-08-29 19:27:26 +0800514struct crypto_attr_u32 {
515 u32 num;
516};
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/*
519 * Transform user interface.
520 */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000523struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524void crypto_free_tfm(struct crypto_tfm *tfm);
525
Herbert Xuda7f0332008-07-31 17:08:25 +0800526int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/*
529 * Transform helpers which query the underlying algorithm.
530 */
531static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
532{
533 return tfm->__crt_alg->cra_name;
534}
535
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000536static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
537{
538 return tfm->__crt_alg->cra_driver_name;
539}
540
541static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
542{
543 return tfm->__crt_alg->cra_priority;
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
547{
548 return module_name(tfm->__crt_alg->cra_module);
549}
550
551static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
552{
553 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
557{
558 return tfm->__crt_alg->cra_blocksize;
559}
560
Herbert Xufbdae9f2005-07-06 13:53:29 -0700561static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
562{
563 return tfm->__crt_alg->cra_alignmask;
564}
565
Herbert Xuf28776a2006-08-13 20:58:18 +1000566static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
567{
568 return tfm->crt_flags;
569}
570
571static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
572{
573 tfm->crt_flags |= flags;
574}
575
576static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
577{
578 tfm->crt_flags &= ~flags;
579}
580
Herbert Xu40725182005-07-06 13:51:52 -0700581static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
582{
Herbert Xuf10b7892006-01-25 22:34:01 +1100583 return tfm->__crt_ctx;
584}
585
586static inline unsigned int crypto_tfm_ctx_alignment(void)
587{
588 struct crypto_tfm *tfm;
589 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * API wrappers.
594 */
Herbert Xu32e39832007-03-24 14:35:34 +1100595static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
596 struct crypto_tfm *tfm)
597{
598 return (struct crypto_ablkcipher *)tfm;
599}
600
Herbert Xu378f4f52007-12-17 20:07:31 +0800601static inline u32 crypto_skcipher_type(u32 type)
602{
Herbert Xuecfc4322007-12-05 21:08:36 +1100603 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800604 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
605 return type;
606}
607
608static inline u32 crypto_skcipher_mask(u32 mask)
609{
Herbert Xuecfc4322007-12-05 21:08:36 +1100610 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800611 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
612 return mask;
613}
614
Herbert Xub9c55aa2007-12-04 12:46:48 +1100615struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
616 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100617
618static inline struct crypto_tfm *crypto_ablkcipher_tfm(
619 struct crypto_ablkcipher *tfm)
620{
621 return &tfm->base;
622}
623
624static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
625{
626 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
627}
628
629static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
630 u32 mask)
631{
Herbert Xu378f4f52007-12-17 20:07:31 +0800632 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
633 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100634}
635
636static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
637 struct crypto_ablkcipher *tfm)
638{
639 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
640}
641
642static inline unsigned int crypto_ablkcipher_ivsize(
643 struct crypto_ablkcipher *tfm)
644{
645 return crypto_ablkcipher_crt(tfm)->ivsize;
646}
647
648static inline unsigned int crypto_ablkcipher_blocksize(
649 struct crypto_ablkcipher *tfm)
650{
651 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
652}
653
654static inline unsigned int crypto_ablkcipher_alignmask(
655 struct crypto_ablkcipher *tfm)
656{
657 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
658}
659
660static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
661{
662 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
663}
664
665static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
666 u32 flags)
667{
668 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
669}
670
671static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
672 u32 flags)
673{
674 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
675}
676
677static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
678 const u8 *key, unsigned int keylen)
679{
Herbert Xuecfc4322007-12-05 21:08:36 +1100680 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
681
682 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100683}
684
685static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
686 struct ablkcipher_request *req)
687{
688 return __crypto_ablkcipher_cast(req->base.tfm);
689}
690
691static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
692{
693 struct ablkcipher_tfm *crt =
694 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
695 return crt->encrypt(req);
696}
697
698static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
699{
700 struct ablkcipher_tfm *crt =
701 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
702 return crt->decrypt(req);
703}
704
Herbert Xub16c3a22007-08-29 19:02:04 +0800705static inline unsigned int crypto_ablkcipher_reqsize(
706 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100707{
708 return crypto_ablkcipher_crt(tfm)->reqsize;
709}
710
Herbert Xue196d622007-04-14 16:09:14 +1000711static inline void ablkcipher_request_set_tfm(
712 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
713{
Herbert Xuecfc4322007-12-05 21:08:36 +1100714 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000715}
716
Herbert Xub5b7f082007-04-16 20:48:54 +1000717static inline struct ablkcipher_request *ablkcipher_request_cast(
718 struct crypto_async_request *req)
719{
720 return container_of(req, struct ablkcipher_request, base);
721}
722
Herbert Xu32e39832007-03-24 14:35:34 +1100723static inline struct ablkcipher_request *ablkcipher_request_alloc(
724 struct crypto_ablkcipher *tfm, gfp_t gfp)
725{
726 struct ablkcipher_request *req;
727
728 req = kmalloc(sizeof(struct ablkcipher_request) +
729 crypto_ablkcipher_reqsize(tfm), gfp);
730
731 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000732 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100733
734 return req;
735}
736
737static inline void ablkcipher_request_free(struct ablkcipher_request *req)
738{
739 kfree(req);
740}
741
742static inline void ablkcipher_request_set_callback(
743 struct ablkcipher_request *req,
744 u32 flags, crypto_completion_t complete, void *data)
745{
746 req->base.complete = complete;
747 req->base.data = data;
748 req->base.flags = flags;
749}
750
751static inline void ablkcipher_request_set_crypt(
752 struct ablkcipher_request *req,
753 struct scatterlist *src, struct scatterlist *dst,
754 unsigned int nbytes, void *iv)
755{
756 req->src = src;
757 req->dst = dst;
758 req->nbytes = nbytes;
759 req->info = iv;
760}
761
Herbert Xu1ae97822007-08-30 15:36:14 +0800762static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
763{
764 return (struct crypto_aead *)tfm;
765}
766
Herbert Xud29ce982007-12-12 19:24:27 +0800767struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800768
769static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
770{
771 return &tfm->base;
772}
773
774static inline void crypto_free_aead(struct crypto_aead *tfm)
775{
776 crypto_free_tfm(crypto_aead_tfm(tfm));
777}
778
779static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
780{
781 return &crypto_aead_tfm(tfm)->crt_aead;
782}
783
784static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
785{
786 return crypto_aead_crt(tfm)->ivsize;
787}
788
789static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
790{
791 return crypto_aead_crt(tfm)->authsize;
792}
793
794static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
795{
796 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
797}
798
799static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
800{
801 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
802}
803
804static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
805{
806 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
807}
808
809static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
810{
811 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
812}
813
814static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
815{
816 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
817}
818
819static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
820 unsigned int keylen)
821{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800822 struct aead_tfm *crt = crypto_aead_crt(tfm);
823
824 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800825}
826
Herbert Xu7ba683a2007-12-02 18:49:21 +1100827int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
828
Herbert Xu1ae97822007-08-30 15:36:14 +0800829static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
830{
831 return __crypto_aead_cast(req->base.tfm);
832}
833
834static inline int crypto_aead_encrypt(struct aead_request *req)
835{
836 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
837}
838
839static inline int crypto_aead_decrypt(struct aead_request *req)
840{
841 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
842}
843
Herbert Xub16c3a22007-08-29 19:02:04 +0800844static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800845{
846 return crypto_aead_crt(tfm)->reqsize;
847}
848
849static inline void aead_request_set_tfm(struct aead_request *req,
850 struct crypto_aead *tfm)
851{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800852 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800853}
854
855static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
856 gfp_t gfp)
857{
858 struct aead_request *req;
859
860 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
861
862 if (likely(req))
863 aead_request_set_tfm(req, tfm);
864
865 return req;
866}
867
868static inline void aead_request_free(struct aead_request *req)
869{
870 kfree(req);
871}
872
873static inline void aead_request_set_callback(struct aead_request *req,
874 u32 flags,
875 crypto_completion_t complete,
876 void *data)
877{
878 req->base.complete = complete;
879 req->base.data = data;
880 req->base.flags = flags;
881}
882
883static inline void aead_request_set_crypt(struct aead_request *req,
884 struct scatterlist *src,
885 struct scatterlist *dst,
886 unsigned int cryptlen, u8 *iv)
887{
888 req->src = src;
889 req->dst = dst;
890 req->cryptlen = cryptlen;
891 req->iv = iv;
892}
893
894static inline void aead_request_set_assoc(struct aead_request *req,
895 struct scatterlist *assoc,
896 unsigned int assoclen)
897{
898 req->assoc = assoc;
899 req->assoclen = assoclen;
900}
901
Herbert Xu5cde0af2006-08-22 00:07:53 +1000902static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
903 struct crypto_tfm *tfm)
904{
905 return (struct crypto_blkcipher *)tfm;
906}
907
908static inline struct crypto_blkcipher *crypto_blkcipher_cast(
909 struct crypto_tfm *tfm)
910{
911 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
912 return __crypto_blkcipher_cast(tfm);
913}
914
915static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
916 const char *alg_name, u32 type, u32 mask)
917{
Herbert Xu332f88402007-11-15 22:36:07 +0800918 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000919 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800920 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000921
922 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
923}
924
925static inline struct crypto_tfm *crypto_blkcipher_tfm(
926 struct crypto_blkcipher *tfm)
927{
928 return &tfm->base;
929}
930
931static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
932{
933 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
934}
935
Herbert Xufce32d72006-08-26 17:35:45 +1000936static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
937{
Herbert Xu332f88402007-11-15 22:36:07 +0800938 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000939 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800940 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000941
942 return crypto_has_alg(alg_name, type, mask);
943}
944
Herbert Xu5cde0af2006-08-22 00:07:53 +1000945static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
946{
947 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
948}
949
950static inline struct blkcipher_tfm *crypto_blkcipher_crt(
951 struct crypto_blkcipher *tfm)
952{
953 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
954}
955
956static inline struct blkcipher_alg *crypto_blkcipher_alg(
957 struct crypto_blkcipher *tfm)
958{
959 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
960}
961
962static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
963{
964 return crypto_blkcipher_alg(tfm)->ivsize;
965}
966
967static inline unsigned int crypto_blkcipher_blocksize(
968 struct crypto_blkcipher *tfm)
969{
970 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
971}
972
973static inline unsigned int crypto_blkcipher_alignmask(
974 struct crypto_blkcipher *tfm)
975{
976 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
977}
978
979static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
980{
981 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
982}
983
984static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
985 u32 flags)
986{
987 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
988}
989
990static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
991 u32 flags)
992{
993 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
994}
995
996static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
997 const u8 *key, unsigned int keylen)
998{
999 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1000 key, keylen);
1001}
1002
1003static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1004 struct scatterlist *dst,
1005 struct scatterlist *src,
1006 unsigned int nbytes)
1007{
1008 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1009 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1010}
1011
1012static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1013 struct scatterlist *dst,
1014 struct scatterlist *src,
1015 unsigned int nbytes)
1016{
1017 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1018}
1019
1020static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1021 struct scatterlist *dst,
1022 struct scatterlist *src,
1023 unsigned int nbytes)
1024{
1025 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1026 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1027}
1028
1029static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1030 struct scatterlist *dst,
1031 struct scatterlist *src,
1032 unsigned int nbytes)
1033{
1034 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1035}
1036
1037static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1038 const u8 *src, unsigned int len)
1039{
1040 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1041}
1042
1043static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1044 u8 *dst, unsigned int len)
1045{
1046 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1047}
1048
Herbert Xuf28776a2006-08-13 20:58:18 +10001049static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1050{
1051 return (struct crypto_cipher *)tfm;
1052}
1053
1054static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1055{
1056 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1057 return __crypto_cipher_cast(tfm);
1058}
1059
1060static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1061 u32 type, u32 mask)
1062{
1063 type &= ~CRYPTO_ALG_TYPE_MASK;
1064 type |= CRYPTO_ALG_TYPE_CIPHER;
1065 mask |= CRYPTO_ALG_TYPE_MASK;
1066
1067 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1068}
1069
1070static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1071{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001072 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001073}
1074
1075static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1076{
1077 crypto_free_tfm(crypto_cipher_tfm(tfm));
1078}
1079
Herbert Xufce32d72006-08-26 17:35:45 +10001080static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1081{
1082 type &= ~CRYPTO_ALG_TYPE_MASK;
1083 type |= CRYPTO_ALG_TYPE_CIPHER;
1084 mask |= CRYPTO_ALG_TYPE_MASK;
1085
1086 return crypto_has_alg(alg_name, type, mask);
1087}
1088
Herbert Xuf28776a2006-08-13 20:58:18 +10001089static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1090{
1091 return &crypto_cipher_tfm(tfm)->crt_cipher;
1092}
1093
1094static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1095{
1096 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1097}
1098
1099static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1100{
1101 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1102}
1103
1104static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1105{
1106 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1107}
1108
1109static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1110 u32 flags)
1111{
1112 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1113}
1114
1115static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1116 u32 flags)
1117{
1118 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1119}
1120
Herbert Xu7226bc872006-08-21 21:40:49 +10001121static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1122 const u8 *key, unsigned int keylen)
1123{
1124 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1125 key, keylen);
1126}
1127
Herbert Xuf28776a2006-08-13 20:58:18 +10001128static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1129 u8 *dst, const u8 *src)
1130{
1131 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1132 dst, src);
1133}
1134
1135static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1136 u8 *dst, const u8 *src)
1137{
1138 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1139 dst, src);
1140}
1141
Herbert Xu055bcee2006-08-19 22:24:23 +10001142static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Herbert Xu055bcee2006-08-19 22:24:23 +10001144 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Herbert Xu055bcee2006-08-19 22:24:23 +10001147static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Herbert Xu055bcee2006-08-19 22:24:23 +10001149 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1150 CRYPTO_ALG_TYPE_HASH_MASK);
1151 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Herbert Xu055bcee2006-08-19 22:24:23 +10001154static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1155 u32 type, u32 mask)
1156{
1157 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001158 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001159 type |= CRYPTO_ALG_TYPE_HASH;
1160 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1161
1162 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1163}
1164
1165static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1166{
1167 return &tfm->base;
1168}
1169
1170static inline void crypto_free_hash(struct crypto_hash *tfm)
1171{
1172 crypto_free_tfm(crypto_hash_tfm(tfm));
1173}
1174
Herbert Xufce32d72006-08-26 17:35:45 +10001175static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1176{
1177 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001178 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001179 type |= CRYPTO_ALG_TYPE_HASH;
1180 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1181
1182 return crypto_has_alg(alg_name, type, mask);
1183}
1184
Herbert Xu055bcee2006-08-19 22:24:23 +10001185static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1186{
1187 return &crypto_hash_tfm(tfm)->crt_hash;
1188}
1189
1190static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1191{
1192 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1193}
1194
1195static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1196{
1197 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1198}
1199
1200static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1201{
1202 return crypto_hash_crt(tfm)->digestsize;
1203}
1204
1205static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1206{
1207 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1208}
1209
1210static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1211{
1212 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1213}
1214
1215static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1216{
1217 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1218}
1219
1220static inline int crypto_hash_init(struct hash_desc *desc)
1221{
1222 return crypto_hash_crt(desc->tfm)->init(desc);
1223}
1224
1225static inline int crypto_hash_update(struct hash_desc *desc,
1226 struct scatterlist *sg,
1227 unsigned int nbytes)
1228{
1229 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1230}
1231
1232static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1233{
1234 return crypto_hash_crt(desc->tfm)->final(desc, out);
1235}
1236
1237static inline int crypto_hash_digest(struct hash_desc *desc,
1238 struct scatterlist *sg,
1239 unsigned int nbytes, u8 *out)
1240{
1241 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1242}
1243
1244static inline int crypto_hash_setkey(struct crypto_hash *hash,
1245 const u8 *key, unsigned int keylen)
1246{
1247 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Herbert Xufce32d72006-08-26 17:35:45 +10001250static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1251{
1252 return (struct crypto_comp *)tfm;
1253}
1254
1255static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1256{
1257 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1258 CRYPTO_ALG_TYPE_MASK);
1259 return __crypto_comp_cast(tfm);
1260}
1261
1262static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1263 u32 type, u32 mask)
1264{
1265 type &= ~CRYPTO_ALG_TYPE_MASK;
1266 type |= CRYPTO_ALG_TYPE_COMPRESS;
1267 mask |= CRYPTO_ALG_TYPE_MASK;
1268
1269 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1270}
1271
1272static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1273{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001274 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001275}
1276
1277static inline void crypto_free_comp(struct crypto_comp *tfm)
1278{
1279 crypto_free_tfm(crypto_comp_tfm(tfm));
1280}
1281
1282static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1283{
1284 type &= ~CRYPTO_ALG_TYPE_MASK;
1285 type |= CRYPTO_ALG_TYPE_COMPRESS;
1286 mask |= CRYPTO_ALG_TYPE_MASK;
1287
1288 return crypto_has_alg(alg_name, type, mask);
1289}
1290
Herbert Xue4d5b792006-08-26 18:12:40 +10001291static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1292{
1293 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1294}
1295
Herbert Xufce32d72006-08-26 17:35:45 +10001296static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1297{
1298 return &crypto_comp_tfm(tfm)->crt_compress;
1299}
1300
1301static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 const u8 *src, unsigned int slen,
1303 u8 *dst, unsigned int *dlen)
1304{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001305 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1306 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Herbert Xufce32d72006-08-26 17:35:45 +10001309static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 const u8 *src, unsigned int slen,
1311 u8 *dst, unsigned int *dlen)
1312{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001313 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1314 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#endif /* _LINUX_CRYPTO_H */
1318