Client-side encryption

03 May 2021

Recently I added client-side encryption to Taigen, my note-taking app. In client-side encryption the client—your web browser—encrypts data before sending it to the server. In contrast end-to-end encryption is used in messaging apps where the message is encrypted by the sender and decrypted by the receiver.

Advantages of client-side encryption

  1. Search warrants are useless.
  2. Database hackers get nothing.
  3. Accidental data breaches reveal nothing.

When I looked into encryption I wasn’t thinking of nullifying police warrants; I was concerned about hackers or a data breach where the web host discards a hard drive but doesn’t erase it first.

Taigen: under the hood

Taigen stores your notes locally as plaintext in IndexedDB, a database built into your browser. Taigen synchronizes this database with the server database in the background whenever it detects a local note change.

Adding encryption was fairly simple: Javascript has a shiny new Web Cryptography API built-in so you don’t need a third party library. I did have to update a few of Taigen’s features:

Encryption key from your password

When you create an account or log in, an encryption key is derived from your password and stored in IndexedDB, per the recommendation of the World Wide Web Consortium which developed the Web Cryptography API standard:

In practice, it is expected that most authors will make use of the Indexed Database API, which allows associative storage of key/value pairs, where the key is some string identifier meaningful to the application, and the value is a CryptoKey object. This allows the storage and retrieval of key material, without ever exposing that key material to the application or the JavaScript environment.

Concepts of Key Storage, World Wide Web Consortium.

Background encryption

Encryption occurs during sync so the user doesn’t notice a slight delay caused by the crypto calls.

Local note export

Before, the client could download a zip file of your notes from the server. Now the client compiles a zip file of your plaintext notes from its IndexedDB. Client-side zipping is faster—the client doesn’t have to poll the server to see if the file is ready—but requires a third party zip library.

Local text file import

Taigen allows you to import text files which are converted into notes. The old way: upload the notes to the server which syncs with the client’s copy of the notes stored in IndexedDB. The new way: notes are added directly to IndexedDB, then encrypted and synced to the server. Implementing this client-side allowed the removal of a huge amount of server-side polling code but only took a few lines of Javascript. Amazing.

Don’t lose your password

Because your notes are encrypted with a derivation of your password, if you lose your password you lose your data—which is why you should back up your notes by exporting them weekly, a good idea when using any cloud-based service.

The Feds want your keys

Speaking of encryption, Congress recently tried to pass a key disclosure law requiring a back door in all public cryptography. Ignoring the foolishness of handing over the keys to your papers and effects because someone demands it—can the US government be trusted with the power to read every financial transaction and hospital record? Can we trust it not to lose the keys?

The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no Warrants shall issue, but upon probable cause, supported by Oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 4, US Constitution

TSA master luggage key hack

The US Transportation Safety Administration (TSA) has a program where TSA-approved luggage locks can be opened with a master key allowing airport security to expedite baggage searches. When the Washington Post published a photo of a TSA master key, hackers studying documents published by Travel Sentry—the group handling TSA protocols—were able to create a TSA master key that could be printed on a 3D printer.

Snowden breach

Edward Snowden was a computer consultant for the CIA. Appalled by the widespread spying on US citizens, he copied and published scores of NSA documents on top-secret programs such as PRISM, which gives the NSA direct access to Americans’ Yahoo and Google accounts.

Snowden revealed so many government secrets they really don’t know the extent of the damage. If the government can’t secure its own data, how can it secure the keys to every financial transaction and medical record in the US?