blob: 44c72f0f9b05e7843c8becd5b7ab1c77aef39d60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Herbert Xu6521f302006-08-06 20:28:44 +100020#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Algorithm masks and types.
30 */
Herbert Xu28259822006-08-06 21:23:26 +100031#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080033#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100035#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080036#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080037#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Loc Ho004a4032008-05-14 20:41:47 +080038#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
39#define CRYPTO_ALG_TYPE_HASH 0x00000009
40#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Herbert Xu3b2f6df2008-08-31 18:52:18 +100041#define CRYPTO_ALG_TYPE_SHASH 0x0000000b
Neil Horman17f0f4a2008-08-14 22:15:52 +100042#define CRYPTO_ALG_TYPE_RNG 0x0000000c
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * Transform masks and values (for crt_flags).
75 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define CRYPTO_TFM_REQ_MASK 0x000fff00
77#define CRYPTO_TFM_RES_MASK 0xfff00000
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070080#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110081#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
83#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
84#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
85#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
86#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
87
88/*
89 * Miscellaneous stuff.
90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define CRYPTO_MAX_ALG_NAME 64
92
Herbert Xu79911102006-08-21 21:03:52 +100093/*
94 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
95 * declaration) is used to ensure that the crypto_tfm context structure is
96 * aligned correctly for the given architecture so that there are no alignment
97 * faults for C data types. In particular, this is required on platforms such
98 * as arm where pointers are 32-bit aligned but there are data types such as
99 * u64 which require 64-bit alignment.
100 */
101#if defined(ARCH_KMALLOC_MINALIGN)
102#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
103#elif defined(ARCH_SLAB_MINALIGN)
104#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
Herbert Xu6eb72282008-01-08 17:16:44 +1100105#else
106#define CRYPTO_MINALIGN __alignof__(unsigned long long)
Herbert Xu79911102006-08-21 21:03:52 +1000107#endif
108
Herbert Xu79911102006-08-21 21:03:52 +1000109#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100112struct crypto_ablkcipher;
113struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800114struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000115struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000116struct crypto_hash;
Loc Ho004a4032008-05-14 20:41:47 +0800117struct crypto_ahash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000118struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700119struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000120struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800121struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800122struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700123
Herbert Xu32e39832007-03-24 14:35:34 +1100124typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
125
126struct crypto_async_request {
127 struct list_head list;
128 crypto_completion_t complete;
129 void *data;
130 struct crypto_tfm *tfm;
131
132 u32 flags;
133};
134
135struct ablkcipher_request {
136 struct crypto_async_request base;
137
138 unsigned int nbytes;
139
140 void *info;
141
142 struct scatterlist *src;
143 struct scatterlist *dst;
144
145 void *__ctx[] CRYPTO_MINALIGN_ATTR;
146};
147
Loc Ho004a4032008-05-14 20:41:47 +0800148struct ahash_request {
149 struct crypto_async_request base;
150
Loc Ho004a4032008-05-14 20:41:47 +0800151 unsigned int nbytes;
152 struct scatterlist *src;
153 u8 *result;
154
155 void *__ctx[] CRYPTO_MINALIGN_ATTR;
156};
157
Herbert Xu1ae97822007-08-30 15:36:14 +0800158/**
159 * struct aead_request - AEAD request
160 * @base: Common attributes for async crypto requests
161 * @assoclen: Length in bytes of associated data for authentication
162 * @cryptlen: Length of data to be encrypted or decrypted
163 * @iv: Initialisation vector
164 * @assoc: Associated data
165 * @src: Source data
166 * @dst: Destination data
167 * @__ctx: Start of private context data
168 */
169struct aead_request {
170 struct crypto_async_request base;
171
172 unsigned int assoclen;
173 unsigned int cryptlen;
174
175 u8 *iv;
176
177 struct scatterlist *assoc;
178 struct scatterlist *src;
179 struct scatterlist *dst;
180
181 void *__ctx[] CRYPTO_MINALIGN_ATTR;
182};
183
Herbert Xu5cde0af2006-08-22 00:07:53 +1000184struct blkcipher_desc {
185 struct crypto_blkcipher *tfm;
186 void *info;
187 u32 flags;
188};
189
Herbert Xu40725182005-07-06 13:51:52 -0700190struct cipher_desc {
191 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000192 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700193 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
194 const u8 *src, unsigned int nbytes);
195 void *info;
196};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Herbert Xu055bcee2006-08-19 22:24:23 +1000198struct hash_desc {
199 struct crypto_hash *tfm;
200 u32 flags;
201};
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
204 * Algorithms: modular crypto algorithm implementations, managed
205 * via crypto_register_alg() and crypto_unregister_alg().
206 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000207struct ablkcipher_alg {
208 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
209 unsigned int keylen);
210 int (*encrypt)(struct ablkcipher_request *req);
211 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800212 int (*givencrypt)(struct skcipher_givcrypt_request *req);
213 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000214
Herbert Xu23508e12007-11-27 21:33:24 +0800215 const char *geniv;
216
Herbert Xub5b7f082007-04-16 20:48:54 +1000217 unsigned int min_keysize;
218 unsigned int max_keysize;
219 unsigned int ivsize;
220};
221
Loc Ho004a4032008-05-14 20:41:47 +0800222struct ahash_alg {
223 int (*init)(struct ahash_request *req);
224 int (*update)(struct ahash_request *req);
225 int (*final)(struct ahash_request *req);
226 int (*digest)(struct ahash_request *req);
227 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
228 unsigned int keylen);
229
230 unsigned int digestsize;
231};
232
Herbert Xu1ae97822007-08-30 15:36:14 +0800233struct aead_alg {
234 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
235 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100236 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800237 int (*encrypt)(struct aead_request *req);
238 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800239 int (*givencrypt)(struct aead_givcrypt_request *req);
240 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800241
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800242 const char *geniv;
243
Herbert Xu1ae97822007-08-30 15:36:14 +0800244 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100245 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800246};
247
Herbert Xu5cde0af2006-08-22 00:07:53 +1000248struct blkcipher_alg {
249 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
250 unsigned int keylen);
251 int (*encrypt)(struct blkcipher_desc *desc,
252 struct scatterlist *dst, struct scatterlist *src,
253 unsigned int nbytes);
254 int (*decrypt)(struct blkcipher_desc *desc,
255 struct scatterlist *dst, struct scatterlist *src,
256 unsigned int nbytes);
257
Herbert Xu23508e12007-11-27 21:33:24 +0800258 const char *geniv;
259
Herbert Xu5cde0af2006-08-22 00:07:53 +1000260 unsigned int min_keysize;
261 unsigned int max_keysize;
262 unsigned int ivsize;
263};
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265struct cipher_alg {
266 unsigned int cia_min_keysize;
267 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000268 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000269 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000270 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
271 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272};
273
274struct digest_alg {
275 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000276 void (*dia_init)(struct crypto_tfm *tfm);
277 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
278 unsigned int len);
279 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
280 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000281 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
Herbert Xu055bcee2006-08-19 22:24:23 +1000284struct hash_alg {
285 int (*init)(struct hash_desc *desc);
286 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
287 unsigned int nbytes);
288 int (*final)(struct hash_desc *desc, u8 *out);
289 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
290 unsigned int nbytes, u8 *out);
291 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
292 unsigned int keylen);
293
294 unsigned int digestsize;
295};
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000298 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
299 unsigned int slen, u8 *dst, unsigned int *dlen);
300 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
301 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Neil Horman17f0f4a2008-08-14 22:15:52 +1000304struct rng_alg {
305 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
306 unsigned int dlen);
307 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
308
309 unsigned int seedsize;
310};
311
312
Herbert Xub5b7f082007-04-16 20:48:54 +1000313#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800314#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000315#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#define cra_cipher cra_u.cipher
317#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000318#define cra_hash cra_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800319#define cra_ahash cra_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000321#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323struct crypto_alg {
324 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000325 struct list_head cra_users;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 u32 cra_flags;
328 unsigned int cra_blocksize;
329 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700330 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100331
332 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000333 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100334
Herbert Xud913ea02006-05-21 08:45:26 +1000335 char cra_name[CRYPTO_MAX_ALG_NAME];
336 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Herbert Xue853c3c2006-08-22 00:06:54 +1000338 const struct crypto_type *cra_type;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000341 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800342 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000343 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct cipher_alg cipher;
345 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000346 struct hash_alg hash;
Loc Ho004a4032008-05-14 20:41:47 +0800347 struct ahash_alg ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000349 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000351
352 int (*cra_init)(struct crypto_tfm *tfm);
353 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000354 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 struct module *cra_module;
357};
358
359/*
360 * Algorithm registration interface.
361 */
362int crypto_register_alg(struct crypto_alg *alg);
363int crypto_unregister_alg(struct crypto_alg *alg);
364
365/*
366 * Algorithm query interface.
367 */
Herbert Xufce32d72006-08-26 17:35:45 +1000368int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370/*
371 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000372 * and core processing logic. Managed via crypto_alloc_*() and
373 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Herbert Xu32e39832007-03-24 14:35:34 +1100376struct ablkcipher_tfm {
377 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
378 unsigned int keylen);
379 int (*encrypt)(struct ablkcipher_request *req);
380 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800381 int (*givencrypt)(struct skcipher_givcrypt_request *req);
382 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
383
Herbert Xuecfc4322007-12-05 21:08:36 +1100384 struct crypto_ablkcipher *base;
385
Herbert Xu32e39832007-03-24 14:35:34 +1100386 unsigned int ivsize;
387 unsigned int reqsize;
388};
389
Herbert Xu1ae97822007-08-30 15:36:14 +0800390struct aead_tfm {
391 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
392 unsigned int keylen);
393 int (*encrypt)(struct aead_request *req);
394 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800395 int (*givencrypt)(struct aead_givcrypt_request *req);
396 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800397
398 struct crypto_aead *base;
399
Herbert Xu1ae97822007-08-30 15:36:14 +0800400 unsigned int ivsize;
401 unsigned int authsize;
402 unsigned int reqsize;
403};
404
Herbert Xu5cde0af2006-08-22 00:07:53 +1000405struct blkcipher_tfm {
406 void *iv;
407 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
408 unsigned int keylen);
409 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
410 struct scatterlist *src, unsigned int nbytes);
411 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
412 struct scatterlist *src, unsigned int nbytes);
413};
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int (*cit_setkey)(struct crypto_tfm *tfm,
417 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000418 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
419 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Herbert Xu055bcee2006-08-19 22:24:23 +1000422struct hash_tfm {
423 int (*init)(struct hash_desc *desc);
424 int (*update)(struct hash_desc *desc,
425 struct scatterlist *sg, unsigned int nsg);
426 int (*final)(struct hash_desc *desc, u8 *out);
427 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
428 unsigned int nsg, u8 *out);
429 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
430 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000431 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432};
433
Loc Ho004a4032008-05-14 20:41:47 +0800434struct ahash_tfm {
435 int (*init)(struct ahash_request *req);
436 int (*update)(struct ahash_request *req);
437 int (*final)(struct ahash_request *req);
438 int (*digest)(struct ahash_request *req);
439 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
440 unsigned int keylen);
441
442 unsigned int digestsize;
Loc Ho004a4032008-05-14 20:41:47 +0800443 unsigned int reqsize;
444};
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446struct compress_tfm {
447 int (*cot_compress)(struct crypto_tfm *tfm,
448 const u8 *src, unsigned int slen,
449 u8 *dst, unsigned int *dlen);
450 int (*cot_decompress)(struct crypto_tfm *tfm,
451 const u8 *src, unsigned int slen,
452 u8 *dst, unsigned int *dlen);
453};
454
Neil Horman17f0f4a2008-08-14 22:15:52 +1000455struct rng_tfm {
456 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
457 unsigned int dlen);
458 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
459};
460
Herbert Xu32e39832007-03-24 14:35:34 +1100461#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800462#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000463#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000465#define crt_hash crt_u.hash
Loc Ho004a4032008-05-14 20:41:47 +0800466#define crt_ahash crt_u.ahash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#define crt_compress crt_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000468#define crt_rng crt_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470struct crypto_tfm {
471
472 u32 crt_flags;
473
474 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100475 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800476 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000477 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000479 struct hash_tfm hash;
Loc Ho004a4032008-05-14 20:41:47 +0800480 struct ahash_tfm ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct compress_tfm compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000482 struct rng_tfm rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700484
485 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100488
Herbert Xu79911102006-08-21 21:03:52 +1000489 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490};
491
Herbert Xu32e39832007-03-24 14:35:34 +1100492struct crypto_ablkcipher {
493 struct crypto_tfm base;
494};
495
Herbert Xu1ae97822007-08-30 15:36:14 +0800496struct crypto_aead {
497 struct crypto_tfm base;
498};
499
Herbert Xu5cde0af2006-08-22 00:07:53 +1000500struct crypto_blkcipher {
501 struct crypto_tfm base;
502};
503
Herbert Xu78a1fe42006-12-24 10:02:00 +1100504struct crypto_cipher {
505 struct crypto_tfm base;
506};
507
508struct crypto_comp {
509 struct crypto_tfm base;
510};
511
Herbert Xu055bcee2006-08-19 22:24:23 +1000512struct crypto_hash {
513 struct crypto_tfm base;
514};
515
Neil Horman17f0f4a2008-08-14 22:15:52 +1000516struct crypto_rng {
517 struct crypto_tfm base;
518};
519
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000520enum {
521 CRYPTOA_UNSPEC,
522 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100523 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800524 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100525 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000526};
527
Herbert Xuebc610e2007-01-01 18:37:02 +1100528#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
529
Herbert Xu39e1ee012007-08-29 19:27:26 +0800530/* Maximum number of (rtattr) parameters for each template. */
531#define CRYPTO_MAX_ATTRS 32
532
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000533struct crypto_attr_alg {
534 char name[CRYPTO_MAX_ALG_NAME];
535};
536
Herbert Xuebc610e2007-01-01 18:37:02 +1100537struct crypto_attr_type {
538 u32 type;
539 u32 mask;
540};
541
Herbert Xu39e1ee012007-08-29 19:27:26 +0800542struct crypto_attr_u32 {
543 u32 num;
544};
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * Transform user interface.
548 */
549
Herbert Xu7b0bac62008-09-21 06:52:53 +0900550struct crypto_tfm *crypto_alloc_tfm(const char *alg_name,
551 const struct crypto_type *frontend,
552 u32 type, u32 mask);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000553struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554void crypto_free_tfm(struct crypto_tfm *tfm);
555
Herbert Xuda7f0332008-07-31 17:08:25 +0800556int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Transform helpers which query the underlying algorithm.
560 */
561static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
562{
563 return tfm->__crt_alg->cra_name;
564}
565
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000566static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
567{
568 return tfm->__crt_alg->cra_driver_name;
569}
570
571static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
572{
573 return tfm->__crt_alg->cra_priority;
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
577{
578 return module_name(tfm->__crt_alg->cra_module);
579}
580
581static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
582{
583 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
587{
588 return tfm->__crt_alg->cra_blocksize;
589}
590
Herbert Xufbdae9f2005-07-06 13:53:29 -0700591static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
592{
593 return tfm->__crt_alg->cra_alignmask;
594}
595
Herbert Xuf28776a2006-08-13 20:58:18 +1000596static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
597{
598 return tfm->crt_flags;
599}
600
601static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
602{
603 tfm->crt_flags |= flags;
604}
605
606static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
607{
608 tfm->crt_flags &= ~flags;
609}
610
Herbert Xu40725182005-07-06 13:51:52 -0700611static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
612{
Herbert Xuf10b7892006-01-25 22:34:01 +1100613 return tfm->__crt_ctx;
614}
615
616static inline unsigned int crypto_tfm_ctx_alignment(void)
617{
618 struct crypto_tfm *tfm;
619 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
623 * API wrappers.
624 */
Herbert Xu32e39832007-03-24 14:35:34 +1100625static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
626 struct crypto_tfm *tfm)
627{
628 return (struct crypto_ablkcipher *)tfm;
629}
630
Herbert Xu378f4f52007-12-17 20:07:31 +0800631static inline u32 crypto_skcipher_type(u32 type)
632{
Herbert Xuecfc4322007-12-05 21:08:36 +1100633 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800634 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
635 return type;
636}
637
638static inline u32 crypto_skcipher_mask(u32 mask)
639{
Herbert Xuecfc4322007-12-05 21:08:36 +1100640 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800641 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
642 return mask;
643}
644
Herbert Xub9c55aa2007-12-04 12:46:48 +1100645struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
646 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100647
648static inline struct crypto_tfm *crypto_ablkcipher_tfm(
649 struct crypto_ablkcipher *tfm)
650{
651 return &tfm->base;
652}
653
654static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
655{
656 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
657}
658
659static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
660 u32 mask)
661{
Herbert Xu378f4f52007-12-17 20:07:31 +0800662 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
663 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100664}
665
666static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
667 struct crypto_ablkcipher *tfm)
668{
669 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
670}
671
672static inline unsigned int crypto_ablkcipher_ivsize(
673 struct crypto_ablkcipher *tfm)
674{
675 return crypto_ablkcipher_crt(tfm)->ivsize;
676}
677
678static inline unsigned int crypto_ablkcipher_blocksize(
679 struct crypto_ablkcipher *tfm)
680{
681 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
682}
683
684static inline unsigned int crypto_ablkcipher_alignmask(
685 struct crypto_ablkcipher *tfm)
686{
687 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
688}
689
690static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
691{
692 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
693}
694
695static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
696 u32 flags)
697{
698 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
699}
700
701static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
702 u32 flags)
703{
704 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
705}
706
707static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
708 const u8 *key, unsigned int keylen)
709{
Herbert Xuecfc4322007-12-05 21:08:36 +1100710 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
711
712 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100713}
714
715static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
716 struct ablkcipher_request *req)
717{
718 return __crypto_ablkcipher_cast(req->base.tfm);
719}
720
721static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
722{
723 struct ablkcipher_tfm *crt =
724 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
725 return crt->encrypt(req);
726}
727
728static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
729{
730 struct ablkcipher_tfm *crt =
731 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
732 return crt->decrypt(req);
733}
734
Herbert Xub16c3a22007-08-29 19:02:04 +0800735static inline unsigned int crypto_ablkcipher_reqsize(
736 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100737{
738 return crypto_ablkcipher_crt(tfm)->reqsize;
739}
740
Herbert Xue196d622007-04-14 16:09:14 +1000741static inline void ablkcipher_request_set_tfm(
742 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
743{
Herbert Xuecfc4322007-12-05 21:08:36 +1100744 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000745}
746
Herbert Xub5b7f082007-04-16 20:48:54 +1000747static inline struct ablkcipher_request *ablkcipher_request_cast(
748 struct crypto_async_request *req)
749{
750 return container_of(req, struct ablkcipher_request, base);
751}
752
Herbert Xu32e39832007-03-24 14:35:34 +1100753static inline struct ablkcipher_request *ablkcipher_request_alloc(
754 struct crypto_ablkcipher *tfm, gfp_t gfp)
755{
756 struct ablkcipher_request *req;
757
758 req = kmalloc(sizeof(struct ablkcipher_request) +
759 crypto_ablkcipher_reqsize(tfm), gfp);
760
761 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000762 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100763
764 return req;
765}
766
767static inline void ablkcipher_request_free(struct ablkcipher_request *req)
768{
769 kfree(req);
770}
771
772static inline void ablkcipher_request_set_callback(
773 struct ablkcipher_request *req,
774 u32 flags, crypto_completion_t complete, void *data)
775{
776 req->base.complete = complete;
777 req->base.data = data;
778 req->base.flags = flags;
779}
780
781static inline void ablkcipher_request_set_crypt(
782 struct ablkcipher_request *req,
783 struct scatterlist *src, struct scatterlist *dst,
784 unsigned int nbytes, void *iv)
785{
786 req->src = src;
787 req->dst = dst;
788 req->nbytes = nbytes;
789 req->info = iv;
790}
791
Herbert Xu1ae97822007-08-30 15:36:14 +0800792static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
793{
794 return (struct crypto_aead *)tfm;
795}
796
Herbert Xud29ce982007-12-12 19:24:27 +0800797struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800798
799static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
800{
801 return &tfm->base;
802}
803
804static inline void crypto_free_aead(struct crypto_aead *tfm)
805{
806 crypto_free_tfm(crypto_aead_tfm(tfm));
807}
808
809static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
810{
811 return &crypto_aead_tfm(tfm)->crt_aead;
812}
813
814static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
815{
816 return crypto_aead_crt(tfm)->ivsize;
817}
818
819static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
820{
821 return crypto_aead_crt(tfm)->authsize;
822}
823
824static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
825{
826 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
827}
828
829static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
830{
831 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
832}
833
834static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
835{
836 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
837}
838
839static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
840{
841 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
842}
843
844static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
845{
846 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
847}
848
849static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
850 unsigned int keylen)
851{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800852 struct aead_tfm *crt = crypto_aead_crt(tfm);
853
854 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800855}
856
Herbert Xu7ba683a2007-12-02 18:49:21 +1100857int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
858
Herbert Xu1ae97822007-08-30 15:36:14 +0800859static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
860{
861 return __crypto_aead_cast(req->base.tfm);
862}
863
864static inline int crypto_aead_encrypt(struct aead_request *req)
865{
866 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
867}
868
869static inline int crypto_aead_decrypt(struct aead_request *req)
870{
871 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
872}
873
Herbert Xub16c3a22007-08-29 19:02:04 +0800874static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800875{
876 return crypto_aead_crt(tfm)->reqsize;
877}
878
879static inline void aead_request_set_tfm(struct aead_request *req,
880 struct crypto_aead *tfm)
881{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800882 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800883}
884
885static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
886 gfp_t gfp)
887{
888 struct aead_request *req;
889
890 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
891
892 if (likely(req))
893 aead_request_set_tfm(req, tfm);
894
895 return req;
896}
897
898static inline void aead_request_free(struct aead_request *req)
899{
900 kfree(req);
901}
902
903static inline void aead_request_set_callback(struct aead_request *req,
904 u32 flags,
905 crypto_completion_t complete,
906 void *data)
907{
908 req->base.complete = complete;
909 req->base.data = data;
910 req->base.flags = flags;
911}
912
913static inline void aead_request_set_crypt(struct aead_request *req,
914 struct scatterlist *src,
915 struct scatterlist *dst,
916 unsigned int cryptlen, u8 *iv)
917{
918 req->src = src;
919 req->dst = dst;
920 req->cryptlen = cryptlen;
921 req->iv = iv;
922}
923
924static inline void aead_request_set_assoc(struct aead_request *req,
925 struct scatterlist *assoc,
926 unsigned int assoclen)
927{
928 req->assoc = assoc;
929 req->assoclen = assoclen;
930}
931
Herbert Xu5cde0af2006-08-22 00:07:53 +1000932static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
933 struct crypto_tfm *tfm)
934{
935 return (struct crypto_blkcipher *)tfm;
936}
937
938static inline struct crypto_blkcipher *crypto_blkcipher_cast(
939 struct crypto_tfm *tfm)
940{
941 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
942 return __crypto_blkcipher_cast(tfm);
943}
944
945static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
946 const char *alg_name, u32 type, u32 mask)
947{
Herbert Xu332f88402007-11-15 22:36:07 +0800948 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000949 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800950 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000951
952 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
953}
954
955static inline struct crypto_tfm *crypto_blkcipher_tfm(
956 struct crypto_blkcipher *tfm)
957{
958 return &tfm->base;
959}
960
961static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
962{
963 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
964}
965
Herbert Xufce32d72006-08-26 17:35:45 +1000966static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
967{
Herbert Xu332f88402007-11-15 22:36:07 +0800968 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000969 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800970 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000971
972 return crypto_has_alg(alg_name, type, mask);
973}
974
Herbert Xu5cde0af2006-08-22 00:07:53 +1000975static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
976{
977 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
978}
979
980static inline struct blkcipher_tfm *crypto_blkcipher_crt(
981 struct crypto_blkcipher *tfm)
982{
983 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
984}
985
986static inline struct blkcipher_alg *crypto_blkcipher_alg(
987 struct crypto_blkcipher *tfm)
988{
989 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
990}
991
992static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
993{
994 return crypto_blkcipher_alg(tfm)->ivsize;
995}
996
997static inline unsigned int crypto_blkcipher_blocksize(
998 struct crypto_blkcipher *tfm)
999{
1000 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1001}
1002
1003static inline unsigned int crypto_blkcipher_alignmask(
1004 struct crypto_blkcipher *tfm)
1005{
1006 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1007}
1008
1009static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1010{
1011 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1012}
1013
1014static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1015 u32 flags)
1016{
1017 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1018}
1019
1020static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1021 u32 flags)
1022{
1023 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1024}
1025
1026static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1027 const u8 *key, unsigned int keylen)
1028{
1029 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1030 key, keylen);
1031}
1032
1033static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1034 struct scatterlist *dst,
1035 struct scatterlist *src,
1036 unsigned int nbytes)
1037{
1038 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1039 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1040}
1041
1042static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1043 struct scatterlist *dst,
1044 struct scatterlist *src,
1045 unsigned int nbytes)
1046{
1047 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1048}
1049
1050static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1051 struct scatterlist *dst,
1052 struct scatterlist *src,
1053 unsigned int nbytes)
1054{
1055 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1056 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1057}
1058
1059static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1060 struct scatterlist *dst,
1061 struct scatterlist *src,
1062 unsigned int nbytes)
1063{
1064 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1065}
1066
1067static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1068 const u8 *src, unsigned int len)
1069{
1070 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1071}
1072
1073static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1074 u8 *dst, unsigned int len)
1075{
1076 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1077}
1078
Herbert Xuf28776a2006-08-13 20:58:18 +10001079static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1080{
1081 return (struct crypto_cipher *)tfm;
1082}
1083
1084static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1085{
1086 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1087 return __crypto_cipher_cast(tfm);
1088}
1089
1090static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1091 u32 type, u32 mask)
1092{
1093 type &= ~CRYPTO_ALG_TYPE_MASK;
1094 type |= CRYPTO_ALG_TYPE_CIPHER;
1095 mask |= CRYPTO_ALG_TYPE_MASK;
1096
1097 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1098}
1099
1100static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1101{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001102 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001103}
1104
1105static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1106{
1107 crypto_free_tfm(crypto_cipher_tfm(tfm));
1108}
1109
Herbert Xufce32d72006-08-26 17:35:45 +10001110static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1111{
1112 type &= ~CRYPTO_ALG_TYPE_MASK;
1113 type |= CRYPTO_ALG_TYPE_CIPHER;
1114 mask |= CRYPTO_ALG_TYPE_MASK;
1115
1116 return crypto_has_alg(alg_name, type, mask);
1117}
1118
Herbert Xuf28776a2006-08-13 20:58:18 +10001119static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1120{
1121 return &crypto_cipher_tfm(tfm)->crt_cipher;
1122}
1123
1124static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1125{
1126 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1127}
1128
1129static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1130{
1131 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1132}
1133
1134static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1135{
1136 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1137}
1138
1139static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1140 u32 flags)
1141{
1142 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1143}
1144
1145static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1146 u32 flags)
1147{
1148 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1149}
1150
Herbert Xu7226bc872006-08-21 21:40:49 +10001151static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1152 const u8 *key, unsigned int keylen)
1153{
1154 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1155 key, keylen);
1156}
1157
Herbert Xuf28776a2006-08-13 20:58:18 +10001158static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1159 u8 *dst, const u8 *src)
1160{
1161 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1162 dst, src);
1163}
1164
1165static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1166 u8 *dst, const u8 *src)
1167{
1168 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1169 dst, src);
1170}
1171
Herbert Xu055bcee2006-08-19 22:24:23 +10001172static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Herbert Xu055bcee2006-08-19 22:24:23 +10001174 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Herbert Xu055bcee2006-08-19 22:24:23 +10001177static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Herbert Xu055bcee2006-08-19 22:24:23 +10001179 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1180 CRYPTO_ALG_TYPE_HASH_MASK);
1181 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Herbert Xu055bcee2006-08-19 22:24:23 +10001184static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1185 u32 type, u32 mask)
1186{
1187 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001188 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001189 type |= CRYPTO_ALG_TYPE_HASH;
1190 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1191
1192 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1193}
1194
1195static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1196{
1197 return &tfm->base;
1198}
1199
1200static inline void crypto_free_hash(struct crypto_hash *tfm)
1201{
1202 crypto_free_tfm(crypto_hash_tfm(tfm));
1203}
1204
Herbert Xufce32d72006-08-26 17:35:45 +10001205static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1206{
1207 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001208 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001209 type |= CRYPTO_ALG_TYPE_HASH;
1210 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1211
1212 return crypto_has_alg(alg_name, type, mask);
1213}
1214
Herbert Xu055bcee2006-08-19 22:24:23 +10001215static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1216{
1217 return &crypto_hash_tfm(tfm)->crt_hash;
1218}
1219
1220static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1221{
1222 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1223}
1224
1225static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1226{
1227 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1228}
1229
1230static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1231{
1232 return crypto_hash_crt(tfm)->digestsize;
1233}
1234
1235static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1236{
1237 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1238}
1239
1240static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1241{
1242 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1243}
1244
1245static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1246{
1247 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1248}
1249
1250static inline int crypto_hash_init(struct hash_desc *desc)
1251{
1252 return crypto_hash_crt(desc->tfm)->init(desc);
1253}
1254
1255static inline int crypto_hash_update(struct hash_desc *desc,
1256 struct scatterlist *sg,
1257 unsigned int nbytes)
1258{
1259 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1260}
1261
1262static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1263{
1264 return crypto_hash_crt(desc->tfm)->final(desc, out);
1265}
1266
1267static inline int crypto_hash_digest(struct hash_desc *desc,
1268 struct scatterlist *sg,
1269 unsigned int nbytes, u8 *out)
1270{
1271 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1272}
1273
1274static inline int crypto_hash_setkey(struct crypto_hash *hash,
1275 const u8 *key, unsigned int keylen)
1276{
1277 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Herbert Xufce32d72006-08-26 17:35:45 +10001280static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1281{
1282 return (struct crypto_comp *)tfm;
1283}
1284
1285static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1286{
1287 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1288 CRYPTO_ALG_TYPE_MASK);
1289 return __crypto_comp_cast(tfm);
1290}
1291
1292static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1293 u32 type, u32 mask)
1294{
1295 type &= ~CRYPTO_ALG_TYPE_MASK;
1296 type |= CRYPTO_ALG_TYPE_COMPRESS;
1297 mask |= CRYPTO_ALG_TYPE_MASK;
1298
1299 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1300}
1301
1302static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1303{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001304 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001305}
1306
1307static inline void crypto_free_comp(struct crypto_comp *tfm)
1308{
1309 crypto_free_tfm(crypto_comp_tfm(tfm));
1310}
1311
1312static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1313{
1314 type &= ~CRYPTO_ALG_TYPE_MASK;
1315 type |= CRYPTO_ALG_TYPE_COMPRESS;
1316 mask |= CRYPTO_ALG_TYPE_MASK;
1317
1318 return crypto_has_alg(alg_name, type, mask);
1319}
1320
Herbert Xue4d5b792006-08-26 18:12:40 +10001321static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1322{
1323 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1324}
1325
Herbert Xufce32d72006-08-26 17:35:45 +10001326static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1327{
1328 return &crypto_comp_tfm(tfm)->crt_compress;
1329}
1330
1331static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 const u8 *src, unsigned int slen,
1333 u8 *dst, unsigned int *dlen)
1334{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001335 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1336 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Herbert Xufce32d72006-08-26 17:35:45 +10001339static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 const u8 *src, unsigned int slen,
1341 u8 *dst, unsigned int *dlen)
1342{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001343 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1344 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#endif /* _LINUX_CRYPTO_H */
1348