]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/crypto.h
Update bcachefs sources to 3610542890 bcachefs: Convert to skcipher interface for...
[bcachefs-tools-debian] / include / linux / crypto.h
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)
6  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7  *
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
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>
26
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
32
33 #define CRYPTO_MAX_ALG_NAME             64
34
35 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
36 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
37
38 struct scatterlist;
39 struct crypto_blkcipher;
40 struct crypto_tfm;
41 struct crypto_type;
42
43 struct blkcipher_desc {
44         struct crypto_blkcipher *tfm;
45         void                    *info;
46         u32                     flags;
47 };
48
49 struct blkcipher_alg {
50         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
51                       unsigned keylen);
52         int (*encrypt)(struct blkcipher_desc *desc,
53                        struct scatterlist *dst, struct scatterlist *src,
54                        unsigned nbytes);
55         int (*decrypt)(struct blkcipher_desc *desc,
56                        struct scatterlist *dst, struct scatterlist *src,
57                        unsigned nbytes);
58 };
59
60 #define cra_blkcipher   cra_u.blkcipher
61
62 struct crypto_alg {
63         struct list_head        cra_list;
64         struct list_head        cra_users;
65
66         u32                     cra_flags;
67         unsigned                cra_ctxsize;
68         char                    cra_name[CRYPTO_MAX_ALG_NAME];
69
70         const struct crypto_type *cra_type;
71
72         union {
73                 struct blkcipher_alg blkcipher;
74         } cra_u;
75
76         int (*cra_init)(struct crypto_tfm *tfm);
77         void (*cra_exit)(struct crypto_tfm *tfm);
78 } CRYPTO_MINALIGN_ATTR;
79
80 int crypto_register_alg(struct crypto_alg *alg);
81
82 struct blkcipher_tfm {
83         int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
84                       unsigned keylen);
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);
89 };
90
91 struct crypto_tfm {
92         u32                     crt_flags;
93
94         struct blkcipher_tfm    crt_blkcipher;
95
96         void (*exit)(struct crypto_tfm *tfm);
97
98         struct crypto_alg       *__crt_alg;
99         void                    *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
100 };
101
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);
104
105 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
106 {
107         return crypto_destroy_tfm(tfm, tfm);
108 }
109
110 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
111 {
112         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
113 }
114
115 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
116 {
117         return tfm->__crt_ctx;
118 }
119
120 struct crypto_blkcipher {
121         struct crypto_tfm base;
122 };
123
124 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
125         struct crypto_tfm *tfm)
126 {
127         return (struct crypto_blkcipher *)tfm;
128 }
129
130 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
131         struct crypto_tfm *tfm)
132 {
133         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
134         return __crypto_blkcipher_cast(tfm);
135 }
136
137 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
138         const char *alg_name, u32 type, u32 mask)
139 {
140         type &= ~CRYPTO_ALG_TYPE_MASK;
141         type |= CRYPTO_ALG_TYPE_BLKCIPHER;
142         mask |= CRYPTO_ALG_TYPE_MASK;
143
144         return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
145 }
146
147 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
148 {
149         crypto_free_tfm(&tfm->base);
150 }
151
152 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
153         struct crypto_blkcipher *tfm)
154 {
155         return &tfm->base.crt_blkcipher;
156 }
157
158 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
159                                           const u8 *key, unsigned keylen)
160 {
161         return crypto_blkcipher_crt(tfm)->setkey(&tfm->base, key, keylen);
162 }
163
164 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
165                                               struct scatterlist *dst,
166                                               struct scatterlist *src,
167                                               unsigned nbytes)
168 {
169         return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
170 }
171
172 #endif  /* _LINUX_CRYPTO_H */
173