home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / msdos / programm / 9208 < prev    next >
Encoding:
Text File  |  1992-09-09  |  20.0 KB  |  424 lines

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