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