E2E encrypted chat (ML-KEM) over MQTT, implemented as a hardware peripheral.
README.md

Selectric-256 #

What If? #

What if E2E encrypted chat was implemented as a hardware peripheral?

What if we rolled-in WiFi, so that the end-user device has no Internet connection of it's own? It's hard to trust endpoint security these days, and we've all go old computers we can re-use.

What if we used MQTT for communication, so that there are already multiple compatible public servers around the globe, but you could also trivially host your own?

What? #

This project has the following technical objectives:

  1. Implement E2E encrypted chat over MQTT using AES-256 GCM.
  2. Implement ephemeral key exchange & compute a shared secret with ML-KEM
  3. Each device is paired to another, on boot they will find each other & exchange keys with HMAC-like authentication.
  4. Runs on an ESP32 (S or S3) as a peripheral to a computer that has no Internet connection of its own
  5. Should have basic protection against message replay
  6. Should a serial+USB peripheral, so that it may be used with computers both ancient and modern. This means it will need an (optional) PCB that adds a real serial port.

Less technically, it lets you:

  1. Plug it in to a device that doesn't have Internet access. Now it can connect to WiFi, but only for the purposes of engaging in E2E encrypted chat with exactly one other device. No other connectivity is permitted. The device doesn't even know how to engage in other connectivity.
  2. This simplifies endpoint security. Since it's only allowed & capable of doing one thing, and that one thing is completely on public channels, it's pretty easy to detect if it's doing anything else.
  3. By implementing chat over public MQTT servers, there's no single central provider.
  4. It is intended to be controlled by a software application that provides a UI etc. suitable for the host device.

There's no TLS support presently. I may add it later, but overall the device doesn't rely on trusting the broker.

Why? #

I was quite ill recently, and in my fever-dreams I recalled a device: The IBM Selectric-251, a fictional typewriter from a sci-fi television show called Fringe.

The nature of my madness mandates that I create the devices that claw their way out of my dreams. Also, my C++ is not very good, and this was an opportunity to work on it a bit. As such, I didn't see much point of using AI to write any code, all hallucinations are entirely my own (re: fever-dreams).

In the television show, two "quantum-entangled" typewriters function such that what you type in one comes out the other, regardless of where they are and without the possibility of the message being intercepted.

Since the device from the television show functions via magical "quantum-entanglement", and real-life quantum entanglement doesn't work that way, we'll need to "entangle" them via some other method. So instead I use two arrays of 32 bytes generated by:

tr -dc A-Za-z0-9 </dev/urandom | head -c 32; echo  

One array identifies the local device, the other array identifies the remote device. These are never used for encryption, because then an attacker that obtains one of the devices could decrypt all prior communications.

So instead, these identifiers are used to:

  1. Compute two MQTT channels, optionally with a user-specified modifier like the first 7 digits of the Unix timestamp
  2. Authenticate (with something HMAC-like) a handshake that performs ML-KEM key exchange

The computed channels are not for security, only to obfuscate traffic analysis if the user wishes to do so.

Once the key exchange is complete, the devices have an ephemeral shared secret suitable for performing AES-GCM. For simplicity, GCM is used to authenticate all future messages. I added a session message counter (encrypted along with the plaintext) to prevent message replay.

This design offers several advantages over the fictional Selectric 251:

  1. It exists
  2. It uses 256 bit encryption, which is 5 bits more than 251 (I presume that's what the '251' indicates)
  3. It's compatible with more than just electric typewriters
  4. It's better than quantum, it's post-quantum!

A weakness of the system is that anyone possessing the 32-byte identifiers on the device can impersonate the owner (although they cannot decrypt prior communications). So, store your devices somewhere safe.

Current Status #

This is currently a working proof-of-concept. The two devices connect up automatically and can exchange messages. The technical objectives are met.

The boards are designed, but they have not arrived from the factory yet. So they are untested.

The Python script is a very primitive interface that lets two devices connect up and chat. The async version is written by AI, the rest of the codebase is not. I wanted to make sure that the protocol was simple enough that an AI could one-shot it (in case that's what people want to do).

Overall, I think this needs more work before I can recommend it for any serious use case. Here are some things I think need work:

  1. The communications convert everything from bytes to hex strings -- this was so that I can more easily listen in on the communications and check everything is working correctly. MQTT supports sending raw bytes I think, so I can add that later to cut bandwidth and memory requirements by about half.
  2. I'm still not very good at C++ (although better than when I started this project) and my memory allocation is all over the place. We've got enough memory to handle it, but it's painful to look at.
  3. It would be neat to do a UI in Godot. Perhaps I could even support old Android phones, although I'm not sure how to be 100% certain they can't send data out on their own.
  4. There are some features on the ESP32 to assist in storing secrets. However, I'm not sure if this is a good feature to add. It would make it a pain to update the device's permanent ID if needed, and if someone gets physical access to your device, I'm not sure it would really help that much. I might look into it more later.
  5. There's no native support for teletype machines, although it should not be too hard to add if someone wants it.