This course will introduce you to the foundations of modern cryptography, with an eye toward practical applications.

Loading...

From the course by University of Maryland, College Park

Cryptography

348 ratings

University of Maryland, College Park

348 ratings

Course 3 of 5 in the Specialization Cybersecurity

This course will introduce you to the foundations of modern cryptography, with an eye toward practical applications.

From the lesson

Week 3

Private-Key Encryption

- Jonathan KatzProfessor, University of Maryland, and Director, Maryland Cybersecurity Center

Maryland Cybersecurity Center

[SOUND] In the last few lectures, we've been talking

Â about security against chosen ciphertext attacks.

Â And in our definition of security against chosen ciphertext attacks, we allow

Â the attacker to obtain the encryption of any cypher text of its choice.

Â Of course, besides the challenge cypher text.

Â And while this makes sense from a definitional point of view because we

Â want to capture any possible sort of chosen cyphertext attack,

Â it's still an unrealistic sort of a model, because in practice,

Â the attacker will not be given the full decryption of arbitrary ciphertext.

Â So what I want to show here is a real-world scenario where only one bit

Â about decrypted ciphertext is leaked to the attacker, but nevertheless,

Â this information can be exploited by an attacker to learn the entire plaintext.

Â And the point of this is to demonstrate the importance of

Â protecting yourself against chosen ciphertext attacks.

Â Because a scheme that was secure against chosen ciphertext attacks would in

Â particular be secure against the attack we're going to show here.

Â Now to describe the attack, I just need to remind you about CBC mode encryption.

Â So remember that in CBC mode encryption,

Â we have a message consisting of say t blocks of plain text.

Â And what we do to encrypt that, is we choose a random initialization vector, IV.

Â Output the IV as the first block of the ciphertext, and then chain

Â preceding blocks of cyphertext XORing them with the next block of the plain text.

Â And then feeding that into our block cipher to produce the next block of

Â ciphertext output.

Â Now in our description of CBC mode encryption,

Â we've been assuming until now that the plain text

Â is an integral multiple of the block length of the block cipher.

Â But in practice, that may not be the case and what we'd like to do is allow for

Â more flexibility and allow the sender to encrypt messages whose length is

Â not an integral multiple of the block length of the underlying cipher.

Â So in general,

Â what we can do to achieve this, is to take the message that the sender wants to send.

Â Encode it in some way, some non-cryptographic way,

Â that will allow efficient reconstruction of the original message, and

Â then encrypt the encoded data using CBC mode encryption to yield the ciphertext.

Â Now in the description I'm going to present here, we're going to assume for

Â simplicity that the message is an integral number of bytes.

Â But it will not necessarily be an integral multiple of the block length

Â of the cipher.

Â And one particular encoding scheme we're going to look at is

Â called PKCS #5 encoding.

Â And what you do in PKCS #5 encoding is the following.

Â So let's let L denote the block length in bytes of our block cipher.

Â So, if we look at the message we've been given, we can determine from the length of

Â the message, how many bytes of padding we need to append to the message

Â in order to get something which is an integral multiple of the block length.

Â And let's let B denote the number of bytes that we need to append to the message

Â in order to get something, whose length is a multiple of L, the block link.

Â I'll note here that B, the number of bytes of padding we need,

Â is going to be strictly between 1 and L, and note that we cannot have B equal to 0.

Â That is we cannot have a case where we don't pad anything.

Â And that might seem a little bit strange,

Â because what if our message actually is an integral multiple of the block length?

Â Well the point is that we need the receiver to be able to

Â unambiguously recover the original message.

Â And if, and if there are cases in which we don't add any padding at all,

Â then that will be impossible.

Â So if it turns out that our message is indeed an integral multiple of the block

Â length, what we end up doing in that case is actually appending an additional L

Â bytes of padding to the end of that message before encrypting it.

Â So what we do then is we take b, the number of bytes that we need to append,

Â and we append that value and code it in one byte, a total of b times.

Â So we're appending indeed b bytes of encoding,

Â which consist of the value b repeated b times.

Â So for example, if we need to append three bytes of padding to our message, then what

Â we do is we append the three bytes, 030303, here written in hexadecimal.

Â To decrypt, the receiver will use CBC-mode decryption to obtain the encoded data.

Â And then they need to recover from the encoded data the original message.

Â And they do this in the following way.

Â So say the final byte of the encoded data has value b.

Â Well, if b is equal to 0, or if b is greater than L, then there's an error.

Â No properly encoded message can have a final byte.

Â Which is either 0 or greater than L.

Â So in that case, the receiver will return an error.

Â Otherwise, what the receiver does is to look at the final b

Â bytes of the encoded data.

Â And these should all be equal to b.

Â Again, any properly encoded message will be of that form.

Â So if the final b bytes of encoded data are not all equal to b,

Â then the receiver again returns an error.

