Subversion Repositories Kolibri OS

Rev

Rev 6465 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7698 dunkaist 1
;    libcrash -- cryptographic hash functions
2
;
3
;    Copyright (C) 2012-2013,2016,2019 Ivan Baravy (dunkaist)
4
;
5
;    This program is free software: you can redistribute it and/or modify
6
;    it under the terms of the GNU General Public License as published by
7
;    the Free Software Foundation, either version 3 of the License, or
8
;    (at your option) any later version.
9
;
10
;    This program is distributed in the hope that it will be useful,
11
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
;    GNU General Public License for more details.
14
;
15
;    You should have received a copy of the GNU General Public License
16
;    along with this program.  If not, see .
17
 
18
 
19
SHA224256_BLOCK_SIZE = 64
20
SHA224_BLOCK_SIZE    = SHA224256_BLOCK_SIZE
21
SHA256_BLOCK_SIZE    = SHA224256_BLOCK_SIZE
22
SHA224_HASH_SIZE     = 28
23
SHA256_HASH_SIZE     = 32
24
 
25
SHA224256_INIT_SIZE  = 32
26
SHA224256_ALIGN      = 4
27
SHA224256_ALIGN_MASK = SHA224256_ALIGN - 1
28
 
29
struct ctx_sha224256
30
        hash            rb SHA224256_INIT_SIZE
31
        block           rb SHA224256_BLOCK_SIZE
32
        index           rd 1
33
        msglen_0        rd 1
34
        msglen_1        rd 1
35
ends
36
 
37
if defined sizeof.crash_ctx
38
  assert sizeof.crash_ctx >= sizeof.ctx_sha224256
39
end if
40
 
41
 
42
macro sha224256._.chn x, y, z
43
{
44
        mov     eax, [y]
45
        xor     eax, [z]
46
        and     eax, [x]
47
        xor     eax, [z]
48
}
49
 
50
macro sha224256._.maj x, y, z
51
{
52
        mov     eax, [x]
53
        xor     eax, [y]
54
        and     eax, [z]
55
        mov     ecx, [x]
56
        and     ecx, [y]
57
        xor     eax, ecx
58
}
59
 
60
macro sha224256._.Sigma0 x
61
{
62
        mov     eax, x
63
        mov     ecx, eax
64
        ror     ecx, 2
65
        ror     eax, 13
66
        xor     eax, ecx
67
        mov     ecx, x
68
        ror     ecx, 22
69
        xor     eax, ecx
70
}
71
 
72
macro sha224256._.Sigma1 x
73
{
74
        mov     eax, x
75
        mov     ecx, eax
76
        ror     ecx, 6
77
        ror     eax, 11
78
        xor     eax, ecx
79
        mov     ecx, x
80
        ror     ecx, 25
81
        xor     eax, ecx
82
}
83
 
84
macro sha224256._.sigma0 x
85
{
86
        mov     eax, x
87
        mov     ecx, eax
88
        ror     ecx, 7
89
        ror     eax, 18
90
        xor     eax, ecx
91
        mov     ecx, x
92
        shr     ecx, 3
93
        xor     eax, ecx
94
}
95
 
96
macro sha224256._.sigma1 x
97
{
98
        mov     eax, x
99
        mov     ecx, eax
100
        ror     ecx, 17
101
        ror     eax, 19
102
        xor     eax, ecx
103
        mov     ecx, x
104
        shr     ecx, 10
105
        xor     eax, ecx
106
}
107
 
108
macro sha224256._.recalculate_w n
109
{
110
        mov     edx, [w + ((n-2) and 15)*4]
111
        sha224256._.sigma1  edx
112
        add     eax, [w + ((n-7) and 15)*4]
113
        push    eax
114
        mov     edx, [w + ((n-15) and 15)*4]
115
        sha224256._.sigma0  edx
116
        pop     ecx
117
        add     eax, ecx
118
        add     [w + (n)*4], eax
119
}
120
 
