How to detect block cipher mode
Asked Answered
P

4

17

How to detect if a message was crypt by CBC or ECB mode?

I have made a function who encrypt in AES 128 CBC or ECB randomly, and I do hamming between clear text and cipher text, but seams not correlated to cipher mode.

How can I detect the block cipher mode?

Thank you in advance

Propolis answered 29/6, 2013 at 16:41 Comment(3)
If it is only your code doing both encryption and decryption then you can add an extra byte e.g. 0 or 1 depending on cipher mode and check that before processing the payload. Other option you could try is first try cbc if that fails try ecb while decrypting.Ischium
I'm afraid that if there are no distinguishing elements in the plain text or cipher text that you cannot reliably detect the block cipher mode. The blocks are meant to be indistinguishable from random. Fortunately the plain text normally isn't. If you did use CBC mode then you should have used an IV, so the IV could be the distinguishing feature. That said; why the heck would you encrypt with CBC or ECB randomly???Pillion
It's on a cryptography exercice. The aim is to detect cypher mode, so I made a function who use randomly CBC and ECB to test my detection function.Propolis
O
11

The answer is pretty much given in the problem statement:

Remember that the problem with ECB is that it is stateless and deterministic; the same 16 byte plaintext block will always produce the same 16 byte cipher text.

Thus, with the assumption that some repeated plaintext blocks occur at the same ciphertext block offsets, we can simply go ahead and look for repeated ciphertext blocks of various lengths.

Olivares answered 21/12, 2013 at 21:35 Comment(1)
One more thing to note is that the key length of AES-ECB is 128 bits(16 bytes). Not knowing this (obvious to some) fact will make you run around in circles like I did!Pharos
R
2

I am doing the same problem set and just finished this problem (using clojure).

My first hint is, it will be more clear what you need to do if you are using a language which supports first class functions/lambdas.

Anyways, let's break down the problem a bit:

First, just write a function which validates that a blackbox is encrypting data with ecb. How would you do this?

It might look something like (pseudocode below)

function boolean isEcbBlackbox(func f) 
{   //what input can I use to determine this?
    result = f("chosen input")
    if(result ...) {//what property of result should I look for?
        true
    } else {
        false
    }
}

Remember, the key weakness of ECB is identical blocks of plaintext will be encrypted to identical blocks of ciphertext.

EDIT: The challenges are now public, so I will link to my solution(s):

https://github.com/dustinconrad/crypto-tutorial/blob/master/src/crypto_tutorial/lib/block.clj#L118

Remonstrance answered 3/7, 2013 at 20:40 Comment(8)
I now actually feel really stupid... I suppose the question never told you what type of text to encrypt, entering a certain type of text makes this exercise really really easy, I already have a function that will do this from when I did the detecting repeating xor exercise...Minyan
Yeah, that last sentence may be the only distinguishing feature you can use on the ciphertext of course.Pillion
From my understanding, in this challenge, the "blackbox function" is not available for evaluation with arbitrary input. Only selected, pre-computed ciphertexts are available. Thus, doing this sort of higher order function evaluation won't be of much use.Olivares
So I intentionally broke the problem down to a simpler sub-problem, to help him understand the underlying concepts. Once you grasp this concept, applying it to fixed input is a shorter leap. My feeling is you learn a lot less if you are spoon-fed the result than if you arrive at it yourself, even if the route is somewhat circuitous.Remonstrance
@Remonstrance if he's going through the challenges one by one, then he already implemented your "hint", as that is exactly one of the earlier challenges...Represent
The challenges are now public, so I have linked my solution in the answer.Remonstrance
@Remonstrance Are we supposed to decrypt the line after we identify it, or just identify it? The instructions say: "Detect it." and nothing else.Uniformity
@Uniformity this confused me too. But unless I am missing something, even if you could detect it, wouldn't you still need the key to decrypt it?Workmanship
D
0

compute block size based on cipher text % 16 or 24 or 32 which ever is == 0

hamming distance should be done by cipher block 1 with rest of the cipher blocks

if we average to per byte using floating point arithmatic, if the value is below certain threshold then it is ECB.

Darkling answered 2/2, 2018 at 3:50 Comment(0)
M
-2

I know the exact exercise you're doing, I'm currently doing it right now myself. I would recommend doing Frequency Analysis on the encrypted strings (don't forget the string might be base64'd or hex). If you get back a frequency distribution that matches the language of the string you encoded then it's safe to assume it's in ECB, otherwise it's probably CBC.

I don't know if this will actually work as I'm just doing the exercise now, but it's a start.

EDIT:

I rushed this answer a bit and feel I should explain more. If it's been encrypted in ECB mode then the frequency analysis should show a normal distribution style regardless of any padding to the start/end of the string and key used. Where as encryption in CBC mode should have a very random and probably flat distribution.

Minyan answered 2/7, 2013 at 9:26 Comment(8)
Ok, I understand. I have privoiously try somthing little bit different, I have try to annalyse the distribution of hamming results between each plain blocs and cipher blocs, but give no interesting results, strange because not irrelevant.Propolis
I don't think a frequency analysis on the encrypted string will get you much. AES completely transforms the content of the block. See en.wikipedia.org/wiki/…. The key weakness of CBC is identical blocks of plaintext will be encrypted to identical blocks of ciphertext.Remonstrance
After reflection, I'm not very sure that your proposition can works, because of 16 bytes block size. If 1 bit in an input block change, the cipher bloc will be totally different (same in CBC and ECB). So frequency analysis on bytes value frequency have no sense!? I'm right or wrong?Propolis
Sorry, I meant ECB, not CBC aboveRemonstrance
Yeah, forget my answer. Tried it, FA doesn't work. I have also tried doing chi-squared and monte-carlo to test for true randomness but as expected, with it being AES, it passed both of these tests too. I emailed today for some help on this challenge. I got back the following response: "If this wasn't clear, you can choose input to the oracle."... Needless to say it didn't help at all.Minyan
@GarryWelding, that is a useful hint.Remonstrance
Ho yes now I undersand, it's realy easy, I have made too complicated study, and the answer of the staff is very helpfull.Propolis
I think you might be on the right track but you want to convolve the cipher text against itself at a hamming level.Abduct

© 2022 - 2024 — McMap. All rights reserved.