des-586.pl 15 KB

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