blob: 0be666b5046322431e1639b4ca2fefdf6c0180eb [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 Xu5cde0af2006-08-22 00:07:53 +100034#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000003
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
36
Herbert Xu28259822006-08-06 21:23:26 +100037#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100038#define CRYPTO_ALG_DEAD 0x00000020
39#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100040#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * Transform masks and values (for crt_flags).
44 */
45#define CRYPTO_TFM_MODE_MASK 0x000000ff
46#define CRYPTO_TFM_REQ_MASK 0x000fff00
47#define CRYPTO_TFM_RES_MASK 0xfff00000
48
49#define CRYPTO_TFM_MODE_ECB 0x00000001
50#define CRYPTO_TFM_MODE_CBC 0x00000002
51#define CRYPTO_TFM_MODE_CFB 0x00000004
52#define CRYPTO_TFM_MODE_CTR 0x00000008
53
54#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070055#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
57#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
58#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
59#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
60#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
61
62/*
63 * Miscellaneous stuff.
64 */
65#define CRYPTO_UNSPEC 0
66#define CRYPTO_MAX_ALG_NAME 64
67
68#define CRYPTO_DIR_ENCRYPT 1
69#define CRYPTO_DIR_DECRYPT 0
70
Herbert Xu79911102006-08-21 21:03:52 +100071/*
72 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
73 * declaration) is used to ensure that the crypto_tfm context structure is
74 * aligned correctly for the given architecture so that there are no alignment
75 * faults for C data types. In particular, this is required on platforms such
76 * as arm where pointers are 32-bit aligned but there are data types such as
77 * u64 which require 64-bit alignment.
78 */
79#if defined(ARCH_KMALLOC_MINALIGN)
80#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
81#elif defined(ARCH_SLAB_MINALIGN)
82#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
83#endif
84
85#ifdef CRYPTO_MINALIGN
86#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
87#else
88#define CRYPTO_MINALIGN_ATTR
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct scatterlist;
Herbert Xu5cde0af2006-08-22 00:07:53 +100092struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -070093struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +100094struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -070095
Herbert Xu5cde0af2006-08-22 00:07:53 +100096struct blkcipher_desc {
97 struct crypto_blkcipher *tfm;
98 void *info;
99 u32 flags;
100};
101
Herbert Xu40725182005-07-06 13:51:52 -0700102struct cipher_desc {
103 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000104 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700105 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
106 const u8 *src, unsigned int nbytes);
107 void *info;
108};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/*
111 * Algorithms: modular crypto algorithm implementations, managed
112 * via crypto_register_alg() and crypto_unregister_alg().
113 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000114struct blkcipher_alg {
115 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
116 unsigned int keylen);
117 int (*encrypt)(struct blkcipher_desc *desc,
118 struct scatterlist *dst, struct scatterlist *src,
119 unsigned int nbytes);
120 int (*decrypt)(struct blkcipher_desc *desc,
121 struct scatterlist *dst, struct scatterlist *src,
122 unsigned int nbytes);
123
124 unsigned int min_keysize;
125 unsigned int max_keysize;
126 unsigned int ivsize;
127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129struct cipher_alg {
130 unsigned int cia_min_keysize;
131 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000132 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000133 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000134 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
135 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700136
137 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
138 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000139 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700140 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
141 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000142 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700143 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
144 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000145 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700146 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
147 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000148 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct digest_alg {
152 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000153 void (*dia_init)(struct crypto_tfm *tfm);
154 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
155 unsigned int len);
156 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
157 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000158 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
161struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000162 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
163 unsigned int slen, u8 *dst, unsigned int *dlen);
164 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
165 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
Herbert Xu5cde0af2006-08-22 00:07:53 +1000168#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define cra_cipher cra_u.cipher
170#define cra_digest cra_u.digest
171#define cra_compress cra_u.compress
172
173struct crypto_alg {
174 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000175 struct list_head cra_users;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 u32 cra_flags;
178 unsigned int cra_blocksize;
179 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700180 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100181
182 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000183 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100184
Herbert Xud913ea02006-05-21 08:45:26 +1000185 char cra_name[CRYPTO_MAX_ALG_NAME];
186 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Herbert Xue853c3c2006-08-22 00:06:54 +1000188 const struct crypto_type *cra_type;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000191 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct cipher_alg cipher;
193 struct digest_alg digest;
194 struct compress_alg compress;
195 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000196
197 int (*cra_init)(struct crypto_tfm *tfm);
198 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000199 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 struct module *cra_module;
202};
203
204/*
205 * Algorithm registration interface.
206 */
207int crypto_register_alg(struct crypto_alg *alg);
208int crypto_unregister_alg(struct crypto_alg *alg);
209
210/*
211 * Algorithm query interface.
212 */
213#ifdef CONFIG_CRYPTO
214int crypto_alg_available(const char *name, u32 flags);
215#else
216static inline int crypto_alg_available(const char *name, u32 flags)
217{
218 return 0;
219}
220#endif
221
222/*
223 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000224 * and core processing logic. Managed via crypto_alloc_*() and
225 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Herbert Xu5cde0af2006-08-22 00:07:53 +1000228struct blkcipher_tfm {
229 void *iv;
230 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
231 unsigned int keylen);
232 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
233 struct scatterlist *src, unsigned int nbytes);
234 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
235 struct scatterlist *src, unsigned int nbytes);
236};
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238struct cipher_tfm {
239 void *cit_iv;
240 unsigned int cit_ivsize;
241 u32 cit_mode;
242 int (*cit_setkey)(struct crypto_tfm *tfm,
243 const u8 *key, unsigned int keylen);
244 int (*cit_encrypt)(struct crypto_tfm *tfm,
245 struct scatterlist *dst,
246 struct scatterlist *src,
247 unsigned int nbytes);
248 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
249 struct scatterlist *dst,
250 struct scatterlist *src,
251 unsigned int nbytes, u8 *iv);
252 int (*cit_decrypt)(struct crypto_tfm *tfm,
253 struct scatterlist *dst,
254 struct scatterlist *src,
255 unsigned int nbytes);
256 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
257 struct scatterlist *dst,
258 struct scatterlist *src,
259 unsigned int nbytes, u8 *iv);
260 void (*cit_xor_block)(u8 *dst, const u8 *src);
Herbert Xuf28776a2006-08-13 20:58:18 +1000261 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
262 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
265struct digest_tfm {
266 void (*dit_init)(struct crypto_tfm *tfm);
267 void (*dit_update)(struct crypto_tfm *tfm,
268 struct scatterlist *sg, unsigned int nsg);
269 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
270 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
271 unsigned int nsg, u8 *out);
272 int (*dit_setkey)(struct crypto_tfm *tfm,
273 const u8 *key, unsigned int keylen);
274#ifdef CONFIG_CRYPTO_HMAC
275 void *dit_hmac_block;
276#endif
277};
278
279struct compress_tfm {
280 int (*cot_compress)(struct crypto_tfm *tfm,
281 const u8 *src, unsigned int slen,
282 u8 *dst, unsigned int *dlen);
283 int (*cot_decompress)(struct crypto_tfm *tfm,
284 const u8 *src, unsigned int slen,
285 u8 *dst, unsigned int *dlen);
286};
287
Herbert Xu5cde0af2006-08-22 00:07:53 +1000288#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#define crt_cipher crt_u.cipher
290#define crt_digest crt_u.digest
291#define crt_compress crt_u.compress
292
293struct crypto_tfm {
294
295 u32 crt_flags;
296
297 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000298 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct cipher_tfm cipher;
300 struct digest_tfm digest;
301 struct compress_tfm compress;
302 } crt_u;
303
304 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100305
Herbert Xu79911102006-08-21 21:03:52 +1000306 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
Herbert Xuf28776a2006-08-13 20:58:18 +1000309#define crypto_cipher crypto_tfm
310
Herbert Xu5cde0af2006-08-22 00:07:53 +1000311struct crypto_blkcipher {
312 struct crypto_tfm base;
313};
314
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000315enum {
316 CRYPTOA_UNSPEC,
317 CRYPTOA_ALG,
318};
319
320struct crypto_attr_alg {
321 char name[CRYPTO_MAX_ALG_NAME];
322};
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Transform user interface.
326 */
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000329struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330void crypto_free_tfm(struct crypto_tfm *tfm);
331
332/*
333 * Transform helpers which query the underlying algorithm.
334 */
335static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
336{
337 return tfm->__crt_alg->cra_name;
338}
339
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000340static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
341{
342 return tfm->__crt_alg->cra_driver_name;
343}
344
345static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
346{
347 return tfm->__crt_alg->cra_priority;
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
351{
352 return module_name(tfm->__crt_alg->cra_module);
353}
354
355static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
356{
357 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
358}
359
Herbert Xu7226bc872006-08-21 21:40:49 +1000360static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
361 __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
363{
364 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
365 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
366}
367
Herbert Xu7226bc872006-08-21 21:40:49 +1000368static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
369 __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
371{
372 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
373 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
374}
375
Herbert Xu7226bc872006-08-21 21:40:49 +1000376static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
378{
379 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
380 return tfm->crt_cipher.cit_ivsize;
381}
382
383static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
384{
385 return tfm->__crt_alg->cra_blocksize;
386}
387
388static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
389{
390 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
391 return tfm->__crt_alg->cra_digest.dia_digestsize;
392}
393
Herbert Xufbdae9f2005-07-06 13:53:29 -0700394static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
395{
396 return tfm->__crt_alg->cra_alignmask;
397}
398
Herbert Xuf28776a2006-08-13 20:58:18 +1000399static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
400{
401 return tfm->crt_flags;
402}
403
404static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
405{
406 tfm->crt_flags |= flags;
407}
408
409static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
410{
411 tfm->crt_flags &= ~flags;
412}
413
Herbert Xu40725182005-07-06 13:51:52 -0700414static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
415{
Herbert Xuf10b7892006-01-25 22:34:01 +1100416 return tfm->__crt_ctx;
417}
418
419static inline unsigned int crypto_tfm_ctx_alignment(void)
420{
421 struct crypto_tfm *tfm;
422 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
426 * API wrappers.
427 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000428static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
429 struct crypto_tfm *tfm)
430{
431 return (struct crypto_blkcipher *)tfm;
432}
433
434static inline struct crypto_blkcipher *crypto_blkcipher_cast(
435 struct crypto_tfm *tfm)
436{
437 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
438 return __crypto_blkcipher_cast(tfm);
439}
440
441static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
442 const char *alg_name, u32 type, u32 mask)
443{
444 type &= ~CRYPTO_ALG_TYPE_MASK;
445 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
446 mask |= CRYPTO_ALG_TYPE_MASK;
447
448 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
449}
450
451static inline struct crypto_tfm *crypto_blkcipher_tfm(
452 struct crypto_blkcipher *tfm)
453{
454 return &tfm->base;
455}
456
457static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
458{
459 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
460}
461
462static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
463{
464 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
465}
466
467static inline struct blkcipher_tfm *crypto_blkcipher_crt(
468 struct crypto_blkcipher *tfm)
469{
470 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
471}
472
473static inline struct blkcipher_alg *crypto_blkcipher_alg(
474 struct crypto_blkcipher *tfm)
475{
476 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
477}
478
479static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
480{
481 return crypto_blkcipher_alg(tfm)->ivsize;
482}
483
484static inline unsigned int crypto_blkcipher_blocksize(
485 struct crypto_blkcipher *tfm)
486{
487 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
488}
489
490static inline unsigned int crypto_blkcipher_alignmask(
491 struct crypto_blkcipher *tfm)
492{
493 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
494}
495
496static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
497{
498 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
499}
500
501static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
502 u32 flags)
503{
504 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
505}
506
507static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
508 u32 flags)
509{
510 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
511}
512
513static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
514 const u8 *key, unsigned int keylen)
515{
516 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
517 key, keylen);
518}
519
520static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
521 struct scatterlist *dst,
522 struct scatterlist *src,
523 unsigned int nbytes)
524{
525 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
526 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
527}
528
529static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
530 struct scatterlist *dst,
531 struct scatterlist *src,
532 unsigned int nbytes)
533{
534 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
535}
536
537static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
538 struct scatterlist *dst,
539 struct scatterlist *src,
540 unsigned int nbytes)
541{
542 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
543 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
544}
545
546static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
547 struct scatterlist *dst,
548 struct scatterlist *src,
549 unsigned int nbytes)
550{
551 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
552}
553
554static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
555 const u8 *src, unsigned int len)
556{
557 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
558}
559
560static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
561 u8 *dst, unsigned int len)
562{
563 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
564}
565
Herbert Xuf28776a2006-08-13 20:58:18 +1000566static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
567{
568 return (struct crypto_cipher *)tfm;
569}
570
571static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
572{
573 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
574 return __crypto_cipher_cast(tfm);
575}
576
577static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
578 u32 type, u32 mask)
579{
580 type &= ~CRYPTO_ALG_TYPE_MASK;
581 type |= CRYPTO_ALG_TYPE_CIPHER;
582 mask |= CRYPTO_ALG_TYPE_MASK;
583
584 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
585}
586
587static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
588{
589 return tfm;
590}
591
592static inline void crypto_free_cipher(struct crypto_cipher *tfm)
593{
594 crypto_free_tfm(crypto_cipher_tfm(tfm));
595}
596
597static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
598{
599 return &crypto_cipher_tfm(tfm)->crt_cipher;
600}
601
602static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
603{
604 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
605}
606
607static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
608{
609 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
610}
611
612static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
613{
614 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
615}
616
617static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
618 u32 flags)
619{
620 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
621}
622
623static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
624 u32 flags)
625{
626 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
627}
628
Herbert Xu7226bc872006-08-21 21:40:49 +1000629static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
630 const u8 *key, unsigned int keylen)
631{
632 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
633 key, keylen);
634}
635
Herbert Xuf28776a2006-08-13 20:58:18 +1000636static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
637 u8 *dst, const u8 *src)
638{
639 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
640 dst, src);
641}
642
643static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
644 u8 *dst, const u8 *src)
645{
646 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
647 dst, src);
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650static inline void crypto_digest_init(struct crypto_tfm *tfm)
651{
652 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
653 tfm->crt_digest.dit_init(tfm);
654}
655
656static inline void crypto_digest_update(struct crypto_tfm *tfm,
657 struct scatterlist *sg,
658 unsigned int nsg)
659{
660 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
661 tfm->crt_digest.dit_update(tfm, sg, nsg);
662}
663
664static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
665{
666 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
667 tfm->crt_digest.dit_final(tfm, out);
668}
669
670static inline void crypto_digest_digest(struct crypto_tfm *tfm,
671 struct scatterlist *sg,
672 unsigned int nsg, u8 *out)
673{
674 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
675 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
676}
677
678static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
679 const u8 *key, unsigned int keylen)
680{
681 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
683}
684
Herbert Xu7226bc872006-08-21 21:40:49 +1000685static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
686 struct scatterlist *dst,
687 struct scatterlist *src,
688 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
690 struct scatterlist *dst,
691 struct scatterlist *src,
692 unsigned int nbytes)
693{
694 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
695 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
696}
697
Herbert Xu7226bc872006-08-21 21:40:49 +1000698static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
699 struct scatterlist *dst,
700 struct scatterlist *src,
701 unsigned int nbytes, u8 *iv) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
703 struct scatterlist *dst,
704 struct scatterlist *src,
705 unsigned int nbytes, u8 *iv)
706{
707 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
709}
710
Herbert Xu7226bc872006-08-21 21:40:49 +1000711static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
712 struct scatterlist *dst,
713 struct scatterlist *src,
714 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
716 struct scatterlist *dst,
717 struct scatterlist *src,
718 unsigned int nbytes)
719{
720 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
721 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
722}
723
Herbert Xu7226bc872006-08-21 21:40:49 +1000724static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
725 struct scatterlist *dst,
726 struct scatterlist *src,
727 unsigned int nbytes, u8 *iv) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
729 struct scatterlist *dst,
730 struct scatterlist *src,
731 unsigned int nbytes, u8 *iv)
732{
733 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
735}
736
Herbert Xu7226bc872006-08-21 21:40:49 +1000737static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
738 const u8 *src, unsigned int len) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
740 const u8 *src, unsigned int len)
741{
742 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
743 memcpy(tfm->crt_cipher.cit_iv, src, len);
744}
745
Herbert Xu7226bc872006-08-21 21:40:49 +1000746static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
747 u8 *dst, unsigned int len) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
749 u8 *dst, unsigned int len)
750{
751 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
752 memcpy(dst, tfm->crt_cipher.cit_iv, len);
753}
754
755static inline int crypto_comp_compress(struct crypto_tfm *tfm,
756 const u8 *src, unsigned int slen,
757 u8 *dst, unsigned int *dlen)
758{
759 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
760 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
761}
762
763static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
764 const u8 *src, unsigned int slen,
765 u8 *dst, unsigned int *dlen)
766{
767 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
768 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
769}
770
771/*
772 * HMAC support.
773 */
774#ifdef CONFIG_CRYPTO_HMAC
775void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
776void crypto_hmac_update(struct crypto_tfm *tfm,
777 struct scatterlist *sg, unsigned int nsg);
778void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
779 unsigned int *keylen, u8 *out);
780void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
781 struct scatterlist *sg, unsigned int nsg, u8 *out);
782#endif /* CONFIG_CRYPTO_HMAC */
783
784#endif /* _LINUX_CRYPTO_H */
785