-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes256-ctr.inc
executable file
·109 lines (85 loc) · 3.29 KB
/
aes256-ctr.inc
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
; aes256-ctr.inc - AES256 Counter Mode
;
; Copyright (C) 2016 Ivan Baravy (dunkaist)
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
struct aes256_ctr_context aes256_context
counter rb 16
output rb 16 ; counter after aes_crypt
ends
proc aes256_ctr_init _counter
push ebx esi edi
mcall 68, 12, sizeof.aes256_ctr_context
; handle errors
mov ecx, 16/4
mov esi, [_counter]
lea edi, [eax + aes256_ctr_context.counter]
rep movsd
; rep movsd is slow, but we don't care while init
pop edi esi ebx
ret
endp
proc aes256_ctr_crypt _ctx, _in, _out
push ebx esi edi
DEBUGF 1,'plain : '
stdcall dump_128bit_hex, [_in]
DEBUGF 1,'\n'
mov esi, [_ctx]
lea eax, [esi + aes256_ctr_context.key]
lea ebx, [esi + aes256_ctr_context.counter]
lea ecx, [esi + aes256_ctr_context.output]
stdcall aes256_encrypt, eax, ebx, ecx ; Key, in, out
mov ebx, [_ctx]
mov esi, [_in]
mov edi, [_out]
mov eax, [esi + 4*0]
xor eax, dword[ebx + aes256_ctr_context.output + 4*0]
mov [edi + 4*0], eax
mov eax, [esi + 4*1]
xor eax, dword[ebx + aes256_ctr_context.output + 4*1]
mov [edi + 4*1], eax
mov eax, [esi + 4*2]
xor eax, dword[ebx + aes256_ctr_context.output + 4*2]
mov [edi + 4*2], eax
mov eax, [esi + 4*3]
xor eax, dword[ebx + aes256_ctr_context.output + 4*3]
mov [edi + 4*3], eax
; Increment counter
mov esi, [_ctx]
mov eax, dword[esi + aes256_ctr_context.counter + 4*0]
mov ebx, dword[esi + aes256_ctr_context.counter + 4*1]
mov ecx, dword[esi + aes256_ctr_context.counter + 4*2]
mov edx, dword[esi + aes256_ctr_context.counter + 4*3]
bswap eax
bswap ebx
bswap ecx
bswap edx
inc edx
adc ecx, 0
adc ebx, 0
adc eax, 0
bswap eax
bswap ebx
bswap ecx
bswap edx
mov dword[esi + aes256_ctr_context.counter + 4*0], eax
mov dword[esi + aes256_ctr_context.counter + 4*1], ebx
mov dword[esi + aes256_ctr_context.counter + 4*2], ecx
mov dword[esi + aes256_ctr_context.counter + 4*3], edx
DEBUGF 1,'cipher : '
stdcall dump_128bit_hex, [_out]
DEBUGF 1,'\n\n'
pop edi esi ebx
ret
endp