blob: 187c6ea919595dc3e595fcda6d6c695d278b665e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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
Herbert Xu6521f302006-08-06 20:28:44 +100020#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Algorithm masks and types.
30 */
Herbert Xu28259822006-08-06 21:23:26 +100031#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
Herbert Xu055bcee2006-08-19 22:24:23 +100034#define CRYPTO_ALG_TYPE_HASH 0x00000003
35#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36#define CRYPTO_ALG_TYPE_COMPRESS 0x00000005
37
38#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Herbert Xu28259822006-08-06 21:23:26 +100040#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100041#define CRYPTO_ALG_DEAD 0x00000020
42#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100043#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
Herbert Xu60104392006-08-26 18:34:10 +100046 * Set this bit if and only if the algorithm requires another algorithm of
47 * the same type to handle corner cases.
48 */
49#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
50
51/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Transform masks and values (for crt_flags).
53 */
54#define CRYPTO_TFM_MODE_MASK 0x000000ff
55#define CRYPTO_TFM_REQ_MASK 0x000fff00
56#define CRYPTO_TFM_RES_MASK 0xfff00000
57
58#define CRYPTO_TFM_MODE_ECB 0x00000001
59#define CRYPTO_TFM_MODE_CBC 0x00000002
60#define CRYPTO_TFM_MODE_CFB 0x00000004
61#define CRYPTO_TFM_MODE_CTR 0x00000008
62
63#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070064#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
66#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
67#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
68#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
69#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
70
71/*
72 * Miscellaneous stuff.
73 */
74#define CRYPTO_UNSPEC 0
75#define CRYPTO_MAX_ALG_NAME 64
76
77#define CRYPTO_DIR_ENCRYPT 1
78#define CRYPTO_DIR_DECRYPT 0
79
Herbert Xu79911102006-08-21 21:03:52 +100080/*
81 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
82 * declaration) is used to ensure that the crypto_tfm context structure is
83 * aligned correctly for the given architecture so that there are no alignment
84 * faults for C data types. In particular, this is required on platforms such
85 * as arm where pointers are 32-bit aligned but there are data types such as
86 * u64 which require 64-bit alignment.
87 */
88#if defined(ARCH_KMALLOC_MINALIGN)
89#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
90#elif defined(ARCH_SLAB_MINALIGN)
91#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
92#endif
93
94#ifdef CRYPTO_MINALIGN
95#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
96#else
97#define CRYPTO_MINALIGN_ATTR
98#endif
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100struct scatterlist;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000101struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000102struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -0700103struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000104struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700105
Herbert Xu5cde0af2006-08-22 00:07:53 +1000106struct blkcipher_desc {
107 struct crypto_blkcipher *tfm;
108 void *info;
109 u32 flags;
110};
111
Herbert Xu40725182005-07-06 13:51:52 -0700112struct cipher_desc {
113 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000114 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700115 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
116 const u8 *src, unsigned int nbytes);
117 void *info;
118};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Herbert Xu055bcee2006-08-19 22:24:23 +1000120struct hash_desc {
121 struct crypto_hash *tfm;
122 u32 flags;
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Algorithms: modular crypto algorithm implementations, managed
127 * via crypto_register_alg() and crypto_unregister_alg().
128 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000129struct blkcipher_alg {
130 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
131 unsigned int keylen);
132 int (*encrypt)(struct blkcipher_desc *desc,
133 struct scatterlist *dst, struct scatterlist *src,
134 unsigned int nbytes);
135 int (*decrypt)(struct blkcipher_desc *desc,
136 struct scatterlist *dst, struct scatterlist *src,
137 unsigned int nbytes);
138
139 unsigned int min_keysize;
140 unsigned int max_keysize;
141 unsigned int ivsize;
142};
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144struct cipher_alg {
145 unsigned int cia_min_keysize;
146 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000147 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000148 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000149 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
150 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700151
152 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
153 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000154 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700155 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
156 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000157 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700158 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
159 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000160 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700161 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
162 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000163 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166struct digest_alg {
167 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000168 void (*dia_init)(struct crypto_tfm *tfm);
169 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
170 unsigned int len);
171 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
172 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000173 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
Herbert Xu055bcee2006-08-19 22:24:23 +1000176struct hash_alg {
177 int (*init)(struct hash_desc *desc);
178 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
179 unsigned int nbytes);
180 int (*final)(struct hash_desc *desc, u8 *out);
181 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
182 unsigned int nbytes, u8 *out);
183 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
184 unsigned int keylen);
185
186 unsigned int digestsize;
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000190 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
191 unsigned int slen, u8 *dst, unsigned int *dlen);
192 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
193 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Herbert Xu5cde0af2006-08-22 00:07:53 +1000196#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#define cra_cipher cra_u.cipher
198#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000199#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define cra_compress cra_u.compress
201
202struct crypto_alg {
203 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000204 struct list_head cra_users;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 u32 cra_flags;
207 unsigned int cra_blocksize;
208 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700209 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100210
211 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000212 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100213
Herbert Xud913ea02006-05-21 08:45:26 +1000214 char cra_name[CRYPTO_MAX_ALG_NAME];
215 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Herbert Xue853c3c2006-08-22 00:06:54 +1000217 const struct crypto_type *cra_type;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000220 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct cipher_alg cipher;
222 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000223 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct compress_alg compress;
225 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000226
227 int (*cra_init)(struct crypto_tfm *tfm);
228 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000229 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 struct module *cra_module;
232};
233
234/*
235 * Algorithm registration interface.
236 */
237int crypto_register_alg(struct crypto_alg *alg);
238int crypto_unregister_alg(struct crypto_alg *alg);
239
240/*
241 * Algorithm query interface.
242 */
243#ifdef CONFIG_CRYPTO
244int crypto_alg_available(const char *name, u32 flags);
Herbert Xufce32d72006-08-26 17:35:45 +1000245int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#else
247static inline int crypto_alg_available(const char *name, u32 flags)
248{
249 return 0;
250}
Herbert Xufce32d72006-08-26 17:35:45 +1000251
252static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
253{
254 return 0;
255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#endif
257
258/*
259 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000260 * and core processing logic. Managed via crypto_alloc_*() and
261 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Herbert Xu5cde0af2006-08-22 00:07:53 +1000264struct blkcipher_tfm {
265 void *iv;
266 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
267 unsigned int keylen);
268 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
269 struct scatterlist *src, unsigned int nbytes);
270 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
271 struct scatterlist *src, unsigned int nbytes);
272};
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274struct cipher_tfm {
275 void *cit_iv;
276 unsigned int cit_ivsize;
277 u32 cit_mode;
278 int (*cit_setkey)(struct crypto_tfm *tfm,
279 const u8 *key, unsigned int keylen);
280 int (*cit_encrypt)(struct crypto_tfm *tfm,
281 struct scatterlist *dst,
282 struct scatterlist *src,
283 unsigned int nbytes);
284 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
285 struct scatterlist *dst,
286 struct scatterlist *src,
287 unsigned int nbytes, u8 *iv);
288 int (*cit_decrypt)(struct crypto_tfm *tfm,
289 struct scatterlist *dst,
290 struct scatterlist *src,
291 unsigned int nbytes);
292 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
293 struct scatterlist *dst,
294 struct scatterlist *src,
295 unsigned int nbytes, u8 *iv);
296 void (*cit_xor_block)(u8 *dst, const u8 *src);
Herbert Xuf28776a2006-08-13 20:58:18 +1000297 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
298 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
Herbert Xu055bcee2006-08-19 22:24:23 +1000301struct hash_tfm {
302 int (*init)(struct hash_desc *desc);
303 int (*update)(struct hash_desc *desc,
304 struct scatterlist *sg, unsigned int nsg);
305 int (*final)(struct hash_desc *desc, u8 *out);
306 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
307 unsigned int nsg, u8 *out);
308 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
309 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000310 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313struct compress_tfm {
314 int (*cot_compress)(struct crypto_tfm *tfm,
315 const u8 *src, unsigned int slen,
316 u8 *dst, unsigned int *dlen);
317 int (*cot_decompress)(struct crypto_tfm *tfm,
318 const u8 *src, unsigned int slen,
319 u8 *dst, unsigned int *dlen);
320};
321
Herbert Xu5cde0af2006-08-22 00:07:53 +1000322#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000324#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#define crt_compress crt_u.compress
326
327struct crypto_tfm {
328
329 u32 crt_flags;
330
331 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000332 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000334 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct compress_tfm compress;
336 } crt_u;
337
338 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100339
Herbert Xu79911102006-08-21 21:03:52 +1000340 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
Herbert Xuf28776a2006-08-13 20:58:18 +1000343#define crypto_cipher crypto_tfm
Herbert Xufce32d72006-08-26 17:35:45 +1000344#define crypto_comp crypto_tfm
Herbert Xuf28776a2006-08-13 20:58:18 +1000345
Herbert Xu5cde0af2006-08-22 00:07:53 +1000346struct crypto_blkcipher {
347 struct crypto_tfm base;
348};
349
Herbert Xu055bcee2006-08-19 22:24:23 +1000350struct crypto_hash {
351 struct crypto_tfm base;
352};
353
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000354enum {
355 CRYPTOA_UNSPEC,
356 CRYPTOA_ALG,
357};
358
359struct crypto_attr_alg {
360 char name[CRYPTO_MAX_ALG_NAME];
361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*
364 * Transform user interface.
365 */
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000368struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369void crypto_free_tfm(struct crypto_tfm *tfm);
370
371/*
372 * Transform helpers which query the underlying algorithm.
373 */
374static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
375{
376 return tfm->__crt_alg->cra_name;
377}
378
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000379static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
380{
381 return tfm->__crt_alg->cra_driver_name;
382}
383
384static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
385{
386 return tfm->__crt_alg->cra_priority;
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
390{
391 return module_name(tfm->__crt_alg->cra_module);
392}
393
394static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
395{
396 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
397}
398
Herbert Xu7226bc872006-08-21 21:40:49 +1000399static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
400 __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
402{
403 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
404 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
405}
406
Herbert Xu7226bc872006-08-21 21:40:49 +1000407static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
408 __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
410{
411 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
412 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
413}
414
Herbert Xu7226bc872006-08-21 21:40:49 +1000415static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
417{
418 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
419 return tfm->crt_cipher.cit_ivsize;
420}
421
422static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
423{
424 return tfm->__crt_alg->cra_blocksize;
425}
426
427static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
428{
429 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
430 return tfm->__crt_alg->cra_digest.dia_digestsize;
431}
432
Herbert Xufbdae9f2005-07-06 13:53:29 -0700433static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
434{
435 return tfm->__crt_alg->cra_alignmask;
436}
437
Herbert Xuf28776a2006-08-13 20:58:18 +1000438static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
439{
440 return tfm->crt_flags;
441}
442
443static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
444{
445 tfm->crt_flags |= flags;
446}
447
448static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
449{
450 tfm->crt_flags &= ~flags;
451}
452
Herbert Xu40725182005-07-06 13:51:52 -0700453static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
454{
Herbert Xuf10b7892006-01-25 22:34:01 +1100455 return tfm->__crt_ctx;
456}
457
458static inline unsigned int crypto_tfm_ctx_alignment(void)
459{
460 struct crypto_tfm *tfm;
461 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * API wrappers.
466 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000467static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
468 struct crypto_tfm *tfm)
469{
470 return (struct crypto_blkcipher *)tfm;
471}
472
473static inline struct crypto_blkcipher *crypto_blkcipher_cast(
474 struct crypto_tfm *tfm)
475{
476 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
477 return __crypto_blkcipher_cast(tfm);
478}
479
480static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
481 const char *alg_name, u32 type, u32 mask)
482{
483 type &= ~CRYPTO_ALG_TYPE_MASK;
484 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
485 mask |= CRYPTO_ALG_TYPE_MASK;
486
487 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
488}
489
490static inline struct crypto_tfm *crypto_blkcipher_tfm(
491 struct crypto_blkcipher *tfm)
492{
493 return &tfm->base;
494}
495
496static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
497{
498 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
499}
500
Herbert Xufce32d72006-08-26 17:35:45 +1000501static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
502{
503 type &= ~CRYPTO_ALG_TYPE_MASK;
504 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
505 mask |= CRYPTO_ALG_TYPE_MASK;
506
507 return crypto_has_alg(alg_name, type, mask);
508}
509
Herbert Xu5cde0af2006-08-22 00:07:53 +1000510static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
511{
512 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
513}
514
515static inline struct blkcipher_tfm *crypto_blkcipher_crt(
516 struct crypto_blkcipher *tfm)
517{
518 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
519}
520
521static inline struct blkcipher_alg *crypto_blkcipher_alg(
522 struct crypto_blkcipher *tfm)
523{
524 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
525}
526
527static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
528{
529 return crypto_blkcipher_alg(tfm)->ivsize;
530}
531
532static inline unsigned int crypto_blkcipher_blocksize(
533 struct crypto_blkcipher *tfm)
534{
535 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
536}
537
538static inline unsigned int crypto_blkcipher_alignmask(
539 struct crypto_blkcipher *tfm)
540{
541 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
542}
543
544static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
545{
546 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
547}
548
549static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
550 u32 flags)
551{
552 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
553}
554
555static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
556 u32 flags)
557{
558 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
559}
560
561static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
562 const u8 *key, unsigned int keylen)
563{
564 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
565 key, keylen);
566}
567
568static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
569 struct scatterlist *dst,
570 struct scatterlist *src,
571 unsigned int nbytes)
572{
573 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
574 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
575}
576
577static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
578 struct scatterlist *dst,
579 struct scatterlist *src,
580 unsigned int nbytes)
581{
582 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
583}
584
585static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
586 struct scatterlist *dst,
587 struct scatterlist *src,
588 unsigned int nbytes)
589{
590 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
591 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
592}
593
594static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
595 struct scatterlist *dst,
596 struct scatterlist *src,
597 unsigned int nbytes)
598{
599 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
600}
601
602static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
603 const u8 *src, unsigned int len)
604{
605 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
606}
607
608static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
609 u8 *dst, unsigned int len)
610{
611 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
612}
613
Herbert Xuf28776a2006-08-13 20:58:18 +1000614static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
615{
616 return (struct crypto_cipher *)tfm;
617}
618
619static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
620{
621 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
622 return __crypto_cipher_cast(tfm);
623}
624
625static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
626 u32 type, u32 mask)
627{
628 type &= ~CRYPTO_ALG_TYPE_MASK;
629 type |= CRYPTO_ALG_TYPE_CIPHER;
630 mask |= CRYPTO_ALG_TYPE_MASK;
631
632 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
633}
634
635static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
636{
637 return tfm;
638}
639
640static inline void crypto_free_cipher(struct crypto_cipher *tfm)
641{
642 crypto_free_tfm(crypto_cipher_tfm(tfm));
643}
644
Herbert Xufce32d72006-08-26 17:35:45 +1000645static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
646{
647 type &= ~CRYPTO_ALG_TYPE_MASK;
648 type |= CRYPTO_ALG_TYPE_CIPHER;
649 mask |= CRYPTO_ALG_TYPE_MASK;
650
651 return crypto_has_alg(alg_name, type, mask);
652}
653
Herbert Xuf28776a2006-08-13 20:58:18 +1000654static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
655{
656 return &crypto_cipher_tfm(tfm)->crt_cipher;
657}
658
659static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
660{
661 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
662}
663
664static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
665{
666 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
667}
668
669static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
670{
671 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
672}
673
674static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
675 u32 flags)
676{
677 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
678}
679
680static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
681 u32 flags)
682{
683 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
684}
685
Herbert Xu7226bc872006-08-21 21:40:49 +1000686static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
687 const u8 *key, unsigned int keylen)
688{
689 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
690 key, keylen);
691}
692
Herbert Xuf28776a2006-08-13 20:58:18 +1000693static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
694 u8 *dst, const u8 *src)
695{
696 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
697 dst, src);
698}
699
700static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
701 u8 *dst, const u8 *src)
702{
703 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
704 dst, src);
705}
706
Herbert Xu055bcee2006-08-19 22:24:23 +1000707void crypto_digest_init(struct crypto_tfm *tfm);
708void crypto_digest_update(struct crypto_tfm *tfm,
709 struct scatterlist *sg, unsigned int nsg);
710void crypto_digest_final(struct crypto_tfm *tfm, u8 *out);
711void crypto_digest_digest(struct crypto_tfm *tfm,
712 struct scatterlist *sg, unsigned int nsg, u8 *out);
713
714static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Herbert Xu055bcee2006-08-19 22:24:23 +1000716 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Herbert Xu055bcee2006-08-19 22:24:23 +1000719static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Herbert Xu055bcee2006-08-19 22:24:23 +1000721 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
722 CRYPTO_ALG_TYPE_HASH_MASK);
723 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
727 const u8 *key, unsigned int keylen)
728{
Herbert Xu055bcee2006-08-19 22:24:23 +1000729 return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen);
730}
731
732static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
733 u32 type, u32 mask)
734{
735 type &= ~CRYPTO_ALG_TYPE_MASK;
736 type |= CRYPTO_ALG_TYPE_HASH;
737 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
738
739 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
740}
741
742static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
743{
744 return &tfm->base;
745}
746
747static inline void crypto_free_hash(struct crypto_hash *tfm)
748{
749 crypto_free_tfm(crypto_hash_tfm(tfm));
750}
751
Herbert Xufce32d72006-08-26 17:35:45 +1000752static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
753{
754 type &= ~CRYPTO_ALG_TYPE_MASK;
755 type |= CRYPTO_ALG_TYPE_HASH;
756 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
757
758 return crypto_has_alg(alg_name, type, mask);
759}
760
Herbert Xu055bcee2006-08-19 22:24:23 +1000761static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
762{
763 return &crypto_hash_tfm(tfm)->crt_hash;
764}
765
766static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
767{
768 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
769}
770
771static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
772{
773 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
774}
775
776static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
777{
778 return crypto_hash_crt(tfm)->digestsize;
779}
780
781static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
782{
783 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
784}
785
786static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
787{
788 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
789}
790
791static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
792{
793 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
794}
795
796static inline int crypto_hash_init(struct hash_desc *desc)
797{
798 return crypto_hash_crt(desc->tfm)->init(desc);
799}
800
801static inline int crypto_hash_update(struct hash_desc *desc,
802 struct scatterlist *sg,
803 unsigned int nbytes)
804{
805 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
806}
807
808static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
809{
810 return crypto_hash_crt(desc->tfm)->final(desc, out);
811}
812
813static inline int crypto_hash_digest(struct hash_desc *desc,
814 struct scatterlist *sg,
815 unsigned int nbytes, u8 *out)
816{
817 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
818}
819
820static inline int crypto_hash_setkey(struct crypto_hash *hash,
821 const u8 *key, unsigned int keylen)
822{
823 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Herbert Xu7226bc872006-08-21 21:40:49 +1000826static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
827 struct scatterlist *dst,
828 struct scatterlist *src,
829 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
831 struct scatterlist *dst,
832 struct scatterlist *src,
833 unsigned int nbytes)
834{
835 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
836 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
837}
838
Herbert Xu7226bc872006-08-21 21:40:49 +1000839static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
840 struct scatterlist *dst,
841 struct scatterlist *src,
842 unsigned int nbytes, u8 *iv) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
844 struct scatterlist *dst,
845 struct scatterlist *src,
846 unsigned int nbytes, u8 *iv)
847{
848 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
850}
851
Herbert Xu7226bc872006-08-21 21:40:49 +1000852static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
853 struct scatterlist *dst,
854 struct scatterlist *src,
855 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
857 struct scatterlist *dst,
858 struct scatterlist *src,
859 unsigned int nbytes)
860{
861 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
862 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
863}
864
Herbert Xu7226bc872006-08-21 21:40:49 +1000865static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
866 struct scatterlist *dst,
867 struct scatterlist *src,
868 unsigned int nbytes, u8 *iv) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
870 struct scatterlist *dst,
871 struct scatterlist *src,
872 unsigned int nbytes, u8 *iv)
873{
874 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
876}
877
Herbert Xu7226bc872006-08-21 21:40:49 +1000878static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
879 const u8 *src, unsigned int len) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
881 const u8 *src, unsigned int len)
882{
883 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
884 memcpy(tfm->crt_cipher.cit_iv, src, len);
885}
886
Herbert Xu7226bc872006-08-21 21:40:49 +1000887static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
888 u8 *dst, unsigned int len) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
890 u8 *dst, unsigned int len)
891{
892 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
893 memcpy(dst, tfm->crt_cipher.cit_iv, len);
894}
895
Herbert Xufce32d72006-08-26 17:35:45 +1000896static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
897{
898 return (struct crypto_comp *)tfm;
899}
900
901static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
902{
903 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
904 CRYPTO_ALG_TYPE_MASK);
905 return __crypto_comp_cast(tfm);
906}
907
908static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
909 u32 type, u32 mask)
910{
911 type &= ~CRYPTO_ALG_TYPE_MASK;
912 type |= CRYPTO_ALG_TYPE_COMPRESS;
913 mask |= CRYPTO_ALG_TYPE_MASK;
914
915 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
916}
917
918static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
919{
920 return tfm;
921}
922
923static inline void crypto_free_comp(struct crypto_comp *tfm)
924{
925 crypto_free_tfm(crypto_comp_tfm(tfm));
926}
927
928static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
929{
930 type &= ~CRYPTO_ALG_TYPE_MASK;
931 type |= CRYPTO_ALG_TYPE_COMPRESS;
932 mask |= CRYPTO_ALG_TYPE_MASK;
933
934 return crypto_has_alg(alg_name, type, mask);
935}
936
Herbert Xue4d5b792006-08-26 18:12:40 +1000937static inline const char *crypto_comp_name(struct crypto_comp *tfm)
938{
939 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
940}
941
Herbert Xufce32d72006-08-26 17:35:45 +1000942static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
943{
944 return &crypto_comp_tfm(tfm)->crt_compress;
945}
946
947static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 const u8 *src, unsigned int slen,
949 u8 *dst, unsigned int *dlen)
950{
Herbert Xufce32d72006-08-26 17:35:45 +1000951 return crypto_comp_crt(tfm)->cot_compress(tfm, src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Herbert Xufce32d72006-08-26 17:35:45 +1000954static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 const u8 *src, unsigned int slen,
956 u8 *dst, unsigned int *dlen)
957{
Herbert Xufce32d72006-08-26 17:35:45 +1000958 return crypto_comp_crt(tfm)->cot_decompress(tfm, src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961#endif /* _LINUX_CRYPTO_H */
962