libcrash: sync with upstream.

* Implement new algorithms:
  - MACs: Poly1305, HMAC (SHA2_256, SHA2_512),
  - ciphers: ChaCha20, AES256CTR, AES256CBC.
* Remove MD4 hash.
* Change API (it happens).
* Update crashtest example.


git-svn-id: svn://kolibrios.org@9216 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
2021-10-15 00:52:46 +00:00
parent 34050385a4
commit 553742f877
31 changed files with 4605 additions and 1621 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,235 @@
; libcrash -- cryptographic hash (and other) functions
;
; Copyright (C) <2016> Jeffrey Amelynck
; Copyright (C) <2016,2021> Ivan Baravy
;
; SPDX-License-Identifier: GPL-2.0-or-later
;
; 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 2 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 ctx_aes_cbc
aes ctx_aes
cbc ctx_cbc
crypt dd ?
finish dd ?
block rd CBC128_BLOCK_SIZE/4
index dd ?
padding dd ?
ends
assert sizeof.ctx_aes_cbc <= LIBCRASH_CTX_LEN
; _crypt: 0/1 = encrypt/decrypt
proc aes256cbc.init uses ebx esi edi, _ctx, _key, _iv, _flags
mov ebx, [_ctx]
stdcall aes256.init, ebx, [_key], [_flags]
mov ecx, CBC128_BLOCK_SIZE/4
mov esi, [_iv]
lea edi, [ebx+ctx_aes_cbc.cbc.vector]
rep movsd
mov [ebx+ctx_aes_cbc.cbc.has_data], 0
mov [ebx+ctx_aes_cbc.index], 0
mov [ebx+ctx_aes_cbc.crypt], aes256cbc._.encrypt_block
mov [ebx+ctx_aes_cbc.finish], aes256cbc._.finish_encrypt
test [_flags], LIBCRASH_CIPHER_DECRYPT
jz @f
mov [ebx+ctx_aes_cbc.crypt], aes256cbc._.decrypt_block
mov [ebx+ctx_aes_cbc.finish], aes256cbc._.finish_decrypt
@@:
xor eax, eax
test [_flags], LIBCRASH_CIPHER_PADDING
setnz al
mov [ebx+ctx_aes_cbc.padding], eax
ret
endp
proc aes256cbc._.encrypt_block uses ebx esi edi, _ctx, _in, _out
mov ebx, [_ctx]
mov esi, [_in]
lea edi, [ebx+ctx_aes_cbc.cbc.vector]
mov ecx, CBC128_BLOCK_SIZE/4
@@:
lodsd
xor [edi], eax
add edi, 4
dec ecx
jnz @b
lea ecx, [ebx+ctx_aes_cbc.cbc.vector]
lea edx, [ebx+ctx_aes_cbc.aes.state]
stdcall aes.encrypt, ebx, ecx, edx
lea esi, [ebx+ctx_aes_cbc.aes.state]
lea edi, [ebx+ctx_aes_cbc.cbc.vector]
mov ecx, CBC128_BLOCK_SIZE/4
rep movsd
lea esi, [ebx+ctx_aes_cbc.aes.state]
mov edi, [_out]
mov ecx, CBC128_BLOCK_SIZE/4
rep movsd
mov eax, CBC128_BLOCK_SIZE
ret
endp
proc aes256cbc._.decrypt_block uses ebx esi edi, _ctx, _in, _out
locals
.done dd ?
endl
mov [.done], 0
mov ebx, [_ctx]
mov ecx, [_in]
lea edx, [ebx+ctx_aes_cbc.aes.state]
stdcall aes.decrypt, ebx, ecx, edx
bts [ebx+ctx_aes_cbc.cbc.has_data], 0
jnc @f
lea esi, [ebx+ctx_aes_cbc.cbc.block]
mov edi, [_out]
mov ecx, CBC128_BLOCK_SIZE/4
rep movsd
add [.done], CBC128_BLOCK_SIZE
@@:
lea esi, [ebx+ctx_aes_cbc.aes.state]
lea edx, [ebx+ctx_aes_cbc.cbc.vector]
lea edi, [ebx+ctx_aes_cbc.cbc.block]
mov ecx, CBC128_BLOCK_SIZE/4
@@:
lodsd
xor eax, [edx]
add edx, 4
stosd
dec ecx
jnz @b
mov esi, [_in]
lea edi, [ebx+ctx_aes_cbc.cbc.vector]
mov ecx, CBC128_BLOCK_SIZE/4
rep movsd
mov eax, [.done]
ret
endp
proc aes256cbc.update uses ebx esi edi, _ctx, _in, _len, _out
locals
.done dd ?
endl
mov [.done], 0
.next_block:
mov ebx, [_ctx]
mov eax, [ebx+ctx_aes_cbc.index]
test eax, eax
jnz .copy_to_buf
test [_in], LIBCRASH_ALIGN-1
jnz .copy_to_buf
.no_copy:
; data is aligned, process it in place without copying
mov ebx, [_ctx]
cmp [_len], CBC128_BLOCK_SIZE
jb .copy_quit
stdcall [ebx+ctx_aes_cbc.crypt], [_ctx], [_in], [_out]
add [_in], CBC128_BLOCK_SIZE
add [_out], eax
add [.done], eax
sub [_len], CBC128_BLOCK_SIZE
jmp .no_copy
.copy_to_buf:
lea edi, [ebx+ctx_aes_cbc.block]
add edi, [ebx+ctx_aes_cbc.index]
mov ecx, CBC128_BLOCK_SIZE
sub ecx, [ebx+ctx_aes_cbc.index]
cmp [_len], ecx
jb .copy_quit
mov esi, [_in]
sub [_len], ecx
add [_in], ecx
rep movsb
mov [ebx+ctx_aes_cbc.index], 0
lea esi, [ebx+ctx_aes_cbc.block]
stdcall [ebx+ctx_aes_cbc.crypt], [_ctx], esi, [_out]
add [.done], eax
add [_out], eax
jmp .next_block
.copy_quit:
mov ebx, [_ctx]
mov esi, [_in]
lea edi, [ebx+ctx_aes_cbc.block]
add edi, [ebx+ctx_aes_cbc.index]
mov ecx, [_len]
add [ebx+ctx_aes_cbc.index], ecx
rep movsb
.quit:
mov eax, [.done]
ret
endp
proc aes256cbc.finish uses ebx esi edi, _ctx, _out
mov ebx, [_ctx]
stdcall [ebx+ctx_aes_cbc.finish], ebx, [_out]
ret
endp
proc aes256cbc._.finish_encrypt uses ebx esi edi, _ctx, _out
mov ebx, [_ctx]
xor eax, eax
cmp [ebx+ctx_aes_cbc.padding], 0
jz .no_padding
; add padding
lea edi, [ebx+ctx_aes_cbc.block]
add edi, [ebx+ctx_aes_cbc.index]
mov ecx, CBC128_BLOCK_SIZE
sub ecx, [ebx+ctx_aes_cbc.index]
mov eax, ecx
rep stosb
lea eax, [ebx+ctx_aes_cbc.block]
stdcall aes256cbc._.encrypt_block, [_ctx], eax, [_out]
mov eax, CBC128_BLOCK_SIZE
.no_padding:
ret
endp
proc aes256cbc._.finish_decrypt uses ebx esi edi, _ctx, _out
mov ebx, [_ctx]
xor eax, eax
cmp eax, [ebx+ctx_aes_cbc.cbc.has_data]
jz .done
lea esi, [ebx+ctx_aes_cbc.cbc.block]
mov edi, [_out]
mov ecx, CBC128_BLOCK_SIZE
cmp [ebx+ctx_aes_cbc.padding], eax
jz @f
sub cl, [esi+CBC128_BLOCK_SIZE-1]
@@:
mov eax, ecx
rep movsb
.done:
ret
endp
proc aes256cbc.oneshot _ctx, _key, _iv, _flags, _in, _len, _out
locals
.done dd ?
endl
mov [.done], 0
stdcall aes256cbc.init, [_ctx], [_key], [_iv], [_flags]
stdcall aes256cbc.update, [_ctx], [_in], [_len], [_out]
add [_out], eax
add [.done], eax
stdcall aes256cbc.finish, [_ctx], [_out]
add eax, [.done]
ret
endp

View File

@@ -0,0 +1,137 @@
; libcrash -- cryptographic hash (and other) functions
;
; Copyright (C) <2016> Jeffrey Amelynck
; Copyright (C) <2016,2021> Ivan Baravy
;
; SPDX-License-Identifier: GPL-2.0-or-later
;
; 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 2 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 ctx_aes_ctr
aes ctx_aes
ctr ctx_ctr
ends
assert sizeof.ctx_aes_ctr <= LIBCRASH_CTX_LEN
; _crypt: 0/1 = encrypt/decrypt
proc aes256ctr.init uses ebx, _ctx, _key, _iv, _flags
mov ebx, [_ctx]
stdcall aes256.init, ebx, [_key], LIBCRASH_CIPHER_ENCRYPT
add ebx, ctx_aes_ctr.ctr
stdcall ctr.init, [_iv]
ret
endp
proc a22es_ctr._.block_init _ctx
mov edi, [_ctx]
lea esi, [edi+ctx_aes_ctr.ctr.block_counter]
mov ecx, AES_BLOCK_SIZE/4
rep movsd
ret
endp
proc aes256ctr.update uses ebx esi edi, _ctx, _in, _len, _out
mov eax, [_len]
pushad
mov ebx, [_ctx]
mov edi, [_in]
mov edx, [ebx+ctx_aes_ctr.ctr.partial_cnt]
.next_chunk:
mov ecx, [_len]
test ecx, ecx
jz .done
test edx, edx
jnz @f
pushad
lea ecx, [ebx+ctx_aes_ctr.ctr.block_counter]
lea edx, [ebx+ctx_aes_ctr.aes.state]
stdcall aes.encrypt, ebx, ecx, edx
popad
mov edx, AES_BLOCK_SIZE
pushad
mov esi, ebx
mov eax, dword[esi+ctx_aes_ctr.ctr.block_counter+4*0]
mov ebx, dword[esi+ctx_aes_ctr.ctr.block_counter+4*1]
mov ecx, dword[esi+ctx_aes_ctr.ctr.block_counter+4*2]
mov edx, dword[esi+ctx_aes_ctr.ctr.block_counter+4*3]
bswap eax
bswap ebx
bswap ecx
bswap edx
add edx, 1
adc ecx, 0
adc ebx, 0
adc eax, 0
bswap eax
bswap ebx
bswap ecx
bswap edx
mov dword[esi+ctx_aes_ctr.ctr.block_counter+4*0], eax
mov dword[esi+ctx_aes_ctr.ctr.block_counter+4*1], ebx
mov dword[esi+ctx_aes_ctr.ctr.block_counter+4*2], ecx
mov dword[esi+ctx_aes_ctr.ctr.block_counter+4*3], edx
popad
@@:
cmp ecx, edx
jbe @f
mov ecx, edx
@@:
lea esi, [ebx+ctx_aes_ctr.aes.state]
add esi, AES_BLOCK_SIZE
sub esi, edx
sub [_len], ecx
sub edx, ecx
push ebx
mov edi, [_out]
mov ebx, [_in]
add [_in], ecx
add [_out], ecx
@@:
lodsb
xor al, [ebx]
inc ebx
stosb
loop @b
pop ebx
jmp .next_chunk
.done:
mov [ebx+ctx_aes_ctr.ctr.partial_cnt], edx
popad
ret
endp
proc aes256ctr.finish _ctx, _out
xor eax, eax
ret
endp
proc aes256ctr.oneshot _ctx, _key, _iv, _flags, _in, _len, _out
locals
.done dd ?
endl
mov [.done], 0
stdcall aes256ctr.init, [_ctx], [_key], [_iv], [_flags]
stdcall aes256ctr.update, [_ctx], [_in], [_len], [_out]
add [_out], eax
add [.done], eax
stdcall aes256ctr.finish, [_ctx], [_out]
add eax, [.done]
ret
endp

View File

@@ -0,0 +1,204 @@
; libcrash -- cryptographic hash (and other) functions
;
; Copyright (C) <2021> Ivan Baravy
;
; SPDX-License-Identifier: GPL-2.0-or-later
;
; 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 2 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/>.
; https://datatracker.ietf.org/doc/html/rfc7539
CHACHA20_BLOCK_SIZE = 64
CHACHA20_KEY_SIZE = 32
CHACHA20_NONCE_SIZE = 12
CHACHA20_IV_SIZE = 16
struct ctx_chacha20
state rd CHACHA20_BLOCK_SIZE/4
key rd CHACHA20_KEY_SIZE/4
block_counter dd ?
nonce rd CHACHA20_NONCE_SIZE/4
partial_cnt dd ?
ends
assert sizeof.ctx_chacha20 <= LIBCRASH_CTX_LEN
proc chacha20.init uses ebx esi edi, _ctx, _key, _iv, _flags
mov ebx, [_ctx]
mov esi, [_key]
lea edi, [ebx+ctx_chacha20.key]
mov ecx, CHACHA20_KEY_SIZE/4
rep movsd
mov esi, [_iv]
lea edi, [ebx+ctx_chacha20.block_counter]
mov ecx, CHACHA20_IV_SIZE/4
rep movsd
mov [ebx+ctx_chacha20.partial_cnt], 0
ret
endp
macro chacha20._.quarter_round a, b, c, d {
; a = PLUS(a,b); d = ROTATE(XOR(d,a),16);
mov eax, [esi+a*4]
add eax, [esi+b*4]
mov [esi+a*4], eax
xor eax, [esi+d*4]
rol eax, 16
mov [esi+d*4], eax
; c = PLUS(c,d); b = ROTATE(XOR(b,c),12);
mov eax, [esi+c*4]
add eax, [esi+d*4]
mov [esi+c*4], eax
xor eax, [esi+b*4]
rol eax, 12
mov [esi+b*4], eax
; a = PLUS(a,b); d = ROTATE(XOR(d,a), 8);
mov eax, [esi+a*4]
add eax, [esi+b*4]
mov [esi+a*4], eax
xor eax, [esi+d*4]
rol eax, 8
mov [esi+d*4], eax
; c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
mov eax, [esi+c*4]
add eax, [esi+d*4]
mov [esi+c*4], eax
xor eax, [esi+b*4]
rol eax, 7
mov [esi+b*4], eax
}
proc chacha20._.inner_block _state
mov esi, [_state]
chacha20._.quarter_round 0, 4, 8, 12
chacha20._.quarter_round 1, 5, 9, 13
chacha20._.quarter_round 2, 6, 10, 14
chacha20._.quarter_round 3, 7, 11, 15
chacha20._.quarter_round 0, 5, 10, 15
chacha20._.quarter_round 1, 6, 11, 12
chacha20._.quarter_round 2, 7, 8, 13
chacha20._.quarter_round 3, 4, 9, 14
ret
endp
proc chacha20._.block_init _ctx
mov edi, [_ctx]
lea esi, [edi+ctx_chacha20.key]
mov [edi+ctx_chacha20.state+0*4], 'expa' ; magic
mov [edi+ctx_chacha20.state+1*4], 'nd 3' ; constants
mov [edi+ctx_chacha20.state+2*4], '2-by' ; from
mov [edi+ctx_chacha20.state+3*4], 'te k' ; the RFC
add edi, 4*4
mov ecx, CHACHA20_BLOCK_SIZE/4-4 ; the four dwords above
rep movsd
ret
endp
proc chacha20._.block _state
locals
.working_state rd CHACHA20_BLOCK_SIZE/4
.i dd ?
endl
stdcall chacha20._.block_init, [_state]
mov esi, [_state]
lea edi, [.working_state]
mov ecx, CHACHA20_BLOCK_SIZE/4
rep movsd
mov [.i], 10
@@:
lea eax, [.working_state]
stdcall chacha20._.inner_block, eax
dec [.i]
jnz @b
lea esi, [.working_state]
mov edi, [_state]
mov ecx, CHACHA20_BLOCK_SIZE/4-1
@@:
mov eax, [esi+ecx*4]
add [edi+ecx*4], eax
dec ecx
jns @b
ret
endp
proc chacha20.update uses ebx esi edi, _ctx, _in, _len, _out
locals
.bytes_done dd ?
endl
mov eax, [_len]
mov [.bytes_done], eax
mov ebx, [_ctx]
mov edx, [ebx+ctx_chacha20.partial_cnt]
.next_chunk:
mov ecx, [_len]
test ecx, ecx
jz .done
test edx, edx
jnz @f
pushad
stdcall chacha20._.block, [_ctx]
popad
mov edx, CHACHA20_BLOCK_SIZE
inc [ebx+ctx_chacha20.block_counter]
@@:
cmp ecx, edx
jbe @f
mov ecx, edx
@@:
lea esi, [ebx+ctx_chacha20.state]
add esi, CHACHA20_BLOCK_SIZE
sub esi, edx
sub [_len], ecx
sub edx, ecx
push ebx
mov edi, [_out]
mov ebx, [_in]
add [_in], ecx
add [_out], ecx
@@:
lodsb
xor al, [ebx]
inc ebx
stosb
loop @b
pop ebx
jmp .next_chunk
.done:
mov [ebx+ctx_chacha20.partial_cnt], edx
mov eax, [.bytes_done]
ret
endp
proc chacha20.finish _ctx, _out
xor eax, eax
ret
endp
proc chacha20.oneshot _ctx, _key, _iv, _flags, _in, _len, _out
locals
.done dd ?
endl
mov [.done], 0
stdcall chacha20.init, [_ctx], [_key], [_iv], [_flags]
stdcall chacha20.update, [_ctx], [_in], [_len], [_out]
add [_out], eax
add [.done], eax
stdcall chacha20.finish, [_ctx], [_out]
add eax, [.done]
ret
endp

View File

@@ -0,0 +1,36 @@
; libcrash -- cryptographic hash (and other) functions
;
; Copyright (C) <2021> Ivan Baravy
;
; SPDX-License-Identifier: GPL-2.0-or-later
;
; 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 2 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/>.
CBC128_BLOCK_SIZE = 128/8
struct ctx_cbc
vector rd CBC128_BLOCK_SIZE/4
block rd CBC128_BLOCK_SIZE/4
has_data dd ?
ends
; ebx = context
proc cbc.init uses esi edi, _iv
mov esi, [_iv]
lea edi, [ebx+ctx_ctr.block_counter]
mov ecx, CTR128_BLOCK_SIZE/4
rep movsd
mov [ebx+ctx_ctr.partial_cnt], 0
ret
endp

View File

@@ -0,0 +1,34 @@
; libcrash -- cryptographic hash (and other) functions
;
; Copyright (C) <2021> Ivan Baravy
;
; SPDX-License-Identifier: GPL-2.0-or-later
;
; 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 2 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/>.
CTR128_BLOCK_SIZE = 128/8
struct ctx_ctr
block_counter rd 4
partial_cnt dd ?
ends
; ebx = context
proc ctr.init uses esi edi, _iv
mov esi, [_iv]
lea edi, [ebx+ctx_ctr.block_counter]
mov ecx, CTR128_BLOCK_SIZE/4
rep movsd
mov [ebx+ctx_ctr.partial_cnt], 0
ret
endp