Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 .. include:: <isonum.txt>
0003 
0004 ===========================================
0005 Fast & Portable DES encryption & decryption
0006 ===========================================
0007 
0008 .. note::
0009 
0010    Below is the original README file from the descore.shar package,
0011    converted to ReST format.
0012 
0013 ------------------------------------------------------------------------------
0014 
0015 des - fast & portable DES encryption & decryption.
0016 
0017 Copyright |copy| 1992  Dana L. How
0018 
0019 This program is free software; you can redistribute it and/or modify
0020 it under the terms of the GNU Library General Public License as published by
0021 the Free Software Foundation; either version 2 of the License, or
0022 (at your option) any later version.
0023 
0024 This program is distributed in the hope that it will be useful,
0025 but WITHOUT ANY WARRANTY; without even the implied warranty of
0026 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0027 GNU Library General Public License for more details.
0028 
0029 You should have received a copy of the GNU Library General Public License
0030 along with this program; if not, write to the Free Software
0031 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0032 
0033 Author's address: how@isl.stanford.edu
0034 
0035 .. README,v 1.15 1992/05/20 00:25:32 how E
0036 
0037 ==>> To compile after untarring/unsharring, just ``make`` <<==
0038 
0039 This package was designed with the following goals:
0040 
0041 1.      Highest possible encryption/decryption PERFORMANCE.
0042 2.      PORTABILITY to any byte-addressable host with a 32bit unsigned C type
0043 3.      Plug-compatible replacement for KERBEROS's low-level routines.
0044 
0045 This second release includes a number of performance enhancements for
0046 register-starved machines.  My discussions with Richard Outerbridge,
0047 71755.204@compuserve.com, sparked a number of these enhancements.
0048 
0049 To more rapidly understand the code in this package, inspect desSmallFips.i
0050 (created by typing ``make``) BEFORE you tackle desCode.h.  The latter is set
0051 up in a parameterized fashion so it can easily be modified by speed-daemon
0052 hackers in pursuit of that last microsecond.  You will find it more
0053 illuminating to inspect one specific implementation,
0054 and then move on to the common abstract skeleton with this one in mind.
0055 
0056 
0057 performance comparison to other available des code which i could
0058 compile on a SPARCStation 1 (cc -O4, gcc -O2):
0059 
0060 this code (byte-order independent):
0061 
0062   - 30us per encryption (options: 64k tables, no IP/FP)
0063   - 33us per encryption (options: 64k tables, FIPS standard bit ordering)
0064   - 45us per encryption (options:  2k tables, no IP/FP)
0065   - 48us per encryption (options:  2k tables, FIPS standard bit ordering)
0066   - 275us to set a new key (uses 1k of key tables)
0067 
0068         this has the quickest encryption/decryption routines i've seen.
0069         since i was interested in fast des filters rather than crypt(3)
0070         and password cracking, i haven't really bothered yet to speed up
0071         the key setting routine. also, i have no interest in re-implementing
0072         all the other junk in the mit kerberos des library, so i've just
0073         provided my routines with little stub interfaces so they can be
0074         used as drop-in replacements with mit's code or any of the mit-
0075         compatible packages below. (note that the first two timings above
0076         are highly variable because of cache effects).
0077 
0078 kerberos des replacement from australia (version 1.95):
0079 
0080   - 53us per encryption (uses 2k of tables)
0081   - 96us to set a new key (uses 2.25k of key tables)
0082 
0083         so despite the author's inclusion of some of the performance
0084         improvements i had suggested to him, this package's
0085         encryption/decryption is still slower on the sparc and 68000.
0086         more specifically, 19-40% slower on the 68020 and 11-35% slower
0087         on the sparc,  depending on the compiler;
0088         in full gory detail (ALT_ECB is a libdes variant):
0089 
0090         =============== ==============  =============== =================
0091         compiler        machine         desCore libdes  ALT_ECB slower by
0092         =============== ==============  =============== =================
0093         gcc 2.1 -O2     Sun 3/110       304  uS 369.5uS 461.8uS  22%
0094         cc      -O1     Sun 3/110       336  uS 436.6uS 399.3uS  19%
0095         cc      -O2     Sun 3/110       360  uS 532.4uS 505.1uS  40%
0096         cc      -O4     Sun 3/110       365  uS 532.3uS 505.3uS  38%
0097         gcc 2.1 -O2     Sun 4/50         48  uS  53.4uS  57.5uS  11%
0098         cc      -O2     Sun 4/50         48  uS  64.6uS  64.7uS  35%
0099         cc      -O4     Sun 4/50         48  uS  64.7uS  64.9uS  35%
0100         =============== ==============  =============== =================
0101 
0102         (my time measurements are not as accurate as his).
0103 
0104    the comments in my first release of desCore on version 1.92:
0105 
0106    - 68us per encryption (uses 2k of tables)
0107    - 96us to set a new key (uses 2.25k of key tables)
0108 
0109         this is a very nice package which implements the most important
0110         of the optimizations which i did in my encryption routines.
0111         it's a bit weak on common low-level optimizations which is why
0112         it's 39%-106% slower.  because he was interested in fast crypt(3) and
0113         password-cracking applications,  he also used the same ideas to
0114         speed up the key-setting routines with impressive results.
0115         (at some point i may do the same in my package).  he also implements
0116         the rest of the mit des library.
0117 
0118         (code from eay@psych.psy.uq.oz.au via comp.sources.misc)
0119 
0120 fast crypt(3) package from denmark:
0121 
0122         the des routine here is buried inside a loop to do the
0123         crypt function and i didn't feel like ripping it out and measuring
0124         performance. his code takes 26 sparc instructions to compute one
0125         des iteration; above, Quick (64k) takes 21 and Small (2k) takes 37.
0126         he claims to use 280k of tables but the iteration calculation seems
0127         to use only 128k.  his tables and code are machine independent.
0128 
0129         (code from glad@daimi.aau.dk via alt.sources or comp.sources.misc)
0130 
0131 swedish reimplementation of Kerberos des library
0132 
0133   - 108us per encryption (uses 34k worth of tables)
0134   - 134us to set a new key (uses 32k of key tables to get this speed!)
0135 
0136         the tables used seem to be machine-independent;
0137         he seems to have included a lot of special case code
0138         so that, e.g., ``long`` loads can be used instead of 4 ``char`` loads
0139         when the machine's architecture allows it.
0140 
0141         (code obtained from chalmers.se:pub/des)
0142 
0143 crack 3.3c package from england:
0144 
0145         as in crypt above, the des routine is buried in a loop. it's
0146         also very modified for crypt.  his iteration code uses 16k
0147         of tables and appears to be slow.
0148 
0149         (code obtained from aem@aber.ac.uk via alt.sources or comp.sources.misc)
0150 
0151 ``highly optimized`` and tweaked Kerberos/Athena code (byte-order dependent):
0152 
0153   - 165us per encryption (uses 6k worth of tables)
0154   - 478us to set a new key (uses <1k of key tables)
0155 
0156         so despite the comments in this code, it was possible to get
0157         faster code AND smaller tables, as well as making the tables
0158         machine-independent.
0159         (code obtained from prep.ai.mit.edu)
0160 
0161 UC Berkeley code (depends on machine-endedness):
0162   -  226us per encryption
0163   - 10848us to set a new key
0164 
0165         table sizes are unclear, but they don't look very small
0166         (code obtained from wuarchive.wustl.edu)
0167 
0168 
0169 motivation and history
0170 ======================
0171 
0172 a while ago i wanted some des routines and the routines documented on sun's
0173 man pages either didn't exist or dumped core.  i had heard of kerberos,
0174 and knew that it used des,  so i figured i'd use its routines.  but once
0175 i got it and looked at the code,  it really set off a lot of pet peeves -
0176 it was too convoluted, the code had been written without taking
0177 advantage of the regular structure of operations such as IP, E, and FP
0178 (i.e. the author didn't sit down and think before coding),
0179 it was excessively slow,  the author had attempted to clarify the code
0180 by adding MORE statements to make the data movement more ``consistent``
0181 instead of simplifying his implementation and cutting down on all data
0182 movement (in particular, his use of L1, R1, L2, R2), and it was full of
0183 idiotic ``tweaks`` for particular machines which failed to deliver significant
0184 speedups but which did obfuscate everything.  so i took the test data
0185 from his verification program and rewrote everything else.
0186 
0187 a while later i ran across the great crypt(3) package mentioned above.
0188 the fact that this guy was computing 2 sboxes per table lookup rather
0189 than one (and using a MUCH larger table in the process) emboldened me to
0190 do the same - it was a trivial change from which i had been scared away
0191 by the larger table size.  in his case he didn't realize you don't need to keep
0192 the working data in TWO forms, one for easy use of half the sboxes in
0193 indexing, the other for easy use of the other half; instead you can keep
0194 it in the form for the first half and use a simple rotate to get the other
0195 half.  this means i have (almost) half the data manipulation and half
0196 the table size.  in fairness though he might be encoding something particular
0197 to crypt(3) in his tables - i didn't check.
0198 
0199 i'm glad that i implemented it the way i did, because this C version is
0200 portable (the ifdef's are performance enhancements) and it is faster
0201 than versions hand-written in assembly for the sparc!
0202 
0203 
0204 porting notes
0205 =============
0206 
0207 one thing i did not want to do was write an enormous mess
0208 which depended on endedness and other machine quirks,
0209 and which necessarily produced different code and different lookup tables
0210 for different machines.  see the kerberos code for an example
0211 of what i didn't want to do; all their endedness-specific ``optimizations``
0212 obfuscate the code and in the end were slower than a simpler machine
0213 independent approach.  however, there are always some portability
0214 considerations of some kind, and i have included some options
0215 for varying numbers of register variables.
0216 perhaps some will still regard the result as a mess!
0217 
0218 1) i assume everything is byte addressable, although i don't actually
0219    depend on the byte order, and that bytes are 8 bits.
0220    i assume word pointers can be freely cast to and from char pointers.
0221    note that 99% of C programs make these assumptions.
0222    i always use unsigned char's if the high bit could be set.
0223 2) the typedef ``word`` means a 32 bit unsigned integral type.
0224    if ``unsigned long`` is not 32 bits, change the typedef in desCore.h.
0225    i assume sizeof(word) == 4 EVERYWHERE.
0226 
0227 the (worst-case) cost of my NOT doing endedness-specific optimizations
0228 in the data loading and storing code surrounding the key iterations
0229 is less than 12%.  also, there is the added benefit that
0230 the input and output work areas do not need to be word-aligned.
0231 
0232 
0233 OPTIONAL performance optimizations
0234 ==================================
0235 
0236 1) you should define one of ``i386,`` ``vax,`` ``mc68000,`` or ``sparc,``
0237    whichever one is closest to the capabilities of your machine.
0238    see the start of desCode.h to see exactly what this selection implies.
0239    note that if you select the wrong one, the des code will still work;
0240    these are just performance tweaks.
0241 2) for those with functional ``asm`` keywords: you should change the
0242    ROR and ROL macros to use machine rotate instructions if you have them.
0243    this will save 2 instructions and a temporary per use,
0244    or about 32 to 40 instructions per en/decryption.
0245 
0246    note that gcc is smart enough to translate the ROL/R macros into
0247    machine rotates!
0248 
0249 these optimizations are all rather persnickety, yet with them you should
0250 be able to get performance equal to assembly-coding, except that:
0251 
0252 1) with the lack of a bit rotate operator in C, rotates have to be synthesized
0253    from shifts.  so access to ``asm`` will speed things up if your machine
0254    has rotates, as explained above in (3) (not necessary if you use gcc).
0255 2) if your machine has less than 12 32-bit registers i doubt your compiler will
0256    generate good code.
0257 
0258    ``i386`` tries to configure the code for a 386 by only declaring 3 registers
0259    (it appears that gcc can use ebx, esi and edi to hold register variables).
0260    however, if you like assembly coding, the 386 does have 7 32-bit registers,
0261    and if you use ALL of them, use ``scaled by 8`` address modes with displacement
0262    and other tricks, you can get reasonable routines for DesQuickCore... with
0263    about 250 instructions apiece.  For DesSmall... it will help to rearrange
0264    des_keymap, i.e., now the sbox # is the high part of the index and
0265    the 6 bits of data is the low part; it helps to exchange these.
0266 
0267    since i have no way to conveniently test it i have not provided my
0268    shoehorned 386 version.  note that with this release of desCore, gcc is able
0269    to put everything in registers(!), and generate about 370 instructions apiece
0270    for the DesQuickCore... routines!
0271 
0272 coding notes
0273 ============
0274 
0275 the en/decryption routines each use 6 necessary register variables,
0276 with 4 being actively used at once during the inner iterations.
0277 if you don't have 4 register variables get a new machine.
0278 up to 8 more registers are used to hold constants in some configurations.
0279 
0280 i assume that the use of a constant is more expensive than using a register:
0281 
0282 a) additionally, i have tried to put the larger constants in registers.
0283    registering priority was by the following:
0284 
0285         - anything more than 12 bits (bad for RISC and CISC)
0286         - greater than 127 in value (can't use movq or byte immediate on CISC)
0287         - 9-127 (may not be able to use CISC shift immediate or add/sub quick),
0288         - 1-8 were never registered, being the cheapest constants.
0289 
0290 b) the compiler may be too stupid to realize table and table+256 should
0291    be assigned to different constant registers and instead repetitively
0292    do the arithmetic, so i assign these to explicit ``m`` register variables
0293    when possible and helpful.
0294 
0295 i assume that indexing is cheaper or equivalent to auto increment/decrement,
0296 where the index is 7 bits unsigned or smaller.
0297 this assumption is reversed for 68k and vax.
0298 
0299 i assume that addresses can be cheaply formed from two registers,
0300 or from a register and a small constant.
0301 for the 68000, the ``two registers and small offset`` form is used sparingly.
0302 all index scaling is done explicitly - no hidden shifts by log2(sizeof).
0303 
0304 the code is written so that even a dumb compiler
0305 should never need more than one hidden temporary,
0306 increasing the chance that everything will fit in the registers.
0307 KEEP THIS MORE SUBTLE POINT IN MIND IF YOU REWRITE ANYTHING.
0308 
0309 (actually, there are some code fragments now which do require two temps,
0310 but fixing it would either break the structure of the macros or
0311 require declaring another temporary).
0312 
0313 
0314 special efficient data format
0315 ==============================
0316 
0317 bits are manipulated in this arrangement most of the time (S7 S5 S3 S1)::
0318 
0319         003130292827xxxx242322212019xxxx161514131211xxxx080706050403xxxx
0320 
0321 (the x bits are still there, i'm just emphasizing where the S boxes are).
0322 bits are rotated left 4 when computing S6 S4 S2 S0::
0323 
0324         282726252423xxxx201918171615xxxx121110090807xxxx040302010031xxxx
0325 
0326 the rightmost two bits are usually cleared so the lower byte can be used
0327 as an index into an sbox mapping table. the next two x'd bits are set
0328 to various values to access different parts of the tables.
0329 
0330 
0331 how to use the routines
0332 
0333 datatypes:
0334         pointer to 8 byte area of type DesData
0335         used to hold keys and input/output blocks to des.
0336 
0337         pointer to 128 byte area of type DesKeys
0338         used to hold full 768-bit key.
0339         must be long-aligned.
0340 
0341 DesQuickInit()
0342         call this before using any other routine with ``Quick`` in its name.
0343         it generates the special 64k table these routines need.
0344 DesQuickDone()
0345         frees this table
0346 
0347 DesMethod(m, k)
0348         m points to a 128byte block, k points to an 8 byte des key
0349         which must have odd parity (or -1 is returned) and which must
0350         not be a (semi-)weak key (or -2 is returned).
0351         normally DesMethod() returns 0.
0352 
0353         m is filled in from k so that when one of the routines below
0354         is called with m, the routine will act like standard des
0355         en/decryption with the key k. if you use DesMethod,
0356         you supply a standard 56bit key; however, if you fill in
0357         m yourself, you will get a 768bit key - but then it won't
0358         be standard.  it's 768bits not 1024 because the least significant
0359         two bits of each byte are not used.  note that these two bits
0360         will be set to magic constants which speed up the encryption/decryption
0361         on some machines.  and yes, each byte controls
0362         a specific sbox during a specific iteration.
0363 
0364         you really shouldn't use the 768bit format directly;  i should
0365         provide a routine that converts 128 6-bit bytes (specified in
0366         S-box mapping order or something) into the right format for you.
0367         this would entail some byte concatenation and rotation.
0368 
0369 Des{Small|Quick}{Fips|Core}{Encrypt|Decrypt}(d, m, s)
0370         performs des on the 8 bytes at s into the 8 bytes at
0371         ``d. (d,s: char *)``.
0372 
0373         uses m as a 768bit key as explained above.
0374 
0375         the Encrypt|Decrypt choice is obvious.
0376 
0377         Fips|Core determines whether a completely standard FIPS initial
0378         and final permutation is done; if not, then the data is loaded
0379         and stored in a nonstandard bit order (FIPS w/o IP/FP).
0380 
0381         Fips slows down Quick by 10%, Small by 9%.
0382 
0383         Small|Quick determines whether you use the normal routine
0384         or the crazy quick one which gobbles up 64k more of memory.
0385         Small is 50% slower then Quick, but Quick needs 32 times as much
0386         memory.  Quick is included for programs that do nothing but DES,
0387         e.g., encryption filters, etc.
0388 
0389 
0390 Getting it to compile on your machine
0391 =====================================
0392 
0393 there are no machine-dependencies in the code (see porting),
0394 except perhaps the ``now()`` macro in desTest.c.
0395 ALL generated tables are machine independent.
0396 you should edit the Makefile with the appropriate optimization flags
0397 for your compiler (MAX optimization).
0398 
0399 
0400 Speeding up kerberos (and/or its des library)
0401 =============================================
0402 
0403 note that i have included a kerberos-compatible interface in desUtil.c
0404 through the functions des_key_sched() and des_ecb_encrypt().
0405 to use these with kerberos or kerberos-compatible code put desCore.a
0406 ahead of the kerberos-compatible library on your linker's command line.
0407 you should not need to #include desCore.h;  just include the header
0408 file provided with the kerberos library.
0409 
0410 Other uses
0411 ==========
0412 
0413 the macros in desCode.h would be very useful for putting inline des
0414 functions in more complicated encryption routines.