SSH AES-128-CTR encryption fixes (Phase 4 refinement)
Some checks failed
Test / test (push) Has been cancelled
Test / build (push) Has been cancelled

Major fixes:
- Persistent cipher state: ciphers maintain counter across packets
- Cipher direction bug: use cipher_ctos for client packets, cipher_stoc for server packets
- MAC key length: 32 bytes for HMAC-SHA256 (was incorrectly 16 bytes)
- MtE mode MAC: calculate MAC over plaintext before encryption
- AES-CTR encryption: encrypt entire packet including packet_length field
- Service name length: corrected to 12 for 'ssh-userauth'
- mpint encoding: properly remove leading zeros and handle high bit

Remaining issue:
- SSH client reports 'Corrupted MAC on input'
- Likely due to key derivation mismatch with OpenSSH client
- Requires further investigation with packet capture analysis

Progress: 80% of SSH encryption implementation complete
Security: Still using RustCrypto authoritative libraries ()
This commit is contained in:
Warren
2026-06-14 15:06:01 +08:00
parent 2cbf0d7b98
commit 7d50c1147d
4 changed files with 179 additions and 93 deletions

View File

@@ -190,7 +190,7 @@ fn perform_ssh_auth(
encryption_ctx.iv_stoc.len()
);
let encrypted_request = EncryptedPacket::read(stream, encryption_ctx, false)?;
let encrypted_request = EncryptedPacket::read(stream, encryption_ctx, true)?; // Reading from client, use cipher_ctos
info!("Received encrypted SSH_MSG_SERVICE_REQUEST");
let payload = encrypted_request.payload();
@@ -211,7 +211,7 @@ fn perform_ssh_auth(
let mut service_accept_payload = Vec::new();
service_accept_payload.write_u8(PacketType::SSH_MSG_SERVICE_ACCEPT as u8)?;
service_accept_payload.write_u32::<BigEndian>(14)?;
service_accept_payload.write_u32::<BigEndian>(12)?; // "ssh-userauth" length is 12, not 14!
service_accept_payload.write_all("ssh-userauth".as_bytes())?;
let encrypted_accept = EncryptedPacket::new(
@@ -223,7 +223,7 @@ fn perform_ssh_auth(
info!("Sent encrypted SSH_MSG_SERVICE_ACCEPT");
loop {
let auth_packet = EncryptedPacket::read(stream, encryption_ctx, false)?;
let auth_packet = EncryptedPacket::read(stream, encryption_ctx, true)?; // Reading from client, use cipher_ctos
let auth_payload = auth_packet.payload();
info!("Received encrypted SSH_MSG_USERAUTH_REQUEST");