des-586.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #!/usr/local/bin/perl
  2. #
  3. # The inner loop instruction sequence and the IP/FP modifications are from
  4. # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
  5. #
  6. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  7. push(@INC,"${dir}","${dir}../../perlasm");
  8. require "x86asm.pl";
  9. require "cbc.pl";
  10. require "desboth.pl";
  11. # base code is in microsft
  12. # op dest, source
  13. # format.
  14. #
  15. &asm_init($ARGV[0],"des-586.pl");
  16. $L="edi";
  17. $R="esi";
  18. $trans="ebp";
  19. $small_footprint=1 if (grep(/\-DOPENSSL_SMALL_FOOTPRINT/,@ARGV));
  20. # one can discuss setting this variable to 1 unconditionally, as
  21. # the folded loop is only 3% slower than unrolled, but >7 times smaller
  22. &public_label("DES_SPtrans");
  23. &static_label("des_sptrans");
  24. &DES_encrypt_internal();
  25. &DES_decrypt_internal();
  26. &DES_encrypt("DES_encrypt1",1);
  27. &DES_encrypt("DES_encrypt2",0);
  28. &DES_encrypt3("DES_encrypt3",1);
  29. &DES_encrypt3("DES_decrypt3",0);
  30. &cbc("DES_ncbc_encrypt","DES_encrypt1","DES_encrypt1",0,4,5,3,5,-1);
  31. &cbc("DES_ede3_cbc_encrypt","DES_encrypt3","DES_decrypt3",0,6,7,3,4,5);
  32. &DES_SPtrans();
  33. &asm_finish();
  34. sub DES_encrypt_internal()
  35. {
  36. &function_begin_B("_x86_DES_encrypt");
  37. if ($small_footprint)
  38. {
  39. &lea("edx",&DWP(128,"ecx"));
  40. &push("edx");
  41. &push("ecx");
  42. &set_label("eloop");
  43. &D_ENCRYPT(0,$L,$R,0,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  44. &comment("");
  45. &D_ENCRYPT(1,$R,$L,2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  46. &comment("");
  47. &add("ecx",16);
  48. &cmp("ecx",&swtmp(1));
  49. &mov(&swtmp(0),"ecx");
  50. &jb(&label("eloop"));
  51. &add("esp",8);
  52. }
  53. else
  54. {
  55. &push("ecx");
  56. for ($i=0; $i<16; $i+=2)
  57. {
  58. &comment("Round $i");
  59. &D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  60. &comment("Round ".sprintf("%d",$i+1));
  61. &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  62. }
  63. &add("esp",4);
  64. }
  65. &ret();
  66. &function_end_B("_x86_DES_encrypt");
  67. }
  68. sub DES_decrypt_internal()
  69. {
  70. &function_begin_B("_x86_DES_decrypt");
  71. if ($small_footprint)
  72. {
  73. &push("ecx");
  74. &lea("ecx",&DWP(128,"ecx"));
  75. &push("ecx");
  76. &set_label("dloop");
  77. &D_ENCRYPT(0,$L,$R,-2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  78. &comment("");
  79. &D_ENCRYPT(1,$R,$L,-4,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  80. &comment("");
  81. &sub("ecx",16);
  82. &cmp("ecx",&swtmp(1));
  83. &mov(&swtmp(0),"ecx");
  84. &ja(&label("dloop"));
  85. &add("esp",8);
  86. }
  87. else
  88. {
  89. &push("ecx");
  90. for ($i=15; $i>0; $i-=2)
  91. {
  92. &comment("Round $i");
  93. &D_ENCRYPT(15-$i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  94. &comment("Round ".sprintf("%d",$i-1));
  95. &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
  96. }
  97. &add("esp",4);
  98. }
  99. &ret();
  100. &function_end_B("_x86_DES_decrypt");
  101. }
  102. sub DES_encrypt
  103. {
  104. local($name,$do_ip)=@_;
  105. &function_begin_B($name);
  106. &push("esi");
  107. &push("edi");
  108. &comment("");
  109. &comment("Load the 2 words");
  110. if ($do_ip)
  111. {
  112. &mov($R,&wparam(0));
  113. &xor( "ecx", "ecx" );
  114. &push("ebx");
  115. &push("ebp");
  116. &mov("eax",&DWP(0,$R,"",0));
  117. &mov("ebx",&wparam(2)); # get encrypt flag
  118. &mov($L,&DWP(4,$R,"",0));
  119. &comment("");
  120. &comment("IP");
  121. &IP_new("eax",$L,$R,3);
  122. }
  123. else
  124. {
  125. &mov("eax",&wparam(0));
  126. &xor( "ecx", "ecx" );
  127. &push("ebx");
  128. &push("ebp");
  129. &mov($R,&DWP(0,"eax","",0));
  130. &mov("ebx",&wparam(2)); # get encrypt flag
  131. &rotl($R,3);
  132. &mov($L,&DWP(4,"eax","",0));
  133. &rotl($L,3);
  134. }
  135. # PIC-ification:-)
  136. &call (&label("pic_point"));
  137. &set_label("pic_point");
  138. &blindpop($trans);
  139. &lea ($trans,&DWP(&label("des_sptrans")."-".&label("pic_point"),$trans));
  140. &mov( "ecx", &wparam(1) );
  141. &cmp("ebx","0");
  142. &je(&label("decrypt"));
  143. &call("_x86_DES_encrypt");
  144. &jmp(&label("done"));
  145. &set_label("decrypt");
  146. &call("_x86_DES_decrypt");
  147. &set_label("done");
  148. if ($do_ip)
  149. {
  150. &comment("");
  151. &comment("FP");
  152. &mov("edx",&wparam(0));
  153. &FP_new($L,$R,"eax",3);
  154. &mov(&DWP(0,"edx","",0),"eax");
  155. &mov(&DWP(4,"edx","",0),$R);
  156. }
  157. else
  158. {
  159. &comment("");
  160. &comment("Fixup");
  161. &rotr($L,3); # r
  162. &mov("eax",&wparam(0));
  163. &rotr($R,3); # l
  164. &mov(&DWP(0,"eax","",0),$L);
  165. &mov(&DWP(4,"eax","",0),$R);
  166. }
  167. &pop("ebp");
  168. &pop("ebx");
  169. &pop("edi");
  170. &pop("esi");
  171. &ret();
  172. &function_end_B($name);
  173. }
  174. sub D_ENCRYPT
  175. {
  176. local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t,$wp1)=@_;
  177. &mov( $u, &DWP(&n2a($S*4),$tmp2,"",0));
  178. &xor( $tmp1, $tmp1);
  179. &mov( $t, &DWP(&n2a(($S+1)*4),$tmp2,"",0));
  180. &xor( $u, $R);
  181. &xor( $tmp2, $tmp2);
  182. &xor( $t, $R);
  183. &and( $u, "0xfcfcfcfc" );
  184. &and( $t, "0xcfcfcfcf" );
  185. &movb( &LB($tmp1), &LB($u) );
  186. &movb( &LB($tmp2), &HB($u) );
  187. &rotr( $t, 4 );
  188. &xor( $L, &DWP(" ",$trans,$tmp1,0));
  189. &movb( &LB($tmp1), &LB($t) );
  190. &xor( $L, &DWP("0x200",$trans,$tmp2,0));
  191. &movb( &LB($tmp2), &HB($t) );
  192. &shr( $u, 16);
  193. &xor( $L, &DWP("0x100",$trans,$tmp1,0));
  194. &movb( &LB($tmp1), &HB($u) );
  195. &shr( $t, 16);
  196. &xor( $L, &DWP("0x300",$trans,$tmp2,0));
  197. &movb( &LB($tmp2), &HB($t) );
  198. &and( $u, "0xff" );
  199. &and( $t, "0xff" );
  200. &xor( $L, &DWP("0x600",$trans,$tmp1,0));
  201. &xor( $L, &DWP("0x700",$trans,$tmp2,0));
  202. &mov( $tmp2, $wp1 );
  203. &xor( $L, &DWP("0x400",$trans,$u,0));
  204. &xor( $L, &DWP("0x500",$trans,$t,0));
  205. }
  206. sub n2a
  207. {
  208. sprintf("%d",$_[0]);
  209. }
  210. # now has a side affect of rotating $a by $shift
  211. sub R_PERM_OP
  212. {
  213. local($a,$b,$tt,$shift,$mask,$last)=@_;
  214. &rotl( $a, $shift ) if ($shift != 0);
  215. &mov( $tt, $a );
  216. &xor( $a, $b );
  217. &and( $a, $mask );
  218. # This can never succeed, and besides it is difficult to see what the
  219. # idea was - Ben 13 Feb 99
  220. if (!$last eq $b)
  221. {
  222. &xor( $b, $a );
  223. &xor( $tt, $a );
  224. }
  225. else
  226. {
  227. &xor( $tt, $a );
  228. &xor( $b, $a );
  229. }
  230. &comment("");
  231. }
  232. sub IP_new
  233. {
  234. local($l,$r,$tt,$lr)=@_;
  235. &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
  236. &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
  237. &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
  238. &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
  239. &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
  240. if ($lr != 3)
  241. {
  242. if (($lr-3) < 0)
  243. { &rotr($tt, 3-$lr); }
  244. else { &rotl($tt, $lr-3); }
  245. }
  246. if ($lr != 2)
  247. {
  248. if (($lr-2) < 0)
  249. { &rotr($r, 2-$lr); }
  250. else { &rotl($r, $lr-2); }
  251. }
  252. }
  253. sub FP_new
  254. {
  255. local($l,$r,$tt,$lr)=@_;
  256. if ($lr != 2)
  257. {
  258. if (($lr-2) < 0)
  259. { &rotl($r, 2-$lr); }
  260. else { &rotr($r, $lr-2); }
  261. }
  262. if ($lr != 3)
  263. {
  264. if (($lr-3) < 0)
  265. { &rotl($l, 3-$lr); }
  266. else { &rotr($l, $lr-3); }
  267. }
  268. &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
  269. &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
  270. &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
  271. &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
  272. &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
  273. &rotr($tt , 4);
  274. }
  275. sub DES_SPtrans
  276. {
  277. &set_label("DES_SPtrans",64);
  278. &set_label("des_sptrans");
  279. &data_word(0x02080800, 0x00080000, 0x02000002, 0x02080802);
  280. &data_word(0x02000000, 0x00080802, 0x00080002, 0x02000002);
  281. &data_word(0x00080802, 0x02080800, 0x02080000, 0x00000802);
  282. &data_word(0x02000802, 0x02000000, 0x00000000, 0x00080002);
  283. &data_word(0x00080000, 0x00000002, 0x02000800, 0x00080800);
  284. &data_word(0x02080802, 0x02080000, 0x00000802, 0x02000800);
  285. &data_word(0x00000002, 0x00000800, 0x00080800, 0x02080002);
  286. &data_word(0x00000800, 0x02000802, 0x02080002, 0x00000000);
  287. &data_word(0x00000000, 0x02080802, 0x02000800, 0x00080002);
  288. &data_word(0x02080800, 0x00080000, 0x00000802, 0x02000800);
  289. &data_word(0x02080002, 0x00000800, 0x00080800, 0x02000002);
  290. &data_word(0x00080802, 0x00000002, 0x02000002, 0x02080000);
  291. &data_word(0x02080802, 0x00080800, 0x02080000, 0x02000802);
  292. &data_word(0x02000000, 0x00000802, 0x00080002, 0x00000000);
  293. &data_word(0x00080000, 0x02000000, 0x02000802, 0x02080800);
  294. &data_word(0x00000002, 0x02080002, 0x00000800, 0x00080802);
  295. # nibble 1
  296. &data_word(0x40108010, 0x00000000, 0x00108000, 0x40100000);
  297. &data_word(0x40000010, 0x00008010, 0x40008000, 0x00108000);
  298. &data_word(0x00008000, 0x40100010, 0x00000010, 0x40008000);
  299. &data_word(0x00100010, 0x40108000, 0x40100000, 0x00000010);
  300. &data_word(0x00100000, 0x40008010, 0x40100010, 0x00008000);
  301. &data_word(0x00108010, 0x40000000, 0x00000000, 0x00100010);
  302. &data_word(0x40008010, 0x00108010, 0x40108000, 0x40000010);
  303. &data_word(0x40000000, 0x00100000, 0x00008010, 0x40108010);
  304. &data_word(0x00100010, 0x40108000, 0x40008000, 0x00108010);
  305. &data_word(0x40108010, 0x00100010, 0x40000010, 0x00000000);
  306. &data_word(0x40000000, 0x00008010, 0x00100000, 0x40100010);
  307. &data_word(0x00008000, 0x40000000, 0x00108010, 0x40008010);
  308. &data_word(0x40108000, 0x00008000, 0x00000000, 0x40000010);
  309. &data_word(0x00000010, 0x40108010, 0x00108000, 0x40100000);
  310. &data_word(0x40100010, 0x00100000, 0x00008010, 0x40008000);
  311. &data_word(0x40008010, 0x00000010, 0x40100000, 0x00108000);
  312. # nibble 2
  313. &data_word(0x04000001, 0x04040100, 0x00000100, 0x04000101);
  314. &data_word(0x00040001, 0x04000000, 0x04000101, 0x00040100);
  315. &data_word(0x04000100, 0x00040000, 0x04040000, 0x00000001);
  316. &data_word(0x04040101, 0x00000101, 0x00000001, 0x04040001);
  317. &data_word(0x00000000, 0x00040001, 0x04040100, 0x00000100);
  318. &data_word(0x00000101, 0x04040101, 0x00040000, 0x04000001);
  319. &data_word(0x04040001, 0x04000100, 0x00040101, 0x04040000);
  320. &data_word(0x00040100, 0x00000000, 0x04000000, 0x00040101);
  321. &data_word(0x04040100, 0x00000100, 0x00000001, 0x00040000);
  322. &data_word(0x00000101, 0x00040001, 0x04040000, 0x04000101);
  323. &data_word(0x00000000, 0x04040100, 0x00040100, 0x04040001);
  324. &data_word(0x00040001, 0x04000000, 0x04040101, 0x00000001);
  325. &data_word(0x00040101, 0x04000001, 0x04000000, 0x04040101);
  326. &data_word(0x00040000, 0x04000100, 0x04000101, 0x00040100);
  327. &data_word(0x04000100, 0x00000000, 0x04040001, 0x00000101);
  328. &data_word(0x04000001, 0x00040101, 0x00000100, 0x04040000);
  329. # nibble 3
  330. &data_word(0x00401008, 0x10001000, 0x00000008, 0x10401008);
  331. &data_word(0x00000000, 0x10400000, 0x10001008, 0x00400008);
  332. &data_word(0x10401000, 0x10000008, 0x10000000, 0x00001008);
  333. &data_word(0x10000008, 0x00401008, 0x00400000, 0x10000000);
  334. &data_word(0x10400008, 0x00401000, 0x00001000, 0x00000008);
  335. &data_word(0x00401000, 0x10001008, 0x10400000, 0x00001000);
  336. &data_word(0x00001008, 0x00000000, 0x00400008, 0x10401000);
  337. &data_word(0x10001000, 0x10400008, 0x10401008, 0x00400000);
  338. &data_word(0x10400008, 0x00001008, 0x00400000, 0x10000008);
  339. &data_word(0x00401000, 0x10001000, 0x00000008, 0x10400000);
  340. &data_word(0x10001008, 0x00000000, 0x00001000, 0x00400008);
  341. &data_word(0x00000000, 0x10400008, 0x10401000, 0x00001000);
  342. &data_word(0x10000000, 0x10401008, 0x00401008, 0x00400000);
  343. &data_word(0x10401008, 0x00000008, 0x10001000, 0x00401008);
  344. &data_word(0x00400008, 0x00401000, 0x10400000, 0x10001008);
  345. &data_word(0x00001008, 0x10000000, 0x10000008, 0x10401000);
  346. # nibble 4
  347. &data_word(0x08000000, 0x00010000, 0x00000400, 0x08010420);
  348. &data_word(0x08010020, 0x08000400, 0x00010420, 0x08010000);
  349. &data_word(0x00010000, 0x00000020, 0x08000020, 0x00010400);
  350. &data_word(0x08000420, 0x08010020, 0x08010400, 0x00000000);
  351. &data_word(0x00010400, 0x08000000, 0x00010020, 0x00000420);
  352. &data_word(0x08000400, 0x00010420, 0x00000000, 0x08000020);
  353. &data_word(0x00000020, 0x08000420, 0x08010420, 0x00010020);
  354. &data_word(0x08010000, 0x00000400, 0x00000420, 0x08010400);
  355. &data_word(0x08010400, 0x08000420, 0x00010020, 0x08010000);
  356. &data_word(0x00010000, 0x00000020, 0x08000020, 0x08000400);
  357. &data_word(0x08000000, 0x00010400, 0x08010420, 0x00000000);
  358. &data_word(0x00010420, 0x08000000, 0x00000400, 0x00010020);
  359. &data_word(0x08000420, 0x00000400, 0x00000000, 0x08010420);
  360. &data_word(0x08010020, 0x08010400, 0x00000420, 0x00010000);
  361. &data_word(0x00010400, 0x08010020, 0x08000400, 0x00000420);
  362. &data_word(0x00000020, 0x00010420, 0x08010000, 0x08000020);
  363. # nibble 5
  364. &data_word(0x80000040, 0x00200040, 0x00000000, 0x80202000);
  365. &data_word(0x00200040, 0x00002000, 0x80002040, 0x00200000);
  366. &data_word(0x00002040, 0x80202040, 0x00202000, 0x80000000);
  367. &data_word(0x80002000, 0x80000040, 0x80200000, 0x00202040);
  368. &data_word(0x00200000, 0x80002040, 0x80200040, 0x00000000);
  369. &data_word(0x00002000, 0x00000040, 0x80202000, 0x80200040);
  370. &data_word(0x80202040, 0x80200000, 0x80000000, 0x00002040);
  371. &data_word(0x00000040, 0x00202000, 0x00202040, 0x80002000);
  372. &data_word(0x00002040, 0x80000000, 0x80002000, 0x00202040);
  373. &data_word(0x80202000, 0x00200040, 0x00000000, 0x80002000);
  374. &data_word(0x80000000, 0x00002000, 0x80200040, 0x00200000);
  375. &data_word(0x00200040, 0x80202040, 0x00202000, 0x00000040);
  376. &data_word(0x80202040, 0x00202000, 0x00200000, 0x80002040);
  377. &data_word(0x80000040, 0x80200000, 0x00202040, 0x00000000);
  378. &data_word(0x00002000, 0x80000040, 0x80002040, 0x80202000);
  379. &data_word(0x80200000, 0x00002040, 0x00000040, 0x80200040);
  380. # nibble 6
  381. &data_word(0x00004000, 0x00000200, 0x01000200, 0x01000004);
  382. &data_word(0x01004204, 0x00004004, 0x00004200, 0x00000000);
  383. &data_word(0x01000000, 0x01000204, 0x00000204, 0x01004000);
  384. &data_word(0x00000004, 0x01004200, 0x01004000, 0x00000204);
  385. &data_word(0x01000204, 0x00004000, 0x00004004, 0x01004204);
  386. &data_word(0x00000000, 0x01000200, 0x01000004, 0x00004200);
  387. &data_word(0x01004004, 0x00004204, 0x01004200, 0x00000004);
  388. &data_word(0x00004204, 0x01004004, 0x00000200, 0x01000000);
  389. &data_word(0x00004204, 0x01004000, 0x01004004, 0x00000204);
  390. &data_word(0x00004000, 0x00000200, 0x01000000, 0x01004004);
  391. &data_word(0x01000204, 0x00004204, 0x00004200, 0x00000000);
  392. &data_word(0x00000200, 0x01000004, 0x00000004, 0x01000200);
  393. &data_word(0x00000000, 0x01000204, 0x01000200, 0x00004200);
  394. &data_word(0x00000204, 0x00004000, 0x01004204, 0x01000000);
  395. &data_word(0x01004200, 0x00000004, 0x00004004, 0x01004204);
  396. &data_word(0x01000004, 0x01004200, 0x01004000, 0x00004004);
  397. # nibble 7
  398. &data_word(0x20800080, 0x20820000, 0x00020080, 0x00000000);
  399. &data_word(0x20020000, 0x00800080, 0x20800000, 0x20820080);
  400. &data_word(0x00000080, 0x20000000, 0x00820000, 0x00020080);
  401. &data_word(0x00820080, 0x20020080, 0x20000080, 0x20800000);
  402. &data_word(0x00020000, 0x00820080, 0x00800080, 0x20020000);
  403. &data_word(0x20820080, 0x20000080, 0x00000000, 0x00820000);
  404. &data_word(0x20000000, 0x00800000, 0x20020080, 0x20800080);
  405. &data_word(0x00800000, 0x00020000, 0x20820000, 0x00000080);
  406. &data_word(0x00800000, 0x00020000, 0x20000080, 0x20820080);
  407. &data_word(0x00020080, 0x20000000, 0x00000000, 0x00820000);
  408. &data_word(0x20800080, 0x20020080, 0x20020000, 0x00800080);
  409. &data_word(0x20820000, 0x00000080, 0x00800080, 0x20020000);
  410. &data_word(0x20820080, 0x00800000, 0x20800000, 0x20000080);
  411. &data_word(0x00820000, 0x00020080, 0x20020080, 0x20800000);
  412. &data_word(0x00000080, 0x20820000, 0x00820080, 0x00000000);
  413. &data_word(0x20000000, 0x20800080, 0x00020000, 0x00820080);
  414. }