blob: 48ce5479386c60fbc954b972636b53cc1dc9d9fb [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
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
28 * Algorithm masks and types.
29 */
Herbert Xu28259822006-08-06 21:23:26 +100030#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080032#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
33#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100034#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080035#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080036#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Loc Ho004a4032008-05-14 20:41:47 +080037#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
Herbert Xu5f7082e2008-08-31 22:21:09 +100038#define CRYPTO_ALG_TYPE_HASH 0x00000008
39#define CRYPTO_ALG_TYPE_SHASH 0x00000009
Loc Ho004a4032008-05-14 20:41:47 +080040#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Neil Horman17f0f4a2008-08-14 22:15:52 +100041#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Geert Uytterhoevena1d2f092009-03-04 15:05:33 +080042#define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100043
44#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080045#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080046#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Herbert Xu28259822006-08-06 21:23:26 +100048#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100049#define CRYPTO_ALG_DEAD 0x00000020
50#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100051#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Herbert Xu60104392006-08-26 18:34:10 +100054 * Set this bit if and only if the algorithm requires another algorithm of
55 * the same type to handle corner cases.
56 */
57#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
58
59/*
Herbert Xuecfc4322007-12-05 21:08:36 +110060 * This bit is set for symmetric key ciphers that have already been wrapped
61 * with a generic IV generator to prevent them from being wrapped again.
62 */
63#define CRYPTO_ALG_GENIV 0x00000200
64
65/*
Herbert Xu73d38642008-08-03 21:15:23 +080066 * Set if the algorithm has passed automated run-time testing. Note that
67 * if there is no run-time testing for a given algorithm it is considered
68 * to have passed.
69 */
70
71#define CRYPTO_ALG_TESTED 0x00000400
72
73/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020074 * Set if the algorithm is an instance that is build from templates.
75 */
76#define CRYPTO_ALG_INSTANCE 0x00000800
77
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010078/* Set this bit if the algorithm provided is hardware accelerated but
79 * not available to userspace via instruction set or so.
80 */
81#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
82
Steffen Klassert64a947b2011-09-27 07:21:26 +020083/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Transform masks and values (for crt_flags).
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define CRYPTO_TFM_REQ_MASK 0x000fff00
87#define CRYPTO_TFM_RES_MASK 0xfff00000
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070090#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110091#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
93#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
94#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
95#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
96#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
97
98/*
99 * Miscellaneous stuff.
100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define CRYPTO_MAX_ALG_NAME 64
102
Herbert Xu79911102006-08-21 21:03:52 +1000103/*
104 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
105 * declaration) is used to ensure that the crypto_tfm context structure is
106 * aligned correctly for the given architecture so that there are no alignment
107 * faults for C data types. In particular, this is required on platforms such
108 * as arm where pointers are 32-bit aligned but there are data types such as
109 * u64 which require 64-bit alignment.
110 */
Herbert Xu79911102006-08-21 21:03:52 +1000111#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000112
Herbert Xu79911102006-08-21 21:03:52 +1000113#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100116struct crypto_ablkcipher;
117struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800118struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000119struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000120struct crypto_hash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000121struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700122struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000123struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800124struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800125struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700126
Herbert Xu32e39832007-03-24 14:35:34 +1100127typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
128
129struct crypto_async_request {
130 struct list_head list;
131 crypto_completion_t complete;
132 void *data;
133 struct crypto_tfm *tfm;
134
135 u32 flags;
136};
137
138struct ablkcipher_request {
139 struct crypto_async_request base;
140
141 unsigned int nbytes;
142
143 void *info;
144
145 struct scatterlist *src;
146 struct scatterlist *dst;
147
148 void *__ctx[] CRYPTO_MINALIGN_ATTR;
149};
150
Herbert Xu1ae97822007-08-30 15:36:14 +0800151/**
152 * struct aead_request - AEAD request
153 * @base: Common attributes for async crypto requests
154 * @assoclen: Length in bytes of associated data for authentication
155 * @cryptlen: Length of data to be encrypted or decrypted
156 * @iv: Initialisation vector
157 * @assoc: Associated data
158 * @src: Source data
159 * @dst: Destination data
160 * @__ctx: Start of private context data
161 */
162struct aead_request {
163 struct crypto_async_request base;
164
165 unsigned int assoclen;
166 unsigned int cryptlen;
167
168 u8 *iv;
169
170 struct scatterlist *assoc;
171 struct scatterlist *src;
172 struct scatterlist *dst;
173
174 void *__ctx[] CRYPTO_MINALIGN_ATTR;
175};
176
Herbert Xu5cde0af2006-08-22 00:07:53 +1000177struct blkcipher_desc {
178 struct crypto_blkcipher *tfm;
179 void *info;
180 u32 flags;
181};
182
Herbert Xu40725182005-07-06 13:51:52 -0700183struct cipher_desc {
184 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000185 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700186 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
187 const u8 *src, unsigned int nbytes);
188 void *info;
189};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Herbert Xu055bcee2006-08-19 22:24:23 +1000191struct hash_desc {
192 struct crypto_hash *tfm;
193 u32 flags;
194};
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
197 * Algorithms: modular crypto algorithm implementations, managed
198 * via crypto_register_alg() and crypto_unregister_alg().
199 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000200struct ablkcipher_alg {
201 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
202 unsigned int keylen);
203 int (*encrypt)(struct ablkcipher_request *req);
204 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800205 int (*givencrypt)(struct skcipher_givcrypt_request *req);
206 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000207
Herbert Xu23508e12007-11-27 21:33:24 +0800208 const char *geniv;
209
Herbert Xub5b7f082007-04-16 20:48:54 +1000210 unsigned int min_keysize;
211 unsigned int max_keysize;
212 unsigned int ivsize;
213};
214
Herbert Xu1ae97822007-08-30 15:36:14 +0800215struct aead_alg {
216 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
217 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100218 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800219 int (*encrypt)(struct aead_request *req);
220 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800221 int (*givencrypt)(struct aead_givcrypt_request *req);
222 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800223
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800224 const char *geniv;
225
Herbert Xu1ae97822007-08-30 15:36:14 +0800226 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100227 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800228};
229
Herbert Xu5cde0af2006-08-22 00:07:53 +1000230struct blkcipher_alg {
231 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
232 unsigned int keylen);
233 int (*encrypt)(struct blkcipher_desc *desc,
234 struct scatterlist *dst, struct scatterlist *src,
235 unsigned int nbytes);
236 int (*decrypt)(struct blkcipher_desc *desc,
237 struct scatterlist *dst, struct scatterlist *src,
238 unsigned int nbytes);
239
Herbert Xu23508e12007-11-27 21:33:24 +0800240 const char *geniv;
241
Herbert Xu5cde0af2006-08-22 00:07:53 +1000242 unsigned int min_keysize;
243 unsigned int max_keysize;
244 unsigned int ivsize;
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247struct cipher_alg {
248 unsigned int cia_min_keysize;
249 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000250 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000251 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000252 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
253 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000257 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
258 unsigned int slen, u8 *dst, unsigned int *dlen);
259 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
260 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Neil Horman17f0f4a2008-08-14 22:15:52 +1000263struct rng_alg {
264 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
265 unsigned int dlen);
266 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
267
268 unsigned int seedsize;
269};
270
271
Herbert Xub5b7f082007-04-16 20:48:54 +1000272#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800273#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000274#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000277#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279struct crypto_alg {
280 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000281 struct list_head cra_users;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u32 cra_flags;
284 unsigned int cra_blocksize;
285 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700286 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100287
288 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000289 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100290
Herbert Xud913ea02006-05-21 08:45:26 +1000291 char cra_name[CRYPTO_MAX_ALG_NAME];
292 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Herbert Xue853c3c2006-08-22 00:06:54 +1000294 const struct crypto_type *cra_type;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000297 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800298 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000299 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000302 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000304
305 int (*cra_init)(struct crypto_tfm *tfm);
306 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000307 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 struct module *cra_module;
310};
311
312/*
313 * Algorithm registration interface.
314 */
315int crypto_register_alg(struct crypto_alg *alg);
316int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000317int crypto_register_algs(struct crypto_alg *algs, int count);
318int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320/*
321 * Algorithm query interface.
322 */
Herbert Xufce32d72006-08-26 17:35:45 +1000323int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/*
326 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000327 * and core processing logic. Managed via crypto_alloc_*() and
328 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Herbert Xu32e39832007-03-24 14:35:34 +1100331struct ablkcipher_tfm {
332 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
333 unsigned int keylen);
334 int (*encrypt)(struct ablkcipher_request *req);
335 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800336 int (*givencrypt)(struct skcipher_givcrypt_request *req);
337 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
338
Herbert Xuecfc4322007-12-05 21:08:36 +1100339 struct crypto_ablkcipher *base;
340
Herbert Xu32e39832007-03-24 14:35:34 +1100341 unsigned int ivsize;
342 unsigned int reqsize;
343};
344
Herbert Xu1ae97822007-08-30 15:36:14 +0800345struct aead_tfm {
346 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
347 unsigned int keylen);
348 int (*encrypt)(struct aead_request *req);
349 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800350 int (*givencrypt)(struct aead_givcrypt_request *req);
351 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800352
353 struct crypto_aead *base;
354
Herbert Xu1ae97822007-08-30 15:36:14 +0800355 unsigned int ivsize;
356 unsigned int authsize;
357 unsigned int reqsize;
358};
359
Herbert Xu5cde0af2006-08-22 00:07:53 +1000360struct blkcipher_tfm {
361 void *iv;
362 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
363 unsigned int keylen);
364 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
365 struct scatterlist *src, unsigned int nbytes);
366 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
367 struct scatterlist *src, unsigned int nbytes);
368};
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int (*cit_setkey)(struct crypto_tfm *tfm,
372 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000373 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
374 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
Herbert Xu055bcee2006-08-19 22:24:23 +1000377struct hash_tfm {
378 int (*init)(struct hash_desc *desc);
379 int (*update)(struct hash_desc *desc,
380 struct scatterlist *sg, unsigned int nsg);
381 int (*final)(struct hash_desc *desc, u8 *out);
382 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
383 unsigned int nsg, u8 *out);
384 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
385 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000386 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388
389struct compress_tfm {
390 int (*cot_compress)(struct crypto_tfm *tfm,
391 const u8 *src, unsigned int slen,
392 u8 *dst, unsigned int *dlen);
393 int (*cot_decompress)(struct crypto_tfm *tfm,
394 const u8 *src, unsigned int slen,
395 u8 *dst, unsigned int *dlen);
396};
397
Neil Horman17f0f4a2008-08-14 22:15:52 +1000398struct rng_tfm {
399 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
400 unsigned int dlen);
401 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
402};
403
Herbert Xu32e39832007-03-24 14:35:34 +1100404#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800405#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000406#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000408#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000410#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412struct crypto_tfm {
413
414 u32 crt_flags;
415
416 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100417 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800418 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000419 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000421 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000423 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700425
426 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100429
Herbert Xu79911102006-08-21 21:03:52 +1000430 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
Herbert Xu32e39832007-03-24 14:35:34 +1100433struct crypto_ablkcipher {
434 struct crypto_tfm base;
435};
436
Herbert Xu1ae97822007-08-30 15:36:14 +0800437struct crypto_aead {
438 struct crypto_tfm base;
439};
440
Herbert Xu5cde0af2006-08-22 00:07:53 +1000441struct crypto_blkcipher {
442 struct crypto_tfm base;
443};
444
Herbert Xu78a1fe42006-12-24 10:02:00 +1100445struct crypto_cipher {
446 struct crypto_tfm base;
447};
448
449struct crypto_comp {
450 struct crypto_tfm base;
451};
452
Herbert Xu055bcee2006-08-19 22:24:23 +1000453struct crypto_hash {
454 struct crypto_tfm base;
455};
456
Neil Horman17f0f4a2008-08-14 22:15:52 +1000457struct crypto_rng {
458 struct crypto_tfm base;
459};
460
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000461enum {
462 CRYPTOA_UNSPEC,
463 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100464 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800465 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100466 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000467};
468
Herbert Xuebc610e2007-01-01 18:37:02 +1100469#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
470
Herbert Xu39e1ee012007-08-29 19:27:26 +0800471/* Maximum number of (rtattr) parameters for each template. */
472#define CRYPTO_MAX_ATTRS 32
473
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000474struct crypto_attr_alg {
475 char name[CRYPTO_MAX_ALG_NAME];
476};
477
Herbert Xuebc610e2007-01-01 18:37:02 +1100478struct crypto_attr_type {
479 u32 type;
480 u32 mask;
481};
482
Herbert Xu39e1ee012007-08-29 19:27:26 +0800483struct crypto_attr_u32 {
484 u32 num;
485};
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * Transform user interface.
489 */
490
Herbert Xu6d7d6842006-07-30 11:53:01 +1000491struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100492void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
493
494static inline void crypto_free_tfm(struct crypto_tfm *tfm)
495{
496 return crypto_destroy_tfm(tfm, tfm);
497}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Herbert Xuda7f0332008-07-31 17:08:25 +0800499int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * Transform helpers which query the underlying algorithm.
503 */
504static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
505{
506 return tfm->__crt_alg->cra_name;
507}
508
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000509static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
510{
511 return tfm->__crt_alg->cra_driver_name;
512}
513
514static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
515{
516 return tfm->__crt_alg->cra_priority;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
520{
521 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
525{
526 return tfm->__crt_alg->cra_blocksize;
527}
528
Herbert Xufbdae9f2005-07-06 13:53:29 -0700529static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
530{
531 return tfm->__crt_alg->cra_alignmask;
532}
533
Herbert Xuf28776a2006-08-13 20:58:18 +1000534static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
535{
536 return tfm->crt_flags;
537}
538
539static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
540{
541 tfm->crt_flags |= flags;
542}
543
544static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
545{
546 tfm->crt_flags &= ~flags;
547}
548
Herbert Xu40725182005-07-06 13:51:52 -0700549static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
550{
Herbert Xuf10b7892006-01-25 22:34:01 +1100551 return tfm->__crt_ctx;
552}
553
554static inline unsigned int crypto_tfm_ctx_alignment(void)
555{
556 struct crypto_tfm *tfm;
557 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/*
561 * API wrappers.
562 */
Herbert Xu32e39832007-03-24 14:35:34 +1100563static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
564 struct crypto_tfm *tfm)
565{
566 return (struct crypto_ablkcipher *)tfm;
567}
568
Herbert Xu378f4f52007-12-17 20:07:31 +0800569static inline u32 crypto_skcipher_type(u32 type)
570{
Herbert Xuecfc4322007-12-05 21:08:36 +1100571 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800572 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
573 return type;
574}
575
576static inline u32 crypto_skcipher_mask(u32 mask)
577{
Herbert Xuecfc4322007-12-05 21:08:36 +1100578 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800579 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
580 return mask;
581}
582
Herbert Xub9c55aa2007-12-04 12:46:48 +1100583struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
584 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100585
586static inline struct crypto_tfm *crypto_ablkcipher_tfm(
587 struct crypto_ablkcipher *tfm)
588{
589 return &tfm->base;
590}
591
592static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
593{
594 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
595}
596
597static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
598 u32 mask)
599{
Herbert Xu378f4f52007-12-17 20:07:31 +0800600 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
601 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100602}
603
604static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
605 struct crypto_ablkcipher *tfm)
606{
607 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
608}
609
610static inline unsigned int crypto_ablkcipher_ivsize(
611 struct crypto_ablkcipher *tfm)
612{
613 return crypto_ablkcipher_crt(tfm)->ivsize;
614}
615
616static inline unsigned int crypto_ablkcipher_blocksize(
617 struct crypto_ablkcipher *tfm)
618{
619 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
620}
621
622static inline unsigned int crypto_ablkcipher_alignmask(
623 struct crypto_ablkcipher *tfm)
624{
625 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
626}
627
628static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
629{
630 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
631}
632
633static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
634 u32 flags)
635{
636 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
637}
638
639static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
640 u32 flags)
641{
642 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
643}
644
645static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
646 const u8 *key, unsigned int keylen)
647{
Herbert Xuecfc4322007-12-05 21:08:36 +1100648 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
649
650 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100651}
652
653static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
654 struct ablkcipher_request *req)
655{
656 return __crypto_ablkcipher_cast(req->base.tfm);
657}
658
659static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
660{
661 struct ablkcipher_tfm *crt =
662 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
663 return crt->encrypt(req);
664}
665
666static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
667{
668 struct ablkcipher_tfm *crt =
669 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
670 return crt->decrypt(req);
671}
672
Herbert Xub16c3a22007-08-29 19:02:04 +0800673static inline unsigned int crypto_ablkcipher_reqsize(
674 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100675{
676 return crypto_ablkcipher_crt(tfm)->reqsize;
677}
678
Herbert Xue196d622007-04-14 16:09:14 +1000679static inline void ablkcipher_request_set_tfm(
680 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
681{
Herbert Xuecfc4322007-12-05 21:08:36 +1100682 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000683}
684
Herbert Xub5b7f082007-04-16 20:48:54 +1000685static inline struct ablkcipher_request *ablkcipher_request_cast(
686 struct crypto_async_request *req)
687{
688 return container_of(req, struct ablkcipher_request, base);
689}
690
Herbert Xu32e39832007-03-24 14:35:34 +1100691static inline struct ablkcipher_request *ablkcipher_request_alloc(
692 struct crypto_ablkcipher *tfm, gfp_t gfp)
693{
694 struct ablkcipher_request *req;
695
696 req = kmalloc(sizeof(struct ablkcipher_request) +
697 crypto_ablkcipher_reqsize(tfm), gfp);
698
699 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000700 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100701
702 return req;
703}
704
705static inline void ablkcipher_request_free(struct ablkcipher_request *req)
706{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800707 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100708}
709
710static inline void ablkcipher_request_set_callback(
711 struct ablkcipher_request *req,
712 u32 flags, crypto_completion_t complete, void *data)
713{
714 req->base.complete = complete;
715 req->base.data = data;
716 req->base.flags = flags;
717}
718
719static inline void ablkcipher_request_set_crypt(
720 struct ablkcipher_request *req,
721 struct scatterlist *src, struct scatterlist *dst,
722 unsigned int nbytes, void *iv)
723{
724 req->src = src;
725 req->dst = dst;
726 req->nbytes = nbytes;
727 req->info = iv;
728}
729
Herbert Xu1ae97822007-08-30 15:36:14 +0800730static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
731{
732 return (struct crypto_aead *)tfm;
733}
734
Herbert Xud29ce982007-12-12 19:24:27 +0800735struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800736
737static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
738{
739 return &tfm->base;
740}
741
742static inline void crypto_free_aead(struct crypto_aead *tfm)
743{
744 crypto_free_tfm(crypto_aead_tfm(tfm));
745}
746
747static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
748{
749 return &crypto_aead_tfm(tfm)->crt_aead;
750}
751
752static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
753{
754 return crypto_aead_crt(tfm)->ivsize;
755}
756
757static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
758{
759 return crypto_aead_crt(tfm)->authsize;
760}
761
762static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
763{
764 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
765}
766
767static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
768{
769 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
770}
771
772static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
773{
774 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
775}
776
777static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
778{
779 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
780}
781
782static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
783{
784 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
785}
786
787static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
788 unsigned int keylen)
789{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800790 struct aead_tfm *crt = crypto_aead_crt(tfm);
791
792 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800793}
794
Herbert Xu7ba683a2007-12-02 18:49:21 +1100795int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
796
Herbert Xu1ae97822007-08-30 15:36:14 +0800797static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
798{
799 return __crypto_aead_cast(req->base.tfm);
800}
801
802static inline int crypto_aead_encrypt(struct aead_request *req)
803{
804 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
805}
806
807static inline int crypto_aead_decrypt(struct aead_request *req)
808{
809 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
810}
811
Herbert Xub16c3a22007-08-29 19:02:04 +0800812static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800813{
814 return crypto_aead_crt(tfm)->reqsize;
815}
816
817static inline void aead_request_set_tfm(struct aead_request *req,
818 struct crypto_aead *tfm)
819{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800820 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800821}
822
823static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
824 gfp_t gfp)
825{
826 struct aead_request *req;
827
828 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
829
830 if (likely(req))
831 aead_request_set_tfm(req, tfm);
832
833 return req;
834}
835
836static inline void aead_request_free(struct aead_request *req)
837{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800838 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800839}
840
841static inline void aead_request_set_callback(struct aead_request *req,
842 u32 flags,
843 crypto_completion_t complete,
844 void *data)
845{
846 req->base.complete = complete;
847 req->base.data = data;
848 req->base.flags = flags;
849}
850
851static inline void aead_request_set_crypt(struct aead_request *req,
852 struct scatterlist *src,
853 struct scatterlist *dst,
854 unsigned int cryptlen, u8 *iv)
855{
856 req->src = src;
857 req->dst = dst;
858 req->cryptlen = cryptlen;
859 req->iv = iv;
860}
861
862static inline void aead_request_set_assoc(struct aead_request *req,
863 struct scatterlist *assoc,
864 unsigned int assoclen)
865{
866 req->assoc = assoc;
867 req->assoclen = assoclen;
868}
869
Herbert Xu5cde0af2006-08-22 00:07:53 +1000870static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
871 struct crypto_tfm *tfm)
872{
873 return (struct crypto_blkcipher *)tfm;
874}
875
876static inline struct crypto_blkcipher *crypto_blkcipher_cast(
877 struct crypto_tfm *tfm)
878{
879 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
880 return __crypto_blkcipher_cast(tfm);
881}
882
883static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
884 const char *alg_name, u32 type, u32 mask)
885{
Herbert Xu332f88402007-11-15 22:36:07 +0800886 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000887 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800888 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000889
890 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
891}
892
893static inline struct crypto_tfm *crypto_blkcipher_tfm(
894 struct crypto_blkcipher *tfm)
895{
896 return &tfm->base;
897}
898
899static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
900{
901 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
902}
903
Herbert Xufce32d72006-08-26 17:35:45 +1000904static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
905{
Herbert Xu332f88402007-11-15 22:36:07 +0800906 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000907 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800908 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000909
910 return crypto_has_alg(alg_name, type, mask);
911}
912
Herbert Xu5cde0af2006-08-22 00:07:53 +1000913static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
914{
915 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
916}
917
918static inline struct blkcipher_tfm *crypto_blkcipher_crt(
919 struct crypto_blkcipher *tfm)
920{
921 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
922}
923
924static inline struct blkcipher_alg *crypto_blkcipher_alg(
925 struct crypto_blkcipher *tfm)
926{
927 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
928}
929
930static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
931{
932 return crypto_blkcipher_alg(tfm)->ivsize;
933}
934
935static inline unsigned int crypto_blkcipher_blocksize(
936 struct crypto_blkcipher *tfm)
937{
938 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
939}
940
941static inline unsigned int crypto_blkcipher_alignmask(
942 struct crypto_blkcipher *tfm)
943{
944 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
945}
946
947static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
948{
949 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
950}
951
952static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
953 u32 flags)
954{
955 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
956}
957
958static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
959 u32 flags)
960{
961 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
962}
963
964static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
965 const u8 *key, unsigned int keylen)
966{
967 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
968 key, keylen);
969}
970
971static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
972 struct scatterlist *dst,
973 struct scatterlist *src,
974 unsigned int nbytes)
975{
976 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
977 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
978}
979
980static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
981 struct scatterlist *dst,
982 struct scatterlist *src,
983 unsigned int nbytes)
984{
985 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
986}
987
988static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
989 struct scatterlist *dst,
990 struct scatterlist *src,
991 unsigned int nbytes)
992{
993 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
994 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
995}
996
997static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
998 struct scatterlist *dst,
999 struct scatterlist *src,
1000 unsigned int nbytes)
1001{
1002 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1003}
1004
1005static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1006 const u8 *src, unsigned int len)
1007{
1008 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1009}
1010
1011static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1012 u8 *dst, unsigned int len)
1013{
1014 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1015}
1016
Herbert Xuf28776a2006-08-13 20:58:18 +10001017static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1018{
1019 return (struct crypto_cipher *)tfm;
1020}
1021
1022static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1023{
1024 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1025 return __crypto_cipher_cast(tfm);
1026}
1027
1028static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1029 u32 type, u32 mask)
1030{
1031 type &= ~CRYPTO_ALG_TYPE_MASK;
1032 type |= CRYPTO_ALG_TYPE_CIPHER;
1033 mask |= CRYPTO_ALG_TYPE_MASK;
1034
1035 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1036}
1037
1038static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1039{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001040 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001041}
1042
1043static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1044{
1045 crypto_free_tfm(crypto_cipher_tfm(tfm));
1046}
1047
Herbert Xufce32d72006-08-26 17:35:45 +10001048static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1049{
1050 type &= ~CRYPTO_ALG_TYPE_MASK;
1051 type |= CRYPTO_ALG_TYPE_CIPHER;
1052 mask |= CRYPTO_ALG_TYPE_MASK;
1053
1054 return crypto_has_alg(alg_name, type, mask);
1055}
1056
Herbert Xuf28776a2006-08-13 20:58:18 +10001057static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1058{
1059 return &crypto_cipher_tfm(tfm)->crt_cipher;
1060}
1061
1062static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1063{
1064 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1065}
1066
1067static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1068{
1069 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1070}
1071
1072static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1073{
1074 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1075}
1076
1077static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1078 u32 flags)
1079{
1080 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1081}
1082
1083static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1084 u32 flags)
1085{
1086 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1087}
1088
Herbert Xu7226bc872006-08-21 21:40:49 +10001089static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1090 const u8 *key, unsigned int keylen)
1091{
1092 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1093 key, keylen);
1094}
1095
Herbert Xuf28776a2006-08-13 20:58:18 +10001096static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1097 u8 *dst, const u8 *src)
1098{
1099 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1100 dst, src);
1101}
1102
1103static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1104 u8 *dst, const u8 *src)
1105{
1106 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1107 dst, src);
1108}
1109
Herbert Xu055bcee2006-08-19 22:24:23 +10001110static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Herbert Xu055bcee2006-08-19 22:24:23 +10001112 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Herbert Xu055bcee2006-08-19 22:24:23 +10001115static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Herbert Xu055bcee2006-08-19 22:24:23 +10001117 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1118 CRYPTO_ALG_TYPE_HASH_MASK);
1119 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Herbert Xu055bcee2006-08-19 22:24:23 +10001122static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1123 u32 type, u32 mask)
1124{
1125 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001126 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001127 type |= CRYPTO_ALG_TYPE_HASH;
1128 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1129
1130 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1131}
1132
1133static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1134{
1135 return &tfm->base;
1136}
1137
1138static inline void crypto_free_hash(struct crypto_hash *tfm)
1139{
1140 crypto_free_tfm(crypto_hash_tfm(tfm));
1141}
1142
Herbert Xufce32d72006-08-26 17:35:45 +10001143static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1144{
1145 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001146 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001147 type |= CRYPTO_ALG_TYPE_HASH;
1148 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1149
1150 return crypto_has_alg(alg_name, type, mask);
1151}
1152
Herbert Xu055bcee2006-08-19 22:24:23 +10001153static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1154{
1155 return &crypto_hash_tfm(tfm)->crt_hash;
1156}
1157
1158static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1159{
1160 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1161}
1162
1163static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1164{
1165 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1166}
1167
1168static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1169{
1170 return crypto_hash_crt(tfm)->digestsize;
1171}
1172
1173static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1174{
1175 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1176}
1177
1178static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1179{
1180 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1181}
1182
1183static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1184{
1185 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1186}
1187
1188static inline int crypto_hash_init(struct hash_desc *desc)
1189{
1190 return crypto_hash_crt(desc->tfm)->init(desc);
1191}
1192
1193static inline int crypto_hash_update(struct hash_desc *desc,
1194 struct scatterlist *sg,
1195 unsigned int nbytes)
1196{
1197 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1198}
1199
1200static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1201{
1202 return crypto_hash_crt(desc->tfm)->final(desc, out);
1203}
1204
1205static inline int crypto_hash_digest(struct hash_desc *desc,
1206 struct scatterlist *sg,
1207 unsigned int nbytes, u8 *out)
1208{
1209 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1210}
1211
1212static inline int crypto_hash_setkey(struct crypto_hash *hash,
1213 const u8 *key, unsigned int keylen)
1214{
1215 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Herbert Xufce32d72006-08-26 17:35:45 +10001218static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1219{
1220 return (struct crypto_comp *)tfm;
1221}
1222
1223static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1224{
1225 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1226 CRYPTO_ALG_TYPE_MASK);
1227 return __crypto_comp_cast(tfm);
1228}
1229
1230static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1231 u32 type, u32 mask)
1232{
1233 type &= ~CRYPTO_ALG_TYPE_MASK;
1234 type |= CRYPTO_ALG_TYPE_COMPRESS;
1235 mask |= CRYPTO_ALG_TYPE_MASK;
1236
1237 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1238}
1239
1240static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1241{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001242 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001243}
1244
1245static inline void crypto_free_comp(struct crypto_comp *tfm)
1246{
1247 crypto_free_tfm(crypto_comp_tfm(tfm));
1248}
1249
1250static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1251{
1252 type &= ~CRYPTO_ALG_TYPE_MASK;
1253 type |= CRYPTO_ALG_TYPE_COMPRESS;
1254 mask |= CRYPTO_ALG_TYPE_MASK;
1255
1256 return crypto_has_alg(alg_name, type, mask);
1257}
1258
Herbert Xue4d5b792006-08-26 18:12:40 +10001259static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1260{
1261 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1262}
1263
Herbert Xufce32d72006-08-26 17:35:45 +10001264static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1265{
1266 return &crypto_comp_tfm(tfm)->crt_compress;
1267}
1268
1269static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 const u8 *src, unsigned int slen,
1271 u8 *dst, unsigned int *dlen)
1272{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001273 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1274 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Herbert Xufce32d72006-08-26 17:35:45 +10001277static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 const u8 *src, unsigned int slen,
1279 u8 *dst, unsigned int *dlen)
1280{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001281 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1282 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#endif /* _LINUX_CRYPTO_H */
1286