des.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2001 by Rene Müller
  4. * DES based crypt() implementation, originally written for dietlibc by
  5. * Rene Müller, based on Bruce Schneier's Applied Cryptography, but
  6. * tightened up quite a bit.
  7. *
  8. * Copyright (C) 2001 by Erik Andersen
  9. * Adjusted each function to be reentrant, hacked in md5 support.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <crypt.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. extern char *md5_magic;
  30. extern char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data);
  31. /* Initial permutation, */
  32. static const char IP[] = {
  33. 57,49,41,33,25,17, 9, 1,
  34. 59,51,43,35,27,19,11, 3,
  35. 61,53,45,37,29,21,13, 5,
  36. 63,55,47,39,31,23,15, 7,
  37. 56,48,40,32,24,16, 8, 0,
  38. 58,50,42,34,26,18,10, 2,
  39. 60,52,44,36,28,20,12, 4,
  40. 62,54,46,38,30,22,14, 6
  41. };
  42. /* Final permutation, FP = IP^(-1) */
  43. static const char FP[] = {
  44. 39, 7,47,15,55,23,63,31,
  45. 38, 6,46,14,54,22,62,30,
  46. 37, 5,45,13,53,21,61,29,
  47. 36, 4,44,12,52,20,60,28,
  48. 35, 3,43,11,51,19,59,27,
  49. 34, 2,42,10,50,18,58,26,
  50. 33, 1,41, 9,49,17,57,25,
  51. 32, 0,40, 8,48,16,56,24
  52. };
  53. /* Permuted-choice 1 from the key bits to yield C and D.
  54. * Note that bits 8,16... are left out: They are intended for a parity check.
  55. */
  56. static const char PC1_C[] = {
  57. 56,48,40,32,24,16, 8,
  58. 0,57,49,41,33,25,17,
  59. 9, 1,58,50,42,34,26,
  60. 18,10, 2,59,51,43,35
  61. };
  62. static const char PC1_D[] = {
  63. 62,54,46,38,30,22,14,
  64. 6,61,53,45,37,29,21,
  65. 13, 5,60,52,44,36,28,
  66. 20,12, 4,27,19,11, 3
  67. };
  68. /* Sequence of shifts used for the key schedule. */
  69. static const char shifts[] = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
  70. /*
  71. * Permuted-choice 2, to pick out the bits from the CD array that generate
  72. * the key schedule.
  73. */
  74. static const char PC2_C[] = {
  75. 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
  76. 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1
  77. };
  78. static const char PC2_D[] = {
  79. 12, 23, 2, 8, 18, 26, 1, 11, 22, 16, 4, 19,
  80. 15, 20, 10, 27, 5, 24, 17, 13, 21, 7, 0, 3
  81. };
  82. static const char e2[] = {
  83. 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
  84. 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
  85. 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
  86. 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
  87. };
  88. /* The 8 selection functions. For some reason, they give a 0-origin index,
  89. * unlike everything else.
  90. */
  91. static const char S[8][64] = {
  92. {
  93. 14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
  94. 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
  95. 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
  96. 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13
  97. },
  98. {
  99. 15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
  100. 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
  101. 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
  102. 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9
  103. },
  104. {
  105. 10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
  106. 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
  107. 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
  108. 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12
  109. },
  110. {
  111. 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
  112. 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
  113. 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
  114. 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14
  115. },
  116. {
  117. 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
  118. 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
  119. 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
  120. 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3
  121. },
  122. {
  123. 12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
  124. 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
  125. 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
  126. 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13
  127. },
  128. {
  129. 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
  130. 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
  131. 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
  132. 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12
  133. },
  134. {
  135. 13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
  136. 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
  137. 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
  138. 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11
  139. }
  140. };
  141. /* P is a permutation on the selected combination of the current L and key. */
  142. static const char P[] = {
  143. 15, 6,19,20, 28,11,27,16, 0,14,22,25, 4,17,30, 9,
  144. 1, 7,23,13, 31,26, 2, 8, 18,12,29, 5, 21,10, 3,24
  145. };
  146. /* Set up the key schedule from the key. */
  147. void setkey_r(const char *key, struct crypt_data *data)
  148. {
  149. register int i, j, k;
  150. int t;
  151. int s;
  152. /* First, generate C and D by permuting the key. The low order bit of each
  153. * 8-bit char is not used, so C and D are only 28 bits apiece.
  154. */
  155. for(i=0; i < 28; i++) {
  156. data->C[i] = key[(int)PC1_C[i]];
  157. data->D[i] = key[(int)PC1_D[i]];
  158. }
  159. /* To generate Ki, rotate C and D according to schedule and pick up a
  160. * permutation using PC2.
  161. */
  162. for(i=0; i < 16; i++) {
  163. /* rotate. */
  164. s = shifts[i];
  165. for(k=0; k < s; k++) {
  166. t = data->C[0];
  167. for(j=0; j < 27; j++)
  168. data->C[j] = data->C[j+1];
  169. data->C[27] = t;
  170. t = data->D[0];
  171. for(j=0; j < 27; j++)
  172. data->D[j] = data->D[j+1];
  173. data->D[27] = t;
  174. }
  175. /* get Ki. Note C and D are concatenated. */
  176. for(j=0; j < 24; j++) {
  177. data->KS[i][j] = data->C[(int)PC2_C[j]];
  178. data->KS[i][j+24] = data->D[(int)PC2_D[j]];
  179. }
  180. }
  181. for(i=0; i < 48; i++)
  182. data->E[i] = e2[i];
  183. }
  184. /* The payoff: encrypt a block. */
  185. void encrypt_r(char block[64], int edflag, struct crypt_data *data)
  186. {
  187. int i, ii;
  188. register int t, j, k;
  189. /* First, permute the bits in the input */
  190. for(j=0; j < 64; j++)
  191. data->L[j] = data->block[(int)IP[j]];
  192. /* Perform an encryption operation 16 times. */
  193. for(ii=0; ii < 16; ii++) {
  194. i = ii;
  195. /* Save the R array, which will be the new L. */
  196. for(j=0; j < 32; j++)
  197. data->tempL[j] = data->R[j];
  198. /* Expand R to 48 bits using the E selector;
  199. * exclusive-or with the current key bits.
  200. */
  201. for(j=0; j < 48; j++)
  202. data->preS[j] = data->R[data->E[j]-1] ^ data->KS[i][j];
  203. /* The pre-select bits are now considered in 8 groups of 6 bits each.
  204. * The 8 selection functions map these 6-bit quantities into 4-bit
  205. * quantities and the results permuted to make an f(R, K).
  206. * The indexing into the selection functions is peculiar;
  207. * it could be simplified by rewriting the tables.
  208. */
  209. for(j=0; j < 8; j++) {
  210. t = ((j<<1)+j)<<1;
  211. k = S[j][(data->preS[t]<<5)+
  212. (data->preS[t+1]<<3)+
  213. (data->preS[t+2]<<2)+
  214. (data->preS[t+3]<<1)+
  215. (data->preS[t+4] )+
  216. (data->preS[t+5]<<4)];
  217. t = j << 2;
  218. data->f[t ] = (k>>3)&01;
  219. data->f[t+1] = (k>>2)&01;
  220. data->f[t+2] = (k>>1)&01;
  221. data->f[t+3] = (k )&01;
  222. }
  223. /* The new R is L ^ f(R, K). The f here has to be permuted first, though. */
  224. for(j=0; j < 32; j++)
  225. data->R[j] = data->L[j] ^ data->f[(int)P[j]];
  226. /* Finally, the new L (the original R) is copied back. */
  227. for(j=0; j < 32; j++)
  228. data->L[j] = data->tempL[j];
  229. }
  230. /* The output L and R are reversed. */
  231. for(j=0; j < 32; j++) {
  232. data->L[j] ^= data->R[j];
  233. data->R[j] ^= data->L[j];
  234. data->L[j] ^= data->R[j];
  235. }
  236. /* The final output gets the inverse permutation of the very original. */
  237. for(j=0; j < 64; j++)
  238. data->block[j] = data->L[(int)FP[j]];
  239. }
  240. char * crypt_r(const char *pw, const char *salt, struct crypt_data *data)
  241. {
  242. register int i, j, c;
  243. /* Check if we are supposed to be using the MD5 encryption replacement. */
  244. if (strncmp (md5_magic, salt, sizeof (md5_magic) - 1) == 0)
  245. return md5_crypt_r(pw, salt, data);
  246. for(i=0; i < 66; i++)
  247. data->block[i] = 0;
  248. for(i=0; (c= *pw) && i < 64; pw++) {
  249. for(j=0; j < 7; j++, i++)
  250. data->block[i] = (c>>(6-j)) & 01;
  251. i++;
  252. }
  253. setkey_r(data->block, data);
  254. for(i=0; i < 66; i++)
  255. data->block[i] = 0;
  256. for(i=0; i < 2; i++) {
  257. c = *salt++;
  258. data->iobuf[i] = c;
  259. if(c > 'Z')
  260. c -= 6;
  261. if(c > '9')
  262. c -= 7;
  263. c -= '.';
  264. for(j=0; j < 6; j++) {
  265. if((c>>j) & 01) {
  266. int ind1 = (((i<<1)+i)<< 1) + j;
  267. int ind2 = ind1 + 24;
  268. data->E[ind1] ^= data->E[ind2];
  269. data->E[ind2] ^= data->E[ind1];
  270. data->E[ind1] ^= data->E[ind2];
  271. }
  272. }
  273. }
  274. for(i=0; i < 25; i++)
  275. encrypt_r(data->block, 0, data);
  276. for(i=0; i < 11; i++) {
  277. c = 0;
  278. for(j=0; j < 6; j++) {
  279. c <<= 1;
  280. c |= data->block[(((i<<1)+i)<<1)+j];
  281. }
  282. c += '.';
  283. if(c > '9')
  284. c += 7;
  285. if(c > 'Z')
  286. c += 6;
  287. data->iobuf[i+2] = c;
  288. }
  289. data->iobuf[i+2] = 0;
  290. if(data->iobuf[1] == 0)
  291. data->iobuf[1] = data->iobuf[0];
  292. return(data->iobuf);
  293. }