Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* 
0003  * Cryptographic API.
0004  *
0005  * TEA, XTEA, and XETA crypto alogrithms
0006  *
0007  * The TEA and Xtended TEA algorithms were developed by David Wheeler 
0008  * and Roger Needham at the Computer Laboratory of Cambridge University.
0009  *
0010  * Due to the order of evaluation in XTEA many people have incorrectly
0011  * implemented it.  XETA (XTEA in the wrong order), exists for
0012  * compatibility with these implementations.
0013  *
0014  * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
0015  */
0016 
0017 #include <linux/init.h>
0018 #include <linux/module.h>
0019 #include <linux/mm.h>
0020 #include <asm/byteorder.h>
0021 #include <linux/crypto.h>
0022 #include <linux/types.h>
0023 
0024 #define TEA_KEY_SIZE        16
0025 #define TEA_BLOCK_SIZE      8
0026 #define TEA_ROUNDS      32
0027 #define TEA_DELTA       0x9e3779b9
0028 
0029 #define XTEA_KEY_SIZE       16
0030 #define XTEA_BLOCK_SIZE     8
0031 #define XTEA_ROUNDS     32
0032 #define XTEA_DELTA      0x9e3779b9
0033 
0034 struct tea_ctx {
0035     u32 KEY[4];
0036 };
0037 
0038 struct xtea_ctx {
0039     u32 KEY[4];
0040 };
0041 
0042 static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
0043               unsigned int key_len)
0044 {
0045     struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
0046     const __le32 *key = (const __le32 *)in_key;
0047 
0048     ctx->KEY[0] = le32_to_cpu(key[0]);
0049     ctx->KEY[1] = le32_to_cpu(key[1]);
0050     ctx->KEY[2] = le32_to_cpu(key[2]);
0051     ctx->KEY[3] = le32_to_cpu(key[3]);
0052 
0053     return 0; 
0054 
0055 }
0056 
0057 static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0058 {
0059     u32 y, z, n, sum = 0;
0060     u32 k0, k1, k2, k3;
0061     struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
0062     const __le32 *in = (const __le32 *)src;
0063     __le32 *out = (__le32 *)dst;
0064 
0065     y = le32_to_cpu(in[0]);
0066     z = le32_to_cpu(in[1]);
0067 
0068     k0 = ctx->KEY[0];
0069     k1 = ctx->KEY[1];
0070     k2 = ctx->KEY[2];
0071     k3 = ctx->KEY[3];
0072 
0073     n = TEA_ROUNDS;
0074 
0075     while (n-- > 0) {
0076         sum += TEA_DELTA;
0077         y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
0078         z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
0079     }
0080     
0081     out[0] = cpu_to_le32(y);
0082     out[1] = cpu_to_le32(z);
0083 }
0084 
0085 static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0086 {
0087     u32 y, z, n, sum;
0088     u32 k0, k1, k2, k3;
0089     struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
0090     const __le32 *in = (const __le32 *)src;
0091     __le32 *out = (__le32 *)dst;
0092 
0093     y = le32_to_cpu(in[0]);
0094     z = le32_to_cpu(in[1]);
0095 
0096     k0 = ctx->KEY[0];
0097     k1 = ctx->KEY[1];
0098     k2 = ctx->KEY[2];
0099     k3 = ctx->KEY[3];
0100 
0101     sum = TEA_DELTA << 5;
0102 
0103     n = TEA_ROUNDS;
0104 
0105     while (n-- > 0) {
0106         z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
0107         y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
0108         sum -= TEA_DELTA;
0109     }
0110     
0111     out[0] = cpu_to_le32(y);
0112     out[1] = cpu_to_le32(z);
0113 }
0114 
0115 static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
0116                unsigned int key_len)
0117 {
0118     struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
0119     const __le32 *key = (const __le32 *)in_key;
0120 
0121     ctx->KEY[0] = le32_to_cpu(key[0]);
0122     ctx->KEY[1] = le32_to_cpu(key[1]);
0123     ctx->KEY[2] = le32_to_cpu(key[2]);
0124     ctx->KEY[3] = le32_to_cpu(key[3]);
0125 
0126     return 0; 
0127 
0128 }
0129 
0130 static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0131 {
0132     u32 y, z, sum = 0;
0133     u32 limit = XTEA_DELTA * XTEA_ROUNDS;
0134     struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
0135     const __le32 *in = (const __le32 *)src;
0136     __le32 *out = (__le32 *)dst;
0137 
0138     y = le32_to_cpu(in[0]);
0139     z = le32_to_cpu(in[1]);
0140 
0141     while (sum != limit) {
0142         y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); 
0143         sum += XTEA_DELTA;
0144         z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]); 
0145     }
0146     
0147     out[0] = cpu_to_le32(y);
0148     out[1] = cpu_to_le32(z);
0149 }
0150 
0151 static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0152 {
0153     u32 y, z, sum;
0154     struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
0155     const __le32 *in = (const __le32 *)src;
0156     __le32 *out = (__le32 *)dst;
0157 
0158     y = le32_to_cpu(in[0]);
0159     z = le32_to_cpu(in[1]);
0160 
0161     sum = XTEA_DELTA * XTEA_ROUNDS;
0162 
0163     while (sum) {
0164         z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
0165         sum -= XTEA_DELTA;
0166         y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
0167     }
0168     
0169     out[0] = cpu_to_le32(y);
0170     out[1] = cpu_to_le32(z);
0171 }
0172 
0173 
0174 static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0175 {
0176     u32 y, z, sum = 0;
0177     u32 limit = XTEA_DELTA * XTEA_ROUNDS;
0178     struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
0179     const __le32 *in = (const __le32 *)src;
0180     __le32 *out = (__le32 *)dst;
0181 
0182     y = le32_to_cpu(in[0]);
0183     z = le32_to_cpu(in[1]);
0184 
0185     while (sum != limit) {
0186         y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
0187         sum += XTEA_DELTA;
0188         z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
0189     }
0190     
0191     out[0] = cpu_to_le32(y);
0192     out[1] = cpu_to_le32(z);
0193 }
0194 
0195 static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0196 {
0197     u32 y, z, sum;
0198     struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
0199     const __le32 *in = (const __le32 *)src;
0200     __le32 *out = (__le32 *)dst;
0201 
0202     y = le32_to_cpu(in[0]);
0203     z = le32_to_cpu(in[1]);
0204 
0205     sum = XTEA_DELTA * XTEA_ROUNDS;
0206 
0207     while (sum) {
0208         z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
0209         sum -= XTEA_DELTA;
0210         y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
0211     }
0212     
0213     out[0] = cpu_to_le32(y);
0214     out[1] = cpu_to_le32(z);
0215 }
0216 
0217 static struct crypto_alg tea_algs[3] = { {
0218     .cra_name       =   "tea",
0219     .cra_driver_name    =   "tea-generic",
0220     .cra_flags      =   CRYPTO_ALG_TYPE_CIPHER,
0221     .cra_blocksize      =   TEA_BLOCK_SIZE,
0222     .cra_ctxsize        =   sizeof (struct tea_ctx),
0223     .cra_alignmask      =   3,
0224     .cra_module     =   THIS_MODULE,
0225     .cra_u          =   { .cipher = {
0226     .cia_min_keysize    =   TEA_KEY_SIZE,
0227     .cia_max_keysize    =   TEA_KEY_SIZE,
0228     .cia_setkey     =   tea_setkey,
0229     .cia_encrypt        =   tea_encrypt,
0230     .cia_decrypt        =   tea_decrypt } }
0231 }, {
0232     .cra_name       =   "xtea",
0233     .cra_driver_name    =   "xtea-generic",
0234     .cra_flags      =   CRYPTO_ALG_TYPE_CIPHER,
0235     .cra_blocksize      =   XTEA_BLOCK_SIZE,
0236     .cra_ctxsize        =   sizeof (struct xtea_ctx),
0237     .cra_alignmask      =   3,
0238     .cra_module     =   THIS_MODULE,
0239     .cra_u          =   { .cipher = {
0240     .cia_min_keysize    =   XTEA_KEY_SIZE,
0241     .cia_max_keysize    =   XTEA_KEY_SIZE,
0242     .cia_setkey     =   xtea_setkey,
0243     .cia_encrypt        =   xtea_encrypt,
0244     .cia_decrypt        =   xtea_decrypt } }
0245 }, {
0246     .cra_name       =   "xeta",
0247     .cra_driver_name    =   "xeta-generic",
0248     .cra_flags      =   CRYPTO_ALG_TYPE_CIPHER,
0249     .cra_blocksize      =   XTEA_BLOCK_SIZE,
0250     .cra_ctxsize        =   sizeof (struct xtea_ctx),
0251     .cra_alignmask      =   3,
0252     .cra_module     =   THIS_MODULE,
0253     .cra_u          =   { .cipher = {
0254     .cia_min_keysize    =   XTEA_KEY_SIZE,
0255     .cia_max_keysize    =   XTEA_KEY_SIZE,
0256     .cia_setkey     =   xtea_setkey,
0257     .cia_encrypt        =   xeta_encrypt,
0258     .cia_decrypt        =   xeta_decrypt } }
0259 } };
0260 
0261 static int __init tea_mod_init(void)
0262 {
0263     return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));
0264 }
0265 
0266 static void __exit tea_mod_fini(void)
0267 {
0268     crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));
0269 }
0270 
0271 MODULE_ALIAS_CRYPTO("tea");
0272 MODULE_ALIAS_CRYPTO("xtea");
0273 MODULE_ALIAS_CRYPTO("xeta");
0274 
0275 subsys_initcall(tea_mod_init);
0276 module_exit(tea_mod_fini);
0277 
0278 MODULE_LICENSE("GPL");
0279 MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");