home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersguides-&-software / hdsk41.zip / SOURCE.ZIP / IDEA.CPP < prev    next >
C/C++ Source or Header  |  1994-04-20  |  3KB  |  171 lines

  1. /*IDEA.C   v2.1
  2.     c source code for IDEA block cipher. IDEA (International Data
  3.     Encryption Algorithm), formerly known as IPES (Improved Proposed
  4.     Encryption Standard). Algorithm developed by Xuejia Lai and James L.
  5.     Massey, of ETH Zurich. This implementation modified and derived from
  6.     original C code developed by Xuejia Lai. Zero-based indexing added,
  7.     names changed from IPES to IDEA. CFB functions added. Random Number
  8.     routines added. Optimized for speed 21 Oct 92 by Colin Plumb
  9.     <colin@nsq.gts.org>  This code assumes that each pair of 8-bit bytes
  10.     comprising a 16-bit word in the key and in the cipher block are
  11.     externally represented with the Most Significant Byte (MSB) first,
  12.     regardless of internal native byte order of the target cpu.
  13.     extended for use with hideseek by Colin Maroney (shaggy@phantom.com)
  14.     3/31/94. source: Dr. Dobbs Journal #208  */
  15.  
  16.  
  17. /* Usage:    use en_key_idea(word16 key[8],IDEAkey Z) to generate an
  18.          encryption key.
  19.          use de_key_idea(IDEAkey Z,IDEAkey DK) to generate a decryption
  20.          key from an encryption key.
  21.          use cipher_idea(word16 in[4], word16 out[4],IDEAkey Z) to
  22.          encode and decode (use DK instead of Z to decode). */
  23.  
  24.  
  25.  
  26. #include "idea.h"
  27.  
  28. static uint16 inv(uint16 x)
  29. {
  30.    uint16 t0,t1;
  31.    uint16 q,y;
  32.    if (x<=1)
  33.       return x;
  34.    t1=(uint16)(0x10001l/x);
  35.    y=(uint16)(0x10001l%x);
  36.    if (y==1)
  37.     return low16(1-t1);
  38.    t0=1;
  39.    do
  40.    {
  41.       q=x/y;
  42.       x=x%y;
  43.       t0+=q*t1;
  44.       if (x==1)
  45.     return t0;
  46.       q=y/x;
  47.       y=y%x;
  48.       t1+=q*t0;
  49.    } while (y!=1);
  50.    return low16(1-t1);
  51. }
  52.  
  53. void en_key_idea(word16 *userkey, word16 *Z)
  54. {
  55.    int i,j;
  56.    /* shifts */
  57.    for (j=0;j<8;j++)
  58.       Z[j]=*userkey++;
  59.    for (i=0;j<KEYLEN;j++)
  60.    {
  61.       i++;
  62.       Z[i+7]=((Z[i&7] << 9) | (Z[i+1 & 7] >> 7));
  63.       Z+=i&8;
  64.       i&=7;
  65.    }
  66. }
  67.  
  68. void de_key_idea(IDEAkey Z,IDEAkey DK)
  69. {
  70.    int j;
  71.    uint16 t1,t2,t3;
  72.    IDEAkey T;
  73.    word16 *p=T+KEYLEN;
  74.    t1=inv(*Z++);
  75.    t2=-*Z++;
  76.    t3=-*Z++;
  77.    *--p=inv(*Z++);
  78.    *--p=t3;
  79.    *--p=t2;
  80.    *--p=t1;
  81.    for (j=1;j<ROUNDS;j++)
  82.    {
  83.       t1=*Z++;
  84.       *--p=*Z++;
  85.       *--p=t1;
  86.       t1=inv(*Z++);
  87.       t2=-*Z++;
  88.       t3=-*Z++;
  89.       *--p=inv(*Z++);
  90.       *--p=t2;
  91.       *--p=t3;
  92.       *--p=t1;
  93.    }
  94.    t1=*Z++;
  95.    *--p=*Z++;
  96.    *--p=t1;
  97.    t1=inv(*Z++);
  98.    t2=-*Z++;
  99.    t3=-*Z++;
  100.    *--p=inv(*Z++);
  101.    *--p=t3;
  102.    *--p=t2;
  103.    *--p=t1;
  104.    /*copy and destroy temp copy*/
  105.    for(j=0,p=T;j<KEYLEN;j++)
  106.    {
  107.       *DK++=*p;
  108.       *p++=0;
  109.    }
  110. }
  111.  
  112.  
  113. uint16 mul(uint16 a, uint16 b)
  114. {
  115.    word32 p;
  116.  
  117.    if (a)
  118.    {
  119.       if (b)
  120.       {
  121.      p=(word32)a*b;
  122.      b=(uint16)(low16(p));
  123.      a=(uint16)(p>>16);
  124.      return b-a+(b<a);
  125.       }
  126.       else
  127.       {
  128.      return 1-a;
  129.       }
  130.    }
  131.    else
  132.       return 1-b;
  133. }
  134.  
  135. #define MUL(x,y) (x=mul(low16(x),y))
  136.  
  137.  
  138. #define CONST
  139.  
  140. void cipher_idea(word16 in[4],word16 out[4],register CONST IDEAkey Z)
  141. {
  142.    register uint16 x1,x2,x3,x4,t1,t2;
  143.    int r=ROUNDS;
  144.    x1=*in++; x2=*in++;
  145.    x3=*in++; x4=*in;
  146.    do
  147.    {
  148.       MUL(x1,*Z++);
  149.       x2+=*Z++;
  150.       x3+=*Z++;
  151.       MUL(x4,*Z++);
  152.       t2=x1^x3;
  153.       MUL(t2,*Z++);
  154.       t1=t2+(x2^x4);
  155.       MUL(t1,*Z++);
  156.       t2=t1+t2;
  157.       x1^=t1;
  158.       x4^=t2;
  159.       t2^=x2;
  160.       x2=x3^t1;
  161.       x3=t2;
  162.    } while (--r);
  163.    MUL(x1,*Z++);
  164.    *out++=x1;
  165.    *out++=(x3+*Z++);
  166.    *out++=(x2+*Z++);
  167.    MUL(x4,*Z);
  168.    *out=x4;
  169. }
  170.  
  171.