linux/crypto/tea.c
<<
>>
Prefs
   1/* 
   2 * Cryptographic API.
   3 *
   4 * TEA, XTEA, and XETA crypto alogrithms
   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 *
   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 *
  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>
  25#include <asm/byteorder.h>
  26#include <linux/crypto.h>
  27#include <linux/types.h>
  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
  39struct tea_ctx {
  40        u32 KEY[4];
  41};
  42
  43struct xtea_ctx {
  44        u32 KEY[4];
  45};
  46
  47static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  48                      unsigned int key_len)
  49{
  50        struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  51        const __le32 *key = (const __le32 *)in_key;
  52
  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]);
  57
  58        return 0; 
  59
  60}
  61
  62static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  63{
  64        u32 y, z, n, sum = 0;
  65        u32 k0, k1, k2, k3;
  66        struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  67        const __le32 *in = (const __le32 *)src;
  68        __le32 *out = (__le32 *)dst;
  69
  70        y = le32_to_cpu(in[0]);
  71        z = le32_to_cpu(in[1]);
  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        
  86        out[0] = cpu_to_le32(y);
  87        out[1] = cpu_to_le32(z);
  88}
  89
  90static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  91{
  92        u32 y, z, n, sum;
  93        u32 k0, k1, k2, k3;
  94        struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  95        const __le32 *in = (const __le32 *)src;
  96        __le32 *out = (__le32 *)dst;
  97
  98        y = le32_to_cpu(in[0]);
  99        z = le32_to_cpu(in[1]);
 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        
 116        out[0] = cpu_to_le32(y);
 117        out[1] = cpu_to_le32(z);
 118}
 119
 120static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
 121                       unsigned int key_len)
 122{
 123        struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
 124        const __le32 *key = (const __le32 *)in_key;
 125
 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]);
 130
 131        return 0; 
 132
 133}
 134
 135static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 136{
 137        u32 y, z, sum = 0;
 138        u32 limit = XTEA_DELTA * XTEA_ROUNDS;
 139        struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
 140        const __le32 *in = (const __le32 *)src;
 141        __le32 *out = (__le32 *)dst;
 142
 143        y = le32_to_cpu(in[0]);
 144        z = le32_to_cpu(in[1]);
 145
 146        while (sum != limit) {
 147                y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); 
 148                sum +KEY[ 148                z += ((zsum) ^ ((y(z) ^ (sum + ctx->KEY[sum&49{
<1a href="crypto/tea.c#L501" id=1L50" class="liass="line" name="L133"> 13m);
<1a href="crypto/tea.c#L511" id=1L51" class="liss="line" name="L133"> 13m;
 116        out[0] = cpu_to_le32(y 52
<1a href="crypto/tea.c#L531" id=1L53" class="line" name="L53"> 117        out[1] = cpu_to_le32(za>[0]);
<1a href="crypto/tea.c#L541" id=1L54" cass="line" name="L133"> 13m[1]);
 1a>[2]);
<1a href="crypto/tea.c#L561" id=1L56" cass="line" name="L135"> 135sstatic void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *sra>[3]);
<1a href="crypto/tea.c#L571" id=1L57" climit" class="sref">limit< 57
<1a href="crypto/tea.c#L581" id=158" class="line" name="L138"> 138        u32 y, z, zaode=sum" a href="crypto/tea.c#L591" id=159" class="line" name="L139"> 139        struct tea_ctx *ctx = crypto_tfm_ctx(tf1 59
<1a href="crypto/tea.c#L601" id=160" class="line" name="L140"> 140        const __le32 *in = (const __le32 *)sr60}
<1a href="crypto/tea.c#L611" id=161" class="line" name="L141"> 141        __le32 *out = (__le32 *)ds 61
<1a href="crypto/tea.c#L621" id=162" class="line" name="L142"> 1rc)
<1a href="crypto/tea.c#L631" id=163" class="line" name="L143"> 143        y = le32_to_cpu(in{
<1a href="crypto/tea.c#L641" id=1L64" class="line" name="L64"> 144        z = le32_to_cpu(in = 0;
<1a href="crypto/tea.c#L651" id=165" class="line" name="L145"> 16/span>
 106        limit = XTEA_DELTA * XTEA_ROUNDm);
<1a href="crypto/tea.c#L671" id=167" class="line" name="L107"> 1rc;
<1a href="crypto/tea.c#L681" id=1L68" class="lie" name="L146"> 146        while (limit;
<1a href="crypto/tea.c#L691" id=169" class="line" name="L148"> 148                z -= ((zsum) ^ ((y(z) ^ (sum + ctx->KEY[key
<1a href="crypto/tea.c#L701" id=1L70" class="liass="line" name="L106"> 106        sum +KEY[ 111                y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[key
<1a href="crypto/tea.c#L721" id=172" class="linass="line" name="L133"> 13 72
<1a href="crypto/tea.c#L731" id=1L73" class="liss="line" name="L133"> 13 3{
<1a href="crypto/tea.c#L741" id=1L74" class="line" name="L74"> 116        out[0] = cpu_to_le32(y/a>[1];
<1a href="crypto/tea.c#L751" id=1L75" class="line" name="L75"> 117        out[1] = cpu_to_le32(z/a>[2];
<1a href="crypto/tea.c#L761" id=1L76" cass="line" name="L133"> 13 /span>
 1 77
<1a href="crypto/tea.c#L781" id=1738" class="line" name="L38"> 1DS;
<1a href="crypto/tea.c#L791" id=1796" cass="line" name="L135"> 135static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *sr 79
<1a href="crypto/tea.c#L801" id=1L80" climit" class="sref">limit 138        u32 y, z, sum;
<1a href="crypto/tea.c#L821" id=182" class="line" name="L102"> 138        u32 limit = XTEA_DELTA * XTEA_ROUND1);
<1a href="crypto/tea.c#L831" id=183" class="line" name="L123"> 123        struct xtea_ctx *ctx = crypto_tfm_ctx(tfm3);
<1a href="crypto/tea.c#L841" id=184" class="line" name="L124"> 124        const __le32 *in = (const __le32 *)sr      }
<1a href="crypto/tea.c#L851" id=1875" class="line" name="L75"> 141        __le32 *out = (__le32 *)ds       
<1a href="crypto/tea.c#L861" id=1846" class="line" name="L46"> 1y);
<1a href="crypto/tea.c#L871" id=1L87" class="line" name="L87"> 143        y = le32_to_cpu(in);
<1a href="crypto/tea.c#L881" id=188" class="line" name="L138"> 144        z = le32_to_cpu(in}
<1a href="crypto/tea.c#L891" id=1L89" class="line" name="L89"> 1 89
<1a href="crypto/tea.c#L901" id=190" class="line" name="L110"> 110        while (sum != limit)
<1a href="crypto/tea.c#L911" id=191" class="line" name="L111"> 111                y -= ((z << 4 ^ y -= ((z        while (sum + ctx->KEY[in{
<1a href="crypto/tea.c#L921" id=1L92" class="liass="line" name="L106"> 106        sum +KEY[  83                z -= ((zsum) ^ ((z -= ((z        while (sum + ctx->KEY[KEY[ 114   m);
<1a href="crypto/tea.c#L951" id=195" class="line" name="L115"> 115  rc;
<1a href="crypto/tea.c#L961" id=1L96" class="line" name="L96"> 116        out[0] = cpu_to_le32(yst;
<1a href="crypto/tea.c#L971" id=197" class="line" name="L117"> 117        out[1] = cpu_to_le32(z 97
<1a href="crypto/tea.c#L981" id=198" class="line" name="L118"> 11a>[0]);
<1a href="crypto/tea.c#L991" id=1989" class="line" name="L89"> 2>[1]);
  90oet>sstatic void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *s200
  2a>[0];
 138        u32 y, z, 2a>[1];
 123        struct tea_ctx *ctx = crypto_tfm_ctx(tf2a>[2];
 124        const __le32 *in = (const __le32 *)s2a>[3];
 141        __le32 *out = (__le32 *)d205
 2lt; 5;
 143        y = le32_to_cpu(in
 144        z = le32_to_cpu(in;
 209
 106        limit = XTEA_DELTA * XTEA_ROUN2; 0) {
 2);
 146        while (limit2);
 113                z -= ((zsum) ^ ((z -= ((z        while (sum + ctx->KEY[KEY[ 106        sum +KEY[ 143        y -= ((z << 4 ^ y -= ((z        while (sum + ctx-> 106        KEY[ 12);
 12<7
 116        out[0] = cpu_to_le32(28}
 117        out[1] = cpu_to_le32(219
  2y,
 2n)
xtea_encryptlimit22{
xtea_encrya_c#L1= "">l"in_k2);
xtea_encrya_flags  struct xtea_encCRYPTO_ALG_TYPE_CIPHER  struct in_k2<    }
xtea_encrya_blocksiz1= xtea_encde=KBLOCK_SIZE= in_k2<5
xtea_encrya_ hrsiz1= xtea_enc>        struct in_k2);
xtea_encrya_alignmask= in_k2<7
xtea_encrya_modul1= xtea_encdHIS_MODULE= in_k2<}
xtea_encrya_/a> = xtea_encriphera> = limit2>[3]);
xtea_encria_mef="+csiz1= xtea_encde=K hr_SIZE= in_k230
xtea_encria_max="+csiz1= xtea_encde=K hr_SIZE= in_k23)
xtea_encria_static int xtea_enct/a>static int in_k23{
xtea_encri>static void xtea_enct/a>tatic void in_k23/a>);
xtea_encri>sstatic void xtea_enct/a>static void  1234
limit2>5
xtea_encrya_c#L1= "x">l"in_k26{
xtea_encrya_flags  struct xtea_encCRYPTO_ALG_TYPE_CIPHER  struct in_k2> = 0;
xtea_encrya_blocksiz1= xtea_encXde=KBLOCK_SIZE= in_k2S;
xtea_encrya_ hrsiz1= xtea_enc>        struct in_k2);
xtea_encrya_alignmask= in_k2c;
xtea_encrya_modul1= xtea_encdHIS_MODULE= in_k2t;
xtea_encrya_/a> = xtea_encriphera> = limit242
xtea_encri>smef="+csiz1= xtea_encXde=K hr_SIZE= in_k2>[0]);
xtea_encri>smax="+csiz1= xtea_encXde=K hr_SIZE= in_k2>4
xtea_encria_static int xtea_enc0static int in_k2>5
xtea_encri>static void xtea_encstatic void in_k2>{
xtea_encri>sstatic void xtea_encsstatic void  12;3]); 
limit2code=sum"2class="sref    +xTEA_KEe2" id=249" class="lin."sref">xtea_encrya_c#L1= "xetl"in_k249{
<2a href="crypto/tea.c#L502" id=250" class="lin."sref">xtea_encrya_flags  struct xtea_encCRYPTO_ALG_TYPE_CIPHER  struct in_k2m);
<2a href="crypto/tea.c#L512" id=251" class="lin."sref">xtea_encrya_blocksiz1= xtea_encXde=KBLOCK_SIZE= in_k2m;
xtea_encrya_ hrsiz1= xtea_enc>        struct in_k2 52
<2a href="crypto/tea.c#L532" id=253" class="lin."sref">xtea_encrya_alignmask= in_k2a>[0]);
<2a href="crypto/tea.c#L542" id=254" class="lin."sref">xtea_encrya_modul1= xtea_encdHIS_MODULE= in_k2m[1]);
xtea_encrya_/a> = xtea_encriphera> = limit2a>[2]);
<2a href="crypto/tea.c#L562" id=256" class="lin."sref">xtea_encri>smef="+csiz1= xtea_encXde=K hr_SIZE= in_k2a>[3]);
<2a href="crypto/tea.c#L572" id=257" class="lin."sref">xtea_encri>smax="+csiz1= xtea_encXde=K hr_SIZE= in_k2 57
<2a href="crypto/tea.c#L582" id=258" class="lin."sref">xtea_encria_static int xtea_enc0static int in_k2aode=sum"2a href="crypto/tea.c#L592" id=259" class="lin."sref">xtea_encri>static void xtea_encstatic void in_k2 59
<2a href="crypto/tea.c#L602" id=260" class="lin."sref">xtea_encria_static void xtea_encsstatic void  1260}
<2a href="crypto/tea.c#L612" id=261" cl}naf="+code=z" class="sref">2 61
<2a href="crypto/tea.c#L622" id=262" class="line" name="L142"> 2rc)
<2a href="crypto/tea.c#L632" id=263" class="line" name="L120"> 12__in">sum != ef="+code=u32" classf="+mod_in">sum != _le32ine"+code=src" class="sref">s263{
<2a href="crypto/tea.c#L642" id=2L64" climit" class="sref">limit2a> = 0;
<2a href="crypto/tea.c#L652" id=265" class="line" name"sref">xtea_encryptf="+algs  struct f="+algs  struct 26/span>
<2  href="crypto/tea.c#L162  id=2676" cass="line" name="L133"> 12m);
<2a href="crypto/tea.c#L672" id=267" class="line" name="L107"> 2rc;
<2a href="crypto/tea.c#L682" id=2L68" class="line" name="L90">  90__ex">sum != ef="+code=u32" classf="+mod_fin"sum != s26ode=sum"2a href="crypto/tea.c#L692" id=269" cllimit" class="sref">limit2 69
<2a href="crypto/tea.c#L702" id=2770" class="line" name="L70">ryptf="+algs  struct f="+algs  struct 2a>[0]);
<2a href="crypto/tea.c#L712" id=271" class="line" name="L133"> 12 61
<2a href="crypto/tea.c#L722" id=272" class="line" name="L142"> 2 72
<2a href="crypto/tea.c#L732" id=2L73" c" class="sref">MODULE_ALIA/a> * "x">l"2a3{
<2a href="crypto/tea.c#L742" id=2L74" c" class="sref">MODULE_ALIA/a> * "xetl"2a> = 0;
<2a href="crypto/tea.c#L752" id=275" class="line" name="L145"> 2/a>[2];
<2a href="crypto/tea.c#L762" id=2L76" c" class="sref">modul1_in">sum != _le32" class="sref">f="+mod_in">sum != _le3ef="+code=z" class="sref">2a);
<2  href="crypto/tea.c#L172  id=2L77" c" class="sref">modul1_ex">sum != _le32" class="sref">f="+mod_fin"sum != 2ac;
<2a href="crypto/tea.c#L782" id=2738" class="line" name="L38"> 2DS;
<2a href="crypto/tea.c#L792" id=2796" c" class="sref">MODULE_LICENSE= "GPL"2 79
<2a href="crypto/tea.c#L802" id=2L80" c" class="sref">MODULE_DESCRIPTION= "de=, Xde=++code XETA Cclassgraph="lAlgorithms"2 >[0]);
<2a href="crypto/tea.c#L812" id=281" cl


The original LXR software by the "+code=z"http://sourceforge.net/projects/lxr">LXR commun">lxr@ptoux.noa hr.
lxr.ptoux.no kindly hosted by "+code=z"http://www.redpill-ptopro.no">Redpill Ltopro A/_le3, provider of Ltouxne" nulting and operss=" n servicen since=1985.