Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
6419 hidnplayr 1
;    aes256-cbc.inc - AES256 Cipher Block Chaining
2
;
3
;    Copyright (C) 2016 Jeffrey Amelynck
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 aes256_cbc_context aes256_context
6469 hidnplayr 19
        vector  rb AES256_BLOCKSIZE
6419 hidnplayr 20
ends
21
 
22
 
23
proc aes256_cbc_init _vector
24
        push    ebx esi edi
25
 
26
        mcall   68, 12, sizeof.aes256_cbc_context
27
        ; handle errors
6469 hidnplayr 28
        mov     ecx, AES256_BLOCKSIZE/4
6419 hidnplayr 29
        mov     esi, [_vector]
30
        lea     edi, [eax + aes256_cbc_context.vector]
6469 hidnplayr 31
        rep movsd
6419 hidnplayr 32
        ; rep movsd is slow, but we don't care while init
33
 
34
        pop     edi esi ebx
35
        ret
36
endp
37
 
38
proc aes256_cbc_encrypt _ctx, _in, _out
39
        push    ebx esi edi
40
 
41
        DEBUGF  1,'plain  : '
6469 hidnplayr 42
        stdcall dump_hex, [_in], 4
6419 hidnplayr 43
 
44
        mov     edi, [_ctx]
45
        lea     edi, [edi + aes256_cbc_context.vector]
46
        mov     esi, [_in]
6469 hidnplayr 47
repeat AES256_BLOCKSIZE/4
6419 hidnplayr 48
        lodsd
49
        xor     eax, [edi]
50
        stosd
6469 hidnplayr 51
end repeat
6419 hidnplayr 52
 
53
        mov     esi, [_ctx]
54
        lea     eax, [esi + aes256_cbc_context.key]
55
        lea     ebx, [esi + aes256_cbc_context.vector]
56
        stdcall aes256_encrypt, eax, ebx, [_out]   ; Key, in, out
57
 
58
        mov     esi, [_out]
59
        mov     eax, [_ctx]
60
        lea     edi, [eax + aes256_cbc_context.vector]
6469 hidnplayr 61
repeat AES256_BLOCKSIZE/4
6419 hidnplayr 62
        movsd
6469 hidnplayr 63
end repeat
6419 hidnplayr 64
 
65
        DEBUGF  1,'cipher : '
6469 hidnplayr 66
        stdcall dump_hex, [_out], 4
6419 hidnplayr 67
 
68
        pop     edi esi ebx
69
        ret
70
endp
71
 
72
proc aes256_cbc_decrypt _ctx, _in, _out
6469 hidnplayr 73
 
74
locals
75
        temp_iv rb AES256_BLOCKSIZE
76
endl
77
 
6419 hidnplayr 78
        push    ebx esi edi
79
 
80
        DEBUGF  1,'cipher : '
6469 hidnplayr 81
        stdcall dump_hex, [_in], 4
6419 hidnplayr 82
 
6469 hidnplayr 83
        mov     esi, [_in]
84
        lea     edi, [temp_iv]
85
repeat AES256_BLOCKSIZE/4
86
        movsd
87
end repeat
88
 
6419 hidnplayr 89
        mov     esi, [_ctx]
90
        lea     eax, [esi + aes256_cbc_context.key]
91
        stdcall aes256_decrypt, eax, [_in], [_out]   ; Key, in, out
92
 
93
        mov     esi, [_ctx]
94
        lea     esi, [esi + aes256_cbc_context.vector]
95
        mov     edi, [_out]
6469 hidnplayr 96
repeat AES256_BLOCKSIZE/4
6419 hidnplayr 97
        lodsd
98
        xor     eax, [edi]
99
        stosd
6469 hidnplayr 100
end repeat
6419 hidnplayr 101
 
6469 hidnplayr 102
        lea     esi, [temp_iv]
6419 hidnplayr 103
        mov     edi, [_ctx]
104
        lea     edi, [edi + aes256_cbc_context.vector]
6469 hidnplayr 105
repeat AES256_BLOCKSIZE/4
6419 hidnplayr 106
        movsd
6469 hidnplayr 107
end repeat
6419 hidnplayr 108
 
109
        DEBUGF  1,'plain  : '
6469 hidnplayr 110
        stdcall dump_hex, [_out], 4
6419 hidnplayr 111
 
112
        pop     edi esi ebx
113
        ret
114
endp