crypt_des.c 33 KB

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