Live data from Hacker News

The design of Chacha20

loup-vaillant.fr

51–60 of 76 posts

Re: The design of Chacha20

#51
post #23

a += b; d ^= a; d I'm curious: could a C compiler look at the quarter-round function above and determine that a+b (or the other terms) might overflow a 32-bit integer, and thus invoke undefined behaviour to eliminate the loop entirely?

Even if it were undefined behaviour, it would still only be conditionally undefined. A compiler can only eliminate a code path that unconditionally results in undefined behaviour.

Re: The design of Chacha20

#52

Earlier quoted context omitted.

> A good place to start understanding why you want rotation and nonlinearity is the Wikipedia page for SP Networks: I guess the real story requires knowing a little bit about linear and differential cryptanalysis, which are conceptually quite simple in their genesis, from a mathematical perspective. XOR and n-bit addition are both forms of addition over different finite fields, GF(2) and GF(2^n). Multiplication in GF…

Are there any concrete, simple examples showing linear and differential cryptanalysis (simple, breakable cipher + example cracking program)? As much as I've studied the theory and perused the design decisions of modern ciphers to avoid such attacks, I've never taken the time to sit down and actually crack a simple cipher using them. Would be neat to do so.

I did some of these exercises a long time ago and learned a lot. https://www.schneier.com/academic/paperfiles/paper-self-stud...

The starter exercise labeled 6.2 is a good way to get your feet wet with the ideas I described. 12-round DES without any S-boxes consists of P-boxes (permutations) and XORs, which are both linear over GF(2) vector spaces, so it's a linear block cipher and hence trivially breakable with any linear algebra package. RC5 without rotations is not exactly linear over either GF(2^n) or GF(2) since it mixes XORs and (mod 2^n) additions, but the combination is only very weakly nonlinear (there's not enough avalanching from the carries to entangle entries that are far apart), and therefore a good demonstration of why you need rotations in ARX to introduce rapid long-range bit entanglement. And in case it wasn't already obvious, the exercise about RC5 with rotations by a round number will show you why the rotation amount in ARX should be relatively prime to the bit width. Otherwise you end up with disconnected rotation orbits where the round function only mixes within a given orbit. In the extreme case where the rotation amount is half the bit width, each orbit contains at most two elements, so it's hardly any better than no rotation at all.

I bet there are also modern textbooks in cryptanalysis with exercises and a more hand-holding approach. Maybe any cryptographers reading this could recommend something.

Re: The design of Chacha20

#53
post #23

a += b; d ^= a; d I'm curious: could a C compiler look at the quarter-round function above and determine that a+b (or the other terms) might overflow a 32-bit integer, and thus invoke undefined behaviour to eliminate the loop entirely?

Even if the numbers were signed (unsigned overflow is well-defined), being able to eliminate the loop would require knowing that overflow MUST occur, not that it MAY occur.

Re: The design of Chacha20

#54
the nonce and counter state words seem to be swapped in the 3rd figure of the "A much bigger nonce: XChacha20" section:

  block'[ 0]: "expa"      block'[ 8]: kcolb[12]
  block'[ 1]: "nd 3"      block'[ 9]: kcolb[13]
  block'[ 2]: "2-by"      block'[10]: kcolb[14]
  block'[ 3]: "te k"      block'[11]: kcolb[15]
  block'[ 4]: kcolb[0]    block'[12]: nonce[4]    

Re: The design of Chacha20

#55

the nonce and counter state words seem to be swapped in the 3rd figure of the "A much bigger nonce: XChacha20" section: block'[ 0]: "expa" block'[ 8]: kcolb[12] block'[ 1]: "nd 3" block'[ 9]: kcolb[13] block'[ 2]: "2-by" block'[10]: kcolb[14] block'[ 3]: "te k" block'[11]: kcolb[15] block'[ 4]: kcolb[0] block'[12]: nonce[4]

Corrected, thanks.

Re: The design of Chacha20

#56
post #4

Earlier quoted context omitted.

