blob: 0aba1046020167aa10df631c1407fdecb5d3de53 [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
33#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
Herbert Xu055bcee2006-08-19 22:24:23 +100034#define CRYPTO_ALG_TYPE_HASH 0x00000003
35#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
Herbert Xu332f88402007-11-15 22:36:07 +080038#define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
39#define CRYPTO_ALG_TYPE_AEAD 0x00000009
Herbert Xu055bcee2006-08-19 22:24:23 +100040
41#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080042#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Herbert Xu28259822006-08-06 21:23:26 +100044#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100045#define CRYPTO_ALG_DEAD 0x00000020
46#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100047#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
Herbert Xu60104392006-08-26 18:34:10 +100050 * Set this bit if and only if the algorithm requires another algorithm of
51 * the same type to handle corner cases.
52 */
53#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
54
55/*
Herbert Xuecfc4322007-12-05 21:08:36 +110056 * This bit is set for symmetric key ciphers that have already been wrapped
57 * with a generic IV generator to prevent them from being wrapped again.
58 */
59#define CRYPTO_ALG_GENIV 0x00000200
60
61/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Transform masks and values (for crt_flags).
63 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define CRYPTO_TFM_REQ_MASK 0x000fff00
65#define CRYPTO_TFM_RES_MASK 0xfff00000
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070068#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110069#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
71#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
72#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
73#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
74#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
75
76/*
77 * Miscellaneous stuff.
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define CRYPTO_MAX_ALG_NAME 64
80
Herbert Xu79911102006-08-21 21:03:52 +100081/*
82 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
83 * declaration) is used to ensure that the crypto_tfm context structure is
84 * aligned correctly for the given architecture so that there are no alignment
85 * faults for C data types. In particular, this is required on platforms such
86 * as arm where pointers are 32-bit aligned but there are data types such as
87 * u64 which require 64-bit alignment.
88 */
89#if defined(ARCH_KMALLOC_MINALIGN)
90#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
91#elif defined(ARCH_SLAB_MINALIGN)
92#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
93#endif
94
95#ifdef CRYPTO_MINALIGN
96#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
97#else
98#define CRYPTO_MINALIGN_ATTR
99#endif
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100102struct crypto_ablkcipher;
103struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800104struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000105struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000106struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -0700107struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000108struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800109struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800110struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700111
Herbert Xu32e39832007-03-24 14:35:34 +1100112typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
113
114struct crypto_async_request {
115 struct list_head list;
116 crypto_completion_t complete;
117 void *data;
118 struct crypto_tfm *tfm;
119
120 u32 flags;
121};
122
123struct ablkcipher_request {
124 struct crypto_async_request base;
125
126 unsigned int nbytes;
127
128 void *info;
129
130 struct scatterlist *src;
131 struct scatterlist *dst;
132
133 void *__ctx[] CRYPTO_MINALIGN_ATTR;
134};
135
Herbert Xu1ae97822007-08-30 15:36:14 +0800136/**
137 * struct aead_request - AEAD request
138 * @base: Common attributes for async crypto requests
139 * @assoclen: Length in bytes of associated data for authentication
140 * @cryptlen: Length of data to be encrypted or decrypted
141 * @iv: Initialisation vector
142 * @assoc: Associated data
143 * @src: Source data
144 * @dst: Destination data
145 * @__ctx: Start of private context data
146 */
147struct aead_request {
148 struct crypto_async_request base;
149
150 unsigned int assoclen;
151 unsigned int cryptlen;
152
153 u8 *iv;
154
155 struct scatterlist *assoc;
156 struct scatterlist *src;
157 struct scatterlist *dst;
158
159 void *__ctx[] CRYPTO_MINALIGN_ATTR;
160};
161
Herbert Xu5cde0af2006-08-22 00:07:53 +1000162struct blkcipher_desc {
163 struct crypto_blkcipher *tfm;
164 void *info;
165 u32 flags;
166};
167
Herbert Xu40725182005-07-06 13:51:52 -0700168struct cipher_desc {
169 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000170 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700171 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
172 const u8 *src, unsigned int nbytes);
173 void *info;
174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Herbert Xu055bcee2006-08-19 22:24:23 +1000176struct hash_desc {
177 struct crypto_hash *tfm;
178 u32 flags;
179};
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * Algorithms: modular crypto algorithm implementations, managed
183 * via crypto_register_alg() and crypto_unregister_alg().
184 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000185struct ablkcipher_alg {
186 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
187 unsigned int keylen);
188 int (*encrypt)(struct ablkcipher_request *req);
189 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800190 int (*givencrypt)(struct skcipher_givcrypt_request *req);
191 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000192
Herbert Xu23508e12007-11-27 21:33:24 +0800193 const char *geniv;
194
Herbert Xub5b7f082007-04-16 20:48:54 +1000195 unsigned int min_keysize;
196 unsigned int max_keysize;
197 unsigned int ivsize;
198};
199
Herbert Xu1ae97822007-08-30 15:36:14 +0800200struct aead_alg {
201 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
202 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100203 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800204 int (*encrypt)(struct aead_request *req);
205 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800206 int (*givencrypt)(struct aead_givcrypt_request *req);
207 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800208
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800209 const char *geniv;
210
Herbert Xu1ae97822007-08-30 15:36:14 +0800211 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100212 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800213};
214
Herbert Xu5cde0af2006-08-22 00:07:53 +1000215struct blkcipher_alg {
216 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
217 unsigned int keylen);
218 int (*encrypt)(struct blkcipher_desc *desc,
219 struct scatterlist *dst, struct scatterlist *src,
220 unsigned int nbytes);
221 int (*decrypt)(struct blkcipher_desc *desc,
222 struct scatterlist *dst, struct scatterlist *src,
223 unsigned int nbytes);
224
Herbert Xu23508e12007-11-27 21:33:24 +0800225 const char *geniv;
226
Herbert Xu5cde0af2006-08-22 00:07:53 +1000227 unsigned int min_keysize;
228 unsigned int max_keysize;
229 unsigned int ivsize;
230};
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232struct cipher_alg {
233 unsigned int cia_min_keysize;
234 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000235 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000236 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000237 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
238 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
241struct digest_alg {
242 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000243 void (*dia_init)(struct crypto_tfm *tfm);
244 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
245 unsigned int len);
246 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
247 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000248 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
Herbert Xu055bcee2006-08-19 22:24:23 +1000251struct hash_alg {
252 int (*init)(struct hash_desc *desc);
253 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
254 unsigned int nbytes);
255 int (*final)(struct hash_desc *desc, u8 *out);
256 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
257 unsigned int nbytes, u8 *out);
258 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
259 unsigned int keylen);
260
261 unsigned int digestsize;
262};
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000265 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
266 unsigned int slen, u8 *dst, unsigned int *dlen);
267 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
268 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Herbert Xub5b7f082007-04-16 20:48:54 +1000271#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800272#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000273#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#define cra_cipher cra_u.cipher
275#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000276#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#define cra_compress cra_u.compress
278
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;
301 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000302 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct compress_alg compress;
304 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000305
306 int (*cra_init)(struct crypto_tfm *tfm);
307 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000308 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 struct module *cra_module;
311};
312
313/*
314 * Algorithm registration interface.
315 */
316int crypto_register_alg(struct crypto_alg *alg);
317int crypto_unregister_alg(struct crypto_alg *alg);
318
319/*
320 * Algorithm query interface.
321 */
322#ifdef CONFIG_CRYPTO
Herbert Xufce32d72006-08-26 17:35:45 +1000323int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#else
Herbert Xufce32d72006-08-26 17:35:45 +1000325static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
326{
327 return 0;
328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#endif
330
331/*
332 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000333 * and core processing logic. Managed via crypto_alloc_*() and
334 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Herbert Xu32e39832007-03-24 14:35:34 +1100337struct ablkcipher_tfm {
338 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
339 unsigned int keylen);
340 int (*encrypt)(struct ablkcipher_request *req);
341 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800342 int (*givencrypt)(struct skcipher_givcrypt_request *req);
343 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
344
Herbert Xuecfc4322007-12-05 21:08:36 +1100345 struct crypto_ablkcipher *base;
346
Herbert Xu32e39832007-03-24 14:35:34 +1100347 unsigned int ivsize;
348 unsigned int reqsize;
349};
350
Herbert Xu1ae97822007-08-30 15:36:14 +0800351struct aead_tfm {
352 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
353 unsigned int keylen);
354 int (*encrypt)(struct aead_request *req);
355 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800356 int (*givencrypt)(struct aead_givcrypt_request *req);
357 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800358
359 struct crypto_aead *base;
360
Herbert Xu1ae97822007-08-30 15:36:14 +0800361 unsigned int ivsize;
362 unsigned int authsize;
363 unsigned int reqsize;
364};
365
Herbert Xu5cde0af2006-08-22 00:07:53 +1000366struct blkcipher_tfm {
367 void *iv;
368 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
369 unsigned int keylen);
370 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
371 struct scatterlist *src, unsigned int nbytes);
372 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
373 struct scatterlist *src, unsigned int nbytes);
374};
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int (*cit_setkey)(struct crypto_tfm *tfm,
378 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000379 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
380 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381};
382
Herbert Xu055bcee2006-08-19 22:24:23 +1000383struct hash_tfm {
384 int (*init)(struct hash_desc *desc);
385 int (*update)(struct hash_desc *desc,
386 struct scatterlist *sg, unsigned int nsg);
387 int (*final)(struct hash_desc *desc, u8 *out);
388 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
389 unsigned int nsg, u8 *out);
390 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
391 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000392 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393};
394
395struct compress_tfm {
396 int (*cot_compress)(struct crypto_tfm *tfm,
397 const u8 *src, unsigned int slen,
398 u8 *dst, unsigned int *dlen);
399 int (*cot_decompress)(struct crypto_tfm *tfm,
400 const u8 *src, unsigned int slen,
401 u8 *dst, unsigned int *dlen);
402};
403
Herbert Xu32e39832007-03-24 14:35:34 +1100404#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800405#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000406#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000408#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#define crt_compress crt_u.compress
410
411struct crypto_tfm {
412
413 u32 crt_flags;
414
415 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100416 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800417 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000418 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000420 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct compress_tfm compress;
422 } crt_u;
423
424 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100425
Herbert Xu79911102006-08-21 21:03:52 +1000426 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Herbert Xu32e39832007-03-24 14:35:34 +1100429struct crypto_ablkcipher {
430 struct crypto_tfm base;
431};
432
Herbert Xu1ae97822007-08-30 15:36:14 +0800433struct crypto_aead {
434 struct crypto_tfm base;
435};
436
Herbert Xu5cde0af2006-08-22 00:07:53 +1000437struct crypto_blkcipher {
438 struct crypto_tfm base;
439};
440
Herbert Xu78a1fe42006-12-24 10:02:00 +1100441struct crypto_cipher {
442 struct crypto_tfm base;
443};
444
445struct crypto_comp {
446 struct crypto_tfm base;
447};
448
Herbert Xu055bcee2006-08-19 22:24:23 +1000449struct crypto_hash {
450 struct crypto_tfm base;
451};
452
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000453enum {
454 CRYPTOA_UNSPEC,
455 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100456 CRYPTOA_TYPE,
Herbert Xu39e1ee02007-08-29 19:27:26 +0800457 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100458 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000459};
460
Herbert Xuebc610e2007-01-01 18:37:02 +1100461#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
462
Herbert Xu39e1ee02007-08-29 19:27:26 +0800463/* Maximum number of (rtattr) parameters for each template. */
464#define CRYPTO_MAX_ATTRS 32
465
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000466struct crypto_attr_alg {
467 char name[CRYPTO_MAX_ALG_NAME];
468};
469
Herbert Xuebc610e2007-01-01 18:37:02 +1100470struct crypto_attr_type {
471 u32 type;
472 u32 mask;
473};
474
Herbert Xu39e1ee02007-08-29 19:27:26 +0800475struct crypto_attr_u32 {
476 u32 num;
477};
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
480 * Transform user interface.
481 */
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000484struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485void crypto_free_tfm(struct crypto_tfm *tfm);
486
487/*
488 * Transform helpers which query the underlying algorithm.
489 */
490static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
491{
492 return tfm->__crt_alg->cra_name;
493}
494
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000495static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
496{
497 return tfm->__crt_alg->cra_driver_name;
498}
499
500static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
501{
502 return tfm->__crt_alg->cra_priority;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
506{
507 return module_name(tfm->__crt_alg->cra_module);
508}
509
510static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
511{
512 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
516{
517 return tfm->__crt_alg->cra_blocksize;
518}
519
Herbert Xufbdae9f2005-07-06 13:53:29 -0700520static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
521{
522 return tfm->__crt_alg->cra_alignmask;
523}
524
Herbert Xuf28776a2006-08-13 20:58:18 +1000525static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
526{
527 return tfm->crt_flags;
528}
529
530static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
531{
532 tfm->crt_flags |= flags;
533}
534
535static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
536{
537 tfm->crt_flags &= ~flags;
538}
539
Herbert Xu40725182005-07-06 13:51:52 -0700540static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
541{
Herbert Xuf10b7892006-01-25 22:34:01 +1100542 return tfm->__crt_ctx;
543}
544
545static inline unsigned int crypto_tfm_ctx_alignment(void)
546{
547 struct crypto_tfm *tfm;
548 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/*
552 * API wrappers.
553 */
Herbert Xu32e39832007-03-24 14:35:34 +1100554static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
555 struct crypto_tfm *tfm)
556{
557 return (struct crypto_ablkcipher *)tfm;
558}
559
Herbert Xu378f4f52007-12-17 20:07:31 +0800560static inline u32 crypto_skcipher_type(u32 type)
561{
Herbert Xuecfc4322007-12-05 21:08:36 +1100562 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800563 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
564 return type;
565}
566
567static inline u32 crypto_skcipher_mask(u32 mask)
568{
Herbert Xuecfc4322007-12-05 21:08:36 +1100569 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800570 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
571 return mask;
572}
573
Herbert Xub9c55aa2007-12-04 12:46:48 +1100574struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
575 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100576
577static inline struct crypto_tfm *crypto_ablkcipher_tfm(
578 struct crypto_ablkcipher *tfm)
579{
580 return &tfm->base;
581}
582
583static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
584{
585 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
586}
587
588static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
589 u32 mask)
590{
Herbert Xu378f4f52007-12-17 20:07:31 +0800591 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
592 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100593}
594
595static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
596 struct crypto_ablkcipher *tfm)
597{
598 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
599}
600
601static inline unsigned int crypto_ablkcipher_ivsize(
602 struct crypto_ablkcipher *tfm)
603{
604 return crypto_ablkcipher_crt(tfm)->ivsize;
605}
606
607static inline unsigned int crypto_ablkcipher_blocksize(
608 struct crypto_ablkcipher *tfm)
609{
610 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
611}
612
613static inline unsigned int crypto_ablkcipher_alignmask(
614 struct crypto_ablkcipher *tfm)
615{
616 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
617}
618
619static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
620{
621 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
622}
623
624static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
625 u32 flags)
626{
627 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
628}
629
630static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
631 u32 flags)
632{
633 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
634}
635
636static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
637 const u8 *key, unsigned int keylen)
638{
Herbert Xuecfc4322007-12-05 21:08:36 +1100639 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
640
641 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100642}
643
644static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
645 struct ablkcipher_request *req)
646{
647 return __crypto_ablkcipher_cast(req->base.tfm);
648}
649
650static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
651{
652 struct ablkcipher_tfm *crt =
653 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
654 return crt->encrypt(req);
655}
656
657static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
658{
659 struct ablkcipher_tfm *crt =
660 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
661 return crt->decrypt(req);
662}
663
Herbert Xub16c3a22007-08-29 19:02:04 +0800664static inline unsigned int crypto_ablkcipher_reqsize(
665 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100666{
667 return crypto_ablkcipher_crt(tfm)->reqsize;
668}
669
Herbert Xue196d622007-04-14 16:09:14 +1000670static inline void ablkcipher_request_set_tfm(
671 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
672{
Herbert Xuecfc4322007-12-05 21:08:36 +1100673 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000674}
675
Herbert Xub5b7f082007-04-16 20:48:54 +1000676static inline struct ablkcipher_request *ablkcipher_request_cast(
677 struct crypto_async_request *req)
678{
679 return container_of(req, struct ablkcipher_request, base);
680}
681
Herbert Xu32e39832007-03-24 14:35:34 +1100682static inline struct ablkcipher_request *ablkcipher_request_alloc(
683 struct crypto_ablkcipher *tfm, gfp_t gfp)
684{
685 struct ablkcipher_request *req;
686
687 req = kmalloc(sizeof(struct ablkcipher_request) +
688 crypto_ablkcipher_reqsize(tfm), gfp);
689
690 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000691 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100692
693 return req;
694}
695
696static inline void ablkcipher_request_free(struct ablkcipher_request *req)
697{
698 kfree(req);
699}
700
701static inline void ablkcipher_request_set_callback(
702 struct ablkcipher_request *req,
703 u32 flags, crypto_completion_t complete, void *data)
704{
705 req->base.complete = complete;
706 req->base.data = data;
707 req->base.flags = flags;
708}
709
710static inline void ablkcipher_request_set_crypt(
711 struct ablkcipher_request *req,
712 struct scatterlist *src, struct scatterlist *dst,
713 unsigned int nbytes, void *iv)
714{
715 req->src = src;
716 req->dst = dst;
717 req->nbytes = nbytes;
718 req->info = iv;
719}
720
Herbert Xu1ae97822007-08-30 15:36:14 +0800721static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
722{
723 return (struct crypto_aead *)tfm;
724}
725
Herbert Xud29ce982007-12-12 19:24:27 +0800726struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +0800727
728static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
729{
730 return &tfm->base;
731}
732
733static inline void crypto_free_aead(struct crypto_aead *tfm)
734{
735 crypto_free_tfm(crypto_aead_tfm(tfm));
736}
737
738static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
739{
740 return &crypto_aead_tfm(tfm)->crt_aead;
741}
742
743static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
744{
745 return crypto_aead_crt(tfm)->ivsize;
746}
747
748static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
749{
750 return crypto_aead_crt(tfm)->authsize;
751}
752
753static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
754{
755 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
756}
757
758static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
759{
760 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
761}
762
763static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
764{
765 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
766}
767
768static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
769{
770 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
771}
772
773static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
774{
775 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
776}
777
778static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
779 unsigned int keylen)
780{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800781 struct aead_tfm *crt = crypto_aead_crt(tfm);
782
783 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +0800784}
785
Herbert Xu7ba683a2007-12-02 18:49:21 +1100786int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
787
Herbert Xu1ae97822007-08-30 15:36:14 +0800788static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
789{
790 return __crypto_aead_cast(req->base.tfm);
791}
792
793static inline int crypto_aead_encrypt(struct aead_request *req)
794{
795 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
796}
797
798static inline int crypto_aead_decrypt(struct aead_request *req)
799{
800 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
801}
802
Herbert Xub16c3a22007-08-29 19:02:04 +0800803static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800804{
805 return crypto_aead_crt(tfm)->reqsize;
806}
807
808static inline void aead_request_set_tfm(struct aead_request *req,
809 struct crypto_aead *tfm)
810{
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800811 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800812}
813
814static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
815 gfp_t gfp)
816{
817 struct aead_request *req;
818
819 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
820
821 if (likely(req))
822 aead_request_set_tfm(req, tfm);
823
824 return req;
825}
826
827static inline void aead_request_free(struct aead_request *req)
828{
829 kfree(req);
830}
831
832static inline void aead_request_set_callback(struct aead_request *req,
833 u32 flags,
834 crypto_completion_t complete,
835 void *data)
836{
837 req->base.complete = complete;
838 req->base.data = data;
839 req->base.flags = flags;
840}
841
842static inline void aead_request_set_crypt(struct aead_request *req,
843 struct scatterlist *src,
844 struct scatterlist *dst,
845 unsigned int cryptlen, u8 *iv)
846{
847 req->src = src;
848 req->dst = dst;
849 req->cryptlen = cryptlen;
850 req->iv = iv;
851}
852
853static inline void aead_request_set_assoc(struct aead_request *req,
854 struct scatterlist *assoc,
855 unsigned int assoclen)
856{
857 req->assoc = assoc;
858 req->assoclen = assoclen;
859}
860
Herbert Xu5cde0af2006-08-22 00:07:53 +1000861static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
862 struct crypto_tfm *tfm)
863{
864 return (struct crypto_blkcipher *)tfm;
865}
866
867static inline struct crypto_blkcipher *crypto_blkcipher_cast(
868 struct crypto_tfm *tfm)
869{
870 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
871 return __crypto_blkcipher_cast(tfm);
872}
873
874static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
875 const char *alg_name, u32 type, u32 mask)
876{
Herbert Xu332f88402007-11-15 22:36:07 +0800877 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000878 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800879 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000880
881 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
882}
883
884static inline struct crypto_tfm *crypto_blkcipher_tfm(
885 struct crypto_blkcipher *tfm)
886{
887 return &tfm->base;
888}
889
890static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
891{
892 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
893}
894
Herbert Xufce32d72006-08-26 17:35:45 +1000895static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
896{
Herbert Xu332f88402007-11-15 22:36:07 +0800897 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000898 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800899 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000900
901 return crypto_has_alg(alg_name, type, mask);
902}
903
Herbert Xu5cde0af2006-08-22 00:07:53 +1000904static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
905{
906 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
907}
908
909static inline struct blkcipher_tfm *crypto_blkcipher_crt(
910 struct crypto_blkcipher *tfm)
911{
912 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
913}
914
915static inline struct blkcipher_alg *crypto_blkcipher_alg(
916 struct crypto_blkcipher *tfm)
917{
918 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
919}
920
921static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
922{
923 return crypto_blkcipher_alg(tfm)->ivsize;
924}
925
926static inline unsigned int crypto_blkcipher_blocksize(
927 struct crypto_blkcipher *tfm)
928{
929 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
930}
931
932static inline unsigned int crypto_blkcipher_alignmask(
933 struct crypto_blkcipher *tfm)
934{
935 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
936}
937
938static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
939{
940 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
941}
942
943static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
944 u32 flags)
945{
946 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
947}
948
949static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
950 u32 flags)
951{
952 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
953}
954
955static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
956 const u8 *key, unsigned int keylen)
957{
958 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
959 key, keylen);
960}
961
962static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
963 struct scatterlist *dst,
964 struct scatterlist *src,
965 unsigned int nbytes)
966{
967 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
968 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
969}
970
971static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
972 struct scatterlist *dst,
973 struct scatterlist *src,
974 unsigned int nbytes)
975{
976 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
977}
978
979static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
980 struct scatterlist *dst,
981 struct scatterlist *src,
982 unsigned int nbytes)
983{
984 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
985 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
986}
987
988static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
989 struct scatterlist *dst,
990 struct scatterlist *src,
991 unsigned int nbytes)
992{
993 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
994}
995
996static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
997 const u8 *src, unsigned int len)
998{
999 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1000}
1001
1002static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1003 u8 *dst, unsigned int len)
1004{
1005 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1006}
1007
Herbert Xuf28776a2006-08-13 20:58:18 +10001008static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1009{
1010 return (struct crypto_cipher *)tfm;
1011}
1012
1013static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1014{
1015 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1016 return __crypto_cipher_cast(tfm);
1017}
1018
1019static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1020 u32 type, u32 mask)
1021{
1022 type &= ~CRYPTO_ALG_TYPE_MASK;
1023 type |= CRYPTO_ALG_TYPE_CIPHER;
1024 mask |= CRYPTO_ALG_TYPE_MASK;
1025
1026 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1027}
1028
1029static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1030{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001031 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001032}
1033
1034static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1035{
1036 crypto_free_tfm(crypto_cipher_tfm(tfm));
1037}
1038
Herbert Xufce32d72006-08-26 17:35:45 +10001039static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1040{
1041 type &= ~CRYPTO_ALG_TYPE_MASK;
1042 type |= CRYPTO_ALG_TYPE_CIPHER;
1043 mask |= CRYPTO_ALG_TYPE_MASK;
1044
1045 return crypto_has_alg(alg_name, type, mask);
1046}
1047
Herbert Xuf28776a2006-08-13 20:58:18 +10001048static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1049{
1050 return &crypto_cipher_tfm(tfm)->crt_cipher;
1051}
1052
1053static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1054{
1055 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1056}
1057
1058static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1059{
1060 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1061}
1062
1063static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1064{
1065 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1066}
1067
1068static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1069 u32 flags)
1070{
1071 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1072}
1073
1074static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1075 u32 flags)
1076{
1077 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1078}
1079
Herbert Xu7226bc82006-08-21 21:40:49 +10001080static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1081 const u8 *key, unsigned int keylen)
1082{
1083 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1084 key, keylen);
1085}
1086
Herbert Xuf28776a2006-08-13 20:58:18 +10001087static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1088 u8 *dst, const u8 *src)
1089{
1090 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1091 dst, src);
1092}
1093
1094static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1095 u8 *dst, const u8 *src)
1096{
1097 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1098 dst, src);
1099}
1100
Herbert Xu055bcee2006-08-19 22:24:23 +10001101static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Herbert Xu055bcee2006-08-19 22:24:23 +10001103 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Herbert Xu055bcee2006-08-19 22:24:23 +10001106static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Herbert Xu055bcee2006-08-19 22:24:23 +10001108 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1109 CRYPTO_ALG_TYPE_HASH_MASK);
1110 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Herbert Xu055bcee2006-08-19 22:24:23 +10001113static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1114 u32 type, u32 mask)
1115{
1116 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001117 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001118 type |= CRYPTO_ALG_TYPE_HASH;
1119 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1120
1121 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1122}
1123
1124static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1125{
1126 return &tfm->base;
1127}
1128
1129static inline void crypto_free_hash(struct crypto_hash *tfm)
1130{
1131 crypto_free_tfm(crypto_hash_tfm(tfm));
1132}
1133
Herbert Xufce32d72006-08-26 17:35:45 +10001134static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1135{
1136 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001137 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001138 type |= CRYPTO_ALG_TYPE_HASH;
1139 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1140
1141 return crypto_has_alg(alg_name, type, mask);
1142}
1143
Herbert Xu055bcee2006-08-19 22:24:23 +10001144static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1145{
1146 return &crypto_hash_tfm(tfm)->crt_hash;
1147}
1148
1149static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1150{
1151 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1152}
1153
1154static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1155{
1156 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1157}
1158
1159static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1160{
1161 return crypto_hash_crt(tfm)->digestsize;
1162}
1163
1164static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1165{
1166 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1167}
1168
1169static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1170{
1171 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1172}
1173
1174static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1175{
1176 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1177}
1178
1179static inline int crypto_hash_init(struct hash_desc *desc)
1180{
1181 return crypto_hash_crt(desc->tfm)->init(desc);
1182}
1183
1184static inline int crypto_hash_update(struct hash_desc *desc,
1185 struct scatterlist *sg,
1186 unsigned int nbytes)
1187{
1188 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1189}
1190
1191static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1192{
1193 return crypto_hash_crt(desc->tfm)->final(desc, out);
1194}
1195
1196static inline int crypto_hash_digest(struct hash_desc *desc,
1197 struct scatterlist *sg,
1198 unsigned int nbytes, u8 *out)
1199{
1200 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1201}
1202
1203static inline int crypto_hash_setkey(struct crypto_hash *hash,
1204 const u8 *key, unsigned int keylen)
1205{
1206 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Herbert Xufce32d72006-08-26 17:35:45 +10001209static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1210{
1211 return (struct crypto_comp *)tfm;
1212}
1213
1214static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1215{
1216 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1217 CRYPTO_ALG_TYPE_MASK);
1218 return __crypto_comp_cast(tfm);
1219}
1220
1221static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1222 u32 type, u32 mask)
1223{
1224 type &= ~CRYPTO_ALG_TYPE_MASK;
1225 type |= CRYPTO_ALG_TYPE_COMPRESS;
1226 mask |= CRYPTO_ALG_TYPE_MASK;
1227
1228 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1229}
1230
1231static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1232{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001233 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001234}
1235
1236static inline void crypto_free_comp(struct crypto_comp *tfm)
1237{
1238 crypto_free_tfm(crypto_comp_tfm(tfm));
1239}
1240
1241static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1242{
1243 type &= ~CRYPTO_ALG_TYPE_MASK;
1244 type |= CRYPTO_ALG_TYPE_COMPRESS;
1245 mask |= CRYPTO_ALG_TYPE_MASK;
1246
1247 return crypto_has_alg(alg_name, type, mask);
1248}
1249
Herbert Xue4d5b792006-08-26 18:12:40 +10001250static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1251{
1252 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1253}
1254
Herbert Xufce32d72006-08-26 17:35:45 +10001255static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1256{
1257 return &crypto_comp_tfm(tfm)->crt_compress;
1258}
1259
1260static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 const u8 *src, unsigned int slen,
1262 u8 *dst, unsigned int *dlen)
1263{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001264 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1265 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Herbert Xufce32d72006-08-26 17:35:45 +10001268static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 const u8 *src, unsigned int slen,
1270 u8 *dst, unsigned int *dlen)
1271{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001272 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1273 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276#endif /* _LINUX_CRYPTO_H */
1277