aboutsummaryrefslogtreecommitdiffstats
path: root/modules/sha_x86/sha1core.S
blob: 57ed7af2306f4baa2935dff30265c6af914a0833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
   .file "sha1core.S"
   .text
   .p2align 4,,15

.global sha160_core
   .type   sha160_core, @function
sha160_core:
        pushl   %ebp
        pushl   %edi
        pushl   %esi
        pushl   %ebx

        movl    24(%esp), %ebp   # byte input[64]
        movl    28(%esp), %edi   # u32bit W[80]

        movl    $0, %esi  # loop counter

        .p2align 4,,7
.LOAD_INPUT_LOOP:
        movl    0(%ebp), %eax
        bswapl  %eax

        movl    4(%ebp), %ebx
        bswapl  %ebx
        movl    %eax, 0(%edi,%esi,4)

        movl    8(%ebp), %ecx
        bswapl  %ecx
        movl    %ebx, 4(%edi,%esi,4)

        movl    12(%ebp), %edx
        bswapl  %edx
        movl    %ecx, 8(%edi,%esi,4)

        movl    %edx, 12(%edi,%esi,4)

        addl    $4, %esi
        addl    $16, %ebp
        cmpl    $16, %esi
        jne     .LOAD_INPUT_LOOP

     leal    64(%edi), %ebp

        .p2align 4,,7
.EXPANSION_LOOP:
        addl $4, %esi

        xorl     %eax,  %eax
        movl  -4(%ebp), %ebx
        movl  -8(%ebp), %ecx
        movl -12(%ebp), %edx

        xorl -20(%ebp), %eax
        xorl -24(%ebp), %ebx
        xorl -28(%ebp), %ecx
        xorl -32(%ebp), %edx

        xorl -44(%ebp), %eax
        xorl -48(%ebp), %ebx
        xorl -52(%ebp), %ecx
        xorl -52(%ebp), %eax

        xorl -56(%ebp), %edx
        xorl -56(%ebp), %ebx
        xorl -60(%ebp), %ecx
        xorl -64(%ebp), %edx

        roll $1, %edx

        roll $1, %ecx
        movl %edx, (%ebp)

        roll $1, %ebx
        movl %ecx, 4(%ebp)

        xorl %edx, %eax
        movl %ebx, 8(%ebp)
        roll $1, %eax
        movl %eax, 12(%ebp)

        addl $16, %ebp
        cmpl $80, %esi
    jne .EXPANSION_LOOP

   movl 20(%esp), %ebp
   movl 0(%ebp), %eax
   movl 4(%ebp), %ebx
   movl 8(%ebp), %ecx
   movl 12(%ebp), %edx
   movl 16(%ebp), %esi

#define MAGIC1 0x5A827999
#define MAGIC2 0x6ED9EBA1
#define MAGIC3 0x8F1BBCDC
#define MAGIC4 0xCA62C1D6

#define F1(A, B, C, D, E, TEMP, MSG) \
   addl 4*MSG(%edi), E          ; \
   movl C, TEMP                 ; \
   roll $5, A                   ; \
   xorl D, TEMP                 ; \
   addl A, E                    ; \
   andl B, TEMP                 ; \
   rorl $2, B                   ; \
   xorl D, TEMP                 ; \
   leal MAGIC1(E,TEMP,1), E     ; \
   rorl $5, A                   ;

#define F2_OR_F4(A, B, C, D, E, TEMP, MSG, MAGIC) \
   addl 4*MSG(%edi), E          ; \
   movl B, TEMP                 ; \
   roll $5, A                   ; \
   xorl D, TEMP                 ; \
   addl A, E                    ; \
   xorl C, TEMP                 ; \
   rorl $2, B                   ; \
   leal MAGIC(E,TEMP,1), E      ; \
   rorl $5, A                   ;

#define F3(A, B, C, D, E, TEMP, MSG) \
   addl 4*MSG(%edi), E          ; \
   movl B, TEMP                 ; \
   roll $5, A                   ; \
   orl  C, TEMP                 ; \
   movl B, (%edi)               ; \
   andl D, TEMP                 ; \
   andl C, (%edi)               ; \
   orl  (%edi), TEMP            ; \
   addl A, E                    ; \
   leal MAGIC3(E,TEMP,1), E     ; \
   rorl $2, B                   ; \
   rorl $5, A                   ;

#define F2(A, B, C, D, E, TEMP, MSG) \
   F2_OR_F4(A, B, C, D, E, TEMP, MSG, MAGIC2)

#define F4(A, B, C, D, E, TEMP, MSG) \
   F2_OR_F4(A, B, C, D, E, TEMP, MSG, MAGIC4)

#define F_BLOCK(F, MSG) \
    F(%eax, %ebx, %ecx, %edx, %esi, %ebp, (MSG+0)) \
    F(%esi, %eax, %ebx, %ecx, %edx, %ebp, (MSG+1)) \
    F(%edx, %esi, %eax, %ebx, %ecx, %ebp, (MSG+2)) \
    F(%ecx, %edx, %esi, %eax, %ebx, %ebp, (MSG+3)) \
    F(%ebx, %ecx, %edx, %esi, %eax, %ebp, (MSG+4))

   F_BLOCK(F1, 0)
   F_BLOCK(F1, 5)
   F_BLOCK(F1, 10)
   F_BLOCK(F1, 15)

   F_BLOCK(F2, 20)
   F_BLOCK(F2, 25)
   F_BLOCK(F2, 30)
   F_BLOCK(F2, 35)

   F_BLOCK(F3, 40)
   F_BLOCK(F3, 45)
   F_BLOCK(F3, 50)
   F_BLOCK(F3, 55)

   F_BLOCK(F4, 60)
   F_BLOCK(F4, 65)
   F_BLOCK(F4, 70)
   F_BLOCK(F4, 75)

   movl 20(%esp), %ebp
   addl %eax, 0(%ebp)
   addl %ebx, 4(%ebp)
   addl %ecx, 8(%ebp)
   addl %edx, 12(%ebp)
   addl %esi, 16(%ebp)

        popl    %ebx
        popl    %esi
        popl    %edi
        popl    %ebp
        ret