121
macro sha224256._.round a, b, c, d, e, f, g, h, k
122
{
123
        mov     ebx, [h]
124
        mov     edx, [e]
125
        sha224256._.Sigma1  edx
126
 
127
        add     ebx, eax
128
        sha224256._.chn     e, f, g
129
 
130
        add     ebx, eax
131
        add     ebx, [k]
132
        add     ebx, edi
133
 
134
        add     [d], ebx
135
 
136
        mov     edx, [a]
137
        sha224256._.Sigma0  edx
138
        add     ebx, eax
139
        sha224256._.maj     a, b, c
140
        add     eax, ebx
141
        mov     [h], eax
142
}
143
 
144
 
145
macro sha224256._.round_1_16 a, b, c, d, e, f, g, h, n
146
{
147
 
148
        mov     eax, [esi + (n)*4]
149
        bswap   eax
150
 
151
        mov     dword[w + (n)*4], eax
152
        mov     edi, eax
153
        sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n)*4)
154
}
155
 
156
macro sha224256._.round_17_64 a, b, c, d, e, f, g, h, n, rep_num
157
{
158
        sha224256._.recalculate_w n
159
        mov     edi, [w + (n)*4]
160
        sha224256._.round a, b, c, d, e, f, g, h, (sha256_table + (n+16*rep_num)*4)
161
}
162
 
163
 
164
proc sha224.init _ctx
165
        mov     ebx, [_ctx]
166
        lea     edi, [ebx + ctx_sha224256.hash]
167
        mov     esi, sha224._.hash_init
168
        mov     ecx, SHA224256_INIT_SIZE/4
169
        rep     movsd
170
        xor     eax, eax
171
        mov     [ebx + ctx_sha224256.index], eax
172
        mov     [ebx + ctx_sha224256.msglen_0], eax
173
        mov     [ebx + ctx_sha224256.msglen_1], eax
174
        ret
175
endp
176
 
177
 
178
proc sha256.init _ctx
179
        mov     ebx, [_ctx]
180
        lea     edi, [ebx + ctx_sha224256.hash]
181
        mov     esi, sha256._.hash_init
182
        mov     ecx, SHA224256_INIT_SIZE/4
183
        rep     movsd
184
        xor     eax, eax
185
        mov     [ebx + ctx_sha224256.index], eax
186
        mov     [ebx + ctx_sha224256.msglen_0], eax
187
        mov     [ebx + ctx_sha224256.msglen_1], eax
188
        ret
189
endp
190
 
191
 
192
proc sha224256._.block _hash
193
locals
194
        w       rd 64
195
        A       rd 1
196
        B       rd 1
197
        C       rd 1
198
        D       rd 1
199
        E       rd 1
200
        F       rd 1
201
        G       rd 1
202
        H       rd 1
203
endl
204
        mov     edi, [_hash]
205
        mov     eax, [edi + 0x00]
206
        mov     [A], eax
207
        mov     eax, [edi + 0x04]
208
        mov     [B], eax
209
        mov     eax, [edi + 0x08]
210
        mov     [C], eax
211
        mov     eax, [edi + 0x0c]
212
        mov     [D], eax
213
        mov     eax, [edi + 0x10]
214
        mov     [E], eax
215
        mov     eax, [edi + 0x14]
216
        mov     [F], eax
217
        mov     eax, [edi + 0x18]
218
        mov     [G], eax
219
        mov     eax, [edi + 0x1c]
220
        mov     [H], eax
221
 
222
        sha224256._.round_1_16  A, B, C, D, E, F, G, H,  0
223
        sha224256._.round_1_16  H, A, B, C, D, E, F, G,  1
224
        sha224256._.round_1_16  G, H, A, B, C, D, E, F,  2
225
        sha224256._.round_1_16  F, G, H, A, B, C, D, E,  3
226
        sha224256._.round_1_16  E, F, G, H, A, B, C, D,  4
227
        sha224256._.round_1_16  D, E, F, G, H, A, B, C,  5
228
        sha224256._.round_1_16  C, D, E, F, G, H, A, B,  6
229
        sha224256._.round_1_16  B, C, D, E, F, G, H, A,  7
230
        sha224256._.round_1_16  A, B, C, D, E, F, G, H,  8
231
        sha224256._.round_1_16  H, A, B, C, D, E, F, G,  9
232
        sha224256._.round_1_16  G, H, A, B, C, D, E, F, 10
233
        sha224256._.round_1_16  F, G, H, A, B, C, D, E, 11
