xdmauth.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Convenience functions to encrypt and decrypt the cookies used in
  3. * XDM-AUTHORIZATION-1.
  4. */
  5. #include "ssh.h"
  6. static ssh_cipher *des_xdmauth_cipher(const void *vkeydata)
  7. {
  8. /*
  9. * XDM-AUTHORIZATION-1 uses single-DES, but packs the key into 7
  10. * bytes, so here we have to repack it manually into the canonical
  11. * form where it occupies 8 bytes each with the low bit unused.
  12. */
  13. const unsigned char *keydata = (const unsigned char *)vkeydata;
  14. unsigned char key[8];
  15. int i, nbits, j;
  16. unsigned int bits;
  17. bits = 0;
  18. nbits = 0;
  19. j = 0;
  20. for (i = 0; i < 8; i++) {
  21. if (nbits < 7) {
  22. bits = (bits << 8) | keydata[j];
  23. nbits += 8;
  24. j++;
  25. }
  26. key[i] = (bits >> (nbits - 7)) << 1;
  27. bits &= ~(0x7F << (nbits - 7));
  28. nbits -= 7;
  29. }
  30. ssh_cipher *c = ssh_cipher_new(&ssh_des);
  31. ssh_cipher_setkey(c, key);
  32. smemclr(key, sizeof(key));
  33. ssh_cipher_setiv(c, key);
  34. return c;
  35. }
  36. void des_encrypt_xdmauth(const void *keydata, void *blk, int len)
  37. {
  38. ssh_cipher *c = des_xdmauth_cipher(keydata);
  39. ssh_cipher_encrypt(c, blk, len);
  40. ssh_cipher_free(c);
  41. }
  42. void des_decrypt_xdmauth(const void *keydata, void *blk, int len)
  43. {
  44. ssh_cipher *c = des_xdmauth_cipher(keydata);
  45. ssh_cipher_decrypt(c, blk, len);
  46. ssh_cipher_free(c);
  47. }