crypt.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Data Encryption Standard
  11. * D.P.Mitchell 83/06/08.
  12. *
  13. * block_cipher(key, block, decrypting)
  14. *
  15. * these routines use the non-standard 7 byte format
  16. * for DES keys.
  17. */
  18. #include <u.h>
  19. #include <libc.h>
  20. #include <auth.h>
  21. #include <libsec.h>
  22. /*
  23. * destructively encrypt the buffer, which
  24. * must be at least 8 characters long.
  25. */
  26. int
  27. encrypt(void *key, void *vbuf, int n)
  28. {
  29. uint32_t ekey[32];
  30. uint8_t *buf;
  31. int i, r;
  32. if(n < 8)
  33. return 0;
  34. key_setup(key, ekey);
  35. buf = vbuf;
  36. n--;
  37. r = n % 7;
  38. n /= 7;
  39. for(i = 0; i < n; i++){
  40. block_cipher(ekey, buf, 0);
  41. buf += 7;
  42. }
  43. if(r)
  44. block_cipher(ekey, buf - 7 + r, 0);
  45. return 1;
  46. }
  47. /*
  48. * destructively decrypt the buffer, which
  49. * must be at least 8 characters long.
  50. */
  51. int
  52. decrypt(void *key, void *vbuf, int n)
  53. {
  54. uint32_t ekey[128];
  55. uint8_t *buf;
  56. int i, r;
  57. if(n < 8)
  58. return 0;
  59. key_setup(key, ekey);
  60. buf = vbuf;
  61. n--;
  62. r = n % 7;
  63. n /= 7;
  64. buf += n * 7;
  65. if(r)
  66. block_cipher(ekey, buf - 7 + r, 1);
  67. for(i = 0; i < n; i++){
  68. buf -= 7;
  69. block_cipher(ekey, buf, 1);
  70. }
  71. return 1;
  72. }