234
        sha224256._.round_1_16  E, F, G, H, A, B, C, D, 12
235
        sha224256._.round_1_16  D, E, F, G, H, A, B, C, 13
236
        sha224256._.round_1_16  C, D, E, F, G, H, A, B, 14
237
        sha224256._.round_1_16  B, C, D, E, F, G, H, A, 15
238
 
239
repeat 3
240
        sha224256._.round_17_64 A, B, C, D, E, F, G, H,  0, %
241
        sha224256._.round_17_64 H, A, B, C, D, E, F, G,  1, %
242
        sha224256._.round_17_64 G, H, A, B, C, D, E, F,  2, %
243
        sha224256._.round_17_64 F, G, H, A, B, C, D, E,  3, %
244
        sha224256._.round_17_64 E, F, G, H, A, B, C, D,  4, %
245
        sha224256._.round_17_64 D, E, F, G, H, A, B, C,  5, %
246
        sha224256._.round_17_64 C, D, E, F, G, H, A, B,  6, %
247
        sha224256._.round_17_64 B, C, D, E, F, G, H, A,  7, %
248
        sha224256._.round_17_64 A, B, C, D, E, F, G, H,  8, %
249
        sha224256._.round_17_64 H, A, B, C, D, E, F, G,  9, %
250
        sha224256._.round_17_64 G, H, A, B, C, D, E, F, 10, %
251
        sha224256._.round_17_64 F, G, H, A, B, C, D, E, 11, %
252
        sha224256._.round_17_64 E, F, G, H, A, B, C, D, 12, %
253
        sha224256._.round_17_64 D, E, F, G, H, A, B, C, 13, %
254
        sha224256._.round_17_64 C, D, E, F, G, H, A, B, 14, %
255
        sha224256._.round_17_64 B, C, D, E, F, G, H, A, 15, %
256
end repeat
257
 
258
        mov     edi, [_hash]
259
        mov     eax, [A]
260
        add     [edi + 0x00], eax
261
        mov     eax, [B]
262
        add     [edi + 0x04], eax
263
        mov     eax, [C]
264
        add     [edi + 0x08], eax
265
        mov     eax, [D]
266
        add     [edi + 0x0c], eax
267
        mov     eax, [E]
268
        add     [edi + 0x10], eax
269
        mov     eax, [F]
270
        add     [edi + 0x14], eax
271
        mov     eax, [G]
272
        add     [edi + 0x18], eax
273
        mov     eax, [H]
274
        add     [edi + 0x1c], eax
275
 
276
        ret
277
endp
278
 
279
 
280
sha224.update = sha224256.update
281
sha256.update = sha224256.update
282
proc sha224256.update _ctx, _msg, _size
283
        mov     ebx, [_ctx]
284
        mov     ecx, [_size]
285
        add     [ebx + ctx_sha224256.msglen_0], ecx
286
        adc     [ebx + ctx_sha224256.msglen_1], 0
287
 
288
  .next_block:
289
        mov     ebx, [_ctx]
290
        mov     esi, [_msg]
291
        mov     eax, [ebx + ctx_sha224256.index]
292
        and     eax, SHA224256_BLOCK_SIZE-1
293
        jnz     .copy_to_buf
294
        test    esi, SHA224256_ALIGN_MASK
295
        jnz     .copy_to_buf
296
  .no_copy:
297
        ; data is aligned, hash it in place without copying
298
        mov     ebx, [_ctx]
299
        cmp     [_size], SHA224256_BLOCK_SIZE
300
        jb      .copy_quit
301
        lea     eax, [ebx + ctx_sha224256.hash]
302
        stdcall sha224256._.block, eax
303
        sub     [_size], SHA224256_BLOCK_SIZE
304
        add     esi, SHA224256_BLOCK_SIZE           ; FIXME
305
        jmp     .no_copy
306
 
307
  .copy_to_buf:
308
        lea     edi, [ebx + ctx_sha224256.block]
309
        add     edi, eax
310
        mov     ecx, SHA224256_BLOCK_SIZE
311
        sub     ecx, eax
312
        cmp     [_size], ecx
313
        jb      .copy_quit
314
        sub     [_size], ecx
315
        add     [_msg], ecx
