blob: 8a94217b298ed0ec3bdbcccc63fc2ac64b5d8c28 [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
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Transform masks and values (for crt_flags).
80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define CRYPTO_TFM_REQ_MASK 0x000fff00
82#define CRYPTO_TFM_RES_MASK 0xfff00000
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070085#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110086#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
88#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
89#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
90#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
91#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
92
93/*
94 * Miscellaneous stuff.
95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define CRYPTO_MAX_ALG_NAME 64
97
Herbert Xu79911102006-08-21 21:03:52 +100098/*
99 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
100 * declaration) is used to ensure that the crypto_tfm context structure is
101 * aligned correctly for the given architecture so that there are no alignment
102 * faults for C data types. In particular, this is required on platforms such
103 * as arm where pointers are 32-bit aligned but there are data types such as
104 * u64 which require 64-bit alignment.
105 */
Herbert Xu79911102006-08-21 21:03:52 +1000106#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000107
Herbert Xu79911102006-08-21 21:03:52 +1000108#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100111struct crypto_ablkcipher;
112struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800113struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000114struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000115struct crypto_hash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000116struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700117struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000118struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800119struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800120struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700121
Herbert Xu32e39832007-03-24 14:35:34 +1100122typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
123
124struct crypto_async_request {
125 struct list_head list;
126 crypto_completion_t complete;
127 void *data;
128 struct crypto_tfm *tfm;
129
130 u32 flags;
131};
132
133struct ablkcipher_request {
134 struct crypto_async_request base;
135
136 unsigned int nbytes;
137
138 void *info;
139
140 struct scatterlist *src;
141 struct scatterlist *dst;
142
143 void *__ctx[] CRYPTO_MINALIGN_ATTR;
144};
145
Herbert Xu1ae97822007-08-30 15:36:14 +0800146/**
147 * struct aead_request - AEAD request
148 * @base: Common attributes for async crypto requests
149 * @assoclen: Length in bytes of associated data for authentication
150 * @cryptlen: Length of data to be encrypted or decrypted
151 * @iv: Initialisation vector
152 * @assoc: Associated data
153 * @src: Source data
154 * @dst: Destination data
155 * @__ctx: Start of private context data
156 */
157struct aead_request {
158 struct crypto_async_request base;
159
160 unsigned int assoclen;
161 unsigned int cryptlen;
162
163 u8 *iv;
164
165 struct scatterlist *assoc;
166 struct scatterlist *src;
167 struct scatterlist *dst;
168
169 void *__ctx[] CRYPTO_MINALIGN_ATTR;
170};
171
Herbert Xu5cde0af2006-08-22 00:07:53 +1000172struct blkcipher_desc {
173 struct crypto_blkcipher *tfm;
174 void *info;
175 u32 flags;
176};
177
Herbert Xu40725182005-07-06 13:51:52 -0700178struct cipher_desc {
179 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000180 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700181 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
182 const u8 *src, unsigned int nbytes);
183 void *info;
184};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Herbert Xu055bcee2006-08-19 22:24:23 +1000186struct hash_desc {
187 struct crypto_hash *tfm;
188 u32 flags;
189};
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Algorithms: modular crypto algorithm implementations, managed
193 * via crypto_register_alg() and crypto_unregister_alg().
194 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000195struct ablkcipher_alg {
196 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
197 unsigned int keylen);
198 int (*encrypt)(struct ablkcipher_request *req);
199 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800200 int (*givencrypt)(struct skcipher_givcrypt_request *req);
201 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000202
Herbert Xu23508e12007-11-27 21:33:24 +0800203 const char *geniv;
204
Herbert Xub5b7f082007-04-16 20:48:54 +1000205 unsigned int min_keysize;
206 unsigned int max_keysize;
207 unsigned int ivsize;
208};
209
Herbert Xu1ae97822007-08-30 15:36:14 +0800210struct aead_alg {
211 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
212 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100213 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800214 int (*encrypt)(struct aead_request *req);
215 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800216 int (*givencrypt)(struct aead_givcrypt_request *req);
217 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800218
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800219 const char *geniv;
220
Herbert Xu1ae97822007-08-30 15:36:14 +0800221 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100222 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800223};
224
Herbert Xu5cde0af2006-08-22 00:07:53 +1000225struct blkcipher_alg {
226 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
227 unsigned int keylen);
228 int (*encrypt)(struct blkcipher_desc *desc,
229 struct scatterlist *dst, struct scatterlist *src,
230 unsigned int nbytes);
231 int (*decrypt)(struct blkcipher_desc *desc,
232 struct scatterlist *dst, struct scatterlist *src,
233 unsigned int nbytes);
234
Herbert Xu23508e12007-11-27 21:33:24 +0800235 const char *geniv;
236
Herbert Xu5cde0af2006-08-22 00:07:53 +1000237 unsigned int min_keysize;
238 unsigned int max_keysize;
239 unsigned int ivsize;
240};
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242struct cipher_alg {
243 unsigned int cia_min_keysize;
244 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000245 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000246 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000247 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
248 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000252 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
253 unsigned int slen, u8 *dst, unsigned int *dlen);
254 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
255 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Neil Horman17f0f4a2008-08-14 22:15:52 +1000258struct rng_alg {
259 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
260 unsigned int dlen);
261 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
262
263 unsigned int seedsize;
264};
265
266
Herbert Xub5b7f082007-04-16 20:48:54 +1000267#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800268#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000269#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000272#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274struct crypto_alg {
275 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000276 struct list_head cra_users;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 u32 cra_flags;
279 unsigned int cra_blocksize;
280 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700281 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100282
283 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000284 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100285
Herbert Xud913ea02006-05-21 08:45:26 +1000286 char cra_name[CRYPTO_MAX_ALG_NAME];
287 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Herbert Xue853c3c2006-08-22 00:06:54 +1000289 const struct crypto_type *cra_type;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000292 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800293 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000294 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000297 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000299
300 int (*cra_init)(struct crypto_tfm *tfm);
301 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000302 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 struct module *cra_module;
305};
306
307/*
308 * Algorithm registration interface.
309 */
310int crypto_register_alg(struct crypto_alg *alg);
311int crypto_unregister_alg(struct crypto_alg *alg);
312
313/*
314 * Algorithm query interface.
315 */
Herbert Xufce32d72006-08-26 17:35:45 +1000316int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/*
319 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000320 * and core processing logic. Managed via crypto_alloc_*() and
321 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Herbert Xu32e39832007-03-24 14:35:34 +1100324struct ablkcipher_tfm {
325 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
326 unsigned int keylen);
327 int (*encrypt)(struct ablkcipher_request *req);
328 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800329 int (*givencrypt)(struct skcipher_givcrypt_request *req);
330 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
331
Herbert Xuecfc4322007-12-05 21:08:36 +1100332 struct crypto_ablkcipher *base;
333
Herbert Xu32e39832007-03-24 14:35:34 +1100334 unsigned int ivsize;
335 unsigned int reqsize;
336};
337
Herbert Xu1ae97822007-08-30 15:36:14 +0800338struct aead_tfm {
339 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
340 unsigned int keylen);
341 int (*encrypt)(struct aead_request *req);
342 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800343 int (*givencrypt)(struct aead_givcrypt_request *req);
344 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800345
346 struct crypto_aead *base;
347
Herbert Xu1ae97822007-08-30 15:36:14 +0800348 unsigned int ivsize;
349 unsigned int authsize;
350 unsigned int reqsize;
351};
352
Herbert Xu5cde0af2006-08-22 00:07:53 +1000353struct blkcipher_tfm {
354 void *iv;
355 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
356 unsigned int keylen);
357 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
358 struct scatterlist *src, unsigned int nbytes);
359 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
360 struct scatterlist *src, unsigned int nbytes);
361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int (*cit_setkey)(struct crypto_tfm *tfm,
365 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000366 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
367 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
Herbert Xu055bcee2006-08-19 22:24:23 +1000370struct hash_tfm {
371 int (*init)(struct hash_desc *desc);
372 int (*update)(struct hash_desc *desc,
373 struct scatterlist *sg, unsigned int nsg);
374 int (*final)(struct hash_desc *desc, u8 *out);
375 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
376 unsigned int nsg, u8 *out);
377 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
378 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000379 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
382struct compress_tfm {
383 int (*cot_compress)(struct crypto_tfm *tfm,
384 const u8 *src, unsigned int slen,
385 u8 *dst, unsigned int *dlen);
386 int (*cot_decompress)(struct crypto_tfm *tfm,
387 const u8 *src, unsigned int slen,
388 u8 *dst, unsigned int *dlen);
389};
390
Neil Horman17f0f4a2008-08-14 22:15:52 +1000391struct rng_tfm {
392 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
393 unsigned int dlen);
394 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
395};
396
Herbert Xu32e39832007-03-24 14:35:34 +1100397#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800398#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000399#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000401#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000403#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405struct crypto_tfm {
406
407 u32 crt_flags;
408
409 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100410 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800411 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000412 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000414 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000416 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700418
419 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100422
Herbert Xu79911102006-08-21 21:03:52 +1000423 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424};
425
Herbert Xu32e39832007-03-24 14:35:34 +1100426struct crypto_ablkcipher {
427 struct crypto_tfm base;
428};
429
Herbert Xu1ae97822007-08-30 15:36:14 +0800430struct crypto_aead {
431 struct crypto_tfm base;
432};
433
Herbert Xu5cde0af2006-08-22 00:07:53 +1000434struct crypto_blkcipher {
435 struct crypto_tfm base;
436};
437
Herbert Xu78a1fe42006-12-24 10:02:00 +1100438struct crypto_cipher {
439 struct crypto_tfm base;
440};
441
442struct crypto_comp {
443 struct crypto_tfm base;
444};
445
Herbert Xu055bcee2006-08-19 22:24:23 +1000446struct crypto_hash {
447 struct crypto_tfm base;
448};
449
Neil Horman17f0f4a2008-08-14 22:15:52 +1000450struct crypto_rng {
451 struct crypto_tfm base;
452};
453
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000454enum {
455 CRYPTOA_UNSPEC,
456 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100457 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800458 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100459 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000460};
461
Herbert Xuebc610e2007-01-01 18:37:02 +1100462#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
463
Herbert Xu39e1ee012007-08-29 19:27:26 +0800464/* Maximum number of (rtattr) parameters for each template. */
465#define CRYPTO_MAX_ATTRS 32
466
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000467struct crypto_attr_alg {
468 char name[CRYPTO_MAX_ALG_NAME];
469};
470
Herbert Xuebc610e2007-01-01 18:37:02 +1100471struct crypto_attr_type {
472 u32 type;
473 u32 mask;
474};
475
Herbert Xu39e1ee012007-08-29 19:27:26 +0800476struct crypto_attr_u32 {
477 u32 num;
478};
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * Transform user interface.
482 */
483
Herbert Xu6d7d6842006-07-30 11:53:01 +1000484struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100485void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
486
487static inline void crypto_free_tfm(struct crypto_tfm *tfm)
488{
489 return crypto_destroy_tfm(tfm, tfm);
490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Herbert Xuda7f0332008-07-31 17:08:25 +0800492int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * Transform helpers which query the underlying algorithm.
496 */
497static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
498{
499 return tfm->__crt_alg->cra_name;
500}
501
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000502static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
503{
504 return tfm->__crt_alg->cra_driver_name;
505}
506
507static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
508{
509 return tfm->__crt_alg->cra_priority;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
513{
514 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
518{
519 return tfm->__crt_alg->cra_blocksize;
520}
521
Herbert Xufbdae9f2005-07-06 13:53:29 -0700522static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
523{
524 return tfm->__crt_alg->cra_alignmask;
525}
526
Herbert Xuf28776a2006-08-13 20:58:18 +1000527static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
528{
529 return tfm->crt_flags;
530}
531
532static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
533{
534 tfm->crt_flags |= flags;
535}
536
537static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
538{
539 tfm->crt_flags &= ~flags;
540}
541
Herbert Xu40725182005-07-06 13:51:52 -0700542static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
543{
Herbert Xuf10b7892006-01-25 22:34:01 +1100544 return tfm->__crt_ctx;
545}
546
547static inline unsigned int crypto_tfm_ctx_alignment(void)
548{
549 struct crypto_tfm *tfm;
550 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
554 * API wrappers.
555 */
Herbert Xu32e39832007-03-24 14:35:34 +1100556static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
557 struct crypto_tfm *tfm)
558{
559 return (struct crypto_ablkcipher *)tfm;
560}
561
Herbert Xu378f4f52007-12-17 20:07:31 +0800562static inline u32 crypto_skcipher_type(u32 type)
563{
Herbert Xuecfc4322007-12-05 21:08:36 +1100564 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800565 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
566 return type;
567}
568
569static inline u32 crypto_skcipher_mask(u32 mask)
570{
Herbert Xuecfc4322007-12-05 21:08:36 +1100571 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800572 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
573 return mask;
574}
575
Herbert Xub9c55aa2007-12-04 12:46:48 +1100576struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
577 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100578
579static inline struct crypto_tfm *crypto_ablkcipher_tfm(
580 struct crypto_ablkcipher *tfm)
581{
582 return &tfm->base;
583}
584
585static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
586{
587 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
588}
589
590static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
591 u32 mask)
592{
Herbert Xu378f4f52007-12-17 20:07:31 +0800593 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
594 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100595}
596
597static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
598 struct crypto_ablkcipher *tfm)
599{
600 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
601}
602
603static inline unsigned int crypto_ablkcipher_ivsize(
604 struct crypto_ablkcipher *tfm)
605{
606 return crypto_ablkcipher_crt(tfm)->ivsize;
607}
608
609static inline unsigned int crypto_ablkcipher_blocksize(
610 struct crypto_ablkcipher *tfm)
611{
612 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
613}
614
615static inline unsigned int crypto_ablkcipher_alignmask(
616 struct crypto_ablkcipher *tfm)
617{
618 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
619}
620
621static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
622{
623 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
624}
625
626static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
627 u32 flags)
628{
629 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
630}
631
632static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
633 u32 flags)
634{
635 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
636}
637
638static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
639 const u8 *key, unsigned int keylen)
640{
Herbert Xuecfc4322007-12-05 21:08:36 +1100641 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
642
643 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100644}
645
646static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
647 struct ablkcipher_request *req)
648{
649 return __crypto_ablkcipher_cast(req->base.tfm);
650}
651
652static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
653{
654 struct ablkcipher_tfm *crt =
655 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
656 return crt->encrypt(req);
657}
658
659static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
660{
661 struct ablkcipher_tfm *crt =
662 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
663 return crt->decrypt(req);
664}
665
Herbert Xub16c3a22007-08-29 19:02:04 +0800666static inline unsigned int crypto_ablkcipher_reqsize(
667 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100668{
669 return crypto_ablkcipher_crt(tfm)->reqsize;
670}
671
Herbert Xue196d622007-04-14 16:09:14 +1000672static inline void ablkcipher_request_set_tfm(
673 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
674{
Herbert Xuecfc4322007-12-05 21:08:36 +1100675 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000676}
677
Herbert Xub5b7f082007-04-16 20:48:54 +1000678static inline struct ablkcipher_request *ablkcipher_request_cast(
679 struct crypto_async_request *req)
680{
681 return container_of(req, struct ablkcipher_request, base);
682}
683
Herbert Xu32e39832007-03-24 14:35:34 +1100684static inline struct ablkcipher_request *ablkcipher_request_alloc(
685 struct crypto_ablkcipher *tfm, gfp_t gfp)
686{
687 struct ablkcipher_request *req;
688
689 req = kmalloc(sizeof(struct ablkcipher_request) +
690 crypto_ablkcipher_reqsize(tfm), gfp);
691
692 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000693 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100694
695 return req;
696}
697
698static inline void ablkcipher_request_free(struct ablkcipher_request *req)
699{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800700 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100701}
702
703static inline void ablkcipher_request_set_callback(
704 struct ablkcipher_request *req,
705 u32 flags, crypto_completion_t complete, void *data)
706{
707 req->base.complete = complete;
708 req->base.data = data;
709 req->base.flags = flags;
710}
711
712static inline void ablkcipher_request_set_crypt(
713 struct ablkcipher_request *req,
714 struct scatterlist *src, struct scatterlist *dst,
715 unsigned int nbytes, void *iv)
716{
717 req->src = src;
718 req->dst = dst;
719 req->nbytes = nbytes;
720 req->info = iv;
721}
722
Herbert Xu1ae97822007-08-30 15:36:14 +0800723static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
724{
725 return (struct crypto_aead *)tfm;
726}
727
Herbert Xud29ce982007-12-12 19:24:27 +0800728struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800729
730static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
731{
732 return &tfm->base;
733}
734
735static inline void crypto_free_aead(struct crypto_aead *tfm)
736{
737 crypto_free_tfm(crypto_aead_tfm(tfm));
738}
739
740static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
741{
742 return &crypto_aead_tfm(tfm)->crt_aead;
743}
744
745static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
746{
747 return crypto_aead_crt(tfm)->ivsize;
748}
749
750static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
751{
752 return crypto_aead_crt(tfm)->authsize;
753}
754
755static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
756{
757 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
758}
759
760static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
761{
762 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
763}
764
765static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
766{
767 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
768}
769
770static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
771{
772 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
773}
774
775static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
776{
777 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
778}
779
780static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
781 unsigned int keylen)
782{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800783 struct aead_tfm *crt = crypto_aead_crt(tfm);
784
785 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800786}
787
Herbert Xu7ba683a2007-12-02 18:49:21 +1100788int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
789
Herbert Xu1ae97822007-08-30 15:36:14 +0800790static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
791{
792 return __crypto_aead_cast(req->base.tfm);
793}
794
795static inline int crypto_aead_encrypt(struct aead_request *req)
796{
797 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
798}
799
800static inline int crypto_aead_decrypt(struct aead_request *req)
801{
802 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
803}
804
Herbert Xub16c3a22007-08-29 19:02:04 +0800805static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800806{
807 return crypto_aead_crt(tfm)->reqsize;
808}
809
810static inline void aead_request_set_tfm(struct aead_request *req,
811 struct crypto_aead *tfm)
812{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800813 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800814}
815
816static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
817 gfp_t gfp)
818{
819 struct aead_request *req;
820
821 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
822
823 if (likely(req))
824 aead_request_set_tfm(req, tfm);
825
826 return req;
827}
828
829static inline void aead_request_free(struct aead_request *req)
830{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800831 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800832}
833
834static inline void aead_request_set_callback(struct aead_request *req,
835 u32 flags,
836 crypto_completion_t complete,
837 void *data)
838{
839 req->base.complete = complete;
840 req->base.data = data;
841 req->base.flags = flags;
842}
843
844static inline void aead_request_set_crypt(struct aead_request *req,
845 struct scatterlist *src,
846 struct scatterlist *dst,
847 unsigned int cryptlen, u8 *iv)
848{
849 req->src = src;
850 req->dst = dst;
851 req->cryptlen = cryptlen;
852 req->iv = iv;
853}
854
855static inline void aead_request_set_assoc(struct aead_request *req,
856 struct scatterlist *assoc,
857 unsigned int assoclen)
858{
859 req->assoc = assoc;
860 req->assoclen = assoclen;
861}
862
Herbert Xu5cde0af2006-08-22 00:07:53 +1000863static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
864 struct crypto_tfm *tfm)
865{
866 return (struct crypto_blkcipher *)tfm;
867}
868
869static inline struct crypto_blkcipher *crypto_blkcipher_cast(
870 struct crypto_tfm *tfm)
871{
872 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
873 return __crypto_blkcipher_cast(tfm);
874}
875
876static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
877 const char *alg_name, u32 type, u32 mask)
878{
Herbert Xu332f88402007-11-15 22:36:07 +0800879 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000880 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800881 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000882
883 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
884}
885
886static inline struct crypto_tfm *crypto_blkcipher_tfm(
887 struct crypto_blkcipher *tfm)
888{
889 return &tfm->base;
890}
891
892static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
893{
894 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
895}
896
Herbert Xufce32d72006-08-26 17:35:45 +1000897static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
898{
Herbert Xu332f88402007-11-15 22:36:07 +0800899 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000900 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800901 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000902
903 return crypto_has_alg(alg_name, type, mask);
904}
905
Herbert Xu5cde0af2006-08-22 00:07:53 +1000906static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
907{
908 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
909}
910
911static inline struct blkcipher_tfm *crypto_blkcipher_crt(
912 struct crypto_blkcipher *tfm)
913{
914 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
915}
916
917static inline struct blkcipher_alg *crypto_blkcipher_alg(
918 struct crypto_blkcipher *tfm)
919{
920 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
921}
922
923static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
924{
925 return crypto_blkcipher_alg(tfm)->ivsize;
926}
927
928static inline unsigned int crypto_blkcipher_blocksize(
929 struct crypto_blkcipher *tfm)
930{
931 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
932}
933
934static inline unsigned int crypto_blkcipher_alignmask(
935 struct crypto_blkcipher *tfm)
936{
937 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
938}
939
940static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
941{
942 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
943}
944
945static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
946 u32 flags)
947{
948 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
949}
950
951static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
952 u32 flags)
953{
954 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
955}
956
957static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
958 const u8 *key, unsigned int keylen)
959{
960 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
961 key, keylen);
962}
963
964static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
965 struct scatterlist *dst,
966 struct scatterlist *src,
967 unsigned int nbytes)
968{
969 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
970 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
971}
972
973static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
974 struct scatterlist *dst,
975 struct scatterlist *src,
976 unsigned int nbytes)
977{
978 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
979}
980
981static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
982 struct scatterlist *dst,
983 struct scatterlist *src,
984 unsigned int nbytes)
985{
986 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
987 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
988}
989
990static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
991 struct scatterlist *dst,
992 struct scatterlist *src,
993 unsigned int nbytes)
994{
995 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
996}
997
998static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
999 const u8 *src, unsigned int len)
1000{
1001 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1002}
1003
1004static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1005 u8 *dst, unsigned int len)
1006{
1007 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1008}
1009
Herbert Xuf28776a2006-08-13 20:58:18 +10001010static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1011{
1012 return (struct crypto_cipher *)tfm;
1013}
1014
1015static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1016{
1017 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1018 return __crypto_cipher_cast(tfm);
1019}
1020
1021static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1022 u32 type, u32 mask)
1023{
1024 type &= ~CRYPTO_ALG_TYPE_MASK;
1025 type |= CRYPTO_ALG_TYPE_CIPHER;
1026 mask |= CRYPTO_ALG_TYPE_MASK;
1027
1028 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1029}
1030
1031static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1032{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001033 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001034}
1035
1036static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1037{
1038 crypto_free_tfm(crypto_cipher_tfm(tfm));
1039}
1040
Herbert Xufce32d72006-08-26 17:35:45 +10001041static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1042{
1043 type &= ~CRYPTO_ALG_TYPE_MASK;
1044 type |= CRYPTO_ALG_TYPE_CIPHER;
1045 mask |= CRYPTO_ALG_TYPE_MASK;
1046
1047 return crypto_has_alg(alg_name, type, mask);
1048}
1049
Herbert Xuf28776a2006-08-13 20:58:18 +10001050static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1051{
1052 return &crypto_cipher_tfm(tfm)->crt_cipher;
1053}
1054
1055static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1056{
1057 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1058}
1059
1060static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1061{
1062 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1063}
1064
1065static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1066{
1067 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1068}
1069
1070static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1071 u32 flags)
1072{
1073 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1074}
1075
1076static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1077 u32 flags)
1078{
1079 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1080}
1081
Herbert Xu7226bc872006-08-21 21:40:49 +10001082static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1083 const u8 *key, unsigned int keylen)
1084{
1085 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1086 key, keylen);
1087}
1088
Herbert Xuf28776a2006-08-13 20:58:18 +10001089static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1090 u8 *dst, const u8 *src)
1091{
1092 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1093 dst, src);
1094}
1095
1096static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1097 u8 *dst, const u8 *src)
1098{
1099 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1100 dst, src);
1101}
1102
Herbert Xu055bcee2006-08-19 22:24:23 +10001103static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Herbert Xu055bcee2006-08-19 22:24:23 +10001105 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Herbert Xu055bcee2006-08-19 22:24:23 +10001108static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Herbert Xu055bcee2006-08-19 22:24:23 +10001110 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1111 CRYPTO_ALG_TYPE_HASH_MASK);
1112 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Herbert Xu055bcee2006-08-19 22:24:23 +10001115static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1116 u32 type, u32 mask)
1117{
1118 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001119 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001120 type |= CRYPTO_ALG_TYPE_HASH;
1121 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1122
1123 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1124}
1125
1126static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1127{
1128 return &tfm->base;
1129}
1130
1131static inline void crypto_free_hash(struct crypto_hash *tfm)
1132{
1133 crypto_free_tfm(crypto_hash_tfm(tfm));
1134}
1135
Herbert Xufce32d72006-08-26 17:35:45 +10001136static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1137{
1138 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001139 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001140 type |= CRYPTO_ALG_TYPE_HASH;
1141 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1142
1143 return crypto_has_alg(alg_name, type, mask);
1144}
1145
Herbert Xu055bcee2006-08-19 22:24:23 +10001146static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1147{
1148 return &crypto_hash_tfm(tfm)->crt_hash;
1149}
1150
1151static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1152{
1153 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1154}
1155
1156static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1157{
1158 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1159}
1160
1161static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1162{
1163 return crypto_hash_crt(tfm)->digestsize;
1164}
1165
1166static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1167{
1168 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1169}
1170
1171static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1172{
1173 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1174}
1175
1176static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1177{
1178 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1179}
1180
1181static inline int crypto_hash_init(struct hash_desc *desc)
1182{
1183 return crypto_hash_crt(desc->tfm)->init(desc);
1184}
1185
1186static inline int crypto_hash_update(struct hash_desc *desc,
1187 struct scatterlist *sg,
1188 unsigned int nbytes)
1189{
1190 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1191}
1192
1193static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1194{
1195 return crypto_hash_crt(desc->tfm)->final(desc, out);
1196}
1197
1198static inline int crypto_hash_digest(struct hash_desc *desc,
1199 struct scatterlist *sg,
1200 unsigned int nbytes, u8 *out)
1201{
1202 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1203}
1204
1205static inline int crypto_hash_setkey(struct crypto_hash *hash,
1206 const u8 *key, unsigned int keylen)
1207{
1208 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Herbert Xufce32d72006-08-26 17:35:45 +10001211static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1212{
1213 return (struct crypto_comp *)tfm;
1214}
1215
1216static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1217{
1218 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1219 CRYPTO_ALG_TYPE_MASK);
1220 return __crypto_comp_cast(tfm);
1221}
1222
1223static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1224 u32 type, u32 mask)
1225{
1226 type &= ~CRYPTO_ALG_TYPE_MASK;
1227 type |= CRYPTO_ALG_TYPE_COMPRESS;
1228 mask |= CRYPTO_ALG_TYPE_MASK;
1229
1230 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1231}
1232
1233static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1234{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001235 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001236}
1237
1238static inline void crypto_free_comp(struct crypto_comp *tfm)
1239{
1240 crypto_free_tfm(crypto_comp_tfm(tfm));
1241}
1242
1243static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1244{
1245 type &= ~CRYPTO_ALG_TYPE_MASK;
1246 type |= CRYPTO_ALG_TYPE_COMPRESS;
1247 mask |= CRYPTO_ALG_TYPE_MASK;
1248
1249 return crypto_has_alg(alg_name, type, mask);
1250}
1251
Herbert Xue4d5b792006-08-26 18:12:40 +10001252static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1253{
1254 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1255}
1256
Herbert Xufce32d72006-08-26 17:35:45 +10001257static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1258{
1259 return &crypto_comp_tfm(tfm)->crt_compress;
1260}
1261
1262static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 const u8 *src, unsigned int slen,
1264 u8 *dst, unsigned int *dlen)
1265{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001266 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1267 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Herbert Xufce32d72006-08-26 17:35:45 +10001270static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 const u8 *src, unsigned int slen,
1272 u8 *dst, unsigned int *dlen)
1273{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001274 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1275 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278#endif /* _LINUX_CRYPTO_H */
1279