aboutsummaryrefslogtreecommitdiff
path: root/src/keyring.cpp
AgeCommit message (Collapse)Author
18 hoursIntroduce hardware auth keys backed by TPMOsmium Sorcerer
Hardware keys are created and managed exclusively in the protected environment of the Trusted Platform Module (TPM 2.0), a separate, secure processor isolated from the rest of the system. Keyring provides them as an alternative to established software keys for challenge-response authentication. Software keys, while far more secure than naive passwords, have their limitations. They're stored in a keyring file in an encrypted form and can be extracted and copied. As a result, it's possible to perform unlimited attempts to decrypt them offline. To mitigate such attacks, a memory-hard key derivation function must be used to derive the decryption key, and the passhprase itself must still be sufficiently strong because a small search space will definitely be exhausted. A side effect of this is a massive latency spike and potentially disruptive peak memory usage. Hardware keys, by design, are non-exportable. Total system compromise won't lead to key exfiltration due to TPM having no such functionality, and TPM's tamper resistance makes extraction of secrets infeasible even with physical access to the machine (if the TPM is genuine). PIN that is used to protect hardware keys is validated by the TPM itself, which also locks itself out after too many failed attempts. This lockout cannot be overridden without either issuing a lockout clear command with authorization value or resetting the TPM outright, erasing all keys stored in it. Because of this, low-entropy secrets (such as six-digit PIN) can provide sufficient security. Their only limitation is the flipside of their strength: because they're non-exportable, you can't back them up and move them between devices. Once created, a hardware key is bound to the machine, unlike software keys which are usable everywhere as long as you have keyring.cbor. Hardware keys require TPM 2.0 and an API to communicate with it. Implementations are provided for: - Windows via Cryptography API: Next Generation (CNG) with Microsoft Platform Crypto Provider. - Unix systems with TPM2 Software Stack (TSS2). Windows scopes keys to a Windows user, so you likely won't be able to move them across users or installations within the same machine. The keys will remain on the system with "SoF_Auth_" prefix and a UUID so you can locate them in your key registry. They'll additionally have the name you set at key creation. Unix might require additoinal user permissions to access the TPM. For example, adding a user to the `tss` group on Linux. The platform input differs. Windows can take a user-friendly key name to display, which is a good feature considering weird requirement of Windows that key names (actual identifiers) must be unique strings, for which I use UUIDs. Unix has no concept of key names or identifiers, but it has to provide PIN to the TPM directly. Windows uses its own PIN prompt from Windows Security UI that's disconnected from the application. This is somewhat awkward because it's modeless. Also, a ridiciulous quirk of Windows CNG API makes it so key handle creation returns the same `NTE_INVALID_HANDLE` error no matter what kind of error it was. In particular, it's impossible to differentiate between the operation failing due to the TPM lockout, or the user voluntarily closing the dialog. The user will always see "hardware locked out" error message. Brilliant API design. The PIN is implemented as a direct authorization value for keys, so it might be vulnerable to the bus sniffing attack if the PIN is traveling in clear between CPU and TPM. Though, a hypothetical adversary who's sitting with a logic analyzer hooked up to your motherboard as you type the PIN will realistically have easier means to log your keystrokes. TPM is capable of remote attestation to prove its authenticity, but I chose to avoid it to protect users' privacy (there's anonymous attestation, but it's not always practical because it requires special CAs) and avoid significant implementation complexity on the server that the attestation entails, such as parsing and validating certificate chains. Because of the unfortunate reality, TPMs overwhelmingly have no support for X25519 (the key exchange algorithm used in software keys). It was added in a recent revision, but it's yet to be implemented, and only on the newest machines. You can't upgrade the TPM hardware, so we have to compromise. Instead, elliptic curve Diffie-Hellman over P-256 curve has been selected, which is also the default curve used in WebAuthn (passkey) protocol. It's ubiquitous, supported by every single TPM 2.0, and secure if reasonably implemented. Keys don't take up limited nonvolatile memory of the TPM. Every reference to TPM objects necessary to perform authentication is stored on disk, and keys and contexts are recreated on every operation and cleared from memory afterwards. For the client public key format, I *only* use compressed P-256 points (1-byte parity of y coordinate followed by a full 32-byte x coordinate) for robustness. Their designated identification byte is 0x33, and they start with the letter M when base64url-encoded.
23 hourskeyring: Encode public keys as base64urlOsmium Sorcerer
23 hourskeyring: Improve key passphrase parametersOsmium Sorcerer
Change Argon2id parameters from 1 gigabyte of memory and 3 iterations to 2 gigabytes and 2 iterations. Memory cost is the primary parameter that provides substantial hardening, and 2 GiB in particular is recommended by RFC 9106 (here with an additional iteration). This will impact performance of key derivation on devices with constrained RAM, but platform-backed keys should provide an alternative. Enforce minimum of 6 characters for the passphrase. This is still not enough considering an adversary can make unlimited offline guesses, even with significant slowdown. However, this is consistent with modern practices for authenticator policies (recommended, for example, by NIST SP 800-63B-4): enforce minimal length and nothing else.
2026-03-29Add the keyring for secret key managementOsmium Sorcerer
The keyring provides the system to store secret keys in an encrypted format, create and delete keys, display public keys and notes for the user, and use these keys to peform public-key authentication on servers. Keyring is serialized into `keyring.cbor` in the application directory. It's a CBOR map with keys being key IDs (fingerprints), and the values are key entries, the schema of which looks like this: key-entry { 0 => uint, ; Algorithm tag, 1 byte 1 => text, ; Comment/note for the key 2 => bytes, ; Public key (certificate), 32 bytes 3 => bytes ; Encrypted and authenticated secret key (AEAD payload) } Key fingerprint is `BLAKE2b-256(tag || public_key)`, where `||` denotes concatenation of byte strings. Encrypted payload is a fixed binary structure (field sizes in bytes): Version(1) Salt(16) Opslimit(4) Memlimit(4) Alg(1) Ciphertext(32) MAC(16) Upon key generation, a new secret key is created, sourced from a secure RNG. The wrap key is derived from the passphrase using Argon2 with the specified iterations, memory cost, variant (3 iterations, 1 GB of memory, Argon2id), and 16 bytes of randomly generated unique salt. This wrap key is used with ChaCha20-Poly1305 to encrypt the secret key, with all the prior fields as additional authenticatied data and all-zero nonce (the uniqueness is already provided by the salt). The key pairs are X25519, used specifically for key exchange. When the server sends the ephemeral public key as a challenge, the client uses `unlock_and_auth` function with the key corresponding to the right certificate. After entering the correct passphrase, the secret key is decrypted and used to derive a shared secret with the server's ephemeral key. The client then responds with: BLAKE2b-256("Einsof-Auth-DHCR" || shared_secret || challenge || certificate || username) Where the first string is provided for domain separation, shared secret proves possession of the secret key, and other parameters are hashed in to bind this authentication attempt to the current session (via random challenge), identity (via public key and username), and transcript. Note on canonicalization: all fields but last are fixed-length, concatenation here is unambiguous. The server, in turn, performs the same opeations, except the shared secret is derived from the server's ephemeral secret and the client's public key. Naturally, username and public key must be correct. If the response matches, the server authenticates the client. The client never transmits its secret. This scheme is essentially deriving a session secret and computing MAC over the transcript with that secret to prove authenticity. It serves as a simple identification protocol. Unlike digital signatures, it's interactive, valid only in the context of a single authentication attempt, and only between two participants involved. Signatures, in contrast, are valid everywhere, for everyone, and they require additional nonces and context. In fact, they're interactive identification protocols turned non-interactive, so forcing them back into this setting is unnecessary complexity. The primitives are fixed: X25519 for key exchange, Argon2 for password-based key derivation, ChaCha20-Poly1305 for encryption, BLAKE2b for hashing. Provided by libsodium. Simplicity is key. There's no flexibility, negotiation, or compatibility, and it'll hopefully stay this way. Unless you're worried about quantum computers appearing tomorrow and attacking a niche AO implementation, in which case I'll add the ML-KEM variant just for you.