End-to-end encryption

End-to-end encryption

Encryption is becoming more and more important every day, as more of what we do is online. This post will attempt to explain everything you need to know about end-to-end encryption.

What are some advantages of end-to-end encryption?

The largest advantage of end-to-end encryption is that only the sender and recipient can ever know the message. No matter at what point the information being sent is grabbed by an attacker, the data is safe. If done properly, end-to-end encryption means that even the service running everything can’t read the messages, without brute-forcing all the private keys. End-to-end encryption can even be used on unsecured HTTP connections without the fear of having confidential information being read.

However, there are very few reasons to send end-to-end encrypted messages over an unsecured connection. It is generally better to send even the end-to-end encrypted data over TLS, because setting up TLS is simple, and it adds an extra layer of protection.

What is encryption?

In order to understand end-to-end encryption, it is necessary to understand encryption in general. There are two types of encryption: symmetric and asymmetric, both of which are used in end-to-end encryption.

Asymmetric

Asymmetric encryption is a form of encryption in which one key is used for encryption, and another is used for decryption. In order to use this form of encryption, two keys must be generated: a public and a private key. The public key can only be used to encrypt data, while the private key can only decrypt data encrypted with the public key. While rather slow, this form of encryption is essential for end-to-end encryption. Only the holder of the private key can decrypt the ciphertext(encrypted data) generated from the public key.

The public key can be shared over an insecure medium(such as before the TLS session is established) because no harm can be done if an attacker gets a hold of the public key. The private should rarely ever be transmitted over any medium, as an attacker with this will be able to decrypt all ciphertext generated with the public key.

The most well known asymmetric cipher is RSA.

Example:
The following example will use the openssl command and the RSA cipher to demonstrate encryption and decryption with an asymmetric cipher(commands adapted and copied from here). If you’re going to use the following for anything but an example, consider encrypting the private key for security purposes.

openssl genrsa -out private.pem 2048

Now, we have the private key. But, asymmetric encryption also requires a public key. This can be generated with:

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

Onto encryption! The rest of the commands are adapted and copied from here. The plaintext file(“test.txt”) contains “Hello”

openssl rsautl -encrypt -inkey public.pem -pubin -in test.txt -out test.enc

To decrypt(notice how this is now private.pem and not public.pem):

openssl rsautl -decrypt -inkey private.pem -pubin -in test.enc

If all went well, the output will be:

Hello

Symmetric

Symmetric encryption is encryption in which the same key, or password, is used for both encryption and decryption. It can’t be decrypted into the original plaintext(unencrypted data) without the correct key. Symmetric encryption generally has better performance, but at the cost of needing to share a key. This is why symmetric encryption is rarely used on its own.

Some well known symmetric ciphers are AES(Advanced Encryption Standard) and Blowflish.

Example: In the following example, I will be using the openssl command and the Blowfish cipher to encrypt and decrypt data using the same key. The key will be “abc123” and the plaintext will be “This is a test”.

echo "This is a test" | openssl enc -a -bf -pass pass:abc123

The resulting ciphertext is:

U2FsdGVkX1+KmcI5uo5wl1fq9AV/aibZI5Yan1qNIVk=

It’s important to note that the ciphertext in this example is encoded with base64(the -a flag) to make it somewhat readable. Without base64, the result is a bunch of weird characters:

Salted__k'~????gO??<|g??tXS.+?

Since Blowfish is a symmetric cipher, the decryption process will use the same exact key.

echo "U2FsdGVkX1+KmcI5uo5wl1fq9AV/aibZI5Yan1qNIVk=" | openssl enc -a -d -bf -pass pass:abc123

If all went well, the result will be the original plaintext:

This is a test

Note: If you run the example above, you will likely get a different ciphertext. This is because OpenSSL will add a salt to make brute-forcing harder.

Now, how does end-to-end encryption work?

End-to-end encryption can utilize both forms of encryption. However, in order to make this easier to understand, only an asymmetric cipher will be used.

The process will look something like the following:

  1. An asymmetric cipher is used to generate public and private keys.
  2. The public key of each user is stored on a server.
  3. The sender pulls the public key of the recipient from the server.
  4. The sender uses the public key to encrypt the message.
  5. The sender sends the encrypted message.
  6. The recipient pulls the message from the server and decrypts it with their private key.

However, RSA, the asymmetric cipher used in most cases, has a rather low size limit. While the limit will likely not be hit for things such as text messages, sending media or longer messages will reach the limits of the asymmetric cipher. End-to-end encryption utilizing both asymmetric and symmetric keys will look like the following:

  1. An asymmetric cipher is used to generate public and private keys.
  2. The public key of each user is stored on a server.
  3. The sender pulls the public key of the recipient from the server.
  4. The sender creates a key for a symmetric cipher, and encrypts that key with the asymmetric public key.
  5. The sender uses the symmetric key to encrypt the message.
  6. The sender sends the encrypted message, and the encrypted symmetric key.
  7. The recipient pulls the symmetric key, and encrypted message from the server.
  8. The recipient uses their private key to decrypt the symmetric key.
  9. The recipient uses the symmetric key to decrypt the message.

As with most things, the more steps there are, the more likely there is to be a mistake. When possible, just using an asymmetric cipher for messages has a smaller chance of a screw-up that could end up compromising all encrypted data!

Sources: SSL2BUY | Thilo(Stack Overflow)

Video

If you’re more of a video person, I recommend the following video from Computerphile:

Leave a Reply(Markdown is On)

%d bloggers like this: