Linux-2.6.12-rc2
[linux-2.6/next.git] / crypto / tea.c
blob03c23cbd3afa1f6373d964b308463701ab316458
1 /*
2 * Cryptographic API.
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>
20 #include <linux/mm.h>
21 #include <asm/scatterlist.h>
22 #include <linux/crypto.h>
24 #define TEA_KEY_SIZE 16
25 #define TEA_BLOCK_SIZE 8
26 #define TEA_ROUNDS 32
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))
37 struct tea_ctx {
38 u32 KEY[4];
41 struct xtea_ctx {
42 u32 KEY[4];
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;
51 if (key_len != 16)
53 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
54 return -EINVAL;
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);
62 return 0;
66 static void tea_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
68 u32 y, z, n, sum = 0;
69 u32 k0, k1, k2, k3;
71 struct tea_ctx *ctx = ctx_arg;
73 y = u32_in (src);
74 z = u32_in (src + 4);
76 k0 = ctx->KEY[0];
77 k1 = ctx->KEY[1];
78 k2 = ctx->KEY[2];
79 k3 = ctx->KEY[3];
81 n = TEA_ROUNDS;
83 while (n-- > 0) {
84 sum += TEA_DELTA;
85 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
86 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
89 u32_out (dst, y);
90 u32_out (dst + 4, z);
93 static void tea_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
95 u32 y, z, n, sum;
96 u32 k0, k1, k2, k3;
98 struct tea_ctx *ctx = ctx_arg;
100 y = u32_in (src);
101 z = u32_in (src + 4);
103 k0 = ctx->KEY[0];
104 k1 = ctx->KEY[1];
105 k2 = ctx->KEY[2];
106 k3 = ctx->KEY[3];
108 sum = TEA_DELTA << 5;
110 n = TEA_ROUNDS;
112 while (n-- > 0) {
113 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
114 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
115 sum -= TEA_DELTA;
118 u32_out (dst, y);
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;
129 if (key_len != 16)
131 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
132 return -EINVAL;
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);
140 return 0;
144 static void xtea_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
147 u32 y, z, sum = 0;
148 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
150 struct xtea_ctx *ctx = ctx_arg;
152 y = u32_in (src);
153 z = u32_in (src + 4);
155 while (sum != limit) {
156 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
157 sum += XTEA_DELTA;
158 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
161 u32_out (dst, y);
162 u32_out (dst + 4, z);
166 static void xtea_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
169 u32 y, z, sum;
170 struct tea_ctx *ctx = ctx_arg;
172 y = u32_in (src);
173 z = u32_in (src + 4);
175 sum = XTEA_DELTA * XTEA_ROUNDS;
177 while (sum) {
178 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
179 sum -= XTEA_DELTA;
180 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
183 u32_out (dst, y);
184 u32_out (dst + 4, z);
188 static struct crypto_alg tea_alg = {
189 .cra_name = "tea",
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 = {
204 .cra_name = "xtea",
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)
220 int ret = 0;
222 ret = crypto_register_alg(&tea_alg);
223 if (ret < 0)
224 goto out;
226 ret = crypto_register_alg(&xtea_alg);
227 if (ret < 0) {
228 crypto_unregister_alg(&tea_alg);
229 goto out;
232 out:
233 return ret;
236 static void __exit fini(void)
238 crypto_unregister_alg(&tea_alg);
239 crypto_unregister_alg(&xtea_alg);
242 MODULE_ALIAS("xtea");
244 module_init(init);
245 module_exit(fini);
247 MODULE_LICENSE("GPL");
248 MODULE_DESCRIPTION("TEA & XTEA Cryptographic Algorithms");