Â And otherwise, what it does is simply strip off the final b bytes of

Â the encoded data and

Â output the initial portion of the message whatever's left as the message itself.

Â So here's just an example.

Â Here we have L equals 8, and

Â we have a 6 byte message that the sender wants to encrypt.

Â So what they'll do is they'll encode their message by appending 2 bytes.

Â And those will of course both have value 2.

Â It will then encrypt that and send it to the receiver.

Â And the receiver will decrypt to obtain the same encoded message.

Â The receiver looks at the final byte, which has value 2.

Â And then strips off the final 2 bytes of padding,

Â which were both indeed equal to 2, to recover the original 6 byte message.

Â Now, the attacker can potentially exploit the behavior of

Â the receiver to learn information about a ciphertext.

Â And this will be done in a sort of a chosen ciphertext attack, but

Â of a limited form.

Â So here we assume that the attacker has intercepted some cyphertext c,

Â which was generated by some honest sender in the appropriate way, i.e.,

Â by padding it appropriately and then encrypting it.

Â And what the attacker can then do, is to construct modified cyphertext,

Â say c prime, send that to the receiver as if it were coming from the honest sender.

Â The receiver will decrypt that to obtain some encoded data,

Â and then try to recover a message from that encoded data.

Â I would point the receiver will either return an error or not, i.e.,

Â either the encoded data was properly formatted, and then there's no error.

Â Or, as on the previous slide, there will be some error in the processing and

Â the receiver will return an error message.

Â We'll call this a padding oracle.

Â So that is we have imagine that the attacker will interact with this receiver

Â i.e this oracle, that takes cyphertext as input, decrypts them and

Â then checks the encoding and outputs a 0 or 1, depending on whether or

Â not the encoded data is correctly formatted.

Â Now this may seem like something that would never happen in real life.

Â However, padding oracles are frequently present in web applications.

Â And if you think about it, this makes sense.

Â Because when the receiver gets a ciphertext, and decrypts it and

Â gets some encoded data which is not properly formatted, in general,

Â it really does need to inform Defender that something went wrong.

Â Maybe the message, or maybe the ciphertext was garbled in transit.

Â Maybe there was a network level error.

Â But either way, there has to be some way for

Â the receiver to recover from that event.

Â And even if the receiver does not explicitly return an error

Â message in the case of improper encoding.

Â An attacker is still might be able to detect whether or

Â not an error occurred based on differences in the timing of what the receiver does or

Â in the behavior of the receiver overall, i.e if the receiver

Â terminates the connection, then that's indicative of an error.

Â And even any kind of change like that can be enough to effectively give

Â the attacker access to a padding oracle.

Â Now let me go through the main idea of the attack, again,

Â assuming that the attacker has access to this padding oracle.

Â For simplicity, let's assume we have a two-block ciphertext

Â throughout the node by IV, c.

Â So IV is the initial block of ciphertext, and c is the next block.

Â So this means we have a one block encoded data.

Â And therefore a, a, a plain text, which is strictly smaller than one block.

Â So, the encoded data is then equal to Fk inverse of c, XOR with IV.

Â This is simply the, the decryption operation when running CBC mode.

Â This is what the receiver will do to obtain the encoded data.

Â Now the attacker doesn't know k,

Â and the attacker of course doesn't know Fk inverse of c.

Â The point is that it does know that this is exactly how the receiver will be

Â computing the encoded data.

Â And the main observation here,

Â is that if the attacker modifies the ith byte of the IV,

Â this will cause a predictable change only to the ith byte of the encoded data.

Â Right, so if c remains unchanged and

Â the attacker modifies the ith byte of the IV, then Fk inverse remains unchanged.

Â And then when you're, when that gets XORed with IV,

Â the result will be effected only in the ith byte of the result.

Â So here I've tried to draw the picture from the point of view of the attacker.

Â We have Fk inverse of c,

Â which consists of a bunch of bytes which are unknown to the attacker.

Â And the attacker knows that those are going to be XORed with the IV,

Â which is known to the attacker, to give some encoded data,

Â which at this point in time is completely unknown to the attacker.

Â The attacker does know, however,

Â that the encoded data that results from this is properly formatted.

Â And that's because

Â the sender properly formatted the message before encrypting it.

Â So the attacker knows that if it forwards IV,c to the receiver,

Â the receiver will decrypt, the padding will be fine, and there will be no error.

Â What the attacker can now do, is try modifying the first byte of the IV.

Â This will result in a modification only to the first byte of the encoded data,

Â as we said before.

Â The attacker can forward the resulting ciphertext with this

Â modified first byte of IV.

Â And check whether or not this yields an error or not.

Â And let's assume that in this case there's no error, so

Â it does get successfully decrypted.

Â Or the attacker can then modify the second byte of the IV and

Â check whether it's successfully decrypted.

Â And then the third byte of the IV and so on.

