linux/crypto/tea.c
<<
lue=.2lue=.2>>lulue=lue=.2 ">lue=.2lue=.2Searchlue=.2Prefs6 .2lulu ">le=.2 26
2 21/*22 22 * Cryptographic API.2 23 *2 24 * TEA, XTEA, and XETA crypto alogrithms2 25 *2 26 * The TEA and Xtended TEA algorithms were developed by David Wheeler22 27 * and Roger2Needham at the Computer2Laboratory of Cambridge University.2 28 *2 29 * Due to the order2of e> aptionin XTEA many people havenincorrectly2 >a> * implemented it. XETA (XTEA in the wrong order), exists for2 11 * compaptbility with these implementaptios.2 12 *2 13 * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com2 14 *2 15 * This program is free software; you ca2 redistribute it and/or modify2 16 * it under2the terms of the GNU General Public License as published by2 17 * the Free Software Foundaptio; either2version 2 of the License, or2 18 * (at your2o v2) any later2version.2 19 *2 2 >a> */2 2162 22#include <linux/init.h>62 23#include <linux/module.h>62 24#include <linux/mm.h>62 25#include <asm/byteorder.h>62 26#include <linux/crypto.h>62 27#include <linux/typ s.h>62 2862 29#define2TEA_KEY_SIZE 1662 30#define2TEA_BLOCK_SIZE 862 31#define2TEA_ROUNDS 2 3262 32#define2TEA_DELTA 2 0x9e3779b962 3362 34#define2XTEA_KEY_SIZE 1662 35#define2XTEA_BLOCK_SIZE 862 36#define2XTEA_ROUNDS 23262 37#define2XTEA_DELTA 2 0x9e3779b962 3862 39struct2tea_ctx {62 40 u32 KEY[4];62 41};62 4262 43struct2xtea_ctx {62 44 u32 KEY[4];62 45};62 4662 47static int2tea_setkey(struct2crypto_tfm *tfm, const2u8 *in_key,62 48 2 unsigned int2key_len)62 49{62 50 struct2tea_ctx *ctx =2crypto_tfm_ctx(tfm);62 51 const2__le32 *key =2(const2__le32 *)in_key;62 5262 53 ctx->KEY[0] =2le32_to_cpu(key[0]);62 54 ctx->KEY[1] =2le32_to_cpu(key[1]);62 55 ctx->KEY[2] =2le32_to_cpu(key[2]);62 56 ctx->KEY[3] =2le32_to_cpu(key[3]);62 5762 58 return 0; 62 5962 60}62 6162 62static void2tea_encrypt(struct2crypto_tfm *tfm, u8 *dst, const2u8 *src)62 63{62 64 u32 y, z, n, sum =20;62 65 u32 k0, k1, k2, k3;62 66 struct2tea_ctx *ctx =2crypto_tfm_ctx(tfm);62 67 const2__le32 *in =2(const2__le32 *)src;62 68 __le32 *out =2(__le32 *)dst;62 6962 70 y =2le32_to_cpu(in[0]);62 71 z =2le32_to_cpu(in[1]);62 7262 73 k0 =2ctx->KEY[0];62 74 k1 =2ctx->KEY[1];62 75 k2 =2ctx->KEY[2];62 76 k3 =2ctx->KEY[3];62 7762 78 n =2TEA_ROUNDS;62 7962 80 while2(n-- > 0) {62 81 sum +=2TEA_DELTA;62 82 y +=2((z << 4) + k0) ^2(z + sum) ^2((z >> 5) + k1);62 83 z +=2((y << 4) + k2) ^2(y + sum) ^2((y >> 5) + k3);62 84 }62 85 62 86 out[0] =2cpu_to_le32(y);62 87 out[1] =2cpu_to_le32(z);62 88}62 8962 90static void2tea_decrypt(struct2crypto_tfm *tfm, u8 *dst, const2u8 *src)62 91{62 92 u32 y, z, n, sum;62 93 u32 k0, k1, k2, k3;62 94 struct2tea_ctx *ctx =2crypto_tfm_ctx(tfm);62 95 const2__le32 *in =2(const2__le32 *)src;62 96 __le32 *out =2(__le32 *)dst;62 9762 98 y =2le32_to_cpu(in[0]);62 99 z =2le32_to_cpu(in[1]);6210062101 k0 =2ctx->KEY[0];62102 k1 =2ctx->KEY[1];62103 k2 =2ctx->KEY[2];62104 k3 =2ctx->KEY[3];6210562106 sum =2TEA_DELTA << 5;6210762108 n =2TEA_ROUNDS;6210962110 while2(n-- > 0) {62111 z -=2((y << 4) + k2) ^2(y + sum) ^2((y >> 5) + k3);62112 y -=2((z << 4) + k0) ^2(z + sum) ^2((z >> 5) + k1);62113 sum -=2TEA_DELTA;62114 }62115 62116 out[0] =2cpu_to_le32(y);62117 out[1] =2cpu_to_le32(z);62118}6211962120static int2xtea_setkey(struct2crypto_tfm *tfm, const2u8 *in_key,62121 unsigned int2key_len)62122{62123 struct2xtea_ctx *ctx =2crypto_tfm_ctx(tfm);62124 const2__le32 *key =2(const2__le32 *)in_key;6212562126 ctx->KEY[0] =2le32_to_cpu(key[0]);62127 ctx->KEY[1] =2le32_to_cpu(key[1]);62128 ctx->KEY[2] =2le32_to_cpu(key[2]);62129 ctx->KEY[3] =2le32_to_cpu(key[3]);6213062131 return 0; 6213262133}6213462135static void2xtea_encrypt(struct2crypto_tfm *tfm, u8 *dst, const2u8 *src)62136{62137 u32 y, z, sum =20;62138 u32 limit =2XTEA_DELTA *2XTEA_ROUNDS;62139 struct2xtea_ctx *ctx =2crypto_tfm_ctx(tfm);62140 const2__le32 *in =2(const2__le32 *)src;62141 __le32 *out =2(__le32 *)dst;6214262143 y =2le32_to_cpu(in[0]);62144 z =2le32_to_cpu(in[1]);6214562146 while2(sum !=2limit) {62147 y +=2((z << 4 ^ z >> 5) + z) ^2(sum + ctx->KEY[sum&3]); 62148 2 sum +=2XTEA_DELTA;62149 2 z +=2((y << 4 ^ y >> 5) + y) ^2(sum + ctx->KEY[sum>>11 &3]); 62150 }62151 62152 out[0] =2cpu_to_le32(y);62153 out[1] =2cpu_to_le32(z);62154}6215562156static void2xtea_decrypt(struct2crypto_tfm *tfm, u8 *dst, const2u8 *src)62157{62158 u32 y, z, sum;62159 struct2tea_ctx *ctx =2crypto_tfm_ctx(tfm);62160 const2__le32 *in =2(const2__le32 *)src;62161 __le32 *out =2(__le32 *)dst;6216262163 y =2le32_to_cpu(in[0]);62164 z =2le32_to_cpu(in[1]);6216562166 sum =2XTEA_DELTA *2XTEA_ROUNDS;6216762168 while2(sum) {62169 2 z -=2((y << 4 ^ y >> 5) + y) ^2(sum + ctx->KEY[sum>>11 & 3]);62170 sum -=2XTEA_DELTA;62171 y -=2((z << 4 ^ z >> 5) + z) ^2(sum + ctx->KEY[sum & 3]);62172 }62173 62174 out[0] =2cpu_to_le32(y);62175 out[1] =2cpu_to_le32(z);62176}621776217862179static void2xeta_encrypt(struct2crypto_tfm *tfm, u8 *dst, const2u8 *src)62180{62181 u32 y, z, sum =20;62182 u32 limit =2XTEA_DELTA *2XTEA_ROUNDS;62183 struct2xtea_ctx *ctx =2crypto_tfm_ctx(tfm);62184 const2__le32 *in =2(const2__le32 *)src;62185 __le32 *out =2(__le32 *)dst;6218662187 y =2le32_to_cpu(in[0]);62188 z =2le32_to_cpu(in[1]);6218962190 while2(sum !=2limit) {62191 y +=2(z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];62192 sum +=2XTEA_DELTA;62193 z +=2(y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];62194 }62195 62196 out[0] =2cpu_to_le32(y);62197 out[1] =2cpu_to_le32(z);62198}6219962200static void2xeta_decrypt(struct2crypto_tfm *tfm, u8 *dst, const2u8 *src)62201{62202 u32 y, z, sum;62203 struct2tea_ctx *ctx =2crypto_tfm_ctx(tfm);62204 const2__le32 *in =2(const2__le32 *)src;62205 __le32 *out =2(__le32 *)dst;6220662207 y =2le32_to_cpu(in[0]);62208 z =2le32_to_cpu(in[1]);6220962210 sum =2XTEA_DELTA *2XTEA_ROUNDS;6221162212 while2(sum) {62213 z -=2(y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];62214 sum -=2XTEA_DELTA;62215 y -=2(z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];62216 }62217 62218 out[0] =2cpu_to_le32(y);62219 out[1] =2cpu_to_le32(z);62220}6222162222static struct2crypto_alg tea_algs[3] =2{ {62223 .cra_nam = "tea",62224 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,62225 .cra_blocksiz = TEA_BLOCK_SIZE,62226 .cra_ctxsiz = siz of (struct2tea_ctx),62227 .cra_alignmask = 3,62228 .cra_modul = THIS_MODULE,62229 .cra_u = { .cipher =2{62230 .cia_min_keysiz = TEA_KEY_SIZE,62231 .cia_max_keysiz = TEA_KEY_SIZE,62232 .cia_setkey = tea_setkey,62233 .cia_encrypt = tea_encrypt,62234 .cia_decrypt = tea_decrypt } }62235},2{62236 .cra_nam = "xtea",62237 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,62238 .cra_blocksiz = XTEA_BLOCK_SIZE,62239 .cra_ctxsiz = siz of (struct2xtea_ctx),62240 .cra_alignmask = 3,62241 .cra_modul = THIS_MODULE,62242 .cra_u = { .cipher =2{62243 .cia_min_keysiz = XTEA_KEY_SIZE,62244 .cia_max_keysiz = XTEA_KEY_SIZE,62245 .cia_setkey = xtea_setkey,62246 .cia_encrypt = xtea_encrypt,62247 .cia_decrypt = xtea_decrypt } }62248},2{62249 .cra_nam = "xeta",62250 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,62251 .cra_blocksiz = XTEA_BLOCK_SIZE,62252 .cra_ctxsiz = siz of (struct2xtea_ctx),62253 .cra_alignmask = 3,62254 .cra_modul = THIS_MODULE,62255 .cra_u = { .cipher =2{62256 .cia_min_keysiz = XTEA_KEY_SIZE,62257 .cia_max_keysiz = XTEA_KEY_SIZE,62258 .cia_setkey = xtea_setkey,62259 .cia_encrypt = xeta_encrypt,62260 .cia_decrypt = xeta_decrypt } }62261} };6226262263static int2__init tea_mod_init(void)62264{62265 return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));62266}6226762268static void2__exit tea_mod_fini(void)62269{62270 crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));62271}6227262273MODULE_ALIAS("xtea");62274MODULE_ALIAS("xeta");6227562276modul _init(tea_mod_init);62277modul _exit(tea_mod_fini);6227862279MODULE_LICENSE("GPL");62280MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");62281
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since21995.