4 * TEA and Xtended TEA Algorithms
6 * The TEA and Xtended TEA algorithms were developed by David Wheeler
7 * and Roger Needham at the Computer Laboratory of Cambridge University.
9 * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
18 #include <linux/init.h>
19 #include <linux/module.h>
21 #include <asm/scatterlist.h>
22 #include <linux/crypto.h>
24 #define TEA_KEY_SIZE 16
25 #define TEA_BLOCK_SIZE 8
27 #define TEA_DELTA 0x9e3779b9
29 #define XTEA_KEY_SIZE 16
30 #define XTEA_BLOCK_SIZE 8
31 #define XTEA_ROUNDS 32
32 #define XTEA_DELTA 0x9e3779b9
34 #define u32_in(x) le32_to_cpu(*(const __le32 *)(x))
35 #define u32_out(to, from) (*(__le32 *)(to) = cpu_to_le32(from))
45 static int tea_setkey(void *ctx_arg
, const u8
*in_key
,
46 unsigned int key_len
, u32
*flags
)
49 struct tea_ctx
*ctx
= ctx_arg
;
53 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
57 ctx
->KEY
[0] = u32_in (in_key
);
58 ctx
->KEY
[1] = u32_in (in_key
+ 4);
59 ctx
->KEY
[2] = u32_in (in_key
+ 8);
60 ctx
->KEY
[3] = u32_in (in_key
+ 12);
66 static void tea_encrypt(void *ctx_arg
, u8
*dst
, const u8
*src
)
71 struct tea_ctx
*ctx
= ctx_arg
;
85 y
+= ((z
<< 4) + k0
) ^ (z
+ sum
) ^ ((z
>> 5) + k1
);
86 z
+= ((y
<< 4) + k2
) ^ (y
+ sum
) ^ ((y
>> 5) + k3
);
93 static void tea_decrypt(void *ctx_arg
, u8
*dst
, const u8
*src
)
98 struct tea_ctx
*ctx
= ctx_arg
;
101 z
= u32_in (src
+ 4);
108 sum
= TEA_DELTA
<< 5;
113 z
-= ((y
<< 4) + k2
) ^ (y
+ sum
) ^ ((y
>> 5) + k3
);
114 y
-= ((z
<< 4) + k0
) ^ (z
+ sum
) ^ ((z
>> 5) + k1
);
119 u32_out (dst
+ 4, z
);
123 static int xtea_setkey(void *ctx_arg
, const u8
*in_key
,
124 unsigned int key_len
, u32
*flags
)
127 struct xtea_ctx
*ctx
= ctx_arg
;
131 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
135 ctx
->KEY
[0] = u32_in (in_key
);
136 ctx
->KEY
[1] = u32_in (in_key
+ 4);
137 ctx
->KEY
[2] = u32_in (in_key
+ 8);
138 ctx
->KEY
[3] = u32_in (in_key
+ 12);
144 static void xtea_encrypt(void *ctx_arg
, u8
*dst
, const u8
*src
)
148 u32 limit
= XTEA_DELTA
* XTEA_ROUNDS
;
150 struct xtea_ctx
*ctx
= ctx_arg
;
153 z
= u32_in (src
+ 4);
155 while (sum
!= limit
) {
156 y
+= (z
<< 4 ^ z
>> 5) + (z
^ sum
) + ctx
->KEY
[sum
&3];
158 z
+= (y
<< 4 ^ y
>> 5) + (y
^ sum
) + ctx
->KEY
[sum
>>11 &3];
162 u32_out (dst
+ 4, z
);
166 static void xtea_decrypt(void *ctx_arg
, u8
*dst
, const u8
*src
)
170 struct tea_ctx
*ctx
= ctx_arg
;
173 z
= u32_in (src
+ 4);
175 sum
= XTEA_DELTA
* XTEA_ROUNDS
;
178 z
-= (y
<< 4 ^ y
>> 5) + (y
^ sum
) + ctx
->KEY
[sum
>>11 & 3];
180 y
-= (z
<< 4 ^ z
>> 5) + (z
^ sum
) + ctx
->KEY
[sum
& 3];
184 u32_out (dst
+ 4, z
);
188 static struct crypto_alg tea_alg
= {
190 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
191 .cra_blocksize
= TEA_BLOCK_SIZE
,
192 .cra_ctxsize
= sizeof (struct tea_ctx
),
193 .cra_module
= THIS_MODULE
,
194 .cra_list
= LIST_HEAD_INIT(tea_alg
.cra_list
),
195 .cra_u
= { .cipher
= {
196 .cia_min_keysize
= TEA_KEY_SIZE
,
197 .cia_max_keysize
= TEA_KEY_SIZE
,
198 .cia_setkey
= tea_setkey
,
199 .cia_encrypt
= tea_encrypt
,
200 .cia_decrypt
= tea_decrypt
} }
203 static struct crypto_alg xtea_alg
= {
205 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
206 .cra_blocksize
= XTEA_BLOCK_SIZE
,
207 .cra_ctxsize
= sizeof (struct xtea_ctx
),
208 .cra_module
= THIS_MODULE
,
209 .cra_list
= LIST_HEAD_INIT(xtea_alg
.cra_list
),
210 .cra_u
= { .cipher
= {
211 .cia_min_keysize
= XTEA_KEY_SIZE
,
212 .cia_max_keysize
= XTEA_KEY_SIZE
,
213 .cia_setkey
= xtea_setkey
,
214 .cia_encrypt
= xtea_encrypt
,
215 .cia_decrypt
= xtea_decrypt
} }
218 static int __init
init(void)
222 ret
= crypto_register_alg(&tea_alg
);
226 ret
= crypto_register_alg(&xtea_alg
);
228 crypto_unregister_alg(&tea_alg
);
236 static void __exit
fini(void)
238 crypto_unregister_alg(&tea_alg
);
239 crypto_unregister_alg(&xtea_alg
);
242 MODULE_ALIAS("xtea");
247 MODULE_LICENSE("GPL");
248 MODULE_DESCRIPTION("TEA & XTEA Cryptographic Algorithms");