2016-05-07 12:42:31 +02:00
|
|
|
; aes256-cbc.inc - AES256 Cipher Block Chaining
|
|
|
|
;
|
|
|
|
; Copyright (C) 2016 Jeffrey Amelynck
|
|
|
|
;
|
|
|
|
; 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_cbc_context aes256_context
|
2016-08-10 18:20:49 +02:00
|
|
|
vector rb AES256_BLOCKSIZE
|
2016-05-07 12:42:31 +02:00
|
|
|
ends
|
|
|
|
|
|
|
|
|
|
|
|
proc aes256_cbc_init _vector
|
|
|
|
push ebx esi edi
|
|
|
|
|
|
|
|
mcall 68, 12, sizeof.aes256_cbc_context
|
|
|
|
; handle errors
|
2016-08-10 18:20:49 +02:00
|
|
|
mov ecx, AES256_BLOCKSIZE/4
|
2016-05-07 12:42:31 +02:00
|
|
|
mov esi, [_vector]
|
|
|
|
lea edi, [eax + aes256_cbc_context.vector]
|
2016-08-10 18:20:49 +02:00
|
|
|
rep movsd
|
2016-05-07 12:42:31 +02:00
|
|
|
; rep movsd is slow, but we don't care while init
|
|
|
|
|
|
|
|
pop edi esi ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
proc aes256_cbc_encrypt _ctx, _in, _out
|
|
|
|
push ebx esi edi
|
|
|
|
|
|
|
|
DEBUGF 1,'plain : '
|
2016-08-10 18:20:49 +02:00
|
|
|
stdcall dump_hex, [_in], 4
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
mov edi, [_ctx]
|
|
|
|
lea edi, [edi + aes256_cbc_context.vector]
|
|
|
|
mov esi, [_in]
|
2016-08-10 18:20:49 +02:00
|
|
|
repeat AES256_BLOCKSIZE/4
|
2016-05-07 12:42:31 +02:00
|
|
|
lodsd
|
|
|
|
xor eax, [edi]
|
|
|
|
stosd
|
2016-08-10 18:20:49 +02:00
|
|
|
end repeat
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
mov esi, [_ctx]
|
|
|
|
lea eax, [esi + aes256_cbc_context.key]
|
|
|
|
lea ebx, [esi + aes256_cbc_context.vector]
|
|
|
|
stdcall aes256_encrypt, eax, ebx, [_out] ; Key, in, out
|
|
|
|
|
|
|
|
mov esi, [_out]
|
|
|
|
mov eax, [_ctx]
|
|
|
|
lea edi, [eax + aes256_cbc_context.vector]
|
2016-08-10 18:20:49 +02:00
|
|
|
repeat AES256_BLOCKSIZE/4
|
2016-05-07 12:42:31 +02:00
|
|
|
movsd
|
2016-08-10 18:20:49 +02:00
|
|
|
end repeat
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
DEBUGF 1,'cipher : '
|
2016-08-10 18:20:49 +02:00
|
|
|
stdcall dump_hex, [_out], 4
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
pop edi esi ebx
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
proc aes256_cbc_decrypt _ctx, _in, _out
|
2016-08-10 18:20:49 +02:00
|
|
|
|
|
|
|
locals
|
|
|
|
temp_iv rb AES256_BLOCKSIZE
|
|
|
|
endl
|
|
|
|
|
2016-05-07 12:42:31 +02:00
|
|
|
push ebx esi edi
|
|
|
|
|
|
|
|
DEBUGF 1,'cipher : '
|
2016-08-10 18:20:49 +02:00
|
|
|
stdcall dump_hex, [_in], 4
|
|
|
|
|
|
|
|
mov esi, [_in]
|
|
|
|
lea edi, [temp_iv]
|
|
|
|
repeat AES256_BLOCKSIZE/4
|
|
|
|
movsd
|
|
|
|
end repeat
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
mov esi, [_ctx]
|
|
|
|
lea eax, [esi + aes256_cbc_context.key]
|
|
|
|
stdcall aes256_decrypt, eax, [_in], [_out] ; Key, in, out
|
|
|
|
|
|
|
|
mov esi, [_ctx]
|
|
|
|
lea esi, [esi + aes256_cbc_context.vector]
|
|
|
|
mov edi, [_out]
|
2016-08-10 18:20:49 +02:00
|
|
|
repeat AES256_BLOCKSIZE/4
|
2016-05-07 12:42:31 +02:00
|
|
|
lodsd
|
|
|
|
xor eax, [edi]
|
|
|
|
stosd
|
2016-08-10 18:20:49 +02:00
|
|
|
end repeat
|
2016-05-07 12:42:31 +02:00
|
|
|
|
2016-08-10 18:20:49 +02:00
|
|
|
lea esi, [temp_iv]
|
2016-05-07 12:42:31 +02:00
|
|
|
mov edi, [_ctx]
|
|
|
|
lea edi, [edi + aes256_cbc_context.vector]
|
2016-08-10 18:20:49 +02:00
|
|
|
repeat AES256_BLOCKSIZE/4
|
2016-05-07 12:42:31 +02:00
|
|
|
movsd
|
2016-08-10 18:20:49 +02:00
|
|
|
end repeat
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
DEBUGF 1,'plain : '
|
2016-08-10 18:20:49 +02:00
|
|
|
stdcall dump_hex, [_out], 4
|
2016-05-07 12:42:31 +02:00
|
|
|
|
|
|
|
pop edi esi ebx
|
|
|
|
ret
|
|
|
|
endp
|