desCBC.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "os.h"
  10. #include <mp.h>
  11. #include <libsec.h>
  12. // Because of the way that non multiple of 8
  13. // buffers are handled, the decryptor must
  14. // be fed buffers of the same size as the
  15. // encryptor
  16. // If the length is not a multiple of 8, I encrypt
  17. // the overflow to be compatible with lacy's cryptlib
  18. void
  19. desCBCencrypt(uint8_t *p, int len, DESstate *s)
  20. {
  21. uint8_t *p2, *ip, *eip;
  22. for(; len >= 8; len -= 8){
  23. p2 = p;
  24. ip = s->ivec;
  25. for(eip = ip+8; ip < eip; )
  26. *p2++ ^= *ip++;
  27. block_cipher(s->expanded, p, 0);
  28. memmove(s->ivec, p, 8);
  29. p += 8;
  30. }
  31. if(len > 0){
  32. ip = s->ivec;
  33. block_cipher(s->expanded, ip, 0);
  34. for(eip = ip+len; ip < eip; )
  35. *p++ ^= *ip++;
  36. }
  37. }
  38. void
  39. desCBCdecrypt(uint8_t *p, int len, DESstate *s)
  40. {
  41. uint8_t *ip, *eip, *tp;
  42. uint8_t tmp[8];
  43. for(; len >= 8; len -= 8){
  44. memmove(tmp, p, 8);
  45. block_cipher(s->expanded, p, 1);
  46. tp = tmp;
  47. ip = s->ivec;
  48. for(eip = ip+8; ip < eip; ){
  49. *p++ ^= *ip;
  50. *ip++ = *tp++;
  51. }
  52. }
  53. if(len > 0){
  54. ip = s->ivec;
  55. block_cipher(s->expanded, ip, 0);
  56. for(eip = ip+len; ip < eip; )
  57. *p++ ^= *ip++;
  58. }
  59. }