2
0

des-586.pl 15 KB

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