descore-readme.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. ===========================================
  4. Fast & Portable DES encryption & decryption
  5. ===========================================
  6. .. note::
  7. Below is the original README file from the descore.shar package,
  8. converted to ReST format.
  9. ------------------------------------------------------------------------------
  10. des - fast & portable DES encryption & decryption.
  11. Copyright |copy| 1992 Dana L. How
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU Library General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU Library General Public License for more details.
  20. You should have received a copy of the GNU Library General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. Author's address: how@isl.stanford.edu
  24. .. README,v 1.15 1992/05/20 00:25:32 how E
  25. ==>> To compile after untarring/unsharring, just ``make`` <<==
  26. This package was designed with the following goals:
  27. 1. Highest possible encryption/decryption PERFORMANCE.
  28. 2. PORTABILITY to any byte-addressable host with a 32bit unsigned C type
  29. 3. Plug-compatible replacement for KERBEROS's low-level routines.
  30. This second release includes a number of performance enhancements for
  31. register-starved machines. My discussions with Richard Outerbridge,
  32. 71755.204@compuserve.com, sparked a number of these enhancements.
  33. To more rapidly understand the code in this package, inspect desSmallFips.i
  34. (created by typing ``make``) BEFORE you tackle desCode.h. The latter is set
  35. up in a parameterized fashion so it can easily be modified by speed-daemon
  36. hackers in pursuit of that last microsecond. You will find it more
  37. illuminating to inspect one specific implementation,
  38. and then move on to the common abstract skeleton with this one in mind.
  39. performance comparison to other available des code which i could
  40. compile on a SPARCStation 1 (cc -O4, gcc -O2):
  41. this code (byte-order independent):
  42. - 30us per encryption (options: 64k tables, no IP/FP)
  43. - 33us per encryption (options: 64k tables, FIPS standard bit ordering)
  44. - 45us per encryption (options: 2k tables, no IP/FP)
  45. - 48us per encryption (options: 2k tables, FIPS standard bit ordering)
  46. - 275us to set a new key (uses 1k of key tables)
  47. this has the quickest encryption/decryption routines i've seen.
  48. since i was interested in fast des filters rather than crypt(3)
  49. and password cracking, i haven't really bothered yet to speed up
  50. the key setting routine. also, i have no interest in re-implementing
  51. all the other junk in the mit kerberos des library, so i've just
  52. provided my routines with little stub interfaces so they can be
  53. used as drop-in replacements with mit's code or any of the mit-
  54. compatible packages below. (note that the first two timings above
  55. are highly variable because of cache effects).
  56. kerberos des replacement from australia (version 1.95):
  57. - 53us per encryption (uses 2k of tables)
  58. - 96us to set a new key (uses 2.25k of key tables)
  59. so despite the author's inclusion of some of the performance
  60. improvements i had suggested to him, this package's
  61. encryption/decryption is still slower on the sparc and 68000.
  62. more specifically, 19-40% slower on the 68020 and 11-35% slower
  63. on the sparc, depending on the compiler;
  64. in full gory detail (ALT_ECB is a libdes variant):
  65. =============== ============== =============== =================
  66. compiler machine desCore libdes ALT_ECB slower by
  67. =============== ============== =============== =================
  68. gcc 2.1 -O2 Sun 3/110 304 uS 369.5uS 461.8uS 22%
  69. cc -O1 Sun 3/110 336 uS 436.6uS 399.3uS 19%
  70. cc -O2 Sun 3/110 360 uS 532.4uS 505.1uS 40%
  71. cc -O4 Sun 3/110 365 uS 532.3uS 505.3uS 38%
  72. gcc 2.1 -O2 Sun 4/50 48 uS 53.4uS 57.5uS 11%
  73. cc -O2 Sun 4/50 48 uS 64.6uS 64.7uS 35%
  74. cc -O4 Sun 4/50 48 uS 64.7uS 64.9uS 35%
  75. =============== ============== =============== =================
  76. (my time measurements are not as accurate as his).
  77. the comments in my first release of desCore on version 1.92:
  78. - 68us per encryption (uses 2k of tables)
  79. - 96us to set a new key (uses 2.25k of key tables)
  80. this is a very nice package which implements the most important
  81. of the optimizations which i did in my encryption routines.
  82. it's a bit weak on common low-level optimizations which is why
  83. it's 39%-106% slower. because he was interested in fast crypt(3) and
  84. password-cracking applications, he also used the same ideas to
  85. speed up the key-setting routines with impressive results.
  86. (at some point i may do the same in my package). he also implements
  87. the rest of the mit des library.
  88. (code from eay@psych.psy.uq.oz.au via comp.sources.misc)
  89. fast crypt(3) package from denmark:
  90. the des routine here is buried inside a loop to do the
  91. crypt function and i didn't feel like ripping it out and measuring
  92. performance. his code takes 26 sparc instructions to compute one
  93. des iteration; above, Quick (64k) takes 21 and Small (2k) takes 37.
  94. he claims to use 280k of tables but the iteration calculation seems
  95. to use only 128k. his tables and code are machine independent.
  96. (code from glad@daimi.aau.dk via alt.sources or comp.sources.misc)
  97. swedish reimplementation of Kerberos des library
  98. - 108us per encryption (uses 34k worth of tables)
  99. - 134us to set a new key (uses 32k of key tables to get this speed!)
  100. the tables used seem to be machine-independent;
  101. he seems to have included a lot of special case code
  102. so that, e.g., ``long`` loads can be used instead of 4 ``char`` loads
  103. when the machine's architecture allows it.
  104. (code obtained from chalmers.se:pub/des)
  105. crack 3.3c package from england:
  106. as in crypt above, the des routine is buried in a loop. it's
  107. also very modified for crypt. his iteration code uses 16k
  108. of tables and appears to be slow.
  109. (code obtained from aem@aber.ac.uk via alt.sources or comp.sources.misc)
  110. ``highly optimized`` and tweaked Kerberos/Athena code (byte-order dependent):
  111. - 165us per encryption (uses 6k worth of tables)
  112. - 478us to set a new key (uses <1k of key tables)
  113. so despite the comments in this code, it was possible to get
  114. faster code AND smaller tables, as well as making the tables
  115. machine-independent.
  116. (code obtained from prep.ai.mit.edu)
  117. UC Berkeley code (depends on machine-endedness):
  118. - 226us per encryption
  119. - 10848us to set a new key
  120. table sizes are unclear, but they don't look very small
  121. (code obtained from wuarchive.wustl.edu)
  122. motivation and history
  123. ======================
  124. a while ago i wanted some des routines and the routines documented on sun's
  125. man pages either didn't exist or dumped core. i had heard of kerberos,
  126. and knew that it used des, so i figured i'd use its routines. but once
  127. i got it and looked at the code, it really set off a lot of pet peeves -
  128. it was too convoluted, the code had been written without taking
  129. advantage of the regular structure of operations such as IP, E, and FP
  130. (i.e. the author didn't sit down and think before coding),
  131. it was excessively slow, the author had attempted to clarify the code
  132. by adding MORE statements to make the data movement more ``consistent``
  133. instead of simplifying his implementation and cutting down on all data
  134. movement (in particular, his use of L1, R1, L2, R2), and it was full of
  135. idiotic ``tweaks`` for particular machines which failed to deliver significant
  136. speedups but which did obfuscate everything. so i took the test data
  137. from his verification program and rewrote everything else.
  138. a while later i ran across the great crypt(3) package mentioned above.
  139. the fact that this guy was computing 2 sboxes per table lookup rather
  140. than one (and using a MUCH larger table in the process) emboldened me to
  141. do the same - it was a trivial change from which i had been scared away
  142. by the larger table size. in his case he didn't realize you don't need to keep
  143. the working data in TWO forms, one for easy use of half the sboxes in
  144. indexing, the other for easy use of the other half; instead you can keep
  145. it in the form for the first half and use a simple rotate to get the other
  146. half. this means i have (almost) half the data manipulation and half
  147. the table size. in fairness though he might be encoding something particular
  148. to crypt(3) in his tables - i didn't check.
  149. i'm glad that i implemented it the way i did, because this C version is
  150. portable (the ifdef's are performance enhancements) and it is faster
  151. than versions hand-written in assembly for the sparc!
  152. porting notes
  153. =============
  154. one thing i did not want to do was write an enormous mess
  155. which depended on endedness and other machine quirks,
  156. and which necessarily produced different code and different lookup tables
  157. for different machines. see the kerberos code for an example
  158. of what i didn't want to do; all their endedness-specific ``optimizations``
  159. obfuscate the code and in the end were slower than a simpler machine
  160. independent approach. however, there are always some portability
  161. considerations of some kind, and i have included some options
  162. for varying numbers of register variables.
  163. perhaps some will still regard the result as a mess!
  164. 1) i assume everything is byte addressable, although i don't actually
  165. depend on the byte order, and that bytes are 8 bits.
  166. i assume word pointers can be freely cast to and from char pointers.
  167. note that 99% of C programs make these assumptions.
  168. i always use unsigned char's if the high bit could be set.
  169. 2) the typedef ``word`` means a 32 bit unsigned integral type.
  170. if ``unsigned long`` is not 32 bits, change the typedef in desCore.h.
  171. i assume sizeof(word) == 4 EVERYWHERE.
  172. the (worst-case) cost of my NOT doing endedness-specific optimizations
  173. in the data loading and storing code surrounding the key iterations
  174. is less than 12%. also, there is the added benefit that
  175. the input and output work areas do not need to be word-aligned.
  176. OPTIONAL performance optimizations
  177. ==================================
  178. 1) you should define one of ``i386,`` ``vax,`` ``mc68000,`` or ``sparc,``
  179. whichever one is closest to the capabilities of your machine.
  180. see the start of desCode.h to see exactly what this selection implies.
  181. note that if you select the wrong one, the des code will still work;
  182. these are just performance tweaks.
  183. 2) for those with functional ``asm`` keywords: you should change the
  184. ROR and ROL macros to use machine rotate instructions if you have them.
  185. this will save 2 instructions and a temporary per use,
  186. or about 32 to 40 instructions per en/decryption.
  187. note that gcc is smart enough to translate the ROL/R macros into
  188. machine rotates!
  189. these optimizations are all rather persnickety, yet with them you should
  190. be able to get performance equal to assembly-coding, except that:
  191. 1) with the lack of a bit rotate operator in C, rotates have to be synthesized
  192. from shifts. so access to ``asm`` will speed things up if your machine
  193. has rotates, as explained above in (3) (not necessary if you use gcc).
  194. 2) if your machine has less than 12 32-bit registers i doubt your compiler will
  195. generate good code.
  196. ``i386`` tries to configure the code for a 386 by only declaring 3 registers
  197. (it appears that gcc can use ebx, esi and edi to hold register variables).
  198. however, if you like assembly coding, the 386 does have 7 32-bit registers,
  199. and if you use ALL of them, use ``scaled by 8`` address modes with displacement
  200. and other tricks, you can get reasonable routines for DesQuickCore... with
  201. about 250 instructions apiece. For DesSmall... it will help to rearrange
  202. des_keymap, i.e., now the sbox # is the high part of the index and
  203. the 6 bits of data is the low part; it helps to exchange these.
  204. since i have no way to conveniently test it i have not provided my
  205. shoehorned 386 version. note that with this release of desCore, gcc is able
  206. to put everything in registers(!), and generate about 370 instructions apiece
  207. for the DesQuickCore... routines!
  208. coding notes
  209. ============
  210. the en/decryption routines each use 6 necessary register variables,
  211. with 4 being actively used at once during the inner iterations.
  212. if you don't have 4 register variables get a new machine.
  213. up to 8 more registers are used to hold constants in some configurations.
  214. i assume that the use of a constant is more expensive than using a register:
  215. a) additionally, i have tried to put the larger constants in registers.
  216. registering priority was by the following:
  217. - anything more than 12 bits (bad for RISC and CISC)
  218. - greater than 127 in value (can't use movq or byte immediate on CISC)
  219. - 9-127 (may not be able to use CISC shift immediate or add/sub quick),
  220. - 1-8 were never registered, being the cheapest constants.
  221. b) the compiler may be too stupid to realize table and table+256 should
  222. be assigned to different constant registers and instead repetitively
  223. do the arithmetic, so i assign these to explicit ``m`` register variables
  224. when possible and helpful.
  225. i assume that indexing is cheaper or equivalent to auto increment/decrement,
  226. where the index is 7 bits unsigned or smaller.
  227. this assumption is reversed for 68k and vax.
  228. i assume that addresses can be cheaply formed from two registers,
  229. or from a register and a small constant.
  230. for the 68000, the ``two registers and small offset`` form is used sparingly.
  231. all index scaling is done explicitly - no hidden shifts by log2(sizeof).
  232. the code is written so that even a dumb compiler
  233. should never need more than one hidden temporary,
  234. increasing the chance that everything will fit in the registers.
  235. KEEP THIS MORE SUBTLE POINT IN MIND IF YOU REWRITE ANYTHING.
  236. (actually, there are some code fragments now which do require two temps,
  237. but fixing it would either break the structure of the macros or
  238. require declaring another temporary).
  239. special efficient data format
  240. ==============================
  241. bits are manipulated in this arrangement most of the time (S7 S5 S3 S1)::
  242. 003130292827xxxx242322212019xxxx161514131211xxxx080706050403xxxx
  243. (the x bits are still there, i'm just emphasizing where the S boxes are).
  244. bits are rotated left 4 when computing S6 S4 S2 S0::
  245. 282726252423xxxx201918171615xxxx121110090807xxxx040302010031xxxx
  246. the rightmost two bits are usually cleared so the lower byte can be used
  247. as an index into an sbox mapping table. the next two x'd bits are set
  248. to various values to access different parts of the tables.
  249. how to use the routines
  250. datatypes:
  251. pointer to 8 byte area of type DesData
  252. used to hold keys and input/output blocks to des.
  253. pointer to 128 byte area of type DesKeys
  254. used to hold full 768-bit key.
  255. must be long-aligned.
  256. DesQuickInit()
  257. call this before using any other routine with ``Quick`` in its name.
  258. it generates the special 64k table these routines need.
  259. DesQuickDone()
  260. frees this table
  261. DesMethod(m, k)
  262. m points to a 128byte block, k points to an 8 byte des key
  263. which must have odd parity (or -1 is returned) and which must
  264. not be a (semi-)weak key (or -2 is returned).
  265. normally DesMethod() returns 0.
  266. m is filled in from k so that when one of the routines below
  267. is called with m, the routine will act like standard des
  268. en/decryption with the key k. if you use DesMethod,
  269. you supply a standard 56bit key; however, if you fill in
  270. m yourself, you will get a 768bit key - but then it won't
  271. be standard. it's 768bits not 1024 because the least significant
  272. two bits of each byte are not used. note that these two bits
  273. will be set to magic constants which speed up the encryption/decryption
  274. on some machines. and yes, each byte controls
  275. a specific sbox during a specific iteration.
  276. you really shouldn't use the 768bit format directly; i should
  277. provide a routine that converts 128 6-bit bytes (specified in
  278. S-box mapping order or something) into the right format for you.
  279. this would entail some byte concatenation and rotation.
  280. Des{Small|Quick}{Fips|Core}{Encrypt|Decrypt}(d, m, s)
  281. performs des on the 8 bytes at s into the 8 bytes at
  282. ``d. (d,s: char *)``.
  283. uses m as a 768bit key as explained above.
  284. the Encrypt|Decrypt choice is obvious.
  285. Fips|Core determines whether a completely standard FIPS initial
  286. and final permutation is done; if not, then the data is loaded
  287. and stored in a nonstandard bit order (FIPS w/o IP/FP).
  288. Fips slows down Quick by 10%, Small by 9%.
  289. Small|Quick determines whether you use the normal routine
  290. or the crazy quick one which gobbles up 64k more of memory.
  291. Small is 50% slower then Quick, but Quick needs 32 times as much
  292. memory. Quick is included for programs that do nothing but DES,
  293. e.g., encryption filters, etc.
  294. Getting it to compile on your machine
  295. =====================================
  296. there are no machine-dependencies in the code (see porting),
  297. except perhaps the ``now()`` macro in desTest.c.
  298. ALL generated tables are machine independent.
  299. you should edit the Makefile with the appropriate optimization flags
  300. for your compiler (MAX optimization).
  301. Speeding up kerberos (and/or its des library)
  302. =============================================
  303. note that i have included a kerberos-compatible interface in desUtil.c
  304. through the functions des_key_sched() and des_ecb_encrypt().
  305. to use these with kerberos or kerberos-compatible code put desCore.a
  306. ahead of the kerberos-compatible library on your linker's command line.
  307. you should not need to #include desCore.h; just include the header
  308. file provided with the kerberos library.
  309. Other uses
  310. ==========
  311. the macros in desCode.h would be very useful for putting inline des
  312. functions in more complicated encryption routines.