r/ProgrammerHumor • • Jul 09 '17

All block cipher modes are beautiful

Post image
316 Upvotes

17 comments sorted by

78

u/TheSlimyDog Jul 09 '17

Upvoted for visibility so someone can tell me what the hell all of this means.

12

u/gandalfx Jul 09 '17

When you encrypt data (e.g. an image file) you treat it as a long stream of bytes (since that's just what it is). A "block cypher" encryption algorithm splits the data into sections of the same length ("blocks") and encrypts each one separately using a key. In the most simple form (ECB) every block is encrypted completely independently, using just the key. As a result identical blocks yield identical results ("cypher text"). Since the blocks themselves aren't that large (e.g. 16 bytes for AES) it can reveal the overall structure of the input even in the result (wikipedia has a good illustration of what that means for bitmaps). Other "modes" for block cyphers reuse the results of previously encrypted blocks and use it to further obfuscate the subsequent blocks. For example in CBC the previous block is simply XOR'd onto the current block before it is encrypted, resulting in ever changing, seemingly random cypher text.

0

u/[deleted] Jul 09 '17

[deleted]

15

u/sablefoxx Jul 09 '17

Well all modes encrypt the blocks with the same "key," where ECB fails is that there is no feedback/randomness per block other than the key. This means two equivalent plaintext blocks encrypted with the same key will produce identical ciphertext. The same thing happens when you re-use an IV under the same key in CBC mode since the feedback (the IV) doesn't change.

3

u/FallenWarrior2k Jul 09 '17 edited Jul 09 '17

That goes for anything tho, since otherwise you wouldn't be able to reverse the encryption. An algorithm that produces different results for the same inputs would be non-deterministic and thus useless for encryption.

Edit: I was referring to the CBC part. Obviously, I know that ECB is not viable

3

u/sablefoxx Jul 09 '17

Not sure what you mean by that, two identical plaintext blocks should always produce distinct outputs. This is achieved using deterministic algorithms and a per operation nonce (i.e. IV). When used securely any cipher mode should produce distinct outputs for each operation even if it's encrypting the same data. ECB does not have this property, which is way it's insecure.

2

u/FallenWarrior2k Jul 09 '17

I was referring to the CBC part, edited my original comment to show that

2

u/iconoclaus Jul 10 '17

a bigger problem is that inter-block patterns are more visible because information is not smeared across blocks. so encrypting an image could result in something like this where each block is encrypted but the larger pattern is still visible.

5

u/kooolk Jul 09 '17

No. Not every cipher mode is appropriate for any use case. Using "strong" cipher mode without understanding it (and how to handle its IV) is how you get weak crypto.

2

u/[deleted] Jul 09 '17

"All modes are beautiful not you and deserve to be used"

2

u/BohrMe Jul 09 '17

CBC should not be used either.

4

u/sablefoxx Jul 09 '17

CBC is a secure cipher mode (assuming you step over a fair number of pitfalls: e.g. IV reuse, no HMAC), it should only be avoided if better cipher modes such as GCM are easily used in its place.

4

u/awesomenesser Jul 09 '17

If you are talking about network traffic then yes you are correct. From what I have read CBC only breaks down when the intruder has access to the drive (for file encryption), and can test the system with their own blocks of data.
I write embedded firmware and CBC is still secure since no intruders will ever have access to the plaintext (they can't watch messages get encrypted). (Depending on the implementation they shouldn't even have access to decrypt their own messages) You do need to use a unique IV during encryption, to prevent patterns across versions.
I will note that I am not an expert and if someone has issue with what I said please let me know.

1

u/vamediah Jul 09 '17

CBC breaks down only due to an implementation error, which results in padding oracle attack. Thus in TLS, it's OK as all common implementation are correct.

For homebrewed code, this is precisely the reason why not choose CBC.

Also, never ever reuse IV - I think it should have been better called as nonce.

Reusing IV in CTR mode has catastrophic consequences - 2 XORs with the same data block => you get plaintext.

0

u/[deleted] Jul 09 '17

[deleted]

2

u/mpyne Jul 10 '17

Funny you should mention that proviso, since cryptographers have developed by now cipher modes that are at least resistant to nonce-reuse. Not necessarily something you want to jump on immediately, but there are things better than GCM coming down the pike.

0

u/[deleted] Jul 09 '17

[deleted]

4

u/Mamish Jul 10 '17

I'm not sure what bothers me more: that you'd consider RSA and AES as replacements for each other (when one's symmetric and the other isn't) or that you'd willing use ECB over any other choice.