]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - crypto/tea.c
4 * TEA, XTEA, and XETA crypto alogrithms
6 * The TEA and Xtended TEA algorithms were developed by David Wheeler
7 * and Roger Needham at the Computer Laboratory of Cambridge University.
9 * 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.
13 * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
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.
22 #include <linux/init.h>
23 #include <linux/module.h>
25 #include <asm/byteorder.h>
26 #include <linux/crypto.h>
27 #include <linux/types.h>
29 #define TEA_KEY_SIZE 16
30 #define TEA_BLOCK_SIZE 8
32 #define TEA_DELTA 0x9e3779b9
34 #define XTEA_KEY_SIZE 16
35 #define XTEA_BLOCK_SIZE 8
36 #define XTEA_ROUNDS 32
37 #define XTEA_DELTA 0x9e3779b9
47 static int tea_setkey(struct crypto_tfm
*tfm
, const u8
*in_key
,
50 struct tea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
51 const __le32
*key
= (const __le32
*)in_key
;
53 ctx
->KEY
[0] = le32_to_cpu(key
[0]);
54 ctx
->KEY
[1] = le32_to_cpu(key
[1]);
55 ctx
->KEY
[2] = le32_to_cpu(key
[2]);
56 ctx
->KEY
[3] = le32_to_cpu(key
[3]);
62 static void tea_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
66 struct tea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
67 const __le32
*in
= (const __le32
*)src
;
68 __le32
*out
= (__le32
*)dst
;
70 y
= le32_to_cpu(in
[0]);
71 z
= le32_to_cpu(in
[1]);
82 y
+= ((z
<< 4) + k0
) ^ (z
+ sum
) ^ ((z
>> 5) + k1
);
83 z
+= ((y
<< 4) + k2
) ^ (y
+ sum
) ^ ((y
>> 5) + k3
);
86 out
[0] = cpu_to_le32(y
);
87 out
[1] = cpu_to_le32(z
);
90 static void tea_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
94 struct tea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
95 const __le32
*in
= (const __le32
*)src
;
96 __le32
*out
= (__le32
*)dst
;
98 y
= le32_to_cpu(in
[0]);
99 z
= le32_to_cpu(in
[1]);
106 sum
= TEA_DELTA
<< 5;
111 z
-= ((y
<< 4) + k2
) ^ (y
+ sum
) ^ ((y
>> 5) + k3
);
112 y
-= ((z
<< 4) + k0
) ^ (z
+ sum
) ^ ((z
>> 5) + k1
);
116 out
[0] = cpu_to_le32(y
);
117 out
[1] = cpu_to_le32(z
);
120 static int xtea_setkey(struct crypto_tfm
*tfm
, const u8
*in_key
,
121 unsigned int key_len
)
123 struct xtea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
124 const __le32
*key
= (const __le32
*)in_key
;
126 ctx
->KEY
[0] = le32_to_cpu(key
[0]);
127 ctx
->KEY
[1] = le32_to_cpu(key
[1]);
128 ctx
->KEY
[2] = le32_to_cpu(key
[2]);
129 ctx
->KEY
[3] = le32_to_cpu(key
[3]);
135 static void xtea_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
138 u32 limit
= XTEA_DELTA
* XTEA_ROUNDS
;
139 struct xtea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
140 const __le32
*in
= (const __le32
*)src
;
141 __le32
*out
= (__le32
*)dst
;
143 y
= le32_to_cpu(in
[0]);
144 z
= le32_to_cpu(in
[1]);
146 while (sum
!= limit
) {
147 y
+= ((z
<< 4 ^ z
>> 5) + z
) ^ (sum
+ ctx
->KEY
[sum
&3]);
149 z
+= ((y
<< 4 ^ y
>> 5) + y
) ^ (sum
+ ctx
->KEY
[sum
>>11 &3]);
152 out
[0] = cpu_to_le32(y
);
153 out
[1] = cpu_to_le32(z
);
156 static void xtea_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
159 struct tea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
160 const __le32
*in
= (const __le32
*)src
;
161 __le32
*out
= (__le32
*)dst
;
163 y
= le32_to_cpu(in
[0]);
164 z
= le32_to_cpu(in
[1]);
166 sum
= XTEA_DELTA
* XTEA_ROUNDS
;
169 z
-= ((y
<< 4 ^ y
>> 5) + y
) ^ (sum
+ ctx
->KEY
[sum
>>11 & 3]);
171 y
-= ((z
<< 4 ^ z
>> 5) + z
) ^ (sum
+ ctx
->KEY
[sum
& 3]);
174 out
[0] = cpu_to_le32(y
);
175 out
[1] = cpu_to_le32(z
);
179 static void xeta_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
182 u32 limit
= XTEA_DELTA
* XTEA_ROUNDS
;
183 struct xtea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
184 const __le32
*in
= (const __le32
*)src
;
185 __le32
*out
= (__le32
*)dst
;
187 y
= le32_to_cpu(in
[0]);
188 z
= le32_to_cpu(in
[1]);
190 while (sum
!= limit
) {
191 y
+= (z
<< 4 ^ z
>> 5) + (z
^ sum
) + ctx
->KEY
[sum
&3];
193 z
+= (y
<< 4 ^ y
>> 5) + (y
^ sum
) + ctx
->KEY
[sum
>>11 &3];
196 out
[0] = cpu_to_le32(y
);
197 out
[1] = cpu_to_le32(z
);
200 static void xeta_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
203 struct tea_ctx
*ctx
= crypto_tfm_ctx(tfm
);
204 const __le32
*in
= (const __le32
*)src
;
205 __le32
*out
= (__le32
*)dst
;
207 y
= le32_to_cpu(in
[0]);
208 z
= le32_to_cpu(in
[1]);
210 sum
= XTEA_DELTA
* XTEA_ROUNDS
;
213 z
-= (y
<< 4 ^ y
>> 5) + (y
^ sum
) + ctx
->KEY
[sum
>>11 & 3];
215 y
-= (z
<< 4 ^ z
>> 5) + (z
^ sum
) + ctx
->KEY
[sum
& 3];
218 out
[0] = cpu_to_le32(y
);
219 out
[1] = cpu_to_le32(z
);
222 static struct crypto_alg tea_alg
= {
224 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
225 .cra_blocksize
= TEA_BLOCK_SIZE
,
226 .cra_ctxsize
= sizeof (struct tea_ctx
),
228 .cra_module
= THIS_MODULE
,
229 .cra_list
= LIST_HEAD_INIT(tea_alg
.cra_list
),
230 .cra_u
= { .cipher
= {
231 .cia_min_keysize
= TEA_KEY_SIZE
,
232 .cia_max_keysize
= TEA_KEY_SIZE
,
233 .cia_setkey
= tea_setkey
,
234 .cia_encrypt
= tea_encrypt
,
235 .cia_decrypt
= tea_decrypt
} }
238 static struct crypto_alg xtea_alg
= {
240 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
241 .cra_blocksize
= XTEA_BLOCK_SIZE
,
242 .cra_ctxsize
= sizeof (struct xtea_ctx
),
244 .cra_module
= THIS_MODULE
,
245 .cra_list
= LIST_HEAD_INIT(xtea_alg
.cra_list
),
246 .cra_u
= { .cipher
= {
247 .cia_min_keysize
= XTEA_KEY_SIZE
,
248 .cia_max_keysize
= XTEA_KEY_SIZE
,
249 .cia_setkey
= xtea_setkey
,
250 .cia_encrypt
= xtea_encrypt
,
251 .cia_decrypt
= xtea_decrypt
} }
254 static struct crypto_alg xeta_alg
= {
256 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
257 .cra_blocksize
= XTEA_BLOCK_SIZE
,
258 .cra_ctxsize
= sizeof (struct xtea_ctx
),
260 .cra_module
= THIS_MODULE
,
261 .cra_list
= LIST_HEAD_INIT(xtea_alg
.cra_list
),
262 .cra_u
= { .cipher
= {
263 .cia_min_keysize
= XTEA_KEY_SIZE
,
264 .cia_max_keysize
= XTEA_KEY_SIZE
,
265 .cia_setkey
= xtea_setkey
,
266 .cia_encrypt
= xeta_encrypt
,
267 .cia_decrypt
= xeta_decrypt
} }
270 static int __init
tea_mod_init(void)
274 ret
= crypto_register_alg(&tea_alg
);
278 ret
= crypto_register_alg(&xtea_alg
);
280 crypto_unregister_alg(&tea_alg
);
284 ret
= crypto_register_alg(&xeta_alg
);
286 crypto_unregister_alg(&tea_alg
);
287 crypto_unregister_alg(&xtea_alg
);
295 static void __exit
tea_mod_fini(void)
297 crypto_unregister_alg(&tea_alg
);
298 crypto_unregister_alg(&xtea_alg
);
299 crypto_unregister_alg(&xeta_alg
);
302 MODULE_ALIAS("xtea");
303 MODULE_ALIAS("xeta");
305 module_init(tea_mod_init
);
306 module_exit(tea_mod_fini
);
308 MODULE_LICENSE("GPL");
309 MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");