1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41#include <asm/byteorder.h>
42#include <crypto/twofish.h>
43#include <linux/module.h>
44#include <linux/init.h>
45#include <linux/types.h>
46#include <linux/errno.h>
47#include <linux/crypto.h>
48#include <linux/bitops.h>
49
50
51
52
53
54#define G1(a) \
55 (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
56 ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
57
58#define G2(b) \
59 (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
60 ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
61
62
63
64
65
66
67#define ENCROUND(n, a, b, c, d) \
68 x = G1 (a); y = G2 (b); \
69 x += y; y += x + ctx->k[2 * (n) + 1]; \
70 (c) ^= x + ctx->k[2 * (n)]; \
71 (c) = ror32((c), 1); \
72 (d) = rol32((d), 1) ^ y
73
74#define DECROUND(n, a, b, c, d) \
75 x = G1 (a); y = G2 (b); \
76 x += y; y += x; \
77 (d) ^= y + ctx->k[2 * (n) + 1]; \
78 (d) = ror32((d), 1); \
79 (c) = rol32((c), 1); \
80 (c) ^= (x + ctx->k[2 * (n)])
81
82
83
84
85#define ENCCYCLE(n) \
86 ENCROUND (2 * (n), a, b, c, d); \
87 ENCROUND (2 * (n) + 1, c, d, a, b)
88
89#define DECCYCLE(n) \
90 DECROUND (2 * (n) + 1, c, d, a, b); \
91 DECROUND (2 * (n), a, b, c, d)
92
93
94
95
96
97
98
99#define INPACK(n, x, m) \
100 x = le32_to_cpu(src[n]) ^ ctx->w[m]
101
102#define OUTUNPACK(n, x, m) \
103 x ^= ctx->w[m]; \
104 dst[n] = cpu_to_le32(x)
105
106
107
108
109static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
110{
111 struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
112 const __le32 *src = (const __le32 *)in;
113 __le32 *dst = (__le32 *)out;
114
115
116 u32 a, b, c, d;
117
118
119 u32 x, y;
120
121
122 INPACK (0, a, 0);
123 INPACK (1, b, 1);
124 INPACK (2, c, 2);
125 INPACK (3, d, 3);
126
127
128 ENCCYCLE (0);
129 ENCCYCLE (1);
130 ENCCYCLE (2);
131 ENCCYCLE (3);
132 ENCCYCLE (4);
133 ENCCYCLE (5);
134 ENCCYCLE (6);
135 ENCCYCLE (7);
136
137
138 OUTUNPACK (0, c, 4);
139 OUTUNPACK (1, d, 5);
140 OUTUNPACK (2, a, 6);
141 OUTUNPACK (3, b, 7);
142
143}
144
145
146static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
147{
148 struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
149 const __le32 *src = (const __le32 *)in;
150 __le32 *dst = (__le32 *)out;
151
152
153 u32 a, b, c, d;
154
155
156 u32 x, y;
157
158
159 INPACK (0, c, 4);
160 INPACK (1, d, 5);
161 INPACK (2, a, 6);
162 INPACK (3, b, 7);
163
164
165 DECCYCLE (7);
166 DECCYCLE (6);
167 DECCYCLE (5);
168 DECCYCLE (4);
169 DECCYCLE (3);
170 DECCYCLE (2);
171 DECCYCLE (1);
172 DECCYCLE (0);
173
174
175 OUTUNPACK (0, a, 0);
176 OUTUNPACK (1, b, 1);
177 OUTUNPACK (2, c, 2);
178 OUTUNPACK (3, d, 3);
179
180}
181
182static struct crypto_alg alg = {
183 .cra_name = "twofish",
184 .cra_driver_name = "twofish-generic",
185 .cra_priority = 100,
186 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
187 .cra_blocksize = TF_BLOCK_SIZE,
188 .cra_ctxsize = sizeof(struct twofish_ctx),
189 .cra_alignmask = 3,
190 .cra_module = THIS_MODULE,
191 .cra_u = { .cipher = {
192 .cia_min_keysize = TF_MIN_KEY_SIZE,
193 .cia_max_keysize = TF_MAX_KEY_SIZE,
194 .cia_setkey = twofish_setkey,
195 .cia_encrypt = twofish_encrypt,
196 .cia_decrypt = twofish_decrypt } }
197};
198
199static int __init twofish_mod_init(void)
200{
201 return crypto_register_alg(&alg);
202}
203
204static void __exit twofish_mod_fini(void)
205{
206 crypto_unregister_alg(&alg);
207}
208
209module_init(twofish_mod_init);
210module_exit(twofish_mod_fini);
211
212MODULE_LICENSE("GPL");
213MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
214MODULE_ALIAS_CRYPTO("twofish");
215MODULE_ALIAS_CRYPTO("twofish-generic");
216