blob: 5367adc82fc9d9cc93827496bbe8628195072033 [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,
49 unsigned int key_len, u32 *flags)
50{
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
54 if (key_len != 16)
55 {
56 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
57 return -EINVAL;
58 }
59
Herbert Xu06ace7a2005-10-30 21:25:15 +110060 ctx->KEY[0] = le32_to_cpu(key[0]);
61 ctx->KEY[1] = le32_to_cpu(key[1]);
62 ctx->KEY[2] = le32_to_cpu(key[2]);
63 ctx->KEY[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 return 0;
66
67}
68
Herbert Xu6c2bb982006-05-16 22:09:29 +100069static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
70{
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u32 y, z, n, sum = 0;
72 u32 k0, k1, k2, k3;
Herbert Xu6c2bb982006-05-16 22:09:29 +100073 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110074 const __le32 *in = (const __le32 *)src;
75 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Herbert Xu06ace7a2005-10-30 21:25:15 +110077 y = le32_to_cpu(in[0]);
78 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 k0 = ctx->KEY[0];
81 k1 = ctx->KEY[1];
82 k2 = ctx->KEY[2];
83 k3 = ctx->KEY[3];
84
85 n = TEA_ROUNDS;
86
87 while (n-- > 0) {
88 sum += TEA_DELTA;
89 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
90 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
91 }
92
Herbert Xu06ace7a2005-10-30 21:25:15 +110093 out[0] = cpu_to_le32(y);
94 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Herbert Xu6c2bb982006-05-16 22:09:29 +100097static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
98{
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 u32 y, z, n, sum;
100 u32 k0, k1, k2, k3;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000101 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100102 const __le32 *in = (const __le32 *)src;
103 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Herbert Xu06ace7a2005-10-30 21:25:15 +1100105 y = le32_to_cpu(in[0]);
106 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 k0 = ctx->KEY[0];
109 k1 = ctx->KEY[1];
110 k2 = ctx->KEY[2];
111 k3 = ctx->KEY[3];
112
113 sum = TEA_DELTA << 5;
114
115 n = TEA_ROUNDS;
116
117 while (n-- > 0) {
118 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
119 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
120 sum -= TEA_DELTA;
121 }
122
Herbert Xu06ace7a2005-10-30 21:25:15 +1100123 out[0] = cpu_to_le32(y);
124 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Herbert Xu6c2bb982006-05-16 22:09:29 +1000127static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
128 unsigned int key_len, u32 *flags)
129{
130 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100131 const __le32 *key = (const __le32 *)in_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (key_len != 16)
134 {
135 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
136 return -EINVAL;
137 }
138
Herbert Xu06ace7a2005-10-30 21:25:15 +1100139 ctx->KEY[0] = le32_to_cpu(key[0]);
140 ctx->KEY[1] = le32_to_cpu(key[1]);
141 ctx->KEY[2] = le32_to_cpu(key[2]);
142 ctx->KEY[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return 0;
145
146}
147
Herbert Xu6c2bb982006-05-16 22:09:29 +1000148static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
149{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 u32 y, z, sum = 0;
151 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000152 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100153 const __le32 *in = (const __le32 *)src;
154 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Herbert Xu06ace7a2005-10-30 21:25:15 +1100156 y = le32_to_cpu(in[0]);
157 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 while (sum != limit) {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700160 y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sum += XTEA_DELTA;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700162 z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
Herbert Xu06ace7a2005-10-30 21:25:15 +1100165 out[0] = cpu_to_le32(y);
166 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Herbert Xu6c2bb982006-05-16 22:09:29 +1000169static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u32 y, z, sum;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000172 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100173 const __le32 *in = (const __le32 *)src;
174 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Herbert Xu06ace7a2005-10-30 21:25:15 +1100176 y = le32_to_cpu(in[0]);
177 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 sum = XTEA_DELTA * XTEA_ROUNDS;
180
181 while (sum) {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700182 z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
183 sum -= XTEA_DELTA;
184 y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
185 }
186
Herbert Xu06ace7a2005-10-30 21:25:15 +1100187 out[0] = cpu_to_le32(y);
188 out[1] = cpu_to_le32(z);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700189}
190
191
Herbert Xu6c2bb982006-05-16 22:09:29 +1000192static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
193{
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700194 u32 y, z, sum = 0;
195 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000196 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100197 const __le32 *in = (const __le32 *)src;
198 __le32 *out = (__le32 *)dst;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700199
Herbert Xu06ace7a2005-10-30 21:25:15 +1100200 y = le32_to_cpu(in[0]);
201 z = le32_to_cpu(in[1]);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700202
203 while (sum != limit) {
204 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
205 sum += XTEA_DELTA;
206 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
207 }
208
Herbert Xu06ace7a2005-10-30 21:25:15 +1100209 out[0] = cpu_to_le32(y);
210 out[1] = cpu_to_le32(z);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700211}
212
Herbert Xu6c2bb982006-05-16 22:09:29 +1000213static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
214{
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700215 u32 y, z, sum;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000216 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100217 const __le32 *in = (const __le32 *)src;
218 __le32 *out = (__le32 *)dst;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700219
Herbert Xu06ace7a2005-10-30 21:25:15 +1100220 y = le32_to_cpu(in[0]);
221 z = le32_to_cpu(in[1]);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700222
223 sum = XTEA_DELTA * XTEA_ROUNDS;
224
225 while (sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
227 sum -= XTEA_DELTA;
228 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
229 }
230
Herbert Xu06ace7a2005-10-30 21:25:15 +1100231 out[0] = cpu_to_le32(y);
232 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static struct crypto_alg tea_alg = {
236 .cra_name = "tea",
237 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
238 .cra_blocksize = TEA_BLOCK_SIZE,
239 .cra_ctxsize = sizeof (struct tea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100240 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .cra_module = THIS_MODULE,
242 .cra_list = LIST_HEAD_INIT(tea_alg.cra_list),
243 .cra_u = { .cipher = {
244 .cia_min_keysize = TEA_KEY_SIZE,
245 .cia_max_keysize = TEA_KEY_SIZE,
246 .cia_setkey = tea_setkey,
247 .cia_encrypt = tea_encrypt,
248 .cia_decrypt = tea_decrypt } }
249};
250
251static struct crypto_alg xtea_alg = {
252 .cra_name = "xtea",
253 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
254 .cra_blocksize = XTEA_BLOCK_SIZE,
255 .cra_ctxsize = sizeof (struct xtea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100256 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .cra_module = THIS_MODULE,
258 .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
259 .cra_u = { .cipher = {
260 .cia_min_keysize = XTEA_KEY_SIZE,
261 .cia_max_keysize = XTEA_KEY_SIZE,
262 .cia_setkey = xtea_setkey,
263 .cia_encrypt = xtea_encrypt,
264 .cia_decrypt = xtea_decrypt } }
265};
266
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700267static struct crypto_alg xeta_alg = {
268 .cra_name = "xeta",
269 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
270 .cra_blocksize = XTEA_BLOCK_SIZE,
271 .cra_ctxsize = sizeof (struct xtea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100272 .cra_alignmask = 3,
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700273 .cra_module = THIS_MODULE,
274 .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
275 .cra_u = { .cipher = {
276 .cia_min_keysize = XTEA_KEY_SIZE,
277 .cia_max_keysize = XTEA_KEY_SIZE,
278 .cia_setkey = xtea_setkey,
279 .cia_encrypt = xeta_encrypt,
280 .cia_decrypt = xeta_decrypt } }
281};
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static int __init init(void)
284{
285 int ret = 0;
286
287 ret = crypto_register_alg(&tea_alg);
288 if (ret < 0)
289 goto out;
290
291 ret = crypto_register_alg(&xtea_alg);
292 if (ret < 0) {
293 crypto_unregister_alg(&tea_alg);
294 goto out;
295 }
296
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700297 ret = crypto_register_alg(&xeta_alg);
298 if (ret < 0) {
299 crypto_unregister_alg(&tea_alg);
300 crypto_unregister_alg(&xtea_alg);
301 goto out;
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304out:
305 return ret;
306}
307
308static void __exit fini(void)
309{
310 crypto_unregister_alg(&tea_alg);
311 crypto_unregister_alg(&xtea_alg);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700312 crypto_unregister_alg(&xeta_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315MODULE_ALIAS("xtea");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700316MODULE_ALIAS("xeta");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318module_init(init);
319module_exit(fini);
320
321MODULE_LICENSE("GPL");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700322MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");