< Return to Video

Ruby Conf 2013 - Being Boring: A Survival Guide to Ruby Cryptography

  • 0:17 - 0:19
    TONY ARCIERI: We ready to go here?
  • 0:20 - 0:21
    So it used to be you didn't have to
  • 0:21 - 0:25
    think about security when you're writing a
    Ruby program.
  • 0:25 - 0:28
    Well, guess what, pall? Times have changed!
    There's all
  • 0:28 - 0:31
    sorts of bad people out there, and you gotta
  • 0:31 - 0:33
    know how to stop them!
  • 0:33 - 0:38
    All right. I'm not gonna do the entire talk
  • 0:38 - 0:40
    as Crocket impressions. Sorry.
  • 0:40 - 0:44
    So I am Tony Arcieri, or @bascule on Twitter.
  • 0:44 - 0:47
    I work on the, or work at the inform-
  • 0:47 - 0:51
    I work at Square on the information security
    team.
  • 0:51 - 0:55
    And my talk today is about cryptography and,
    you
  • 0:55 - 0:58
    know, I just want to encrypt something. How
    hard
  • 0:58 - 1:01
    could it possibly be?
  • 1:01 - 1:05
    The answer to this is definitely hard. So
    how
  • 1:05 - 1:07
    hard is it? Well, today you're gonna drink
    from
  • 1:07 - 1:11
    the fire hose, and I'm gonna show you exactly
  • 1:11 - 1:12
    how hard it is.
  • 1:12 - 1:18
    So quick itinerary here. We're gonna talk
    about attacks.
  • 1:18 - 1:20
    We're going to talk about how to defeat them
  • 1:20 - 1:25
    using something called authenticated encryption.
    And then we're going
  • 1:25 - 1:27
    to learn how to completely avoid all these
    problems
  • 1:27 - 1:31
    by just letting cryptographers do all this
    stuff for
  • 1:31 - 1:31
    us.
  • 1:31 - 1:35
    So, I think Ruby's traditionally been in a
    pretty
  • 1:35 - 1:38
    bad situation when it comes to cryptography,
    and the
  • 1:38 - 1:42
    main reason for this is the OpenSSL library
    has
  • 1:42 - 1:44
    been the only game in town for quite some
  • 1:44 - 1:45
    time.
  • 1:45 - 1:49
    So I think we can change this. I think
  • 1:49 - 1:52
    this is a fixable problem. But to do that,
  • 1:52 - 1:55
    we really need to know, how the hell does
  • 1:55 - 1:57
    crypto actually work?
  • 1:57 - 2:02
    So, the answer to this is magic. But, not
  • 2:02 - 2:07
    really magic. It's actually just math. So
    there's two
  • 2:07 - 2:11
    types of encryption cyphers I'm gonna talk
    about today.
  • 2:11 - 2:14
    We'll show how one builds on the other. The
  • 2:14 - 2:16
    first is called Symmetric. So here we have
    Alice
  • 2:16 - 2:19
    who just wants to send a message to herself,
  • 2:19 - 2:23
    or rather, just put this message away in a
  • 2:23 - 2:26
    box and lock it, and then she can use
  • 2:26 - 2:28
    the key to open it again and get the
  • 2:28 - 2:30
    original message out.
  • 2:30 - 2:33
    The other is asymmetric. So Alice wants to
    send
  • 2:33 - 2:36
    a message to Bob. Same idea here. She's gonna
  • 2:36 - 2:37
    put it in a box and lock it with
  • 2:37 - 2:40
    one key, and then Bob magically has another
    key
  • 2:40 - 2:44
    that lets him get the message out.
  • 2:44 - 2:47
    The big problem with encryption is it lies
    at
  • 2:47 - 2:53
    the intersection of math and security. So,
    like, math
  • 2:53 - 2:55
    is hard and security is hard, and when we
  • 2:55 - 2:58
    put all these things together, especially
    when we're talking
  • 2:58 - 3:01
    about doing this in a programming language
    where we
  • 3:01 - 3:05
    have bugs, things are gonna get pretty hard.
  • 3:05 - 3:08
    And this is one of my favorite quotes from
  • 3:08 - 3:12
    Cryptonomicon. So, this is kind of the situation
    we're
  • 3:12 - 3:14
    in, I think, in the Ruby world. That most
  • 3:14 - 3:18
    of the encryption gems out there aren't designed
    by
  • 3:18 - 3:23
    cryptographers, they're designed by amateurish
    people who are trying
  • 3:23 - 3:26
    to, trying to put everything together but
    maybe just
  • 3:26 - 3:29
    don't quite know how.
  • 3:29 - 3:32
    So I'm gonna go through all the attacks on
  • 3:32 - 3:35
    symmetric crypto. Unfortunately, I don't think
    I have time
  • 3:35 - 3:38
    to do asymmetric crypto. So I left a bunch
  • 3:38 - 3:41
    of those attacks out. But, guess what, it's
    even
  • 3:41 - 3:46
    worse. So we're gonna talk about AES today.
    AES
  • 3:46 - 3:50
    is a symmetric encryption cipher. It's great.
    I would
  • 3:50 - 3:54
    recommend you use AES, but it's sort of like
  • 3:54 - 3:57
    aim away from face, read all instructions
    before proceeding
  • 3:57 - 3:59
    kind of thing.
  • 3:59 - 4:03
    So this is how AES works. It's a block
  • 4:03 - 4:06
    cypher, so it works on fix-sized blocks. So
    we're
  • 4:06 - 4:10
    gonna take a sixteen byte piece of plain text
  • 4:10 - 4:12
    and we're gonna use a key, which can either
  • 4:12 - 4:16
    be sixteen, twenty-four, or thirty-two bytes.
    You want to
  • 4:16 - 4:17
    generate that randomly.
  • 4:17 - 4:20
    And from that we're gonna get a sixteen byte
  • 4:20 - 4:21
    block of cyphertext.
  • 4:21 - 4:25
    So, that's all well good. AES works great.
    There's
  • 4:25 - 4:28
    just this little problem. How do we encrypt
    something
  • 4:28 - 4:30
    that is large than sixteen bytes?
  • 4:30 - 4:35
    Well, one solution to this, PHP decided to
    use
  • 4:35 - 4:39
    a different ver- so AES is derived from a
  • 4:39 - 4:43
    cypher called Rijndael. PHP should be the
    version of
  • 4:43 - 4:48
    Rijndael with the 256 byte block size. So
    this
  • 4:48 - 4:52
    is awesome. It's a completely non-standard
    version of AES
  • 4:52 - 4:55
    that only PHP uses.
  • 4:55 - 4:58
    So let's not do that.
  • 4:58 - 5:01
    So let's figure out how to do this, right.
  • 5:01 - 5:03
    So the naive solution to this is to use
  • 5:03 - 5:08
    something called ECB mode. So there's this
    guy here,
  • 5:08 - 5:11
    he apparently couldn't find out how to use,
    do
  • 5:11 - 5:15
    encryption with OpenSSL itself, so he went
    off and
  • 5:15 - 5:19
    he made his own gem to encrypt stuff with
  • 5:19 - 5:20
    ECB mode.
  • 5:20 - 5:24
    You see there, he has C security nodes, ECB
  • 5:24 - 5:27
    mode only. The problem with the ECB mode is
  • 5:27 - 5:32
    it's really, really bad. So what ECB mode
    would
  • 5:32 - 5:35
    have you to is just take all these blocks
  • 5:35 - 5:38
    of plain text and encrypt them under the same
  • 5:38 - 5:40
    key.
  • 5:40 - 5:42
    So what's the problem with that? Well, it
    leaks
  • 5:42 - 5:47
    information. So let's say this is our plain
    text
  • 5:47 - 5:49
    we want to encrypt. If we run that through
  • 5:49 - 5:52
    ECB mode, we get something that looks like
    this.
  • 5:52 - 5:54
    So I think you can all see from this
  • 5:54 - 5:59
    that this thing isn't very well-encrypted.
    We can totally,
  • 5:59 - 6:02
    can totally see what it is.
  • 6:02 - 6:06
    So what's the solution to this? So, we need
  • 6:06 - 6:11
    to use a different block cipher mode of operation.
  • 6:11 - 6:13
    There's lots of these guys. We just kind of
  • 6:13 - 6:16
    have to pick one. There's all sorts of various
  • 6:16 - 6:20
    trade-offs. The one that people mostly settled
    on today
  • 6:20 - 6:22
    is countermode.
  • 6:22 - 6:26
    So counter mode is fairly easy to understand,
    I
  • 6:26 - 6:30
    think. It's sort of similar to a one-time
    pad.
  • 6:30 - 6:32
    So what we're gonna do is take a block
  • 6:32 - 6:35
    cypher, like AES and try to turn it into
  • 6:35 - 6:39
    a stream cypher. So what we're gonna do is
  • 6:39 - 6:42
    effectively generate a bunch of pseudo-random
    numbers.
  • 6:42 - 6:45
    So we feed in a nance, which is some
  • 6:45 - 6:49
    secret starting place, and a key, which is
    the
  • 6:49 - 6:51
    same as what we were putting in before. And
  • 6:51 - 6:53
    then there's this little counter, and every
    time we
  • 6:53 - 6:57
    crypt a block, it's just gonna count it by
  • 6:57 - 6:57
    one.
  • 6:57 - 6:59
    So what we're actually gonna do is combine
    the
  • 6:59 - 7:02
    nance and the counter, and encrypt that with
    AES,
  • 7:02 - 7:03
    and what we're gonna get is a little bit
  • 7:03 - 7:07
    of pseudo-random pad for each part of the
    plain
  • 7:07 - 7:10
    text. And for each of these paths, for each
  • 7:10 - 7:12
    of these blocks, we're gonna x over the pad
  • 7:12 - 7:15
    with a plain text, and that's gonna give us
  • 7:15 - 7:16
    our cypher text.
  • 7:16 - 7:19
    So if we go back to our little Ruby
  • 7:19 - 7:23
    gem image here, what we're gonna do is combine
  • 7:23 - 7:26
    this with a pseudo random pad, and what we're
  • 7:26 - 7:30
    gonna get is the cypher text. So I hope
  • 7:30 - 7:34
    you can see that little subtle change there.
    So
  • 7:34 - 7:37
    this is actually encrypted, right. We can
    no longer
  • 7:37 - 7:42
    see that gem and now we're done. Success!
  • 7:42 - 7:48
    We've, we've obtained confidentiality. Except
    a problem.
  • 7:48 - 7:51
    Our repeating nonces will leak information.
    So we can't
  • 7:51 - 7:54
    ever use the same nance and key. So the
  • 7:54 - 7:57
    solution is just don't do that.
  • 7:57 - 8:02
    So we've got another problem here. Support
    for counter
  • 8:02 - 8:07
    mode in Ruby OpenSSL is spotty. So unfortunately
    we
  • 8:07 - 8:10
    can't use sort of the industry best practices
    here.
  • 8:10 - 8:12
    And what we're gonna use is CBC mode. CDC
  • 8:12 - 8:16
    mode is fine. There are a few small issues
  • 8:16 - 8:20
    with it, but unfortunately I don't have time
    to
  • 8:20 - 8:21
    go into them.
  • 8:21 - 8:24
    So, next problem. Attacker, who we hand this
    message
  • 8:24 - 8:30
    to, can manipulate, with something called
    malleability. So let's
  • 8:30 - 8:33
    say our plain text is attack at dawn, and
  • 8:33 - 8:35
    we encrypt that and we have a cypher text
  • 8:35 - 8:37
    and we hand that to an attacker. Let's say
  • 8:37 - 8:40
    this attacker is able to guess what the plain
  • 8:40 - 8:41
    text was.
  • 8:41 - 8:44
    So what this attacker can then do is x
  • 8:44 - 8:48
    over part of the cypher text with what he
  • 8:48 - 8:50
    thinks the original plain text is, and then
    x
  • 8:50 - 8:52
    over that again with what he wants it to
  • 8:52 - 8:54
    be. So what you get is sort of this
  • 8:54 - 8:58
    like manipulated cypher text, and now when
    we decrypt
  • 8:58 - 9:01
    it, it gives us the wrong thing. This isn't
  • 9:01 - 9:03
    what we expected it to be.
  • 9:03 - 9:06
    So the solution to this is to use something
  • 9:06 - 9:10
    called a message authentication code. When
    we combine this
  • 9:10 - 9:13
    with a encryption cypher, what we get is something
  • 9:13 - 9:15
    called authenticate encryption.
  • 9:15 - 9:18
    So with a Mac, what we do is we
  • 9:18 - 9:22
    take the message and then we have a key,
  • 9:22 - 9:24
    and when we combine the message and the key,
  • 9:24 - 9:27
    we get this fix-lengthed tag. So we know we
  • 9:27 - 9:31
    have the right message when we take the same
  • 9:31 - 9:33
    message and the same key and then we get
  • 9:33 - 9:35
    the mac we expect.
  • 9:35 - 9:39
    So once again, there's a whole lot of these.
  • 9:39 - 9:42
    Like, HMAC is the one I'm sure you've heard
  • 9:42 - 9:45
    of if you've heard of one of these. The
  • 9:45 - 9:47
    others are somewhat less common.
  • 9:47 - 9:52
    So now we have yet another problem. What order
  • 9:52 - 9:57
    do we combine the encryption and the mac?
    So
  • 9:57 - 10:01
    there's effectively three ways to do this,
    and common
  • 10:01 - 10:04
    internet tools have all sort of chosen their
    own
  • 10:04 - 10:08
    different way. So the first is called mac-then-encrypt.
    This
  • 10:08 - 10:11
    is what's used by SSL and TLS. So the
  • 10:11 - 10:14
    idea is you take the plain text and you
  • 10:14 - 10:17
    compute the mac to the plain text, and then
  • 10:17 - 10:22
    you sort of combine those together and encrypt
    both.
  • 10:22 - 10:25
    Another way to do it is called encrypt-then-mac.
    This
  • 10:25 - 10:30
    is used by the IP sect protocol for, like,
  • 10:30 - 10:35
    yeah. So we have plain text. What we're gonna
  • 10:35 - 10:38
    do is encrypt it first, and then we're going
  • 10:38 - 10:42
    to calculate the mac of the cypher text. So
  • 10:42 - 10:45
    the third way, which is used by SSH, we
  • 10:45 - 10:49
    take the plain text and we encrypt it, then
  • 10:49 - 10:52
    we get the cypher text, and then we take
  • 10:52 - 10:54
    the original plain text and we compute the
    mac
  • 10:54 - 10:56
    of that and then we put the cypher text
  • 10:56 - 10:59
    and the mac of the plain text together.
  • 10:59 - 11:03
    So which, which of these sort of three standards
  • 11:03 - 11:06
    or three approaches got it right? Anybody
    want to
  • 11:06 - 11:10
    take a guess which of these is actually the
  • 11:10 - 11:12
    right way?
  • 11:12 - 11:14
    None. There's actually a right answer. One
    of them
  • 11:14 - 11:18
    did get it right. So the answer is encrypt-then-mac
  • 11:18 - 11:21
    used by IP sect. So why? What's wrong with
  • 11:21 - 11:24
    these other methods?
  • 11:24 - 11:27
    So SSL/TLS, you might remember there was this
    attack
  • 11:27 - 11:31
    called Beast. It's using something called
    a padding oracle.
  • 11:31 - 11:34
    I didn't really go into how padding actually
    works,
  • 11:34 - 11:38
    but it's how you deal with, when your plain
  • 11:38 - 11:40
    text isn't actually aligned to a block.
  • 11:40 - 11:43
    So, using Beast, they were able to get a
  • 11:43 - 11:46
    little bit of information out of when it decrypts
  • 11:46 - 11:50
    and it does this padding check and you can
  • 11:50 - 11:52
    tell if it got through the padding or not
  • 11:52 - 11:54
    before it hit the mac.
  • 11:54 - 11:57
    So the solution TLS uses now is to make
  • 11:57 - 11:59
    sure it always checks the mac, even if the
  • 11:59 - 12:02
    padding fails. So that's a little bit of a
  • 12:02 - 12:03
    band aid.
  • 12:03 - 12:08
    Encrypt-and-mac, used by SSH, is vulnerable
    to chosen cyphertext
  • 12:08 - 12:12
    attacks. There's a fun little paper on this.
    Fortunately
  • 12:12 - 12:18
    the SSH protocol was extensible enough they
    managed to
  • 12:18 - 12:23
    avoid, they managed to effectively fix this
    retroactively.
  • 12:23 - 12:26
    So in review here, if we were trying to
  • 12:26 - 12:30
    build our own authenticated encryption scheme,
    what we have
  • 12:30 - 12:33
    so far is sort of using AES in CBC
  • 12:33 - 12:37
    mode, since Ruby OpenSSL doesn't support countermode.
    We're gonna
  • 12:37 - 12:41
    do the IP sect thing and encrypt-then-mac
    and I
  • 12:41 - 12:44
    showed HMAC. HMAC is really nice cause it
    takes
  • 12:44 - 12:46
    a lot of the sharp edges off some of
  • 12:46 - 12:47
    the other macs.
  • 12:47 - 12:51
    So what this actually ends up looking like
    is
  • 12:51 - 12:56
    what's in Rails. The ActiveSupport message
    encrypter. So this
  • 12:56 - 12:58
    is definitely a cool thing to use if you
  • 12:58 - 13:01
    just need to encrypt something and you're
    using Rails.
  • 13:01 - 13:04
    It's definitely a way to go.
  • 13:04 - 13:08
    So let's talk about what else could go wrong.
  • 13:08 - 13:10
    Not done yet.
  • 13:10 - 13:13
    So the next thing is timing attacks on. So
  • 13:13 - 13:17
    speaking of that ActiveSupport message encryptor,
    it used to
  • 13:17 - 13:20
    be vulnerable to these. So this is a patch
  • 13:20 - 13:24
    by Koda Hail to implement a constant time
    comparison
  • 13:24 - 13:27
    of the mac. And the problem is if you
  • 13:27 - 13:29
    don't do this, the attacker can use this sort
  • 13:29 - 13:34
    of, like, infinitesimal timing information,
    especially if they're on
  • 13:34 - 13:35
    the same LAN as you or on the same
  • 13:35 - 13:38
    host, to just try to guess at the mac
  • 13:38 - 13:39
    a byte at a time.
  • 13:39 - 13:42
    So you see before what it was doing was
  • 13:42 - 13:45
    not equals. So the problem with not equals
    is
  • 13:45 - 13:48
    it'll sort of bail fast and exit early as
  • 13:48 - 13:51
    soon as it sees something that doesn't match.
    So
  • 13:51 - 13:54
    by using the timing information off of that,
    an
  • 13:54 - 13:58
    attacker can effectively guess the mac a byte
    at
  • 13:58 - 13:59
    a time.
  • 13:59 - 14:01
    So the solution is all that nonsense you see
  • 14:01 - 14:04
    down there at the bottom, where they're doing
    xor
  • 14:04 - 14:07
    between the two bytes and doing or equals
    and
  • 14:07 - 14:10
    doing this over all the bytes and then looking
  • 14:10 - 14:13
    at the actual value of the result.
  • 14:13 - 14:17
    So this is all pretty crazy stuff, right.
    And
  • 14:17 - 14:22
    we haven't even talked about pubkey. So really,
    I
  • 14:22 - 14:25
    don't think amateurs should be trying to put
    all
  • 14:25 - 14:27
    this stuff together. Unfortunately that's
    sort of been the
  • 14:27 - 14:32
    state of the world in Ruby.
  • 14:32 - 14:35
    So what can we do better? We can have
  • 14:35 - 14:42
    more boring crypto constructs. So what, what
    qualifies as
  • 14:42 - 14:45
    boring? Well, to do that, let's talk about
    what
  • 14:45 - 14:46
    isn't boring.
  • 14:46 - 14:50
    So this has been how most things have worked
  • 14:50 - 14:54
    in the Ruby world. So OpenSSL is kind of
  • 14:54 - 14:58
    a terrible library to begin with, and then
    pretty
  • 14:58 - 15:01
    much every Ruby gem you see that deals with
  • 15:01 - 15:05
    encryption just layers on a bunch of amateur
    code,
  • 15:05 - 15:06
    trying to put all this stuff I just described
  • 15:06 - 15:07
    to you together.
  • 15:07 - 15:11
    And typically, they'll get at least one thing
    wrong.
  • 15:11 - 15:14
    So, I'm not gonna name anymore names besides
    that
  • 15:14 - 15:17
    Fast APS gem, because I think that one was
  • 15:17 - 15:20
    just pretty crazy and dangerous. But pretty
    much every
  • 15:20 - 15:23
    gem out there, if you go through and you
  • 15:23 - 15:24
    try to look for all these things to make
  • 15:24 - 15:27
    sure they got it all right, they will generally
  • 15:27 - 15:30
    get something wrong. Oftentimes they will
    not use a
  • 15:30 - 15:33
    mac at all, so they'll just use encryption,
    and
  • 15:33 - 15:37
    the attacker can screw with your cypher text.
  • 15:37 - 15:39
    And then there's all sorts of other little
    things
  • 15:39 - 15:42
    that they can get wrong which I didn't even
  • 15:42 - 15:43
    cover here.
  • 15:43 - 15:49
    So the boring approach in my opinion is to
  • 15:49 - 15:52
    use a crypto library written by cryptographers.
    And when
  • 15:52 - 15:55
    I talk about a cryptographer, who am I talking
  • 15:55 - 15:58
    about? And it isn't someone like me, right.
    I'm
  • 15:58 - 16:01
    a crypto enthusiast, but I'm not a cryptographer.
    I
  • 16:01 - 16:04
    cannot design my own cyphers that will be
    secure
  • 16:04 - 16:08
    yet. It's something I aspire to maybe, but
    not
  • 16:08 - 16:09
    quite yet.
  • 16:09 - 16:12
    So what we really want is to bind to
  • 16:12 - 16:16
    a library that was actually written by cryptographers,
    people
  • 16:16 - 16:19
    who spend all their time thinking about these
    kinds
  • 16:19 - 16:21
    of attacks.
  • 16:21 - 16:25
    So I have written a library like this that
  • 16:25 - 16:30
    is actually a FFI binding to an actual library
  • 16:30 - 16:35
    written by cryptographers. So the name is,
    unfortunately, a
  • 16:35 - 16:39
    little bit confusing. I call it RbNaCl, but
    you
  • 16:39 - 16:43
    may also know there is Google native client.
    It
  • 16:43 - 16:48
    isn't that. And there's also a, a, an ACL
  • 16:48 - 16:54
    organization that, in Japan, you might be
    familiar with.
  • 16:54 - 16:56
    It isn't that.
  • 16:56 - 16:58
    So this is a library by this guy Dan
  • 16:58 - 17:01
    Burnstein. You may know him for libraries
    like, or
  • 17:01 - 17:08
    projects like Qmail, DJBDNS, and DaemonTools.
    And if you
  • 17:08 - 17:10
    haven't been paying attention to him for the
    past
  • 17:10 - 17:14
    decade or so, what he's really going hardcore
    for
  • 17:14 - 17:15
    is cryptography.
  • 17:15 - 17:18
    So he has designed quite a few of his
  • 17:18 - 17:22
    own cyphers. And all sorts of algorithms and
    created
  • 17:22 - 17:27
    this NaCl library, which is slowly gaining
    a little
  • 17:27 - 17:31
    bit of popularity. And you see the URL down
  • 17:31 - 17:36
    there. It's under the cryptosphere organization,
    slash rbnacl.
  • 17:36 - 17:40
    So to make things even more confusing, this
    isn't
  • 17:40 - 17:44
    actually binding to nacl, it's binding to
    a portable
  • 17:44 - 17:48
    repackaging of nacl called libsodium. So the
    best description
  • 17:48 - 17:53
    I've heard of this, is it's de-Burnsteinized.
    A lot
  • 17:53 - 17:56
    of, a lot of people don't like DJB's bold
  • 17:56 - 17:58
    system, and it's kind of crazy cause the way
  • 17:58 - 18:01
    it works, once you bolt the library it's completely
  • 18:01 - 18:05
    non-relocatable, so you can't really package
    it as a
  • 18:05 - 18:07
    binary for a distribution.
  • 18:07 - 18:11
    So libsodium took nacl and added a standard
    automake
  • 18:11 - 18:16
    style build system. So it's easy to install,
    it
  • 18:16 - 18:18
    is in Mac ports, and you can install it
  • 18:18 - 18:23
    with brew install libsodium. There are packages
    for various
  • 18:23 - 18:26
    Linux distributions. Some of them, you know,
    you'll have
  • 18:26 - 18:29
    to actually build yourself from source, but
    the basic
  • 18:29 - 18:32
    work is there, and it's getting more and more
  • 18:32 - 18:34
    widespread option.
  • 18:34 - 18:39
    So nacl has many primitives. I'm only gonna
    talk
  • 18:39 - 18:43
    about two here. So these are the symmetric
    and
  • 18:43 - 18:49
    asymmetric encryption I was referring to earlier.
    But it
  • 18:49 - 18:51
    also has a ton of other stuff, so it
  • 18:51 - 18:57
    has HMAC. It has digital signatures. It has
    all
  • 18:57 - 19:00
    sorts of fun stuff for elliptic curve cryptography.
  • 19:00 - 19:05
    So I definitely recommend checking out. You
    can go
  • 19:05 - 19:07
    to the rbnacl wiki and it has all the
  • 19:07 - 19:12
    features listed out for you.
  • 19:12 - 19:15
    So first I'm gonna talk about the symmetric
    encryption
  • 19:15 - 19:21
    primitive it provides. So this is authenticated
    symmetric encryption
  • 19:21 - 19:22
    and it is called secretbox.
  • 19:22 - 19:29
    So this may be a little hard to see.
  • 19:29 - 19:33
    But it should be fairly straightforward to
    follow, I
  • 19:33 - 19:35
    hope. So what we're gonna do is we're gonna
  • 19:35 - 19:37
    make a key that is the size of a
  • 19:37 - 19:41
    secret box key. It's actually thirty-two bytes.
  • 19:41 - 19:46
    So you get a random key and that is
  • 19:46 - 19:52
    using rbnacl's own random number generation.
    On Unix-like operating
  • 19:52 - 19:57
    systems it upholds from devurandom and on
    Windows it
  • 19:57 - 20:02
    uses, I forget what it's called. Yeah. Windows.
  • 20:02 - 20:05
    Anyway. It does work on Windows.
  • 20:05 - 20:07
    So after we've done that, we're gonna make
    a
  • 20:07 - 20:11
    new secret box with the key, and then here's
  • 20:11 - 20:14
    the fun part. We need to make a nance.
  • 20:14 - 20:18
    So this is just generating a random nance
    of
  • 20:18 - 20:21
    the secret box's nance byte's length.
  • 20:21 - 20:26
    There's also another feature in rbnacl called
    random-nance box
  • 20:26 - 20:28
    that'll do all this stuff for you. The important
  • 20:28 - 20:33
    part is a nance is a single-use value. So
  • 20:33 - 20:36
    every time you encrypt something with this
    box, you
  • 20:36 - 20:38
    need to make a new nance.
  • 20:38 - 20:41
    So it doesn't actually have to be random.
    It
  • 20:41 - 20:44
    could just be a counter. The nice thing about
  • 20:44 - 20:48
    it being random is it's long, so it's twenty-four
  • 20:48 - 20:51
    bytes, so if you just do a random number
  • 20:51 - 20:54
    every time it's hard to screw up. You don't
  • 20:54 - 20:56
    need to keep track of the state of how
  • 20:56 - 20:59
    many nances you've used.
  • 20:59 - 21:01
    So after that you hand the nance and the
  • 21:01 - 21:04
    message to the secret box. It'll make the
    cypher
  • 21:04 - 21:07
    text for you. And to decrypt it, you give
  • 21:07 - 21:10
    the same nance and the cypher text and it
  • 21:10 - 21:12
    will decrypt. And behind the scenes, this
    is doing
  • 21:12 - 21:15
    all the authenticate encryption stuff. So
    it's adding a
  • 21:15 - 21:18
    mac, it's checking it, and it's using an encrypted
  • 21:18 - 21:19
    mac.
  • 21:19 - 21:21
    And then down there at the bottom, you see
  • 21:21 - 21:25
    if you try to open the box and something
  • 21:25 - 21:27
    is wrong, either the nance is wrong or the
  • 21:27 - 21:30
    cypher text has been corrupted, it'll raise
    an exception
  • 21:30 - 21:34
    for you reliably.
  • 21:34 - 21:37
    So this is using a couple of algorithms designed
  • 21:37 - 21:41
    by Dan Burnstein. So the encryption cypher
    is called
  • 21:41 - 21:46
    the XSalsa20, and the Mac is called Poly1305.
    So
  • 21:46 - 21:49
    probably right now, you're thinking, what
    the hell is
  • 21:49 - 21:53
    XSalsa20. Like, never heard of this. That
    can't be
  • 21:53 - 21:53
    boring, right.
  • 21:53 - 21:57
    So I think this is boring because it is
  • 21:57 - 22:00
    a cypher that has sort of been standardized
    through
  • 22:00 - 22:03
    this contest in Europe called ecrypt. And
    the goal
  • 22:03 - 22:06
    of ecrypt is sort of like when there is
  • 22:06 - 22:10
    an AES contest. They're trying to produce
    better stream
  • 22:10 - 22:14
    cyphers so the Salsa20 cypher was one four
    they
  • 22:14 - 22:18
    selected for inclusion in what they call the
    estream
  • 22:18 - 22:19
    portfolio.
  • 22:19 - 22:23
    I say this is boring because things have gotten
  • 22:23 - 22:30
    very not boring in the cryptography world
    lately. So
  • 22:31 - 22:34
    you may have heard nist standardized a random
    number
  • 22:34 - 22:39
    generator designed by the NSA called duel
    EC DRBG,
  • 22:39 - 22:43
    and turns out that thing had a back door.
  • 22:43 - 22:48
    So there's still open questions about the
    nist elliptic
  • 22:48 - 22:51
    curves as well and whether or not they contain
  • 22:51 - 22:56
    possible NSA backdoors. I mean, if you're
    asking one
  • 22:56 - 23:00
    layman's opinion, I'd say it's probably unlikely
    they have
  • 23:00 - 23:05
    backdoors in nist elliptic curves, but it's
    hard to
  • 23:05 - 23:07
    know, and I think the boring thing is to
  • 23:07 - 23:13
    use cyphers that are beyond reproach.
  • 23:13 - 23:18
    So to take, to do asymmetric crypto using
    the
  • 23:18 - 23:20
    same sort of primitives, we have this thing
    called
  • 23:20 - 23:27
    box. And here is an example on how to
  • 23:27 - 23:28
    use box. So this time we have to actually
  • 23:28 - 23:32
    generate a random key.
  • 23:32 - 23:35
    So this is using that elliptic curve stuff
    I
  • 23:35 - 23:37
    was talking about earlier. I'll get into how
    that
  • 23:37 - 23:41
    works a bit later. So we take a private
  • 23:41 - 23:45
    key and, unlike RSA, with elliptic curves
    we can
  • 23:45 - 23:49
    calculate the public key from the private
    key. I
  • 23:49 - 23:55
    think I spot a typo there. Awesome. Oh no.
  • 23:55 - 23:57
    So yeah. So once we've done that, we have
  • 23:57 - 24:02
    a public key and you'll notice here when we
  • 24:02 - 24:05
    make a new box we're giving it both somebody
  • 24:05 - 24:09
    else's public key and our private key, and
    the
  • 24:09 - 24:11
    neat thing about this is it's performing something
    called
  • 24:11 - 24:13
    mutual authentication.
  • 24:13 - 24:16
    So all these boxes are based around a set
  • 24:16 - 24:19
    of keys. It's who you're sending the message
    to,
  • 24:19 - 24:22
    and your private key, so that way when somebody
  • 24:22 - 24:24
    opens up the box, they'll know they got it
  • 24:24 - 24:26
    from the right person.
  • 24:26 - 24:31
    So make a new box. They'll also have a
  • 24:31 - 24:35
    box on the other side. It is effectively the
  • 24:35 - 24:39
    same box, just computed from the irreversible
    keys.
  • 24:39 - 24:42
    So then after here it's pretty straightforward,
    just like
  • 24:42 - 24:46
    the symmetric crypto. So what we're gonna
    do is
  • 24:46 - 24:49
    make a nance again. We have the message and
  • 24:49 - 24:53
    we're going to encrypt the message under that
    nance
  • 24:53 - 24:57
    and under that pair of keys. We get a
  • 24:57 - 24:59
    cypher text again and then the person on the
  • 24:59 - 25:02
    other side can open it, and once again, like
  • 25:02 - 25:05
    before, if it has been tampered with it will
  • 25:05 - 25:06
    raise an exception.
  • 25:06 - 25:11
    So box is built on the same primitives as
  • 25:11 - 25:15
    secret box, so it's still using the xSalsa20
    cypher
  • 25:15 - 25:19
    and the Poly1305 mac. The main difference
    here is
  • 25:19 - 25:24
    we're adding in a Diffie-Hellman function.
    So that is
  • 25:24 - 25:30
    called curve25519. And that is an elliptic
    curve algorithm
  • 25:30 - 25:31
    designed by Dan Burnstein.
  • 25:31 - 25:35
    So one of the big worries about elliptic curve
  • 25:35 - 25:39
    cryptography, in addition to possible NSA
    tampering has been
  • 25:39 - 25:44
    patents. So DJB has designed this curve specifically
    to
  • 25:44 - 25:47
    side-step all the patents. It's pretty awesome.
    If you
  • 25:47 - 25:50
    go to the website for it you'll also add
  • 25:50 - 25:52
    the patent, and he lists out the prior art
  • 25:52 - 25:55
    saying this patent is bullshit, anyway. But
    then he's
  • 25:55 - 25:59
    like, here I completely avoided that problem
    entirely.
  • 25:59 - 26:06
    So really quick, here's how Diffie-Hellman
    works. SO it's
  • 26:07 - 26:10
    pretty simple. What we do is a thing called
  • 26:10 - 26:15
    scalar multiplication. So we take Alice's
    private key and
  • 26:15 - 26:19
    perform a scalar multiplication operation
    with Bob's public key
  • 26:19 - 26:21
    and we get a shared secret.
  • 26:21 - 26:26
    And Bob is able to calculate the same secret
  • 26:26 - 26:30
    by multiplying his private key by Alice's
    public key.
  • 26:30 - 26:34
    So these two, two operations are sort of like
  • 26:34 - 26:36
    a reflection of each other.
  • 26:36 - 26:41
    So secret, box works by taking that shared
    secret
  • 26:41 - 26:43
    and using it to drive a symmetric key.
  • 26:46 - 26:50
    And that's all I got. Keep it boring.
Title:
Ruby Conf 2013 - Being Boring: A Survival Guide to Ruby Cryptography
Description:

more » « less
Duration:
27:18

English subtitles

Revisions