crypt_des.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * This version has been further modified by Rich Felker, primary author
  3. * and maintainer of musl libc, to remove table generation code and
  4. * replaced all runtime-generated constant tables with static-initialized
  5. * tables in the binary, in the interest of minimizing non-shareable
  6. * memory usage and stack size requirements.
  7. */
  8. /*
  9. * This version is derived from the original implementation of FreeSec
  10. * (release 1.1) by David Burren. I've made it reentrant, reduced its memory
  11. * usage from about 70 KB to about 7 KB (with only minimal performance impact
  12. * and keeping code size about the same), made the handling of invalid salts
  13. * mostly UFC-crypt compatible, added a quick runtime self-test (which also
  14. * serves to zeroize the stack from sensitive data), and added optional tests.
  15. * - Solar Designer <solar at openwall.com>
  16. */
  17. /*
  18. * FreeSec: libcrypt for NetBSD
  19. *
  20. * Copyright (c) 1994 David Burren
  21. * Copyright (c) 2000,2002,2010,2012 Solar Designer
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. * 1. Redistributions of source code must retain the above copyright
  28. * notice, this list of conditions and the following disclaimer.
  29. * 2. Redistributions in binary form must reproduce the above copyright
  30. * notice, this list of conditions and the following disclaimer in the
  31. * documentation and/or other materials provided with the distribution.
  32. * 3. Neither the name of the author nor the names of other contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  37. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. *
  48. * $Owl: Owl/packages/glibc/crypt_freesec.c,v 1.6 2010/02/20 14:45:06 solar Exp $
  49. * $Id: crypt.c,v 1.15 1994/09/13 04:58:49 davidb Exp $
  50. *
  51. * This is an original implementation of the DES and the crypt(3) interfaces
  52. * by David Burren. It has been heavily re-worked by Solar Designer.
  53. */
  54. #include <stdint.h>
  55. #include <string.h>
  56. struct expanded_key {
  57. uint32_t l[16], r[16];
  58. };
  59. #define _PASSWORD_EFMT1 '_'
  60. static const unsigned char key_shifts[16] = {
  61. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  62. };
  63. static const uint32_t psbox[8][64] = {
  64. {
  65. 0x00808200,0x00000000,0x00008000,0x00808202,
  66. 0x00808002,0x00008202,0x00000002,0x00008000,
  67. 0x00000200,0x00808200,0x00808202,0x00000200,
  68. 0x00800202,0x00808002,0x00800000,0x00000002,
  69. 0x00000202,0x00800200,0x00800200,0x00008200,
  70. 0x00008200,0x00808000,0x00808000,0x00800202,
  71. 0x00008002,0x00800002,0x00800002,0x00008002,
  72. 0x00000000,0x00000202,0x00008202,0x00800000,
  73. 0x00008000,0x00808202,0x00000002,0x00808000,
  74. 0x00808200,0x00800000,0x00800000,0x00000200,
  75. 0x00808002,0x00008000,0x00008200,0x00800002,
  76. 0x00000200,0x00000002,0x00800202,0x00008202,
  77. 0x00808202,0x00008002,0x00808000,0x00800202,
  78. 0x00800002,0x00000202,0x00008202,0x00808200,
  79. 0x00000202,0x00800200,0x00800200,0x00000000,
  80. 0x00008002,0x00008200,0x00000000,0x00808002,
  81. },{
  82. 0x40084010,0x40004000,0x00004000,0x00084010,
  83. 0x00080000,0x00000010,0x40080010,0x40004010,
  84. 0x40000010,0x40084010,0x40084000,0x40000000,
  85. 0x40004000,0x00080000,0x00000010,0x40080010,
  86. 0x00084000,0x00080010,0x40004010,0x00000000,
  87. 0x40000000,0x00004000,0x00084010,0x40080000,
  88. 0x00080010,0x40000010,0x00000000,0x00084000,
  89. 0x00004010,0x40084000,0x40080000,0x00004010,
  90. 0x00000000,0x00084010,0x40080010,0x00080000,
  91. 0x40004010,0x40080000,0x40084000,0x00004000,
  92. 0x40080000,0x40004000,0x00000010,0x40084010,
  93. 0x00084010,0x00000010,0x00004000,0x40000000,
  94. 0x00004010,0x40084000,0x00080000,0x40000010,
  95. 0x00080010,0x40004010,0x40000010,0x00080010,
  96. 0x00084000,0x00000000,0x40004000,0x00004010,
  97. 0x40000000,0x40080010,0x40084010,0x00084000,
  98. },{
  99. 0x00000104,0x04010100,0x00000000,0x04010004,
  100. 0x04000100,0x00000000,0x00010104,0x04000100,
  101. 0x00010004,0x04000004,0x04000004,0x00010000,
  102. 0x04010104,0x00010004,0x04010000,0x00000104,
  103. 0x04000000,0x00000004,0x04010100,0x00000100,
  104. 0x00010100,0x04010000,0x04010004,0x00010104,
  105. 0x04000104,0x00010100,0x00010000,0x04000104,
  106. 0x00000004,0x04010104,0x00000100,0x04000000,
  107. 0x04010100,0x04000000,0x00010004,0x00000104,
  108. 0x00010000,0x04010100,0x04000100,0x00000000,
  109. 0x00000100,0x00010004,0x04010104,0x04000100,
  110. 0x04000004,0x00000100,0x00000000,0x04010004,
  111. 0x04000104,0x00010000,0x04000000,0x04010104,
  112. 0x00000004,0x00010104,0x00010100,0x04000004,
  113. 0x04010000,0x04000104,0x00000104,0x04010000,
  114. 0x00010104,0x00000004,0x04010004,0x00010100,
  115. },{
  116. 0x80401000,0x80001040,0x80001040,0x00000040,
  117. 0x00401040,0x80400040,0x80400000,0x80001000,
  118. 0x00000000,0x00401000,0x00401000,0x80401040,
  119. 0x80000040,0x00000000,0x00400040,0x80400000,
  120. 0x80000000,0x00001000,0x00400000,0x80401000,
  121. 0x00000040,0x00400000,0x80001000,0x00001040,
  122. 0x80400040,0x80000000,0x00001040,0x00400040,
  123. 0x00001000,0x00401040,0x80401040,0x80000040,
  124. 0x00400040,0x80400000,0x00401000,0x80401040,
  125. 0x80000040,0x00000000,0x00000000,0x00401000,
  126. 0x00001040,0x00400040,0x80400040,0x80000000,
  127. 0x80401000,0x80001040,0x80001040,0x00000040,
  128. 0x80401040,0x80000040,0x80000000,0x00001000,
  129. 0x80400000,0x80001000,0x00401040,0x80400040,
  130. 0x80001000,0x00001040,0x00400000,0x80401000,
  131. 0x00000040,0x00400000,0x00001000,0x00401040,
  132. },{
  133. 0x00000080,0x01040080,0x01040000,0x21000080,
  134. 0x00040000,0x00000080,0x20000000,0x01040000,
  135. 0x20040080,0x00040000,0x01000080,0x20040080,
  136. 0x21000080,0x21040000,0x00040080,0x20000000,
  137. 0x01000000,0x20040000,0x20040000,0x00000000,
  138. 0x20000080,0x21040080,0x21040080,0x01000080,
  139. 0x21040000,0x20000080,0x00000000,0x21000000,
  140. 0x01040080,0x01000000,0x21000000,0x00040080,
  141. 0x00040000,0x21000080,0x00000080,0x01000000,
  142. 0x20000000,0x01040000,0x21000080,0x20040080,
  143. 0x01000080,0x20000000,0x21040000,0x01040080,
  144. 0x20040080,0x00000080,0x01000000,0x21040000,
  145. 0x21040080,0x00040080,0x21000000,0x21040080,
  146. 0x01040000,0x00000000,0x20040000,0x21000000,
  147. 0x00040080,0x01000080,0x20000080,0x00040000,
  148. 0x00000000,0x20040000,0x01040080,0x20000080,
  149. },{
  150. 0x10000008,0x10200000,0x00002000,0x10202008,
  151. 0x10200000,0x00000008,0x10202008,0x00200000,
  152. 0x10002000,0x00202008,0x00200000,0x10000008,
  153. 0x00200008,0x10002000,0x10000000,0x00002008,
  154. 0x00000000,0x00200008,0x10002008,0x00002000,
  155. 0x00202000,0x10002008,0x00000008,0x10200008,
  156. 0x10200008,0x00000000,0x00202008,0x10202000,
  157. 0x00002008,0x00202000,0x10202000,0x10000000,
  158. 0x10002000,0x00000008,0x10200008,0x00202000,
  159. 0x10202008,0x00200000,0x00002008,0x10000008,
  160. 0x00200000,0x10002000,0x10000000,0x00002008,
  161. 0x10000008,0x10202008,0x00202000,0x10200000,
  162. 0x00202008,0x10202000,0x00000000,0x10200008,
  163. 0x00000008,0x00002000,0x10200000,0x00202008,
  164. 0x00002000,0x00200008,0x10002008,0x00000000,
  165. 0x10202000,0x10000000,0x00200008,0x10002008,
  166. },{
  167. 0x00100000,0x02100001,0x02000401,0x00000000,
  168. 0x00000400,0x02000401,0x00100401,0x02100400,
  169. 0x02100401,0x00100000,0x00000000,0x02000001,
  170. 0x00000001,0x02000000,0x02100001,0x00000401,
  171. 0x02000400,0x00100401,0x00100001,0x02000400,
  172. 0x02000001,0x02100000,0x02100400,0x00100001,
  173. 0x02100000,0x00000400,0x00000401,0x02100401,
  174. 0x00100400,0x00000001,0x02000000,0x00100400,
  175. 0x02000000,0x00100400,0x00100000,0x02000401,
  176. 0x02000401,0x02100001,0x02100001,0x00000001,
  177. 0x00100001,0x02000000,0x02000400,0x00100000,
  178. 0x02100400,0x00000401,0x00100401,0x02100400,
  179. 0x00000401,0x02000001,0x02100401,0x02100000,
  180. 0x00100400,0x00000000,0x00000001,0x02100401,
  181. 0x00000000,0x00100401,0x02100000,0x00000400,
  182. 0x02000001,0x02000400,0x00000400,0x00100001,
  183. },{
  184. 0x08000820,0x00000800,0x00020000,0x08020820,
  185. 0x08000000,0x08000820,0x00000020,0x08000000,
  186. 0x00020020,0x08020000,0x08020820,0x00020800,
  187. 0x08020800,0x00020820,0x00000800,0x00000020,
  188. 0x08020000,0x08000020,0x08000800,0x00000820,
  189. 0x00020800,0x00020020,0x08020020,0x08020800,
  190. 0x00000820,0x00000000,0x00000000,0x08020020,
  191. 0x08000020,0x08000800,0x00020820,0x00020000,
  192. 0x00020820,0x00020000,0x08020800,0x00000800,
  193. 0x00000020,0x08020020,0x00000800,0x00020820,
  194. 0x08000800,0x00000020,0x08000020,0x08020000,
  195. 0x08020020,0x08000000,0x00020000,0x08000820,
  196. 0x00000000,0x08020820,0x00020020,0x08000020,
  197. 0x08020000,0x08000800,0x08000820,0x00000000,
  198. 0x08020820,0x00020800,0x00020800,0x00000820,
  199. 0x00000820,0x00020020,0x08000000,0x08020800,
  200. },
  201. };
  202. static const uint32_t ip_maskl[16][16] = {
  203. {
  204. 0x00000000,0x00010000,0x00000000,0x00010000,
  205. 0x01000000,0x01010000,0x01000000,0x01010000,
  206. 0x00000000,0x00010000,0x00000000,0x00010000,
  207. 0x01000000,0x01010000,0x01000000,0x01010000,
  208. },{
  209. 0x00000000,0x00000001,0x00000000,0x00000001,
  210. 0x00000100,0x00000101,0x00000100,0x00000101,
  211. 0x00000000,0x00000001,0x00000000,0x00000001,
  212. 0x00000100,0x00000101,0x00000100,0x00000101,
  213. },{
  214. 0x00000000,0x00020000,0x00000000,0x00020000,
  215. 0x02000000,0x02020000,0x02000000,0x02020000,
  216. 0x00000000,0x00020000,0x00000000,0x00020000,
  217. 0x02000000,0x02020000,0x02000000,0x02020000,
  218. },{
  219. 0x00000000,0x00000002,0x00000000,0x00000002,
  220. 0x00000200,0x00000202,0x00000200,0x00000202,
  221. 0x00000000,0x00000002,0x00000000,0x00000002,
  222. 0x00000200,0x00000202,0x00000200,0x00000202,
  223. },{
  224. 0x00000000,0x00040000,0x00000000,0x00040000,
  225. 0x04000000,0x04040000,0x04000000,0x04040000,
  226. 0x00000000,0x00040000,0x00000000,0x00040000,
  227. 0x04000000,0x04040000,0x04000000,0x04040000,
  228. },{
  229. 0x00000000,0x00000004,0x00000000,0x00000004,
  230. 0x00000400,0x00000404,0x00000400,0x00000404,
  231. 0x00000000,0x00000004,0x00000000,0x00000004,
  232. 0x00000400,0x00000404,0x00000400,0x00000404,
  233. },{
  234. 0x00000000,0x00080000,0x00000000,0x00080000,
  235. 0x08000000,0x08080000,0x08000000,0x08080000,
  236. 0x00000000,0x00080000,0x00000000,0x00080000,
  237. 0x08000000,0x08080000,0x08000000,0x08080000,
  238. },{
  239. 0x00000000,0x00000008,0x00000000,0x00000008,
  240. 0x00000800,0x00000808,0x00000800,0x00000808,
  241. 0x00000000,0x00000008,0x00000000,0x00000008,
  242. 0x00000800,0x00000808,0x00000800,0x00000808,
  243. },{
  244. 0x00000000,0x00100000,0x00000000,0x00100000,
  245. 0x10000000,0x10100000,0x10000000,0x10100000,
  246. 0x00000000,0x00100000,0x00000000,0x00100000,
  247. 0x10000000,0x10100000,0x10000000,0x10100000,
  248. },{
  249. 0x00000000,0x00000010,0x00000000,0x00000010,
  250. 0x00001000,0x00001010,0x00001000,0x00001010,
  251. 0x00000000,0x00000010,0x00000000,0x00000010,
  252. 0x00001000,0x00001010,0x00001000,0x00001010,
  253. },{
  254. 0x00000000,0x00200000,0x00000000,0x00200000,
  255. 0x20000000,0x20200000,0x20000000,0x20200000,
  256. 0x00000000,0x00200000,0x00000000,0x00200000,
  257. 0x20000000,0x20200000,0x20000000,0x20200000,
  258. },{
  259. 0x00000000,0x00000020,0x00000000,0x00000020,
  260. 0x00002000,0x00002020,0x00002000,0x00002020,
  261. 0x00000000,0x00000020,0x00000000,0x00000020,
  262. 0x00002000,0x00002020,0x00002000,0x00002020,
  263. },{
  264. 0x00000000,0x00400000,0x00000000,0x00400000,
  265. 0x40000000,0x40400000,0x40000000,0x40400000,
  266. 0x00000000,0x00400000,0x00000000,0x00400000,
  267. 0x40000000,0x40400000,0x40000000,0x40400000,
  268. },{
  269. 0x00000000,0x00000040,0x00000000,0x00000040,
  270. 0x00004000,0x00004040,0x00004000,0x00004040,
  271. 0x00000000,0x00000040,0x00000000,0x00000040,
  272. 0x00004000,0x00004040,0x00004000,0x00004040,
  273. },{
  274. 0x00000000,0x00800000,0x00000000,0x00800000,
  275. 0x80000000,0x80800000,0x80000000,0x80800000,
  276. 0x00000000,0x00800000,0x00000000,0x00800000,
  277. 0x80000000,0x80800000,0x80000000,0x80800000,
  278. },{
  279. 0x00000000,0x00000080,0x00000000,0x00000080,
  280. 0x00008000,0x00008080,0x00008000,0x00008080,
  281. 0x00000000,0x00000080,0x00000000,0x00000080,
  282. 0x00008000,0x00008080,0x00008000,0x00008080,
  283. },
  284. };
  285. static const uint32_t ip_maskr[16][16] = {
  286. {
  287. 0x00000000,0x00000000,0x00010000,0x00010000,
  288. 0x00000000,0x00000000,0x00010000,0x00010000,
  289. 0x01000000,0x01000000,0x01010000,0x01010000,
  290. 0x01000000,0x01000000,0x01010000,0x01010000,
  291. },{
  292. 0x00000000,0x00000000,0x00000001,0x00000001,
  293. 0x00000000,0x00000000,0x00000001,0x00000001,
  294. 0x00000100,0x00000100,0x00000101,0x00000101,
  295. 0x00000100,0x00000100,0x00000101,0x00000101,
  296. },{
  297. 0x00000000,0x00000000,0x00020000,0x00020000,
  298. 0x00000000,0x00000000,0x00020000,0x00020000,
  299. 0x02000000,0x02000000,0x02020000,0x02020000,
  300. 0x02000000,0x02000000,0x02020000,0x02020000,
  301. },{
  302. 0x00000000,0x00000000,0x00000002,0x00000002,
  303. 0x00000000,0x00000000,0x00000002,0x00000002,
  304. 0x00000200,0x00000200,0x00000202,0x00000202,
  305. 0x00000200,0x00000200,0x00000202,0x00000202,
  306. },{
  307. 0x00000000,0x00000000,0x00040000,0x00040000,
  308. 0x00000000,0x00000000,0x00040000,0x00040000,
  309. 0x04000000,0x04000000,0x04040000,0x04040000,
  310. 0x04000000,0x04000000,0x04040000,0x04040000,
  311. },{
  312. 0x00000000,0x00000000,0x00000004,0x00000004,
  313. 0x00000000,0x00000000,0x00000004,0x00000004,
  314. 0x00000400,0x00000400,0x00000404,0x00000404,
  315. 0x00000400,0x00000400,0x00000404,0x00000404,
  316. },{
  317. 0x00000000,0x00000000,0x00080000,0x00080000,
  318. 0x00000000,0x00000000,0x00080000,0x00080000,
  319. 0x08000000,0x08000000,0x08080000,0x08080000,
  320. 0x08000000,0x08000000,0x08080000,0x08080000,
  321. },{
  322. 0x00000000,0x00000000,0x00000008,0x00000008,
  323. 0x00000000,0x00000000,0x00000008,0x00000008,
  324. 0x00000800,0x00000800,0x00000808,0x00000808,
  325. 0x00000800,0x00000800,0x00000808,0x00000808,
  326. },{
  327. 0x00000000,0x00000000,0x00100000,0x00100000,
  328. 0x00000000,0x00000000,0x00100000,0x00100000,
  329. 0x10000000,0x10000000,0x10100000,0x10100000,
  330. 0x10000000,0x10000000,0x10100000,0x10100000,
  331. },{
  332. 0x00000000,0x00000000,0x00000010,0x00000010,
  333. 0x00000000,0x00000000,0x00000010,0x00000010,
  334. 0x00001000,0x00001000,0x00001010,0x00001010,
  335. 0x00001000,0x00001000,0x00001010,0x00001010,
  336. },{
  337. 0x00000000,0x00000000,0x00200000,0x00200000,
  338. 0x00000000,0x00000000,0x00200000,0x00200000,
  339. 0x20000000,0x20000000,0x20200000,0x20200000,
  340. 0x20000000,0x20000000,0x20200000,0x20200000,
  341. },{
  342. 0x00000000,0x00000000,0x00000020,0x00000020,
  343. 0x00000000,0x00000000,0x00000020,0x00000020,
  344. 0x00002000,0x00002000,0x00002020,0x00002020,
  345. 0x00002000,0x00002000,0x00002020,0x00002020,
  346. },{
  347. 0x00000000,0x00000000,0x00400000,0x00400000,
  348. 0x00000000,0x00000000,0x00400000,0x00400000,
  349. 0x40000000,0x40000000,0x40400000,0x40400000,
  350. 0x40000000,0x40000000,0x40400000,0x40400000,
  351. },{
  352. 0x00000000,0x00000000,0x00000040,0x00000040,
  353. 0x00000000,0x00000000,0x00000040,0x00000040,
  354. 0x00004000,0x00004000,0x00004040,0x00004040,
  355. 0x00004000,0x00004000,0x00004040,0x00004040,
  356. },{
  357. 0x00000000,0x00000000,0x00800000,0x00800000,
  358. 0x00000000,0x00000000,0x00800000,0x00800000,
  359. 0x80000000,0x80000000,0x80800000,0x80800000,
  360. 0x80000000,0x80000000,0x80800000,0x80800000,
  361. },{
  362. 0x00000000,0x00000000,0x00000080,0x00000080,
  363. 0x00000000,0x00000000,0x00000080,0x00000080,
  364. 0x00008000,0x00008000,0x00008080,0x00008080,
  365. 0x00008000,0x00008000,0x00008080,0x00008080,
  366. },
  367. };
  368. static const uint32_t fp_maskl[8][16] = {
  369. {
  370. 0x00000000,0x40000000,0x00400000,0x40400000,
  371. 0x00004000,0x40004000,0x00404000,0x40404000,
  372. 0x00000040,0x40000040,0x00400040,0x40400040,
  373. 0x00004040,0x40004040,0x00404040,0x40404040,
  374. },{
  375. 0x00000000,0x10000000,0x00100000,0x10100000,
  376. 0x00001000,0x10001000,0x00101000,0x10101000,
  377. 0x00000010,0x10000010,0x00100010,0x10100010,
  378. 0x00001010,0x10001010,0x00101010,0x10101010,
  379. },{
  380. 0x00000000,0x04000000,0x00040000,0x04040000,
  381. 0x00000400,0x04000400,0x00040400,0x04040400,
  382. 0x00000004,0x04000004,0x00040004,0x04040004,
  383. 0x00000404,0x04000404,0x00040404,0x04040404,
  384. },{
  385. 0x00000000,0x01000000,0x00010000,0x01010000,
  386. 0x00000100,0x01000100,0x00010100,0x01010100,
  387. 0x00000001,0x01000001,0x00010001,0x01010001,
  388. 0x00000101,0x01000101,0x00010101,0x01010101,
  389. },{
  390. 0x00000000,0x80000000,0x00800000,0x80800000,
  391. 0x00008000,0x80008000,0x00808000,0x80808000,
  392. 0x00000080,0x80000080,0x00800080,0x80800080,
  393. 0x00008080,0x80008080,0x00808080,0x80808080,
  394. },{
  395. 0x00000000,0x20000000,0x00200000,0x20200000,
  396. 0x00002000,0x20002000,0x00202000,0x20202000,
  397. 0x00000020,0x20000020,0x00200020,0x20200020,
  398. 0x00002020,0x20002020,0x00202020,0x20202020,
  399. },{
  400. 0x00000000,0x08000000,0x00080000,0x08080000,
  401. 0x00000800,0x08000800,0x00080800,0x08080800,
  402. 0x00000008,0x08000008,0x00080008,0x08080008,
  403. 0x00000808,0x08000808,0x00080808,0x08080808,
  404. },{
  405. 0x00000000,0x02000000,0x00020000,0x02020000,
  406. 0x00000200,0x02000200,0x00020200,0x02020200,
  407. 0x00000002,0x02000002,0x00020002,0x02020002,
  408. 0x00000202,0x02000202,0x00020202,0x02020202,
  409. },
  410. };
  411. static const uint32_t fp_maskr[8][16] = {
  412. {
  413. 0x00000000,0x40000000,0x00400000,0x40400000,
  414. 0x00004000,0x40004000,0x00404000,0x40404000,
  415. 0x00000040,0x40000040,0x00400040,0x40400040,
  416. 0x00004040,0x40004040,0x00404040,0x40404040,
  417. },{
  418. 0x00000000,0x10000000,0x00100000,0x10100000,
  419. 0x00001000,0x10001000,0x00101000,0x10101000,
  420. 0x00000010,0x10000010,0x00100010,0x10100010,
  421. 0x00001010,0x10001010,0x00101010,0x10101010,
  422. },{
  423. 0x00000000,0x04000000,0x00040000,0x04040000,
  424. 0x00000400,0x04000400,0x00040400,0x04040400,
  425. 0x00000004,0x04000004,0x00040004,0x04040004,
  426. 0x00000404,0x04000404,0x00040404,0x04040404,
  427. },{
  428. 0x00000000,0x01000000,0x00010000,0x01010000,
  429. 0x00000100,0x01000100,0x00010100,0x01010100,
  430. 0x00000001,0x01000001,0x00010001,0x01010001,
  431. 0x00000101,0x01000101,0x00010101,0x01010101,
  432. },{
  433. 0x00000000,0x80000000,0x00800000,0x80800000,
  434. 0x00008000,0x80008000,0x00808000,0x80808000,
  435. 0x00000080,0x80000080,0x00800080,0x80800080,
  436. 0x00008080,0x80008080,0x00808080,0x80808080,
  437. },{
  438. 0x00000000,0x20000000,0x00200000,0x20200000,
  439. 0x00002000,0x20002000,0x00202000,0x20202000,
  440. 0x00000020,0x20000020,0x00200020,0x20200020,
  441. 0x00002020,0x20002020,0x00202020,0x20202020,
  442. },{
  443. 0x00000000,0x08000000,0x00080000,0x08080000,
  444. 0x00000800,0x08000800,0x00080800,0x08080800,
  445. 0x00000008,0x08000008,0x00080008,0x08080008,
  446. 0x00000808,0x08000808,0x00080808,0x08080808,
  447. },{
  448. 0x00000000,0x02000000,0x00020000,0x02020000,
  449. 0x00000200,0x02000200,0x00020200,0x02020200,
  450. 0x00000002,0x02000002,0x00020002,0x02020002,
  451. 0x00000202,0x02000202,0x00020202,0x02020202,
  452. },
  453. };
  454. static const uint32_t key_perm_maskl[8][16] = {
  455. {
  456. 0x00000000,0x00000000,0x00000010,0x00000010,
  457. 0x00001000,0x00001000,0x00001010,0x00001010,
  458. 0x00100000,0x00100000,0x00100010,0x00100010,
  459. 0x00101000,0x00101000,0x00101010,0x00101010,
  460. },{
  461. 0x00000000,0x00000000,0x00000020,0x00000020,
  462. 0x00002000,0x00002000,0x00002020,0x00002020,
  463. 0x00200000,0x00200000,0x00200020,0x00200020,
  464. 0x00202000,0x00202000,0x00202020,0x00202020,
  465. },{
  466. 0x00000000,0x00000000,0x00000040,0x00000040,
  467. 0x00004000,0x00004000,0x00004040,0x00004040,
  468. 0x00400000,0x00400000,0x00400040,0x00400040,
  469. 0x00404000,0x00404000,0x00404040,0x00404040,
  470. },{
  471. 0x00000000,0x00000000,0x00000080,0x00000080,
  472. 0x00008000,0x00008000,0x00008080,0x00008080,
  473. 0x00800000,0x00800000,0x00800080,0x00800080,
  474. 0x00808000,0x00808000,0x00808080,0x00808080,
  475. },{
  476. 0x00000000,0x00000001,0x00000100,0x00000101,
  477. 0x00010000,0x00010001,0x00010100,0x00010101,
  478. 0x01000000,0x01000001,0x01000100,0x01000101,
  479. 0x01010000,0x01010001,0x01010100,0x01010101,
  480. },{
  481. 0x00000000,0x00000002,0x00000200,0x00000202,
  482. 0x00020000,0x00020002,0x00020200,0x00020202,
  483. 0x02000000,0x02000002,0x02000200,0x02000202,
  484. 0x02020000,0x02020002,0x02020200,0x02020202,
  485. },{
  486. 0x00000000,0x00000004,0x00000400,0x00000404,
  487. 0x00040000,0x00040004,0x00040400,0x00040404,
  488. 0x04000000,0x04000004,0x04000400,0x04000404,
  489. 0x04040000,0x04040004,0x04040400,0x04040404,
  490. },{
  491. 0x00000000,0x00000008,0x00000800,0x00000808,
  492. 0x00080000,0x00080008,0x00080800,0x00080808,
  493. 0x08000000,0x08000008,0x08000800,0x08000808,
  494. 0x08080000,0x08080008,0x08080800,0x08080808,
  495. },
  496. };
  497. static const uint32_t key_perm_maskr[12][16] = {
  498. {
  499. 0x00000000,0x00000001,0x00000000,0x00000001,
  500. 0x00000000,0x00000001,0x00000000,0x00000001,
  501. 0x00000000,0x00000001,0x00000000,0x00000001,
  502. 0x00000000,0x00000001,0x00000000,0x00000001,
  503. },{
  504. 0x00000000,0x00000000,0x00100000,0x00100000,
  505. 0x00001000,0x00001000,0x00101000,0x00101000,
  506. 0x00000010,0x00000010,0x00100010,0x00100010,
  507. 0x00001010,0x00001010,0x00101010,0x00101010,
  508. },{
  509. 0x00000000,0x00000002,0x00000000,0x00000002,
  510. 0x00000000,0x00000002,0x00000000,0x00000002,
  511. 0x00000000,0x00000002,0x00000000,0x00000002,
  512. 0x00000000,0x00000002,0x00000000,0x00000002,
  513. },{
  514. 0x00000000,0x00000000,0x00200000,0x00200000,
  515. 0x00002000,0x00002000,0x00202000,0x00202000,
  516. 0x00000020,0x00000020,0x00200020,0x00200020,
  517. 0x00002020,0x00002020,0x00202020,0x00202020,
  518. },{
  519. 0x00000000,0x00000004,0x00000000,0x00000004,
  520. 0x00000000,0x00000004,0x00000000,0x00000004,
  521. 0x00000000,0x00000004,0x00000000,0x00000004,
  522. 0x00000000,0x00000004,0x00000000,0x00000004,
  523. },{
  524. 0x00000000,0x00000000,0x00400000,0x00400000,
  525. 0x00004000,0x00004000,0x00404000,0x00404000,
  526. 0x00000040,0x00000040,0x00400040,0x00400040,
  527. 0x00004040,0x00004040,0x00404040,0x00404040,
  528. },{
  529. 0x00000000,0x00000008,0x00000000,0x00000008,
  530. 0x00000000,0x00000008,0x00000000,0x00000008,
  531. 0x00000000,0x00000008,0x00000000,0x00000008,
  532. 0x00000000,0x00000008,0x00000000,0x00000008,
  533. },{
  534. 0x00000000,0x00000000,0x00800000,0x00800000,
  535. 0x00008000,0x00008000,0x00808000,0x00808000,
  536. 0x00000080,0x00000080,0x00800080,0x00800080,
  537. 0x00008080,0x00008080,0x00808080,0x00808080,
  538. },{
  539. 0x00000000,0x00000000,0x01000000,0x01000000,
  540. 0x00010000,0x00010000,0x01010000,0x01010000,
  541. 0x00000100,0x00000100,0x01000100,0x01000100,
  542. 0x00010100,0x00010100,0x01010100,0x01010100,
  543. },{
  544. 0x00000000,0x00000000,0x02000000,0x02000000,
  545. 0x00020000,0x00020000,0x02020000,0x02020000,
  546. 0x00000200,0x00000200,0x02000200,0x02000200,
  547. 0x00020200,0x00020200,0x02020200,0x02020200,
  548. },{
  549. 0x00000000,0x00000000,0x04000000,0x04000000,
  550. 0x00040000,0x00040000,0x04040000,0x04040000,
  551. 0x00000400,0x00000400,0x04000400,0x04000400,
  552. 0x00040400,0x00040400,0x04040400,0x04040400,
  553. },{
  554. 0x00000000,0x00000000,0x08000000,0x08000000,
  555. 0x00080000,0x00080000,0x08080000,0x08080000,
  556. 0x00000800,0x00000800,0x08000800,0x08000800,
  557. 0x00080800,0x00080800,0x08080800,0x08080800,
  558. },
  559. };
  560. static const uint32_t comp_maskl0[4][8] = {
  561. {
  562. 0x00000000,0x00020000,0x00000001,0x00020001,
  563. 0x00080000,0x000a0000,0x00080001,0x000a0001,
  564. },{
  565. 0x00000000,0x00001000,0x00000000,0x00001000,
  566. 0x00000040,0x00001040,0x00000040,0x00001040,
  567. },{
  568. 0x00000000,0x00400000,0x00000020,0x00400020,
  569. 0x00008000,0x00408000,0x00008020,0x00408020,
  570. },{
  571. 0x00000000,0x00100000,0x00000800,0x00100800,
  572. 0x00000000,0x00100000,0x00000800,0x00100800,
  573. },
  574. };
  575. static const uint32_t comp_maskr0[4][8] = {
  576. {
  577. 0x00000000,0x00200000,0x00020000,0x00220000,
  578. 0x00000002,0x00200002,0x00020002,0x00220002,
  579. },{
  580. 0x00000000,0x00000000,0x00100000,0x00100000,
  581. 0x00000004,0x00000004,0x00100004,0x00100004,
  582. },{
  583. 0x00000000,0x00004000,0x00000800,0x00004800,
  584. 0x00000000,0x00004000,0x00000800,0x00004800,
  585. },{
  586. 0x00000000,0x00400000,0x00008000,0x00408000,
  587. 0x00000008,0x00400008,0x00008008,0x00408008,
  588. },
  589. };
  590. static const uint32_t comp_maskl1[4][16] = {
  591. {
  592. 0x00000000,0x00000010,0x00004000,0x00004010,
  593. 0x00040000,0x00040010,0x00044000,0x00044010,
  594. 0x00000100,0x00000110,0x00004100,0x00004110,
  595. 0x00040100,0x00040110,0x00044100,0x00044110,
  596. },{
  597. 0x00000000,0x00800000,0x00000002,0x00800002,
  598. 0x00000200,0x00800200,0x00000202,0x00800202,
  599. 0x00200000,0x00a00000,0x00200002,0x00a00002,
  600. 0x00200200,0x00a00200,0x00200202,0x00a00202,
  601. },{
  602. 0x00000000,0x00002000,0x00000004,0x00002004,
  603. 0x00000400,0x00002400,0x00000404,0x00002404,
  604. 0x00000000,0x00002000,0x00000004,0x00002004,
  605. 0x00000400,0x00002400,0x00000404,0x00002404,
  606. },{
  607. 0x00000000,0x00010000,0x00000008,0x00010008,
  608. 0x00000080,0x00010080,0x00000088,0x00010088,
  609. 0x00000000,0x00010000,0x00000008,0x00010008,
  610. 0x00000080,0x00010080,0x00000088,0x00010088,
  611. },
  612. };
  613. static const uint32_t comp_maskr1[4][16] = {
  614. {
  615. 0x00000000,0x00000000,0x00000080,0x00000080,
  616. 0x00002000,0x00002000,0x00002080,0x00002080,
  617. 0x00000001,0x00000001,0x00000081,0x00000081,
  618. 0x00002001,0x00002001,0x00002081,0x00002081,
  619. },{
  620. 0x00000000,0x00000010,0x00800000,0x00800010,
  621. 0x00010000,0x00010010,0x00810000,0x00810010,
  622. 0x00000200,0x00000210,0x00800200,0x00800210,
  623. 0x00010200,0x00010210,0x00810200,0x00810210,
  624. },{
  625. 0x00000000,0x00000400,0x00001000,0x00001400,
  626. 0x00080000,0x00080400,0x00081000,0x00081400,
  627. 0x00000020,0x00000420,0x00001020,0x00001420,
  628. 0x00080020,0x00080420,0x00081020,0x00081420,
  629. },{
  630. 0x00000000,0x00000100,0x00040000,0x00040100,
  631. 0x00000000,0x00000100,0x00040000,0x00040100,
  632. 0x00000040,0x00000140,0x00040040,0x00040140,
  633. 0x00000040,0x00000140,0x00040040,0x00040140,
  634. },
  635. };
  636. static const unsigned char ascii64[] =
  637. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  638. /* 0000000000111111111122222222223333333333444444444455555555556666 */
  639. /* 0123456789012345678901234567890123456789012345678901234567890123 */
  640. /*
  641. * We match the behavior of UFC-crypt on systems where "char" is signed by
  642. * default (the majority), regardless of char's signedness on our system.
  643. */
  644. static uint32_t ascii_to_bin(int ch)
  645. {
  646. int sch = (ch < 0x80) ? ch : -(0x100 - ch);
  647. int retval;
  648. retval = sch - '.';
  649. if (sch >= 'A') {
  650. retval = sch - ('A' - 12);
  651. if (sch >= 'a')
  652. retval = sch - ('a' - 38);
  653. }
  654. retval &= 0x3f;
  655. return retval;
  656. }
  657. /*
  658. * When we choose to "support" invalid salts, nevertheless disallow those
  659. * containing characters that would violate the passwd file format.
  660. */
  661. static inline int ascii_is_unsafe(unsigned char ch)
  662. {
  663. return !ch || ch == '\n' || ch == ':';
  664. }
  665. static uint32_t setup_salt(uint32_t salt)
  666. {
  667. uint32_t obit, saltbit, saltbits;
  668. unsigned int i;
  669. saltbits = 0;
  670. saltbit = 1;
  671. obit = 0x800000;
  672. for (i = 0; i < 24; i++) {
  673. if (salt & saltbit)
  674. saltbits |= obit;
  675. saltbit <<= 1;
  676. obit >>= 1;
  677. }
  678. return saltbits;
  679. }
  680. void __des_setkey(const unsigned char *key, struct expanded_key *ekey)
  681. {
  682. uint32_t k0, k1, rawkey0, rawkey1;
  683. unsigned int shifts, round, i, ibit;
  684. rawkey0 =
  685. (uint32_t)key[3] |
  686. ((uint32_t)key[2] << 8) |
  687. ((uint32_t)key[1] << 16) |
  688. ((uint32_t)key[0] << 24);
  689. rawkey1 =
  690. (uint32_t)key[7] |
  691. ((uint32_t)key[6] << 8) |
  692. ((uint32_t)key[5] << 16) |
  693. ((uint32_t)key[4] << 24);
  694. /*
  695. * Do key permutation and split into two 28-bit subkeys.
  696. */
  697. k0 = k1 = 0;
  698. for (i = 0, ibit = 28; i < 4; i++, ibit -= 4) {
  699. unsigned int j = i << 1;
  700. k0 |= key_perm_maskl[i][(rawkey0 >> ibit) & 0xf] |
  701. key_perm_maskl[i + 4][(rawkey1 >> ibit) & 0xf];
  702. k1 |= key_perm_maskr[j][(rawkey0 >> ibit) & 0xf];
  703. ibit -= 4;
  704. k1 |= key_perm_maskr[j + 1][(rawkey0 >> ibit) & 0xf] |
  705. key_perm_maskr[i + 8][(rawkey1 >> ibit) & 0xf];
  706. }
  707. /*
  708. * Rotate subkeys and do compression permutation.
  709. */
  710. shifts = 0;
  711. for (round = 0; round < 16; round++) {
  712. uint32_t t0, t1;
  713. uint32_t kl, kr;
  714. shifts += key_shifts[round];
  715. t0 = (k0 << shifts) | (k0 >> (28 - shifts));
  716. t1 = (k1 << shifts) | (k1 >> (28 - shifts));
  717. kl = kr = 0;
  718. ibit = 25;
  719. for (i = 0; i < 4; i++) {
  720. kl |= comp_maskl0[i][(t0 >> ibit) & 7];
  721. kr |= comp_maskr0[i][(t1 >> ibit) & 7];
  722. ibit -= 4;
  723. kl |= comp_maskl1[i][(t0 >> ibit) & 0xf];
  724. kr |= comp_maskr1[i][(t1 >> ibit) & 0xf];
  725. ibit -= 3;
  726. }
  727. ekey->l[round] = kl;
  728. ekey->r[round] = kr;
  729. }
  730. }
  731. /*
  732. * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
  733. */
  734. void __do_des(uint32_t l_in, uint32_t r_in,
  735. uint32_t *l_out, uint32_t *r_out,
  736. uint32_t count, uint32_t saltbits, const struct expanded_key *ekey)
  737. {
  738. uint32_t l, r;
  739. /*
  740. * Do initial permutation (IP).
  741. */
  742. l = r = 0;
  743. if (l_in | r_in) {
  744. unsigned int i, ibit;
  745. for (i = 0, ibit = 28; i < 8; i++, ibit -= 4) {
  746. l |= ip_maskl[i][(l_in >> ibit) & 0xf] |
  747. ip_maskl[i + 8][(r_in >> ibit) & 0xf];
  748. r |= ip_maskr[i][(l_in >> ibit) & 0xf] |
  749. ip_maskr[i + 8][(r_in >> ibit) & 0xf];
  750. }
  751. }
  752. while (count--) {
  753. /*
  754. * Do each round.
  755. */
  756. unsigned int round = 16;
  757. const uint32_t *kl = ekey->l;
  758. const uint32_t *kr = ekey->r;
  759. uint32_t f;
  760. while (round--) {
  761. uint32_t r48l, r48r;
  762. /*
  763. * Expand R to 48 bits (simulate the E-box).
  764. */
  765. r48l = ((r & 0x00000001) << 23)
  766. | ((r & 0xf8000000) >> 9)
  767. | ((r & 0x1f800000) >> 11)
  768. | ((r & 0x01f80000) >> 13)
  769. | ((r & 0x001f8000) >> 15);
  770. r48r = ((r & 0x0001f800) << 7)
  771. | ((r & 0x00001f80) << 5)
  772. | ((r & 0x000001f8) << 3)
  773. | ((r & 0x0000001f) << 1)
  774. | ((r & 0x80000000) >> 31);
  775. /*
  776. * Do salting for crypt() and friends, and
  777. * XOR with the permuted key.
  778. */
  779. f = (r48l ^ r48r) & saltbits;
  780. r48l ^= f ^ *kl++;
  781. r48r ^= f ^ *kr++;
  782. /*
  783. * Do S-box lookups (which shrink it back to 32 bits)
  784. * and do the P-box permutation at the same time.
  785. */
  786. f = psbox[0][r48l >> 18]
  787. | psbox[1][(r48l >> 12) & 0x3f]
  788. | psbox[2][(r48l >> 6) & 0x3f]
  789. | psbox[3][r48l & 0x3f]
  790. | psbox[4][r48r >> 18]
  791. | psbox[5][(r48r >> 12) & 0x3f]
  792. | psbox[6][(r48r >> 6) & 0x3f]
  793. | psbox[7][r48r & 0x3f];
  794. /*
  795. * Now that we've permuted things, complete f().
  796. */
  797. f ^= l;
  798. l = r;
  799. r = f;
  800. }
  801. r = l;
  802. l = f;
  803. }
  804. /*
  805. * Do final permutation (inverse of IP).
  806. */
  807. {
  808. unsigned int i, ibit;
  809. uint32_t lo, ro;
  810. lo = ro = 0;
  811. for (i = 0, ibit = 28; i < 4; i++, ibit -= 4) {
  812. ro |= fp_maskr[i][(l >> ibit) & 0xf] |
  813. fp_maskr[i + 4][(r >> ibit) & 0xf];
  814. ibit -= 4;
  815. lo |= fp_maskl[i][(l >> ibit) & 0xf] |
  816. fp_maskl[i + 4][(r >> ibit) & 0xf];
  817. }
  818. *l_out = lo;
  819. *r_out = ro;
  820. }
  821. }
  822. static void des_cipher(const unsigned char *in, unsigned char *out,
  823. uint32_t count, uint32_t saltbits, const struct expanded_key *ekey)
  824. {
  825. uint32_t l_out, r_out, rawl, rawr;
  826. rawl =
  827. (uint32_t)in[3] |
  828. ((uint32_t)in[2] << 8) |
  829. ((uint32_t)in[1] << 16) |
  830. ((uint32_t)in[0] << 24);
  831. rawr =
  832. (uint32_t)in[7] |
  833. ((uint32_t)in[6] << 8) |
  834. ((uint32_t)in[5] << 16) |
  835. ((uint32_t)in[4] << 24);
  836. __do_des(rawl, rawr, &l_out, &r_out, count, saltbits, ekey);
  837. out[0] = l_out >> 24;
  838. out[1] = l_out >> 16;
  839. out[2] = l_out >> 8;
  840. out[3] = l_out;
  841. out[4] = r_out >> 24;
  842. out[5] = r_out >> 16;
  843. out[6] = r_out >> 8;
  844. out[7] = r_out;
  845. }
  846. static char *_crypt_extended_r_uut(const char *_key, const char *_setting, char *output)
  847. {
  848. const unsigned char *key = (const unsigned char *)_key;
  849. const unsigned char *setting = (const unsigned char *)_setting;
  850. struct expanded_key ekey;
  851. unsigned char keybuf[8];
  852. unsigned char *p, *q;
  853. uint32_t count, salt, l, r0, r1;
  854. unsigned int i;
  855. /*
  856. * Copy the key, shifting each character left by one bit and padding
  857. * with zeroes.
  858. */
  859. q = keybuf;
  860. while (q <= &keybuf[sizeof(keybuf) - 1]) {
  861. *q++ = *key << 1;
  862. if (*key)
  863. key++;
  864. }
  865. __des_setkey(keybuf, &ekey);
  866. if (*setting == _PASSWORD_EFMT1) {
  867. /*
  868. * "new"-style:
  869. * setting - underscore, 4 chars of count, 4 chars of salt
  870. * key - unlimited characters
  871. */
  872. for (i = 1, count = 0; i < 5; i++) {
  873. uint32_t value = ascii_to_bin(setting[i]);
  874. if (ascii64[value] != setting[i])
  875. return NULL;
  876. count |= value << (i - 1) * 6;
  877. }
  878. if (!count)
  879. return NULL;
  880. for (i = 5, salt = 0; i < 9; i++) {
  881. uint32_t value = ascii_to_bin(setting[i]);
  882. if (ascii64[value] != setting[i])
  883. return NULL;
  884. salt |= value << (i - 5) * 6;
  885. }
  886. while (*key) {
  887. /*
  888. * Encrypt the key with itself.
  889. */
  890. des_cipher(keybuf, keybuf, 1, 0, &ekey);
  891. /*
  892. * And XOR with the next 8 characters of the key.
  893. */
  894. q = keybuf;
  895. while (q <= &keybuf[sizeof(keybuf) - 1] && *key)
  896. *q++ ^= *key++ << 1;
  897. __des_setkey(keybuf, &ekey);
  898. }
  899. memcpy(output, setting, 9);
  900. output[9] = '\0';
  901. p = (unsigned char *)output + 9;
  902. } else {
  903. /*
  904. * "old"-style:
  905. * setting - 2 chars of salt
  906. * key - up to 8 characters
  907. */
  908. count = 25;
  909. if (ascii_is_unsafe(setting[0]) || ascii_is_unsafe(setting[1]))
  910. return NULL;
  911. salt = (ascii_to_bin(setting[1]) << 6)
  912. | ascii_to_bin(setting[0]);
  913. output[0] = setting[0];
  914. output[1] = setting[1];
  915. p = (unsigned char *)output + 2;
  916. }
  917. /*
  918. * Do it.
  919. */
  920. __do_des(0, 0, &r0, &r1, count, setup_salt(salt), &ekey);
  921. /*
  922. * Now encode the result...
  923. */
  924. l = (r0 >> 8);
  925. *p++ = ascii64[(l >> 18) & 0x3f];
  926. *p++ = ascii64[(l >> 12) & 0x3f];
  927. *p++ = ascii64[(l >> 6) & 0x3f];
  928. *p++ = ascii64[l & 0x3f];
  929. l = (r0 << 16) | ((r1 >> 16) & 0xffff);
  930. *p++ = ascii64[(l >> 18) & 0x3f];
  931. *p++ = ascii64[(l >> 12) & 0x3f];
  932. *p++ = ascii64[(l >> 6) & 0x3f];
  933. *p++ = ascii64[l & 0x3f];
  934. l = r1 << 2;
  935. *p++ = ascii64[(l >> 12) & 0x3f];
  936. *p++ = ascii64[(l >> 6) & 0x3f];
  937. *p++ = ascii64[l & 0x3f];
  938. *p = 0;
  939. return output;
  940. }
  941. char *__crypt_des(const char *key, const char *setting, char *output)
  942. {
  943. const char *test_key = "\x80\xff\x80\x01 "
  944. "\x7f\x81\x80\x80\x0d\x0a\xff\x7f \x81 test";
  945. const char *test_setting = "_0.../9Zz";
  946. const char *test_hash = "_0.../9ZzX7iSJNd21sU";
  947. char test_buf[21];
  948. char *retval;
  949. const char *p;
  950. if (*setting != _PASSWORD_EFMT1) {
  951. test_setting = "\x80x";
  952. test_hash = "\x80x22/wK52ZKGA";
  953. }
  954. /*
  955. * Hash the supplied password.
  956. */
  957. retval = _crypt_extended_r_uut(key, setting, output);
  958. /*
  959. * Perform a quick self-test. It is important that we make both calls
  960. * to _crypt_extended_r_uut() from the same scope such that they likely
  961. * use the same stack locations, which makes the second call overwrite
  962. * the first call's sensitive data on the stack and makes it more
  963. * likely that any alignment related issues would be detected.
  964. */
  965. p = _crypt_extended_r_uut(test_key, test_setting, test_buf);
  966. if (p && !strcmp(p, test_hash) && retval)
  967. return retval;
  968. return (setting[0]=='*') ? "x" : "*";
  969. }