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