Blame view

crypto/tea.c 6.59 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /* 
   * Cryptographic API.
   *
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
4
   * TEA, XTEA, and XETA crypto alogrithms
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
   *
   * The TEA and Xtended TEA algorithms were developed by David Wheeler 
   * and Roger Needham at the Computer Laboratory of Cambridge University.
   *
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
9
10
11
12
   * Due to the order of evaluation in XTEA many people have incorrectly
   * implemented it.  XETA (XTEA in the wrong order), exists for
   * compatibility with these implementations.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
18
19
20
21
22
23
24
   * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   */
  
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/mm.h>
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
25
  #include <asm/byteorder.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  #include <linux/crypto.h>
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
27
  #include <linux/types.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
35
36
37
  
  #define TEA_KEY_SIZE		16
  #define TEA_BLOCK_SIZE		8
  #define TEA_ROUNDS		32
  #define TEA_DELTA		0x9e3779b9
  
  #define XTEA_KEY_SIZE		16
  #define XTEA_BLOCK_SIZE		8
  #define XTEA_ROUNDS		32
  #define XTEA_DELTA		0x9e3779b9
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
41
42
43
44
  struct tea_ctx {
  	u32 KEY[4];
  };
  
  struct xtea_ctx {
  	u32 KEY[4];
  };
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
45
  static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
