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

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