desCBC.c 1.1 KB

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