smbdes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. a partial implementation of DES designed for use in the
  5. SMB authentication protocol
  6. Copyright (C) Andrew Tridgell 1998
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "includes.h"
  20. /* NOTES:
  21. This code makes no attempt to be fast! In fact, it is a very
  22. slow implementation
  23. This code is NOT a complete DES implementation. It implements only
  24. the minimum necessary for SMB authentication, as used by all SMB
  25. products (including every copy of Microsoft Windows95 ever sold)
  26. In particular, it can only do a unchained forward DES pass. This
  27. means it is not possible to use this code for encryption/decryption
  28. of data, instead it is only useful as a "hash" algorithm.
  29. There is no entry point into this code that allows normal DES operation.
  30. I believe this means that this code does not come under ITAR
  31. regulations but this is NOT a legal opinion. If you are concerned
  32. about the applicability of ITAR regulations to this code then you
  33. should confirm it for yourself (and maybe let me know if you come
  34. up with a different answer to the one above)
  35. */
  36. #undef uchar
  37. #define uchar const unsigned char
  38. static uchar perm1[56] = {57, 49, 41, 33, 25, 17, 9,
  39. 1, 58, 50, 42, 34, 26, 18,
  40. 10, 2, 59, 51, 43, 35, 27,
  41. 19, 11, 3, 60, 52, 44, 36,
  42. 63, 55, 47, 39, 31, 23, 15,
  43. 7, 62, 54, 46, 38, 30, 22,
  44. 14, 6, 61, 53, 45, 37, 29,
  45. 21, 13, 5, 28, 20, 12, 4};
  46. static uchar perm2[48] = {14, 17, 11, 24, 1, 5,
  47. 3, 28, 15, 6, 21, 10,
  48. 23, 19, 12, 4, 26, 8,
  49. 16, 7, 27, 20, 13, 2,
  50. 41, 52, 31, 37, 47, 55,
  51. 30, 40, 51, 45, 33, 48,
  52. 44, 49, 39, 56, 34, 53,
  53. 46, 42, 50, 36, 29, 32};
  54. static uchar perm3[64] = {58, 50, 42, 34, 26, 18, 10, 2,
  55. 60, 52, 44, 36, 28, 20, 12, 4,
  56. 62, 54, 46, 38, 30, 22, 14, 6,
  57. 64, 56, 48, 40, 32, 24, 16, 8,
  58. 57, 49, 41, 33, 25, 17, 9, 1,
  59. 59, 51, 43, 35, 27, 19, 11, 3,
  60. 61, 53, 45, 37, 29, 21, 13, 5,
  61. 63, 55, 47, 39, 31, 23, 15, 7};
  62. static uchar perm4[48] = { 32, 1, 2, 3, 4, 5,
  63. 4, 5, 6, 7, 8, 9,
  64. 8, 9, 10, 11, 12, 13,
  65. 12, 13, 14, 15, 16, 17,
  66. 16, 17, 18, 19, 20, 21,
  67. 20, 21, 22, 23, 24, 25,
  68. 24, 25, 26, 27, 28, 29,
  69. 28, 29, 30, 31, 32, 1};
  70. static uchar perm5[32] = { 16, 7, 20, 21,
  71. 29, 12, 28, 17,
  72. 1, 15, 23, 26,
  73. 5, 18, 31, 10,
  74. 2, 8, 24, 14,
  75. 32, 27, 3, 9,
  76. 19, 13, 30, 6,
  77. 22, 11, 4, 25};
  78. static uchar perm6[64] ={ 40, 8, 48, 16, 56, 24, 64, 32,
  79. 39, 7, 47, 15, 55, 23, 63, 31,
  80. 38, 6, 46, 14, 54, 22, 62, 30,
  81. 37, 5, 45, 13, 53, 21, 61, 29,
  82. 36, 4, 44, 12, 52, 20, 60, 28,
  83. 35, 3, 43, 11, 51, 19, 59, 27,
  84. 34, 2, 42, 10, 50, 18, 58, 26,
  85. 33, 1, 41, 9, 49, 17, 57, 25};
  86. static uchar sc[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
  87. static uchar sbox[8][4][16] = {
  88. {{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
  89. {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
  90. {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
  91. {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}},
  92. {{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
  93. {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
  94. {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
  95. {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}},
  96. {{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
  97. {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
  98. {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
  99. {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}},
  100. {{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
  101. {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
  102. {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
  103. {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}},
  104. {{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
  105. {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
  106. {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
  107. {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}},
  108. {{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
  109. {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
  110. {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
  111. {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}},
  112. {{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
  113. {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
  114. {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
  115. {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}},
  116. {{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
  117. {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
  118. {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
  119. {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}}};
  120. static void permute(char *out, char *in, uchar *p, int n)
  121. {
  122. int i;
  123. for (i=0;i<n;i++)
  124. out[i] = in[p[i]-1];
  125. }
  126. static void lshift(char *d, int count, int n)
  127. {
  128. char out[64];
  129. int i;
  130. for (i=0;i<n;i++)
  131. out[i] = d[(i+count)%n];
  132. for (i=0;i<n;i++)
  133. d[i] = out[i];
  134. }
  135. static void concat(char *out, char *in1, char *in2, int l1, int l2)
  136. {
  137. while (l1--)
  138. *out++ = *in1++;
  139. while (l2--)
  140. *out++ = *in2++;
  141. }
  142. static void xor(char *out, char *in1, char *in2, int n)
  143. {
  144. int i;
  145. for (i=0;i<n;i++)
  146. out[i] = in1[i] ^ in2[i];
  147. }
  148. static void dohash(char *out, char *in, char *key, int forw)
  149. {
  150. int i, j, k;
  151. char pk1[56];
  152. char c[28];
  153. char d[28];
  154. char cd[56];
  155. char ki[16][48];
  156. char pd1[64];
  157. char l[32], r[32];
  158. char rl[64];
  159. permute(pk1, key, perm1, 56);
  160. for (i=0;i<28;i++)
  161. c[i] = pk1[i];
  162. for (i=0;i<28;i++)
  163. d[i] = pk1[i+28];
  164. for (i=0;i<16;i++) {
  165. lshift(c, sc[i], 28);
  166. lshift(d, sc[i], 28);
  167. concat(cd, c, d, 28, 28);
  168. permute(ki[i], cd, perm2, 48);
  169. }
  170. permute(pd1, in, perm3, 64);
  171. for (j=0;j<32;j++) {
  172. l[j] = pd1[j];
  173. r[j] = pd1[j+32];
  174. }
  175. for (i=0;i<16;i++) {
  176. char er[48];
  177. char erk[48];
  178. char b[8][6];
  179. char cb[32];
  180. char pcb[32];
  181. char r2[32];
  182. permute(er, r, perm4, 48);
  183. xor(erk, er, ki[forw ? i : 15 - i], 48);
  184. for (j=0;j<8;j++)
  185. for (k=0;k<6;k++)
  186. b[j][k] = erk[j*6 + k];
  187. for (j=0;j<8;j++) {
  188. int m, n;
  189. m = (b[j][0]<<1) | b[j][5];
  190. n = (b[j][1]<<3) | (b[j][2]<<2) | (b[j][3]<<1) | b[j][4];
  191. for (k=0;k<4;k++)
  192. b[j][k] = (sbox[j][m][n] & (1<<(3-k)))?1:0;
  193. }
  194. for (j=0;j<8;j++)
  195. for (k=0;k<4;k++)
  196. cb[j*4+k] = b[j][k];
  197. permute(pcb, cb, perm5, 32);
  198. xor(r2, l, pcb, 32);
  199. for (j=0;j<32;j++)
  200. l[j] = r[j];
  201. for (j=0;j<32;j++)
  202. r[j] = r2[j];
  203. }
  204. concat(rl, r, l, 32, 32);
  205. permute(out, rl, perm6, 64);
  206. }
  207. static void str_to_key(unsigned char *str,unsigned char *key)
  208. {
  209. int i;
  210. key[0] = str[0]>>1;
  211. key[1] = ((str[0]&0x01)<<6) | (str[1]>>2);
  212. key[2] = ((str[1]&0x03)<<5) | (str[2]>>3);
  213. key[3] = ((str[2]&0x07)<<4) | (str[3]>>4);
  214. key[4] = ((str[3]&0x0F)<<3) | (str[4]>>5);
  215. key[5] = ((str[4]&0x1F)<<2) | (str[5]>>6);
  216. key[6] = ((str[5]&0x3F)<<1) | (str[6]>>7);
  217. key[7] = str[6]&0x7F;
  218. for (i=0;i<8;i++) {
  219. key[i] = (key[i]<<1);
  220. }
  221. }
  222. static void smbhash(unsigned char *out, unsigned char *in, unsigned char *key, int forw)
  223. {
  224. int i;
  225. char outb[64];
  226. char inb[64];
  227. char keyb[64];
  228. unsigned char key2[8];
  229. str_to_key(key, key2);
  230. for (i=0;i<64;i++) {
  231. inb[i] = (in[i/8] & (1<<(7-(i%8)))) ? 1 : 0;
  232. keyb[i] = (key2[i/8] & (1<<(7-(i%8)))) ? 1 : 0;
  233. outb[i] = 0;
  234. }
  235. dohash(outb, inb, keyb, forw);
  236. for (i=0;i<8;i++) {
  237. out[i] = 0;
  238. }
  239. for (i=0;i<64;i++) {
  240. if (outb[i])
  241. out[i/8] |= (1<<(7-(i%8)));
  242. }
  243. }
  244. void E_P16(unsigned char *p14,unsigned char *p16)
  245. {
  246. unsigned char sp8[8] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
  247. smbhash(p16, sp8, p14, 1);
  248. smbhash(p16+8, sp8, p14+7, 1);
  249. }
  250. void E_P24(unsigned char *p21, unsigned char *c8, unsigned char *p24)
  251. {
  252. smbhash(p24, c8, p21, 1);
  253. smbhash(p24+8, c8, p21+7, 1);
  254. smbhash(p24+16, c8, p21+14, 1);
  255. }
  256. void D_P16(unsigned char *p14, unsigned char *in, unsigned char *out)
  257. {
  258. smbhash(out, in, p14, 0);
  259. smbhash(out+8, in+8, p14+7, 0);
  260. }
  261. void E_old_pw_hash( unsigned char *p14, unsigned char *in, unsigned char *out)
  262. {
  263. smbhash(out, in, p14, 1);
  264. smbhash(out+8, in+8, p14+7, 1);
  265. }
  266. void cred_hash1(unsigned char *out,unsigned char *in,unsigned char *key)
  267. {
  268. unsigned char buf[8];
  269. smbhash(buf, in, key, 1);
  270. smbhash(out, buf, key+9, 1);
  271. }
  272. void cred_hash2(unsigned char *out,unsigned char *in,unsigned char *key)
  273. {
  274. unsigned char buf[8];
  275. static unsigned char key2[8];
  276. smbhash(buf, in, key, 1);
  277. key2[0] = key[7];
  278. smbhash(out, buf, key2, 1);
  279. }
  280. void cred_hash3(unsigned char *out,unsigned char *in,unsigned char *key, int forw)
  281. {
  282. static unsigned char key2[8];
  283. smbhash(out, in, key, forw);
  284. key2[0] = key[7];
  285. smbhash(out + 8, in + 8, key2, forw);
  286. }
  287. void SamOEMhash( unsigned char *data, unsigned char *key, int val)
  288. {
  289. unsigned char s_box[256];
  290. unsigned char index_i = 0;
  291. unsigned char index_j = 0;
  292. unsigned char j = 0;
  293. int ind;
  294. for (ind = 0; ind < 256; ind++)
  295. {
  296. s_box[ind] = (unsigned char)ind;
  297. }
  298. for( ind = 0; ind < 256; ind++)
  299. {
  300. unsigned char tc;
  301. j += (s_box[ind] + key[ind%16]);
  302. tc = s_box[ind];
  303. s_box[ind] = s_box[j];
  304. s_box[j] = tc;
  305. }
  306. for( ind = 0; ind < (val ? 516 : 16); ind++)
  307. {
  308. unsigned char tc;
  309. unsigned char t;
  310. index_i++;
  311. index_j += s_box[index_i];
  312. tc = s_box[index_i];
  313. s_box[index_i] = s_box[index_j];
  314. s_box[index_j] = tc;
  315. t = s_box[index_i] + s_box[index_j];
  316. data[ind] = data[ind] ^ s_box[t];
  317. }
  318. }