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

  1. /* cbc_encrypt.c */
  2. /* Copyright (C) 1992 Eric Young - see COPYING for more details */
  3. #include "des_local.h"
  4.  
  5. int des_cbc_encrypt(input,output,length,schedule,ivec,encrypt)
  6. des_cblock *input;
  7. des_cblock *output;
  8. long length;
  9. des_key_schedule schedule;
  10. des_cblock *ivec;
  11. int encrypt;
  12.     {
  13.     register ulong tin0,tin1;
  14.     register ulong tout0,tout1,xor0,xor1;
  15.     register uchar *in,*out;
  16.     register long l=length;
  17.     ulong tout[2],tin[2];
  18.     uchar *iv;
  19.  
  20.     in=(uchar *)input;
  21.     out=(uchar *)output;
  22.     iv=(uchar *)ivec;
  23.  
  24.     if (encrypt)
  25.         {
  26.         c2l(iv,tout0);
  27.         c2l(iv,tout1);
  28.         for (; l>0; l-=8)
  29.             {
  30.             if (l >= 8)
  31.                 {
  32.                 c2l(in,tin0);
  33.                 c2l(in,tin1);
  34.                 }
  35.             else
  36.                 c2ln(in,tin0,tin1,l);
  37.             tin0^=tout0;
  38.             tin1^=tout1;
  39.             tin[0]=tin0;
  40.             tin[1]=tin1;
  41.             des_encrypt((ulong *)tin,(ulong *)tout,
  42.                 schedule,encrypt);
  43.             tout0=tout[0];
  44.             tout1=tout[1];
  45.             l2c(tout0,out);
  46.             l2c(tout1,out);
  47.             }
  48.         }
  49.     else
  50.         {
  51.         c2l(iv,xor0);
  52.         c2l(iv,xor1);
  53.         for (; l>0; l-=8)
  54.             {
  55.             c2l(in,tin0);
  56.             c2l(in,tin1);
  57.             tin[0]=tin0;
  58.             tin[1]=tin1;
  59.             des_encrypt((ulong *)tin,(ulong *)tout,
  60.                 schedule,encrypt);
  61.             tout0=tout[0]^xor0;
  62.             tout1=tout[1]^xor1;
  63.             if (l >= 8)
  64.                 {
  65.                 l2c(tout0,out);
  66.                 l2c(tout1,out);
  67.                 }
  68.             else
  69.                 l2cn(tout0,tout1,out,l);
  70.             xor0=tin0;
  71.             xor1=tin1;
  72.             }
  73.         }
  74.     tin0=tin1=tout0=tout1=xor0=xor1=0;
  75.     return(0);
  76.     }
  77.  
  78.