Securing SSH Private Keys with TPM to Prevent Leaks

Safe SSH Authentication with nCryptAgent
Safe SSH Authentication with nCryptAgent

SSH uses the ssh-keygen command to store private keys in ~/.ssh and register public keys in ~/.ssh/authorized_keys on remote systems. This mechanism assumes that the private key remains secure, with protection limited to restricting read access to the owner.

For example, with the increasing use of AI agents on local systems, these agents operate under user permissions and could potentially access private keys. While some users may run AI agents in sandbox environments like Docker Sandboxes, security risks remain in today’s environment.

nCryptAgent

I discovered nCryptAgent (a Windows application, targeting WSL2 in this article) which acts as an intermediary between SSH and TPM, enabling the use of TPM-stored private keys for SSH authentication.
https://github.com/unreality/nCryptAgent

Private keys are stored in the PC’s TPM rather than as files in ~/.ssh, so they don’t appear on the file system.

(A quick code inspection with DeepSeekHarness found no suspicious elements.)

Installation

  1. Download nCryptAgent.zip from the latest GitHub release.
  2. Extract and run it.
  3. The GUI appears in the system tray.

Screenshot 2026-11-05 232017
Screenshot 2026-11-05 232017

Key Generation

Click the “Create Key” button in the lower-left corner to generate a private-public key pair.

Screenshot 2026-11-05 232402
Screenshot 2026-11-05 232402

Name: Name displayed in nCryptAgent
Container Name: Internal identifier used in Windows Cryptographic Key Management.
Password/PIN: PIN code. Omitting this allows SSH connections without PIN entry. Security recommends setting it.

Select a key algorithm and click “Save” to generate the key.

Installing socat

Install it as a bridge between WSL2 and Windows.

sudo apt update
sudo apt install socat

Usage

Select a key and click “Copy Key” to copy the public key, then register it in the remote ~/.ssh/authorized_keys file. It can also be used as a GitHub key.

Screenshot 2026-11-05 232846
Screenshot 2026-11-05 232846

Click the Config tab, copy the Shell Script in the WSL2(Hyper-V Socket) section, and append it to ~/.bashrc, then run:

source ~/.bashrc

After that, you should be able to log in to the remote system as usual via SSH.

© 2025 Hiroe Tech Notes. All rights reserved.

Comments

No comments yet.