crypt.c 963 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Data Encryption Standard
  3. * D.P.Mitchell 83/06/08.
  4. *
  5. * block_cipher(key, block, decrypting)
  6. */
  7. #include "lib9.h"
  8. #include "auth.h"
  9. #include "libsec/libsec.h"
  10. /*
  11. * destructively encrypt the buffer, which
  12. * must be at least 8 characters long.
  13. */
  14. int
  15. encrypt(void *key, void *vbuf, int n)
  16. {
  17. ulong ekey[32];
  18. uchar *buf;
  19. int i, r;
  20. if(n < 8)
  21. return 0;
  22. key_setup(key, ekey);
  23. buf = vbuf;
  24. n--;
  25. r = n % 7;
  26. n /= 7;
  27. for(i = 0; i < n; i++){
  28. block_cipher(ekey, buf, 0);
  29. buf += 7;
  30. }
  31. if(r)
  32. block_cipher(ekey, buf - 7 + r, 0);
  33. return 1;
  34. }
  35. /*
  36. * destructively decrypt the buffer, which
  37. * must be at least 8 characters long.
  38. */
  39. int
  40. decrypt(void *key, void *vbuf, int n)
  41. {
  42. ulong ekey[32];
  43. uchar *buf;
  44. int i, r;
  45. if(n < 8)
  46. return 0;
  47. key_setup(key, ekey);
  48. buf = vbuf;
  49. n--;
  50. r = n % 7;
  51. n /= 7;
  52. buf += n * 7;
  53. if(r)
  54. block_cipher(ekey, buf - 7 + r, 1);
  55. for(i = 0; i < n; i++){
  56. buf -= 7;
  57. block_cipher(ekey, buf, 1);
  58. }
  59. return 1;
  60. }