desCBC.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "../lib9.h"
  2. #include "../libsec/libsec.h"
  3. // Because of the way that non multiple of 8
  4. // buffers are handled, the decryptor must
  5. // be fed buffers of the same size as the
  6. // encryptor
  7. // If the length is not a multiple of 8, I encrypt
  8. // the overflow to be compatible with lacy's cryptlib
  9. void
  10. desCBCencrypt(uchar *p, int len, DESstate *s)
  11. {
  12. uchar *p2, *ip, *eip;
  13. for(; len >= 8; len -= 8){
  14. p2 = p;
  15. ip = s->ivec;
  16. for(eip = ip+8; ip < eip; )
  17. *p2++ ^= *ip++;
  18. block_cipher(s->expanded, p, 0);
  19. memmove(s->ivec, p, 8);
  20. p += 8;
  21. }
  22. if(len > 0){
  23. ip = s->ivec;
  24. block_cipher(s->expanded, ip, 0);
  25. for(eip = ip+len; ip < eip; )
  26. *p++ ^= *ip++;
  27. }
  28. }
  29. void
  30. desCBCdecrypt(uchar *p, int len, DESstate *s)
  31. {
  32. uchar *ip, *eip, *tp;
  33. uchar tmp[8];
  34. for(; len >= 8; len -= 8){
  35. memmove(tmp, p, 8);
  36. block_cipher(s->expanded, p, 1);
  37. tp = tmp;
  38. ip = s->ivec;
  39. for(eip = ip+8; ip < eip; ){
  40. *p++ ^= *ip;
  41. *ip++ = *tp++;
  42. }
  43. }
  44. if(len > 0){
  45. ip = s->ivec;
  46. block_cipher(s->expanded, ip, 0);
  47. for(eip = ip+len; ip < eip; )
  48. *p++ ^= *ip++;
  49. }
  50. }