home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume28 / ufc-crypt / part02 < prev    next >
Internet Message Format  |  1992-03-19  |  51KB

  1. From: glad@daimi.aau.dk (Michael Glad)
  2. Newsgroups: comp.sources.misc
  3. Subject: v28i116: ufc-crypt - fast crypt/fcrypt routines, Part02/02
  4. Message-ID: <1992Mar18.144051.24256@aber.ac.uk>
  5. Date: 18 Mar 92 14:40:51 GMT
  6. Approved: aem@aber.ac.uk
  7. X-Md4-Signature: 3dc97c8443c2770033c683d270568e77
  8.  
  9. Submitted-by: glad@daimi.aau.dk (Michael Glad)
  10. Posting-number: Volume 28, Issue 116
  11. Archive-name: ufc-crypt/part02
  12. Environment: UNIX, 32BIT
  13. Supersedes: ufc-crypt: Volume 23, 97-98
  14.  
  15. #! /bin/sh
  16. # it by saving it into a file and typing "sh file".  To overwrite existing
  17. # files, type "sh file -c".  You can also feed this as standard input via
  18. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  19. # will see the following message at the end:
  20. #        "End of archive 2 (of 2)."
  21. # Contents:  COPYING crypt_util.c
  22. # Wrapped by glad@estella on Fri Mar  6 09:19:49 1992
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. if test -f 'COPYING' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'COPYING'\"
  26. else
  27. echo shar: Extracting \"'COPYING'\" \(25265 characters\)
  28. sed "s/^X//" >'COPYING' <<'END_OF_FILE'
  29. X          GNU LIBRARY GENERAL PUBLIC LICENSE
  30. X               Version 2, June 1991
  31. X
  32. X Copyright (C) 1991 Free Software Foundation, Inc.
  33. X                    675 Mass Ave, Cambridge, MA 02139, USA
  34. X Everyone is permitted to copy and distribute verbatim copies
  35. X of this license document, but changing it is not allowed.
  36. X
  37. X[This is the first released version of the library GPL.  It is
  38. X numbered 2 because it goes with version 2 of the ordinary GPL.]
  39. X
  40. X                Preamble
  41. X
  42. X  The licenses for most software are designed to take away your
  43. Xfreedom to share and change it.  By contrast, the GNU General Public
  44. XLicenses are intended to guarantee your freedom to share and change
  45. Xfree software--to make sure the software is free for all its users.
  46. X
  47. X  This license, the Library General Public License, applies to some
  48. Xspecially designated Free Software Foundation software, and to any
  49. Xother libraries whose authors decide to use it.  You can use it for
  50. Xyour libraries, too.
  51. X
  52. X  When we speak of free software, we are referring to freedom, not
  53. Xprice.  Our General Public Licenses are designed to make sure that you
  54. Xhave the freedom to distribute copies of free software (and charge for
  55. Xthis service if you wish), that you receive source code or can get it
  56. Xif you want it, that you can change the software or use pieces of it
  57. Xin new free programs; and that you know you can do these things.
  58. X
  59. X  To protect your rights, we need to make restrictions that forbid
  60. Xanyone to deny you these rights or to ask you to surrender the rights.
  61. XThese restrictions translate to certain responsibilities for you if
  62. Xyou distribute copies of the library, or if you modify it.
  63. X
  64. X  For example, if you distribute copies of the library, whether gratis
  65. Xor for a fee, you must give the recipients all the rights that we gave
  66. Xyou.  You must make sure that they, too, receive or can get the source
  67. Xcode.  If you link a program with the library, you must provide
  68. Xcomplete object files to the recipients so that they can relink them
  69. Xwith the library, after making changes to the library and recompiling
  70. Xit.  And you must show them these terms so they know their rights.
  71. X
  72. X  Our method of protecting your rights has two steps: (1) copyright
  73. Xthe library, and (2) offer you this license which gives you legal
  74. Xpermission to copy, distribute and/or modify the library.
  75. X
  76. X  Also, for each distributor's protection, we want to make certain
  77. Xthat everyone understands that there is no warranty for this free
  78. Xlibrary.  If the library is modified by someone else and passed on, we
  79. Xwant its recipients to know that what they have is not the original
  80. Xversion, so that any problems introduced by others will not reflect on
  81. Xthe original authors' reputations.
  82. X
  83. X  Finally, any free program is threatened constantly by software
  84. Xpatents.  We wish to avoid the danger that companies distributing free
  85. Xsoftware will individually obtain patent licenses, thus in effect
  86. Xtransforming the program into proprietary software.  To prevent this,
  87. Xwe have made it clear that any patent must be licensed for everyone's
  88. Xfree use or not licensed at all.
  89. X
  90. X  Most GNU software, including some libraries, is covered by the ordinary
  91. XGNU General Public License, which was designed for utility programs.  This
  92. Xlicense, the GNU Library General Public License, applies to certain
  93. Xdesignated libraries.  This license is quite different from the ordinary
  94. Xone; be sure to read it in full, and don't assume that anything in it is
  95. Xthe same as in the ordinary license.
  96. X
  97. X  The reason we have a separate public license for some libraries is that
  98. Xthey blur the distinction we usually make between modifying or adding to a
  99. Xprogram and simply using it.  Linking a program with a library, without
  100. Xchanging the library, is in some sense simply using the library, and is
  101. Xanalogous to running a utility program or application program.  However, in
  102. Xa textual and legal sense, the linked executable is a combined work, a
  103. Xderivative of the original library, and the ordinary General Public License
  104. Xtreats it as such.
  105. X
  106. X  Because of this blurred distinction, using the ordinary General
  107. XPublic License for libraries did not effectively promote software
  108. Xsharing, because most developers did not use the libraries.  We
  109. Xconcluded that weaker conditions might promote sharing better.
  110. X
  111. X  However, unrestricted linking of non-free programs would deprive the
  112. Xusers of those programs of all benefit from the free status of the
  113. Xlibraries themselves.  This Library General Public License is intended to
  114. Xpermit developers of non-free programs to use free libraries, while
  115. Xpreserving your freedom as a user of such programs to change the free
  116. Xlibraries that are incorporated in them.  (We have not seen how to achieve
  117. Xthis as regards changes in header files, but we have achieved it as regards
  118. Xchanges in the actual functions of the Library.)  The hope is that this
  119. Xwill lead to faster development of free libraries.
  120. X
  121. X  The precise terms and conditions for copying, distribution and
  122. Xmodification follow.  Pay close attention to the difference between a
  123. X"work based on the library" and a "work that uses the library".  The
  124. Xformer contains code derived from the library, while the latter only
  125. Xworks together with the library.
  126. X
  127. X  Note that it is possible for a library to be covered by the ordinary
  128. XGeneral Public License rather than by this special one.
  129. X
  130. X          GNU LIBRARY GENERAL PUBLIC LICENSE
  131. X   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  132. X
  133. X  0. This License Agreement applies to any software library which
  134. Xcontains a notice placed by the copyright holder or other authorized
  135. Xparty saying it may be distributed under the terms of this Library
  136. XGeneral Public License (also called "this License").  Each licensee is
  137. Xaddressed as "you".
  138. X
  139. X  A "library" means a collection of software functions and/or data
  140. Xprepared so as to be conveniently linked with application programs
  141. X(which use some of those functions and data) to form executables.
  142. X
  143. X  The "Library", below, refers to any such software library or work
  144. Xwhich has been distributed under these terms.  A "work based on the
  145. XLibrary" means either the Library or any derivative work under
  146. Xcopyright law: that is to say, a work containing the Library or a
  147. Xportion of it, either verbatim or with modifications and/or translated
  148. Xstraightforwardly into another language.  (Hereinafter, translation is
  149. Xincluded without limitation in the term "modification".)
  150. X
  151. X  "Source code" for a work means the preferred form of the work for
  152. Xmaking modifications to it.  For a library, complete source code means
  153. Xall the source code for all modules it contains, plus any associated
  154. Xinterface definition files, plus the scripts used to control compilation
  155. Xand installation of the library.
  156. X
  157. X  Activities other than copying, distribution and modification are not
  158. Xcovered by this License; they are outside its scope.  The act of
  159. Xrunning a program using the Library is not restricted, and output from
  160. Xsuch a program is covered only if its contents constitute a work based
  161. Xon the Library (independent of the use of the Library in a tool for
  162. Xwriting it).  Whether that is true depends on what the Library does
  163. Xand what the program that uses the Library does.
  164. X  
  165. X  1. You may copy and distribute verbatim copies of the Library's
  166. Xcomplete source code as you receive it, in any medium, provided that
  167. Xyou conspicuously and appropriately publish on each copy an
  168. Xappropriate copyright notice and disclaimer of warranty; keep intact
  169. Xall the notices that refer to this License and to the absence of any
  170. Xwarranty; and distribute a copy of this License along with the
  171. XLibrary.
  172. X
  173. X  You may charge a fee for the physical act of transferring a copy,
  174. Xand you may at your option offer warranty protection in exchange for a
  175. Xfee.
  176. X
  177. X  2. You may modify your copy or copies of the Library or any portion
  178. Xof it, thus forming a work based on the Library, and copy and
  179. Xdistribute such modifications or work under the terms of Section 1
  180. Xabove, provided that you also meet all of these conditions:
  181. X
  182. X    a) The modified work must itself be a software library.
  183. X
  184. X    b) You must cause the files modified to carry prominent notices
  185. X    stating that you changed the files and the date of any change.
  186. X
  187. X    c) You must cause the whole of the work to be licensed at no
  188. X    charge to all third parties under the terms of this License.
  189. X
  190. X    d) If a facility in the modified Library refers to a function or a
  191. X    table of data to be supplied by an application program that uses
  192. X    the facility, other than as an argument passed when the facility
  193. X    is invoked, then you must make a good faith effort to ensure that,
  194. X    in the event an application does not supply such function or
  195. X    table, the facility still operates, and performs whatever part of
  196. X    its purpose remains meaningful.
  197. X
  198. X    (For example, a function in a library to compute square roots has
  199. X    a purpose that is entirely well-defined independent of the
  200. X    application.  Therefore, Subsection 2d requires that any
  201. X    application-supplied function or table used by this function must
  202. X    be optional: if the application does not supply it, the square
  203. X    root function must still compute square roots.)
  204. X
  205. XThese requirements apply to the modified work as a whole.  If
  206. Xidentifiable sections of that work are not derived from the Library,
  207. Xand can be reasonably considered independent and separate works in
  208. Xthemselves, then this License, and its terms, do not apply to those
  209. Xsections when you distribute them as separate works.  But when you
  210. Xdistribute the same sections as part of a whole which is a work based
  211. Xon the Library, the distribution of the whole must be on the terms of
  212. Xthis License, whose permissions for other licensees extend to the
  213. Xentire whole, and thus to each and every part regardless of who wrote
  214. Xit.
  215. X
  216. XThus, it is not the intent of this section to claim rights or contest
  217. Xyour rights to work written entirely by you; rather, the intent is to
  218. Xexercise the right to control the distribution of derivative or
  219. Xcollective works based on the Library.
  220. X
  221. XIn addition, mere aggregation of another work not based on the Library
  222. Xwith the Library (or with a work based on the Library) on a volume of
  223. Xa storage or distribution medium does not bring the other work under
  224. Xthe scope of this License.
  225. X
  226. X  3. You may opt to apply the terms of the ordinary GNU General Public
  227. XLicense instead of this License to a given copy of the Library.  To do
  228. Xthis, you must alter all the notices that refer to this License, so
  229. Xthat they refer to the ordinary GNU General Public License, version 2,
  230. Xinstead of to this License.  (If a newer version than version 2 of the
  231. Xordinary GNU General Public License has appeared, then you can specify
  232. Xthat version instead if you wish.)  Do not make any other change in
  233. Xthese notices.
  234. X
  235. X  Once this change is made in a given copy, it is irreversible for
  236. Xthat copy, so the ordinary GNU General Public License applies to all
  237. Xsubsequent copies and derivative works made from that copy.
  238. X
  239. X  This option is useful when you wish to copy part of the code of
  240. Xthe Library into a program that is not a library.
  241. X
  242. X  4. You may copy and distribute the Library (or a portion or
  243. Xderivative of it, under Section 2) in object code or executable form
  244. Xunder the terms of Sections 1 and 2 above provided that you accompany
  245. Xit with the complete corresponding machine-readable source code, which
  246. Xmust be distributed under the terms of Sections 1 and 2 above on a
  247. Xmedium customarily used for software interchange.
  248. X
  249. X  If distribution of object code is made by offering access to copy
  250. Xfrom a designated place, then offering equivalent access to copy the
  251. Xsource code from the same place satisfies the requirement to
  252. Xdistribute the source code, even though third parties are not
  253. Xcompelled to copy the source along with the object code.
  254. X
  255. X  5. A program that contains no derivative of any portion of the
  256. XLibrary, but is designed to work with the Library by being compiled or
  257. Xlinked with it, is called a "work that uses the Library".  Such a
  258. Xwork, in isolation, is not a derivative work of the Library, and
  259. Xtherefore falls outside the scope of this License.
  260. X
  261. X  However, linking a "work that uses the Library" with the Library
  262. Xcreates an executable that is a derivative of the Library (because it
  263. Xcontains portions of the Library), rather than a "work that uses the
  264. Xlibrary".  The executable is therefore covered by this License.
  265. XSection 6 states terms for distribution of such executables.
  266. X
  267. X  When a "work that uses the Library" uses material from a header file
  268. Xthat is part of the Library, the object code for the work may be a
  269. Xderivative work of the Library even though the source code is not.
  270. XWhether this is true is especially significant if the work can be
  271. Xlinked without the Library, or if the work is itself a library.  The
  272. Xthreshold for this to be true is not precisely defined by law.
  273. X
  274. X  If such an object file uses only numerical parameters, data
  275. Xstructure layouts and accessors, and small macros and small inline
  276. Xfunctions (ten lines or less in length), then the use of the object
  277. Xfile is unrestricted, regardless of whether it is legally a derivative
  278. Xwork.  (Executables containing this object code plus portions of the
  279. XLibrary will still fall under Section 6.)
  280. X
  281. X  Otherwise, if the work is a derivative of the Library, you may
  282. Xdistribute the object code for the work under the terms of Section 6.
  283. XAny executables containing that work also fall under Section 6,
  284. Xwhether or not they are linked directly with the Library itself.
  285. X
  286. X  6. As an exception to the Sections above, you may also compile or
  287. Xlink a "work that uses the Library" with the Library to produce a
  288. Xwork containing portions of the Library, and distribute that work
  289. Xunder terms of your choice, provided that the terms permit
  290. Xmodification of the work for the customer's own use and reverse
  291. Xengineering for debugging such modifications.
  292. X
  293. X  You must give prominent notice with each copy of the work that the
  294. XLibrary is used in it and that the Library and its use are covered by
  295. Xthis License.  You must supply a copy of this License.  If the work
  296. Xduring execution displays copyright notices, you must include the
  297. Xcopyright notice for the Library among them, as well as a reference
  298. Xdirecting the user to the copy of this License.  Also, you must do one
  299. Xof these things:
  300. X
  301. X    a) Accompany the work with the complete corresponding
  302. X    machine-readable source code for the Library including whatever
  303. X    changes were used in the work (which must be distributed under
  304. X    Sections 1 and 2 above); and, if the work is an executable linked
  305. X    with the Library, with the complete machine-readable "work that
  306. X    uses the Library", as object code and/or source code, so that the
  307. X    user can modify the Library and then relink to produce a modified
  308. X    executable containing the modified Library.  (It is understood
  309. X    that the user who changes the contents of definitions files in the
  310. X    Library will not necessarily be able to recompile the application
  311. X    to use the modified definitions.)
  312. X
  313. X    b) Accompany the work with a written offer, valid for at
  314. X    least three years, to give the same user the materials
  315. X    specified in Subsection 6a, above, for a charge no more
  316. X    than the cost of performing this distribution.
  317. X
  318. X    c) If distribution of the work is made by offering access to copy
  319. X    from a designated place, offer equivalent access to copy the above
  320. X    specified materials from the same place.
  321. X
  322. X    d) Verify that the user has already received a copy of these
  323. X    materials or that you have already sent this user a copy.
  324. X
  325. X  For an executable, the required form of the "work that uses the
  326. XLibrary" must include any data and utility programs needed for
  327. Xreproducing the executable from it.  However, as a special exception,
  328. Xthe source code distributed need not include anything that is normally
  329. Xdistributed (in either source or binary form) with the major
  330. Xcomponents (compiler, kernel, and so on) of the operating system on
  331. Xwhich the executable runs, unless that component itself accompanies
  332. Xthe executable.
  333. X
  334. X  It may happen that this requirement contradicts the license
  335. Xrestrictions of other proprietary libraries that do not normally
  336. Xaccompany the operating system.  Such a contradiction means you cannot
  337. Xuse both them and the Library together in an executable that you
  338. Xdistribute.
  339. X
  340. X  7. You may place library facilities that are a work based on the
  341. XLibrary side-by-side in a single library together with other library
  342. Xfacilities not covered by this License, and distribute such a combined
  343. Xlibrary, provided that the separate distribution of the work based on
  344. Xthe Library and of the other library facilities is otherwise
  345. Xpermitted, and provided that you do these two things:
  346. X
  347. X    a) Accompany the combined library with a copy of the same work
  348. X    based on the Library, uncombined with any other library
  349. X    facilities.  This must be distributed under the terms of the
  350. X    Sections above.
  351. X
  352. X    b) Give prominent notice with the combined library of the fact
  353. X    that part of it is a work based on the Library, and explaining
  354. X    where to find the accompanying uncombined form of the same work.
  355. X
  356. X  8. You may not copy, modify, sublicense, link with, or distribute
  357. Xthe Library except as expressly provided under this License.  Any
  358. Xattempt otherwise to copy, modify, sublicense, link with, or
  359. Xdistribute the Library is void, and will automatically terminate your
  360. Xrights under this License.  However, parties who have received copies,
  361. Xor rights, from you under this License will not have their licenses
  362. Xterminated so long as such parties remain in full compliance.
  363. X
  364. X  9. You are not required to accept this License, since you have not
  365. Xsigned it.  However, nothing else grants you permission to modify or
  366. Xdistribute the Library or its derivative works.  These actions are
  367. Xprohibited by law if you do not accept this License.  Therefore, by
  368. Xmodifying or distributing the Library (or any work based on the
  369. XLibrary), you indicate your acceptance of this License to do so, and
  370. Xall its terms and conditions for copying, distributing or modifying
  371. Xthe Library or works based on it.
  372. X
  373. X  10. Each time you redistribute the Library (or any work based on the
  374. XLibrary), the recipient automatically receives a license from the
  375. Xoriginal licensor to copy, distribute, link with or modify the Library
  376. Xsubject to these terms and conditions.  You may not impose any further
  377. Xrestrictions on the recipients' exercise of the rights granted herein.
  378. XYou are not responsible for enforcing compliance by third parties to
  379. Xthis License.
  380. X
  381. X  11. If, as a consequence of a court judgment or allegation of patent
  382. Xinfringement or for any other reason (not limited to patent issues),
  383. Xconditions are imposed on you (whether by court order, agreement or
  384. Xotherwise) that contradict the conditions of this License, they do not
  385. Xexcuse you from the conditions of this License.  If you cannot
  386. Xdistribute so as to satisfy simultaneously your obligations under this
  387. XLicense and any other pertinent obligations, then as a consequence you
  388. Xmay not distribute the Library at all.  For example, if a patent
  389. Xlicense would not permit royalty-free redistribution of the Library by
  390. Xall those who receive copies directly or indirectly through you, then
  391. Xthe only way you could satisfy both it and this License would be to
  392. Xrefrain entirely from distribution of the Library.
  393. X
  394. XIf any portion of this section is held invalid or unenforceable under any
  395. Xparticular circumstance, the balance of the section is intended to apply,
  396. Xand the section as a whole is intended to apply in other circumstances.
  397. X
  398. XIt is not the purpose of this section to induce you to infringe any
  399. Xpatents or other property right claims or to contest validity of any
  400. Xsuch claims; this section has the sole purpose of protecting the
  401. Xintegrity of the free software distribution system which is
  402. Ximplemented by public license practices.  Many people have made
  403. Xgenerous contributions to the wide range of software distributed
  404. Xthrough that system in reliance on consistent application of that
  405. Xsystem; it is up to the author/donor to decide if he or she is willing
  406. Xto distribute software through any other system and a licensee cannot
  407. Ximpose that choice.
  408. X
  409. XThis section is intended to make thoroughly clear what is believed to
  410. Xbe a consequence of the rest of this License.
  411. X
  412. X  12. If the distribution and/or use of the Library is restricted in
  413. Xcertain countries either by patents or by copyrighted interfaces, the
  414. Xoriginal copyright holder who places the Library under this License may add
  415. Xan explicit geographical distribution limitation excluding those countries,
  416. Xso that distribution is permitted only in or among countries not thus
  417. Xexcluded.  In such case, this License incorporates the limitation as if
  418. Xwritten in the body of this License.
  419. X
  420. X  13. The Free Software Foundation may publish revised and/or new
  421. Xversions of the Library General Public License from time to time.
  422. XSuch new versions will be similar in spirit to the present version,
  423. Xbut may differ in detail to address new problems or concerns.
  424. X
  425. XEach version is given a distinguishing version number.  If the Library
  426. Xspecifies a version number of this License which applies to it and
  427. X"any later version", you have the option of following the terms and
  428. Xconditions either of that version or of any later version published by
  429. Xthe Free Software Foundation.  If the Library does not specify a
  430. Xlicense version number, you may choose any version ever published by
  431. Xthe Free Software Foundation.
  432. X
  433. X  14. If you wish to incorporate parts of the Library into other free
  434. Xprograms whose distribution conditions are incompatible with these,
  435. Xwrite to the author to ask for permission.  For software which is
  436. Xcopyrighted by the Free Software Foundation, write to the Free
  437. XSoftware Foundation; we sometimes make exceptions for this.  Our
  438. Xdecision will be guided by the two goals of preserving the free status
  439. Xof all derivatives of our free software and of promoting the sharing
  440. Xand reuse of software generally.
  441. X
  442. X                NO WARRANTY
  443. X
  444. X  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
  445. XWARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
  446. XEXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
  447. XOTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
  448. XKIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
  449. XIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  450. XPURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  451. XLIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
  452. XTHE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  453. X
  454. X  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  455. XWRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
  456. XAND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
  457. XFOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
  458. XCONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
  459. XLIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
  460. XRENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
  461. XFAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
  462. XSUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  463. XDAMAGES.
  464. X
  465. X             END OF TERMS AND CONDITIONS
  466. X
  467. X     Appendix: How to Apply These Terms to Your New Libraries
  468. X
  469. X  If you develop a new library, and you want it to be of the greatest
  470. Xpossible use to the public, we recommend making it free software that
  471. Xeveryone can redistribute and change.  You can do so by permitting
  472. Xredistribution under these terms (or, alternatively, under the terms of the
  473. Xordinary General Public License).
  474. X
  475. X  To apply these terms, attach the following notices to the library.  It is
  476. Xsafest to attach them to the start of each source file to most effectively
  477. Xconvey the exclusion of warranty; and each file should have at least the
  478. X"copyright" line and a pointer to where the full notice is found.
  479. X
  480. X    <one line to give the library's name and a brief idea of what it does.>
  481. X    Copyright (C) <year>  <name of author>
  482. X
  483. X    This library is free software; you can redistribute it and/or
  484. X    modify it under the terms of the GNU Library General Public
  485. X    License as published by the Free Software Foundation; either
  486. X    version 2 of the License, or (at your option) any later version.
  487. X
  488. X    This library is distributed in the hope that it will be useful,
  489. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  490. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  491. X    Library General Public License for more details.
  492. X
  493. X    You should have received a copy of the GNU Library General Public
  494. X    License along with this library; if not, write to the Free
  495. X    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  496. X
  497. XAlso add information on how to contact you by electronic and paper mail.
  498. X
  499. XYou should also get your employer (if you work as a programmer) or your
  500. Xschool, if any, to sign a "copyright disclaimer" for the library, if
  501. Xnecessary.  Here is a sample; alter the names:
  502. X
  503. X  Yoyodyne, Inc., hereby disclaims all copyright interest in the
  504. X  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
  505. X
  506. X  <signature of Ty Coon>, 1 April 1990
  507. X  Ty Coon, President of Vice
  508. X
  509. XThat's all there is to it!
  510. END_OF_FILE
  511. if test 25265 -ne `wc -c <'COPYING'`; then
  512.     echo shar: \"'COPYING'\" unpacked with wrong size!
  513. fi
  514. # end of 'COPYING'
  515. fi
  516. if test -f 'crypt_util.c' -a "${1}" != "-c" ; then 
  517.   echo shar: Will not clobber existing file \"'crypt_util.c'\"
  518. else
  519. echo shar: Extracting \"'crypt_util.c'\" \(22326 characters\)
  520. sed "s/^X//" >'crypt_util.c' <<'END_OF_FILE'
  521. X/*
  522. X * UFC-crypt: ultra fast crypt(3) implementation
  523. X *
  524. X * Copyright (C) 1991, 1992, Free Software Foundation, Inc.
  525. X *
  526. X * This library is free software; you can redistribute it and/or
  527. X * modify it under the terms of the GNU Library General Public
  528. X * License as published by the Free Software Foundation; either
  529. X * version 2 of the License, or (at your option) any later version.
  530. X *
  531. X * This library is distributed in the hope that it will be useful,
  532. X * but WITHOUT ANY WARRANTY; without even the implied warranty of
  533. X * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  534. X * Library General Public License for more details.
  535. X *
  536. X * You should have received a copy of the GNU Library General Public
  537. X * License along with this library; if not, write to the Free
  538. X * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  539. X *
  540. X * @(#)crypt_util.c    2.31 02/08/92
  541. X *
  542. X * Support routines
  543. X *
  544. X */
  545. X
  546. X#ifdef DEBUG
  547. X#include <stdio.h>
  548. X#endif
  549. X
  550. X#ifndef STATIC
  551. X#define STATIC static
  552. X#endif
  553. X
  554. X#include "patchlevel.h"
  555. X#include "ufc-crypt.h"
  556. X
  557. Xstatic char patchlevel_str[] = PATCHLEVEL;
  558. X
  559. X/* 
  560. X * Permutation done once on the 56 bit 
  561. X *  key derived from the original 8 byte ASCII key.
  562. X */
  563. Xstatic int pc1[56] = { 
  564. X  57, 49, 41, 33, 25, 17,  9,  1, 58, 50, 42, 34, 26, 18,
  565. X  10,  2, 59, 51, 43, 35, 27, 19, 11,  3, 60, 52, 44, 36,
  566. X  63, 55, 47, 39, 31, 23, 15,  7, 62, 54, 46, 38, 30, 22,
  567. X  14,  6, 61, 53, 45, 37, 29, 21, 13,  5, 28, 20, 12,  4
  568. X};
  569. X
  570. X/*
  571. X * How much to rotate each 28 bit half of the pc1 permutated
  572. X *  56 bit key before using pc2 to give the i' key
  573. X */
  574. Xstatic int rots[16] = { 
  575. X  1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 
  576. X};
  577. X
  578. X/* 
  579. X * Permutation giving the key 
  580. X * of the i' DES round 
  581. X */
  582. Xstatic int pc2[48] = { 
  583. X  14, 17, 11, 24,  1,  5,  3, 28, 15,  6, 21, 10,
  584. X  23, 19, 12,  4, 26,  8, 16,  7, 27, 20, 13,  2,
  585. X  41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
  586. X  44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
  587. X};
  588. X
  589. X/*
  590. X * The E expansion table which selects
  591. X * bits from the 32 bit intermediate result.
  592. X */
  593. Xstatic int esel[48] = { 
  594. X  32,  1,  2,  3,  4,  5,  4,  5,  6,  7,  8,  9,
  595. X   8,  9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
  596. X  16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
  597. X  24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32,  1
  598. X};
  599. Xstatic int e_inverse[64];
  600. X
  601. X/* 
  602. X * Permutation done on the 
  603. X * result of sbox lookups 
  604. X */
  605. Xstatic int perm32[32] = {
  606. X  16,  7, 20, 21, 29, 12, 28, 17,  1, 15, 23, 26,  5, 18, 31, 10,
  607. X  2,   8, 24, 14, 32, 27,  3,  9, 19, 13, 30,  6, 22, 11,  4, 25
  608. X};
  609. X
  610. X/* 
  611. X * The sboxes
  612. X */
  613. Xstatic int sbox[8][4][16]= {
  614. X        { { 14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7 },
  615. X          {  0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8 },
  616. X          {  4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0 },
  617. X          { 15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13 }
  618. X        },
  619. X
  620. X        { { 15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10 },
  621. X          {  3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5 },
  622. X          {  0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15 },
  623. X          { 13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9 }
  624. X        },
  625. X
  626. X        { { 10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8 },
  627. X          { 13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1 },
  628. X          { 13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7 },
  629. X          {  1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12 }
  630. X        },
  631. X
  632. X        { {  7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15 },
  633. X          { 13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9 },
  634. X          { 10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4 },
  635. X          {  3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14 }
  636. X        },
  637. X
  638. X        { {  2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9 },
  639. X          { 14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6 },
  640. X          {  4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14 },
  641. X          { 11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3 }
  642. X        },
  643. X
  644. X        { { 12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11 },
  645. X          { 10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8 },
  646. X          {  9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6 },
  647. X          {  4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13 }
  648. X        },
  649. X
  650. X        { {  4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1 },
  651. X          { 13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6 },
  652. X          {  1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2 },
  653. X          {  6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12 }
  654. X        },
  655. X
  656. X        { { 13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7 },
  657. X          {  1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2 },
  658. X          {  7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8 },
  659. X          {  2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11 }
  660. X        }
  661. X};
  662. X
  663. X/* 
  664. X * This is the initial 
  665. X * permutation matrix
  666. X */
  667. Xstatic int initial_perm[64] = { 
  668. X  58, 50, 42, 34, 26, 18, 10,  2, 60, 52, 44, 36, 28, 20, 12, 4,
  669. X  62, 54, 46, 38, 30, 22, 14,  6, 64, 56, 48, 40, 32, 24, 16, 8,
  670. X  57, 49, 41, 33, 25, 17,  9,  1, 59, 51, 43, 35, 27, 19, 11, 3,
  671. X  61, 53, 45, 37, 29, 21, 13,  5, 63, 55, 47, 39, 31, 23, 15, 7
  672. X};
  673. X
  674. X/* 
  675. X * This is the final 
  676. X * permutation matrix
  677. X */
  678. Xstatic int final_perm[64] = {
  679. X  40,  8, 48, 16, 56, 24, 64, 32, 39,  7, 47, 15, 55, 23, 63, 31,
  680. X  38,  6, 46, 14, 54, 22, 62, 30, 37,  5, 45, 13, 53, 21, 61, 29,
  681. X  36,  4, 44, 12, 52, 20, 60, 28, 35,  3, 43, 11, 51, 19, 59, 27,
  682. X  34,  2, 42, 10, 50, 18, 58, 26, 33,  1, 41,  9, 49, 17, 57, 25
  683. X};
  684. X
  685. X/* 
  686. X * The 16 DES keys in BITMASK format 
  687. X */
  688. X#ifdef _UFC_32_
  689. Xlong32 _ufc_keytab[16][2];
  690. X#endif
  691. X#ifdef _UFC_64_
  692. Xlong64 _ufc_keytab[16];
  693. X#endif
  694. X
  695. X#define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
  696. X#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
  697. X
  698. X/* Macro to set a bit (0..23) */
  699. X#define BITMASK(i) ( (1<<(11-(i)%12+3)) << ((i)<12?16:0) )
  700. X
  701. X/*
  702. X * sb arrays:
  703. X *
  704. X * Workhorses of the inner loop of the DES implementation.
  705. X * They do sbox lookup, shifting of this  value, 32 bit
  706. X * permutation and E permutation for the next round.
  707. X *
  708. X * Kept in 'BITMASK' format.
  709. X */
  710. X
  711. X#ifdef _UFC_32_
  712. Xlong32 _ufc_sb0[8192], _ufc_sb1[8192], _ufc_sb2[8192], _ufc_sb3[8192];
  713. Xstatic long32 *sb[4] = {_ufc_sb0, _ufc_sb1, _ufc_sb2, _ufc_sb3}; 
  714. X#endif
  715. X
  716. X#ifdef _UFC_64_
  717. Xlong64 _ufc_sb0[4096], _ufc_sb1[4096], _ufc_sb2[4096], _ufc_sb3[4096];
  718. Xstatic long64 *sb[4] = {_ufc_sb0, _ufc_sb1, _ufc_sb2, _ufc_sb3}; 
  719. X#endif
  720. X
  721. X/* 
  722. X * eperm32tab: do 32 bit permutation and E selection
  723. X *
  724. X * The first index is the byte number in the 32 bit value to be permuted
  725. X *  -  second  -   is the value of this byte
  726. X *  -  third   -   selects the two 32 bit values
  727. X *
  728. X * The table is used and generated internally in init_des to speed it up
  729. X */
  730. Xstatic ufc_long eperm32tab[4][256][2];
  731. X
  732. X/* 
  733. X * do_pc1: permform pc1 permutation in the key schedule generation.
  734. X *
  735. X * The first   index is the byte number in the 8 byte ASCII key
  736. X *  -  second    -      -    the two 28 bits halfs of the result
  737. X *  -  third     -   selects the 7 bits actually used of each byte
  738. X *
  739. X * The result is kept with 28 bit per 32 bit with the 4 most significant
  740. X * bits zero.
  741. X */
  742. Xstatic ufc_long do_pc1[8][2][128];
  743. X
  744. X/*
  745. X * do_pc2: permform pc2 permutation in the key schedule generation.
  746. X *
  747. X * The first   index is the septet number in the two 28 bit intermediate values
  748. X *  -  second    -    -  -  septet values
  749. X *
  750. X * Knowledge of the structure of the pc2 permutation is used.
  751. X *
  752. X * The result is kept with 28 bit per 32 bit with the 4 most significant
  753. X * bits zero.
  754. X */
  755. Xstatic ufc_long do_pc2[8][128];
  756. X
  757. X/*
  758. X * efp: undo an extra e selection and do final
  759. X *      permutation giving the DES result.
  760. X * 
  761. X *      Invoked 6 bit a time on two 48 bit values
  762. X *      giving two 32 bit longs.
  763. X */
  764. Xstatic ufc_long efp[16][64][2];
  765. X
  766. Xstatic unsigned char bytemask[8]  = {
  767. X  0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  768. X};
  769. X
  770. Xstatic ufc_long longmask[32] = {
  771. X  0x80000000, 0x40000000, 0x20000000, 0x10000000,
  772. X  0x08000000, 0x04000000, 0x02000000, 0x01000000,
  773. X  0x00800000, 0x00400000, 0x00200000, 0x00100000,
  774. X  0x00080000, 0x00040000, 0x00020000, 0x00010000,
  775. X  0x00008000, 0x00004000, 0x00002000, 0x00001000,
  776. X  0x00000800, 0x00000400, 0x00000200, 0x00000100,
  777. X  0x00000080, 0x00000040, 0x00000020, 0x00000010,
  778. X  0x00000008, 0x00000004, 0x00000002, 0x00000001
  779. X};
  780. X
  781. X#ifdef DEBUG
  782. X
  783. Xpr_bits(a, n)
  784. X  ufc_long *a;
  785. X  int n;
  786. X  { ufc_long i, j, t, tmp;
  787. X    n /= 8;
  788. X    for(i = 0; i < n; i++) {
  789. X      tmp=0;
  790. X      for(j = 0; j < 8; j++) {
  791. X    t=8*i+j;
  792. X    tmp|=(a[t/24] & BITMASK(t % 24))?bytemask[j]:0;
  793. X      }
  794. X      (void)printf("%02x ",tmp);
  795. X    }
  796. X    printf(" ");
  797. X  }
  798. X
  799. Xstatic set_bits(v, b)
  800. X  ufc_long v;
  801. X  ufc_long *b;
  802. X  { ufc_long i;
  803. X    *b = 0;
  804. X    for(i = 0; i < 24; i++) {
  805. X      if(v & longmask[8 + i])
  806. X    *b |= BITMASK(i);
  807. X    }
  808. X  }
  809. X
  810. X#endif
  811. X
  812. X/*
  813. X * Silly rewrite of 'bzero'. I do so
  814. X * because some machines don't have
  815. X * bzero and some don't have memset.
  816. X */
  817. X
  818. XSTATIC void clearmem(start, cnt)
  819. X  char *start;
  820. X  int cnt;
  821. X  { while(cnt--)
  822. X      *start++ = '\0';
  823. X  }
  824. X
  825. Xstatic int initialized = 0;
  826. X
  827. X/* lookup a 6 bit value in sbox */
  828. X
  829. X#define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf];
  830. X
  831. X/*
  832. X * Initialize unit - may be invoked directly
  833. X * by fcrypt users.
  834. X */
  835. X
  836. Xvoid init_des()
  837. X  { int comes_from_bit;
  838. X    int bit, sg;
  839. X    ufc_long j;
  840. X    ufc_long mask1, mask2;
  841. X
  842. X    /*
  843. X     * Create the do_pc1 table used
  844. X     * to affect pc1 permutation
  845. X     * when generating keys
  846. X     */
  847. X    for(bit = 0; bit < 56; bit++) {
  848. X      comes_from_bit  = pc1[bit] - 1;
  849. X      mask1 = bytemask[comes_from_bit % 8 + 1];
  850. X      mask2 = longmask[bit % 28 + 4];
  851. X      for(j = 0; j < 128; j++) {
  852. X    if(j & mask1) 
  853. X      do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
  854. X      }
  855. X    }
  856. X
  857. X    /*
  858. X     * Create the do_pc2 table used
  859. X     * to affect pc2 permutation when
  860. X     * generating keys
  861. X     */
  862. X    for(bit = 0; bit < 48; bit++) {
  863. X      comes_from_bit  = pc2[bit] - 1;
  864. X      mask1 = bytemask[comes_from_bit % 7 + 1];
  865. X      mask2 = BITMASK(bit % 24);
  866. X      for(j = 0; j < 128; j++) {
  867. X    if(j & mask1)
  868. X      do_pc2[comes_from_bit / 7][j] |= mask2;
  869. X      }
  870. X    }
  871. X
  872. X    /* 
  873. X     * Now generate the table used to do combined
  874. X     * 32 bit permutation and e expansion
  875. X     *
  876. X     * We use it because we have to permute 16384 32 bit
  877. X     * longs into 48 bit in order to initialize sb.
  878. X     *
  879. X     * Looping 48 rounds per permutation becomes 
  880. X     * just too slow...
  881. X     *
  882. X     */
  883. X
  884. X    clearmem((char*)eperm32tab, sizeof(eperm32tab));
  885. X
  886. X    for(bit = 0; bit < 48; bit++) {
  887. X      ufc_long mask1,comes_from;
  888. X    
  889. X      comes_from = perm32[esel[bit]-1]-1;
  890. X      mask1      = bytemask[comes_from % 8];
  891. X    
  892. X      for(j = 256; j--;) {
  893. X    if(j & mask1)
  894. X      eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK(bit % 24);
  895. X      }
  896. X    }
  897. X    
  898. X    /* 
  899. X     * Create the sb tables:
  900. X     *
  901. X     * For each 12 bit segment of an 48 bit intermediate
  902. X     * result, the sb table precomputes the two 4 bit
  903. X     * values of the sbox lookups done with the two 6
  904. X     * bit halves, shifts them to their proper place,
  905. X     * sends them through perm32 and finally E expands
  906. X     * them so that they are ready for the next
  907. X     * DES round.
  908. X     *
  909. X     */
  910. X    for(sg = 0; sg < 4; sg++) {
  911. X      int j1, j2;
  912. X      int s1, s2;
  913. X    
  914. X      for(j1 = 0; j1 < 64; j1++) {
  915. X    s1 = s_lookup(2 * sg, j1);
  916. X    for(j2 = 0; j2 < 64; j2++) {
  917. X      ufc_long to_permute, inx;
  918. X    
  919. X      s2         = s_lookup(2 * sg + 1, j2);
  920. X      to_permute = ((s1 << 4)  | s2) << (24 - 8 * sg);
  921. X
  922. X#ifdef _UFC_32_
  923. X      inx = ((j1 << 6)  | j2) << 1;
  924. X      sb[sg][inx  ]  = eperm32tab[0][(to_permute >> 24) & 0xff][0];
  925. X      sb[sg][inx+1]  = eperm32tab[0][(to_permute >> 24) & 0xff][1];
  926. X      sb[sg][inx  ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
  927. X      sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
  928. X        sb[sg][inx  ] |= eperm32tab[2][(to_permute >>  8) & 0xff][0];
  929. X      sb[sg][inx+1] |= eperm32tab[2][(to_permute >>  8) & 0xff][1];
  930. X      sb[sg][inx  ] |= eperm32tab[3][(to_permute)       & 0xff][0];
  931. X      sb[sg][inx+1] |= eperm32tab[3][(to_permute)       & 0xff][1];
  932. X#endif
  933. X#ifdef _UFC_64_
  934. X      inx = ((j1 << 6)  | j2);
  935. X      sb[sg][inx]  = 
  936. X        ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) |
  937. X         (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1];
  938. X      sb[sg][inx] |=
  939. X        ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) |
  940. X         (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1];
  941. X        sb[sg][inx] |= 
  942. X        ((long64)eperm32tab[2][(to_permute >>  8) & 0xff][0] << 32) |
  943. X         (long64)eperm32tab[2][(to_permute >>  8) & 0xff][1];
  944. X      sb[sg][inx] |=
  945. X        ((long64)eperm32tab[3][(to_permute)       & 0xff][0] << 32) |
  946. X         (long64)eperm32tab[3][(to_permute)       & 0xff][1];
  947. X#endif
  948. X    }
  949. X      }
  950. X    }  
  951. X
  952. X    /* 
  953. X     * Create an inverse matrix for esel telling
  954. X     * where to plug out bits if undoing it
  955. X     */
  956. X    for(bit=48; bit--;) {
  957. X      e_inverse[esel[bit] - 1     ] = bit;
  958. X      e_inverse[esel[bit] - 1 + 32] = bit + 48;
  959. X    }
  960. X
  961. X    /* 
  962. X     * create efp: the matrix used to
  963. X     * undo the E expansion and effect final permutation
  964. X     */
  965. X    clearmem((char*)efp, sizeof efp);
  966. X    for(bit = 0; bit < 64; bit++) {
  967. X      int o_bit, o_long;
  968. X      ufc_long word_value, mask1, mask2;
  969. X      int comes_from_f_bit, comes_from_e_bit;
  970. X      int comes_from_word, bit_within_word;
  971. X
  972. X      /* See where bit i belongs in the two 32 bit long's */
  973. X      o_long = bit / 32; /* 0..1  */
  974. X      o_bit  = bit % 32; /* 0..31 */
  975. X
  976. X      /* 
  977. X       * And find a bit in the e permutated value setting this bit.
  978. X       *
  979. X       * Note: the e selection may have selected the same bit several
  980. X       * times. By the initialization of e_inverse, we only look
  981. X       * for one specific instance.
  982. X       */
  983. X      comes_from_f_bit = final_perm[bit] - 1;         /* 0..63 */
  984. X      comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
  985. X      comes_from_word  = comes_from_e_bit / 6;        /* 0..15 */
  986. X      bit_within_word  = comes_from_e_bit % 6;        /* 0..5  */
  987. X
  988. X      mask1 = longmask[bit_within_word + 26];
  989. X      mask2 = longmask[o_bit];
  990. X
  991. X      for(word_value = 64; word_value--;) {
  992. X    if(word_value & mask1)
  993. X      efp[comes_from_word][word_value][o_long] |= mask2;
  994. X      }
  995. X    }
  996. X    initialized++;
  997. X  }
  998. X
  999. X/* 
  1000. X * Process the elements of the sb table permuting the
  1001. X * bits swapped in the expansion by the current salt.
  1002. X */
  1003. X
  1004. X#ifdef _UFC_32_
  1005. XSTATIC void shuffle_sb(k, saltbits)
  1006. X  long32 *k;
  1007. X  ufc_long saltbits;
  1008. X  { ufc_long j;
  1009. X    long32 x;
  1010. X    for(j=4096; j--;) {
  1011. X      x = (k[0] ^ k[1]) & (long32)saltbits;
  1012. X      *k++ ^= x;
  1013. X      *k++ ^= x;
  1014. X    }
  1015. X  }
  1016. X#endif
  1017. X
  1018. X#ifdef _UFC_64_
  1019. XSTATIC void shuffle_sb(k, saltbits)
  1020. X  long64 *k;
  1021. X  ufc_long saltbits;
  1022. X  { ufc_long j;
  1023. X    long64 x;
  1024. X    for(j=4096; j--;) {
  1025. X      x = ((*k >> 32) ^ *k) & (long64)saltbits;
  1026. X      *k++ ^= (x << 32) | x;
  1027. X    }
  1028. X  }
  1029. X#endif
  1030. X
  1031. X/* 
  1032. X * Setup the unit for a new salt
  1033. X * Hopefully we'll not see a new salt in each crypt call.
  1034. X */
  1035. X
  1036. Xstatic unsigned char current_salt[3] = "&&"; /* invalid value */
  1037. Xstatic ufc_long current_saltbits = 0;
  1038. Xstatic int direction = 0;
  1039. X
  1040. XSTATIC void setup_salt(s)
  1041. X  char *s;
  1042. X  { ufc_long i, j, saltbits;
  1043. X
  1044. X    if(!initialized)
  1045. X      init_des();
  1046. X
  1047. X    if(s[0] == current_salt[0] && s[1] == current_salt[1])
  1048. X      return;
  1049. X    current_salt[0] = s[0]; current_salt[1] = s[1];
  1050. X
  1051. X    /* 
  1052. X     * This is the only crypt change to DES:
  1053. X     * entries are swapped in the expansion table
  1054. X     * according to the bits set in the salt.
  1055. X     */
  1056. X    saltbits = 0;
  1057. X    for(i = 0; i < 2; i++) {
  1058. X      long c=ascii_to_bin(s[i]);
  1059. X      if(c < 0 || c > 63)
  1060. X    c = 0;
  1061. X      for(j = 0; j < 6; j++) {
  1062. X    if((c >> j) & 0x1)
  1063. X      saltbits |= BITMASK(6 * i + j);
  1064. X      }
  1065. X    }
  1066. X
  1067. X    /*
  1068. X     * Permute the sb table values
  1069. X     * to reflect the changed e
  1070. X     * selection table
  1071. X     */
  1072. X    shuffle_sb(_ufc_sb0, current_saltbits ^ saltbits); 
  1073. X    shuffle_sb(_ufc_sb1, current_saltbits ^ saltbits);
  1074. X    shuffle_sb(_ufc_sb2, current_saltbits ^ saltbits);
  1075. X    shuffle_sb(_ufc_sb3, current_saltbits ^ saltbits);
  1076. X
  1077. X    current_saltbits = saltbits;
  1078. X  }
  1079. X
  1080. XSTATIC void ufc_mk_keytab(key)
  1081. X  char *key;
  1082. X  { ufc_long v1, v2, *k1;
  1083. X    int i;
  1084. X#ifdef _UFC_32_
  1085. X    long32 v, *k2 = &_ufc_keytab[0][0];
  1086. X#endif
  1087. X#ifdef _UFC_64_
  1088. X    long64 v, *k2 = &_ufc_keytab[0];
  1089. X#endif
  1090. X
  1091. X    v1 = v2 = 0; k1 = &do_pc1[0][0][0];
  1092. X    for(i = 8; i--;) {
  1093. X      v1 |= k1[*key   & 0x7f]; k1 += 128;
  1094. X      v2 |= k1[*key++ & 0x7f]; k1 += 128;
  1095. X    }
  1096. X
  1097. X    for(i = 0; i < 16; i++) {
  1098. X      k1 = &do_pc2[0][0];
  1099. X
  1100. X      v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i]));
  1101. X      v  = k1[(v1 >> 21) & 0x7f]; k1 += 128;
  1102. X      v |= k1[(v1 >> 14) & 0x7f]; k1 += 128;
  1103. X      v |= k1[(v1 >>  7) & 0x7f]; k1 += 128;
  1104. X      v |= k1[(v1      ) & 0x7f]; k1 += 128;
  1105. X
  1106. X#ifdef _UFC_32_
  1107. X      *k2++ = v;
  1108. X      v = 0;
  1109. X#endif
  1110. X#ifdef _UFC_64_
  1111. X      v <<= 32;
  1112. X#endif
  1113. X
  1114. X      v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i]));
  1115. X      v |= k1[(v2 >> 21) & 0x7f]; k1 += 128;
  1116. X      v |= k1[(v2 >> 14) & 0x7f]; k1 += 128;
  1117. X      v |= k1[(v2 >>  7) & 0x7f]; k1 += 128;
  1118. X      v |= k1[(v2      ) & 0x7f];
  1119. X
  1120. X      *k2++ = v;
  1121. X    }
  1122. X
  1123. X    direction = 0;
  1124. X  }
  1125. X
  1126. X/* 
  1127. X * Undo an extra E selection and do final permutations
  1128. X */
  1129. X
  1130. Xufc_long *_ufc_dofinalperm(l1, l2, r1, r2)
  1131. X  ufc_long l1,l2,r1,r2;
  1132. X  { ufc_long v1, v2, x;
  1133. X    static ufc_long ary[2];
  1134. X
  1135. X    x = (l1 ^ l2) & current_saltbits; l1 ^= x; l2 ^= x;
  1136. X    x = (r1 ^ r2) & current_saltbits; r1 ^= x; r2 ^= x;
  1137. X
  1138. X    v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;
  1139. X
  1140. X    v1 |= efp[15][ r2         & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];
  1141. X    v1 |= efp[14][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];
  1142. X    v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];
  1143. X    v1 |= efp[12][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];
  1144. X
  1145. X    v1 |= efp[11][ r1         & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];
  1146. X    v1 |= efp[10][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];
  1147. X    v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];
  1148. X    v1 |= efp[ 8][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];
  1149. X
  1150. X    v1 |= efp[ 7][ l2         & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];
  1151. X    v1 |= efp[ 6][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];
  1152. X    v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];
  1153. X    v1 |= efp[ 4][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];
  1154. X
  1155. X    v1 |= efp[ 3][ l1         & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];
  1156. X    v1 |= efp[ 2][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];
  1157. X    v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];
  1158. X    v1 |= efp[ 0][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];
  1159. X
  1160. X    ary[0] = v1; ary[1] = v2;
  1161. X    return ary;
  1162. X  }
  1163. X
  1164. X/* 
  1165. X * crypt only: convert from 64 bit to 11 bit ASCII 
  1166. X * prefixing with the salt
  1167. X */
  1168. X
  1169. XSTATIC char *output_conversion(v1, v2, salt)
  1170. X  ufc_long v1, v2;
  1171. X  char *salt;
  1172. X  { static char outbuf[14];
  1173. X    int i, s;
  1174. X
  1175. X    outbuf[0] = salt[0];
  1176. X    outbuf[1] = salt[1] ? salt[1] : salt[0];
  1177. X
  1178. X    for(i = 0; i < 5; i++)
  1179. X      outbuf[i + 2] = bin_to_ascii((v1 >> (26 - 6 * i)) & 0x3f);
  1180. X
  1181. X    s  = (v2 & 0xf) << 2;
  1182. X    v2 = (v2 >> 2) | ((v1 & 0x3) << 30);
  1183. X
  1184. X    for(i = 5; i < 10; i++)
  1185. X      outbuf[i + 2] = bin_to_ascii((v2 >> (56 - 6 * i)) & 0x3f);
  1186. X
  1187. X    outbuf[12] = bin_to_ascii(s);
  1188. X    outbuf[13] = 0;
  1189. X
  1190. X    return outbuf;
  1191. X  }
  1192. X
  1193. Xufc_long *_ufc_doit();
  1194. X
  1195. X/* 
  1196. X * UNIX crypt function
  1197. X */
  1198. X   
  1199. Xchar *crypt(key, salt)
  1200. X  char *key, *salt;
  1201. X  { ufc_long *s;
  1202. X    char ktab[9];
  1203. X
  1204. X    /*
  1205. X     * Hack DES tables according to salt
  1206. X     */
  1207. X    setup_salt(salt);
  1208. X
  1209. X    /*
  1210. X     * Setup key schedule
  1211. X     */
  1212. X    clearmem(ktab, sizeof ktab);
  1213. X    (void)strncpy(ktab, key, 8);
  1214. X    ufc_mk_keytab(ktab);
  1215. X
  1216. X    /*
  1217. X     * Go for the 25 DES encryptions
  1218. X     */
  1219. X    s = _ufc_doit((ufc_long)0, (ufc_long)0, 
  1220. X          (ufc_long)0, (ufc_long)0, (ufc_long)25);
  1221. X
  1222. X    /*
  1223. X     * And convert back to 6 bit ASCII
  1224. X     */
  1225. X    return output_conversion(s[0], s[1], salt);
  1226. X  }
  1227. X
  1228. X/* 
  1229. X * To make fcrypt users happy.
  1230. X * They don't need to call init_des.
  1231. X */
  1232. X
  1233. Xchar *fcrypt(key, salt)
  1234. X  char *key;
  1235. X  char *salt;
  1236. X  { return crypt(key, salt);
  1237. X  }
  1238. X
  1239. X/* 
  1240. X * UNIX encrypt function. Takes a bitvector
  1241. X * represented by one byte per bit and
  1242. X * encrypt/decrypt according to edflag
  1243. X */
  1244. X
  1245. Xvoid encrypt(block, edflag)
  1246. X  char *block;
  1247. X  int edflag;
  1248. X  { ufc_long l1, l2, r1, r2, *s;
  1249. X    int i;
  1250. X
  1251. X    /*
  1252. X     * Undo any salt changes to E expansion
  1253. X     */
  1254. X    setup_salt("..");
  1255. X
  1256. X    /*
  1257. X     * Reverse key table if
  1258. X     * changing operation (encrypt/decrypt)
  1259. X     */
  1260. X    if((edflag == 0) != (direction == 0)) {
  1261. X      for(i = 0; i < 8; i++) {
  1262. X#ifdef _UFC_32_
  1263. X    long32 x;
  1264. X    x = _ufc_keytab[15-i][0]; 
  1265. X        _ufc_keytab[15-i][0] = _ufc_keytab[i][0]; 
  1266. X        _ufc_keytab[i][0] = x;
  1267. X
  1268. X    x = _ufc_keytab[15-i][1]; 
  1269. X        _ufc_keytab[15-i][1] = _ufc_keytab[i][1]; 
  1270. X        _ufc_keytab[i][1] = x;
  1271. X#endif
  1272. X#ifdef _UFC_64_
  1273. X    long64 x;
  1274. X    x = _ufc_keytab[15-i];
  1275. X    _ufc_keytab[15-i] = _ufc_keytab[i];
  1276. X    _ufc_keytab[i] = x;
  1277. X#endif
  1278. X      }
  1279. X      direction = edflag;
  1280. X    }
  1281. X
  1282. X    /*
  1283. X     * Do initial permutation + E expansion
  1284. X     */
  1285. X    i = 0;
  1286. X    for(l1 = 0; i < 24; i++) {
  1287. X      if(block[initial_perm[esel[i]-1]-1])
  1288. X    l1 |= BITMASK(i);
  1289. X    }
  1290. X    for(l2 = 0; i < 48; i++) {
  1291. X      if(block[initial_perm[esel[i]-1]-1])
  1292. X    l2 |= BITMASK(i-24);
  1293. X    }
  1294. X
  1295. X    i = 0;
  1296. X    for(r1 = 0; i < 24; i++) {
  1297. X      if(block[initial_perm[esel[i]-1+32]-1])
  1298. X    r1 |= BITMASK(i);
  1299. X    }
  1300. X    for(r2 = 0; i < 48; i++) {
  1301. X      if(block[initial_perm[esel[i]-1+32]-1])
  1302. X    r2 |= BITMASK(i-24);
  1303. X    }
  1304. X
  1305. X    /*
  1306. X     * Do DES inner loops + final conversion
  1307. X     */
  1308. X    s = _ufc_doit(l1, l2, r1, r2, (ufc_long)1);
  1309. X
  1310. X    /*
  1311. X     * And convert to bit array
  1312. X     */
  1313. X    l1 = s[0]; r1 = s[1];
  1314. X    for(i = 0; i < 32; i++) {
  1315. X      *block++ = (l1 & longmask[i]) != 0;
  1316. X    }
  1317. X    for(i = 0; i < 32; i++) {
  1318. X      *block++ = (r1 & longmask[i]) != 0;
  1319. X    }
  1320. X    
  1321. X  }
  1322. X
  1323. X/* 
  1324. X * UNIX setkey function. Take a 64 bit DES
  1325. X * key and setup the machinery.
  1326. X */
  1327. X
  1328. Xvoid setkey(key)
  1329. X  char *key;
  1330. X  { int i,j;
  1331. X    unsigned char c;
  1332. X    unsigned char ktab[8];
  1333. X
  1334. X    setup_salt(".."); /* be sure we're initialized */
  1335. X
  1336. X    for(i = 0; i < 8; i++) {
  1337. X      for(j = 0, c = 0; j < 8; j++)
  1338. X    c = c << 1 | *key++;
  1339. X      ktab[i] = c >> 1;
  1340. X    }
  1341. X    
  1342. X    ufc_mk_keytab(ktab);
  1343. X  }
  1344. X
  1345. X
  1346. X
  1347. X
  1348. X
  1349. END_OF_FILE
  1350. if test 22326 -ne `wc -c <'crypt_util.c'`; then
  1351.     echo shar: \"'crypt_util.c'\" unpacked with wrong size!
  1352. fi
  1353. # end of 'crypt_util.c'
  1354. fi
  1355. echo shar: End of archive 2 \(of 2\).
  1356. cp /dev/null ark2isdone
  1357. MISSING=""
  1358. for I in 1 2 ; do
  1359.     if test ! -f ark${I}isdone ; then
  1360.     MISSING="${MISSING} ${I}"
  1361.     fi
  1362. done
  1363. if test "${MISSING}" = "" ; then
  1364.     echo You have unpacked both archives.
  1365.     rm -f ark[1-9]isdone
  1366. else
  1367.     echo You still need to unpack the following archives:
  1368.     echo "        " ${MISSING}
  1369. fi
  1370. ##  End of shell archive.
  1371. exit 0
  1372. exit 0 # Just in case...
  1373.