;***************************************************************************************************
;
; void MakeSHA1Helper_asm(const void *pBuffer, void *hash, DWORD len)
;
;***************************************************************************************************

.586
.model flat, c



ROUND1      MACRO   a, b, c, d, e, num
    mov esi, c
    mov ebp, [w+(num+0)*4]
    xor esi, d
    add ebp, 05a827999h
    and esi, b
    add ebp, e
    xor esi, d
    mov e, a
    add ebp, esi
    rol e, 5
    ror b, 2
    add e, ebp
ENDM


ROUND2      MACRO   a, b, c, d, e, num
    mov ebp, [w+(num+20)*4]
    mov esi, b
    add ebp, e
    xor esi, c
    add ebp, 06ed9eba1h
    mov e, a
    xor esi, d
    rol e, 5
    add esi, ebp
    ror b, 2
    add e, esi
ENDM


ROUND3      MACRO   a, b, c, d, e, num
    mov esi, b
    mov ebp, e
    or  esi, c
    mov e, b
    and esi, d
    add ebp, [w+(num+40)*4]
    and e, c
    add ebp, 08f1bbcdch
    or  esi, e
    add esi, ebp
    mov e, a
    rol e, 5
    add e, esi
    ror b, 2
ENDM


ROUND4      MACRO   a, b, c, d, e, num
    mov ebp, [w+(num+60)*4]
    mov esi, b
    add ebp, e
    xor esi, c
    add ebp, 0ca62c1d6h
    xor esi, d
    mov e, a
    add esi, ebp
    rol e, 5
    ror b, 2
    add e, esi
ENDM


REVERSE_ENDIAN      MACRO   num
    mov eax, [esi+num]
    mov ebx, [esi+num+4]
    bswap eax
    bswap ebx
    mov [w+num], eax
    mov [w+num+4], ebx

    mov ecx, [esi+num+ 8]
    mov edx, [esi+num+12]
    bswap ecx
    bswap edx
    mov [w+num+ 8], ecx
    mov [w+num+12], edx
ENDM


PREPROCESS1      MACRO
    mov eax, [w+((0+16)*4-12)]
    mov ebx, [w+((1+16)*4-12)]
    mov ecx, [w+((2+16)*4-12)]
ENDM


PREPROCESS2      MACRO   num
    xor eax, [w+((num+0+16)*4-32)]
    xor ebx, [w+((num+1+16)*4-32)]
    xor ecx, [w+((num+2+16)*4-32)]
    xor eax, [w+((num+0+16)*4-56)]
    xor ebx, [w+((num+1+16)*4-56)]
    xor ecx, [w+((num+2+16)*4-56)]
    xor eax, [w+((num+0+16)*4-64)]
    xor ebx, [w+((num+1+16)*4-64)]
    xor ecx, [w+((num+2+16)*4-64)]
    rol eax, 1
    rol ebx, 1
    rol ecx, 1
    mov [w+(num+0+16)*4], eax
    mov [w+(num+1+16)*4], ebx
    mov [w+(num+2+16)*4], ecx
ENDM


PREPROCESS3      MACRO   num
    xor eax, [w+((num+0+16)*4-32)]
    xor eax, [w+((num+0+16)*4-56)]
    xor eax, [w+((num+0+16)*4-64)]
    rol eax, 1
    mov [w+(num+0+16)*4], eax
ENDM



;***************************************************************************************************
; 定義

; 引数
pBuffer equ     <esp+424>
hash    equ     <esp+428>
len     equ     <esp+432>

; ローカル変数
w       equ     <esp>


;***************************************************************************************************

.code

