cbc.pl 8.4 KB

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