Secretly Generating Keys

- 5 mins read

Generating cryptographic keys is the most important* part in the cryptography world.

Why and How?

random numbers

I’m not going to talk about the theories and principles of cryptography, but there’s one thing you need to know that, for asymmetric cryptography, the keys are everything. Your private keys take the responsibility of authenticating, decrypting and signing etc.

Therefore, there are mainly two important parts. The first one is to safely generate your keys. Keys are essentially processed random numbers. The randomness of the numbers is of most significance. However, computers, as a strict and accurate machine, is not good at doing these kinds of things that need entropy (the randomness_).

Then you might ask, my computer can generate “random” numbers. That’s because your computer generates numbers based on some algorithms which needs an input. We call that a seed. Same seed will output same numbers for the same algorithm. This is called a pseudo random number.

Ironically, the seeds that modern computers use are not safe. Mostly they will only use the current date and time, which is super unsecure. There are more than one example that the second-person obtained the original password by knowing the accurate date and time of the password generation.

Therefore, you would want something called a true random number generator.

It’s worth clarifying that there’s no true random in the world. The nature always works in certain rules. What we can do is to make the possibilities as small as possible, i.e. to be as random as possible.

Some Intel and AMD CPUs have a virtual TRNG built-in, which I suppose used the current heat noise. But critics say that there might be backdoors in such TRNGs. Therefore it’s not suggested to use, although you might be using it already.

What I suggest is to get a USB TRNG device like InfiniGANG. These devices aren’t cheap. Most of them are around 100 US$.

I haven’t had one till this day though.

The easiest and probably-effective way is to use what you already have. In my case, I generated some random string and dumped them to the system RNG. (in Unix/Linux, it is /dev/urandom)

If you have a YubiKey, YubiKey has a TRNG built-in which we can use.

You might want to do something like this.

echo "somerandomstring" | dd of=/dev/urandom
# OR
gpg-connect-agent 'scd random $RANDOM\00' | dd of=/dev/urandom

physical environment

The most fragile part of modern cryptography is likely to be yourself.

No secure measures really matter if there’s someone who is staring at you from your back, or a CCTV camera is pointing at your computer screen, monitoring your fingers. So make sure the environment is safe. Preferrably in a dark room with curtains closed and lights down. Or even better, in the restroom, where there’s unlikely to be any surveiliance existing.

virtual environment

Again, it doesn’t matter if you are going to put your keys on a smartcard or a security key if you are using a commercial operating system, especially Microsoft Windows, where the telemetry is literally everywhere, potentially dumping your system memory and phoning home without your permission. Apple’s macOS might be better but it makes literally no difference when it comes to the mass surveiliance of the US government, or any other government-backed instituion.

What I suggest would be an open-source SBC with a fresh-copy of operating system, with Internet never plugged in, and completely wipe the system storage after generation. Alternatively, and, might be even better, is to use Tails, the completely amnesia operating system recommended by multiple whistleblowers and journalists. (It’s nice.)

Both the Tor project and Tails have their sponsorships from the US Defense, so it wouldn’t be complete secure. But since they are all open-source and being monitored by the public, I wouldn’t say there can be any major problems since a lot of public whistleblowers have been using it for a long time without any problems.

keystore

The second to the most insecure would be putting your keys on your computer, like in ~/.gnupg.

GNU Privacy Guard encrypts your key using a passphrase which you need to provide upon key generation, but we need to take keyloggers into consideration.

The easiest, though costly, solution is to buy a smartcard, or smartcard-like device, like a YubiKey, and store your keys there.

You should ALWAYS have a backup of your key, unless you know exactly what you are doing. Usually the suggested way of storing the backup is to copy them to an encrypted, removable flash storage, e.g. a USB drive, or an SD card. Alternatively, you could export them in an ASCII-armored format and print it out.

Keep in mind that some printers will keep a copy of what you print (which is kinda understandable), but most of the times it happens between your computer and the printer. So do NEVER print these kinds of things using a wireless connection. Use a USB cable instead.

If you are still being too paranoid, just grab a pen and a piece of paper and copy them one character by one character.

Don’t use OCR. Most of the times they need Internet, and you wouldn’t want to let a remote server to process your keys.

What I do is to encode them to the format of QR codes, stripping all the signatures (those aren’t necessary for a backup, and they exist everywhere as a part of public key), and mail them to some of my most-trusted friends, encrypted, of course.

Conclusion

Note that these methods are BASICS and are nowhere even near bulletproof. Do with caution.