]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - crypto/tea.c
Merge branch 'dev-replace-fixes-4.7' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / crypto / tea.c
CommitLineData
1da177e4
LT
1/*
2 * Cryptographic API.
3 *
fb4f10ed 4 * TEA, XTEA, and XETA crypto alogrithms
1da177e4
LT
5 *
6 * The TEA and Xtended TEA algorithms were developed by David Wheeler
7 * and Roger Needham at the Computer Laboratory of Cambridge University.
8 *
fb4f10ed
AG
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.
12 *
1da177e4
LT
13 * 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>
06ace7a9 25#include <asm/byteorder.h>
1da177e4 26#include <linux/crypto.h>
06ace7a9 27#include <linux/types.h>
1da177e4
LT
28
29#define TEA_KEY_SIZE 16
30#define TEA_BLOCK_SIZE 8
31#define TEA_ROUNDS 32
32#define TEA_DELTA 0x9e3779b9
33
34#define XTEA_KEY_SIZE 16
35#define XTEA_BLOCK_SIZE 8
36#define XTEA_ROUNDS 32
37#define XTEA_DELTA 0x9e3779b9
38
1da177e4
LT
39struct tea_ctx {
40 u32 KEY[4];
41};
42
43struct xtea_ctx {
44 u32 KEY[4];
45};
46
6c2bb98b 47static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
560c06ae 48 unsigned int key_len)
6c2bb98b
HX
49{
50 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9 51 const __le32 *key = (const __le32 *)in_key;
1da177e4 52
06ace7a9
HX
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]);
1da177e4
LT
57
58 return 0;
59
60}
61
6c2bb98b
HX
62static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
63{
1da177e4
LT
64 u32 y, z, n, sum = 0;
65 u32 k0, k1, k2, k3;
6c2bb98b 66 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9
HX
67 const __le32 *in = (const __le32 *)src;
68 __le32 *out = (__le32 *)dst;
1da177e4 69
06ace7a9
HX
70 y = le32_to_cpu(in[0]);
71 z = le32_to_cpu(in[1]);
1da177e4
LT
72
73 k0 = ctx->KEY[0];
74 k1 = ctx->KEY[1];
75 k2 = ctx->KEY[2];
76 k3 = ctx->KEY[3];
77
78 n = TEA_ROUNDS;
79
80 while (n-- > 0) {
81 sum += TEA_DELTA;
82 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
83 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
84 }
85
06ace7a9
HX
86 out[0] = cpu_to_le32(y);
87 out[1] = cpu_to_le32(z);
1da177e4
LT
88}
89
6c2bb98b
HX
90static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
91{
1da177e4
LT
92 u32 y, z, n, sum;
93 u32 k0, k1, k2, k3;
6c2bb98b 94 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9
HX
95 const __le32 *in = (const __le32 *)src;
96 __le32 *out = (__le32 *)dst;
1da177e4 97
06ace7a9
HX
98 y = le32_to_cpu(in[0]);
99 z = le32_to_cpu(in[1]);
1da177e4
LT
100
101 k0 = ctx->KEY[0];
102 k1 = ctx->KEY[1];
103 k2 = ctx->KEY[2];
104 k3 = ctx->KEY[3];
105
106 sum = TEA_DELTA << 5;
107
108 n = TEA_ROUNDS;
109
110 while (n-- > 0) {
111 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
112 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
113 sum -= TEA_DELTA;
114 }
115
06ace7a9
HX
116 out[0] = cpu_to_le32(y);
117 out[1] = cpu_to_le32(z);
1da177e4
LT
118}
119
6c2bb98b 120static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
560c06ae 121 unsigned int key_len)
6c2bb98b
HX
122{
123 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9 124 const __le32 *key = (const __le32 *)in_key;
1da177e4 125
06ace7a9
HX
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]);
1da177e4
LT
130
131 return 0;
132
133}
134
6c2bb98b
HX
135static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
136{
1da177e4
LT
137 u32 y, z, sum = 0;
138 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
6c2bb98b 139 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9
HX
140 const __le32 *in = (const __le32 *)src;
141 __le32 *out = (__le32 *)dst;
1da177e4 142
06ace7a9
HX
143 y = le32_to_cpu(in[0]);
144 z = le32_to_cpu(in[1]);
1da177e4
LT
145
146 while (sum != limit) {
fb4f10ed 147 y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
1da177e4 148 sum += XTEA_DELTA;
fb4f10ed 149 z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
1da177e4
LT
150 }
151
06ace7a9
HX
152 out[0] = cpu_to_le32(y);
153 out[1] = cpu_to_le32(z);
1da177e4
LT
154}
155
6c2bb98b
HX
156static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
157{
1da177e4 158 u32 y, z, sum;
6c2bb98b 159 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9
HX
160 const __le32 *in = (const __le32 *)src;
161 __le32 *out = (__le32 *)dst;
1da177e4 162
06ace7a9
HX
163 y = le32_to_cpu(in[0]);
164 z = le32_to_cpu(in[1]);
1da177e4
LT
165
166 sum = XTEA_DELTA * XTEA_ROUNDS;
167
fb4f10ed
AG
168 while (sum) {
169 z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
170 sum -= XTEA_DELTA;
171 y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
172 }
173
06ace7a9
HX
174 out[0] = cpu_to_le32(y);
175 out[1] = cpu_to_le32(z);
fb4f10ed
AG
176}
177
178
6c2bb98b
HX
179static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
180{
fb4f10ed
AG
181 u32 y, z, sum = 0;
182 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
6c2bb98b 183 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9
HX
184 const __le32 *in = (const __le32 *)src;
185 __le32 *out = (__le32 *)dst;
fb4f10ed 186
06ace7a9
HX
187 y = le32_to_cpu(in[0]);
188 z = le32_to_cpu(in[1]);
fb4f10ed
AG
189
190 while (sum != limit) {
191 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
192 sum += XTEA_DELTA;
193 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
194 }
195
06ace7a9
HX
196 out[0] = cpu_to_le32(y);
197 out[1] = cpu_to_le32(z);
fb4f10ed
AG
198}
199
6c2bb98b
HX
200static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
201{
fb4f10ed 202 u32 y, z, sum;
6c2bb98b 203 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
06ace7a9
HX
204 const __le32 *in = (const __le32 *)src;
205 __le32 *out = (__le32 *)dst;
fb4f10ed 206
06ace7a9
HX
207 y = le32_to_cpu(in[0]);
208 z = le32_to_cpu(in[1]);
fb4f10ed
AG
209
210 sum = XTEA_DELTA * XTEA_ROUNDS;
211
1da177e4
LT
212 while (sum) {
213 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
214 sum -= XTEA_DELTA;
215 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
216 }
217
06ace7a9
HX
218 out[0] = cpu_to_le32(y);
219 out[1] = cpu_to_le32(z);
1da177e4
LT
220}
221
738206d3 222static struct crypto_alg tea_algs[3] = { {
1da177e4
LT
223 .cra_name = "tea",
224 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
225 .cra_blocksize = TEA_BLOCK_SIZE,
226 .cra_ctxsize = sizeof (struct tea_ctx),
a429d260 227 .cra_alignmask = 3,
1da177e4 228 .cra_module = THIS_MODULE,
1da177e4
LT
229 .cra_u = { .cipher = {
230 .cia_min_keysize = TEA_KEY_SIZE,
231 .cia_max_keysize = TEA_KEY_SIZE,
232 .cia_setkey = tea_setkey,
233 .cia_encrypt = tea_encrypt,
234 .cia_decrypt = tea_decrypt } }
738206d3 235}, {
1da177e4
LT
236 .cra_name = "xtea",
237 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
238 .cra_blocksize = XTEA_BLOCK_SIZE,
239 .cra_ctxsize = sizeof (struct xtea_ctx),
a429d260 240 .cra_alignmask = 3,
1da177e4 241 .cra_module = THIS_MODULE,
1da177e4
LT
242 .cra_u = { .cipher = {
243 .cia_min_keysize = XTEA_KEY_SIZE,
244 .cia_max_keysize = XTEA_KEY_SIZE,
245 .cia_setkey = xtea_setkey,
246 .cia_encrypt = xtea_encrypt,
247 .cia_decrypt = xtea_decrypt } }
738206d3 248}, {
fb4f10ed
AG
249 .cra_name = "xeta",
250 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
251 .cra_blocksize = XTEA_BLOCK_SIZE,
252 .cra_ctxsize = sizeof (struct xtea_ctx),
a429d260 253 .cra_alignmask = 3,
fb4f10ed 254 .cra_module = THIS_MODULE,
fb4f10ed
AG
255 .cra_u = { .cipher = {
256 .cia_min_keysize = XTEA_KEY_SIZE,
257 .cia_max_keysize = XTEA_KEY_SIZE,
258 .cia_setkey = xtea_setkey,
259 .cia_encrypt = xeta_encrypt,
260 .cia_decrypt = xeta_decrypt } }
738206d3 261} };
fb4f10ed 262
3af5b90b 263static int __init tea_mod_init(void)
1da177e4 264{
738206d3 265 return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));
1da177e4
LT
266}
267
3af5b90b 268static void __exit tea_mod_fini(void)
1da177e4 269{
738206d3 270 crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));
1da177e4
LT
271}
272
3e14dcf7 273MODULE_ALIAS_CRYPTO("tea");
5d26a105
KC
274MODULE_ALIAS_CRYPTO("xtea");
275MODULE_ALIAS_CRYPTO("xeta");
1da177e4 276
3af5b90b
KB
277module_init(tea_mod_init);
278module_exit(tea_mod_fini);
1da177e4
LT
279
280MODULE_LICENSE("GPL");
fb4f10ed 281MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");