Guest User

gost34_code.inc

a guest
Apr 18th, 2014
1,502
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. align 4
  2. GOST_3411_2012: ;( EBX variant, ESI const unsigned char *message, ECX length, EDI unsigned char *out)
  3. ; *hash = IV
  4. shl ecx, 0x00000003 ; convert bytes to bits
  5. pushad
  6. mov edi, G12_VARS
  7. mov ecx, G12_VARS_SZ / 4
  8. xor eax, eax
  9. cld
  10. rep stosd
  11. ;
  12. mov eax, 0x00000000
  13. cmp ebx, 256
  14. jne .h512
  15. mov eax, 0x01010101
  16. .h512:
  17. mov edi, gosth_hash
  18. mov ecx, 64/4
  19. cld
  20. rep stosd
  21. ;
  22. mov byte[hash_X_v512 + 62], 0x02 ; init hash_X_v512
  23. popad
  24. ;
  25. ; Stage 2
  26. ;; while (len >= 512)
  27. ;; {
  28. ;; memcpy(m, message + len/8 - 63 - ( (len & 0x7) == 0 ), 64);
  29. ;; g_N(N,hash,m);
  30. ;; AddModulo512(N,v512,N);
  31. ;; AddModulo512(hash_X_Sigma,m,hash_X_Sigma);
  32. ;; len -= 512;
  33. ;; }
  34. align 4
  35. .stage_2:
  36. cmp ecx, 512
  37. jb .stage_25
  38. ;
  39. push ecx esi edi
  40. mov edi, g12_X_m
  41. push ecx
  42. shr ecx, 0x00000003
  43. add esi, ecx
  44. sub esi, 64
  45. pop ecx
  46. mov ecx, 64/4
  47. cld
  48. rep movsd
  49. pop edi esi ecx
  50. ; g_N(N,hash,m)
  51. push edx esi edi
  52. mov edi, hash_X_N
  53. mov esi, gosth_hash
  54. mov edx, g12_X_m
  55. call fn_g_N ; EDI *N, ESI *h, EDX *m ; +++++
  56. pop edi esi edx
  57. ; AddModulo512(N,v512,N)
  58. push eax esi edi
  59. mov eax, hash_X_N
  60. mov esi, hash_X_v512
  61. mov edi, hash_X_N
  62. call AddModulo512 ; eax *a esi *b edi *c
  63. pop edi esi eax
  64. ; AddModulo512(hash_X_Sigma,m,hash_X_Sigma)
  65. push eax esi edi
  66. mov eax, hash_X_Sigma
  67. mov esi, g12_X_m
  68. mov edi, hash_X_Sigma
  69. call AddModulo512 ; eax *a esi *b edi *c
  70. pop edi esi eax
  71. ; len -= 512;
  72. sub ecx, 512
  73. cmp ecx, 512
  74. jae .stage_2
  75. ; }
  76. .stage_25:
  77. ; +OK memset(m,0,64)
  78. push edi ecx eax
  79. mov edi, g12_X_m
  80. xor eax, eax
  81. mov ecx, 64/4
  82. cld
  83. rep stosd
  84. pop eax ecx edi
  85. ; +OK memcpy(m + 63 - len/8 + ( (len & 0x7) == 0 ), message, len/8 + 1 - ( (len & 0x7) == 0 ))
  86. push eax ecx esi edi
  87. mov eax, 64
  88. shr ecx, 0x00000003
  89. sub eax, ecx
  90. mov edi, g12_X_m
  91. add edi, eax
  92. cld
  93. rep movsb
  94. pop edi esi ecx eax
  95. ;
  96. ;
  97. ; Stage 3
  98. ; m[ 63 - len/8 ] |= (1 << (len & 0x7))
  99. push ebx ecx edx
  100. push ecx
  101. and ecx, 0x00000007
  102. mov ebx, 0x00000001
  103. shl ebx, cl
  104. pop ecx
  105. push ecx
  106. shr ecx, 0x00000003
  107. mov edx, 63
  108. sub edx, ecx
  109. pop ecx
  110. add edx, g12_X_m
  111. or byte[edx], bl
  112. pop edx ecx ebx
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. ; g_N(N,hash,m);
  120. push edx esi edi
  121. mov edi, hash_X_N
  122. mov esi, gosth_hash
  123. mov edx, g12_X_m
  124. call fn_g_N ; EDI *N, ESI *h, EDX *m
  125. pop edi esi edx
  126. ; v512[63] = len & 0xFF;
  127. ; push ecx
  128. ; and ecx, 0x000000FF
  129. mov byte[hash_X_v512 + 63], cl
  130. ; pop ecx
  131. ; v512[62] = len >> 8;
  132. push ecx
  133. shr ecx, 0x00000008
  134. mov byte[hash_X_v512 + 62], cl
  135. pop ecx
  136. ; AddModulo512(N,v512,N);
  137. push esi edi
  138. mov eax, hash_X_N
  139. mov esi, hash_X_v512
  140. mov edi, hash_X_N
  141. call AddModulo512 ; eax *a esi *b edi *c
  142. pop edi esi
  143. ; AddModulo512(hash_X_Sigma, m, hash_X_Sigma)
  144. push esi edi
  145. mov eax, hash_X_Sigma
  146. mov esi, g12_X_m
  147. mov edi, hash_X_Sigma
  148. call AddModulo512 ; eax *a esi *b edi *c
  149. pop edi esi
  150. ; g_N(v0,hash,N);
  151. push esi edi
  152. mov edi, hash_X_v0
  153. mov esi, gosth_hash
  154. mov edx, hash_X_N
  155. call fn_g_N ; EDI *N, ESI *h, EDX *m
  156. ; g_N(v0,hash,hash_X_Sigma);
  157. mov edi, hash_X_v0
  158. mov esi, gosth_hash
  159. mov edx, hash_X_Sigma
  160. call fn_g_N ; EDI *N, ESI *h, EDX *m
  161. pop edi esi
  162. ; memcpy(out, hash, 64)
  163. push ecx esi edi
  164. mov esi, gosth_hash
  165.  
  166. mov ecx, 64/4
  167. cmp ebx, 256
  168. jne .h512c
  169. mov ecx, 32/4
  170. .h512c:
  171. cld
  172. rep movsd
  173. pop edi esi ecx
  174. ret
  175.  
  176.  
  177.  
  178.  
  179.  
  180. ALIGN 4
  181. HEX2STR_256_BIT: ; ^ESI->, ^EDI<-
  182. PUSHAD
  183. XOR ECX, ECX
  184. ;
  185. @@: INC ECX
  186. CMP ECX, 32
  187. JA .END
  188. LODSB
  189. PUSH ECX
  190. MOV DL, AL
  191. MOV ECX, 0x02
  192. .L1: ROL DL, 0x04
  193. MOV EAX, 0x300F
  194. AND AL, DL
  195. AAA
  196. AAD 0x11
  197. STOSB
  198. LOOP .L1
  199. POP ECX
  200. JMP @B
  201. .END:
  202. XOR EAX, EAX
  203. STOSB
  204. POPAD
  205. RETD
  206.  
  207. ALIGN 4
  208. HEX2STR_512_BIT: ; ^ESI->, ^EDI<-
  209. PUSHAD
  210. XOR ECX, ECX
  211. ;
  212. @@: INC ECX
  213. CMP ECX, 64
  214. JA .END
  215. LODSB
  216. PUSH ECX
  217. MOV DL, AL
  218. MOV ECX, 0x02
  219. .L1: ROL DL, 0x04
  220. MOV EAX, 0x300F
  221. AND AL, DL
  222. AAA
  223. AAD 0x11
  224. STOSB
  225. LOOP .L1
  226. POP ECX
  227. JMP @B
  228. .END:
  229. XOR EAX, EAX
  230. STOSB
  231. POPAD
  232. RETD
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. ; FASM x86: Vladislav Kabak
  241.  
  242.  
  243. ;include 'lib_gost_data.inc'
  244.  
  245. align 4
  246. KeySchedule: ; unsigned char *K EAX, int i ECX ; +OK
  247. push eax ebx ecx esi
  248. ; K = K xor C[i]
  249. push eax
  250. mov ebx, eax
  251. mov esi, eax
  252. mov eax, ecx
  253. shl eax, 0x06 ; *64 (get block # by index)
  254. add eax, const_C ; point to C-table
  255. call AddXor512 ; ebx eax esi ; AddXor512(K,C[i],K)
  256. pop esi
  257. ;
  258. call fn_S ; K = S(K)
  259. call fn_P ; K = P(K)
  260. call fn_L ; K = L(K)
  261. ;
  262. pop esi ecx ebx eax
  263. ret
  264.  
  265. align 4
  266. fn_S: ; SubBytes S function. unsigned char *state [esi] ; +OK
  267. push eax ebx ecx edx
  268. xor ecx, ecx ; int i = 0
  269. mov ebx, G12_Sbox ; S-Box
  270. xor edx, edx
  271. align 4 ; for(i=0;i<64;i++) state[i] = Sbox[state[i]]
  272. @@: mov dl, byte[esi + ecx] ; edx = state[i]
  273. mov al, byte[G12_Sbox + edx] ; al = Sbox[state[i]]
  274. mov byte[esi + ecx], al ; state[i] = Sbox[state[i]]
  275. inc ecx
  276. cmp ecx, 64
  277. jne @b
  278. pop edx ecx ebx eax
  279. ret
  280.  
  281. align 4
  282. fn_P: ; Transposition P function. unsigned char *state in ESI ; +OK
  283. pushad
  284. xor ecx, ecx ; int i = 0
  285. xor edx, edx
  286. align 4
  287. @@: mov dl, byte [Tau + ecx] ; edx = Tau[i]
  288. mov al, byte [esi + edx] ; eax = state[Tau[i]]
  289. mov byte[fn_P_t + ecx], al ; t[i] = state[Tau[i]]
  290. inc ecx ; for(i=0;i<64;i++)
  291. cmp ecx, 64
  292. jne @b
  293. ; memcpy(state,t,64)
  294. mov ecx, 64/4
  295. mov edi, esi
  296. mov esi, fn_P_t
  297. cld
  298. rep movsd
  299. popad
  300. ret
  301.  
  302.  
  303. align 4
  304. fn_E: ; eax: ^K, ebx: *m, esi: *state ; +OK
  305. ; unsigned char *K, const unsigned char *m, unsigned char *state
  306. push ebx ecx
  307. ; AddXor512(m,K,state)
  308. call AddXor512 ; m=ebx, K=eax, state=esi, safe needed
  309. xor ecx, ecx ; int i = 0
  310. align 4 ; for(i=0;i<12;i++) {
  311. @@: call fn_S ; state = S(state)
  312. call fn_P ; state = P(state)
  313. call fn_L ; state = L(state)
  314. ; K = KeySchedule(K, i)
  315. call KeySchedule ; eax=K, i=ecx, safe needed
  316. ; AddXor512(state,K,state)
  317. mov ebx, esi
  318. call AddXor512 ; (state, K, state)
  319. inc ecx
  320. cmp ecx, 12
  321. jne @b ; }
  322. pop ecx ebx
  323. ret
  324.  
  325. align 4
  326. AddXor512: ; AddRoundKey X-function. XOR 512-bytes ; +OK EBX EAX ESI
  327. push ecx edx
  328. xor ecx, ecx
  329. align 4
  330. @@: mov edx, dword[ebx + ecx]
  331. xor edx, dword[eax + ecx]
  332. mov dword[esi + ecx], edx
  333. add ecx, 4
  334. cmp ecx, 64
  335. jne @b
  336. pop edx ecx
  337. ret
  338.  
  339. ; g (N,m,H)
  340. align 4
  341. fn_g_N: ; EDI *N, ESI *h, EDX *m ; +OK
  342. ; const unsigned char *N,unsigned char *h,const unsigned char *m
  343. pushad
  344. push esi
  345. ; AddXor512(N,h,K)
  346. mov ebx, edi ; N
  347. mov eax, esi ; h
  348. mov esi, fn_g_N_K
  349. call AddXor512 ; ebx eax esi
  350. mov esi, fn_g_N_K ;
  351. call fn_S ; K = S(K)
  352. call fn_P ; K = P(K)
  353. call fn_L ; K = L(K)
  354. ; E(K,m,t) ; t = E(K, m)
  355. mov eax, fn_g_N_K ; K
  356. mov ebx, edx ; m
  357. mov esi, fn_g_N_t ; =t
  358. call fn_E ; eax, ebx, esi
  359. ;
  360. pop esi ; h
  361. push esi ; h
  362. ; AddXor512(t,h,t) ; t = h xor t
  363. mov ebx, fn_g_N_t ; t
  364. mov eax, esi ; h
  365. mov esi, fn_g_N_t ; t
  366. call AddXor512 ; ebx eax esi
  367. pop esi ; h
  368. ; AddXor512(t,m,h) ; G = t xor m
  369. mov ebx, fn_g_N_t ; t
  370. mov eax, edx ; m
  371. call AddXor512 ; ebx eax esi
  372. popad
  373. ret ; result = G
  374.  
  375.  
  376. align 4
  377. AddModulo512: ; eax *a esi *b edi *c ; +OK
  378. ; const unsigned char *a,const unsigned char *b,unsigned char *c
  379. push ebx ecx edx
  380. ;
  381. mov ecx, 64 ; int i = 63
  382. xor ebx, ebx ; int t = 0
  383. ; for(i=63;i>=0;i--)
  384. ; {
  385. align 4
  386. @@: sar ebx, 0x00000008 ; t := t >> 8
  387. movzx edx, byte[eax + ecx -1] ; edx = a[i]
  388. add ebx, edx ; t := (t>>8) + a[i]
  389. movzx edx, byte[esi + ecx -1] ; edx = b[i]
  390. add ebx, edx ; t := (t>>8) + +a[i] + b[i]
  391. mov byte[edi + ecx -1], bl ; c[i] = t & 0xFF
  392. ;
  393. dec ecx
  394. or ecx, ecx
  395. jnz @b
  396. ; }
  397. pop edx ecx ebx
  398. ret
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423. ; L: umnozhenie 64bit vectora vhodnogo na matritsu A(64x64)
  424. ; unsigned long long v = 0;
  425. ; int i = 0, j = 0, k = 0;
  426. ; for(i=0;i<8;i++) {
  427. ; v=0;
  428. ; for(k=0;k<8;k++) {
  429. ; for(j=0;j<8;j++) {
  430. ; if ((state[i*8+k] & (1<<(7-j))) != 0)
  431. ; v ^= A[k*8+j]
  432. ; }
  433. ; }
  434. ; for(k=0;k<8;k++)
  435. ; {
  436. ; state[i*8+k] = (v & ((unsigned long long)0xFF << (7-k)*8)) >> (7-k)*8;
  437. ; }
  438. ;}
  439.  
  440. align 4
  441. fn_L: ; unsigned char *state in ESI ;
  442. pushad
  443. ;
  444. xor ecx, ecx ; int i = 0
  445. xor ebx, ebx ; int j = 0
  446. xor edx, edx ; int k = 0
  447. pxor mm0, mm0 ;
  448. ; for(i=0;i<8;i++) {
  449. align 4
  450. .next_i:
  451. pxor mm0, mm0 ; v = 0
  452. ;
  453. ; for(k=0;k<8;k++) {
  454. xor edx, edx ; k = 0
  455. align 4
  456. .next_k:
  457. ; for(j=0;j<8;j++) {
  458. ; if ((state[i*8+k] & (1<<(7-j))) != 0) v ^= A[k*8+j]
  459. ; }
  460. xor ebx, ebx ; j = 0
  461. align 4
  462. .next_j:
  463. mov eax, 0x00000007 ; 7
  464. sub eax, ebx ; 7-j
  465. mov edi, 0x00000001 ; 1
  466. ;
  467. push ecx
  468. mov ecx, eax
  469. shl edi, cl ; (1<<(7-j)) == EDI
  470. pop ecx
  471. ;
  472. mov eax, ecx ; i
  473. shl eax, 0x00000003 ; i*8
  474. add eax, edx ; (i*8+k)
  475. movzx eax, byte[esi + eax] ; state[i*8+k]
  476. and eax, edi ; ( state[i*8+k] & (1<<(7-j) )
  477. cmp eax, 0 ; if ((state[i*8+k] & (1<<(7-j))) != 0) v ^= A[k*8+j] ???
  478. jz .next_ji ; == 0
  479. ; v ^= A[k*8+j] ; != 0
  480. mov eax, edx ; k
  481. shl eax, 0x00000003 ; k*8
  482. add eax, ebx ; k*8+j
  483. shl eax, 0x00000003 ; *8 (point from index to 64bit value (8bytes per value))
  484. add eax, matrix_A ; ^A[k*8+j]
  485. movq mm1, qword[eax] ; A[k*8+j]
  486. pxor mm0, mm1 ; v ^= A[k*8+j]
  487. jmp .next_ji
  488. ; }
  489. jmp .next_ki
  490. ; }
  491.  
  492. .next_k2start:
  493. xor edx, edx
  494. align 4
  495. .next_k2:
  496. mov eax, 0x00000007 ; 7
  497. sub eax, edx ; (7-k)
  498. shl eax, 0x00000003 ; (7-k)*8
  499. mov edi, 0x000000FF
  500. pxor mm2, mm2
  501. movd mm2, edi ; (unsigned long long)0xFF
  502. pxor mm4, mm4
  503. movd mm4, eax
  504. psllq mm2, mm4 ; (unsigned long long)0xFF << (7-k)*8
  505. movq mm3, mm0 ; v
  506. pand mm3, mm2 ; (v & ((unsigned long long)0xFF << (7-k)*8))
  507. psrlq mm3, mm4 ; (v & ((unsigned long long)0xFF << (7-k)*8)) >> (7-k)*8
  508. movd eax, mm3
  509. mov edi, ecx ; i
  510. shl edi, 0x00000003 ; i*8
  511. add edi, edx ; i*8+k
  512. add edi, esi ; ^state[i*8+k]
  513. mov byte[edi], al ; state[i*8+k] = (v & ((unsigned long long)0xFF << (7-k)*8)) >> (7-k)*8;
  514. jmp .next_k2i
  515.  
  516. align 4
  517. .next_ji:
  518. inc ebx
  519. cmp ebx, 8
  520. jb .next_j
  521. jmp .next_ki
  522. align 4
  523. .next_ki:
  524. inc edx
  525. cmp edx, 8
  526. jb .next_k
  527. jmp .next_k2start
  528. align 4
  529. .next_k2i:
  530. inc edx
  531. cmp edx, 8
  532. jb .next_k2
  533. jmp .next_ii
  534. align 4
  535. .next_ii:
  536. inc ecx
  537. cmp ecx, 8
  538. jb .next_i
  539. jmp .end
  540. .end:
  541. ;
  542. emms
  543. popad
  544. ret
RAW Paste Data