blob: 02efc5d816903097d5f9ba0fcd0dce79ff4e9f50 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API.
4 *
Aaron Grothefb4f10e2005-09-01 17:42:46 -07005 * TEA, XTEA, and XETA crypto alogrithms
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * The TEA and Xtended TEA algorithms were developed by David Wheeler
8 * and Roger Needham at the Computer Laboratory of Cambridge University.
9 *
Aaron Grothefb4f10e2005-09-01 17:42:46 -070010 * Due to the order of evaluation in XTEA many people have incorrectly
11 * implemented it. XETA (XTEA in the wrong order), exists for
12 * compatibility with these implementations.
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110020#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/crypto.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110022#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define TEA_KEY_SIZE 16
25#define TEA_BLOCK_SIZE 8
26#define TEA_ROUNDS 32
27#define TEA_DELTA 0x9e3779b9
28
29#define XTEA_KEY_SIZE 16
30#define XTEA_BLOCK_SIZE 8
31#define XTEA_ROUNDS 32
32#define XTEA_DELTA 0x9e3779b9
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct tea_ctx {
35 u32 KEY[4];
36};
37
38struct xtea_ctx {
39 u32 KEY[4];
40};
41
Herbert Xu6c2bb982006-05-16 22:09:29 +100042static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100043 unsigned int key_len)
Herbert Xu6c2bb982006-05-16 22:09:29 +100044{
45 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110046 const __le32 *key = (const __le32 *)in_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Herbert Xu06ace7a2005-10-30 21:25:15 +110048 ctx->KEY[0] = le32_to_cpu(key[0]);
49 ctx->KEY[1] = le32_to_cpu(key[1]);
50 ctx->KEY[2] = le32_to_cpu(key[2]);
51 ctx->KEY[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 return 0;
54
55}
56
Herbert Xu6c2bb982006-05-16 22:09:29 +100057static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
58{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 u32 y, z, n, sum = 0;
60 u32 k0, k1, k2, k3;
Herbert Xu6c2bb982006-05-16 22:09:29 +100061 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110062 const __le32 *in = (const __le32 *)src;
63 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Herbert Xu06ace7a2005-10-30 21:25:15 +110065 y = le32_to_cpu(in[0]);
66 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 k0 = ctx->KEY[0];
69 k1 = ctx->KEY[1];
70 k2 = ctx->KEY[2];
71 k3 = ctx->KEY[3];
72
73 n = TEA_ROUNDS;
74
75 while (n-- > 0) {
76 sum += TEA_DELTA;
77 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
78 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
79 }
80
Herbert Xu06ace7a2005-10-30 21:25:15 +110081 out[0] = cpu_to_le32(y);
82 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Herbert Xu6c2bb982006-05-16 22:09:29 +100085static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
86{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 u32 y, z, n, sum;
88 u32 k0, k1, k2, k3;
Herbert Xu6c2bb982006-05-16 22:09:29 +100089 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +110090 const __le32 *in = (const __le32 *)src;
91 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Herbert Xu06ace7a2005-10-30 21:25:15 +110093 y = le32_to_cpu(in[0]);
94 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 k0 = ctx->KEY[0];
97 k1 = ctx->KEY[1];
98 k2 = ctx->KEY[2];
99 k3 = ctx->KEY[3];
100
101 sum = TEA_DELTA << 5;
102
103 n = TEA_ROUNDS;
104
105 while (n-- > 0) {
106 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
107 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
108 sum -= TEA_DELTA;
109 }
110
Herbert Xu06ace7a2005-10-30 21:25:15 +1100111 out[0] = cpu_to_le32(y);
112 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Herbert Xu6c2bb982006-05-16 22:09:29 +1000115static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000116 unsigned int key_len)
Herbert Xu6c2bb982006-05-16 22:09:29 +1000117{
118 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100119 const __le32 *key = (const __le32 *)in_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Herbert Xu06ace7a2005-10-30 21:25:15 +1100121 ctx->KEY[0] = le32_to_cpu(key[0]);
122 ctx->KEY[1] = le32_to_cpu(key[1]);
123 ctx->KEY[2] = le32_to_cpu(key[2]);
124 ctx->KEY[3] = le32_to_cpu(key[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 return 0;
127
128}
129
Herbert Xu6c2bb982006-05-16 22:09:29 +1000130static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 u32 y, z, sum = 0;
133 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000134 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100135 const __le32 *in = (const __le32 *)src;
136 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Herbert Xu06ace7a2005-10-30 21:25:15 +1100138 y = le32_to_cpu(in[0]);
139 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 while (sum != limit) {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700142 y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 sum += XTEA_DELTA;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700144 z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Herbert Xu06ace7a2005-10-30 21:25:15 +1100147 out[0] = cpu_to_le32(y);
148 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Herbert Xu6c2bb982006-05-16 22:09:29 +1000151static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 u32 y, z, sum;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000154 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100155 const __le32 *in = (const __le32 *)src;
156 __le32 *out = (__le32 *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Herbert Xu06ace7a2005-10-30 21:25:15 +1100158 y = le32_to_cpu(in[0]);
159 z = le32_to_cpu(in[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 sum = XTEA_DELTA * XTEA_ROUNDS;
162
163 while (sum) {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700164 z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
165 sum -= XTEA_DELTA;
166 y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
167 }
168
Herbert Xu06ace7a2005-10-30 21:25:15 +1100169 out[0] = cpu_to_le32(y);
170 out[1] = cpu_to_le32(z);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700171}
172
173
Herbert Xu6c2bb982006-05-16 22:09:29 +1000174static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
175{
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700176 u32 y, z, sum = 0;
177 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000178 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100179 const __le32 *in = (const __le32 *)src;
180 __le32 *out = (__le32 *)dst;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700181
Herbert Xu06ace7a2005-10-30 21:25:15 +1100182 y = le32_to_cpu(in[0]);
183 z = le32_to_cpu(in[1]);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700184
185 while (sum != limit) {
186 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
187 sum += XTEA_DELTA;
188 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
189 }
190
Herbert Xu06ace7a2005-10-30 21:25:15 +1100191 out[0] = cpu_to_le32(y);
192 out[1] = cpu_to_le32(z);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700193}
194
Herbert Xu6c2bb982006-05-16 22:09:29 +1000195static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
196{
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700197 u32 y, z, sum;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000198 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu06ace7a2005-10-30 21:25:15 +1100199 const __le32 *in = (const __le32 *)src;
200 __le32 *out = (__le32 *)dst;
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700201
Herbert Xu06ace7a2005-10-30 21:25:15 +1100202 y = le32_to_cpu(in[0]);
203 z = le32_to_cpu(in[1]);
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700204
205 sum = XTEA_DELTA * XTEA_ROUNDS;
206
207 while (sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
209 sum -= XTEA_DELTA;
210 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
211 }
212
Herbert Xu06ace7a2005-10-30 21:25:15 +1100213 out[0] = cpu_to_le32(y);
214 out[1] = cpu_to_le32(z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Jussi Kivilinna738206d32012-07-11 14:19:55 +0300217static struct crypto_alg tea_algs[3] = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .cra_name = "tea",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700219 .cra_driver_name = "tea-generic",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
221 .cra_blocksize = TEA_BLOCK_SIZE,
222 .cra_ctxsize = sizeof (struct tea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100223 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .cra_u = { .cipher = {
226 .cia_min_keysize = TEA_KEY_SIZE,
227 .cia_max_keysize = TEA_KEY_SIZE,
228 .cia_setkey = tea_setkey,
229 .cia_encrypt = tea_encrypt,
230 .cia_decrypt = tea_decrypt } }
Jussi Kivilinna738206d32012-07-11 14:19:55 +0300231}, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .cra_name = "xtea",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700233 .cra_driver_name = "xtea-generic",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
235 .cra_blocksize = XTEA_BLOCK_SIZE,
236 .cra_ctxsize = sizeof (struct xtea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100237 .cra_alignmask = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .cra_u = { .cipher = {
240 .cia_min_keysize = XTEA_KEY_SIZE,
241 .cia_max_keysize = XTEA_KEY_SIZE,
242 .cia_setkey = xtea_setkey,
243 .cia_encrypt = xtea_encrypt,
244 .cia_decrypt = xtea_decrypt } }
Jussi Kivilinna738206d32012-07-11 14:19:55 +0300245}, {
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700246 .cra_name = "xeta",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700247 .cra_driver_name = "xeta-generic",
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700248 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
249 .cra_blocksize = XTEA_BLOCK_SIZE,
250 .cra_ctxsize = sizeof (struct xtea_ctx),
Herbert Xua429d262006-01-07 16:38:15 +1100251 .cra_alignmask = 3,
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700252 .cra_module = THIS_MODULE,
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700253 .cra_u = { .cipher = {
254 .cia_min_keysize = XTEA_KEY_SIZE,
255 .cia_max_keysize = XTEA_KEY_SIZE,
256 .cia_setkey = xtea_setkey,
257 .cia_encrypt = xeta_encrypt,
258 .cia_decrypt = xeta_decrypt } }
Jussi Kivilinna738206d32012-07-11 14:19:55 +0300259} };
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700260
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800261static int __init tea_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Jussi Kivilinna738206d32012-07-11 14:19:55 +0300263 return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800266static void __exit tea_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Jussi Kivilinna738206d32012-07-11 14:19:55 +0300268 crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100271MODULE_ALIAS_CRYPTO("tea");
Kees Cook5d26a102014-11-20 17:05:53 -0800272MODULE_ALIAS_CRYPTO("xtea");
273MODULE_ALIAS_CRYPTO("xeta");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Eric Biggersc4741b22019-04-11 21:57:42 -0700275subsys_initcall(tea_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800276module_exit(tea_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278MODULE_LICENSE("GPL");
Aaron Grothefb4f10e2005-09-01 17:42:46 -0700279MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");