Skip to content

Bug: Host key comparison uses non-constant-time == instead of constant_time_bytes_eq #2585

Open

Description

@themavik

Bug Description

Host key comparisons in paramiko/hostkeys.py and paramiko/transport.py use Python's == and != operators, which short-circuit on the first mismatched byte. This creates a timing side channel that could allow an attacker to infer the correct host key byte-by-byte.

Code Locations

paramiko/hostkeys.py line ~224:

return host_key.asbytes() == key.asbytes()

paramiko/transport.py line ~1423:

or key.asbytes() != hostkey.asbytes()

Context

Paramiko already has a constant-time comparison utility (paramiko.util.constant_time_bytes_eq) and uses it for hashed hostname matching in hostkeys.py line ~203 (_hostname_matches). However, the actual host key bytes comparison does not use it.

Suggested Fix

# hostkeys.py line ~224
return constant_time_bytes_eq(host_key.asbytes(), key.asbytes())

# transport.py line ~1423
or not constant_time_bytes_eq(key.asbytes(), hostkey.asbytes())

Impact

An attacker with network position could use timing measurements across many connections to determine bytes of the expected host key. Exploitation requires many measurements and network control, so severity is Medium.

Environment

  • Observed on latest main branch
  • constant_time_bytes_eq already exists in paramiko.util

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions