2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
27 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
28 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
29 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e
30 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
31 #define CRYPTO_ALG_ASYNC 0x00000080
33 #define CRYPTO_MAX_ALG_NAME 64
35 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
36 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
39 struct crypto_blkcipher;
43 struct blkcipher_desc {
44 struct crypto_blkcipher *tfm;
49 struct blkcipher_alg {
50 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
52 int (*encrypt)(struct blkcipher_desc *desc,
53 struct scatterlist *dst, struct scatterlist *src,
55 int (*decrypt)(struct blkcipher_desc *desc,
56 struct scatterlist *dst, struct scatterlist *src,
60 #define cra_blkcipher cra_u.blkcipher
63 struct list_head cra_list;
64 struct list_head cra_users;
68 char cra_name[CRYPTO_MAX_ALG_NAME];
70 const struct crypto_type *cra_type;
73 struct blkcipher_alg blkcipher;
76 int (*cra_init)(struct crypto_tfm *tfm);
77 void (*cra_exit)(struct crypto_tfm *tfm);
78 } CRYPTO_MINALIGN_ATTR;
80 int crypto_register_alg(struct crypto_alg *alg);
82 struct blkcipher_tfm {
83 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
85 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
86 struct scatterlist *src, unsigned nbytes);
87 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
88 struct scatterlist *src, unsigned nbytes);
94 struct blkcipher_tfm crt_blkcipher;
96 void (*exit)(struct crypto_tfm *tfm);
98 struct crypto_alg *__crt_alg;
99 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
102 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
103 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
105 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
107 return crypto_destroy_tfm(tfm, tfm);
110 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
112 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
115 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
117 return tfm->__crt_ctx;
120 struct crypto_blkcipher {
121 struct crypto_tfm base;
124 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
125 struct crypto_tfm *tfm)
127 return (struct crypto_blkcipher *)tfm;
130 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
131 struct crypto_tfm *tfm)
133 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
134 return __crypto_blkcipher_cast(tfm);
137 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
138 const char *alg_name, u32 type, u32 mask)
140 type &= ~CRYPTO_ALG_TYPE_MASK;
141 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
142 mask |= CRYPTO_ALG_TYPE_MASK;
144 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
147 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
149 crypto_free_tfm(&tfm->base);
152 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
153 struct crypto_blkcipher *tfm)
155 return &tfm->base.crt_blkcipher;
158 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
159 const u8 *key, unsigned keylen)
161 return crypto_blkcipher_crt(tfm)->setkey(&tfm->base, key, keylen);
164 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
165 struct scatterlist *dst,
166 struct scatterlist *src,
169 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
172 #endif /* _LINUX_CRYPTO_H */