560c06ae1   Herbert Xu   [CRYPTO] api: Get...
46
  		      unsigned int key_len)
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
47
48
  {
  	struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
49
  	const __le32 *key = (const __le32 *)in_key;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
51
52
53
54
  	ctx->KEY[0] = le32_to_cpu(key[0]);
  	ctx->KEY[1] = le32_to_cpu(key[1]);
  	ctx->KEY[2] = le32_to_cpu(key[2]);
  	ctx->KEY[3] = le32_to_cpu(key[3]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
  
  	return 0; 
  
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
59
60
  static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
  	u32 y, z, n, sum = 0;
  	u32 k0, k1, k2, k3;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
63
  	struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
64
65
  	const __le32 *in = (const __le32 *)src;
  	__le32 *out = (__le32 *)dst;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
67
68
  	y = le32_to_cpu(in[0]);
  	z = le32_to_cpu(in[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  
  	k0 = ctx->KEY[0];
  	k1 = ctx->KEY[1];
  	k2 = ctx->KEY[2];
  	k3 = ctx->KEY[3];
  
  	n = TEA_ROUNDS;
  
  	while (n-- > 0) {
  		sum += TEA_DELTA;
  		y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  		z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  	}
  	
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
83
84
  	out[0] = cpu_to_le32(y);
  	out[1] = cpu_to_le32(z);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
86
87
  static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
  	u32 y, z, n, sum;
  	u32 k0, k1, k2, k3;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
90
  	struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
91
92
  	const __le32 *in = (const __le32 *)src;
  	__le32 *out = (__le32 *)dst;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
94
95
  	y = le32_to_cpu(in[0]);
  	z = le32_to_cpu(in[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  
  	k0 = ctx->KEY[0];
  	k1 = ctx->KEY[1];
  	k2 = ctx->KEY[2];
  	k3 = ctx->KEY[3];
  
  	sum = TEA_DELTA << 5;
  
  	n = TEA_ROUNDS;
  
  	while (n-- > 0) {
  		z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  		y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  		sum -= TEA_DELTA;
  	}
  	
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
112
113
  	out[0] = cpu_to_le32(y);
  	out[1] = cpu_to_le32(z);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
115
  static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
560c06ae1   Herbert Xu   [CRYPTO] api: Get...
116
  		       unsigned int key_len)
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
117
118
  {
  	struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
119
  	const __le32 *key = (const __le32 *)in_key;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
121
122
123
124
  	ctx->KEY[0] = le32_to_cpu(key[0]);
  	ctx->KEY[1] = le32_to_cpu(key[1]);
  	ctx->KEY[2] = le32_to_cpu(key[2]);
  	ctx->KEY[3] = le32_to_cpu(key[3]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
  
  	return 0; 
  
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
129
130
  static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
  	u32 y, z, sum = 0;
  	u32 limit = XTEA_DELTA * XTEA_ROUNDS;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
133
  	struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
134
135
  	const __le32 *in = (const __le32 *)src;
  	__le32 *out = (__le32 *)dst;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
137
138
  	y = le32_to_cpu(in[0]);
  	z = le32_to_cpu(in[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
  
  	while (sum != limit) {
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
141
  		y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  		sum += XTEA_DELTA;
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
143
  		z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]); 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
  	}
  	
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
146
147
  	out[0] = cpu_to_le32(y);
  	out[1] = cpu_to_le32(z);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
149
150
  static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  	u32 y, z, sum;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
152
  	struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
153
154
  	const __le32 *in = (const __le32 *)src;
  	__le32 *out = (__le32 *)dst;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
156
157
  	y = le32_to_cpu(in[0]);
  	z = le32_to_cpu(in[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
  
  	sum = XTEA_DELTA * XTEA_ROUNDS;
  
  	while (sum) {
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
162
163
164
165
166
  		z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
  		sum -= XTEA_DELTA;
  		y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
  	}
  	
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
167
168
  	out[0] = cpu_to_le32(y);
  	out[1] = cpu_to_le32(z);
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
169
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
170
171
  static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  {
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
172
173
  	u32 y, z, sum = 0;
  	u32 limit = XTEA_DELTA * XTEA_ROUNDS;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
174
  	struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
175
176
  	const __le32 *in = (const __le32 *)src;
  	__le32 *out = (__le32 *)dst;
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
177

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
178
179
  	y = le32_to_cpu(in[0]);
  	z = le32_to_cpu(in[1]);
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
180
181
182
183
184
185
186
  
  	while (sum != limit) {
  		y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
  		sum += XTEA_DELTA;
  		z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
  	}
  	
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
187
188
  	out[0] = cpu_to_le32(y);
  	out[1] = cpu_to_le32(z);
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
189
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
190
191
  static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  {
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
192
  	u32 y, z, sum;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
193
  	struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
194
195
  	const __le32 *in = (const __le32 *)src;
  	__le32 *out = (__le32 *)dst;
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
196

06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
197
198
  	y = le32_to_cpu(in[0]);
  	z = le32_to_cpu(in[1]);
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
199
200
201
202
  
  	sum = XTEA_DELTA * XTEA_ROUNDS;
  
  	while (sum) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
204
205
206
207
  		z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
  		sum -= XTEA_DELTA;
  		y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
  	}
  	
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
208
209
  	out[0] = cpu_to_le32(y);
  	out[1] = cpu_to_le32(z);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
  }
738206d32   Jussi Kivilinna   crypto: tea - use...
211
  static struct crypto_alg tea_algs[3] = { {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
214
215
  	.cra_name		=	"tea",
  	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
  	.cra_blocksize		=	TEA_BLOCK_SIZE,
  	.cra_ctxsize		=	sizeof (struct tea_ctx),
a429d2609   Herbert Xu   [CRYPTO] cipher: ...
216
  	.cra_alignmask		=	3,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
  	.cra_module		=	THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
221
222
223
  	.cra_u			=	{ .cipher = {
  	.cia_min_keysize	=	TEA_KEY_SIZE,
  	.cia_max_keysize	=	TEA_KEY_SIZE,
  	.cia_setkey		= 	tea_setkey,
  	.cia_encrypt		=	tea_encrypt,
  	.cia_decrypt		=	tea_decrypt } }
738206d32   Jussi Kivilinna   crypto: tea - use...
224
  }, {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
228
  	.cra_name		=	"xtea",
  	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
  	.cra_blocksize		=	XTEA_BLOCK_SIZE,
  	.cra_ctxsize		=	sizeof (struct xtea_ctx),
a429d2609   Herbert Xu   [CRYPTO] cipher: ...
229
  	.cra_alignmask		=	3,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
  	.cra_module		=	THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
233
234
235
236
  	.cra_u			=	{ .cipher = {
  	.cia_min_keysize	=	XTEA_KEY_SIZE,
  	.cia_max_keysize	=	XTEA_KEY_SIZE,
  	.cia_setkey		= 	xtea_setkey,
  	.cia_encrypt		=	xtea_encrypt,
  	.cia_decrypt		=	xtea_decrypt } }
738206d32   Jussi Kivilinna   crypto: tea - use...
237
  }, {
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
238
239
240
241
  	.cra_name		=	"xeta",
  	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
  	.cra_blocksize		=	XTEA_BLOCK_SIZE,
  	.cra_ctxsize		=	sizeof (struct xtea_ctx),
a429d2609   Herbert Xu   [CRYPTO] cipher: ...
242
  	.cra_alignmask		=	3,
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
243
  	.cra_module		=	THIS_MODULE,
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
244
245
246
247
248
249
  	.cra_u			=	{ .cipher = {
  	.cia_min_keysize	=	XTEA_KEY_SIZE,
  	.cia_max_keysize	=	XTEA_KEY_SIZE,
  	.cia_setkey		= 	xtea_setkey,
  	.cia_encrypt		=	xeta_encrypt,
  	.cia_decrypt		=	xeta_decrypt } }
738206d32   Jussi Kivilinna   crypto: tea - use...
250
  } };
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
251

3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
252
  static int __init tea_mod_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
  {
738206d32   Jussi Kivilinna   crypto: tea - use...
254
  	return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
256
  static void __exit tea_mod_fini(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
  {
738206d32   Jussi Kivilinna   crypto: tea - use...
258
  	crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
  }
3e14dcf7c   Mathias Krause   crypto: add missi...
260
  MODULE_ALIAS_CRYPTO("tea");
5d26a105b   Kees Cook   crypto: prefix mo...
261
262
  MODULE_ALIAS_CRYPTO("xtea");
  MODULE_ALIAS_CRYPTO("xeta");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263

3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
264
265
  module_init(tea_mod_init);
  module_exit(tea_mod_fini);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
  
  MODULE_LICENSE("GPL");
fb4f10ed5   Aaron Grothe   [CRYPTO]: Fix XTE...
268
  MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");