cbc.pl 9.2 KB

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