Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .global sha1_chunk
- .text
- counter: .asciz "counter: %ld "
- w1: .asciz "w[i-3]: %p \t"
- w2: .asciz "w[i-8]: %p \t"
- w3: .asciz "w[i-14]: %p \t"
- w4: .asciz "w[i-16]: %p \t"
- xor: .asciz "XOR: %ld "
- lor: .asciz "ROTATE: %ld\n"
- offset: .asciz "OFFSET: %ld "
- address: .asciz "ADDRESS: %ld\n"
- test: .asciz "TEST: %ld\n"
- # First parameter (rdi): address of h0
- # Second parameter (rsi): address of the first 32-bit word of an array of 80 32-bit words.
- sha1_chunk:
- # Setting up subroutine
- pushq %rbp # Push the base pointer on the stack
- movq %rsp, %rbp # Put the stack pointer into rbp
- pushq %rdi # -8
- pushq %rsi # -16
- # Extend the 16 32-bit words into 80 32-bit words
- # for i from 16 to 79
- # w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
- # Create memory space for loop counter
- pushq $16
- loopextend:
- # w[i-3]
- movq -24(%rbp), %r9
- subq $3, %r9
- movq $4, %rax
- imulq %r9, %rax
- addq %rsi, %rax
- movl (%rax), %r9d # address of w[i-3] is now in %r9d
- # w[i-8]
- movq -24(%rbp), %r10
- subq $8, %r10
- movq $4, %rax
- imulq %r10, %rax
- addq %rsi, %rax
- movl (%rax), %r10d # address of w[i-8] is now in %r10d
- # w[i-14]
- movq -24(%rbp), %r11
- subq $14, %r11
- movq $4, %rax
- imulq %r11, %rax
- addq %rsi, %rax
- movl (%rax), %r11d # address of w[i-14] is now in %r11d
- # w[i-16]
- movq -24(%rbp), %r12
- subq $16, %r12
- movq $4, %rax
- imulq %r12, %rax
- addq %rsi, %rax
- movl (%rax), %r12d # address of w[i-14] is now in %r12d
- # xor's
- xorl %r9d, %r10d
- xorl %r10d, %r11d
- xorl %r11d, %r12d
- # (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
- rol %r12d
- # Assign value
- movq $4, %rax
- imulq -24(%rbp), %rax
- addq %rsi, %rax
- movl %r12d, (%rax)
- incq -24(%rbp)
- cmp $79, -24(%rbp)
- jle loopextend
- # Initialize hash value
- movl (%rdi), %r8d # h0, a
- movl 4(%rdi), %r9d # h1, b
- movl 8(%rdi), %r10d # h2, c
- movl 12(%rdi), %r11d # h3, d
- movl 16(%rdi), %r12d # h4, e
- # Main loop counter
- movq $0, -24(%rbp)
- mainloop:
- cmp $19, -24(%rbp)
- jle if
- cmp $39, -24(%rbp)
- jle else1
- cmp $59, -24(%rbp)
- jle else2
- cmp $79, -24(%rbp)
- jle else3
- # 0 ≤ i ≤ 19
- if:
- movl %r9d, %ebx # b
- movl %r10d, %ecx # c
- movl %r11d, %edx # d
- andl %ebx, %ecx # (b and c)
- notl %ebx # (not b)
- andl %ebx, %edx # ((not b) and d)
- orl %ecx, %edx # (b and c) or ((not b) and d)
- movl %edx, %r14d
- movl $0x5A827999, %r15d
- jmp done
- # 20 ≤ i ≤ 39
- else1:
- movl %r9d, %ebx # b
- movl %r10d, %ecx # c
- movl %r11d, %edx # d
- xorl %ebx, %ecx # b xor c
- xorl %ecx, %edx # (b xor c) xor d
- movl %edx, %r14d
- movl $0x6ED9EBA1, %r15d
- jmp done
- # 40 ≤ i ≤ 59
- else2:
- movl %r9d, %ebx # b
- movl %r10d, %ecx # c
- movl %r11d, %edx # d
- andl %ebx, %ecx # b and c
- andl %ebx, %edx # b and d
- orl %ecx, %edx # (b and c) or (b and d)
- movl 8(%rdi), %r13d # c
- movl 12(%rdi), %r14d # d
- andl %r13d, %r14d # c and d
- orl %edx, %r14d # (b and c) or (b and d) and (c and d)
- movl $0x8F1BBCDC, %r15d
- jmp done
- # 60 ≤ i ≤ 79
- else3:
- movl %r9d, %ebx # b
- movl %r10d, %ecx # c
- movl %r11d, %edx # d
- xorl %ebx, %ecx # b xor c
- xorl %ecx, %edx # (b xor c) xor d
- movl %edx, %r14d
- movl $0xCA62C1D6, %r15d
- done:
- # f in %r14d
- # k in %r15d
- movl %r8d, %edx
- # temp = (a leftrotate 5) + f + e + k + w[i]
- rol $5, %r8d # (a leftrotate 5)
- addl %r8d, %r14d # +f
- addl %r12d, %r14d # +e
- addl %r14d, %r15d # +k
- # Calculate w[i]
- movq $4, %rax
- imulq -24(%rbp), %rax
- addq %rsi, %rax
- movl (%rax), %r13d # w[i] is in %r13d
- addl %r13d, %r15d # temp is in %r15d
- # e = d (d -> e)
- movl %r11d, %r12d
- # d = c
- movl %r10d, %r11d
- # c = b leftrotate 30
- rol $30, %r9d
- movl %r9d, %r10d
- # b = a
- movl %edx, %r9d
- # a = temp
- movl %r15d, %r8d
- incq -24(%rbp)
- cmp $79, -24(%rbp)
- jle mainloop
- # a in %r8d
- # b in %r9d
- # c in %r10d
- # d in %r11d
- # e in %r12d
- # h0 = h0 + a
- addl (%rdi), %r8d
- movl %r8d, (%rdi)
- # h1 = h1 + b
- addl 4(%rdi), %r9d
- movl %r9d, 4(%rdi)
- # h2 = h2 + c
- addl 8(%rdi), %r10d
- movl %r10d, 8(%rdi)
- # h3 = h3 + d
- addl 12(%rdi), %r11d
- movl %r11d, 12(%rdi)
- # h4 = h4 + e
- addl 16(%rdi), %r12d
- movl %r12d, 16(%rdi)
- # Cleaning up subroutine
- movq %rbp, %rsp
- popq %rbp
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement