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