desECB.c 860 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "../lib9.h"
  2. #include "../libsec/libsec.h"
  3. // I wasn't sure what to do when the buffer was not
  4. // a multiple of 8. I did what lacy's cryptolib did
  5. // to be compatible, but it looks dangerous to me
  6. // since its encrypting plain text with the key. -- presotto
  7. void
  8. desECBencrypt(uchar *p, int len, DESstate *s)
  9. {
  10. int i;
  11. uchar tmp[8];
  12. for(; len >= 8; len -= 8){
  13. block_cipher(s->expanded, p, 0);
  14. p += 8;
  15. }
  16. if(len > 0){
  17. for (i=0; i<8; i++)
  18. tmp[i] = i;
  19. block_cipher(s->expanded, tmp, 0);
  20. for (i = 0; i < len; i++)
  21. p[i] ^= tmp[i];
  22. }
  23. }
  24. void
  25. desECBdecrypt(uchar *p, int len, DESstate *s)
  26. {
  27. int i;
  28. uchar tmp[8];
  29. for(; len >= 8; len -= 8){
  30. block_cipher(s->expanded, p, 1);
  31. p += 8;
  32. }
  33. if(len > 0){
  34. for (i=0; i<8; i++)
  35. tmp[i] = i;
  36. block_cipher(s->expanded, tmp, 0);
  37. for (i = 0; i < len; i++)
  38. p[i] ^= tmp[i];
  39. }
  40. }