Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. .global sha1_chunk
  2.  
  3. .text
  4. counter: .asciz "counter: %ld "
  5. w1: .asciz "w[i-3]: %p \t"
  6. w2: .asciz "w[i-8]: %p \t"
  7. w3: .asciz "w[i-14]: %p \t"
  8. w4: .asciz "w[i-16]: %p \t"
  9. xor: .asciz "XOR: %ld "
  10. lor: .asciz "ROTATE: %ld\n"
  11. offset: .asciz "OFFSET: %ld "
  12. address: .asciz "ADDRESS: %ld\n"
  13. test: .asciz "TEST: %ld\n"
  14.  
  15. # First parameter (rdi): address of h0
  16. # Second parameter (rsi): address of the first 32-bit word of an array of 80 32-bit words.
  17.  
  18. sha1_chunk:
  19. # Setting up subroutine
  20. pushq %rbp # Push the base pointer on the stack
  21. movq %rsp, %rbp # Put the stack pointer into rbp
  22.  
  23. pushq %rdi # -8
  24. pushq %rsi # -16
  25.  
  26. # Extend the 16 32-bit words into 80 32-bit words
  27. # for i from 16 to 79
  28. # w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
  29.  
  30. # Create memory space for loop counter
  31. pushq $16
  32.  
  33. loopextend:
  34. # w[i-3]
  35. movq -24(%rbp), %r9
  36. subq $3, %r9
  37. movq $4, %rax
  38. imulq %r9, %rax
  39. addq %rsi, %rax
  40. movl (%rax), %r9d # address of w[i-3] is now in %r9d
  41.  
  42. # w[i-8]
  43. movq -24(%rbp), %r10
  44. subq $8, %r10
  45. movq $4, %rax
  46. imulq %r10, %rax
  47. addq %rsi, %rax
  48. movl (%rax), %r10d # address of w[i-8] is now in %r10d
  49.  
  50. # w[i-14]
  51. movq -24(%rbp), %r11
  52. subq $14, %r11
  53. movq $4, %rax
  54. imulq %r11, %rax
  55. addq %rsi, %rax
  56. movl (%rax), %r11d # address of w[i-14] is now in %r11d
  57.  
  58. # w[i-16]
  59. movq -24(%rbp), %r12
  60. subq $16, %r12
  61. movq $4, %rax
  62. imulq %r12, %rax
  63. addq %rsi, %rax
  64. movl (%rax), %r12d # address of w[i-14] is now in %r12d
  65.  
  66. # xor's
  67. xorl %r9d, %r10d
  68. xorl %r10d, %r11d
  69. xorl %r11d, %r12d
  70.  
  71. # (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
  72. rol %r12d
  73.  
  74. # Assign value
  75. movq $4, %rax
  76. imulq -24(%rbp), %rax
  77. addq %rsi, %rax
  78. movl %r12d, (%rax)
  79.  
  80. incq -24(%rbp)
  81. cmp $79, -24(%rbp)
  82. jle loopextend
  83.  
  84.  
  85. # Initialize hash value
  86. movl (%rdi), %r8d # h0, a
  87. movl 4(%rdi), %r9d # h1, b
  88. movl 8(%rdi), %r10d # h2, c
  89. movl 12(%rdi), %r11d # h3, d
  90. movl 16(%rdi), %r12d # h4, e
  91.  
  92. # Main loop counter
  93. movq $0, -24(%rbp)
  94.  
  95. mainloop:
  96. cmp $19, -24(%rbp)
  97. jle if
  98.  
  99. cmp $39, -24(%rbp)
  100. jle else1
  101.  
  102. cmp $59, -24(%rbp)
  103. jle else2
  104.  
  105. cmp $79, -24(%rbp)
  106. jle else3
  107.  
  108. # 0 ≤ i ≤ 19
  109. if:
  110. movl %r9d, %ebx # b
  111. movl %r10d, %ecx # c
  112. movl %r11d, %edx # d
  113.  
  114. andl %ebx, %ecx # (b and c)
  115. notl %ebx # (not b)
  116. andl %ebx, %edx # ((not b) and d)
  117. orl %ecx, %edx # (b and c) or ((not b) and d)
  118.  
  119. movl %edx, %r14d
  120. movl $0x5A827999, %r15d
  121.  
  122. jmp done
  123. # 20 ≤ i ≤ 39
  124. else1:
  125. movl %r9d, %ebx # b
  126. movl %r10d, %ecx # c
  127. movl %r11d, %edx # d
  128.  
  129. xorl %ebx, %ecx # b xor c
  130. xorl %ecx, %edx # (b xor c) xor d
  131.  
  132. movl %edx, %r14d
  133. movl $0x6ED9EBA1, %r15d
  134.  
  135. jmp done
  136. # 40 ≤ i ≤ 59
  137. else2:
  138. movl %r9d, %ebx # b
  139. movl %r10d, %ecx # c
  140. movl %r11d, %edx # d
  141.  
  142. andl %ebx, %ecx # b and c
  143. andl %ebx, %edx # b and d
  144. orl %ecx, %edx # (b and c) or (b and d)
  145.  
  146. movl 8(%rdi), %r13d # c
  147. movl 12(%rdi), %r14d # d
  148. andl %r13d, %r14d # c and d
  149.  
  150. orl %edx, %r14d # (b and c) or (b and d) and (c and d)
  151.  
  152. movl $0x8F1BBCDC, %r15d
  153.  
  154. jmp done
  155. # 60 ≤ i ≤ 79
  156. else3:
  157. movl %r9d, %ebx # b
  158. movl %r10d, %ecx # c
  159. movl %r11d, %edx # d
  160.  
  161. xorl %ebx, %ecx # b xor c
  162. xorl %ecx, %edx # (b xor c) xor d
  163.  
  164. movl %edx, %r14d
  165. movl $0xCA62C1D6, %r15d
  166.  
  167. done:
  168. # f in %r14d
  169. # k in %r15d
  170.  
  171. # temp = (a leftrotate 5) + f + e + k + w[i]
  172. rol $5, %r8d # (a leftrotate 5)
  173.  
  174. addl %r8d, %r14d # +f
  175. addl %r12d, %r14d # +e
  176. addl %r14d, %r15d # +k
  177.  
  178. # Calculate w[i]
  179. movq $4, %rax
  180. imulq -24(%rbp), %rax
  181. addq %rsi, %rax
  182. movl (%rax), %r13d # w[i] is in %r13d
  183.  
  184. addl %r13d, %r15d # temp is in %r15d
  185.  
  186. # e = d (d -> e)
  187. movl %r11d, %r12d
  188.  
  189. # d = c
  190. movl %r10d, %r11d
  191.  
  192. # c = b leftrotate 30
  193. rol $30, %r9d
  194. movl %r9d, %r10d
  195.  
  196. # b = a
  197. movl %r8d, %r9d
  198.  
  199. # a = temp
  200. movl %r15d, %r8d
  201.  
  202. incq -24(%rbp)
  203. cmp $79, -24(%rbp)
  204. jle mainloop
  205.  
  206. # a in %r8d
  207. # b in %r9d
  208. # c in %r10d
  209. # d in %r11d
  210. # e in %r12d
  211.  
  212. # h0 = h0 + a
  213. addl (%rdi), %r8d
  214. movl %r8d, (%rdi)
  215.  
  216. # h1 = h1 + b
  217. addl 4(%rdi), %r9d
  218. movl %r9d, 4(%rdi)
  219.  
  220. # h2 = h2 + c
  221. addl 8(%rdi), %r10d
  222. movl %r10d, 8(%rdi)
  223.  
  224. # h3 = h3 + d
  225. addl 12(%rdi), %r11d
  226. movl %r11d, 12(%rdi)
  227.  
  228. # h4 = h4 + e
  229. addl 16(%rdi), %r12d
  230. movl %r12d, 16(%rdi)
  231.  
  232. # Cleaning up subroutine
  233. movq %rbp, %rsp
  234. popq %rbp
  235.  
  236. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement