des3CBC.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. des3CBCencrypt(uchar *p, int len, DES3state *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. triple_block_cipher(s->expanded, p, DES3EDE);
  20. memmove(s->ivec, p, 8);
  21. p += 8;
  22. }
  23. if(len > 0){
  24. ip = s->ivec;
  25. triple_block_cipher(s->expanded, ip, DES3EDE);
  26. for(eip = ip+len; ip < eip; )
  27. *p++ ^= *ip++;
  28. }
  29. }
  30. void
  31. des3CBCdecrypt(uchar *p, int len, DES3state *s)
  32. {
  33. uchar *ip, *eip, *tp;
  34. uchar tmp[8];
  35. for(; len >= 8; len -= 8){
  36. memmove(tmp, p, 8);
  37. triple_block_cipher(s->expanded, p, DES3DED);
  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. triple_block_cipher(s->expanded, ip, DES3EDE);
  48. for(eip = ip+len; ip < eip; )
  49. *p++ ^= *ip++;
  50. }
  51. }