Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 9071 → Rev 9070

/programs/network/ssh/seed.inc
File deleted
\ No newline at end of file
/programs/network/ssh/ssh.asm
51,7 → 51,6
include 'dh_gex.inc'
 
include 'mpint.inc'
include 'seed.inc'
include 'random.inc'
 
include 'aes256.inc'
119,12 → 118,9
rx_crypt_blocksize dd ?
tx_crypt_blocksize dd ?
 
; Padding
rx_padsize dd ? ; = Max(8, rx_crypt_blocksize)
tx_padsize dd ? ; = Max(8, tx_crypt_blocksize)
 
; rx_padsize dd ? ; = Max(8, rx_crypt_blocksize)
tx_pad_size dd ? ; = Max(8, tx_crypt_blocksize)
tx_pad_proc dd ?
 
; Message authentication
 
rx_mac_proc dd ?
193,7 → 189,6
jnz exit
 
DEBUGF 2, "SSH: Init PRNG\n"
call create_seed
call init_random
 
DEBUGF 2, "SSH: Init Console\n"
340,9 → 335,8
mov [con.tx_mac_proc], 0
mov [con.rx_mac_length], 0
mov [con.tx_mac_length], 0
; mov [con.rx_padsize], 8 ; minimum padsize
mov [con.tx_pad_size], 8
mov [con.tx_pad_proc], padding_zero
mov [con.rx_padsize], 8 ; minimum padsize
mov [con.tx_padsize], 8
 
DEBUGF 2, "Sending KEX init\n"
mov edi, ssh_kex.cookie
442,7 → 436,7
test eax, eax
jnz exit
 
; Set keys and initialize transport subroutines
; Set keys
 
DEBUGF 2, "SSH: Setting encryption keys\n"
 
452,7 → 446,7
stdcall aes256_set_encrypt_key, eax, con.rx_enc_key
mov [con.rx_crypt_proc], aes256_ctr_crypt
mov [con.rx_crypt_blocksize], AES256_BLOCKSIZE
; mov [con.rx_pad_size], AES256_BLOCKSIZE
mov [con.rx_padsize], AES256_BLOCKSIZE
 
stdcall aes256_ctr_init, con.tx_iv
mov [con.tx_crypt_ctx_ptr], eax
460,10 → 454,8
stdcall aes256_set_encrypt_key, eax, con.tx_enc_key
mov [con.tx_crypt_proc], aes256_ctr_crypt
mov [con.tx_crypt_blocksize], AES256_BLOCKSIZE
mov [con.tx_padsize], AES256_BLOCKSIZE
 
mov [con.tx_pad_size], AES256_BLOCKSIZE
mov [con.tx_pad_proc], MBRandom
 
stdcall hmac_sha256_setkey, con.rx_mac_ctx, con.rx_int_key, SHA256_HASH_SIZE
mov [con.rx_mac_proc], hmac_sha256
mov [con.rx_mac_length], SHA256_HASH_SIZE
472,10 → 464,6
mov [con.tx_mac_proc], hmac_sha256
mov [con.tx_mac_length], SHA256_HASH_SIZE
 
; Re-seed RNG for padding bytes
call create_seed
call init_random
 
; TODO: erase all keys from memory and free the memory
 
; >> Request service (user-auth)
790,7 → 778,7
ssh_ident_ha:
dd_n (ssh_ident.length-2)
ssh_ident:
db "SSH-2.0-KolibriOS_SSH_0.04",13,10
db "SSH-2.0-KolibriOS_SSH_0.03",13,10
.length = $ - ssh_ident
 
ssh_kex:
/programs/network/ssh/ssh_transport.inc
24,13 → 24,7
message_code db ? ; First byte of payload
ends
 
proc padding_zero
 
xor eax, eax
ret
 
endp
 
proc ssh_recv_packet connection, flags
 
