crypt.c 1.0 KB

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