blob: 1c54e26fa529342d6ee893b7636233f9c317f3ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
Aaron Grothefb4f10e2005-09-01 17:42:46 -07004 * TEA, XTEA, and XETA crypto alogrithms
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * The TEA and Xtended TEA algorithms were developed by David Wheeler
7 * and Roger Needham at the Computer Laboratory of Cambridge University.
8 *
Aaron Grothefb4f10e2005-09-01 17:42:46 -07009 * Due to the order of evaluation in XTEA many people have incorrectly
10 * implemented it. XETA (XTEA in the wrong order), exists for
11 * compatibility with these implementations.
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/mm.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110025#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/scatterlist.h>
27#include <linux/crypto.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110028#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define TEA_KEY_SIZE 16
31#define TEA_BLOCK_SIZE 8
32#define TEA_ROUNDS 32
33#define TEA_DELTA 0x9e3779b9
34
35#define XTEA_KEY_SIZE 16
36#define XTEA_BLOCK_SIZE 8
37#define XTEA_ROUNDS 32
38#define XTEA_DELTA 0x9e3779b9
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040struct tea_ctx {
41 u32 KEY[4];
42};
43
44struct xtea_ctx {
45 u32 KEY[4];
46};
47
Herbert Xu6c2bb982006-05-16 22:09:29 +100048static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100049 unsigned int key_len)
Herbert Xu6c2bb982006-05-16 22:09:29 +100050{
51 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110052 const __le32 *key = (const __le32 *)in_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Herbert Xu06ace7a2005-10-30 21:25:15 +110054 ctx->KEY[0] = le32_to_cpu(key[0]);
55 ctx->KEY[1] = le32_to_cpu(key[1]);
56 ctx->KEY[2] = le32_to_cpu(key[2]);
57 ctx->KEY[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 return 0;
60
61}
62
Herbert Xu6c2bb982006-05-16 22:09:29 +100063static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
64{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 u32 y, z, n, sum = 0;
66 u32 k0, k1, k2, k3;
Herbert Xu6c2bb982006-05-16 22:09:29 +100067 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110068 const __le32 *in = (const __le32 *)src;
69 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Herbert Xu06ace7a2005-10-30 21:25:15 +110071 y = le32_to_cpu(in[0]);
72 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 k0 = ctx->KEY[0];
75 k1 = ctx->KEY[1];
76 k2 = ctx->KEY[2];
77 k3 = ctx->KEY[3];
78
79 n = TEA_ROUNDS;
80
81 while (n-- > 0) {
82 sum += TEA_DELTA;
83 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
84 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
85 }
86
Herbert Xu06ace7a2005-10-30 21:25:15 +110087 out[0] = cpu_to_le32(y);
88 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Herbert Xu6c2bb982006-05-16 22:09:29 +100091static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
92{
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 u32 y, z, n, sum;
94 u32 k0, k1, k2, k3;
Herbert Xu6c2bb982006-05-16 22:09:29 +100095 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110096 const __le32 *in = (const __le32 *)src;
97 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Herbert Xu06ace7a2005-10-30 21:25:15 +110099 y = le32_to_cpu(in[0]);
100 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 k0 = ctx->KEY[0];
103 k1 = ctx->KEY[1];
104 k2 = ctx->KEY[2];
105 k3 = ctx->KEY[3];
106
107 sum = TEA_DELTA << 5;
108
109 n = TEA_ROUNDS;
110
111 while (n-- > 0) {
112 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
113 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
114 sum -= TEA_DELTA;
115 }
116
Herbert Xu06ace7a2005-10-30 21:25:15 +1100117 out[0] = cpu_to_le32(y);
118 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Herbert Xu6c2bb982006-05-16 22:09:29 +1000121static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000122 unsigned int key_len)
Herbert Xu6c2bb982006-05-16 22:09:29 +1000123{
124 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100125 const __le32 *key = (const __le32 *)in_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu06ace7a2005-10-30 21:25:15 +1100127 ctx->KEY[0] = le32_to_cpu(key[0]);
128 ctx->KEY[1] = le32_to_cpu(key[1]);
129 ctx->KEY[2] = le32_to_cpu(key[2]);
130 ctx->KEY[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return 0;
133
134}
135
Herbert Xu6c2bb982006-05-16 22:09:29 +1000136static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 u32 y, z, sum = 0;
139 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000140 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100141 const __le32 *in = (const __le32 *)src;
142 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Herbert Xu06ace7a2005-10-30 21:25:15 +1100144 y = le32_to_cpu(in[0]);
145 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 while (sum != limit) {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700148 y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 sum += XTEA_DELTA;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700150 z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Herbert Xu06ace7a2005-10-30 21:25:15 +1100153 out[0] = cpu_to_le32(y);
154 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Herbert Xu6c2bb982006-05-16 22:09:29 +1000157static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
158{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 u32 y, z, sum;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000160 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100161 const __le32 *in = (const __le32 *)src;
162 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Herbert Xu06ace7a2005-10-30 21:25:15 +1100164 y = le32_to_cpu(in[0]);
165 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 sum = XTEA_DELTA * XTEA_ROUNDS;
168
169 while (sum) {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700170 z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
171 sum -= XTEA_DELTA;
172 y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
173 }
174
Herbert Xu06ace7a2005-10-30 21:25:15 +1100175 out[0] = cpu_to_le32(y);
176 out[1] = cpu_to_le32(z);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700177}
178
179
Herbert Xu6c2bb982006-05-16 22:09:29 +1000180static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
181{
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700182 u32 y, z, sum = 0;
183 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000184 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100185 const __le32 *in = (const __le32 *)src;
186 __le32 *out = (__le32 *)dst;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700187
Herbert Xu06ace7a2005-10-30 21:25:15 +1100188 y = le32_to_cpu(in[0]);
189 z = le32_to_cpu(in[1]);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700190
191 while (sum != limit) {
192 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
193 sum += XTEA_DELTA;
194 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
195 }
196
Herbert Xu06ace7a2005-10-30 21:25:15 +1100197 out[0] = cpu_to_le32(y);
198 out[1] = cpu_to_le32(z);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700199}
200
Herbert Xu6c2bb982006-05-16 22:09:29 +1000201static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
202{
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700203 u32 y, z, sum;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000204 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100205 const __le32 *in = (const __le32 *)src;
206 __le32 *out = (__le32 *)dst;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700207
Herbert Xu06ace7a2005-10-30 21:25:15 +1100208 y = le32_to_cpu(in[0]);
209 z = le32_to_cpu(in[1]);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700210
211 sum = XTEA_DELTA * XTEA_ROUNDS;
212
213 while (sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
215 sum -= XTEA_DELTA;
216 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
217 }
218
Herbert Xu06ace7a2005-10-30 21:25:15 +1100219 out[0] = cpu_to_le32(y);
220 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static struct crypto_alg tea_alg = {
224 .cra_name = "tea",
225 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
226 .cra_blocksize = TEA_BLOCK_SIZE,
227 .cra_ctxsize = sizeof (struct tea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100228 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .cra_module = THIS_MODULE,
230 .cra_list = LIST_HEAD_INIT(tea_alg.cra_list),
231 .cra_u = { .cipher = {
232 .cia_min_keysize = TEA_KEY_SIZE,
233 .cia_max_keysize = TEA_KEY_SIZE,
234 .cia_setkey = tea_setkey,
235 .cia_encrypt = tea_encrypt,
236 .cia_decrypt = tea_decrypt } }
237};
238
239static struct crypto_alg xtea_alg = {
240 .cra_name = "xtea",
241 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
242 .cra_blocksize = XTEA_BLOCK_SIZE,
243 .cra_ctxsize = sizeof (struct xtea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100244 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .cra_module = THIS_MODULE,
246 .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
247 .cra_u = { .cipher = {
248 .cia_min_keysize = XTEA_KEY_SIZE,
249 .cia_max_keysize = XTEA_KEY_SIZE,
250 .cia_setkey = xtea_setkey,
251 .cia_encrypt = xtea_encrypt,
252 .cia_decrypt = xtea_decrypt } }
253};
254
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700255static struct crypto_alg xeta_alg = {
256 .cra_name = "xeta",
257 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
258 .cra_blocksize = XTEA_BLOCK_SIZE,
259 .cra_ctxsize = sizeof (struct xtea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100260 .cra_alignmask = 3,
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700261 .cra_module = THIS_MODULE,
262 .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
263 .cra_u = { .cipher = {
264 .cia_min_keysize = XTEA_KEY_SIZE,
265 .cia_max_keysize = XTEA_KEY_SIZE,
266 .cia_setkey = xtea_setkey,
267 .cia_encrypt = xeta_encrypt,
268 .cia_decrypt = xeta_decrypt } }
269};
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int __init init(void)
272{
273 int ret = 0;
274
275 ret = crypto_register_alg(&tea_alg);
276 if (ret < 0)
277 goto out;
278
279 ret = crypto_register_alg(&xtea_alg);
280 if (ret < 0) {
281 crypto_unregister_alg(&tea_alg);
282 goto out;
283 }
284
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700285 ret = crypto_register_alg(&xeta_alg);
286 if (ret < 0) {
287 crypto_unregister_alg(&tea_alg);
288 crypto_unregister_alg(&xtea_alg);
289 goto out;
290 }
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292out:
293 return ret;
294}
295
296static void __exit fini(void)
297{
298 crypto_unregister_alg(&tea_alg);
299 crypto_unregister_alg(&xtea_alg);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700300 crypto_unregister_alg(&xeta_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303MODULE_ALIAS("xtea");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700304MODULE_ALIAS("xeta");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306module_init(init);
307module_exit(fini);
308
309MODULE_LICENSE("GPL");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700310MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");