locals
133,10 → 127,14
repe cmpsd
jne .mac_failed
.mac_complete:
add byte[ebx+ssh_connection.rx_seq+3], 1 ; Update sequence counter
adc byte[ebx+ssh_connection.rx_seq+2], 0
adc byte[ebx+ssh_connection.rx_seq+1], 0
adc byte[ebx+ssh_connection.rx_seq+0], 0
inc byte[ebx+ssh_connection.rx_seq+3] ; Update sequence counter
jnc @f
inc byte[ebx+ssh_connection.rx_seq+2]
jnc @f
inc byte[ebx+ssh_connection.rx_seq+1]
jnc @f
inc byte[ebx+ssh_connection.rx_seq+0]
@@:
 
; Return useful data length to the caller via eax register
.got_all_data:
168,63 → 166,56
endl
DEBUGF 2, "< "
 
; Check how many bytes we should pad
; Pad the packet with random data
mov eax, [payload_size]
inc eax ; padding length byte
lea edx, [eax+4] ; total packet size (without padding and MAC)
mov [packet_size], edx
 
mov ecx, [connection]
mov ebx, [ecx+ssh_connection.tx_pad_size]
mov ebx, [ecx+ssh_connection.tx_padsize]
dec ebx
and edx, ebx
neg edx
add edx, [ecx+ssh_connection.tx_pad_size]
add edx, [ecx+ssh_connection.tx_pad_size]
add edx, [ecx+ssh_connection.tx_padsize]
cmp edx, 4 ; minimum padding size
jae @f
add edx, [ecx+ssh_connection.tx_padsize]
@@:
DEBUGF 1, "padding %u bytes ", edx
add [packet_size], edx ; total packet size with padding
add [packet_size], edx
 
; Start building the packet
; First comes the packet length, in network byte order ofcourse.
add eax, edx
DEBUGF 1, "total size: %u ", eax
bswap eax
lea edi, [ecx+ssh_connection.tx_buffer]
stosd
; Then the padding length
stosd ; packet_length
mov al, dl
stosb
; And the actual payload bytes
stosb ; padding_length
mov esi, [buf]
mov ecx, [payload_size]
rep movsb
 
; Append the packet with #edx padding bytes.
; Since we must pad at least 8 bytes, we can always use DWORD writes.
; First do an (unaligned) write exactly following the data
dec edx
mov ebx, edx
mov esi, edx
shr esi, 2 ; number dwords
mov ebx, edx
and ebx, 3
inc ebx ; number bytes in first write (1-4)
mov edx, [connection]
call [edx+ssh_connection.tx_pad_proc]
jz @f
call MBRandom
mov dword[edi], eax
add edi, ebx
; Then, do as many aligned writes as nescessary
mov ebx, [connection]
@@:
call [ebx+ssh_connection.tx_pad_proc]
 
shr esi, 2
@@:
call MBRandom
stosd
dec esi
jnz @r
 
; Append the packet with Message Authentication Code
; Message authentication
mov edx, [connection]
cmp [edx+ssh_connection.tx_mac_proc], 0
je .mac_complete
DEBUGF 1, "MAC sequence number: 0x%x\n", [edx+ssh_connection.tx_seq]
; DEBUGF 1, "MAC sequence number: 0x%x\n", [edx+ssh_connection.tx_seq]
lea esi, [edx+ssh_connection.tx_seq]
mov ecx, [packet_size]
add ecx, 4 ; Sequence number length
238,12 → 229,16
shr ecx, 2
rep movsd
.mac_complete:
add byte[edx+ssh_connection.tx_seq+3], 1 ; Update sequence counter
adc byte[edx+ssh_connection.tx_seq+2], 0
adc byte[edx+ssh_connection.tx_seq+1], 0
adc byte[edx+ssh_connection.tx_seq+0], 0
inc byte[edx+ssh_connection.tx_seq+3] ; Update sequence counter
jnc @f
inc byte[edx+ssh_connection.tx_seq+2]
jnc @f
inc byte[edx+ssh_connection.tx_seq+1]
jnc @f
inc byte[edx+ssh_connection.tx_seq+0]
@@:
 
; Now, encrypt everything but MAC
; Encrypt data
cmp [edx+ssh_connection.tx_crypt_proc], 0
je .encrypt_complete
lea esi, [edx+ssh_connection.tx_buffer]
/programs/network/ssh/random.inc
36,6 → 36,7
 
proc init_random
 
mcall 26, 10 ; seed
xor ecx, ecx
; make random numbers and put them into buffer
@@: