rc4-586.masm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. IF @Version LT 800
  2. ECHO MASM version 8.00 or later is strongly recommended.
  3. ENDIF
  4. .686
  5. .XMM
  6. IF @Version LT 800
  7. XMMWORD STRUCT 16
  8. DQ 2 dup (?)
  9. XMMWORD ENDS
  10. ENDIF
  11. .MODEL FLAT
  12. OPTION DOTNAME
  13. IF @Version LT 800
  14. .text$ SEGMENT PAGE 'CODE'
  15. ELSE
  16. .text$ SEGMENT ALIGN(64) 'CODE'
  17. ENDIF
  18. ;EXTERN _OPENSSL_ia32cap_P:NEAR
  19. ALIGN 16
  20. _RC4 PROC PUBLIC
  21. $L_RC4_begin::
  22. push ebp
  23. push ebx
  24. push esi
  25. push edi
  26. mov edi,DWORD PTR 20[esp]
  27. mov edx,DWORD PTR 24[esp]
  28. mov esi,DWORD PTR 28[esp]
  29. mov ebp,DWORD PTR 32[esp]
  30. xor eax,eax
  31. xor ebx,ebx
  32. cmp edx,0
  33. je $L000abort
  34. mov al,BYTE PTR [edi]
  35. mov bl,BYTE PTR 4[edi]
  36. add edi,8
  37. lea ecx,DWORD PTR [edx*1+esi]
  38. sub ebp,esi
  39. mov DWORD PTR 24[esp],ecx
  40. inc al
  41. cmp DWORD PTR 256[edi],-1
  42. je $L001RC4_CHAR
  43. mov ecx,DWORD PTR [eax*4+edi]
  44. and edx,-4
  45. jz $L002loop1
  46. mov DWORD PTR 32[esp],ebp
  47. test edx,-8
  48. jz $L003go4loop4
  49. lea ebp,DWORD PTR _OPENSSL_ia32cap_P
  50. bt DWORD PTR [ebp],26
  51. jnc $L003go4loop4
  52. mov ebp,DWORD PTR 32[esp]
  53. and edx,-8
  54. lea edx,DWORD PTR [edx*1+esi-8]
  55. mov DWORD PTR [edi-4],edx
  56. add bl,cl
  57. mov edx,DWORD PTR [ebx*4+edi]
  58. mov DWORD PTR [ebx*4+edi],ecx
  59. mov DWORD PTR [eax*4+edi],edx
  60. inc eax
  61. add edx,ecx
  62. movzx eax,al
  63. movzx edx,dl
  64. movq mm0,QWORD PTR [esi]
  65. mov ecx,DWORD PTR [eax*4+edi]
  66. movd mm2,DWORD PTR [edx*4+edi]
  67. jmp $L004loop_mmx_enter
  68. ALIGN 16
  69. $L005loop_mmx:
  70. add bl,cl
  71. psllq mm1,56
  72. mov edx,DWORD PTR [ebx*4+edi]
  73. mov DWORD PTR [ebx*4+edi],ecx
  74. mov DWORD PTR [eax*4+edi],edx
  75. inc eax
  76. add edx,ecx
  77. movzx eax,al
  78. movzx edx,dl
  79. pxor mm2,mm1
  80. movq mm0,QWORD PTR [esi]
  81. movq QWORD PTR [esi*1+ebp-8],mm2
  82. mov ecx,DWORD PTR [eax*4+edi]
  83. movd mm2,DWORD PTR [edx*4+edi]
  84. $L004loop_mmx_enter:
  85. add bl,cl
  86. mov edx,DWORD PTR [ebx*4+edi]
  87. mov DWORD PTR [ebx*4+edi],ecx
  88. mov DWORD PTR [eax*4+edi],edx
  89. inc eax
  90. add edx,ecx
  91. movzx eax,al
  92. movzx edx,dl
  93. pxor mm2,mm0
  94. mov ecx,DWORD PTR [eax*4+edi]
  95. movd mm1,DWORD PTR [edx*4+edi]
  96. add bl,cl
  97. psllq mm1,8
  98. mov edx,DWORD PTR [ebx*4+edi]
  99. mov DWORD PTR [ebx*4+edi],ecx
  100. mov DWORD PTR [eax*4+edi],edx
  101. inc eax
  102. add edx,ecx
  103. movzx eax,al
  104. movzx edx,dl
  105. pxor mm2,mm1
  106. mov ecx,DWORD PTR [eax*4+edi]
  107. movd mm1,DWORD PTR [edx*4+edi]
  108. add bl,cl
  109. psllq mm1,16
  110. mov edx,DWORD PTR [ebx*4+edi]
  111. mov DWORD PTR [ebx*4+edi],ecx
  112. mov DWORD PTR [eax*4+edi],edx
  113. inc eax
  114. add edx,ecx
  115. movzx eax,al
  116. movzx edx,dl
  117. pxor mm2,mm1
  118. mov ecx,DWORD PTR [eax*4+edi]
  119. movd mm1,DWORD PTR [edx*4+edi]
  120. add bl,cl
  121. psllq mm1,24
  122. mov edx,DWORD PTR [ebx*4+edi]
  123. mov DWORD PTR [ebx*4+edi],ecx
  124. mov DWORD PTR [eax*4+edi],edx
  125. inc eax
  126. add edx,ecx
  127. movzx eax,al
  128. movzx edx,dl
  129. pxor mm2,mm1
  130. mov ecx,DWORD PTR [eax*4+edi]
  131. movd mm1,DWORD PTR [edx*4+edi]
  132. add bl,cl
  133. psllq mm1,32
  134. mov edx,DWORD PTR [ebx*4+edi]
  135. mov DWORD PTR [ebx*4+edi],ecx
  136. mov DWORD PTR [eax*4+edi],edx
  137. inc eax
  138. add edx,ecx
  139. movzx eax,al
  140. movzx edx,dl
  141. pxor mm2,mm1
  142. mov ecx,DWORD PTR [eax*4+edi]
  143. movd mm1,DWORD PTR [edx*4+edi]
  144. add bl,cl
  145. psllq mm1,40
  146. mov edx,DWORD PTR [ebx*4+edi]
  147. mov DWORD PTR [ebx*4+edi],ecx
  148. mov DWORD PTR [eax*4+edi],edx
  149. inc eax
  150. add edx,ecx
  151. movzx eax,al
  152. movzx edx,dl
  153. pxor mm2,mm1
  154. mov ecx,DWORD PTR [eax*4+edi]
  155. movd mm1,DWORD PTR [edx*4+edi]
  156. add bl,cl
  157. psllq mm1,48
  158. mov edx,DWORD PTR [ebx*4+edi]
  159. mov DWORD PTR [ebx*4+edi],ecx
  160. mov DWORD PTR [eax*4+edi],edx
  161. inc eax
  162. add edx,ecx
  163. movzx eax,al
  164. movzx edx,dl
  165. pxor mm2,mm1
  166. mov ecx,DWORD PTR [eax*4+edi]
  167. movd mm1,DWORD PTR [edx*4+edi]
  168. mov edx,ebx
  169. xor ebx,ebx
  170. mov bl,dl
  171. cmp esi,DWORD PTR [edi-4]
  172. lea esi,DWORD PTR 8[esi]
  173. jb $L005loop_mmx
  174. psllq mm1,56
  175. pxor mm2,mm1
  176. movq QWORD PTR [esi*1+ebp-8],mm2
  177. emms
  178. cmp esi,DWORD PTR 24[esp]
  179. je $L006done
  180. jmp $L002loop1
  181. ALIGN 16
  182. $L003go4loop4:
  183. lea edx,DWORD PTR [edx*1+esi-4]
  184. mov DWORD PTR 28[esp],edx
  185. $L007loop4:
  186. add bl,cl
  187. mov edx,DWORD PTR [ebx*4+edi]
  188. mov DWORD PTR [ebx*4+edi],ecx
  189. mov DWORD PTR [eax*4+edi],edx
  190. add edx,ecx
  191. inc al
  192. and edx,255
  193. mov ecx,DWORD PTR [eax*4+edi]
  194. mov ebp,DWORD PTR [edx*4+edi]
  195. add bl,cl
  196. mov edx,DWORD PTR [ebx*4+edi]
  197. mov DWORD PTR [ebx*4+edi],ecx
  198. mov DWORD PTR [eax*4+edi],edx
  199. add edx,ecx
  200. inc al
  201. and edx,255
  202. ror ebp,8
  203. mov ecx,DWORD PTR [eax*4+edi]
  204. or ebp,DWORD PTR [edx*4+edi]
  205. add bl,cl
  206. mov edx,DWORD PTR [ebx*4+edi]
  207. mov DWORD PTR [ebx*4+edi],ecx
  208. mov DWORD PTR [eax*4+edi],edx
  209. add edx,ecx
  210. inc al
  211. and edx,255
  212. ror ebp,8
  213. mov ecx,DWORD PTR [eax*4+edi]
  214. or ebp,DWORD PTR [edx*4+edi]
  215. add bl,cl
  216. mov edx,DWORD PTR [ebx*4+edi]
  217. mov DWORD PTR [ebx*4+edi],ecx
  218. mov DWORD PTR [eax*4+edi],edx
  219. add edx,ecx
  220. inc al
  221. and edx,255
  222. ror ebp,8
  223. mov ecx,DWORD PTR 32[esp]
  224. or ebp,DWORD PTR [edx*4+edi]
  225. ror ebp,8
  226. xor ebp,DWORD PTR [esi]
  227. cmp esi,DWORD PTR 28[esp]
  228. mov DWORD PTR [esi*1+ecx],ebp
  229. lea esi,DWORD PTR 4[esi]
  230. mov ecx,DWORD PTR [eax*4+edi]
  231. jb $L007loop4
  232. cmp esi,DWORD PTR 24[esp]
  233. je $L006done
  234. mov ebp,DWORD PTR 32[esp]
  235. ALIGN 16
  236. $L002loop1:
  237. add bl,cl
  238. mov edx,DWORD PTR [ebx*4+edi]
  239. mov DWORD PTR [ebx*4+edi],ecx
  240. mov DWORD PTR [eax*4+edi],edx
  241. add edx,ecx
  242. inc al
  243. and edx,255
  244. mov edx,DWORD PTR [edx*4+edi]
  245. xor dl,BYTE PTR [esi]
  246. lea esi,DWORD PTR 1[esi]
  247. mov ecx,DWORD PTR [eax*4+edi]
  248. cmp esi,DWORD PTR 24[esp]
  249. mov BYTE PTR [esi*1+ebp-1],dl
  250. jb $L002loop1
  251. jmp $L006done
  252. ALIGN 16
  253. $L001RC4_CHAR:
  254. movzx ecx,BYTE PTR [eax*1+edi]
  255. $L008cloop1:
  256. add bl,cl
  257. movzx edx,BYTE PTR [ebx*1+edi]
  258. mov BYTE PTR [ebx*1+edi],cl
  259. mov BYTE PTR [eax*1+edi],dl
  260. add dl,cl
  261. movzx edx,BYTE PTR [edx*1+edi]
  262. add al,1
  263. xor dl,BYTE PTR [esi]
  264. lea esi,DWORD PTR 1[esi]
  265. movzx ecx,BYTE PTR [eax*1+edi]
  266. cmp esi,DWORD PTR 24[esp]
  267. mov BYTE PTR [esi*1+ebp-1],dl
  268. jb $L008cloop1
  269. $L006done:
  270. dec al
  271. mov DWORD PTR [edi-4],ebx
  272. mov BYTE PTR [edi-8],al
  273. $L000abort:
  274. pop edi
  275. pop esi
  276. pop ebx
  277. pop ebp
  278. ret
  279. _RC4 ENDP
  280. ALIGN 16
  281. _RC4_set_key PROC PUBLIC
  282. $L_RC4_set_key_begin::
  283. push ebp
  284. push ebx
  285. push esi
  286. push edi
  287. mov edi,DWORD PTR 20[esp]
  288. mov ebp,DWORD PTR 24[esp]
  289. mov esi,DWORD PTR 28[esp]
  290. lea edx,DWORD PTR _OPENSSL_ia32cap_P
  291. lea edi,DWORD PTR 8[edi]
  292. lea esi,DWORD PTR [ebp*1+esi]
  293. neg ebp
  294. xor eax,eax
  295. mov DWORD PTR [edi-4],ebp
  296. bt DWORD PTR [edx],20
  297. jc $L009c1stloop
  298. ALIGN 16
  299. $L010w1stloop:
  300. mov DWORD PTR [eax*4+edi],eax
  301. add al,1
  302. jnc $L010w1stloop
  303. xor ecx,ecx
  304. xor edx,edx
  305. ALIGN 16
  306. $L011w2ndloop:
  307. mov eax,DWORD PTR [ecx*4+edi]
  308. add dl,BYTE PTR [ebp*1+esi]
  309. add dl,al
  310. add ebp,1
  311. mov ebx,DWORD PTR [edx*4+edi]
  312. jnz $L012wnowrap
  313. mov ebp,DWORD PTR [edi-4]
  314. $L012wnowrap:
  315. mov DWORD PTR [edx*4+edi],eax
  316. mov DWORD PTR [ecx*4+edi],ebx
  317. add cl,1
  318. jnc $L011w2ndloop
  319. jmp $L013exit
  320. ALIGN 16
  321. $L009c1stloop:
  322. mov BYTE PTR [eax*1+edi],al
  323. add al,1
  324. jnc $L009c1stloop
  325. xor ecx,ecx
  326. xor edx,edx
  327. xor ebx,ebx
  328. ALIGN 16
  329. $L014c2ndloop:
  330. mov al,BYTE PTR [ecx*1+edi]
  331. add dl,BYTE PTR [ebp*1+esi]
  332. add dl,al
  333. add ebp,1
  334. mov bl,BYTE PTR [edx*1+edi]
  335. jnz $L015cnowrap
  336. mov ebp,DWORD PTR [edi-4]
  337. $L015cnowrap:
  338. mov BYTE PTR [edx*1+edi],al
  339. mov BYTE PTR [ecx*1+edi],bl
  340. add cl,1
  341. jnc $L014c2ndloop
  342. mov DWORD PTR 256[edi],-1
  343. $L013exit:
  344. xor eax,eax
  345. mov DWORD PTR [edi-8],eax
  346. mov DWORD PTR [edi-4],eax
  347. pop edi
  348. pop esi
  349. pop ebx
  350. pop ebp
  351. ret
  352. _RC4_set_key ENDP
  353. ALIGN 16
  354. _RC4_options PROC PUBLIC
  355. $L_RC4_options_begin::
  356. call $L016pic_point
  357. $L016pic_point:
  358. pop eax
  359. lea eax,DWORD PTR ($L017opts-$L016pic_point)[eax]
  360. lea edx,DWORD PTR _OPENSSL_ia32cap_P
  361. mov edx,DWORD PTR [edx]
  362. bt edx,20
  363. jc $L0181xchar
  364. bt edx,26
  365. jnc $L019ret
  366. add eax,25
  367. ret
  368. $L0181xchar:
  369. add eax,12
  370. $L019ret:
  371. ret
  372. ALIGN 64
  373. $L017opts:
  374. DB 114,99,52,40,52,120,44,105,110,116,41,0
  375. DB 114,99,52,40,49,120,44,99,104,97,114,41,0
  376. DB 114,99,52,40,56,120,44,109,109,120,41,0
  377. DB 82,67,52,32,102,111,114,32,120,56,54,44,32,67,82,89
  378. DB 80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114
  379. DB 111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
  380. ALIGN 64
  381. _RC4_options ENDP
  382. .text$ ENDS
  383. .bss SEGMENT 'BSS'
  384. COMM _OPENSSL_ia32cap_P:DWORD:4
  385. .bss ENDS
  386. END