OpenSSH keys
Load and encode OpenSSH private keys, including encrypted ones, and public key lines.
Warning
Experimental: built on the pinned pre-release ssh-key 0.7.0-rc.12.
from oxcrypto.signature.ecdsa import ECDSA
from oxcrypto.ssh.openssh import OPENSSH
with open("id_ecdsa", "rb") as f:
private_key = OPENSSH.OPENSSH.load_private_key(f.read(), password=b"pw")
signature = ECDSA.P256.sign(private_key, b"message")
public_key = OPENSSH.OPENSSH.load_public_key(b"ecdsa-sha2-nistp256 AAAA... me")
print(OPENSSH.OPENSSH.fingerprint(public_key)) # SHA256:...load_private_key reads an -----BEGIN OPENSSH PRIVATE KEY----- file and
returns an oxcrypto private key. It checks that the private key matches the
public key stored in the file. An encrypted file needs password; passing a
password for an unencrypted file, or omitting it for an encrypted one,
raises ValueError. A wrong password or a corrupted ciphertext raises
oxcrypto.InvalidTag.
encode_private_key writes the same format with an optional comment. With
password, it encrypts the key with bcrypt-pbkdf (16 rounds) and AES-256-CTR,
as ssh-keygen does. The password must not be empty.
load_public_key reads one public key line, <algorithm> <base64> [comment],
and encode_public_key writes one without a trailing newline. fingerprint
returns the SHA256: fingerprint that ssh-keygen -l prints.
Encrypted keys
Loading accepts keys encrypted with any cipher ssh-key supports, including
aes256-ctr, aes256-gcm@openssh.com and chacha20-poly1305@openssh.com.
Before deriving a key, load_private_key raises ValueError if the bcrypt-pbkdf
round count is above 1024, so a crafted file cannot demand unbounded work.
OpenSSH writes 16 rounds unless ssh-keygen -a asks for more. bcrypt-pbkdf
uses a fixed amount of memory for any round count.