NaCl provides Salsa20 and XSalsa20 ( https://nacl.cr.yp.to/stream.html ). libsodium adds ChaCha20 ( https://download.libsodium.org/doc/advanced/chacha20.html ) but not XChaCha20.

That's coming soon: https://github.com/jedisct1/libsodium/blob/master/src/libsod...

ugh, an extra ietf variant that pads the remaining 64-bit from the nonce to fit 96 bit thats incompatible with all the implementations out there... :-(

why can't we get our shit together...

Re: The design of Chacha20

#57
post #29
post #26

Earlier quoted context omitted.

Chacha/Salsa is: * Intrinsically simpler than AES * Easier to implement * As an ARX design, doesn't need S-boxes, and so doesn't leave a cache footprint * Has free key setup AES is: * A global standard * Available in hardware on most platforms (extremely important) * A conventional block cipher for which a bunch of modes (in particular: wide-block and AEAD) are already defined But unlike Salsa, AES: * Has relatively…

> * A global standard ChaCha is standard enough to make it into TLS and IPSec

Even then, they actually used a tweaked version of ChaCha20 that uses a 96-bit nonce (just barely large enough to be suitable for randomly-generated nonces) and a 32-bit counter (limiting its use to 128GiB for a given nonce). Also, an extension XChaCha20 was recently published which performs an extra 20 rounds to initialize the cipher state, allowing for 192-bit nonces with no corresponding reduction in counter size.

So now there's three variants of ChaCha20

  * ChaCha20 (256-bit key, 64-bit nonce, 64-bit counter)
  * IETF ChaCha20 (256-bit key, 96-bit nonce, 32-bit counter)
  * XChaCha20 (256-bit key, 192-bit nonce, 64-bit counter)

Re: The design of Chacha20

#58

the nonce and counter state words seem to be swapped in the 3rd figure of the "A much bigger nonce: XChacha20" section: block'[ 0]: "expa" block'[ 8]: kcolb[12] block'[ 1]: "nd 3" block'[ 9]: kcolb[13] block'[ 2]: "2-by" block'[10]: kcolb[14] block'[ 3]: "te k" block'[11]: kcolb[15] block'[ 4]: kcolb[0] block'[12]: nonce[4]

Corrected, thanks.

the page didnt change for me yet, caches?

Re: The design of Chacha20

#59
post #25
post #23

a += b; d ^= a; d I'm curious: could a C compiler look at the quarter-round function above and determine that a+b (or the other terms) might overflow a 32-bit integer, and thus invoke undefined behaviour to eliminate the loop entirely?

In C, unsigned overflow is defined as wrapping, and all these numbers are unsigned. It's signed overflow that's undefined in C.

'Safe for for now'

Notable I think the NaCl crypto library implements a compare as follows

   uint32_t diff_bits = 0;
   diff_bits |= x[0] ^ y[0];
   ...
   diff_bits |= x[31] ^ y[31];
   return (1 & ((diff_bits - 1) >> 8)) - 1;
This because memcmp() leaks timing information. And implementing it with a for loop also leaks.

Longer term worry is the optimizer will figure the above out as well.

Re: The design of Chacha20

#60
post #26
post #8

How does ChaCha20 compare to the established AES standard? Is it stronger? weaker? faster? slower? easier to implement correctly? harder to implement correctly? better for some other reason? worse for some other reason?

Chacha/Salsa is: * Intrinsically simpler than AES * Easier to implement * As an ARX design, doesn't need S-boxes, and so doesn't leave a cache footprint * Has free key setup AES is: * A global standard * Available in hardware on most platforms (extremely important) * A conventional block cipher for which a bunch of modes (in particular: wide-block and AEAD) are already defined But unlike Salsa, AES: * Has relatively…

> Both are so fast that the speed difference is not particularly important,

Without hardware support timing attack resistant AES is not so fast.

(and then there is the adventure of many motherboards shipping with hardware AES disabled in the bios...)

Post reply on HN