1
0

desECB.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // I wasn't sure what to do when the buffer was not
  13. // a multiple of 8. I did what lacy's cryptolib did
  14. // to be compatible, but it looks dangerous to me
  15. // since its encrypting plain text with the key. -- presotto
  16. void
  17. desECBencrypt(uint8_t *p, int len, DESstate *s)
  18. {
  19. int i;
  20. uint8_t tmp[8];
  21. for(; len >= 8; len -= 8){
  22. block_cipher(s->expanded, p, 0);
  23. p += 8;
  24. }
  25. if(len > 0){
  26. for (i=0; i<8; i++)
  27. tmp[i] = i;
  28. block_cipher(s->expanded, tmp, 0);
  29. for (i = 0; i < len; i++)
  30. p[i] ^= tmp[i];
  31. }
  32. }
  33. void
  34. desECBdecrypt(uint8_t *p, int len, DESstate *s)
  35. {
  36. int i;
  37. uint8_t tmp[8];
  38. for(; len >= 8; len -= 8){
  39. block_cipher(s->expanded, p, 1);
  40. p += 8;
  41. }
  42. if(len > 0){
  43. for (i=0; i<8; i++)
  44. tmp[i] = i;
  45. block_cipher(s->expanded, tmp, 0);
  46. for (i = 0; i < len; i++)
  47. p[i] ^= tmp[i];
  48. }
  49. }