V. Suryawanshi, home

Writing · · 6 min read

Why I didn’t write
my own cryptography

VPass is a password manager I built alone in five days and shipped without a security audit. That only works because of one rule: every part that keeps a secret safe is borrowed from people who have been audited. Here’s what I borrowed, the two places I wrote crypto-shaped code anyway, and how I checked them.

The ruleCopy link to this section

“Don’t roll your own crypto” is old advice, and it usually gets read as “don’t invent a cipher”. Nobody building an app is inventing ciphers. The mistakes people actually make are smaller: a homemade storage format, a key kept next to the data it protects, a random number from the wrong function. Each one looks like ordinary code.

A password manager is the worst place to make those mistakes, and a solo project is the worst place to find them, because nobody else reads the code. Paying for a proper audit of a personal app wasn’t realistic. So before writing any code, I put the rule into the brief as a constraint: no homemade cryptography. Every part that protects a secret has to be something that has already been through someone else’s audit.

What I borrowedCopy link to this section

  • Storing the vault: Apple’s Keychain. Each credential is one Keychain item, encrypted by the system with keys VPass never sees, and readable only while the device is unlocked.
  • Syncing iPhone and Mac: iCloud Keychain. It’s end-to-end encrypted, and VPass has no server, so there’s no copy of anything on a machine I’d have to protect.
  • Proving it’s you: LocalAuthentication. Touch ID, Face ID or the device password. There’s no master password guarding the vault; the only password VPass keeps is the backup one, in the Keychain.
  • The building blocks: CryptoKit. HMAC, SHA-256 and AES-GCM from Apple’s library, and random bytes from the system’s secure generator.
  • Trusting updates: Apple and Sparkle. The Mac download is signed with a Developer ID and notarized by Apple, and Sparkle only installs updates signed with my key.

The surprising part was how little code that left me. The vault is a few hundred lines of Keychain queries, and most of their complexity is about syncing deletions correctly between devices, not about security.

Where I wrote crypto-shaped code anywayCopy link to this section

Two places, both standard algorithms and neither invented. But a standard algorithm written by hand can still be written wrong, so each one needed a check that would catch me.

The 2FA codes

The six-digit codes follow RFC 6238: an HMAC of the current 30-second window, then a step called dynamic truncation that picks 31 bits out of the result. CryptoKit does the HMAC; the truncation is mine, about a dozen lines. (HMAC-SHA1 sounds alarming, but it’s still sound, and it’s what every authenticator app expects.)

let counter = UInt64(date.timeIntervalSince1970) / UInt64(max(period, 1))
var bigEndianCounter = counter.bigEndian
let counterData = withUnsafeBytes(of: &bigEndianCounter) { Data($0) }
let signature = HMAC<Insecure.SHA1>.authenticationCode(for: counterData, using: SymmetricKey(data: secret))
let bytes = Array(signature)
let offset = Int(bytes[bytes.count - 1] & 0x0f)
let truncated = UInt32(bytes[offset] & 0x7f) << 24
    | UInt32(bytes[offset + 1] & 0xff) << 16
    | UInt32(bytes[offset + 2] & 0xff) << 8
    | UInt32(bytes[offset + 3] & 0xff)
let divisor = UInt32(pow(10.0, Double(digits)))
return String(format: "%0*u", digits, truncated % divisor)

The check is the RFC’s own: it publishes test vectors, and the first unit test uses one. The RFC’s secret, 59 seconds after 1970, must give 94287082. If my bit-shifting were off by a byte, that test would fail.

The backup key

Backups are encrypted with AES-GCM, using a key derived from a backup password with PBKDF2-HMAC-SHA256: 210,000 iterations and a random 16-byte salt. Each file records the algorithm, the iteration count, the salt and the nonce, so the settings can change later without breaking old backups.

CryptoKit has AES-GCM and HMAC, but no PBKDF2. So the key derivation is a hand-written loop around CryptoKit’s HMAC. That is exactly the kind of code the rule warns about.

Here’s the honest part. The backup tests are a round trip (export, import, compare) and a wrong-password check. Those prove the code agrees with itself. They don’t prove it agrees with PBKDF2. If the loop had a subtle bug, say the block counter in the wrong byte order, export and import would still match perfectly. The backups would just be weaker than they claim, and no test would ever turn red.

So while writing this, I ran the exact function against the published PBKDF2-HMAC-SHA256 vectors in RFC 7914, and against Python’s hashlib at the real 210,000 iterations. Every result matches: it’s correct. But checking once by hand isn’t a test. What I’d change: make those vectors unit tests, and swap the loop for CommonCrypto’s CCKeyDerivationPBKDF, Apple’s own implementation. Because the backup file records its algorithm, old backups would keep working.

What borrowing costsCopy link to this section

  • Apple only. No Android, no Windows, no web. It’s an app for my own devices, so that was an easy trade.
  • Recovery is yours, not mine. Lose access to your Apple ID and your backup password, and the data is gone. There’s no “forgot password” email, because there’s nobody to send one.
  • The lock screen is a door, not a safe. VPass’s own lock (Touch ID when it opens, auto-lock after 10 minutes by default) decides who can use the app. What protects the data at rest is the Keychain and the device’s own lock.

What I’d tell someone starting oneCopy link to this section

Write the list of things you won’t build before the list of features. For VPass it was three lines: no server, no accounts, no cryptography of my own. Each one removed a whole category of mistakes I could have made, and it’s why five days was enough.

And where you do end up writing something security-shaped, test it against someone else’s answers, not your own. A round trip only tells you the code is consistent.