316
        add     [ebx + ctx_sha224256.index], ecx
317
        rep     movsb
318
        lea     eax, [ebx + ctx_sha224256.hash]
319
        lea     esi, [ebx + ctx_sha224256.block]
320
        stdcall sha224256._.block, eax
321
        jmp     .next_block
322
 
323
  .copy_quit:
324
        mov     ebx, [_ctx]
325
        lea     edi, [ebx + ctx_sha224256.block]
326
        mov     eax, [ebx + ctx_sha224256.index]
327
        and     eax, SHA224256_BLOCK_SIZE-1
328
        add     edi, eax
329
        mov     ecx, [_size]
330
        add     [ebx + ctx_sha224256.index], ecx
331
        rep     movsb
332
  .quit:
333
 
334
        ret
335
endp
336
 
337
 
338
sha224.final = sha224256.final
339
sha256.final = sha224256.final
340
proc sha224256.final _ctx
341
        mov     ebx, [_ctx]
342
        lea     edi, [ebx + ctx_sha224256.block]
343
        mov     ecx, [ebx + ctx_sha224256.msglen_0]
344
        and     ecx, SHA224256_BLOCK_SIZE-1
345
        add     edi, ecx
346
        mov     byte[edi], 0x80
347
        inc     edi
348
        neg     ecx
349
        add     ecx, SHA224256_BLOCK_SIZE
350
        cmp     ecx, 8
351
        ja      .last
352
 
353
        dec     ecx
354
        xor     eax, eax
355
        rep     stosb
356
        lea     esi, [ebx + ctx_sha224256.block]
357
        lea     eax, [ebx + ctx_sha224256.hash]
358
        stdcall sha224256._.block, eax
359
        mov     ebx, [_ctx]
360
        lea     edi, [ebx + ctx_sha224256.block]
361
        mov     ecx, SHA224256_BLOCK_SIZE+1
362
  .last:
363
        dec     ecx
364
        sub     ecx, 8
365
        xor     eax, eax
366
        rep     stosb
367
        mov     eax, [ebx + ctx_sha224256.msglen_0]
368
        mov     edx, [ebx + ctx_sha224256.msglen_1]
369
        shld    edx, eax, 3
370
        shl     eax, 3
371
        bswap   eax
372
        bswap   edx
373
        mov     dword[edi], edx
374
        mov     dword[edi+4], eax
375
        lea     esi, [ebx + ctx_sha224256.block]
376
        lea     eax, [ebx + ctx_sha224256.hash]
377
        stdcall sha224256._.block, eax
378
 
379
        mov     ebx, [_ctx]
380
        lea     eax, [ebx + ctx_sha224256.hash]
381
        stdcall sha224256._.postprocess, ebx, eax
382
 
383
        ret
384
endp
385
 
386
 
387
proc sha224256._.postprocess _ctx, _hash
388
        mov     ecx, 8
389
        mov     esi, [_hash]
390
        mov     edi, esi
391
    @@:
392
        lodsd
393
        bswap   eax
394
        stosd
395
        dec     ecx
396
        jnz     @b
397
        ret
398
endp
399
 
400
 
401
proc sha224.oneshot _ctx, _data, _len
402
	stdcall	sha224.init, [_ctx]
403
	stdcall	sha224.update, [_ctx], [_data], [_len]
404
	stdcall	sha224.final, [_ctx]
405
	ret
406
endp
407
 
408
 
409
proc sha256.oneshot _ctx, _data, _len
410
	stdcall	sha256.init, [_ctx]
411
	stdcall	sha256.update, [_ctx], [_data], [_len]
412
	stdcall	sha256.final, [_ctx]
413
	ret
414
endp
415
 
416
 
417
iglobal
418
align SHA224256_ALIGN
419
sha224._.hash_init      dd 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,\
420
                           0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
421
 
422
sha256._.hash_init      dd 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\
423
                           0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
424
 
425
sha256_table            dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
426
                           0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
427
                           0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
428
                           0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
429
                           0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
430
                           0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
431
                           0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
432
                           0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
433
                           0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
434
                           0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
435
                           0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
436
                           0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
437
                           0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
438
                           0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
439
                           0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
440
                           0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
441
endg