patch-src_common_passcrypt_c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. $OpenBSD: patch-src_common_passcrypt_c,v 1.1 2015/01/12 21:47:13 landry Exp $
  2. Use libressl des code to read/write the pwd. backwards-compatible with 5.6
  3. --- src/common/passcrypt.c.orig Sat Dec 14 11:15:06 2013
  4. +++ src/common/passcrypt.c Sun Jan 11 22:32:43 2015
  5. @@ -35,6 +35,7 @@
  6. #endif
  7. #include <glib.h>
  8. +#include <openssl/des.h>
  9. #include "passcrypt.h"
  10. @@ -72,100 +73,30 @@ crypt_cfb_buf(const char key[8], unsigned char *buf, u
  11. ecb_crypt(des_key, buf, len, DES_ENCRYPT);
  12. }
  13. #else
  14. -static void crypt_cfb_shift(unsigned char *to,
  15. - const unsigned char *from, unsigned len);
  16. -static void crypt_cfb_xor(unsigned char *to, const unsigned char *from,
  17. - unsigned len);
  18. -static void crypt_unpack(unsigned char *a);
  19. -
  20. static void
  21. crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
  22. unsigned chunksize, int decrypt)
  23. {
  24. - unsigned char temp[64];
  25. + unsigned char *out;
  26. + char des_key[8];
  27. + DES_key_schedule keysched;
  28. - memcpy(temp, key, 8);
  29. - crypt_unpack(temp);
  30. - setkey((const char *) temp);
  31. - memset(temp, 0, sizeof(temp));
  32. + out = malloc(len);
  33. + if(out == NULL)
  34. + return;
  35. + strncpy(des_key, PASSCRYPT_KEY, 8);
  36. + memset(&crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
  37. +
  38. + DES_set_odd_parity(&des_key);
  39. + DES_set_key_unchecked(&des_key, &keysched);
  40. + if (decrypt)
  41. + DES_cfb_encrypt(buf, out, crypt_cfb_blocksize,\
  42. + len, &keysched, &crypt_cfb_iv, DES_DECRYPT);
  43. + else
  44. + DES_cfb_encrypt(buf, out, crypt_cfb_blocksize,\
  45. + len, &keysched, &crypt_cfb_iv, DES_ENCRYPT);
  46. - memset(crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
  47. -
  48. - if (chunksize > crypt_cfb_blocksize)
  49. - chunksize = crypt_cfb_blocksize;
  50. -
  51. - while (len) {
  52. - memcpy(temp, crypt_cfb_iv, sizeof(temp));
  53. - encrypt((char *) temp, 0);
  54. - if (chunksize > len)
  55. - chunksize = len;
  56. - if (decrypt)
  57. - crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
  58. - crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
  59. - if (!decrypt)
  60. - crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
  61. - len -= chunksize;
  62. - buf += chunksize;
  63. - }
  64. -}
  65. -
  66. -/*
  67. -* Shift len bytes from end of to buffer to beginning, then put len
  68. -* bytes from from at the end. Caution: the to buffer is unpacked,
  69. -* but the from buffer is not.
  70. -*/
  71. -static void
  72. -crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
  73. -{
  74. - unsigned i;
  75. - unsigned j;
  76. - unsigned k;
  77. -
  78. - if (len < crypt_cfb_blocksize) {
  79. - i = len * 8;
  80. - j = crypt_cfb_blocksize * 8;
  81. - for (k = i; k < j; k++) {
  82. - to[0] = to[i];
  83. - ++to;
  84. - }
  85. - }
  86. -
  87. - for (i = 0; i < len; i++) {
  88. - j = *from++;
  89. - for (k = 0x80; k; k >>= 1)
  90. - *to++ = ((j & k) != 0);
  91. - }
  92. -}
  93. -
  94. -/*
  95. -* XOR len bytes from from into the data at to. Caution: the from buffer
  96. -* is unpacked, but the to buffer is not.
  97. -*/
  98. -static void
  99. -crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
  100. -{
  101. - unsigned i;
  102. - unsigned j;
  103. - unsigned char c;
  104. -
  105. - for (i = 0; i < len; i++) {
  106. - c = 0;
  107. - for (j = 0; j < 8; j++)
  108. - c = (c << 1) | *from++;
  109. - *to++ ^= c;
  110. - }
  111. -}
  112. -
  113. -/*
  114. -* Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
  115. -* each bit into its own byte.
  116. -*/
  117. -static void crypt_unpack(unsigned char *a)
  118. -{
  119. - int i, j;
  120. -
  121. - for (i = 7; i >= 0; --i)
  122. - for (j = 7; j >= 0; --j)
  123. - a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;
  124. + strncpy(buf, out, len);
  125. + free(out);
  126. }
  127. #endif