home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume29 / libdes / part01 / ecb_encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-04  |  3.5 KB  |  146 lines

  1. /* ecb_encrypt.c */
  2. /* Copyright (C) 1992 Eric Young - see COPYING for more details */
  3. #include "des_local.h"
  4. #include "sp.h"
  5.  
  6. int des_ecb_encrypt(input,output,ks,encrypt)
  7. des_cblock *input;
  8. des_cblock *output;
  9. des_key_schedule ks;
  10. int encrypt;
  11.     {
  12.     register ulong l0,l1;
  13.     register uchar *in,*out;
  14.     ulong ll[2];
  15.  
  16.     in=(uchar *)input;
  17.     out=(uchar *)output;
  18.     c2l(in,l0);
  19.     c2l(in,l1);
  20.     ll[0]=l0;
  21.     ll[1]=l1;
  22.     des_encrypt(ll,ll,ks,encrypt);
  23.     l0=ll[0];
  24.     l1=ll[1];
  25.     l2c(l0,out);
  26.     l2c(l1,out);
  27.     return(0);
  28.     }
  29.  
  30. #define D_ENCRYPT(L,R,S)    \
  31.     t=(R<<1)|(R>>31); \
  32.     u=(t^s[S  ]); \
  33.     t=(t^s[S+1]); \
  34.     t=(t>>4)|(t<<28); \
  35.     L^=    des_SPtrans[1][(t    )&0x3f]| \
  36.         des_SPtrans[3][(t>> 8)&0x3f]| \
  37.         des_SPtrans[5][(t>>16)&0x3f]| \
  38.         des_SPtrans[7][(t>>24)&0x3f]| \
  39.         des_SPtrans[0][(u    )&0x3f]| \
  40.         des_SPtrans[2][(u>> 8)&0x3f]| \
  41.         des_SPtrans[4][(u>>16)&0x3f]| \
  42.         des_SPtrans[6][(u>>24)&0x3f];
  43.  
  44.     /* IP and FP
  45.      * The problem is more of a geometric problem that random bit fiddling.
  46.      0  1  2  3  4  5  6  7      62 54 46 38 30 22 14  6
  47.      8  9 10 11 12 13 14 15      60 52 44 36 28 20 12  4
  48.         16 17 18 19 20 21 22 23      58 50 42 34 26 18 10  2
  49.     24 25 26 27 28 29 30 31  to  56 48 40 32 24 16  8  0
  50.  
  51.     32 33 34 35 36 37 38 39      63 55 47 39 31 23 15  7
  52.     40 41 42 43 44 45 46 47      61 53 45 37 29 21 13  5
  53.     48 49 50 51 52 53 54 55      59 51 43 35 27 19 11  3
  54.     56 57 58 59 60 61 62 63      57 49 41 33 25 17  9  1
  55.  
  56.     The output has been subject to swaps of the form
  57.     0 1 -> 3 1 but the odd and even bits have been put into
  58.     2 3    2 0 
  59.     different words.  The main trick is to remember that
  60.     t=((l>>size)^r)&(mask);
  61.     r^=t;
  62.     l^=(t<<size);
  63.     can be used to swap and move bits between words.
  64.  
  65.     So l =  0  1  2  3  r = 16 17 18 19
  66.             4  5  6  7      20 21 22 23
  67.             8  9 10 11      24 25 26 27
  68.            12 13 14 15      28 29 30 31
  69.     becomes (for size == 2 and mask == 0x3333)
  70.        t =   2^16  3^17 -- --   l =  0  1 16 17  r =  2  3 18 19
  71.          6^20  7^21 -- --        4  5 20 21       6  7 22 23
  72.         10^24 11^25 -- --        8  9 24 25      10 11 24 25
  73.                 14^28 15^29 -- --       12 13 28 29      14 15 28 29
  74.  
  75.     Thanks for hints from Richard Outerbridge - he told me IP&FP
  76.     could be done in 15 xor, 10 shifts and 5 ands.
  77.     When I finally started to think of the problem in 2D
  78.     I first got ~42 operations without xors.  When I remembered
  79.     how to use xors :-) I got it to its final state.
  80.     */
  81. #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  82.     (b)^=(t),\
  83.     (a)^=((t)<<(n)))
  84.  
  85. int des_encrypt(input,output,ks,encrypt)
  86. ulong *input;
  87. ulong *output;
  88. des_key_schedule ks;
  89. int encrypt;
  90.     {
  91.     register ulong l,r,t,u;
  92.     register int i;
  93.     register ulong *s;
  94.  
  95.     l=input[0];
  96.     r=input[1];
  97.  
  98.     /* do IP */
  99.     PERM_OP(r,l,t, 4,0x0f0f0f0f);
  100.     PERM_OP(l,r,t,16,0x0000ffff);
  101.     PERM_OP(r,l,t, 2,0x33333333);
  102.     PERM_OP(l,r,t, 8,0x00ff00ff);
  103.     PERM_OP(r,l,t, 1,0x55555555);
  104.     /* r and l are reversed - remember that :-) */
  105.     t=l;
  106.     l=r;
  107.     r=t;
  108.  
  109.     s=(ulong *)ks;
  110.  
  111.     /* I don't know if it is worth the effort of loop unrolling the
  112.      * inner loop */
  113.     if (encrypt)
  114.         {
  115.         for (i=0; i<32; i+=4)
  116.             {
  117.             D_ENCRYPT(l,r,i+0); /*  1 */
  118.             D_ENCRYPT(r,l,i+2); /*  2 */
  119.             }
  120.         }
  121.     else
  122.         {
  123.         for (i=30; i>0; i-=4)
  124.             {
  125.             D_ENCRYPT(l,r,i-0); /* 16 */
  126.             D_ENCRYPT(r,l,i-2); /* 15 */
  127.             }
  128.         }
  129.  
  130.     /* swap l and r
  131.      * we will not do the swap so just remember they are
  132.      * reversed for the rest of the subroutine
  133.      * luckily FP fixes this problem :-) */
  134.  
  135.     PERM_OP(r,l,t, 1,0x55555555);
  136.     PERM_OP(l,r,t, 8,0x00ff00ff);
  137.     PERM_OP(r,l,t, 2,0x33333333);
  138.     PERM_OP(l,r,t,16,0x0000ffff);
  139.     PERM_OP(r,l,t, 4,0x0f0f0f0f);
  140.  
  141.     output[0]=l;
  142.     output[1]=r;
  143.     return(0);
  144.     }
  145.  
  146.