]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Cryptographic API. | |
4 | * | |
fb4f10ed | 5 | * TEA, XTEA, and XETA crypto alogrithms |
1da177e4 LT |
6 | * |
7 | * The TEA and Xtended TEA algorithms were developed by David Wheeler | |
8 | * and Roger Needham at the Computer Laboratory of Cambridge University. | |
9 | * | |
fb4f10ed AG |
10 | * Due to the order of evaluation in XTEA many people have incorrectly |
11 | * implemented it. XETA (XTEA in the wrong order), exists for | |
12 | * compatibility with these implementations. | |
13 | * | |
1da177e4 | 14 | * Copyright (c) 2004 Aaron Grothe [email protected] |
1da177e4 LT |
15 | */ |
16 | ||
17 | #include <linux/init.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/mm.h> | |
06ace7a9 | 20 | #include <asm/byteorder.h> |
1da177e4 | 21 | #include <linux/crypto.h> |
06ace7a9 | 22 | #include <linux/types.h> |
1da177e4 LT |
23 | |
24 | #define TEA_KEY_SIZE 16 | |
25 | #define TEA_BLOCK_SIZE 8 | |
26 | #define TEA_ROUNDS 32 | |
27 | #define TEA_DELTA 0x9e3779b9 | |
28 | ||
29 | #define XTEA_KEY_SIZE 16 | |
30 | #define XTEA_BLOCK_SIZE 8 | |
31 | #define XTEA_ROUNDS 32 | |
32 | #define XTEA_DELTA 0x9e3779b9 | |
33 | ||
1da177e4 LT |
34 | struct tea_ctx { |
35 | u32 KEY[4]; | |
36 | }; | |
37 | ||
38 | struct xtea_ctx { | |
39 | u32 KEY[4]; | |
40 | }; | |
41 | ||
6c2bb98b | 42 | static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key, |
560c06ae | 43 | unsigned int key_len) |
6c2bb98b HX |
44 | { |
45 | struct tea_ctx *ctx = crypto_tfm_ctx(tfm); | |
06ace7a9 | 46 | const __le32 *key = (const __le32 *)in_key; |
1da177e4 | 47 | |
06ace7a9 HX |
48 | ctx->KEY[0] = le32_to_cpu(key[0]); |
49 | ctx->KEY[1] = le32_to_cpu(key[1]); | |
50 | ctx->KEY[2] = le32_to_cpu(key[2]); | |
51 | ctx->KEY[3] = le32_to_cpu(key[3]); | |
1da177e4 LT |
52 | |
53 | return 0; | |
54 | ||
55 | } | |
56 | ||
6c2bb98b HX |
57 | static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
58 | { | |
1da177e4 LT |
59 | u32 y, z, n, sum = 0; |
60 | u32 k0, k1, k2, k3; | |
6c2bb98b | 61 | struct tea_ctx *ctx = crypto_tfm_ctx(tfm); |
06ace7a9 HX |
62 | const __le32 *in = (const __le32 *)src; |
63 | __le32 *out = (__le32 *)dst; | |
1da177e4 | 64 | |
06ace7a9 HX |
65 | y = le32_to_cpu(in[0]); |
66 | z = le32_to_cpu(in[1]); | |
1da177e4 LT |
67 | |
68 | k0 = ctx->KEY[0]; | |
69 | k1 = ctx->KEY[1]; | |
70 | k2 = ctx->KEY[2]; | |
71 | k3 = ctx->KEY[3]; | |
72 | ||
73 | n = TEA_ROUNDS; | |
74 | ||
75 | while (n-- > 0) { | |
76 | sum += TEA_DELTA; | |
77 | y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1); | |
78 | z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3); | |
79 | } | |
80 | ||
06ace7a9 HX |
81 | out[0] = cpu_to_le32(y); |
82 | out[1] = cpu_to_le32(z); | |
1da177e4 LT |
83 | } |
84 | ||
6c2bb98b HX |
85 | static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
86 | { | |
1da177e4 LT |
87 | u32 y, z, n, sum; |
88 | u32 k0, k1, k2, k3; | |
6c2bb98b | 89 | struct tea_ctx *ctx = crypto_tfm_ctx(tfm); |
06ace7a9 HX |
90 | const __le32 *in = (const __le32 *)src; |
91 | __le32 *out = (__le32 *)dst; | |
1da177e4 | 92 | |
06ace7a9 HX |
93 | y = le32_to_cpu(in[0]); |
94 | z = le32_to_cpu(in[1]); | |
1da177e4 LT |
95 | |
96 | k0 = ctx->KEY[0]; | |
97 | k1 = ctx->KEY[1]; | |
98 | k2 = ctx->KEY[2]; | |
99 | k3 = ctx->KEY[3]; | |
100 | ||
101 | sum = TEA_DELTA << 5; | |
102 | ||
103 | n = TEA_ROUNDS; | |
104 | ||
105 | while (n-- > 0) { | |
106 | z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3); | |
107 | y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1); | |
108 | sum -= TEA_DELTA; | |
109 | } | |
110 | ||
06ace7a9 HX |
111 | out[0] = cpu_to_le32(y); |
112 | out[1] = cpu_to_le32(z); | |
1da177e4 LT |
113 | } |
114 | ||
6c2bb98b | 115 | static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key, |
560c06ae | 116 | unsigned int key_len) |
6c2bb98b HX |
117 | { |
118 | struct xtea_ctx *ctx = crypto_tfm_ctx(tfm); | |
06ace7a9 | 119 | const __le32 *key = (const __le32 *)in_key; |
1da177e4 | 120 | |
06ace7a9 HX |
121 | ctx->KEY[0] = le32_to_cpu(key[0]); |
122 | ctx->KEY[1] = le32_to_cpu(key[1]); | |
123 | ctx->KEY[2] = le32_to_cpu(key[2]); | |
124 | ctx->KEY[3] = le32_to_cpu(key[3]); | |
1da177e4 LT |
125 | |
126 | return 0; | |
127 | ||
128 | } | |
129 | ||
6c2bb98b HX |
130 | static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
131 | { | |
1da177e4 LT |
132 | u32 y, z, sum = 0; |
133 | u32 limit = XTEA_DELTA * XTEA_ROUNDS; | |
6c2bb98b | 134 | struct xtea_ctx *ctx = crypto_tfm_ctx(tfm); |
06ace7a9 HX |
135 | const __le32 *in = (const __le32 *)src; |
136 | __le32 *out = (__le32 *)dst; | |
1da177e4 | 137 | |
06ace7a9 HX |
138 | y = le32_to_cpu(in[0]); |
139 | z = le32_to_cpu(in[1]); | |
1da177e4 LT |
140 | |
141 | while (sum != limit) { | |
fb4f10ed | 142 | y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); |
1da177e4 | 143 | sum += XTEA_DELTA; |
fb4f10ed | 144 | z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]); |
1da177e4 LT |
145 | } |
146 | ||
06ace7a9 HX |
147 | out[0] = cpu_to_le32(y); |
148 | out[1] = cpu_to_le32(z); | |
1da177e4 LT |
149 | } |
150 | ||
6c2bb98b HX |
151 | static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
152 | { | |
1da177e4 | 153 | u32 y, z, sum; |
6c2bb98b | 154 | struct tea_ctx *ctx = crypto_tfm_ctx(tfm); |
06ace7a9 HX |
155 | const __le32 *in = (const __le32 *)src; |
156 | __le32 *out = (__le32 *)dst; | |
1da177e4 | 157 | |
06ace7a9 HX |
158 | y = le32_to_cpu(in[0]); |
159 | z = le32_to_cpu(in[1]); | |
1da177e4 LT |
160 | |
161 | sum = XTEA_DELTA * XTEA_ROUNDS; | |
162 | ||
fb4f10ed AG |
163 | while (sum) { |
164 | z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]); | |
165 | sum -= XTEA_DELTA; | |
166 | y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]); | |
167 | } | |
168 | ||
06ace7a9 HX |
169 | out[0] = cpu_to_le32(y); |
170 | out[1] = cpu_to_le32(z); | |
fb4f10ed AG |
171 | } |
172 | ||
173 | ||
6c2bb98b HX |
174 | static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
175 | { | |
fb4f10ed AG |
176 | u32 y, z, sum = 0; |
177 | u32 limit = XTEA_DELTA * XTEA_ROUNDS; | |
6c2bb98b | 178 | struct xtea_ctx *ctx = crypto_tfm_ctx(tfm); |
06ace7a9 HX |
179 | const __le32 *in = (const __le32 *)src; |
180 | __le32 *out = (__le32 *)dst; | |
fb4f10ed | 181 | |
06ace7a9 HX |
182 | y = le32_to_cpu(in[0]); |
183 | z = le32_to_cpu(in[1]); | |
fb4f10ed AG |
184 | |
185 | while (sum != limit) { | |
186 | y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3]; | |
187 | sum += XTEA_DELTA; | |
188 | z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3]; | |
189 | } | |
190 | ||
06ace7a9 HX |
191 | out[0] = cpu_to_le32(y); |
192 | out[1] = cpu_to_le32(z); | |
fb4f10ed AG |
193 | } |
194 | ||
6c2bb98b HX |
195 | static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
196 | { | |
fb4f10ed | 197 | u32 y, z, sum; |
6c2bb98b | 198 | struct tea_ctx *ctx = crypto_tfm_ctx(tfm); |
06ace7a9 HX |
199 | const __le32 *in = (const __le32 *)src; |
200 | __le32 *out = (__le32 *)dst; | |
fb4f10ed | 201 | |
06ace7a9 HX |
202 | y = le32_to_cpu(in[0]); |
203 | z = le32_to_cpu(in[1]); | |
fb4f10ed AG |
204 | |
205 | sum = XTEA_DELTA * XTEA_ROUNDS; | |
206 | ||
1da177e4 LT |
207 | while (sum) { |
208 | z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3]; | |
209 | sum -= XTEA_DELTA; | |
210 | y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3]; | |
211 | } | |
212 | ||
06ace7a9 HX |
213 | out[0] = cpu_to_le32(y); |
214 | out[1] = cpu_to_le32(z); | |
1da177e4 LT |
215 | } |
216 | ||
738206d3 | 217 | static struct crypto_alg tea_algs[3] = { { |
1da177e4 | 218 | .cra_name = "tea", |
d6ebf528 | 219 | .cra_driver_name = "tea-generic", |
1da177e4 LT |
220 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
221 | .cra_blocksize = TEA_BLOCK_SIZE, | |
222 | .cra_ctxsize = sizeof (struct tea_ctx), | |
a429d260 | 223 | .cra_alignmask = 3, |
1da177e4 | 224 | .cra_module = THIS_MODULE, |
1da177e4 LT |
225 | .cra_u = { .cipher = { |
226 | .cia_min_keysize = TEA_KEY_SIZE, | |
227 | .cia_max_keysize = TEA_KEY_SIZE, | |
228 | .cia_setkey = tea_setkey, | |
229 | .cia_encrypt = tea_encrypt, | |
230 | .cia_decrypt = tea_decrypt } } | |
738206d3 | 231 | }, { |
1da177e4 | 232 | .cra_name = "xtea", |
d6ebf528 | 233 | .cra_driver_name = "xtea-generic", |
1da177e4 LT |
234 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
235 | .cra_blocksize = XTEA_BLOCK_SIZE, | |
236 | .cra_ctxsize = sizeof (struct xtea_ctx), | |
a429d260 | 237 | .cra_alignmask = 3, |
1da177e4 | 238 | .cra_module = THIS_MODULE, |
1da177e4 LT |
239 | .cra_u = { .cipher = { |
240 | .cia_min_keysize = XTEA_KEY_SIZE, | |
241 | .cia_max_keysize = XTEA_KEY_SIZE, | |
242 | .cia_setkey = xtea_setkey, | |
243 | .cia_encrypt = xtea_encrypt, | |
244 | .cia_decrypt = xtea_decrypt } } | |
738206d3 | 245 | }, { |
fb4f10ed | 246 | .cra_name = "xeta", |
d6ebf528 | 247 | .cra_driver_name = "xeta-generic", |
fb4f10ed AG |
248 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
249 | .cra_blocksize = XTEA_BLOCK_SIZE, | |
250 | .cra_ctxsize = sizeof (struct xtea_ctx), | |
a429d260 | 251 | .cra_alignmask = 3, |
fb4f10ed | 252 | .cra_module = THIS_MODULE, |
fb4f10ed AG |
253 | .cra_u = { .cipher = { |
254 | .cia_min_keysize = XTEA_KEY_SIZE, | |
255 | .cia_max_keysize = XTEA_KEY_SIZE, | |
256 | .cia_setkey = xtea_setkey, | |
257 | .cia_encrypt = xeta_encrypt, | |
258 | .cia_decrypt = xeta_decrypt } } | |
738206d3 | 259 | } }; |
fb4f10ed | 260 | |
3af5b90b | 261 | static int __init tea_mod_init(void) |
1da177e4 | 262 | { |
738206d3 | 263 | return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs)); |
1da177e4 LT |
264 | } |
265 | ||
3af5b90b | 266 | static void __exit tea_mod_fini(void) |
1da177e4 | 267 | { |
738206d3 | 268 | crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs)); |
1da177e4 LT |
269 | } |
270 | ||
3e14dcf7 | 271 | MODULE_ALIAS_CRYPTO("tea"); |
5d26a105 KC |
272 | MODULE_ALIAS_CRYPTO("xtea"); |
273 | MODULE_ALIAS_CRYPTO("xeta"); | |
1da177e4 | 274 | |
c4741b23 | 275 | subsys_initcall(tea_mod_init); |
3af5b90b | 276 | module_exit(tea_mod_fini); |
1da177e4 LT |
277 | |
278 | MODULE_LICENSE("GPL"); | |
fb4f10ed | 279 | MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms"); |