Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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) | |
5cb1454b | 6 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4 LT |
7 | * |
8 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> | |
18735dd8 | 9 | * and Nettle, by Niels Möller. |
1da177e4 LT |
10 | * |
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 | ||
60063497 | 20 | #include <linux/atomic.h> |
1da177e4 | 21 | #include <linux/kernel.h> |
1da177e4 | 22 | #include <linux/list.h> |
79911102 | 23 | #include <linux/slab.h> |
1da177e4 | 24 | #include <linux/string.h> |
79911102 | 25 | #include <linux/uaccess.h> |
1da177e4 LT |
26 | |
27 | /* | |
28 | * Algorithm masks and types. | |
29 | */ | |
2825982d | 30 | #define CRYPTO_ALG_TYPE_MASK 0x0000000f |
1da177e4 | 31 | #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 |
004a403c LH |
32 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 |
33 | #define CRYPTO_ALG_TYPE_AEAD 0x00000003 | |
055bcee3 | 34 | #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004 |
332f8840 | 35 | #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005 |
61da88e2 | 36 | #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006 |
004a403c | 37 | #define CRYPTO_ALG_TYPE_DIGEST 0x00000008 |
5f7082ed HX |
38 | #define CRYPTO_ALG_TYPE_HASH 0x00000008 |
39 | #define CRYPTO_ALG_TYPE_SHASH 0x00000009 | |
004a403c | 40 | #define CRYPTO_ALG_TYPE_AHASH 0x0000000a |
17f0f4a4 | 41 | #define CRYPTO_ALG_TYPE_RNG 0x0000000c |
a1d2f095 | 42 | #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f |
055bcee3 HX |
43 | |
44 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e | |
004a403c | 45 | #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c |
332f8840 | 46 | #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c |
1da177e4 | 47 | |
2825982d | 48 | #define CRYPTO_ALG_LARVAL 0x00000010 |
6bfd4809 HX |
49 | #define CRYPTO_ALG_DEAD 0x00000020 |
50 | #define CRYPTO_ALG_DYING 0x00000040 | |
f3f632d6 | 51 | #define CRYPTO_ALG_ASYNC 0x00000080 |
2825982d | 52 | |
6010439f HX |
53 | /* |
54 | * 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 | ||
ecfc4329 HX |
59 | /* |
60 | * 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 | ||
73d3864a HX |
65 | /* |
66 | * 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 | ||
64a947b1 SK |
73 | /* |
74 | * Set if the algorithm is an instance that is build from templates. | |
75 | */ | |
76 | #define CRYPTO_ALG_INSTANCE 0x00000800 | |
77 | ||
d912bb76 NM |
78 | /* Set this bit if the algorithm provided is hardware accelerated but |
79 | * not available to userspace via instruction set or so. | |
80 | */ | |
81 | #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 | |
82 | ||
1da177e4 LT |
83 | /* |
84 | * Transform masks and values (for crt_flags). | |
85 | */ | |
1da177e4 LT |
86 | #define CRYPTO_TFM_REQ_MASK 0x000fff00 |
87 | #define CRYPTO_TFM_RES_MASK 0xfff00000 | |
88 | ||
1da177e4 | 89 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 |
64baf3cf | 90 | #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 |
32e3983f | 91 | #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 |
1da177e4 LT |
92 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 |
93 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 | |
94 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 | |
95 | #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 | |
96 | #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 | |
97 | ||
98 | /* | |
99 | * Miscellaneous stuff. | |
100 | */ | |
1da177e4 LT |
101 | #define CRYPTO_MAX_ALG_NAME 64 |
102 | ||
79911102 HX |
103 | /* |
104 | * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual | |
105 | * declaration) is used to ensure that the crypto_tfm context structure is | |
106 | * aligned correctly for the given architecture so that there are no alignment | |
107 | * faults for C data types. In particular, this is required on platforms such | |
108 | * as arm where pointers are 32-bit aligned but there are data types such as | |
109 | * u64 which require 64-bit alignment. | |
110 | */ | |
79911102 | 111 | #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN |
79911102 | 112 | |
79911102 | 113 | #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) |
79911102 | 114 | |
1da177e4 | 115 | struct scatterlist; |
32e3983f HX |
116 | struct crypto_ablkcipher; |
117 | struct crypto_async_request; | |
1ae97820 | 118 | struct crypto_aead; |
5cde0af2 | 119 | struct crypto_blkcipher; |
055bcee3 | 120 | struct crypto_hash; |
17f0f4a4 | 121 | struct crypto_rng; |
40725181 | 122 | struct crypto_tfm; |
e853c3cf | 123 | struct crypto_type; |
743edf57 | 124 | struct aead_givcrypt_request; |
61da88e2 | 125 | struct skcipher_givcrypt_request; |
40725181 | 126 | |
32e3983f HX |
127 | typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); |
128 | ||
129 | struct crypto_async_request { | |
130 | struct list_head list; | |
131 | crypto_completion_t complete; | |
132 | void *data; | |
133 | struct crypto_tfm *tfm; | |
134 | ||
135 | u32 flags; | |
136 | }; | |
137 | ||
138 | struct ablkcipher_request { | |
139 | struct crypto_async_request base; | |
140 | ||
141 | unsigned int nbytes; | |
142 | ||
143 | void *info; | |
144 | ||
145 | struct scatterlist *src; | |
146 | struct scatterlist *dst; | |
147 | ||
148 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | |
149 | }; | |
150 | ||
1ae97820 HX |
151 | /** |
152 | * struct aead_request - AEAD request | |
153 | * @base: Common attributes for async crypto requests | |
154 | * @assoclen: Length in bytes of associated data for authentication | |
155 | * @cryptlen: Length of data to be encrypted or decrypted | |
156 | * @iv: Initialisation vector | |
157 | * @assoc: Associated data | |
158 | * @src: Source data | |
159 | * @dst: Destination data | |
160 | * @__ctx: Start of private context data | |
161 | */ | |
162 | struct aead_request { | |
163 | struct crypto_async_request base; | |
164 | ||
165 | unsigned int assoclen; | |
166 | unsigned int cryptlen; | |
167 | ||
168 | u8 *iv; | |
169 | ||
170 | struct scatterlist *assoc; | |
171 | struct scatterlist *src; | |
172 | struct scatterlist *dst; | |
173 | ||
174 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | |
175 | }; | |
176 | ||
5cde0af2 HX |
177 | struct blkcipher_desc { |
178 | struct crypto_blkcipher *tfm; | |
179 | void *info; | |
180 | u32 flags; | |
181 | }; | |
182 | ||
40725181 HX |
183 | struct cipher_desc { |
184 | struct crypto_tfm *tfm; | |
6c2bb98b | 185 | void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
40725181 HX |
186 | unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, |
187 | const u8 *src, unsigned int nbytes); | |
188 | void *info; | |
189 | }; | |
1da177e4 | 190 | |
055bcee3 HX |
191 | struct hash_desc { |
192 | struct crypto_hash *tfm; | |
193 | u32 flags; | |
194 | }; | |
195 | ||
1da177e4 LT |
196 | /* |
197 | * Algorithms: modular crypto algorithm implementations, managed | |
198 | * via crypto_register_alg() and crypto_unregister_alg(). | |
199 | */ | |
b5b7f088 HX |
200 | struct ablkcipher_alg { |
201 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | |
202 | unsigned int keylen); | |
203 | int (*encrypt)(struct ablkcipher_request *req); | |
204 | int (*decrypt)(struct ablkcipher_request *req); | |
61da88e2 HX |
205 | int (*givencrypt)(struct skcipher_givcrypt_request *req); |
206 | int (*givdecrypt)(struct skcipher_givcrypt_request *req); | |
b5b7f088 | 207 | |
23508e11 HX |
208 | const char *geniv; |
209 | ||
b5b7f088 HX |
210 | unsigned int min_keysize; |
211 | unsigned int max_keysize; | |
212 | unsigned int ivsize; | |
213 | }; | |
214 | ||
1ae97820 HX |
215 | struct aead_alg { |
216 | int (*setkey)(struct crypto_aead *tfm, const u8 *key, | |
217 | unsigned int keylen); | |
7ba683a6 | 218 | int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize); |
1ae97820 HX |
219 | int (*encrypt)(struct aead_request *req); |
220 | int (*decrypt)(struct aead_request *req); | |
743edf57 HX |
221 | int (*givencrypt)(struct aead_givcrypt_request *req); |
222 | int (*givdecrypt)(struct aead_givcrypt_request *req); | |
1ae97820 | 223 | |
5b6d2d7f HX |
224 | const char *geniv; |
225 | ||
1ae97820 | 226 | unsigned int ivsize; |
7ba683a6 | 227 | unsigned int maxauthsize; |
1ae97820 HX |
228 | }; |
229 | ||
5cde0af2 HX |
230 | struct blkcipher_alg { |
231 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | |
232 | unsigned int keylen); | |
233 | int (*encrypt)(struct blkcipher_desc *desc, | |
234 | struct scatterlist *dst, struct scatterlist *src, | |
235 | unsigned int nbytes); | |
236 | int (*decrypt)(struct blkcipher_desc *desc, | |
237 | struct scatterlist *dst, struct scatterlist *src, | |
238 | unsigned int nbytes); | |
239 | ||
23508e11 HX |
240 | const char *geniv; |
241 | ||
5cde0af2 HX |
242 | unsigned int min_keysize; |
243 | unsigned int max_keysize; | |
244 | unsigned int ivsize; | |
245 | }; | |
246 | ||
1da177e4 LT |
247 | struct cipher_alg { |
248 | unsigned int cia_min_keysize; | |
249 | unsigned int cia_max_keysize; | |
6c2bb98b | 250 | int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, |
560c06ae | 251 | unsigned int keylen); |
6c2bb98b HX |
252 | void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
253 | void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); | |
1da177e4 LT |
254 | }; |
255 | ||
1da177e4 | 256 | struct compress_alg { |
6c2bb98b HX |
257 | int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, |
258 | unsigned int slen, u8 *dst, unsigned int *dlen); | |
259 | int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, | |
260 | unsigned int slen, u8 *dst, unsigned int *dlen); | |
1da177e4 LT |
261 | }; |
262 | ||
17f0f4a4 NH |
263 | struct rng_alg { |
264 | int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata, | |
265 | unsigned int dlen); | |
266 | int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen); | |
267 | ||
268 | unsigned int seedsize; | |
269 | }; | |
270 | ||
271 | ||
b5b7f088 | 272 | #define cra_ablkcipher cra_u.ablkcipher |
1ae97820 | 273 | #define cra_aead cra_u.aead |
5cde0af2 | 274 | #define cra_blkcipher cra_u.blkcipher |
1da177e4 | 275 | #define cra_cipher cra_u.cipher |
1da177e4 | 276 | #define cra_compress cra_u.compress |
17f0f4a4 | 277 | #define cra_rng cra_u.rng |
1da177e4 LT |
278 | |
279 | struct crypto_alg { | |
280 | struct list_head cra_list; | |
6bfd4809 HX |
281 | struct list_head cra_users; |
282 | ||
1da177e4 LT |
283 | u32 cra_flags; |
284 | unsigned int cra_blocksize; | |
285 | unsigned int cra_ctxsize; | |
95477377 | 286 | unsigned int cra_alignmask; |
5cb1454b HX |
287 | |
288 | int cra_priority; | |
6521f302 | 289 | atomic_t cra_refcnt; |
5cb1454b | 290 | |
d913ea0d HX |
291 | char cra_name[CRYPTO_MAX_ALG_NAME]; |
292 | char cra_driver_name[CRYPTO_MAX_ALG_NAME]; | |
1da177e4 | 293 | |
e853c3cf HX |
294 | const struct crypto_type *cra_type; |
295 | ||
1da177e4 | 296 | union { |
b5b7f088 | 297 | struct ablkcipher_alg ablkcipher; |
1ae97820 | 298 | struct aead_alg aead; |
5cde0af2 | 299 | struct blkcipher_alg blkcipher; |
1da177e4 | 300 | struct cipher_alg cipher; |
1da177e4 | 301 | struct compress_alg compress; |
17f0f4a4 | 302 | struct rng_alg rng; |
1da177e4 | 303 | } cra_u; |
c7fc0599 HX |
304 | |
305 | int (*cra_init)(struct crypto_tfm *tfm); | |
306 | void (*cra_exit)(struct crypto_tfm *tfm); | |
6521f302 | 307 | void (*cra_destroy)(struct crypto_alg *alg); |
1da177e4 LT |
308 | |
309 | struct module *cra_module; | |
310 | }; | |
311 | ||
312 | /* | |
313 | * Algorithm registration interface. | |
314 | */ | |
315 | int crypto_register_alg(struct crypto_alg *alg); | |
316 | int crypto_unregister_alg(struct crypto_alg *alg); | |
317 | ||
318 | /* | |
319 | * Algorithm query interface. | |
320 | */ | |
fce32d70 | 321 | int crypto_has_alg(const char *name, u32 type, u32 mask); |
1da177e4 LT |
322 | |
323 | /* | |
324 | * Transforms: user-instantiated objects which encapsulate algorithms | |
6d7d684d HX |
325 | * and core processing logic. Managed via crypto_alloc_*() and |
326 | * crypto_free_*(), as well as the various helpers below. | |
1da177e4 | 327 | */ |
1da177e4 | 328 | |
32e3983f HX |
329 | struct ablkcipher_tfm { |
330 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | |
331 | unsigned int keylen); | |
332 | int (*encrypt)(struct ablkcipher_request *req); | |
333 | int (*decrypt)(struct ablkcipher_request *req); | |
61da88e2 HX |
334 | int (*givencrypt)(struct skcipher_givcrypt_request *req); |
335 | int (*givdecrypt)(struct skcipher_givcrypt_request *req); | |
336 | ||
ecfc4329 HX |
337 | struct crypto_ablkcipher *base; |
338 | ||
32e3983f HX |
339 | unsigned int ivsize; |
340 | unsigned int reqsize; | |
341 | }; | |
342 | ||
1ae97820 HX |
343 | struct aead_tfm { |
344 | int (*setkey)(struct crypto_aead *tfm, const u8 *key, | |
345 | unsigned int keylen); | |
346 | int (*encrypt)(struct aead_request *req); | |
347 | int (*decrypt)(struct aead_request *req); | |
743edf57 HX |
348 | int (*givencrypt)(struct aead_givcrypt_request *req); |
349 | int (*givdecrypt)(struct aead_givcrypt_request *req); | |
5b6d2d7f HX |
350 | |
351 | struct crypto_aead *base; | |
352 | ||
1ae97820 HX |
353 | unsigned int ivsize; |
354 | unsigned int authsize; | |
355 | unsigned int reqsize; | |
356 | }; | |
357 | ||
5cde0af2 HX |
358 | struct blkcipher_tfm { |
359 | void *iv; | |
360 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | |
361 | unsigned int keylen); | |
362 | int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, | |
363 | struct scatterlist *src, unsigned int nbytes); | |
364 | int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, | |
365 | struct scatterlist *src, unsigned int nbytes); | |
366 | }; | |
367 | ||
1da177e4 | 368 | struct cipher_tfm { |
1da177e4 LT |
369 | int (*cit_setkey)(struct crypto_tfm *tfm, |
370 | const u8 *key, unsigned int keylen); | |
f28776a3 HX |
371 | void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
372 | void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); | |
1da177e4 LT |
373 | }; |
374 | ||
055bcee3 HX |
375 | struct hash_tfm { |
376 | int (*init)(struct hash_desc *desc); | |
377 | int (*update)(struct hash_desc *desc, | |
378 | struct scatterlist *sg, unsigned int nsg); | |
379 | int (*final)(struct hash_desc *desc, u8 *out); | |
380 | int (*digest)(struct hash_desc *desc, struct scatterlist *sg, | |
381 | unsigned int nsg, u8 *out); | |
382 | int (*setkey)(struct crypto_hash *tfm, const u8 *key, | |
383 | unsigned int keylen); | |
055bcee3 | 384 | unsigned int digestsize; |
1da177e4 LT |
385 | }; |
386 | ||
387 | struct compress_tfm { | |
388 | int (*cot_compress)(struct crypto_tfm *tfm, | |
389 | const u8 *src, unsigned int slen, | |
390 | u8 *dst, unsigned int *dlen); | |
391 | int (*cot_decompress)(struct crypto_tfm *tfm, | |
392 | const u8 *src, unsigned int slen, | |
393 | u8 *dst, unsigned int *dlen); | |
394 | }; | |
395 | ||
17f0f4a4 NH |
396 | struct rng_tfm { |
397 | int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata, | |
398 | unsigned int dlen); | |
399 | int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen); | |
400 | }; | |
401 | ||
32e3983f | 402 | #define crt_ablkcipher crt_u.ablkcipher |
1ae97820 | 403 | #define crt_aead crt_u.aead |
5cde0af2 | 404 | #define crt_blkcipher crt_u.blkcipher |
1da177e4 | 405 | #define crt_cipher crt_u.cipher |
055bcee3 | 406 | #define crt_hash crt_u.hash |
1da177e4 | 407 | #define crt_compress crt_u.compress |
17f0f4a4 | 408 | #define crt_rng crt_u.rng |
1da177e4 LT |
409 | |
410 | struct crypto_tfm { | |
411 | ||
412 | u32 crt_flags; | |
413 | ||
414 | union { | |
32e3983f | 415 | struct ablkcipher_tfm ablkcipher; |
1ae97820 | 416 | struct aead_tfm aead; |
5cde0af2 | 417 | struct blkcipher_tfm blkcipher; |
1da177e4 | 418 | struct cipher_tfm cipher; |
055bcee3 | 419 | struct hash_tfm hash; |
1da177e4 | 420 | struct compress_tfm compress; |
17f0f4a4 | 421 | struct rng_tfm rng; |
1da177e4 | 422 | } crt_u; |
4a779486 HX |
423 | |
424 | void (*exit)(struct crypto_tfm *tfm); | |
1da177e4 LT |
425 | |
426 | struct crypto_alg *__crt_alg; | |
f10b7897 | 427 | |
79911102 | 428 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; |
1da177e4 LT |
429 | }; |
430 | ||
32e3983f HX |
431 | struct crypto_ablkcipher { |
432 | struct crypto_tfm base; | |
433 | }; | |
434 | ||
1ae97820 HX |
435 | struct crypto_aead { |
436 | struct crypto_tfm base; | |
437 | }; | |
438 | ||
5cde0af2 HX |
439 | struct crypto_blkcipher { |
440 | struct crypto_tfm base; | |
441 | }; | |
442 | ||
78a1fe4f HX |
443 | struct crypto_cipher { |
444 | struct crypto_tfm base; | |
445 | }; | |
446 | ||
447 | struct crypto_comp { | |
448 | struct crypto_tfm base; | |
449 | }; | |
450 | ||
055bcee3 HX |
451 | struct crypto_hash { |
452 | struct crypto_tfm base; | |
453 | }; | |
454 | ||
17f0f4a4 NH |
455 | struct crypto_rng { |
456 | struct crypto_tfm base; | |
457 | }; | |
458 | ||
2b8c19db HX |
459 | enum { |
460 | CRYPTOA_UNSPEC, | |
461 | CRYPTOA_ALG, | |
ebc610e5 | 462 | CRYPTOA_TYPE, |
39e1ee01 | 463 | CRYPTOA_U32, |
ebc610e5 | 464 | __CRYPTOA_MAX, |
2b8c19db HX |
465 | }; |
466 | ||
ebc610e5 HX |
467 | #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) |
468 | ||
39e1ee01 HX |
469 | /* Maximum number of (rtattr) parameters for each template. */ |
470 | #define CRYPTO_MAX_ATTRS 32 | |
471 | ||
2b8c19db HX |
472 | struct crypto_attr_alg { |
473 | char name[CRYPTO_MAX_ALG_NAME]; | |
474 | }; | |
475 | ||
ebc610e5 HX |
476 | struct crypto_attr_type { |
477 | u32 type; | |
478 | u32 mask; | |
479 | }; | |
480 | ||
39e1ee01 HX |
481 | struct crypto_attr_u32 { |
482 | u32 num; | |
483 | }; | |
484 | ||
1da177e4 LT |
485 | /* |
486 | * Transform user interface. | |
487 | */ | |
488 | ||
6d7d684d | 489 | struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); |
7b2cd92a HX |
490 | void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); |
491 | ||
492 | static inline void crypto_free_tfm(struct crypto_tfm *tfm) | |
493 | { | |
494 | return crypto_destroy_tfm(tfm, tfm); | |
495 | } | |
1da177e4 | 496 | |
da7f033d HX |
497 | int alg_test(const char *driver, const char *alg, u32 type, u32 mask); |
498 | ||
1da177e4 LT |
499 | /* |
500 | * Transform helpers which query the underlying algorithm. | |
501 | */ | |
502 | static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) | |
503 | { | |
504 | return tfm->__crt_alg->cra_name; | |
505 | } | |
506 | ||
b14cdd67 ML |
507 | static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) |
508 | { | |
509 | return tfm->__crt_alg->cra_driver_name; | |
510 | } | |
511 | ||
512 | static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) | |
513 | { | |
514 | return tfm->__crt_alg->cra_priority; | |
515 | } | |
516 | ||
1da177e4 LT |
517 | static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) |
518 | { | |
519 | return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; | |
520 | } | |
521 | ||
1da177e4 LT |
522 | static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) |
523 | { | |
524 | return tfm->__crt_alg->cra_blocksize; | |
525 | } | |
526 | ||
fbdae9f3 HX |
527 | static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) |
528 | { | |
529 | return tfm->__crt_alg->cra_alignmask; | |
530 | } | |
531 | ||
f28776a3 HX |
532 | static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) |
533 | { | |
534 | return tfm->crt_flags; | |
535 | } | |
536 | ||
537 | static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) | |
538 | { | |
539 | tfm->crt_flags |= flags; | |
540 | } | |
541 | ||
542 | static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) | |
543 | { | |
544 | tfm->crt_flags &= ~flags; | |
545 | } | |
546 | ||
40725181 HX |
547 | static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) |
548 | { | |
f10b7897 HX |
549 | return tfm->__crt_ctx; |
550 | } | |
551 | ||
552 | static inline unsigned int crypto_tfm_ctx_alignment(void) | |
553 | { | |
554 | struct crypto_tfm *tfm; | |
555 | return __alignof__(tfm->__crt_ctx); | |
40725181 HX |
556 | } |
557 | ||
1da177e4 LT |
558 | /* |
559 | * API wrappers. | |
560 | */ | |
32e3983f HX |
561 | static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( |
562 | struct crypto_tfm *tfm) | |
563 | { | |
564 | return (struct crypto_ablkcipher *)tfm; | |
565 | } | |
566 | ||
378f4f51 | 567 | static inline u32 crypto_skcipher_type(u32 type) |
32e3983f | 568 | { |
ecfc4329 | 569 | type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
32e3983f | 570 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
378f4f51 HX |
571 | return type; |
572 | } | |
573 | ||
574 | static inline u32 crypto_skcipher_mask(u32 mask) | |
575 | { | |
ecfc4329 | 576 | mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
332f8840 | 577 | mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; |
378f4f51 HX |
578 | return mask; |
579 | } | |
32e3983f | 580 | |
b9c55aa4 HX |
581 | struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name, |
582 | u32 type, u32 mask); | |
32e3983f HX |
583 | |
584 | static inline struct crypto_tfm *crypto_ablkcipher_tfm( | |
585 | struct crypto_ablkcipher *tfm) | |
586 | { | |
587 | return &tfm->base; | |
588 | } | |
589 | ||
590 | static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) | |
591 | { | |
592 | crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); | |
593 | } | |
594 | ||
595 | static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, | |
596 | u32 mask) | |
597 | { | |
378f4f51 HX |
598 | return crypto_has_alg(alg_name, crypto_skcipher_type(type), |
599 | crypto_skcipher_mask(mask)); | |
32e3983f HX |
600 | } |
601 | ||
602 | static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( | |
603 | struct crypto_ablkcipher *tfm) | |
604 | { | |
605 | return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; | |
606 | } | |
607 | ||
608 | static inline unsigned int crypto_ablkcipher_ivsize( | |
609 | struct crypto_ablkcipher *tfm) | |
610 | { | |
611 | return crypto_ablkcipher_crt(tfm)->ivsize; | |
612 | } | |
613 | ||
614 | static inline unsigned int crypto_ablkcipher_blocksize( | |
615 | struct crypto_ablkcipher *tfm) | |
616 | { | |
617 | return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); | |
618 | } | |
619 | ||
620 | static inline unsigned int crypto_ablkcipher_alignmask( | |
621 | struct crypto_ablkcipher *tfm) | |
622 | { | |
623 | return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); | |
624 | } | |
625 | ||
626 | static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) | |
627 | { | |
628 | return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); | |
629 | } | |
630 | ||
631 | static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, | |
632 | u32 flags) | |
633 | { | |
634 | crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); | |
635 | } | |
636 | ||
637 | static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, | |
638 | u32 flags) | |
639 | { | |
640 | crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); | |
641 | } | |
642 | ||
643 | static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, | |
644 | const u8 *key, unsigned int keylen) | |
645 | { | |
ecfc4329 HX |
646 | struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); |
647 | ||
648 | return crt->setkey(crt->base, key, keylen); | |
32e3983f HX |
649 | } |
650 | ||
651 | static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( | |
652 | struct ablkcipher_request *req) | |
653 | { | |
654 | return __crypto_ablkcipher_cast(req->base.tfm); | |
655 | } | |
656 | ||
657 | static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) | |
658 | { | |
659 | struct ablkcipher_tfm *crt = | |
660 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); | |
661 | return crt->encrypt(req); | |
662 | } | |
663 | ||
664 | static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) | |
665 | { | |
666 | struct ablkcipher_tfm *crt = | |
667 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); | |
668 | return crt->decrypt(req); | |
669 | } | |
670 | ||
b16c3a2e HX |
671 | static inline unsigned int crypto_ablkcipher_reqsize( |
672 | struct crypto_ablkcipher *tfm) | |
32e3983f HX |
673 | { |
674 | return crypto_ablkcipher_crt(tfm)->reqsize; | |
675 | } | |
676 | ||
e196d625 HX |
677 | static inline void ablkcipher_request_set_tfm( |
678 | struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) | |
679 | { | |
ecfc4329 | 680 | req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); |
e196d625 HX |
681 | } |
682 | ||
b5b7f088 HX |
683 | static inline struct ablkcipher_request *ablkcipher_request_cast( |
684 | struct crypto_async_request *req) | |
685 | { | |
686 | return container_of(req, struct ablkcipher_request, base); | |
687 | } | |
688 | ||
32e3983f HX |
689 | static inline struct ablkcipher_request *ablkcipher_request_alloc( |
690 | struct crypto_ablkcipher *tfm, gfp_t gfp) | |
691 | { | |
692 | struct ablkcipher_request *req; | |
693 | ||
694 | req = kmalloc(sizeof(struct ablkcipher_request) + | |
695 | crypto_ablkcipher_reqsize(tfm), gfp); | |
696 | ||
697 | if (likely(req)) | |
e196d625 | 698 | ablkcipher_request_set_tfm(req, tfm); |
32e3983f HX |
699 | |
700 | return req; | |
701 | } | |
702 | ||
703 | static inline void ablkcipher_request_free(struct ablkcipher_request *req) | |
704 | { | |
aef73cfc | 705 | kzfree(req); |
32e3983f HX |
706 | } |
707 | ||
708 | static inline void ablkcipher_request_set_callback( | |
709 | struct ablkcipher_request *req, | |
710 | u32 flags, crypto_completion_t complete, void *data) | |
711 | { | |
712 | req->base.complete = complete; | |
713 | req->base.data = data; | |
714 | req->base.flags = flags; | |
715 | } | |
716 | ||
717 | static inline void ablkcipher_request_set_crypt( | |
718 | struct ablkcipher_request *req, | |
719 | struct scatterlist *src, struct scatterlist *dst, | |
720 | unsigned int nbytes, void *iv) | |
721 | { | |
722 | req->src = src; | |
723 | req->dst = dst; | |
724 | req->nbytes = nbytes; | |
725 | req->info = iv; | |
726 | } | |
727 | ||
1ae97820 HX |
728 | static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm) |
729 | { | |
730 | return (struct crypto_aead *)tfm; | |
731 | } | |
732 | ||
d29ce988 | 733 | struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask); |
1ae97820 HX |
734 | |
735 | static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm) | |
736 | { | |
737 | return &tfm->base; | |
738 | } | |
739 | ||
740 | static inline void crypto_free_aead(struct crypto_aead *tfm) | |
741 | { | |
742 | crypto_free_tfm(crypto_aead_tfm(tfm)); | |
743 | } | |
744 | ||
745 | static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm) | |
746 | { | |
747 | return &crypto_aead_tfm(tfm)->crt_aead; | |
748 | } | |
749 | ||
750 | static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm) | |
751 | { | |
752 | return crypto_aead_crt(tfm)->ivsize; | |
753 | } | |
754 | ||
755 | static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm) | |
756 | { | |
757 | return crypto_aead_crt(tfm)->authsize; | |
758 | } | |
759 | ||
760 | static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm) | |
761 | { | |
762 | return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm)); | |
763 | } | |
764 | ||
765 | static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm) | |
766 | { | |
767 | return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm)); | |
768 | } | |
769 | ||
770 | static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm) | |
771 | { | |
772 | return crypto_tfm_get_flags(crypto_aead_tfm(tfm)); | |
773 | } | |
774 | ||
775 | static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags) | |
776 | { | |
777 | crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags); | |
778 | } | |
779 | ||
780 | static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags) | |
781 | { | |
782 | crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags); | |
783 | } | |
784 | ||
785 | static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, | |
786 | unsigned int keylen) | |
787 | { | |
5b6d2d7f HX |
788 | struct aead_tfm *crt = crypto_aead_crt(tfm); |
789 | ||
790 | return crt->setkey(crt->base, key, keylen); | |
1ae97820 HX |
791 | } |
792 | ||
7ba683a6 HX |
793 | int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize); |
794 | ||
1ae97820 HX |
795 | static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req) |
796 | { | |
797 | return __crypto_aead_cast(req->base.tfm); | |
798 | } | |
799 | ||
800 | static inline int crypto_aead_encrypt(struct aead_request *req) | |
801 | { | |
802 | return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req); | |
803 | } | |
804 | ||
805 | static inline int crypto_aead_decrypt(struct aead_request *req) | |
806 | { | |
807 | return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req); | |
808 | } | |
809 | ||
b16c3a2e | 810 | static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) |
1ae97820 HX |
811 | { |
812 | return crypto_aead_crt(tfm)->reqsize; | |
813 | } | |
814 | ||
815 | static inline void aead_request_set_tfm(struct aead_request *req, | |
816 | struct crypto_aead *tfm) | |
817 | { | |
5b6d2d7f | 818 | req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base); |
1ae97820 HX |
819 | } |
820 | ||
821 | static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm, | |
822 | gfp_t gfp) | |
823 | { | |
824 | struct aead_request *req; | |
825 | ||
826 | req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp); | |
827 | ||
828 | if (likely(req)) | |
829 | aead_request_set_tfm(req, tfm); | |
830 | ||
831 | return req; | |
832 | } | |
833 | ||
834 | static inline void aead_request_free(struct aead_request *req) | |
835 | { | |
aef73cfc | 836 | kzfree(req); |
1ae97820 HX |
837 | } |
838 | ||
839 | static inline void aead_request_set_callback(struct aead_request *req, | |
840 | u32 flags, | |
841 | crypto_completion_t complete, | |
842 | void *data) | |
843 | { | |
844 | req->base.complete = complete; | |
845 | req->base.data = data; | |
846 | req->base.flags = flags; | |
847 | } | |
848 | ||
849 | static inline void aead_request_set_crypt(struct aead_request *req, | |
850 | struct scatterlist *src, | |
851 | struct scatterlist *dst, | |
852 | unsigned int cryptlen, u8 *iv) | |
853 | { | |
854 | req->src = src; | |
855 | req->dst = dst; | |
856 | req->cryptlen = cryptlen; | |
857 | req->iv = iv; | |
858 | } | |
859 | ||
860 | static inline void aead_request_set_assoc(struct aead_request *req, | |
861 | struct scatterlist *assoc, | |
862 | unsigned int assoclen) | |
863 | { | |
864 | req->assoc = assoc; | |
865 | req->assoclen = assoclen; | |
866 | } | |
867 | ||
5cde0af2 HX |
868 | static inline struct crypto_blkcipher *__crypto_blkcipher_cast( |
869 | struct crypto_tfm *tfm) | |
870 | { | |
871 | return (struct crypto_blkcipher *)tfm; | |
872 | } | |
873 | ||
874 | static inline struct crypto_blkcipher *crypto_blkcipher_cast( | |
875 | struct crypto_tfm *tfm) | |
876 | { | |
877 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); | |
878 | return __crypto_blkcipher_cast(tfm); | |
879 | } | |
880 | ||
881 | static inline struct crypto_blkcipher *crypto_alloc_blkcipher( | |
882 | const char *alg_name, u32 type, u32 mask) | |
883 | { | |
332f8840 | 884 | type &= ~CRYPTO_ALG_TYPE_MASK; |
5cde0af2 | 885 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
332f8840 | 886 | mask |= CRYPTO_ALG_TYPE_MASK; |
5cde0af2 HX |
887 | |
888 | return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); | |
889 | } | |
890 | ||
891 | static inline struct crypto_tfm *crypto_blkcipher_tfm( | |
892 | struct crypto_blkcipher *tfm) | |
893 | { | |
894 | return &tfm->base; | |
895 | } | |
896 | ||
897 | static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) | |
898 | { | |
899 | crypto_free_tfm(crypto_blkcipher_tfm(tfm)); | |
900 | } | |
901 | ||
fce32d70 HX |
902 | static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) |
903 | { | |
332f8840 | 904 | type &= ~CRYPTO_ALG_TYPE_MASK; |
fce32d70 | 905 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
332f8840 | 906 | mask |= CRYPTO_ALG_TYPE_MASK; |
fce32d70 HX |
907 | |
908 | return crypto_has_alg(alg_name, type, mask); | |
909 | } | |
910 | ||
5cde0af2 HX |
911 | static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) |
912 | { | |
913 | return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); | |
914 | } | |
915 | ||
916 | static inline struct blkcipher_tfm *crypto_blkcipher_crt( | |
917 | struct crypto_blkcipher *tfm) | |
918 | { | |
919 | return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; | |
920 | } | |
921 | ||
922 | static inline struct blkcipher_alg *crypto_blkcipher_alg( | |
923 | struct crypto_blkcipher *tfm) | |
924 | { | |
925 | return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; | |
926 | } | |
927 | ||
928 | static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) | |
929 | { | |
930 | return crypto_blkcipher_alg(tfm)->ivsize; | |
931 | } | |
932 | ||
933 | static inline unsigned int crypto_blkcipher_blocksize( | |
934 | struct crypto_blkcipher *tfm) | |
935 | { | |
936 | return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); | |
937 | } | |
938 | ||
939 | static inline unsigned int crypto_blkcipher_alignmask( | |
940 | struct crypto_blkcipher *tfm) | |
941 | { | |
942 | return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); | |
943 | } | |
944 | ||
945 | static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) | |
946 | { | |
947 | return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); | |
948 | } | |
949 | ||
950 | static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, | |
951 | u32 flags) | |
952 | { | |
953 | crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); | |
954 | } | |
955 | ||
956 | static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, | |
957 | u32 flags) | |
958 | { | |
959 | crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); | |
960 | } | |
961 | ||
962 | static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, | |
963 | const u8 *key, unsigned int keylen) | |
964 | { | |
965 | return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), | |
966 | key, keylen); | |
967 | } | |
968 | ||
969 | static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, | |
970 | struct scatterlist *dst, | |
971 | struct scatterlist *src, | |
972 | unsigned int nbytes) | |
973 | { | |
974 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; | |
975 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); | |
976 | } | |
977 | ||
978 | static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, | |
979 | struct scatterlist *dst, | |
980 | struct scatterlist *src, | |
981 | unsigned int nbytes) | |
982 | { | |
983 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); | |
984 | } | |
985 | ||
986 | static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, | |
987 | struct scatterlist *dst, | |
988 | struct scatterlist *src, | |
989 | unsigned int nbytes) | |
990 | { | |
991 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; | |
992 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); | |
993 | } | |
994 | ||
995 | static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, | |
996 | struct scatterlist *dst, | |
997 | struct scatterlist *src, | |
998 | unsigned int nbytes) | |
999 | { | |
1000 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); | |
1001 | } | |
1002 | ||
1003 | static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, | |
1004 | const u8 *src, unsigned int len) | |
1005 | { | |
1006 | memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); | |
1007 | } | |
1008 | ||
1009 | static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, | |
1010 | u8 *dst, unsigned int len) | |
1011 | { | |
1012 | memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); | |
1013 | } | |
1014 | ||
f28776a3 HX |
1015 | static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) |
1016 | { | |
1017 | return (struct crypto_cipher *)tfm; | |
1018 | } | |
1019 | ||
1020 | static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) | |
1021 | { | |
1022 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); | |
1023 | return __crypto_cipher_cast(tfm); | |
1024 | } | |
1025 | ||
1026 | static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, | |
1027 | u32 type, u32 mask) | |
1028 | { | |
1029 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1030 | type |= CRYPTO_ALG_TYPE_CIPHER; | |
1031 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1032 | ||
1033 | return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); | |
1034 | } | |
1035 | ||
1036 | static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) | |
1037 | { | |
78a1fe4f | 1038 | return &tfm->base; |
f28776a3 HX |
1039 | } |
1040 | ||
1041 | static inline void crypto_free_cipher(struct crypto_cipher *tfm) | |
1042 | { | |
1043 | crypto_free_tfm(crypto_cipher_tfm(tfm)); | |
1044 | } | |
1045 | ||
fce32d70 HX |
1046 | static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) |
1047 | { | |
1048 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1049 | type |= CRYPTO_ALG_TYPE_CIPHER; | |
1050 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1051 | ||
1052 | return crypto_has_alg(alg_name, type, mask); | |
1053 | } | |
1054 | ||
f28776a3 HX |
1055 | static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) |
1056 | { | |
1057 | return &crypto_cipher_tfm(tfm)->crt_cipher; | |
1058 | } | |
1059 | ||
1060 | static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) | |
1061 | { | |
1062 | return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); | |
1063 | } | |
1064 | ||
1065 | static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) | |
1066 | { | |
1067 | return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); | |
1068 | } | |
1069 | ||
1070 | static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) | |
1071 | { | |
1072 | return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); | |
1073 | } | |
1074 | ||
1075 | static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, | |
1076 | u32 flags) | |
1077 | { | |
1078 | crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); | |
1079 | } | |
1080 | ||
1081 | static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, | |
1082 | u32 flags) | |
1083 | { | |
1084 | crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); | |
1085 | } | |
1086 | ||
7226bc87 HX |
1087 | static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, |
1088 | const u8 *key, unsigned int keylen) | |
1089 | { | |
1090 | return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), | |
1091 | key, keylen); | |
1092 | } | |
1093 | ||
f28776a3 HX |
1094 | static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, |
1095 | u8 *dst, const u8 *src) | |
1096 | { | |
1097 | crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), | |
1098 | dst, src); | |
1099 | } | |
1100 | ||
1101 | static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, | |
1102 | u8 *dst, const u8 *src) | |
1103 | { | |
1104 | crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), | |
1105 | dst, src); | |
1106 | } | |
1107 | ||
055bcee3 | 1108 | static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) |
1da177e4 | 1109 | { |
055bcee3 | 1110 | return (struct crypto_hash *)tfm; |
1da177e4 LT |
1111 | } |
1112 | ||
055bcee3 | 1113 | static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) |
1da177e4 | 1114 | { |
055bcee3 HX |
1115 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & |
1116 | CRYPTO_ALG_TYPE_HASH_MASK); | |
1117 | return __crypto_hash_cast(tfm); | |
1da177e4 LT |
1118 | } |
1119 | ||
055bcee3 HX |
1120 | static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, |
1121 | u32 type, u32 mask) | |
1da177e4 | 1122 | { |
055bcee3 | 1123 | type &= ~CRYPTO_ALG_TYPE_MASK; |
551a09a7 | 1124 | mask &= ~CRYPTO_ALG_TYPE_MASK; |
055bcee3 HX |
1125 | type |= CRYPTO_ALG_TYPE_HASH; |
1126 | mask |= CRYPTO_ALG_TYPE_HASH_MASK; | |
1127 | ||
1128 | return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); | |
1da177e4 LT |
1129 | } |
1130 | ||
055bcee3 | 1131 | static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) |
1da177e4 | 1132 | { |
055bcee3 HX |
1133 | return &tfm->base; |
1134 | } | |
1135 | ||
1136 | static inline void crypto_free_hash(struct crypto_hash *tfm) | |
1137 | { | |
1138 | crypto_free_tfm(crypto_hash_tfm(tfm)); | |
1139 | } | |
1140 | ||
fce32d70 HX |
1141 | static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask) |
1142 | { | |
1143 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
551a09a7 | 1144 | mask &= ~CRYPTO_ALG_TYPE_MASK; |
fce32d70 HX |
1145 | type |= CRYPTO_ALG_TYPE_HASH; |
1146 | mask |= CRYPTO_ALG_TYPE_HASH_MASK; | |
1147 | ||
1148 | return crypto_has_alg(alg_name, type, mask); | |
1149 | } | |
1150 | ||
055bcee3 HX |
1151 | static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) |
1152 | { | |
1153 | return &crypto_hash_tfm(tfm)->crt_hash; | |
1154 | } | |
1155 | ||
1156 | static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) | |
1157 | { | |
1158 | return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); | |
1159 | } | |
1160 | ||
1161 | static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) | |
1162 | { | |
1163 | return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); | |
1164 | } | |
1165 | ||
1166 | static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) | |
1167 | { | |
1168 | return crypto_hash_crt(tfm)->digestsize; | |
1169 | } | |
1170 | ||
1171 | static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) | |
1172 | { | |
1173 | return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); | |
1174 | } | |
1175 | ||
1176 | static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) | |
1177 | { | |
1178 | crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); | |
1179 | } | |
1180 | ||
1181 | static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) | |
1182 | { | |
1183 | crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); | |
1184 | } | |
1185 | ||
1186 | static inline int crypto_hash_init(struct hash_desc *desc) | |
1187 | { | |
1188 | return crypto_hash_crt(desc->tfm)->init(desc); | |
1189 | } | |
1190 | ||
1191 | static inline int crypto_hash_update(struct hash_desc *desc, | |
1192 | struct scatterlist *sg, | |
1193 | unsigned int nbytes) | |
1194 | { | |
1195 | return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); | |
1196 | } | |
1197 | ||
1198 | static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) | |
1199 | { | |
1200 | return crypto_hash_crt(desc->tfm)->final(desc, out); | |
1201 | } | |
1202 | ||
1203 | static inline int crypto_hash_digest(struct hash_desc *desc, | |
1204 | struct scatterlist *sg, | |
1205 | unsigned int nbytes, u8 *out) | |
1206 | { | |
1207 | return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); | |
1208 | } | |
1209 | ||
1210 | static inline int crypto_hash_setkey(struct crypto_hash *hash, | |
1211 | const u8 *key, unsigned int keylen) | |
1212 | { | |
1213 | return crypto_hash_crt(hash)->setkey(hash, key, keylen); | |
1da177e4 LT |
1214 | } |
1215 | ||
fce32d70 HX |
1216 | static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) |
1217 | { | |
1218 | return (struct crypto_comp *)tfm; | |
1219 | } | |
1220 | ||
1221 | static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) | |
1222 | { | |
1223 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & | |
1224 | CRYPTO_ALG_TYPE_MASK); | |
1225 | return __crypto_comp_cast(tfm); | |
1226 | } | |
1227 | ||
1228 | static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, | |
1229 | u32 type, u32 mask) | |
1230 | { | |
1231 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1232 | type |= CRYPTO_ALG_TYPE_COMPRESS; | |
1233 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1234 | ||
1235 | return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); | |
1236 | } | |
1237 | ||
1238 | static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) | |
1239 | { | |
78a1fe4f | 1240 | return &tfm->base; |
fce32d70 HX |
1241 | } |
1242 | ||
1243 | static inline void crypto_free_comp(struct crypto_comp *tfm) | |
1244 | { | |
1245 | crypto_free_tfm(crypto_comp_tfm(tfm)); | |
1246 | } | |
1247 | ||
1248 | static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) | |
1249 | { | |
1250 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1251 | type |= CRYPTO_ALG_TYPE_COMPRESS; | |
1252 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1253 | ||
1254 | return crypto_has_alg(alg_name, type, mask); | |
1255 | } | |
1256 | ||
e4d5b79c HX |
1257 | static inline const char *crypto_comp_name(struct crypto_comp *tfm) |
1258 | { | |
1259 | return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); | |
1260 | } | |
1261 | ||
fce32d70 HX |
1262 | static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) |
1263 | { | |
1264 | return &crypto_comp_tfm(tfm)->crt_compress; | |
1265 | } | |
1266 | ||
1267 | static inline int crypto_comp_compress(struct crypto_comp *tfm, | |
1da177e4 LT |
1268 | const u8 *src, unsigned int slen, |
1269 | u8 *dst, unsigned int *dlen) | |
1270 | { | |
78a1fe4f HX |
1271 | return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), |
1272 | src, slen, dst, dlen); | |
1da177e4 LT |
1273 | } |
1274 | ||
fce32d70 | 1275 | static inline int crypto_comp_decompress(struct crypto_comp *tfm, |
1da177e4 LT |
1276 | const u8 *src, unsigned int slen, |
1277 | u8 *dst, unsigned int *dlen) | |
1278 | { | |
78a1fe4f HX |
1279 | return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), |
1280 | src, slen, dst, dlen); | |
1da177e4 LT |
1281 | } |
1282 | ||
1da177e4 LT |
1283 | #endif /* _LINUX_CRYPTO_H */ |
1284 |