kcrypt2 - a retrospective and a lab notebook.
Origins⌗
In August and September this year I was researching a broad assortment of topics in coding theory and information theory. The most important ones for today’s blog post are:
- Reed-Solomon codes, which I was already familiar with. Working on my book de facto forced me to write a few different Reed-Solomon error/erasure correcting code implementations (original view: Berlekamp-Welch decoder and the Gao decoder - I believe that my book will contain the first public and correct implementation of Shuhong Gao’s EEA-based, original view RS decoder; BCH view: Berlekamp-Massey, Peterson-Gorenstein-Ziegler, Yasuo Sugiyama’s EEA decoder).
- Goppa codes, which have certain interesting properties in the realm of cryptography.
Note that this blog post is much different from many resources you will find online: I chose to document my exact thought process and evolution of my idea. This means that, in essence, a lot of the ideas you will see that precede the main product simply suck. This is the ugly nature of research that no scientific paper or thesis admits, but in the author’s opinion it carries a lot of value.
Primers of Reed-Solomon⌗
Suppose that we have a -RS (Reed-Solomon) code. is the input block size, is the output block size. Some commonly used values for these parameters include (CCSDS). The codeword is formed by interpolating a polynomial in a finite field given the input block as the values, and then evaluating said polynomial at distinct points.
A polynomial of degree is uniquely determined by its values at distinct points. In other words, the mapping from coefficients to values of polynomials is a bijective linear map with the Vandermonde matrix . Further, the interpolation problem (i.e. the problem of determining the coefficients of a polynomial given its values) has a unique solution.
This theorem (sometimes called by the literature the unisolvence theorem) can be proved using the factor lemma. But most importantly, it tells us something very important: the -RS code is the 255 consecutive (wrt. field generator) values of a degree-223 polynomial. Hence, even if all 32 of the parity values were corrupted and we knew their locations (i.e. they were known erasures), we would be able to restore the original data.
Unfortunately, real life is not so beautiful and we usually don’t know where exactly errors occur. Various algorithms for decoding Reed-Solomon error-correcting codes have been proposed. However, for the -RS code, the PGZ and BW algorithms can correct only up to errors. In general, this is related to the fact that linear block code decoding, and more specifically polynomial reconstruction are a NP-hard problem: to decode the RS-code up to its capacity (which is 32 errors), we would have to try, in the worst case, all of the different possible options. Some recent reserach into list decoding and related methods makes it more realistic to correct more than 16 errors in such a code, but we will not spend too much time talking about that.
Applications to cryptography?⌗
When I finished the RS-codes section in my book, I thought that Reed-Solomon codes could make for a great cryptographic primitive: easy to encode, difficult to decode up to capcacity if you don’t know the error location. Perfect?
My first algorithm used MT19937 to generate the positions of the error locations. The key was the MT19937 seed, which made it possible for the holders of the key to reconstruct the corruption sequence. Then, after encoding the input blocks with a special variant of the RS-code, the randomly selected values were overwritten with the contents of /dev/random
. The modification that I made to RS-codes concerned the fact that they are nominally systematic code (i.e. the input is a part of the codeword - a desirable feature of error and erasure-correcting codes). To prevent leaking information about the plaintext, I have decided to interpolate the polynomial and then perform the evaluation at a distinct set of points.
It didn’t take me long to find problems with this idea:
- MT19937 is not a cryptographically secure pseudo-random number generator. If there was a way for the attacker with an encryption oracle to extract some of the consecutive outputs of the random number generator, the key is compromised and the scheme is insecure. There are ways to make MT19937 work in a cryptosystem (more on that later), but this is the most glaring issue to most cryptography experts.
- Low variance inputs result in low variance outputs. Interpolating a polynomial through a zero polynomial (which is what effectively happens when we encrypt a block full of zeroes) obviously ends up with us sampling the parity bits as zero too. Hence the corruptions in the ciphertext are very obviously visible and it is easy to relate the ciphertext to the plaintext. Further, it is also easy to recover the key, as we have some degree of knowledge about the MT19937 outputs that have lead to some particular choice of values to corrupt.
- My implementation of Lagrange interpolation in was cubic-time. While Lagrange interpolation is unavoidably cubic time for floating point computations, an optimisation to it can be made in finite fields to reduce the running time to quadratic. Such an optimisation is not feasible in scientific computing, as it leads to a catastrophic loss of precision.
- The output is expanded by the presence of the parity bits. Such eccentric behaviour is exhibited by some existing ciphers (e.g. ElGamal encryption), but I have considered it a big flaw from the very start.
Soon after I have fixed the second problem producing the canonical “kcrypt2” implementation that I blogged about a while ago that appended a key-derived nonce to a segment of the polynomial values before interpolation. As always I am skimming over a few details, because detailed explanations are very tiresome to write, so please refer to the previous blog post for how the program actually worked.
Fixing the problems, one by one.⌗
The 2x expansion problem was so bad that it didn’t let me sleep. Thankfully, I found a solution to it: instead of just sampling the polynomial and then corrupting parts of it, what if we sampled it at a random subset of points given by the key? This way, the corruptions become implicit: we transmit only the correct values, but we don’t inform the other party about what points these values have been sampled from (they’d need a key to determine that).
Another problem that I wanted to tackle was the MT19937 insecurities. The Mersenne Twister FAQ hosted by the mathematics department of the Hiroshima university says that applying a compression function to a set of MT19937 outputs (e.g. like the RIPEMD160 compression function - “one way”, non-bijective mapping) makes it effectively a cryptographic random number generator. Indeed, if we wanted to use a compression function on existing non-CSPRNG output, Mersenne Twister is actually one of the best choices thanks to its large period.
The game plan for the compression function was a set of matrix products, transpositions and row rearrangements that are difficult to invert.
kcrypt2b, demonstrated.⌗
The prelude, containing MT19937 code and the finite field code:
The compression function for MT19937 outputs:
We will interject at this moment. How can we be sure that this compression function is actually secure? While there are many premises that would support this theory, we will first try to make sure that its explicit formula performs sufficient amounts of diffusion. For this purpose, I wrote a small Python script using z3 for analysing this particular compression function:
The simplified outputs made it clear that each cell in the result vector is a (rather complicated) polynomial expression in 64 variables and at the time, I believed that inverting it across paddings to obtain consistent results would be difficult enough. In retrospect, I think that this step was either unnecessary, or I should have used a more complex compression function.
When it comes to these, it is very easy to make a mistake. For example, the CryptMT variant of Mersenne Twister that is cryptographically secure uses a similar idea, but the properties of its compression function were not very well understood, hence it was rejected in the eSTREAM competition.
Finally, the block cipher part was slightly simpler than before, using the -optimised version of Lagrange interpolation:
More details regarding this variant are available in the repository.
kcrypt2c⌗
The main difference between kcrypt2b and kcrypt2c was me succumbing to an existing, high quality compression function. I decided to use RIPEMD160.
kcrypt2d⌗
At this point, I have realised (too late!) that all symmetric block ciphers are merely a clever way of combining the key and the plaintext. While AES accomplishes this through a set of rather obscure and difficult to explain operations, kcrypt2 achieves this through polynomial interpolation. Thinking more about this made me realise that many things about my cipher were excessively defensive: AES keys are small, AES is not IND-CPA and it aims to provide significantly less than my original idea. Is it time to backtrack?
The new idea (and the last one I present in this blog post) is simple:
- Take the RIPEMD160 compression function which is a mapping 64B -> 16B.
- Generate a 60 byte key.
- For each block compute
ripemd160_compress(key + uint32(block_number))
- this is our padding. That gives us 16B of data that is derived from the key in an irreversible form and different for each block. - Interpolate 16B of data at and the 16B of key-derived nonce at to form a degree-32 polynomial.
- Sample it at 16 points at . That is our ciphertext.
To decrypt the data, the code simply regenerates the block padding based on the key and interpolates the ciphertext and padding to get the data back. An implementation of that is found here.
Wrapping up⌗
Exploring cryptography by trying to make my own symmetric cipher was very fun. I want to look into turning kcrypt into an assymmetric cipher (just for the fun of it!) and experiment with other existing algorithms. I hope that you enjoyed this very brief overview of my somewhat original idea of applying polynomial interpolation to cryptography.
Regarding security: some of the variants are probably more secure than others, but the overall security for data at rest remains an open question. If you would like to conduct cryptoanalysis of the strongest (in your opinion) variant, make sure to send me an e-mail.