cbc.pl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. &xor("edx", "edx") if $ppro; # ppro friendly
  131. &movb(&HB("edx"), &BP(6,$in,"",0));
  132. &shl("edx",8);
  133. &set_label("ej6");
  134. &movb(&HB("edx"), &BP(5,$in,"",0));
  135. &set_label("ej5");
  136. &movb(&LB("edx"), &BP(4,$in,"",0));
  137. &set_label("ej4");
  138. &mov("ecx", &DWP(0,$in,"",0));
  139. &jmp(&label("ejend"));
  140. &set_label("ej3");
  141. &movb(&HB("ecx"), &BP(2,$in,"",0));
  142. &xor("ecx", "ecx") if $ppro; # ppro friendly
  143. &shl("ecx",8);
  144. &set_label("ej2");
  145. &movb(&HB("ecx"), &BP(1,$in,"",0));
  146. &set_label("ej1");
  147. &movb(&LB("ecx"), &BP(0,$in,"",0));
  148. &set_label("ejend");
  149. &xor("eax", "ecx");
  150. &xor("ebx", "edx");
  151. &bswap("eax") if $swap;
  152. &bswap("ebx") if $swap;
  153. &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
  154. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  155. &call($enc_func);
  156. &mov("eax", &DWP($data_off,"esp","",0));
  157. &mov("ebx", &DWP($data_off+4,"esp","",0));
  158. &bswap("eax") if $swap;
  159. &bswap("ebx") if $swap;
  160. &mov(&DWP(0,$out,"",0),"eax");
  161. &mov(&DWP(4,$out,"",0),"ebx");
  162. &jmp(&label("finish"));
  163. #############################################################
  164. #############################################################
  165. &set_label("decrypt",1);
  166. # decrypt start
  167. &and($count,0xfffffff8);
  168. # The next 2 instructions are only for if the jz is taken
  169. &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
  170. &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
  171. &jz(&label("decrypt_finish"));
  172. &set_label("decrypt_loop");
  173. &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
  174. &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
  175. &bswap("eax") if $swap;
  176. &bswap("ebx") if $swap;
  177. &mov(&DWP($data_off,"esp","",0), "eax"); # put back
  178. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  179. &call($dec_func);
  180. &mov("eax", &DWP($data_off,"esp","",0)); # get return
  181. &mov("ebx", &DWP($data_off+4,"esp","",0)); #
  182. &bswap("eax") if $swap;
  183. &bswap("ebx") if $swap;
  184. &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
  185. &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
  186. &xor("ecx", "eax");
  187. &xor("edx", "ebx");
  188. &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
  189. &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
  190. &mov(&DWP(0,$out,"",0),"ecx");
  191. &mov(&DWP(4,$out,"",0),"edx");
  192. &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
  193. &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
  194. &add($in, 8);
  195. &add($out, 8);
  196. &sub($count, 8);
  197. &jnz(&label("decrypt_loop"));
  198. ############################ ENDIT #######################3
  199. &set_label("decrypt_finish");
  200. &mov($count, &wparam(2)); # length
  201. &and($count, 7);
  202. &jz(&label("finish"));
  203. &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
  204. &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
  205. &bswap("eax") if $swap;
  206. &bswap("ebx") if $swap;
  207. &mov(&DWP($data_off,"esp","",0), "eax"); # put back
  208. &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
  209. &call($dec_func);
  210. &mov("eax", &DWP($data_off,"esp","",0)); # get return
  211. &mov("ebx", &DWP($data_off+4,"esp","",0)); #
  212. &bswap("eax") if $swap;
  213. &bswap("ebx") if $swap;
  214. &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
  215. &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
  216. &xor("ecx", "eax");
  217. &xor("edx", "ebx");
  218. # this is for when we exit
  219. &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
  220. &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
  221. &set_label("dj7");
  222. &rotr("edx", 16);
  223. &movb(&BP(6,$out,"",0), &LB("edx"));
  224. &shr("edx",16);
  225. &set_label("dj6");
  226. &movb(&BP(5,$out,"",0), &HB("edx"));
  227. &set_label("dj5");
  228. &movb(&BP(4,$out,"",0), &LB("edx"));
  229. &set_label("dj4");
  230. &mov(&DWP(0,$out,"",0), "ecx");
  231. &jmp(&label("djend"));
  232. &set_label("dj3");
  233. &rotr("ecx", 16);
  234. &movb(&BP(2,$out,"",0), &LB("ecx"));
  235. &shl("ecx",16);
  236. &set_label("dj2");
  237. &movb(&BP(1,$in,"",0), &HB("ecx"));
  238. &set_label("dj1");
  239. &movb(&BP(0,$in,"",0), &LB("ecx"));
  240. &set_label("djend");
  241. # final iv is still in eax:ebx
  242. &jmp(&label("finish"));
  243. ############################ FINISH #######################3
  244. &set_label("finish",1);
  245. &mov("ecx", &wparam($iv_off)); # Get iv ptr
  246. #################################################
  247. $total=16+4;
  248. $total+=4 if ($p1 > 0);
  249. $total+=4 if ($p2 > 0);
  250. $total+=4 if ($p3 > 0);
  251. &add("esp",$total);
  252. &mov(&DWP(0,"ecx","",0), "eax"); # save iv
  253. &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
  254. &function_end_A($name);
  255. &align(64);
  256. &set_label("cbc_enc_jmp_table");
  257. &data_word("0");
  258. &data_word(&label("ej1")."-".&label("PIC_point"));
  259. &data_word(&label("ej2")."-".&label("PIC_point"));
  260. &data_word(&label("ej3")."-".&label("PIC_point"));
  261. &data_word(&label("ej4")."-".&label("PIC_point"));
  262. &data_word(&label("ej5")."-".&label("PIC_point"));
  263. &data_word(&label("ej6")."-".&label("PIC_point"));
  264. &data_word(&label("ej7")."-".&label("PIC_point"));
  265. # not used
  266. #&set_label("cbc_dec_jmp_table",1);
  267. #&data_word("0");
  268. #&data_word(&label("dj1")."-".&label("PIC_point"));
  269. #&data_word(&label("dj2")."-".&label("PIC_point"));
  270. #&data_word(&label("dj3")."-".&label("PIC_point"));
  271. #&data_word(&label("dj4")."-".&label("PIC_point"));
  272. #&data_word(&label("dj5")."-".&label("PIC_point"));
  273. #&data_word(&label("dj6")."-".&label("PIC_point"));
  274. #&data_word(&label("dj7")."-".&label("PIC_point"));
  275. &align(64);
  276. &function_end_B($name);
  277. }
  278. 1;