1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* 1 /* 3 * Cryptographic API. 2 * Cryptographic API. 4 * 3 * 5 * TEA, XTEA, and XETA crypto alogrithms 4 * TEA, XTEA, and XETA crypto alogrithms 6 * 5 * 7 * The TEA and Xtended TEA algorithms were dev 6 * The TEA and Xtended TEA algorithms were developed by David Wheeler 8 * and Roger Needham at the Computer Laborator 7 * and Roger Needham at the Computer Laboratory of Cambridge University. 9 * 8 * 10 * Due to the order of evaluation in XTEA many 9 * Due to the order of evaluation in XTEA many people have incorrectly 11 * implemented it. XETA (XTEA in the wrong or 10 * implemented it. XETA (XTEA in the wrong order), exists for 12 * compatibility with these implementations. 11 * compatibility with these implementations. 13 * 12 * 14 * Copyright (c) 2004 Aaron Grothe ajgrothe@ya 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 * 15 */ 20 */ 16 21 17 #include <crypto/algapi.h> << 18 #include <linux/init.h> 22 #include <linux/init.h> 19 #include <linux/module.h> 23 #include <linux/module.h> 20 #include <linux/mm.h> 24 #include <linux/mm.h> 21 #include <asm/byteorder.h> 25 #include <asm/byteorder.h> >> 26 #include <linux/crypto.h> 22 #include <linux/types.h> 27 #include <linux/types.h> 23 28 24 #define TEA_KEY_SIZE 16 29 #define TEA_KEY_SIZE 16 25 #define TEA_BLOCK_SIZE 8 30 #define TEA_BLOCK_SIZE 8 26 #define TEA_ROUNDS 32 31 #define TEA_ROUNDS 32 27 #define TEA_DELTA 0x9e3779b9 32 #define TEA_DELTA 0x9e3779b9 28 33 29 #define XTEA_KEY_SIZE 16 34 #define XTEA_KEY_SIZE 16 30 #define XTEA_BLOCK_SIZE 8 35 #define XTEA_BLOCK_SIZE 8 31 #define XTEA_ROUNDS 32 36 #define XTEA_ROUNDS 32 32 #define XTEA_DELTA 0x9e3779b9 37 #define XTEA_DELTA 0x9e3779b9 33 38 34 struct tea_ctx { 39 struct tea_ctx { 35 u32 KEY[4]; 40 u32 KEY[4]; 36 }; 41 }; 37 42 38 struct xtea_ctx { 43 struct xtea_ctx { 39 u32 KEY[4]; 44 u32 KEY[4]; 40 }; 45 }; 41 46 42 static int tea_setkey(struct crypto_tfm *tfm, 47 static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key, 43 unsigned int key_len) 48 unsigned int key_len) 44 { 49 { 45 struct tea_ctx *ctx = crypto_tfm_ctx(t 50 struct tea_ctx *ctx = crypto_tfm_ctx(tfm); 46 const __le32 *key = (const __le32 *)in 51 const __le32 *key = (const __le32 *)in_key; 47 52 48 ctx->KEY[0] = le32_to_cpu(key[0]); 53 ctx->KEY[0] = le32_to_cpu(key[0]); 49 ctx->KEY[1] = le32_to_cpu(key[1]); 54 ctx->KEY[1] = le32_to_cpu(key[1]); 50 ctx->KEY[2] = le32_to_cpu(key[2]); 55 ctx->KEY[2] = le32_to_cpu(key[2]); 51 ctx->KEY[3] = le32_to_cpu(key[3]); 56 ctx->KEY[3] = le32_to_cpu(key[3]); 52 57 53 return 0; 58 return 0; 54 59 55 } 60 } 56 61 57 static void tea_encrypt(struct crypto_tfm *tfm 62 static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 58 { 63 { 59 u32 y, z, n, sum = 0; 64 u32 y, z, n, sum = 0; 60 u32 k0, k1, k2, k3; 65 u32 k0, k1, k2, k3; 61 struct tea_ctx *ctx = crypto_tfm_ctx(t 66 struct tea_ctx *ctx = crypto_tfm_ctx(tfm); 62 const __le32 *in = (const __le32 *)src 67 const __le32 *in = (const __le32 *)src; 63 __le32 *out = (__le32 *)dst; 68 __le32 *out = (__le32 *)dst; 64 69 65 y = le32_to_cpu(in[0]); 70 y = le32_to_cpu(in[0]); 66 z = le32_to_cpu(in[1]); 71 z = le32_to_cpu(in[1]); 67 72 68 k0 = ctx->KEY[0]; 73 k0 = ctx->KEY[0]; 69 k1 = ctx->KEY[1]; 74 k1 = ctx->KEY[1]; 70 k2 = ctx->KEY[2]; 75 k2 = ctx->KEY[2]; 71 k3 = ctx->KEY[3]; 76 k3 = ctx->KEY[3]; 72 77 73 n = TEA_ROUNDS; 78 n = TEA_ROUNDS; 74 79 75 while (n-- > 0) { 80 while (n-- > 0) { 76 sum += TEA_DELTA; 81 sum += TEA_DELTA; 77 y += ((z << 4) + k0) ^ (z + su 82 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1); 78 z += ((y << 4) + k2) ^ (y + su 83 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3); 79 } 84 } 80 85 81 out[0] = cpu_to_le32(y); 86 out[0] = cpu_to_le32(y); 82 out[1] = cpu_to_le32(z); 87 out[1] = cpu_to_le32(z); 83 } 88 } 84 89 85 static void tea_decrypt(struct crypto_tfm *tfm 90 static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 86 { 91 { 87 u32 y, z, n, sum; 92 u32 y, z, n, sum; 88 u32 k0, k1, k2, k3; 93 u32 k0, k1, k2, k3; 89 struct tea_ctx *ctx = crypto_tfm_ctx(t 94 struct tea_ctx *ctx = crypto_tfm_ctx(tfm); 90 const __le32 *in = (const __le32 *)src 95 const __le32 *in = (const __le32 *)src; 91 __le32 *out = (__le32 *)dst; 96 __le32 *out = (__le32 *)dst; 92 97 93 y = le32_to_cpu(in[0]); 98 y = le32_to_cpu(in[0]); 94 z = le32_to_cpu(in[1]); 99 z = le32_to_cpu(in[1]); 95 100 96 k0 = ctx->KEY[0]; 101 k0 = ctx->KEY[0]; 97 k1 = ctx->KEY[1]; 102 k1 = ctx->KEY[1]; 98 k2 = ctx->KEY[2]; 103 k2 = ctx->KEY[2]; 99 k3 = ctx->KEY[3]; 104 k3 = ctx->KEY[3]; 100 105 101 sum = TEA_DELTA << 5; 106 sum = TEA_DELTA << 5; 102 107 103 n = TEA_ROUNDS; 108 n = TEA_ROUNDS; 104 109 105 while (n-- > 0) { 110 while (n-- > 0) { 106 z -= ((y << 4) + k2) ^ (y + su 111 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3); 107 y -= ((z << 4) + k0) ^ (z + su 112 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1); 108 sum -= TEA_DELTA; 113 sum -= TEA_DELTA; 109 } 114 } 110 115 111 out[0] = cpu_to_le32(y); 116 out[0] = cpu_to_le32(y); 112 out[1] = cpu_to_le32(z); 117 out[1] = cpu_to_le32(z); 113 } 118 } 114 119 115 static int xtea_setkey(struct crypto_tfm *tfm, 120 static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key, 116 unsigned int key_len) 121 unsigned int key_len) 117 { 122 { 118 struct xtea_ctx *ctx = crypto_tfm_ctx( 123 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm); 119 const __le32 *key = (const __le32 *)in 124 const __le32 *key = (const __le32 *)in_key; 120 125 121 ctx->KEY[0] = le32_to_cpu(key[0]); 126 ctx->KEY[0] = le32_to_cpu(key[0]); 122 ctx->KEY[1] = le32_to_cpu(key[1]); 127 ctx->KEY[1] = le32_to_cpu(key[1]); 123 ctx->KEY[2] = le32_to_cpu(key[2]); 128 ctx->KEY[2] = le32_to_cpu(key[2]); 124 ctx->KEY[3] = le32_to_cpu(key[3]); 129 ctx->KEY[3] = le32_to_cpu(key[3]); 125 130 126 return 0; 131 return 0; 127 132 128 } 133 } 129 134 130 static void xtea_encrypt(struct crypto_tfm *tf 135 static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 131 { 136 { 132 u32 y, z, sum = 0; 137 u32 y, z, sum = 0; 133 u32 limit = XTEA_DELTA * XTEA_ROUNDS; 138 u32 limit = XTEA_DELTA * XTEA_ROUNDS; 134 struct xtea_ctx *ctx = crypto_tfm_ctx( 139 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm); 135 const __le32 *in = (const __le32 *)src 140 const __le32 *in = (const __le32 *)src; 136 __le32 *out = (__le32 *)dst; 141 __le32 *out = (__le32 *)dst; 137 142 138 y = le32_to_cpu(in[0]); 143 y = le32_to_cpu(in[0]); 139 z = le32_to_cpu(in[1]); 144 z = le32_to_cpu(in[1]); 140 145 141 while (sum != limit) { 146 while (sum != limit) { 142 y += ((z << 4 ^ z >> 5) + z) ^ 147 y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); 143 sum += XTEA_DELTA; 148 sum += XTEA_DELTA; 144 z += ((y << 4 ^ y >> 5) + y) ^ 149 z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]); 145 } 150 } 146 151 147 out[0] = cpu_to_le32(y); 152 out[0] = cpu_to_le32(y); 148 out[1] = cpu_to_le32(z); 153 out[1] = cpu_to_le32(z); 149 } 154 } 150 155 151 static void xtea_decrypt(struct crypto_tfm *tf 156 static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 152 { 157 { 153 u32 y, z, sum; 158 u32 y, z, sum; 154 struct tea_ctx *ctx = crypto_tfm_ctx(t 159 struct tea_ctx *ctx = crypto_tfm_ctx(tfm); 155 const __le32 *in = (const __le32 *)src 160 const __le32 *in = (const __le32 *)src; 156 __le32 *out = (__le32 *)dst; 161 __le32 *out = (__le32 *)dst; 157 162 158 y = le32_to_cpu(in[0]); 163 y = le32_to_cpu(in[0]); 159 z = le32_to_cpu(in[1]); 164 z = le32_to_cpu(in[1]); 160 165 161 sum = XTEA_DELTA * XTEA_ROUNDS; 166 sum = XTEA_DELTA * XTEA_ROUNDS; 162 167 163 while (sum) { 168 while (sum) { 164 z -= ((y << 4 ^ y >> 5) + y) ^ 169 z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]); 165 sum -= XTEA_DELTA; 170 sum -= XTEA_DELTA; 166 y -= ((z << 4 ^ z >> 5) + z) ^ 171 y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]); 167 } 172 } 168 173 169 out[0] = cpu_to_le32(y); 174 out[0] = cpu_to_le32(y); 170 out[1] = cpu_to_le32(z); 175 out[1] = cpu_to_le32(z); 171 } 176 } 172 177 173 178 174 static void xeta_encrypt(struct crypto_tfm *tf 179 static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 175 { 180 { 176 u32 y, z, sum = 0; 181 u32 y, z, sum = 0; 177 u32 limit = XTEA_DELTA * XTEA_ROUNDS; 182 u32 limit = XTEA_DELTA * XTEA_ROUNDS; 178 struct xtea_ctx *ctx = crypto_tfm_ctx( 183 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm); 179 const __le32 *in = (const __le32 *)src 184 const __le32 *in = (const __le32 *)src; 180 __le32 *out = (__le32 *)dst; 185 __le32 *out = (__le32 *)dst; 181 186 182 y = le32_to_cpu(in[0]); 187 y = le32_to_cpu(in[0]); 183 z = le32_to_cpu(in[1]); 188 z = le32_to_cpu(in[1]); 184 189 185 while (sum != limit) { 190 while (sum != limit) { 186 y += (z << 4 ^ z >> 5) + (z ^ 191 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3]; 187 sum += XTEA_DELTA; 192 sum += XTEA_DELTA; 188 z += (y << 4 ^ y >> 5) + (y ^ 193 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3]; 189 } 194 } 190 195 191 out[0] = cpu_to_le32(y); 196 out[0] = cpu_to_le32(y); 192 out[1] = cpu_to_le32(z); 197 out[1] = cpu_to_le32(z); 193 } 198 } 194 199 195 static void xeta_decrypt(struct crypto_tfm *tf 200 static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 196 { 201 { 197 u32 y, z, sum; 202 u32 y, z, sum; 198 struct tea_ctx *ctx = crypto_tfm_ctx(t 203 struct tea_ctx *ctx = crypto_tfm_ctx(tfm); 199 const __le32 *in = (const __le32 *)src 204 const __le32 *in = (const __le32 *)src; 200 __le32 *out = (__le32 *)dst; 205 __le32 *out = (__le32 *)dst; 201 206 202 y = le32_to_cpu(in[0]); 207 y = le32_to_cpu(in[0]); 203 z = le32_to_cpu(in[1]); 208 z = le32_to_cpu(in[1]); 204 209 205 sum = XTEA_DELTA * XTEA_ROUNDS; 210 sum = XTEA_DELTA * XTEA_ROUNDS; 206 211 207 while (sum) { 212 while (sum) { 208 z -= (y << 4 ^ y >> 5) + (y ^ 213 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3]; 209 sum -= XTEA_DELTA; 214 sum -= XTEA_DELTA; 210 y -= (z << 4 ^ z >> 5) + (z ^ 215 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3]; 211 } 216 } 212 217 213 out[0] = cpu_to_le32(y); 218 out[0] = cpu_to_le32(y); 214 out[1] = cpu_to_le32(z); 219 out[1] = cpu_to_le32(z); 215 } 220 } 216 221 217 static struct crypto_alg tea_algs[3] = { { 222 static struct crypto_alg tea_algs[3] = { { 218 .cra_name = "tea", 223 .cra_name = "tea", 219 .cra_driver_name = "tea-g << 220 .cra_flags = CRYPTO 224 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 221 .cra_blocksize = TEA_BL 225 .cra_blocksize = TEA_BLOCK_SIZE, 222 .cra_ctxsize = sizeof 226 .cra_ctxsize = sizeof (struct tea_ctx), 223 .cra_alignmask = 3, 227 .cra_alignmask = 3, 224 .cra_module = THIS_M 228 .cra_module = THIS_MODULE, 225 .cra_u = { .cip 229 .cra_u = { .cipher = { 226 .cia_min_keysize = TEA_KE 230 .cia_min_keysize = TEA_KEY_SIZE, 227 .cia_max_keysize = TEA_KE 231 .cia_max_keysize = TEA_KEY_SIZE, 228 .cia_setkey = tea_se 232 .cia_setkey = tea_setkey, 229 .cia_encrypt = tea_en 233 .cia_encrypt = tea_encrypt, 230 .cia_decrypt = tea_de 234 .cia_decrypt = tea_decrypt } } 231 }, { 235 }, { 232 .cra_name = "xtea" 236 .cra_name = "xtea", 233 .cra_driver_name = "xtea- << 234 .cra_flags = CRYPTO 237 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 235 .cra_blocksize = XTEA_B 238 .cra_blocksize = XTEA_BLOCK_SIZE, 236 .cra_ctxsize = sizeof 239 .cra_ctxsize = sizeof (struct xtea_ctx), 237 .cra_alignmask = 3, 240 .cra_alignmask = 3, 238 .cra_module = THIS_M 241 .cra_module = THIS_MODULE, 239 .cra_u = { .cip 242 .cra_u = { .cipher = { 240 .cia_min_keysize = XTEA_K 243 .cia_min_keysize = XTEA_KEY_SIZE, 241 .cia_max_keysize = XTEA_K 244 .cia_max_keysize = XTEA_KEY_SIZE, 242 .cia_setkey = xtea_s 245 .cia_setkey = xtea_setkey, 243 .cia_encrypt = xtea_e 246 .cia_encrypt = xtea_encrypt, 244 .cia_decrypt = xtea_d 247 .cia_decrypt = xtea_decrypt } } 245 }, { 248 }, { 246 .cra_name = "xeta" 249 .cra_name = "xeta", 247 .cra_driver_name = "xeta- << 248 .cra_flags = CRYPTO 250 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 249 .cra_blocksize = XTEA_B 251 .cra_blocksize = XTEA_BLOCK_SIZE, 250 .cra_ctxsize = sizeof 252 .cra_ctxsize = sizeof (struct xtea_ctx), 251 .cra_alignmask = 3, 253 .cra_alignmask = 3, 252 .cra_module = THIS_M 254 .cra_module = THIS_MODULE, 253 .cra_u = { .cip 255 .cra_u = { .cipher = { 254 .cia_min_keysize = XTEA_K 256 .cia_min_keysize = XTEA_KEY_SIZE, 255 .cia_max_keysize = XTEA_K 257 .cia_max_keysize = XTEA_KEY_SIZE, 256 .cia_setkey = xtea_s 258 .cia_setkey = xtea_setkey, 257 .cia_encrypt = xeta_e 259 .cia_encrypt = xeta_encrypt, 258 .cia_decrypt = xeta_d 260 .cia_decrypt = xeta_decrypt } } 259 } }; 261 } }; 260 262 261 static int __init tea_mod_init(void) 263 static int __init tea_mod_init(void) 262 { 264 { 263 return crypto_register_algs(tea_algs, 265 return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs)); 264 } 266 } 265 267 266 static void __exit tea_mod_fini(void) 268 static void __exit tea_mod_fini(void) 267 { 269 { 268 crypto_unregister_algs(tea_algs, ARRAY 270 crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs)); 269 } 271 } 270 272 271 MODULE_ALIAS_CRYPTO("tea"); 273 MODULE_ALIAS_CRYPTO("tea"); 272 MODULE_ALIAS_CRYPTO("xtea"); 274 MODULE_ALIAS_CRYPTO("xtea"); 273 MODULE_ALIAS_CRYPTO("xeta"); 275 MODULE_ALIAS_CRYPTO("xeta"); 274 276 275 subsys_initcall(tea_mod_init); !! 277 module_init(tea_mod_init); 276 module_exit(tea_mod_fini); 278 module_exit(tea_mod_fini); 277 279 278 MODULE_LICENSE("GPL"); 280 MODULE_LICENSE("GPL"); 279 MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptogra 281 MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms"); 280 282
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.