Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
589 diamond 1
; Implementation of SHA-256 hash algorithm.
2
; Written by diamond in 2007.
3
 
4
iglobal
5
align 4
6
sha256_start_digest:
7
        dd      0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A
8
        dd      0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
9
sha256_const:
10
        dd      0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5
11
        dd      0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5
12
        dd      0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3
13
        dd      0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174
14
        dd      0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC
15
        dd      0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA
16
        dd      0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7
17
        dd      0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967
18
        dd      0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13
19
        dd      0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85
20
        dd      0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3
21
        dd      0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070
22
        dd      0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5
23
        dd      0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3
24
        dd      0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208
25
        dd      0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
26
endg
27
 
28
uglobal
29
align 4
30
sha256_buf      rb      64
31
sha256_digest   rd      8
32
sha256_count    dd      ?
33
sha256_size     dq      ?
34
endg
35
 
36
sha256_init:
37
        mov     edi, sha256_digest
38
        mov     esi, sha256_start_digest
39
        push    8
40
        pop     ecx
41
        rep     movsd
42
        xor     eax, eax
43
        stosd           ; 0 bytes in buffer
44
        stosd
45
        stosd           ; 0 bytes processed
46
        ret
47
 
48
; Core of SHA-256: transform 64-byte 'sha256_buf' to updated 'sha256_digest'
49
sha256_transform:
50
        mov     esi, sha256_buf
51
        mov     edi, esi
52
rept 16
53
{
54
        lodsd
55
        bswap   eax
56
        stosd
57
}
58
        push    ebp
59
        mov     ebp, [esi+7*4]
60
        mov     edi, [esi+6*4]
61
        push    dword [esi+5*4]
62
        push    dword [esi+4*4]
63
        push    dword [esi+3*4]
64
        push    dword [esi+2*4]
65
        push    dword [esi+1*4]
66
        push    dword [esi+0*4]
67
        xor     ecx, ecx
68
.loop:
69
macro cmd1 cmd,a,b
70
{
71
if (b and 7) = 7
72
        cmd     a, ebp
73
else if (b and 7) = 6
74
        cmd     a, edi
75
else
76
        cmd     a, [esp+(b and 7)*4]
77
end if
78
}
79
macro cmd2 cmd,a,b
80
{
81
if (a and 7) = 7
82
        cmd     ebp, b
83
else if (a and 7) = 6
84
        cmd     edi, b
85
else
86
        cmd     [esp+(a and 7)*4], b
87
end if
88
}
89
rept 16 counter
90
{
91
cmd1    mov,    eax, (5-counter)
92
        ror     eax, 6
93
        mov     edx, eax
94
        ror     eax, 5
95
        xor     edx, eax
96
        ror     eax, 14
97
        xor     edx, eax
98
cmd1    mov,    eax, (6-counter)
99
cmd1    mov,    esi, (7-counter)
100
        xor     eax, esi
101
cmd1    and,    eax, (5-counter)
102
        xor     eax, esi
103
        add     edx, eax
104
        add     edx, [sha256_const+ecx+(counter-1)*4]
105
        add     edx, dword [sha256_buf+(counter-1)*4]
106
        test    ecx, ecx
107
        jz      @f
108
        mov     eax, dword [sha256_buf+((counter-3) and 15)*4]
109
        mov     esi, eax
110
        ror     eax, 17
111
        shr     esi, 10
112
        xor     esi, eax
113
        ror     eax, 2
114
        xor     esi, eax
115
        add     esi, dword [sha256_buf+((counter-8) and 15)*4]
116
        mov     eax, dword [sha256_buf+(counter and 15)*4]
117
        mov     ebx, eax
118
        ror     eax, 7
119
        shr     ebx, 3
120
        xor     ebx, eax
121
        ror     eax, 11
122
        xor     ebx, eax
123
        add     esi, ebx
124
        add     dword [sha256_buf+(counter-1)*4], esi
125
        add     edx, esi
126
@@:
127
cmd1    add,    edx, (8-counter)
128
cmd2    mov,    (8-counter), edx
129
cmd2    add,    (4-counter), edx
130
cmd1    mov,    ebx, (1-counter)
131
        mov     eax, ebx
132
cmd1    mov,    edx, (2-counter)
133
        mov     esi, ebx
134
        ror     eax, 2
135
        or      esi, edx
136
        and     ebx, edx
137
cmd1    and,    esi, (3-counter)
138
        mov     edx, eax
139
        or      esi, ebx
140
        ror     eax, 11
141
        xor     edx, eax
142
        ror     eax, 9
143
        xor     edx, eax
144
        add     esi, edx
145
cmd2    add,    (8-counter), esi
146
}
147
purge cmd1,cmd2
148
        add     cl, 64
149
        jnz     .loop
150
        mov     esi, sha256_digest
151
        pop     eax
152
        add     [esi+0*4], eax
153
        pop     eax
154
        add     [esi+1*4], eax
155
        pop     eax
156
        add     [esi+2*4], eax
157
        pop     eax
158
        add     [esi+3*4], eax
159
        pop     eax
160
        add     [esi+4*4], eax
161
        pop     eax
162
        add     [esi+5*4], eax
163
        add     [esi+6*4], edi
164
        add     [esi+7*4], ebp
165
        pop     ebp
166
        ret
167
 
168
sha256_update.transform:
169
        push    esi edx
170
        call    sha256_transform
171
        pop     edx esi
172
        mov     [sha256_count], ecx
173
 
174
sha256_update:
175
; in: esi->data, edx=size
176
        mov     eax, 64
177
        sub     eax, [sha256_count]
178
        sub     eax, edx
179
        sbb     ecx, ecx
180
        and     ecx, eax
181
        add     ecx, edx
182
        sub     edx, ecx
183
        mov     edi, sha256_buf
184
        add     edi, [sha256_count]
185
        add     [sha256_count], ecx
186
        add     dword [sha256_size], ecx
187
        adc     dword [sha256_size+4], 0
188
        rep     movsb
189
        cmp     edi, sha256_buf+64
190
        jz      .transform
191
.ret:
192
        ret
193
 
194
sha256_final:
195
; out: edi->digest
196
        push    edi
197
        mov     eax, [sha256_count]
198
        mov     [sha256_buf+eax], 0x80
199
        inc     eax
200
        cmp     al, 64-8
201
        jbe     @f
202
        lea     edi, [sha256_buf+eax]
203
        push    64
204
        pop     ecx
205
        sub     ecx, eax
206
        xor     eax, eax
207
        rep     stosb
208
        push    edx
209
        call    sha256_transform
210
        pop     edx
211
        xor     eax, eax
212
@@:
213
        push    64-8
214
        pop     ecx
215
        sub     ecx, eax
216
        lea     edi, [sha256_buf+eax]
217
        xor     eax, eax
218
        rep     stosb
219
        mov     eax, dword [sha256_size]
220
        mov     edx, dword [sha256_size+4]
221
        shld    edx, eax, 3
222
        shl     eax, 3
223
        bswap   edx
224
        bswap   eax
225
        xchg    eax, edx
226
        stosd
227
        xchg    eax, edx
228
        stosd
229
        call    sha256_transform
230
        mov     esi, sha256_digest
231
        mov     cl, 8
232
        pop     edi
233
@@:
234
        lodsd
235
        bswap   eax
236
        stosd
237
        loop    @b
238
        ret