KatInterview

Password-Protected Encrypted Key-Value Store

System DesignModel answer
MediumModel answerSystem Designcryptographyauthenticationkey-management

Last updated

Design a user-authenticated key-value store where values are encrypted at rest using the user's password, covering key derivation, storage layout, and cryptographic trade-offs.

The Problem

You are building a small personal secrets store — think a stripped-down password manager or config vault. Each user registers with a username and password, logs in, and can then store and retrieve arbitrary string values under named keys.

The critical requirement: if someone steals a full copy of the database or storage files, they must not be able to read any stored values without knowing the user's password.

You may treat this as a language-agnostic library or service. You can use any standard cryptographic primitives (AES-GCM, Argon2, HKDF, etc.) from an existing library — you do not need to implement ciphers from scratch.

API surface

Register(username, password)
Login(username, password) → session_token
Put(session_token, key, value)
Get(session_token, key) → value
Delete(session_token, key)          // optional
ListKeys(session_token) → [key]     // optional, discuss trade-offs

Requirements

  1. No plaintext passwords in storage. The system must tolerate a full database leak without exposing credentials.
  2. Values are unreadable without the user's password. Someone with the raw storage rows cannot recover any value.
  3. Tampering is detectable. If a stored ciphertext is modified, the system should refuse to return a result rather than silently returning corrupted data.
  4. Password changes should be practical. Changing a password must not require re-encrypting every stored value.
  5. Users are isolated. One user's ciphertexts cannot be transplanted into another user's account.

Threat model

Assume an attacker can:

  • Obtain a full snapshot of persistent storage (all rows, all files, all backups).

Assume an attacker cannot:

  • Read process memory while the service is running.
  • Observe network traffic (TLS is assumed).

What to cover in your answer

  • Data model: what you store per user and per item, including every piece of metadata needed to decrypt later.
  • Cryptographic choices: which algorithms, why, and what pitfalls they avoid.
  • Key hierarchy: how you get from a raw password to the keys that actually encrypt data.
  • Core flows: Register, Login, Put, Get step by step.
  • Edge cases: wrong password, password change, storage corruption, multi-user isolation, key-name confidentiality.

Where helpful, include a code sketch (any language) to make the flows concrete.

Hints

Hint 1

Start by separating two distinct jobs: proving the user knows their password (authentication) and deriving the key that encrypts their data. These should produce independent keys from the same password-derivation step — not share a single output.

Hint 2

A password change shouldn't mean re-encrypting every stored value. Think about what would make that practical: if the data is encrypted under a random key, and that random key is itself encrypted under the password, only one small blob needs to change.

Hint 3

Every encryption operation needs a fresh random nonce/IV. Think about what goes wrong if two writes share a nonce under AES-GCM, and why a random-per-write strategy avoids it. Also consider what authenticated encryption gives you that plain AES-CBC does not.