blob: a8fa6541b86c7a0ee946de1c678bc648a9aadf5a [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 Sharma60063492011-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 Xu332f8842007-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 Xu332f8842007-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);
317
318/*
319 * Algorithm query interface.
320 */
Herbert Xufce32d72006-08-26 17:35:45 +1000321int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/*
324 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000325 * and core processing logic. Managed via crypto_alloc_*() and
326 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Herbert Xu32e39832007-03-24 14:35:34 +1100329struct ablkcipher_tfm {
330 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
331 unsigned int keylen);
332 int (*encrypt)(struct ablkcipher_request *req);
333 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800334 int (*givencrypt)(struct skcipher_givcrypt_request *req);
335 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
336
Herbert Xuecfc4322007-12-05 21:08:36 +1100337 struct crypto_ablkcipher *base;
338
Herbert Xu32e39832007-03-24 14:35:34 +1100339 unsigned int ivsize;
340 unsigned int reqsize;
341};
342
Herbert Xu1ae97822007-08-30 15:36:14 +0800343struct aead_tfm {
344 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
345 unsigned int keylen);
346 int (*encrypt)(struct aead_request *req);
347 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800348 int (*givencrypt)(struct aead_givcrypt_request *req);
349 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800350
351 struct crypto_aead *base;
352
Herbert Xu1ae97822007-08-30 15:36:14 +0800353 unsigned int ivsize;
354 unsigned int authsize;
355 unsigned int reqsize;
356};
357
Herbert Xu5cde0af2006-08-22 00:07:53 +1000358struct blkcipher_tfm {
359 void *iv;
360 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
361 unsigned int keylen);
362 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
363 struct scatterlist *src, unsigned int nbytes);
364 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
365 struct scatterlist *src, unsigned int nbytes);
366};
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int (*cit_setkey)(struct crypto_tfm *tfm,
370 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000371 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
372 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
Herbert Xu055bcee2006-08-19 22:24:23 +1000375struct hash_tfm {
376 int (*init)(struct hash_desc *desc);
377 int (*update)(struct hash_desc *desc,
378 struct scatterlist *sg, unsigned int nsg);
379 int (*final)(struct hash_desc *desc, u8 *out);
380 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
381 unsigned int nsg, u8 *out);
382 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
383 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000384 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
387struct compress_tfm {
388 int (*cot_compress)(struct crypto_tfm *tfm,
389 const u8 *src, unsigned int slen,
390 u8 *dst, unsigned int *dlen);
391 int (*cot_decompress)(struct crypto_tfm *tfm,
392 const u8 *src, unsigned int slen,
393 u8 *dst, unsigned int *dlen);
394};
395
Neil Horman17f0f4a2008-08-14 22:15:52 +1000396struct rng_tfm {
397 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
398 unsigned int dlen);
399 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
400};
401
Herbert Xu32e39832007-03-24 14:35:34 +1100402#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800403#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000404#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000406#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000408#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410struct crypto_tfm {
411
412 u32 crt_flags;
413
414 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100415 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800416 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000417 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000419 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000421 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700423
424 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100427
Herbert Xu79911102006-08-21 21:03:52 +1000428 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429};
430
Herbert Xu32e39832007-03-24 14:35:34 +1100431struct crypto_ablkcipher {
432 struct crypto_tfm base;
433};
434
Herbert Xu1ae97822007-08-30 15:36:14 +0800435struct crypto_aead {
436 struct crypto_tfm base;
437};
438
Herbert Xu5cde0af2006-08-22 00:07:53 +1000439struct crypto_blkcipher {
440 struct crypto_tfm base;
441};
442
Herbert Xu78a1fe42006-12-24 10:02:00 +1100443struct crypto_cipher {
444 struct crypto_tfm base;
445};
446
447struct crypto_comp {
448 struct crypto_tfm base;
449};
450
Herbert Xu055bcee2006-08-19 22:24:23 +1000451struct crypto_hash {
452 struct crypto_tfm base;
453};
454
Neil Horman17f0f4a2008-08-14 22:15:52 +1000455struct crypto_rng {
456 struct crypto_tfm base;
457};
458
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000459enum {
460 CRYPTOA_UNSPEC,
461 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100462 CRYPTOA_TYPE,
Herbert Xu39e1ee02007-08-29 19:27:26 +0800463 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100464 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000465};
466
Herbert Xuebc610e2007-01-01 18:37:02 +1100467#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
468
Herbert Xu39e1ee02007-08-29 19:27:26 +0800469/* Maximum number of (rtattr) parameters for each template. */
470#define CRYPTO_MAX_ATTRS 32
471
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000472struct crypto_attr_alg {
473 char name[CRYPTO_MAX_ALG_NAME];
474};
475
Herbert Xuebc610e2007-01-01 18:37:02 +1100476struct crypto_attr_type {
477 u32 type;
478 u32 mask;
479};
480
Herbert Xu39e1ee02007-08-29 19:27:26 +0800481struct crypto_attr_u32 {
482 u32 num;
483};
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
486 * Transform user interface.
487 */
488
Herbert Xu6d7d6842006-07-30 11:53:01 +1000489struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100490void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
491
492static inline void crypto_free_tfm(struct crypto_tfm *tfm)
493{
494 return crypto_destroy_tfm(tfm, tfm);
495}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Herbert Xuda7f0332008-07-31 17:08:25 +0800497int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/*
500 * Transform helpers which query the underlying algorithm.
501 */
502static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
503{
504 return tfm->__crt_alg->cra_name;
505}
506
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000507static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
508{
509 return tfm->__crt_alg->cra_driver_name;
510}
511
512static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
513{
514 return tfm->__crt_alg->cra_priority;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
518{
519 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
523{
524 return tfm->__crt_alg->cra_blocksize;
525}
526
Herbert Xufbdae9f2005-07-06 13:53:29 -0700527static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
528{
529 return tfm->__crt_alg->cra_alignmask;
530}
531
Herbert Xuf28776a2006-08-13 20:58:18 +1000532static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
533{
534 return tfm->crt_flags;
535}
536
537static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
538{
539 tfm->crt_flags |= flags;
540}
541
542static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
543{
544 tfm->crt_flags &= ~flags;
545}
546
Herbert Xu40725182005-07-06 13:51:52 -0700547static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
548{
Herbert Xuf10b7892006-01-25 22:34:01 +1100549 return tfm->__crt_ctx;
550}
551
552static inline unsigned int crypto_tfm_ctx_alignment(void)
553{
554 struct crypto_tfm *tfm;
555 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * API wrappers.
560 */
Herbert Xu32e39832007-03-24 14:35:34 +1100561static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
562 struct crypto_tfm *tfm)
563{
564 return (struct crypto_ablkcipher *)tfm;
565}
566
Herbert Xu378f4f52007-12-17 20:07:31 +0800567static inline u32 crypto_skcipher_type(u32 type)
568{
Herbert Xuecfc4322007-12-05 21:08:36 +1100569 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800570 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
571 return type;
572}
573
574static inline u32 crypto_skcipher_mask(u32 mask)
575{
Herbert Xuecfc4322007-12-05 21:08:36 +1100576 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800577 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
578 return mask;
579}
580
Herbert Xub9c55aa2007-12-04 12:46:48 +1100581struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
582 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100583
584static inline struct crypto_tfm *crypto_ablkcipher_tfm(
585 struct crypto_ablkcipher *tfm)
586{
587 return &tfm->base;
588}
589
590static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
591{
592 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
593}
594
595static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
596 u32 mask)
597{
Herbert Xu378f4f52007-12-17 20:07:31 +0800598 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
599 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100600}
601
602static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
603 struct crypto_ablkcipher *tfm)
604{
605 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
606}
607
608static inline unsigned int crypto_ablkcipher_ivsize(
609 struct crypto_ablkcipher *tfm)
610{
611 return crypto_ablkcipher_crt(tfm)->ivsize;
612}
613
614static inline unsigned int crypto_ablkcipher_blocksize(
615 struct crypto_ablkcipher *tfm)
616{
617 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
618}
619
620static inline unsigned int crypto_ablkcipher_alignmask(
621 struct crypto_ablkcipher *tfm)
622{
623 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
624}
625
626static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
627{
628 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
629}
630
631static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
632 u32 flags)
633{
634 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
635}
636
637static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
638 u32 flags)
639{
640 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
641}
642
643static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
644 const u8 *key, unsigned int keylen)
645{
Herbert Xuecfc4322007-12-05 21:08:36 +1100646 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
647
648 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100649}
650
651static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
652 struct ablkcipher_request *req)
653{
654 return __crypto_ablkcipher_cast(req->base.tfm);
655}
656
657static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
658{
659 struct ablkcipher_tfm *crt =
660 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
661 return crt->encrypt(req);
662}
663
664static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
665{
666 struct ablkcipher_tfm *crt =
667 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
668 return crt->decrypt(req);
669}
670
Herbert Xub16c3a22007-08-29 19:02:04 +0800671static inline unsigned int crypto_ablkcipher_reqsize(
672 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100673{
674 return crypto_ablkcipher_crt(tfm)->reqsize;
675}
676
Herbert Xue196d622007-04-14 16:09:14 +1000677static inline void ablkcipher_request_set_tfm(
678 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
679{
Herbert Xuecfc4322007-12-05 21:08:36 +1100680 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000681}
682
Herbert Xub5b7f082007-04-16 20:48:54 +1000683static inline struct ablkcipher_request *ablkcipher_request_cast(
684 struct crypto_async_request *req)
685{
686 return container_of(req, struct ablkcipher_request, base);
687}
688
Herbert Xu32e39832007-03-24 14:35:34 +1100689static inline struct ablkcipher_request *ablkcipher_request_alloc(
690 struct crypto_ablkcipher *tfm, gfp_t gfp)
691{
692 struct ablkcipher_request *req;
693
694 req = kmalloc(sizeof(struct ablkcipher_request) +
695 crypto_ablkcipher_reqsize(tfm), gfp);
696
697 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000698 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100699
700 return req;
701}
702
703static inline void ablkcipher_request_free(struct ablkcipher_request *req)
704{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800705 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100706}
707
708static inline void ablkcipher_request_set_callback(
709 struct ablkcipher_request *req,
710 u32 flags, crypto_completion_t complete, void *data)
711{
712 req->base.complete = complete;
713 req->base.data = data;
714 req->base.flags = flags;
715}
716
717static inline void ablkcipher_request_set_crypt(
718 struct ablkcipher_request *req,
719 struct scatterlist *src, struct scatterlist *dst,
720 unsigned int nbytes, void *iv)
721{
722 req->src = src;
723 req->dst = dst;
724 req->nbytes = nbytes;
725 req->info = iv;
726}
727
Herbert Xu1ae97822007-08-30 15:36:14 +0800728static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
729{
730 return (struct crypto_aead *)tfm;
731}
732
Herbert Xud29ce982007-12-12 19:24:27 +0800733struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800734
735static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
736{
737 return &tfm->base;
738}
739
740static inline void crypto_free_aead(struct crypto_aead *tfm)
741{
742 crypto_free_tfm(crypto_aead_tfm(tfm));
743}
744
745static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
746{
747 return &crypto_aead_tfm(tfm)->crt_aead;
748}
749
750static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
751{
752 return crypto_aead_crt(tfm)->ivsize;
753}
754
755static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
756{
757 return crypto_aead_crt(tfm)->authsize;
758}
759
760static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
761{
762 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
763}
764
765static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
766{
767 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
768}
769
770static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
771{
772 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
773}
774
775static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
776{
777 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
778}
779
780static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
781{
782 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
783}
784
785static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
786 unsigned int keylen)
787{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800788 struct aead_tfm *crt = crypto_aead_crt(tfm);
789
790 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800791}
792
Herbert Xu7ba683a2007-12-02 18:49:21 +1100793int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
794
Herbert Xu1ae97822007-08-30 15:36:14 +0800795static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
796{
797 return __crypto_aead_cast(req->base.tfm);
798}
799
800static inline int crypto_aead_encrypt(struct aead_request *req)
801{
802 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
803}
804
805static inline int crypto_aead_decrypt(struct aead_request *req)
806{
807 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
808}
809
Herbert Xub16c3a22007-08-29 19:02:04 +0800810static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800811{
812 return crypto_aead_crt(tfm)->reqsize;
813}
814
815static inline void aead_request_set_tfm(struct aead_request *req,
816 struct crypto_aead *tfm)
817{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800818 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800819}
820
821static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
822 gfp_t gfp)
823{
824 struct aead_request *req;
825
826 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
827
828 if (likely(req))
829 aead_request_set_tfm(req, tfm);
830
831 return req;
832}
833
834static inline void aead_request_free(struct aead_request *req)
835{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800836 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800837}
838
839static inline void aead_request_set_callback(struct aead_request *req,
840 u32 flags,
841 crypto_completion_t complete,
842 void *data)
843{
844 req->base.complete = complete;
845 req->base.data = data;
846 req->base.flags = flags;
847}
848
849static inline void aead_request_set_crypt(struct aead_request *req,
850 struct scatterlist *src,
851 struct scatterlist *dst,
852 unsigned int cryptlen, u8 *iv)
853{
854 req->src = src;
855 req->dst = dst;
856 req->cryptlen = cryptlen;
857 req->iv = iv;
858}
859
860static inline void aead_request_set_assoc(struct aead_request *req,
861 struct scatterlist *assoc,
862 unsigned int assoclen)
863{
864 req->assoc = assoc;
865 req->assoclen = assoclen;
866}
867
Herbert Xu5cde0af2006-08-22 00:07:53 +1000868static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
869 struct crypto_tfm *tfm)
870{
871 return (struct crypto_blkcipher *)tfm;
872}
873
874static inline struct crypto_blkcipher *crypto_blkcipher_cast(
875 struct crypto_tfm *tfm)
876{
877 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
878 return __crypto_blkcipher_cast(tfm);
879}
880
881static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
882 const char *alg_name, u32 type, u32 mask)
883{
Herbert Xu332f8842007-11-15 22:36:07 +0800884 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000885 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800886 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000887
888 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
889}
890
891static inline struct crypto_tfm *crypto_blkcipher_tfm(
892 struct crypto_blkcipher *tfm)
893{
894 return &tfm->base;
895}
896
897static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
898{
899 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
900}
901
Herbert Xufce32d72006-08-26 17:35:45 +1000902static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
903{
Herbert Xu332f8842007-11-15 22:36:07 +0800904 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000905 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f8842007-11-15 22:36:07 +0800906 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000907
908 return crypto_has_alg(alg_name, type, mask);
909}
910
Herbert Xu5cde0af2006-08-22 00:07:53 +1000911static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
912{
913 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
914}
915
916static inline struct blkcipher_tfm *crypto_blkcipher_crt(
917 struct crypto_blkcipher *tfm)
918{
919 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
920}
921
922static inline struct blkcipher_alg *crypto_blkcipher_alg(
923 struct crypto_blkcipher *tfm)
924{
925 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
926}
927
928static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
929{
930 return crypto_blkcipher_alg(tfm)->ivsize;
931}
932
933static inline unsigned int crypto_blkcipher_blocksize(
934 struct crypto_blkcipher *tfm)
935{
936 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
937}
938
939static inline unsigned int crypto_blkcipher_alignmask(
940 struct crypto_blkcipher *tfm)
941{
942 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
943}
944
945static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
946{
947 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
948}
949
950static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
951 u32 flags)
952{
953 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
954}
955
956static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
957 u32 flags)
958{
959 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
960}
961
962static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
963 const u8 *key, unsigned int keylen)
964{
965 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
966 key, keylen);
967}
968
969static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
970 struct scatterlist *dst,
971 struct scatterlist *src,
972 unsigned int nbytes)
973{
974 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
975 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
976}
977
978static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
979 struct scatterlist *dst,
980 struct scatterlist *src,
981 unsigned int nbytes)
982{
983 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
984}
985
986static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
987 struct scatterlist *dst,
988 struct scatterlist *src,
989 unsigned int nbytes)
990{
991 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
992 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
993}
994
995static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
996 struct scatterlist *dst,
997 struct scatterlist *src,
998 unsigned int nbytes)
999{
1000 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1001}
1002
1003static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1004 const u8 *src, unsigned int len)
1005{
1006 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1007}
1008
1009static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1010 u8 *dst, unsigned int len)
1011{
1012 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1013}
1014
Herbert Xuf28776a2006-08-13 20:58:18 +10001015static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1016{
1017 return (struct crypto_cipher *)tfm;
1018}
1019
1020static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1021{
1022 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1023 return __crypto_cipher_cast(tfm);
1024}
1025
1026static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1027 u32 type, u32 mask)
1028{
1029 type &= ~CRYPTO_ALG_TYPE_MASK;
1030 type |= CRYPTO_ALG_TYPE_CIPHER;
1031 mask |= CRYPTO_ALG_TYPE_MASK;
1032
1033 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1034}
1035
1036static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1037{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001038 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001039}
1040
1041static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1042{
1043 crypto_free_tfm(crypto_cipher_tfm(tfm));
1044}
1045
Herbert Xufce32d72006-08-26 17:35:45 +10001046static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1047{
1048 type &= ~CRYPTO_ALG_TYPE_MASK;
1049 type |= CRYPTO_ALG_TYPE_CIPHER;
1050 mask |= CRYPTO_ALG_TYPE_MASK;
1051
1052 return crypto_has_alg(alg_name, type, mask);
1053}
1054
Herbert Xuf28776a2006-08-13 20:58:18 +10001055static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1056{
1057 return &crypto_cipher_tfm(tfm)->crt_cipher;
1058}
1059
1060static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1061{
1062 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1063}
1064
1065static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1066{
1067 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1068}
1069
1070static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1071{
1072 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1073}
1074
1075static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1076 u32 flags)
1077{
1078 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1079}
1080
1081static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1082 u32 flags)
1083{
1084 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1085}
1086
Herbert Xu7226bc82006-08-21 21:40:49 +10001087static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1088 const u8 *key, unsigned int keylen)
1089{
1090 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1091 key, keylen);
1092}
1093
Herbert Xuf28776a2006-08-13 20:58:18 +10001094static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1095 u8 *dst, const u8 *src)
1096{
1097 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1098 dst, src);
1099}
1100
1101static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1102 u8 *dst, const u8 *src)
1103{
1104 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1105 dst, src);
1106}
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 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Herbert Xu055bcee2006-08-19 22:24:23 +10001113static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Herbert Xu055bcee2006-08-19 22:24:23 +10001115 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1116 CRYPTO_ALG_TYPE_HASH_MASK);
1117 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Herbert Xu055bcee2006-08-19 22:24:23 +10001120static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1121 u32 type, u32 mask)
1122{
1123 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001124 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001125 type |= CRYPTO_ALG_TYPE_HASH;
1126 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1127
1128 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1129}
1130
1131static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1132{
1133 return &tfm->base;
1134}
1135
1136static inline void crypto_free_hash(struct crypto_hash *tfm)
1137{
1138 crypto_free_tfm(crypto_hash_tfm(tfm));
1139}
1140
Herbert Xufce32d72006-08-26 17:35:45 +10001141static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1142{
1143 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001144 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001145 type |= CRYPTO_ALG_TYPE_HASH;
1146 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1147
1148 return crypto_has_alg(alg_name, type, mask);
1149}
1150
Herbert Xu055bcee2006-08-19 22:24:23 +10001151static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1152{
1153 return &crypto_hash_tfm(tfm)->crt_hash;
1154}
1155
1156static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1157{
1158 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1159}
1160
1161static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1162{
1163 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1164}
1165
1166static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1167{
1168 return crypto_hash_crt(tfm)->digestsize;
1169}
1170
1171static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1172{
1173 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1174}
1175
1176static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1177{
1178 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1179}
1180
1181static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1182{
1183 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1184}
1185
1186static inline int crypto_hash_init(struct hash_desc *desc)
1187{
1188 return crypto_hash_crt(desc->tfm)->init(desc);
1189}
1190
1191static inline int crypto_hash_update(struct hash_desc *desc,
1192 struct scatterlist *sg,
1193 unsigned int nbytes)
1194{
1195 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1196}
1197
1198static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1199{
1200 return crypto_hash_crt(desc->tfm)->final(desc, out);
1201}
1202
1203static inline int crypto_hash_digest(struct hash_desc *desc,
1204 struct scatterlist *sg,
1205 unsigned int nbytes, u8 *out)
1206{
1207 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1208}
1209
1210static inline int crypto_hash_setkey(struct crypto_hash *hash,
1211 const u8 *key, unsigned int keylen)
1212{
1213 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Herbert Xufce32d72006-08-26 17:35:45 +10001216static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1217{
1218 return (struct crypto_comp *)tfm;
1219}
1220
1221static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1222{
1223 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1224 CRYPTO_ALG_TYPE_MASK);
1225 return __crypto_comp_cast(tfm);
1226}
1227
1228static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1229 u32 type, u32 mask)
1230{
1231 type &= ~CRYPTO_ALG_TYPE_MASK;
1232 type |= CRYPTO_ALG_TYPE_COMPRESS;
1233 mask |= CRYPTO_ALG_TYPE_MASK;
1234
1235 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1236}
1237
1238static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1239{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001240 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001241}
1242
1243static inline void crypto_free_comp(struct crypto_comp *tfm)
1244{
1245 crypto_free_tfm(crypto_comp_tfm(tfm));
1246}
1247
1248static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1249{
1250 type &= ~CRYPTO_ALG_TYPE_MASK;
1251 type |= CRYPTO_ALG_TYPE_COMPRESS;
1252 mask |= CRYPTO_ALG_TYPE_MASK;
1253
1254 return crypto_has_alg(alg_name, type, mask);
1255}
1256
Herbert Xue4d5b792006-08-26 18:12:40 +10001257static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1258{
1259 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1260}
1261
Herbert Xufce32d72006-08-26 17:35:45 +10001262static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1263{
1264 return &crypto_comp_tfm(tfm)->crt_compress;
1265}
1266
1267static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 const u8 *src, unsigned int slen,
1269 u8 *dst, unsigned int *dlen)
1270{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001271 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1272 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Herbert Xufce32d72006-08-26 17:35:45 +10001275static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 const u8 *src, unsigned int slen,
1277 u8 *dst, unsigned int *dlen)
1278{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001279 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1280 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283#endif /* _LINUX_CRYPTO_H */
1284