readme 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. First up, let me say I don't like writing in assembler. It is not portable,
  2. dependant on the particular CPU architecture release and is generally a pig
  3. to debug and get right. Having said that, the x86 architecture is probably
  4. the most important for speed due to number of boxes and since
  5. it appears to be the worst architecture to to get
  6. good C compilers for. So due to this, I have lowered myself to do
  7. assembler for the inner DES routines in libdes :-).
  8. The file to implement in assembler is des_enc.c. Replace the following
  9. 4 functions
  10. des_encrypt1(DES_LONG data[2],des_key_schedule ks, int encrypt);
  11. des_encrypt2(DES_LONG data[2],des_key_schedule ks, int encrypt);
  12. des_encrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  13. des_decrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  14. They encrypt/decrypt the 64 bits held in 'data' using
  15. the 'ks' key schedules. The only difference between the 4 functions is that
  16. des_encrypt2() does not perform IP() or FP() on the data (this is an
  17. optimization for when doing triple DES and des_encrypt3() and des_decrypt3()
  18. perform triple des. The triple DES routines are in here because it does
  19. make a big difference to have them located near the des_encrypt2 function
  20. at link time..
  21. Now as we all know, there are lots of different operating systems running on
  22. x86 boxes, and unfortunately they normally try to make sure their assembler
  23. formating is not the same as the other peoples.
  24. The 4 main formats I know of are
  25. Microsoft Windows 95/Windows NT
  26. Elf Includes Linux and FreeBSD(?).
  27. a.out The older Linux.
  28. Solaris Same as Elf but different comments :-(.
  29. Now I was not overly keen to write 4 different copies of the same code,
  30. so I wrote a few perl routines to output the correct assembler, given
  31. a target assembler type. This code is ugly and is just a hack.
  32. The libraries are x86unix.pl and x86ms.pl.
  33. des586.pl, des686.pl and des-som[23].pl are the programs to actually
  34. generate the assembler.
  35. So to generate elf assembler
  36. perl des-som3.pl elf >dx86-elf.s
  37. For Windows 95/NT
  38. perl des-som2.pl win32 >win32.asm
  39. [ update 4 Jan 1996 ]
  40. I have added another way to do things.
  41. perl des-som3.pl cpp >dx86-cpp.s
  42. generates a file that will be included by dx86unix.cpp when it is compiled.
  43. To build for elf, a.out, solaris, bsdi etc,
  44. cc -E -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o
  45. cc -E -DSOL asm/dx86unix.cpp | as -o asm/dx86-sol.o
  46. cc -E -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o
  47. cc -E -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o
  48. This was done to cut down the number of files in the distribution.
  49. Now the ugly part. I acquired my copy of Intels
  50. "Optimization's For Intel's 32-Bit Processors" and found a few interesting
  51. things. First, the aim of the exersize is to 'extract' one byte at a time
  52. from a word and do an array lookup. This involves getting the byte from
  53. the 4 locations in the word and moving it to a new word and doing the lookup.
  54. The most obvious way to do this is
  55. xor eax, eax # clear word
  56. movb al, cl # get low byte
  57. xor edi DWORD PTR 0x100+des_SP[eax] # xor in word
  58. movb al, ch # get next byte
  59. xor edi DWORD PTR 0x300+des_SP[eax] # xor in word
  60. shr ecx 16
  61. which seems ok. For the pentium, this system appears to be the best.
  62. One has to do instruction interleaving to keep both functional units
  63. operating, but it is basically very efficient.
  64. Now the crunch. When a full register is used after a partial write, eg.
  65. mov al, cl
  66. xor edi, DWORD PTR 0x100+des_SP[eax]
  67. 386 - 1 cycle stall
  68. 486 - 1 cycle stall
  69. 586 - 0 cycle stall
  70. 686 - at least 7 cycle stall (page 22 of the above mentioned document).
  71. So the technique that produces the best results on a pentium, according to
  72. the documentation, will produce hideous results on a pentium pro.
  73. To get around this, des686.pl will generate code that is not as fast on
  74. a pentium, should be very good on a pentium pro.
  75. mov eax, ecx # copy word
  76. shr ecx, 8 # line up next byte
  77. and eax, 0fch # mask byte
  78. xor edi DWORD PTR 0x100+des_SP[eax] # xor in array lookup
  79. mov eax, ecx # get word
  80. shr ecx 8 # line up next byte
  81. and eax, 0fch # mask byte
  82. xor edi DWORD PTR 0x300+des_SP[eax] # xor in array lookup
  83. Due to the execution units in the pentium, this actually works quite well.
  84. For a pentium pro it should be very good. This is the type of output
  85. Visual C++ generates.
  86. There is a third option. instead of using
  87. mov al, ch
  88. which is bad on the pentium pro, one may be able to use
  89. movzx eax, ch
  90. which may not incur the partial write penalty. On the pentium,
  91. this instruction takes 4 cycles so is not worth using but on the
  92. pentium pro it appears it may be worth while. I need access to one to
  93. experiment :-).
  94. eric (20 Oct 1996)
  95. 22 Nov 1996 - I have asked people to run the 2 different version on pentium
  96. pros and it appears that the intel documentation is wrong. The
  97. mov al,bh is still faster on a pentium pro, so just use the des586.pl
  98. install des686.pl
  99. 3 Dec 1996 - I added des_encrypt3/des_decrypt3 because I have moved these
  100. functions into des_enc.c because it does make a massive performance
  101. difference on some boxes to have the functions code located close to
  102. the des_encrypt2() function.
  103. 9 Jan 1997 - des-som2.pl is now the correct perl script to use for
  104. pentiums. It contains an inner loop from
  105. Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk> which does raw ecb DES calls at
  106. 273,000 per second. He had a previous version at 250,000 and the best
  107. I was able to get was 203,000. The content has not changed, this is all
  108. due to instruction sequencing (and actual instructions choice) which is able
  109. to keep both functional units of the pentium going.
  110. We may have lost the ugly register usage restrictions when x86 went 32 bit
  111. but for the pentium it has been replaced by evil instruction ordering tricks.
  112. 13 Jan 1997 - des-som3.pl, more optimizations from Svend Olaf.
  113. raw DES at 281,000 per second on a pentium 100.