Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
9070 hidnplayr 1
;    blowfish-ctr.inc - Blowfish Counter Mode
2
;
3
;    Copyright (C) 2016 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
struct blowfish_ctr_context blowfish_context
19
        counter rb BLOWFISH_BLOCKSIZE
20
        output  rb BLOWFISH_BLOCKSIZE     ; counter after blowfish_crypt
21
ends
22
 
23
 
24
proc blowfish_ctr_init _counter
25
        push    ebx esi edi
26
 
27
        mcall   68, 12, sizeof.blowfish_ctr_context
28
        ; handle errors
29
        mov     ecx, BLOWFISH_BLOCKSIZE/4
30
        mov     esi, [_counter]
31
        lea     edi, [eax + blowfish_ctr_context.counter]
32
        rep movsd
33
        ; rep movsd is slow, but we don't care while init
34
 
35
        pop     edi esi ebx
36
        ret
37
endp
38
 
39
 
40
proc blowfish_ctr_crypt _ctx, _in, _out
41
 
42
        push    ebx esi edi
43
 
44
        DEBUGF  1,'plain  : '
45
        stdcall dump_hex, [_in], 4
46
 
47
        mov     esi, [_ctx]
48
        lea     eax, [esi + blowfish_ctr_context.key]
49
        lea     ebx, [esi + blowfish_ctr_context.counter]
50
        lea     ecx, [esi + blowfish_ctr_context.output]
51
 
52
        stdcall blowfish_encrypt, eax, ebx, ecx   ; Key, in, out
53
 
54
        mov     ebx, [_ctx]
55
        mov     esi, [_in]
56
        mov     edi, [_out]
57
 
58
        mov     eax, [esi + 4*0]
59
        xor     eax, dword[ebx + blowfish_ctr_context.output + 4*0]
60
        mov     [edi + 4*0], eax
61
 
62
        mov     eax, [esi + 4*1]
63
        xor     eax, dword[ebx + blowfish_ctr_context.output + 4*1]
64
        mov     [edi + 4*1], eax
65
 
66
        mov     eax, [esi + 4*2]
67
        xor     eax, dword[ebx + blowfish_ctr_context.output + 4*2]
68
        mov     [edi + 4*2], eax
69
 
70
        mov     eax, [esi + 4*3]
71
        xor     eax, dword[ebx + blowfish_ctr_context.output + 4*3]
72
        mov     [edi + 4*3], eax
73
 
74
; Increment counter
75
        mov     esi, [_ctx]
76
 
77
        mov     eax, dword[esi + blowfish_ctr_context.counter + 4*0]
78
        mov     ebx, dword[esi + blowfish_ctr_context.counter + 4*1]
79
        mov     ecx, dword[esi + blowfish_ctr_context.counter + 4*2]
80
        mov     edx, dword[esi + blowfish_ctr_context.counter + 4*3]
81
 
82
        bswap   eax
83
        bswap   ebx
84
        bswap   ecx
85
        bswap   edx
86
 
9106 hidnplayr 87
        adc     edx, 1
9070 hidnplayr 88
        adc     ecx, 0
89
        adc     ebx, 0
90
        adc     eax, 0
91
 
92
        bswap   eax
93
        bswap   ebx
94
        bswap   ecx
95
        bswap   edx
96
 
97
        mov     dword[esi + blowfish_ctr_context.counter + 4*0], eax
98
        mov     dword[esi + blowfish_ctr_context.counter + 4*1], ebx
99
        mov     dword[esi + blowfish_ctr_context.counter + 4*2], ecx
100
        mov     dword[esi + blowfish_ctr_context.counter + 4*3], edx
101
 
102
        DEBUGF  1,'cipher : '
103
        stdcall dump_hex, [_out], 4
104
 
105
        pop     edi esi ebx
106
        ret
107
endp