(8 minute read)
I recently released version 2 of Heartnotes - my secure, encrypted diary app. This was a major rewrite which changed it from being a purely offline app into one which synchronized with a server back-end.
The challenge with this was to ensure that a good user experience could be maintained given these changes. Heartnotes' security comes from its implementation of client-side encryption. All data is encrypted on the user's computer before it gets pushed to the server. And the user's password never leaves their computer. All together this means that only the user can open and read their diary; no one else can, not even the server owner.
In this post I'm going to outline the general principles behind the encryption architecture I've implemented - these principles are applicable to any app that does client-side encryption though I'll refer to Heartnotes as an example.
First, let's talk about the raw data...
How you encrypt your data and sync it with the server depends on how you envision accessing and modifying the data in the first place.
In the case of Heartnotes, a user may write one or more entries into their diary. Each entry has a unique id and the actual entry content - its body. In v1 of Heartnotes the way the encryption was performed was as follows:
This flow was suitable for v1 since we were only communicating with the local file system (i.e. no network delay). Plus it means that the diary can be opened and closed quickly - since we encrypt and decrypt all entries in one go.
In Heartnotes v2 the user's encrypted entry data needed to be synced with the server on-the-fly. The above flow is not really suitable for this since we don't want to be sending the entire encrypted diary file to the server on every save.
So the first change to be made is to encrypt each entry individually:
This new flow has the added benefit of slightly speeding up the encryption process during each save since we no longer have to re-encrypt the entire diary file. It also minimises redundant work since entries that weren't modified aren't being unnecessarily re-encrypted.
The downside of this change is that now when we open the diary we have to decrypt each entry individually, which means loading can take a bit longer. But we can mitigate this by redesigning the UI to allow for loading and decrypting entries in parallel (i.e. use loading indicators until decryption is done) - it's a minor inconvenience for users in return for the server sync benefits.
So now that we encrypt each diary entry individually, how do we handle sync with the server? Our server synchronisation serves two purposes:
Cross-instance sync is a standard feature of many apps today, such as Google Docs. But we want to enable this with data that's encrypted client-side, which makes things a little trickier.
So when the user updates an entry here is what we can choose to send to the server:
Here is why the latter is preferable...
Sending just a delta means less data needs to be sent to the server, and is thus more bandwidth and time efficient. This is the sort of thing see happening in multiplayer games - where they send just the key strokes made by the user on the source machine rather than the whole game state. On the other person's machine the retrieved keystrokes and are then applied to the local game state to reproduce the same up-to-date game state as on the source machine.
The caveat - and thus the weakness with this method - is that both machines must always be at the same point in terms of game state in order for deltas to be applied properly. In multi-player games it's immediately obvious if this isn't the case (you'll notice weird game errors, lag, etc). Typically for such games, both parties have to be connected to the internet at the same time in order for the game to work anyway, so it's easier to meet this requirement.
With an app like Heartnotes we want to enable offline editing. So for example, I may edit an entry on my laptop now and have it synced to the server. Later on, I may re-edit this entry from its old version on my tablet which doesn't have an internet connection. How do we reconcile the two? Obviously the tablet-edited version is newer than the laptop-edited version so it should previal. But on the server-side we now have the laptop-edited delta stored. This delta would need to be dropped, plus the tablet-edited delta would need to be sent to the server and then synced across the laptop next time I open the laptop:
Thus in the above scenario, the Laptop would need to keep storing the old pre-edited version of entry A just in case it will in future receive a Tablet-edit entry A delta. It is immediately obvious how inefficient this is.
Furthermore, what happens when we sign in to the diary on a new device? We want all the entries to be downloaded and displayed and they should all be up-to-date. We don't want to be dealing with deltas yet.
If we weren't using client-side encryption, the server would be to apply each incoming delta to its locally stored copy of the entry. But since we are using client-side encryption it cannot do this, and thus, we end up having to send the fully encrypted entry to the server on each save in order to keep the architecture clean and efficient.
As mentioned previously, one of the downsides of encrypting each entry in the diary individually is that it takes time to decrypt the diary on initial load. But there's another problem. If I change my encryption key then every single entry will have to be re-encrypted.
In v1 of Heartnotes this wasn't a problem since all entries were encrypted together in a single pass. But in v2, we not only encrypt each entry individually but also sync each encrypted entry to the server and across to other devices. What are the implications of this with respect to updating the encryption key?
Well, let's say I change my encryption password on my Laptop, and let's say my Tablet is currently not connected to the internet and/or Heartnotes isn't running on it. The only way I can ensure that my Tablet diary data is correct when it does eventually connect is to re-encrypt every entry on my Laptop and send all of this data to the server - for the Tablet to pull down later on:
Ah, but there's a problem with this. Remember that we want to allow for offline editing. So I might go and edit an entry on my Tablet (whilst its offline) after I've already updated my password on my Laptop. If this happens then I would still want to keep my Tablet edits. But now I have a problem: my tablet edit is encrypted using my old password but all my server-side data is now encrypted with my new password. Damn.
The solution is to NOT link the encryption key to the user's password.
In v1 Heartnotes we used PBKDF2 (SHA-512) to generate the encryption key as follows:
In v2 we now introduce the concept of a master key which is separate to the actual encryption key:
The key thing to note is that since we don't store either the salt, hash or iteration count used to generate the encryption key it's not really feasible to derive it from the user's password later on.
Thus, when we change the user's password we simply derive a new master key and then use this to re-encrypt the bundle containing the actual encryption key. Thus we would only need to send the new salt, iteration count and bundle to the server and sync that across to other devices to ensure everything is updated everywhere. The actual encrypted entries themselves remain unchanged when the user changes the password.
##Â Authentication with the server
Note the authentication key mentioned in the flow above for the generation of the encryption key in Heartnotes v2. Client-side encryption followed by server sync means nothing if the server gets to see the user's password.
We don't want anyone with access to the server being able to derive the master key from the user's password yet we still want users to be able to login. The authentication key enables this by virtue of the following properties:
As long as the above two properties hold, the authentication key is safely usable as a means of authenticating the user with the server. On the server-side, for additional security we should store a hashed version of the authentication key in the database - to prevent people who get hold of the database from being able to login as a user even if they can't read the user's data.
One way in which authentication security could be strengthened is by using a vetted protocol such as SRP. This may be implemented in a future release of Heartnotes.