MakeSHA1Helper_asm      proc

    mov eax, [esp+12]       ; eax <- len
    shr eax, 6
    jnz PRE
    ret

  PRE:
    push ebp
    push ecx
    push esi
    push edi
    push ebx
    sub esp, 400
    mov [len], eax
    mov esi, [pBuffer]


  MAINLOOP:
    REVERSE_ENDIAN      0
    REVERSE_ENDIAN     16
    REVERSE_ENDIAN     32
    REVERSE_ENDIAN     48

    PREPROCESS1
    PREPROCESS2         0
    PREPROCESS2         3
    PREPROCESS2         6
    PREPROCESS2         9
    PREPROCESS2        12
    PREPROCESS2        15
    PREPROCESS2        18
    PREPROCESS2        21
    PREPROCESS2        24
    PREPROCESS2        27
    PREPROCESS2        30
    PREPROCESS2        33
    PREPROCESS2        36
    PREPROCESS2        39
    PREPROCESS2        42
    PREPROCESS2        45
    PREPROCESS2        48
    PREPROCESS2        51
    PREPROCESS2        54
    PREPROCESS2        57
    PREPROCESS2        60
    PREPROCESS3        63


    mov esi, [hash]
    mov eax, [esi]
    mov ebx, [esi+4]
    mov ecx, [esi+8]
    mov edx, [esi+12]
    mov edi, [esi+16]


    ROUND1  eax,ebx,ecx,edx,edi, 0
    ROUND1  edi,eax,ebx,ecx,edx, 1
    ROUND1  edx,edi,eax,ebx,ecx, 2
    ROUND1  ecx,edx,edi,eax,ebx, 3
    ROUND1  ebx,ecx,edx,edi,eax, 4
    ROUND1  eax,ebx,ecx,edx,edi, 5
    ROUND1  edi,eax,ebx,ecx,edx, 6
    ROUND1  edx,edi,eax,ebx,ecx, 7
    ROUND1  ecx,edx,edi,eax,ebx, 8
    ROUND1  ebx,ecx,edx,edi,eax, 9
    ROUND1  eax,ebx,ecx,edx,edi,10
    ROUND1  edi,eax,ebx,ecx,edx,11
    ROUND1  edx,edi,eax,ebx,ecx,12
    ROUND1  ecx,edx,edi,eax,ebx,13
    ROUND1  ebx,ecx,edx,edi,eax,14
    ROUND1  eax,ebx,ecx,edx,edi,15
    ROUND1  edi,eax,ebx,ecx,edx,16
    ROUND1  edx,edi,eax,ebx,ecx,17
    ROUND1  ecx,edx,edi,eax,ebx,18
    ROUND1  ebx,ecx,edx,edi,eax,19

    ROUND2  eax,ebx,ecx,edx,edi, 0
    ROUND2  edi,eax,ebx,ecx,edx, 1
    ROUND2  edx,edi,eax,ebx,ecx, 2
    ROUND2  ecx,edx,edi,eax,ebx, 3
    ROUND2  ebx,ecx,edx,edi,eax, 4
    ROUND2  eax,ebx,ecx,edx,edi, 5
    ROUND2  edi,eax,ebx,ecx,edx, 6
    ROUND2  edx,edi,eax,ebx,ecx, 7
    ROUND2  ecx,edx,edi,eax,ebx, 8
    ROUND2  ebx,ecx,edx,edi,eax, 9
    ROUND2  eax,ebx,ecx,edx,edi,10
    ROUND2  edi,eax,ebx,ecx,edx,11
    ROUND2  edx,edi,eax,ebx,ecx,12
    ROUND2  ecx,edx,edi,eax,ebx,13
    ROUND2  ebx,ecx,edx,edi,eax,14
    ROUND2  eax,ebx,ecx,edx,edi,15
    ROUND2  edi,eax,ebx,ecx,edx,16
    ROUND2  edx,edi,eax,ebx,ecx,17
    ROUND2  ecx,edx,edi,eax,ebx,18
    ROUND2  ebx,ecx,edx,edi,eax,19

    ROUND3  eax,ebx,ecx,edx,edi, 0
    ROUND3  edi,eax,ebx,ecx,edx, 1
    ROUND3  edx,edi,eax,ebx,ecx, 2
    ROUND3  ecx,edx,edi,eax,ebx, 3
    ROUND3  ebx,ecx,edx,edi,eax, 4
    ROUND3  eax,ebx,ecx,edx,edi, 5
    ROUND3  edi,eax,ebx,ecx,edx, 6
    ROUND3  edx,edi,eax,ebx,ecx, 7
    ROUND3  ecx,edx,edi,eax,ebx, 8
    ROUND3  ebx,ecx,edx,edi,eax, 9
    ROUND3  eax,ebx,ecx,edx,edi,10
    ROUND3  edi,eax,ebx,ecx,edx,11
    ROUND3  edx,edi,eax,ebx,ecx,12
    ROUND3  ecx,edx,edi,eax,ebx,13
    ROUND3  ebx,ecx,edx,edi,eax,14
    ROUND3  eax,ebx,ecx,edx,edi,15
    ROUND3  edi,eax,ebx,ecx,edx,16
    ROUND3  edx,edi,eax,ebx,ecx,17
    ROUND3  ecx,edx,edi,eax,ebx,18
    ROUND3  ebx,ecx,edx,edi,eax,19

    ROUND4  eax,ebx,ecx,edx,edi, 0
    ROUND4  edi,eax,ebx,ecx,edx, 1
    ROUND4  edx,edi,eax,ebx,ecx, 2
    ROUND4  ecx,edx,edi,eax,ebx, 3
    ROUND4  ebx,ecx,edx,edi,eax, 4
    ROUND4  eax,ebx,ecx,edx,edi, 5
    ROUND4  edi,eax,ebx,ecx,edx, 6
    ROUND4  edx,edi,eax,ebx,ecx, 7
    ROUND4  ecx,edx,edi,eax,ebx, 8
    ROUND4  ebx,ecx,edx,edi,eax, 9
    ROUND4  eax,ebx,ecx,edx,edi,10
    ROUND4  edi,eax,ebx,ecx,edx,11
    ROUND4  edx,edi,eax,ebx,ecx,12
    ROUND4  ecx,edx,edi,eax,ebx,13
    ROUND4  ebx,ecx,edx,edi,eax,14
    ROUND4  eax,ebx,ecx,edx,edi,15
    ROUND4  edi,eax,ebx,ecx,edx,16
    ROUND4  edx,edi,eax,ebx,ecx,17
    ROUND4  ecx,edx,edi,eax,ebx,18
    ROUND4  ebx,ecx,edx,edi,eax,19


    mov ebp, [hash]
    add eax, [ebp]
    add ebx, [ebp+4]
    add ecx, [ebp+8]
    add edx, [ebp+12]
    add edi, [ebp+16]
    mov [ebp],    eax
    mov [ebp+4],  ebx
    mov [ebp+8],  ecx
    mov [ebp+12], edx
    mov [ebp+16], edi

    mov esi, [pBuffer]
    mov eax, [len]
    add esi, 64
    sub eax, 1
    mov [pBuffer], esi
    mov [len], eax
    jnz MAINLOOP


    add esp, 400
    pop ebx
    pop edi
    pop esi
    pop ecx
    pop ebp
    ret

MakeSHA1Helper_asm      endp

end
