You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

350 rivejä
8.9 KiB

  1. #!/usr/local/bin/perl
  2. # void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
  3. # des_cblock (*input);
  4. # des_cblock (*output);
  5. # long length;
  6. # des_key_schedule schedule;
  7. # des_cblock (*ivec);
  8. # int enc;
  9. #
  10. # calls
  11. # des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  12. #
  13. #&cbc("des_ncbc_encrypt","des_encrypt",0);
  14. #&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",
  15. # 1,4,5,3,5,-1);
  16. #&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",
  17. # 0,4,5,3,5,-1);
  18. #&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",
  19. # 0,6,7,3,4,5);
  20. #
  21. # When doing a cipher that needs bigendian order,
  22. # for encrypt, the iv is kept in bigendian form,
  23. # while for decrypt, it is kept in little endian.
  24. sub cbc
  25. {
  26. local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_;
  27. # name is the function name
  28. # enc_func and dec_func and the functions to call for encrypt/decrypt
  29. # swap is true if byte order needs to be reversed
  30. # iv_off is parameter number for the iv
  31. # enc_off is parameter number for the encrypt/decrypt flag
  32. # p1,p2,p3 are the offsets for parameters to be passed to the
  33. # underlying calls.
  34. &function_begin_B($name,"");
  35. &comment("");
  36. $in="esi";
  37. $out="edi";
  38. $count="ebp";
  39. &push("ebp");
  40. &push("ebx");
  41. &push("esi");
  42. &push("edi");
  43. $data_off=4;
  44. $data_off+=4 if ($p1 > 0);
  45. $data_off+=4 if ($p2 > 0);
  46. $data_off+=4 if ($p3 > 0);
  47. &mov($count, &wparam(2)); # length
  48. &comment("getting iv ptr from parameter $iv_off");
  49. &mov("ebx", &wparam($iv_off)); # Get iv ptr
  50. &mov($in, &DWP(0,"ebx","",0));# iv[0]
  51. &mov($out, &DWP(4,"ebx","",0));# iv[1]
  52. &push($out);
  53. &push($in);
  54. &push($out); # used in decrypt for iv[1]
  55. &push($in); # used in decrypt for iv[0]
  56. &mov("ebx", "esp"); # This is the address of tin[2]
  57. &mov($in, &wparam(0)); # in
  58. &mov($out, &wparam(1)); # out
  59. # We have loaded them all, how lets push things
  60. &comment("getting encrypt flag from parameter $enc_off");
  61. &mov("ecx", &wparam($enc_off)); # Get enc flag
  62. if ($p3 > 0)
  63. {
  64. &comment("get and push parameter $p3");
  65. if ($enc_off != $p3)
  66. { &mov("eax", &wparam($p3)); &push("eax"); }
  67. else { &push("ecx"); }
  68. }
  69. if ($p2 > 0)
  70. {
  71. &comment("get and push parameter $p2");
  72. if ($enc_off != $p2)
  73. { &mov("eax", &wparam($p2)); &push("eax"); }
  74. else { &push("ecx"); }
  75. }
  76. if ($p1 > 0)
  77. {
  78. &comment("get and push parameter $p1");
  79. if ($enc_off != $p1)
  80. { &mov("eax", &wparam($p1)); &push("eax"); }
  81. else { &push("ecx"); }
  82. }
  83. &push("ebx"); # push data/iv
  84. &cmp("ecx",0);
  85. &jz(&label("decrypt"));
  86. &and($count,0xfffffff8);
  87. &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0]
  88. &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1]
  89. &jz(&label("encrypt_finish"));
  90. #############################################################
  91. &set_label("encrypt_loop");
  92. # encrypt start
  93. # "eax" and "ebx" hold iv (or the last cipher text)
  94. &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes
  95. &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes
  96. &xor("eax", "ecx");
  97. &xor("ebx", "edx");
  98. &bswap("eax") if $swap;
  99. &bswap("ebx") if $swap;
  100. &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
  101. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  102. &call($enc_func);
  103. &mov("eax", &DWP($data_off,"esp","",0));
  104. &mov("ebx", &DWP($data_off+4,"esp","",0));
  105. &bswap("eax") if $swap;
  106. &bswap("ebx") if $swap;
  107. &mov(&DWP(0,$out,"",0),"eax");
  108. &mov(&DWP(4,$out,"",0),"ebx");
  109. # eax and ebx are the next iv.
  110. &add($in, 8);
  111. &add($out, 8);
  112. &sub($count, 8);
  113. &jnz(&label("encrypt_loop"));
  114. ###################################################################3
  115. &set_label("encrypt_finish");
  116. &mov($count, &wparam(2)); # length
  117. &and($count, 7);
  118. &jz(&label("finish"));
  119. &call(&label("PIC_point"));
  120. &set_label("PIC_point");
  121. &blindpop("edx");
  122. &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx"));
  123. &mov($count,&DWP(0,"ecx",$count,4));
  124. &add($count,"edx");
  125. &xor("ecx","ecx");
  126. &xor("edx","edx");
  127. #&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4));
  128. &jmp_ptr($count);
  129. &set_label("ej7");
  130. &movb(&HB("edx"), &BP(6,$in,"",0));
  131. &shl("edx",8);
  132. &set_label("ej6");
  133. &movb(&HB("edx"), &BP(5,$in,"",0));
  134. &set_label("ej5");
  135. &movb(&LB("edx"), &BP(4,$in,"",0));
  136. &set_label("ej4");
  137. &mov("ecx", &DWP(0,$in,"",0));
  138. &jmp(&label("ejend"));
  139. &set_label("ej3");
  140. &movb(&HB("ecx"), &BP(2,$in,"",0));
  141. &shl("ecx",8);
  142. &set_label("ej2");
  143. &movb(&HB("ecx"), &BP(1,$in,"",0));
  144. &set_label("ej1");
  145. &movb(&LB("ecx"), &BP(0,$in,"",0));
  146. &set_label("ejend");
  147. &xor("eax", "ecx");
  148. &xor("ebx", "edx");
  149. &bswap("eax") if $swap;
  150. &bswap("ebx") if $swap;
  151. &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
  152. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  153. &call($enc_func);
  154. &mov("eax", &DWP($data_off,"esp","",0));
  155. &mov("ebx", &DWP($data_off+4,"esp","",0));
  156. &bswap("eax") if $swap;
  157. &bswap("ebx") if $swap;
  158. &mov(&DWP(0,$out,"",0),"eax");
  159. &mov(&DWP(4,$out,"",0),"ebx");
  160. &jmp(&label("finish"));
  161. #############################################################
  162. #############################################################
  163. &set_label("decrypt",1);
  164. # decrypt start
  165. &and($count,0xfffffff8);
  166. # The next 2 instructions are only for if the jz is taken
  167. &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
  168. &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
  169. &jz(&label("decrypt_finish"));
  170. &set_label("decrypt_loop");
  171. &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
  172. &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
  173. &bswap("eax") if $swap;
  174. &bswap("ebx") if $swap;
  175. &mov(&DWP($data_off,"esp","",0), "eax"); # put back
  176. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  177. &call($dec_func);
  178. &mov("eax", &DWP($data_off,"esp","",0)); # get return
  179. &mov("ebx", &DWP($data_off+4,"esp","",0)); #
  180. &bswap("eax") if $swap;
  181. &bswap("ebx") if $swap;
  182. &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
  183. &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
  184. &xor("ecx", "eax");
  185. &xor("edx", "ebx");
  186. &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
  187. &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
  188. &mov(&DWP(0,$out,"",0),"ecx");
  189. &mov(&DWP(4,$out,"",0),"edx");
  190. &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
  191. &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
  192. &add($in, 8);
  193. &add($out, 8);
  194. &sub($count, 8);
  195. &jnz(&label("decrypt_loop"));
  196. ############################ ENDIT #######################3
  197. &set_label("decrypt_finish");
  198. &mov($count, &wparam(2)); # length
  199. &and($count, 7);
  200. &jz(&label("finish"));
  201. &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
  202. &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
  203. &bswap("eax") if $swap;
  204. &bswap("ebx") if $swap;
  205. &mov(&DWP($data_off,"esp","",0), "eax"); # put back
  206. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  207. &call($dec_func);
  208. &mov("eax", &DWP($data_off,"esp","",0)); # get return
  209. &mov("ebx", &DWP($data_off+4,"esp","",0)); #
  210. &bswap("eax") if $swap;
  211. &bswap("ebx") if $swap;
  212. &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
  213. &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
  214. &xor("ecx", "eax");
  215. &xor("edx", "ebx");
  216. # this is for when we exit
  217. &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
  218. &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
  219. &set_label("dj7");
  220. &rotr("edx", 16);
  221. &movb(&BP(6,$out,"",0), &LB("edx"));
  222. &shr("edx",16);
  223. &set_label("dj6");
  224. &movb(&BP(5,$out,"",0), &HB("edx"));
  225. &set_label("dj5");
  226. &movb(&BP(4,$out,"",0), &LB("edx"));
  227. &set_label("dj4");
  228. &mov(&DWP(0,$out,"",0), "ecx");
  229. &jmp(&label("djend"));
  230. &set_label("dj3");
  231. &rotr("ecx", 16);
  232. &movb(&BP(2,$out,"",0), &LB("ecx"));
  233. &shl("ecx",16);
  234. &set_label("dj2");
  235. &movb(&BP(1,$in,"",0), &HB("ecx"));
  236. &set_label("dj1");
  237. &movb(&BP(0,$in,"",0), &LB("ecx"));
  238. &set_label("djend");
  239. # final iv is still in eax:ebx
  240. &jmp(&label("finish"));
  241. ############################ FINISH #######################3
  242. &set_label("finish",1);
  243. &mov("ecx", &wparam($iv_off)); # Get iv ptr
  244. #################################################
  245. $total=16+4;
  246. $total+=4 if ($p1 > 0);
  247. $total+=4 if ($p2 > 0);
  248. $total+=4 if ($p3 > 0);
  249. &add("esp",$total);
  250. &mov(&DWP(0,"ecx","",0), "eax"); # save iv
  251. &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
  252. &function_end_A($name);
  253. &align(64);
  254. &set_label("cbc_enc_jmp_table");
  255. &data_word("0");
  256. &data_word(&label("ej1")."-".&label("PIC_point"));
  257. &data_word(&label("ej2")."-".&label("PIC_point"));
  258. &data_word(&label("ej3")."-".&label("PIC_point"));
  259. &data_word(&label("ej4")."-".&label("PIC_point"));
  260. &data_word(&label("ej5")."-".&label("PIC_point"));
  261. &data_word(&label("ej6")."-".&label("PIC_point"));
  262. &data_word(&label("ej7")."-".&label("PIC_point"));
  263. # not used
  264. #&set_label("cbc_dec_jmp_table",1);
  265. #&data_word("0");
  266. #&data_word(&label("dj1")."-".&label("PIC_point"));
  267. #&data_word(&label("dj2")."-".&label("PIC_point"));
  268. #&data_word(&label("dj3")."-".&label("PIC_point"));
  269. #&data_word(&label("dj4")."-".&label("PIC_point"));
  270. #&data_word(&label("dj5")."-".&label("PIC_point"));
  271. #&data_word(&label("dj6")."-".&label("PIC_point"));
  272. #&data_word(&label("dj7")."-".&label("PIC_point"));
  273. &align(64);
  274. &function_end_B($name);
  275. }
  276. 1;