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