Â Let's assume that after modifying the third byte of the IV,

Â the receiver returns an error.

Â So this means that at this point in time, when a third bit of the IV has been

Â modified, suddenly the encoded data is no longer properly formatted.

Â What does that tell the attacker?

Â Well, what that tells the attacker is that the receiver is checking the final 6

Â bytes of the encoded data to check whether the encoding was done properly, right?

Â And that's because when the attacker had only modified the second byte of the IV,

Â decryption succeeded, but when it modified the 3 byte, decryption suddenly failed.

Â That tells the attacker that exactly the final 6 bytes of

Â encoded data are being checked.

Â And that means in turn,

Â that the final 6 bytes of the original encoded data were all equal to 6.

Â So if we go back to the original situation with the original IV, the attacker has

Â now learned that the final 6 bytes of encoded data were all equal to 6.

Â But the remaining 2 bytes of encoded data,

Â which are the original message, are still unknown.

Â But let's see what the attacker can do next.

Â What they can do is they can modify, say the right-most byte of the IV,

Â in a particular way to cause a predictable change in the right-most byte of

Â the encoded data.

Â So the right most byte of the IV started out being equal to 9E.

Â If the attacker modified that to a value which is equal to 9E XORed with 6,

Â XORed with 7, then what it knows is that

Â that will result in a final byte of encoded data being equal to 7.

Â And it can do something similar with the second to right most byte of the IV

Â in order to cause the second to right most byte of the encoded data to come out

Â equal to 7.

Â And so on for the final 6 bytes of the IV and the 6 bytes of the encoded data.

Â Now if the attacker sends this to the padding oracle,

Â most likely decryption will fail because it's, it's unlikely in general that

Â the second byte of the encoded data will happen to be equal to 7.

Â But what the attacker can do now is simply try all possibilities for

Â the second byte of the IV.

Â So it can start out by setting the second byte of the IV to 0, and

Â check to see whether or not the resulting cyphertext decrypts correctly or not.

Â It can then try this with the byte having value 1, 2, et cetera, until

Â eventually it reaches a value for the second byte, at which decryption succeeds.

Â And when that happens, the attacker then knows that the second byte of

Â the encoded data, at that point, must be equal to the value 7.

Â Because that's the only way decryption will succeed.

Â And now we're done.

Â The attacker has learned the second byte of encoded data.

Â Why is that?

Â Because the attacker now knows that xx, that is the value of the second byte

Â of Fk inverse of c, XORed with 41 in this case, is equal to the value 7.

Â So now we can determine what xx XORed with 1 is equal to.

Â So remember 1 was the actual value in the cyphertext in

Â the second byte of the cyphertext that the attacker had intercepted.

Â And so we can figure out just through some basic math that the second byte

Â of the original encoded data transmitted by the sender was equal to 47.

Â That just comes out from doing the math.

Â And it can repeat the process now with the first byte of the IV,

Â in order to learn the first byte of the encoded data.

Â What's the complexity of this attack?

Â Well, it takes at most L tries for

Â the attacker to learn the number of padding bytes.

Â This is because it's just going through the bytes of the IV one by one.

Â For at most L bytes until decryption fails, at which point the attacker learns

Â exactly how much padding there is and what those padding bytes are.

Â And then for each byte of the original plaintext message,

Â it takes at most 256 tries to cycle through all possibilities.

Â All possible bytes of that position of the IV.

Â And at that point, the attacker was guaranteed to learn the value of

Â the original plaintext at that byte.

Â So it takes 256 tries per byte, so 256 times the number of bytes in

Â the original message, and the attacker learns the entire original message.

Â Now that might sound like a lot, but in fact if the receiver were not doing any

Â sort of throttling to prevent decrypting cyphertext, and, and, and

Â continuing to get errors like that, then it wouldn't actually take very long in

Â terms of time for the attacker to run 256 tries with the receiver.

Â This could be done on the order of seconds.

Â So what I've tried to show in this lecture,

Â is an example where chosen-cyphertext attacks are a significant

Â real-world threat on a deployed use of cryptography.

Â And I'll highlight again that these kind of attacks have been found to exist in

Â the real world.

Â And have, in fact, been shown to be exploitable in the real world.

Â So this is not just a theoretical sort of a threat.

Â But I think it also demonstrates what kind of things can be done with chosen

Â ciphertext attacks, and shows more generally why such

Â attacks are problematic, and why you would want to defend against them.

Â And for this reason, modern encryption schemes are generally designed to be

Â CCA-secure in order to rule out any such attacks,

Â in particular the padding oracle type attacks like we've shown here.

Â Now, we're not going to see an example of a CCA-secure scheme now.

Â However, we will see an example at the end of next week.

Â I'll see you next time.

Â Coursera provides universal access to the worldâ€™s best education,
partnering with top universities and organizations to offer courses online.