Crypt(1), Again
November 1, 2019
In the previous exercise I tried to write a replacement for the old Unix crypt(1) program, but never did figure out how to enter a password in Chez Scheme. So today I have the program in C.
To answer some questions that came up in the previous exercise: Yes, I know about ccrypt
. Yes, I know that I should not rely on any cryptographic code I write. Yes, the original Unix crypt
wasn’t very secure, though at least I can claim that my crypt
is better now than the Unix crypt
was at the time.
You can see my crypt
program on the next page.
I’m sure no-one was suggesting you personally were unaware of these things, I certainly wasn’t.
It occurs to me that both your solution and mine are deeply flawed – the keystream depends only on key so, as is well known, if two messages are encrypted with the same key, xoring them together gives the xor of the two plaintexts, with the two identical keystreams cancelling each other out, and a skilled cryptographer will be able to split out the two messages and reconstruct the keystream (this is how the brilliant John Tiltman made the first inroad into the Lorenz cipher at Bletchley Park).
Usual solution is to use a salt or nonce value to perturb the encryption, here’s Speck again, with a randomly generated salt, written as the first 8 bytes of the ciphertext. Unfortunately, this means we have to distinguish between encryption and decryption modes:
There’s a serious flaw in that solution as well – the read at line 15 isn’t guaranteed to read N bytes, even if we aren’t at EOF (eg. if reading from the terminal). Better to use fread (and fwrite), which may also be more efficient since the internal buffering mean fewer syscalls are made. open and read are the right thing for /dev/urandom, which we are told doesn’t block, but in fact on Linux we can use “getrandom(2)” to make life even easier:
Updated my Python solution to eliminate the issue raised by matthew.
Here’s a solution in C. The code depends on wraparound for some of the modular arithmetic.
Example usage (the key is “praxis”):