desECB.c 861 B

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