< Return to Video

Modes of operation: many time key (CBC) (16 min)

  • 0:00 - 0:04
    Now that we understand chosen plaintext
    security, let's build encryption schemes
  • 0:04 - 0:09
    that are chosen plaintext secure. And the
    first such encryption scheme is going to
  • 0:09 - 0:13
    be called cipher bock chaining. So here
    is how cipher block chaining works.
  • 0:13 - 0:17
    Cipher block chaining is a way of using a
    block cipher to get chosen plaintext
  • 0:17 - 0:21
    security. In particular, we are going to
    look at a mode called cipher block chaining
  • 0:21 - 0:25
    with a random IV. CBC stands for cipher
    block chaning. So suppose we have a block
  • 0:25 - 0:29
    cipher, so EB is a block cipher. So now
    let's define CBC to be the following
  • 0:29 - 0:33
    encryption scheme. So the encryption
    algorithm when it's asked to encrypt a
  • 0:33 - 0:38
    message m, the first thing it's going to do
    is it's going to choose a random IV that's
  • 0:38 - 0:42
    exactly one block of the block
    cipher. So IV is one cypher block.
  • 0:42 - 0:46
    So in the case of AES the IV
    would be 16 bytes. And then we're
  • 0:46 - 0:51
    gonna run through the algorithm here, the
    IV basically that we chose is gonna be XORed
  • 0:51 - 0:55
    to the first plain text
    block. And then the result is gonna be
  • 0:55 - 0:59
    encrypted using the block cipher and
    output of the first block of the ciphertext.
  • 0:59 - 1:03
    And now comes the chaining part
    where we actually use the first block of
  • 1:03 - 1:07
    the ciphertext to kind of mask the second
    block of the plaintext. So we XOR
  • 1:07 - 1:12
    the two together and the encryption of
    that becomes the second ciphertext block.
  • 1:12 - 1:16
    And so on, and so on, and so forth. So
    this is cIpher block chaining, you can
  • 1:18 - 1:20
    see that each cIpher block is chained and
    XORed into the next plaintext
  • 1:20 - 1:24
    block, and the final ciphertext is going to
    be essentially the IV, the initial IV
  • 1:24 - 1:30
    that we chose along with all the ciphertext blocks. I should say that IV stands
  • 1:30 - 1:36
    for Initialization Vector. And we're going to
    be seeing that term used quite a bit,
  • 1:36 - 1:40
    every time we need to pick something at
    random at the beginning of the encryption
  • 1:40 - 1:44
    scheme typically we'll call that an IV
    for initialization vector. So you notice
  • 1:44 - 1:47
    that the cIphertext is a little bit
    longer than the plain text because we had
  • 1:47 - 1:51
    to include this IV in the cIphertexts
    which basically captures the randomness
  • 1:51 - 1:55
    that was used during encryption. So the
    first question is how do we decrypt the
  • 1:55 - 2:00
    results of CBC encryption, and so
    let me remind you again that if when we
  • 2:00 - 2:04
    encrypt the first message block we
    XOR it with the IV, encrypt the
  • 2:04 - 2:09
    result and that becomes the first ciphertext
    block. So let me ask you how would
  • 2:09 - 2:14
    you decrypt that? So given the first
    ciphertext block, how would you recover
  • 2:14 - 2:18
    the original first plaintext block? So
    decryption is actually very similar to
  • 2:18 - 2:22
    encryption, here I wrote down the
    decryption circuit, you can see basically
  • 2:22 - 2:26
    it's almost the same thing except the XOR
    is on the bottom, instead of on the top, and
  • 2:26 - 2:30
    again you realize that essentially we
    chopped off the IV as part of the
  • 2:30 - 2:34
    decryption process and we only output the
    original message back, the IV is dropped
  • 2:34 - 2:38
    by the decryption algorithm. Okay, so the
    following theorem is going to show that in
  • 2:38 - 2:44
    fact CBC mode encryption with a random IV
    is in fact semantically secure under a
  • 2:44 - 2:49
    chosen plaintext attack, and so let's
    take that more precisely, basically if we
  • 2:49 - 2:54
    start with a PRP, in other words, our
    block cipher E, that is defined over a
  • 2:54 - 2:59
    space X, then we are gonna to end up with
    a encryption algorithm Ecbc that takes
  • 2:59 - 3:04
    messages of length L and outputs
    ciphertexts of length L+1. And then
  • 3:04 - 3:09
    suppose we have an adversary that makes q
    chosen plaintext queries. Then we can
  • 3:09 - 3:15
    state the following security fact, that
    for every such adversary that's attacking
  • 3:15 - 3:20
    Ecbc, to exist an adversary that's
    attacking the PRP, the block cipher, with
  • 3:20 - 3:25
    the following relation between the two
    algorithms, in other words, the advantage
  • 3:25 - 3:30
    of algorithm A against the encryption scheme
    is less than the advantage of algorithm B
  • 3:30 - 3:35
    against the original PRP plus some noise
    term. So let me interpret this theorem for
  • 3:35 - 3:40
    you as usual, so what this means is that
    essentially since E is a secure PRP this
  • 3:40 - 3:45
    quantity here is negligible, and our goal
    is to say that adversary A's advantage is
  • 3:45 - 3:50
    also negligible. However, here we are
    prevented from saying that because we got
  • 3:50 - 3:55
    this extra error term. This is often
    called an error term and to argue that CBC
  • 3:55 - 4:00
    is secure we have to make sure that the
    error term is also negligible. Because if
  • 4:00 - 4:04
    both of these terms on the right are
    negligible, there sum is negligible and
  • 4:04 - 4:09
    therefore the advantage of A against Ecbc
    would also be negligible. So this says
  • 4:09 - 4:15
    that in fact for Ecbc to be secure it has better
    be the case that q squared L squared Is
  • 4:15 - 4:20
    much, much, much smaller than the value X,
    so let me remind you what q and L are, so
  • 4:20 - 4:25
    L is simply the length of the messages
    that we're encrypting. Okay, so L could be
  • 4:25 - 4:30
    like say a 1000, which means that we are
    encrypting messages that are at most 1000
  • 4:30 - 4:35
    AES blocks. q is the number of ciphertexts
    that the adversary gets to see under the
  • 4:35 - 4:41
    CPA attack, but in real life what q is, is
    basically the number of times that we have
  • 4:41 - 4:46
    used the key K to encrypt messages, in other
    words if we use a particular AES key to
  • 4:46 - 4:51
    encrypt 100 messages, Q would be 100.
    It is because the adversary would then see
  • 4:51 - 4:56
    at most 100 messages encrypted under this key K. Okay
    so lets see what this means in the real
  • 4:56 - 5:01
    world. So here I've rewrote the error
    balance from the theorem. And just to remind
  • 5:01 - 5:05
    you to use the messages encrypted with K
    and L with the lengths of the messages and so
  • 5:05 - 5:09
    suppose we want the adversary's advantage
    to be less than one over two to the thirty
  • 5:09 - 5:13
    two. This means that the error term had
    better be less than one over two to the
  • 5:13 - 5:18
    thirty two. Okay, so let's look at AES and see
    what this mean. For AES, AES of course uses
  • 5:18 - 5:22
    128 bit blocks, so X is going to be two
    to the 128, the
  • 5:22 - 5:26
    size of X is going to be 2 to the
    128, and if you
  • 5:26 - 5:31
    plug this into the expression you see that
    basically the product q times L had
  • 5:31 - 5:35
    better be less than two to the forty eight.
    This means that after we use a particular
  • 5:35 - 5:40
    key to encrypt 2 to the 48 AES
    blocks we have to change the key. Okay, so
  • 5:40 - 5:47
    essentially CBC stops being secure after
    the key is used to encrypt 2 to the 48 different AES blocks.
  • 5:47 - 5:50
    So its
    kinda nice that the security theorem tells
  • 5:50 - 5:54
    you exactly how long the key can be used
    and then how frequently, essentially, you have to
  • 5:54 - 6:00
    replace the key. Now interestingly if you
    apply the same analogy to the 3DES it
  • 6:00 - 6:05
    actually has a much shorter block, maybe
    only 64 bits, you see the key has to be
  • 6:05 - 6:10
    changed much more frequently, maybe after every
    65 thousand DES blocks, essentially you need to generate a new key. So
  • 6:10 - 6:15
    this is one of the reasons why AES has a
    larger block size so that in fact modes
  • 6:15 - 6:20
    like CBC would be more secure and one can
    use the keys for a longer period of time, before having
  • 6:20 - 6:25
    to replace it. What this means is having
    to replace two to the sixteen blocks,
  • 6:25 - 6:30
    each block of course is 8 bytes, so
    after you encrypt about half a megabyte of
  • 6:30 - 6:34
    data you would have to change the DES key
    which is actually quite low. And you
  • 6:34 - 6:38
    notice with AES you can encrypt quite a
    bit more data before you have to change the
  • 6:38 - 6:43
    key. So I want to warn you about a very
    common mistake that people have made when
  • 6:43 - 6:48
    using CBC with a random IV. That is that
    the minute that the attacker can predict
  • 6:48 - 6:53
    the IV that you're going to be using for
    encrypting a particular message decipher
  • 6:53 - 6:58
    this Ecbc is no longer CPA secure. So when
    using CBC with a random IV like we've
  • 6:58 - 7:02
    just shown It's crucial that the IV is not
    predictable. But lets see an attack. So
  • 7:02 - 7:06
    suppose it so happens that given a
    particular encryption in a message that
  • 7:06 - 7:11
    attacker can actually predict that IV that
    will be used for the next message. Well
  • 7:11 - 7:15
    let's show that in fact the resulting
    system is not CPA secure. So the first thing the
  • 7:15 - 7:19
    adversary is going to do is, he is going
    to ask for the encryption of a one block
  • 7:19 - 7:23
    message. In particular that one block is
    going to be zero. So what the adversary
  • 7:23 - 7:28
    gets back is the encryption of one
    block, which namely is the encryption of
  • 7:28 - 7:32
    the message namely zero, XOR the IV. Okay
    and of course the adversary also gets the
  • 7:32 - 7:36
    IV. Okay so now the adversary by
    assumption can predict the IV that's gonna
  • 7:36 - 7:40
    be used for the next encryption. Okay so
    let's say that IV is called, well IV. So
  • 7:40 - 7:44
    next the adversary is going to issue his
    semantic security challenge and the
  • 7:44 - 7:49
    message m0 is going to be the predicted IV
    XOR IV1 which was used in the encryption
  • 7:49 - 7:54
    of c1. And the, the message of m1 is just
    going to be some other message, it doesn't
  • 7:54 - 7:58
    really matter what it is. So now let's see
    what happens when the adversary receives
  • 7:58 - 8:02
    the result of the semantic security
    challenge. Well, he is going to get the
  • 8:02 - 8:06
    encryption of m0 or m1. So when the
    adversary receives the encryption of m0,
  • 8:06 - 8:11
    tell me what is the actual plain text
    that is encrypted in the ciphertext c?
  • 8:11 - 8:17
    Well so the answer is that what is
    actually encrypted is the message which is
  • 8:17 - 8:23
    IV XOR IV1 XOR the IV that's used to
    encrypt that message which happens to be
  • 8:23 - 8:28
    IV and this of course is IV1. So when the
    adversary receives the encryption of m0,
  • 8:28 - 8:33
    he is actually receiving the block cipher
    encryption of IV1. And lo and behold,
  • 8:33 - 8:38
    you'll notice that he already has that
    value from his chosen plaintext query.
  • 8:38 - 8:43
    And then, when he is receiving the
    encryption of message m1, he just received
  • 8:43 - 8:48
    a normal CBC encryption of the message m1.
    So you realize that now he has a simple
  • 8:48 - 8:53
    way of breaking the scheme, namely what
    he'll do is he'll say, he's gonna ask, "Is the second
  • 8:53 - 8:58
    block of the ciphertext c equal to the
    value that I received in my CPA query?" If
  • 8:58 - 9:04
    so I'll say that I received the encryption
    of m0, otherwise I'll say that I received
  • 9:04 - 9:09
    the encryption of m1. So really his test
    is c1 he refers to the second block
  • 9:09 - 9:14
    of c and c11 refers to the second block of
    c1, if the two are equal he says zero,
  • 9:14 - 9:20
    otherwise he says one. So the advantage of
    this adversary is going to be 1 and as a
  • 9:20 - 9:26
    result, he completely breaks CPA security
    of this CBC encryption. So the lesson here
  • 9:26 - 9:30
    is, if the IV is predictable then, in
    fact, there is no CPA security and
  • 9:30 - 9:36
    unfortunately, this is actually a very
    common mistake in practice. In particular
  • 9:36 - 9:41
    even in SSL protocol and in TLS 1.1, it turns
    out that IV for record number I is in fact
  • 9:41 - 9:46
    the last ciphertext block of record I-1. That means that exactly given
  • 9:46 - 9:52
    the encryption of record I-1, the
    adversary knows exactly what IV is going
  • 9:52 - 9:56
    to be used as record number I. Very
    recently, just last summer, this was
  • 9:56 - 10:01
    actually converted into a pretty
    devastating attack on SSL. We'll describe
  • 10:01 - 10:06
    that attack once we talk about SSL in more
    detail, but for now I wanted to make sure
  • 10:06 - 10:12
    you understand than when you use CBC encryption,
    its absolutely crucial that the IV be random.
  • 10:12 - 10:16
    Okay, so now I going to show you the nonce based version of CBC encryption
  • 10:16 - 10:21
    So in this mode the IV is replaced by non random but unique nonce
  • 10:21 - 10:24
    for example the numbers 1,2,3,4,5, could all be used as a nonce, and now, the appeal of this mode
  • 10:24 - 10:25
    is that if the recipient actually knows
    what the nonce is supposed to be
  • 10:25 - 10:26
    then there's no reason to include the nonce
    in the ciphertext, in which case, the ciphertext
  • 10:26 - 10:26
    is exactly the same length as the plaintext,
    unlike CBC with the random IV,
  • 10:26 - 10:26
    where we had to expand the ciphertext to include the IV, here, if the nonce is already known to the recipient,
  • 10:26 - 10:26
    there's no reason to include it in the ciphertext, and
    the ciphertext is exactly the same length as the plaintext.
  • 10:26 - 10:26
    So it's perfectly fine to use a non-random but unique nonce. However, it's absolutely crucial to know that,
  • 10:26 - 10:26
    if you do this, there's one more step that you have
    to do before you use the nonce in the CBC chain.
  • 10:26 - 10:26
    In particular, in this mode now we're going to
    be using two independent keys, k and k1.
  • 10:26 - 10:26
    The key k is, as before, going to be used to
    encrypt the individual message blocks,
  • 10:26 - 10:26
    However, this key k1 is going to be used to
    encrypt the non-random but unique nonce,
  • 10:26 - 10:26
    so that the output is going to be a random IV,
    which is then used in the CBC chain.
  • 10:26 - 10:27
    So this extra step here, encrypting the nonce
    with the key k1, is absolutely crucial.
  • 10:27 - 10:27
    Without it, CBC mode encryption would not be secure.
  • 10:27 - 10:27

    However it if is going to be a counter you
  • 10:27 - 10:32
    need to do one more step. Before actually
    encryption CBC and in particular you have
  • 10:32 - 10:37
    to actually encrypt the notes to obtain
    the IV that will actually be used for
  • 10:37 - 10:43
    encryption. The notes on CBC is similar to
    a random IV, the difference is that the
  • 10:43 - 10:48
    notes is first encrypted and the results
    is that the IV is used in the CBC
  • 10:48 - 10:53
    encryption Now the beauty of this mode is
    that the Nance doesn't necessarily have to
  • 10:53 - 10:57
    be included in the cipher text. It only
    needs to be in there if its unknowns are
  • 10:57 - 11:01
    the decrypter but it if the decrypter
    happens to already know the value of the
  • 11:01 - 11:05
    counter by some other means then in fact
    the cipher text is only as big as the
  • 11:05 - 11:09
    plain text. There's no extra value
    transmitted in the cipher text. And again,
  • 11:09 - 11:14
    I warn that when you're using non spaced
    encryption, it's absolutely crucial that
  • 11:14 - 11:18
    the key common Nance spare is only used
    for one message so for every message,
  • 11:18 - 11:22
    either the Nance has changed or the key
    has changed. Okay, so here emphasize the
  • 11:22 - 11:26
    fact that you need to do this extra
    encryption step before actual using the
  • 11:26 - 11:31
    Nance. This is very common mistake that
    actually forgotten in practice and for
  • 11:31 - 11:36
    example in TLS, this was not done and as a
    result there was a significant attack
  • 11:36 - 11:40
    against CBC encryption in TLS. Remember
    the reason that this is so important to
  • 11:40 - 11:45
    know is that in fact many crypto APIs are
    set up to almost deliberately mislead the
  • 11:45 - 11:49
    user to using CBC incorrectly. So let's
    look to see how CBC implemented inside of
  • 11:49 - 11:54
    open SSL. So here are the arguments of the
    function. Basically this is the plain
  • 11:54 - 11:58
    text, this is the place where the cipher
    text will get written to. This is the
  • 11:58 - 12:03
    length of the plain text. This is a, a Yes
    key Finally there is an argument here that
  • 12:03 - 12:06
    says whether you are crypting or
    decrypting. And the most important
  • 12:06 - 12:11
    parameter that I wanted to point out here
    is the actual IV and unfortunately, the
  • 12:11 - 12:15
    user is asked to supply this IV and the
    function uses the IV directly in the CBC
  • 12:15 - 12:20
    encryption mechanism. It doesn't encrypt
    the IV before using it and as a result, if
  • 12:20 - 12:24
    you ever call this function using a non
    random IV, the resulting encryption system
  • 12:24 - 12:29
    won't be CPA secure. Okay, so it's very
    important to know that when calling
  • 12:29 - 12:34
    functions like this. Cbc encryption or
    open SSL either supply a truly random IV
  • 12:34 - 12:39
    or if you want the IV to be a counter than
    you have to encrypt a counter using AAS
  • 12:39 - 12:44
    before you actually call a CBC encrypt and
    you have to that yourself. So again, it's
  • 12:44 - 12:48
    very important that the programmer knows
    that it needs to be done otherwise the CBC
  • 12:48 - 12:52
    encryption is insecure. One last
    technicality about CBC is what to do when
  • 12:52 - 12:57
    the message is not a multiple of the block
    cipher block length? That is what do we do
  • 12:57 - 13:02
    if the last message block is shorter than
    the block length of AES, for example? So
  • 13:02 - 13:06
    the last message block is less than
    sixteen bytes. And the answer is if we add
  • 13:06 - 13:12
    a pad to the last block so it becomes as
    long as sixteen bytes, as long as the AES
  • 13:12 - 13:17
    block size. And this pad, of course, if
    going to be removed during encryption. So
  • 13:17 - 13:22
    here is a typical path, this is the path
    that is used in TLS. Basically a pad with
  • 13:22 - 13:27
    N bytes then essentially what you do is
    you write the number N, N times. So for
  • 13:27 - 13:32
    example if you pad with five bytes, you
    pad with the string 555555. So five bytes
  • 13:32 - 13:37
    where each byte is the value five. And the
    key thing about this pad is basically when
  • 13:37 - 13:42
    the decrypter receives the message, what
    he does is he looks at the last byte of
  • 13:42 - 13:47
    the last block. So suppose that value is
    five, then he simply removes the last five
  • 13:47 - 13:52
    bytes of the message. Now the question is
    what do we do if in fact the message is a
  • 13:52 - 13:56
    multiple of sixteen bytes so in fact no
    pad is needed? If we don't pad at all,
  • 13:56 - 14:00
    well that's a problem because the
    decrypter is going to look at the very
  • 14:00 - 14:05
    last byte of the last block which is not
    part of the actual message and he's going
  • 14:05 - 14:10
    to remove that many bytes from the plain
    text. So that actually would be a problem.
  • 14:10 - 14:15
    So the solution is, if in fact there is no
    pad that's needed, nevertheless we still
  • 14:15 - 14:21
    have to add a dummy block. And since we
    add the dummy block this would be a block
  • 14:21 - 14:26
    that's basically contains sixteen bytes
    each one containing the number sixteen.
  • 14:26 - 14:30
    Okay, so we add essentially sixteen dummy
    blocks. The decrypter, that when he's
  • 14:30 - 14:34
    decrypting, he looks at the last byte of
    the last block, he sees that the value is
  • 14:34 - 14:39
    sixteen, therefore he removes the entire
    block. And whatever's left is the actual
  • 14:39 - 14:43
    plain text. So it's a bit unfortunate that
    in fact if you're encrypting short
  • 14:43 - 14:47
    messages with CBC and the messages happen
    to be, say, 32 bytes, so they are a
  • 14:47 - 14:51
    multiple of sixteen bytes, then you have
    to add one more block and make all these
  • 14:51 - 14:55
    ciphertexts be 48 bytes just to
    accommodate the CBC padding. I should
  • 14:55 - 15:00
    mention there's a variant of CBC called
    CBC with ciphertext stealing that actually
  • 15:00 - 15:04
    avoids this problem, but I'm not gonna
    describe that here. If you're interested
  • 15:04 - 15:08
    you can look that up online. Okay, so
    that's the end of our discussion of CBC
  • 15:08 - 15:12
    and in the next segment we'll see how to
    use counter modes to encrypt multiple
  • 15:12 - 15:14
    messages using a single key.
Title:
Modes of operation: many time key (CBC) (16 min)
Video Language:
English

English subtitles

Revisions