Magic Pixie Dust: What to Know About Encryption
This is talk 4 of 5 in my Rock Talk Security Series:
- Everything Leaks
- Mission UnPossible
- What's in YOUR Threat Model?
- Magic Pixie Dust
- Trust Me Bro
On October 12, 2019, I spoke at
ShellCon (opens in a new tab),
a security conference in San Pedro, California. The talk was
Magic Pixie Dust: What to Know About Encryption, and I pitched it as a back-to-basics session for
people newer to the field.
Watch it
The talk ran about an hour, but unfortunately the first 45 minutes of the recording didn't work and were lost! What survives is the last stretch, about 17 minutes, on ShellCon's YouTube channel.
What the talk covered
- A quick history of encryption.
- How analog encryption works, and how it differs from digital encryption.
- How to implement encryption well.
The main point
Encryption isn't magic pixie dust. Having it doesn't make a system secure; using it well is what matters.
The slides
Here’s the deck from the talk, lightly cleaned up for the web, with my speaker notes under each slide. Since most of the recording was lost, the notes are the closest thing to the full talk. Use the arrows (or swipe) to step through it.
Hey, so… welcome to Magic Pixie Dust: A Brief Intro to Encryption
I am not a cool InfoSec person like most of you here in the audience today….
I’m just a Software Engineer. Specifically, a .NET software engineer – I mostly write in C# -- judge me if you must ( I won’t let words hurt me). So this talk isn’t about how to attack encryption or how to invent new encryption methods – it’s more from the perspective of me, as a developer – it’s what I’ve learned while trying not to screw it up.
So – Encryption. It’s not magic pixie dust. You can’t just sprinkle encryption on your data and expect that it makes everything magically secure. But we will be going to never never land because we’ll go over some things that you should never never do.
I promise that’s my last dad joke for this talk, I'll be here all week, folks…. but we will talk about some things you should avoid.
The origins of this talk go back to a conversation that I had with a co-worker. I said something dumb. Well, what I said wasn’t dumb – it’s that I said something like it was fact without first having done my research. Here’s how it went.
We were discussing the conspiracy theory about the moon landing being faked. I offered, what I thought, was the most compelling argument for why it was, definitely…. Not faked.
While the moon landing was supposedly taking place, I said that all you’d really need to do to verify that “yes, they’re on the moon”, would be to point a radio antenna at the Moon, hear the signal, and triangulate it to confirm. Surely someone with a vested interest in confirming that we really were on the moon (Russia) would have done this?
And here’s what I said without first doing my research: I said “and they couldn’t encrypt voice radio transmissions back then either”.
I said this because, at the time that I made the comment, when I thought of encryption I thought of the “fancy math” encryption – the type that we use in today’s encryption. Modern Day Encryption, like AES, depends upon having a discrete signal and radio transmissions in 1969 were analog – there just wasn’t the computational power needed to convert analog signals to binary on the fly, encrypt, and send the data along. Remember: our cell phones were analog until the late 90s! Anyone with a handheld radio could tune to a specific frequency and listen in on your phone conversation. If your encryption algorithm requires discrete input, then you can’t use that algorithm on an analog signal.
My co-worker said “That’s not true, they COULD encrypt analog signals in 1969”.
It’s true that the Apollo radio transmissions were analog.
It’s also true that when we, in a general sense, talk about Encryption, ESPECIALLY in the context of computer security, we’re talking about algorithms that operate on discrete input – not analog. And computers are a product of the modern age.
But what if we’re talking about cryptography that is not of the modern age? Let’s go back a couple of thousand years. Discuss some basics, and then circle back to the Moon landing.
Show of hands: Who knows what ROT13 is?
ROT13 also known as the “Caesar Cipher” – that’s a picture of a Caesar Salad…. But it’s called the Caesar Cipher because….
….because Julius Caesar used it in his messages of military importance.
Here’s how the shift works. With a key of 1, every letter moves one place down the alphabet: A becomes B, B becomes C, and H becomes I. So “Hello World” becomes “Ifmmp Xpsme”.
With a key of 2, every letter moves two places: H becomes J, and “Hello World” becomes “Jgnnq Yqtnf”.
To decrypt, you shift back by the same amount. The key is just the number of places you shift.
ROT13 is the same idea with a key of 13. “Hello World” becomes “Uryyb Jbeyq”.
To decrypt, shift back 13, and you have “Hello World” again.
But here’s the neat part: the alphabet has 26 letters, and 13 is half of that. So shifting forward another 13 lands you in the same place as shifting back. Every letter just swaps with its partner on the other row. Encrypt twice with ROT13 and you get the original back, so the same operation both encrypts and decrypts.
The Caesar Cipher is what’s known as a substitution cipher because you substitute one value for another. Substitution Ciphers are a whole class of encryption, but the this one specifically is sometimes called a “shift” cipher because the way you substitute is by shifting the alphabet back and forth.
In encryption, we also talk about Keys. In the case pictured above, the key would 3 because you shift all the input letters to the left by 3 spaces. To decrypt, you’d shift right by the same amount.
Rot 13 (which is short for “Rotate 13”) is shifted 13 letters. Because English has 26 letters, and 13 is half of that, by “encrypting” a second time you get the decrypted value – convenient!
So here is “Hello World” encrypted with the Caesar Cipher with a key of 2, and then again with a key of 13.
Does anyone know what this is?
Yes – Base64. Who would like the elevator-explanation of Base64?
Does anyone know what this is?
Yes – this is Base64.
Here’s the elevator explanation of Base64.
Computers store text as bytes, 8 bits each. Base64 regroups those bits into 6-bit chunks. Six bits can hold 64 different values, and each value maps to one of 64 printable characters: A through Z, a through z, 0 through 9, plus and slash.
Take “Hel”. H, e and l are these three bytes. Regroup the same 24 bits into four groups of six, and you get 18, 6, 21 and 44, which map to S, G, V and lowercase s. Every 3 bytes become 4 characters, and when the input doesn’t divide evenly by 3, the end is padded with equals signs.
Notice there’s no key and nothing secret: anyone can reverse it.
Conceivably, you could create base 63 – just use every character in base 64 except 1 – maybe you don’t use uppercase G, for example…
If you have just pen & paper, and a base 64 (or whatever base) is considerably more difficult than the Caesar Cipher to convert back to plaintext. So why do we call the Caesar Cipher encryption and Base 64 encoding?
The answer is intent. It depends on what you intend the transformation algorithm to do. Caesar intended to keep his correspondence secret. Base64 was intended to be a common, well-known, way to map binary data to ASCII, for a variety of purposes in computer science.
If Morse Code had been created with the intent of taking plain-text messages and converting them into something “strange” to keep them secret… then we would call that encryption. But we don’t, we simply call it encoding. Also…. If Morse code was considered encryption, then it would be incredibly bad encryption!
This definition comes from the Wikipedia page for Encryption….. They put it better than I could!
And Encoding is just mapping one set of symbols to another set – secret or public.
For example, let’s do this with Emojis. Suppose we map the word Look to this zany face, Sheep to a sheep, and Fun to a party popper.
Then we can start building sentences with our encoding scheme… zany face, sheep, party popper means “look at the sheep having fun”.
The Cipher is the algorithm that’s used to perform the mapping. Some are more complex than others.
So….. So far, we’ve covered some pretty basic stuff, and I want to get to some more fun stuff – talk about the more complex algorithms, the types of ones we use today, like AES, but first I want to circle back to the story about the moon landing and the idea of encrypting analog signals.
So…. Coming full circle….. And looping back to the moon landing.
I promise, this is the only “wall of text” in this whole presentation. In doing my research to figure out if and how analog signals could be encrypted, especially voice, I came across this answer on Stack Exchange Cryptography. I pasted the screenshot up here because this user put it better than I ever could paraphrase.
If you want to dig into this further for yourself (there are several good links in there), here’s the link to the Stack Exchange page.
One method to secure analog signals is to add pre-recorded white noise to the transmission. On the receiving end, audio filters would filter out the white noise by using the same pre-recording.
In this case, the cipher is the fact that you add white noise and the key is the pre-shared white-noise recording.
Another technique would be frequency shifting, or you could call it frequency modulation. If both the sender and receiver know what frequency shifts occur at what time-offsets, then this can be effective. The knowledge of what frequency shifts occur and at what time periods is the encryption key.
I mentioned frequency modulation – that’s also called FM, which is the same thing as everyday FM radio. Again, the difference between encryption and encoding really is intent. So, you can say that obfuscation is encryption…. But you should never never say something like “oh, the data, or the code, is obfuscated, so we’re fine….”. But enough about the analog encryption techniques.
Let’s get back to the fun stuff – let’s start talking about stuff that is more relevant to our everyday lives. Modern Encryption.
We know that the Caesar Cipher is easy to break. If there are only 26 letters in the alphabet, you can brute-force it in 26 tries or fewer. It’s easy to break because it’s so simple. So we might be tempted to say that the more complicated the algorithm, the better.
The more complex an algorithm, the slower it is. Even if you have the correct key.
A super complex algorithm may be fine if you have a super computer. But if you’re dealing with very low-power scenarios, then the same algorithm may not be the best choice. There’s a balance to be had between complexity (that is, difficulty), and speed.
A good encryption algorithm is easy to encrypt and decrypt with the right key, but difficult if you don’t.
If you’d like a really good, more formal, and academic discussion of what is “good” encryption then…….
Then I’d recommend Cryptography Engineering by Niels Ferguson, Bruce Schneier, and Tadayoshi Kohno. I think the whole thing is good and talks about a lot more than just formal/academic definitions of “good encryption”, really. And while you’re looking at books, I think that you can safely ignore Practical Cryptography. It is a good book, but it’s mostly a waste of your time. You see, Crypto Engineering is, for all intents and purposes, just “Practical Cryptography, 2nd Edition”. It’s by the same authors, it’s mostly the same content, but has been updated with newer information and organized in such a way that it can be used as a textbook in college classes. Otherwise, they’re more, or less, the same book.
So, let’s talk about Encryption in a more real-world sense. You may have heard of some of these terms and know that that there’s encryption involved, but let’s dig in a little bit.
I’m going to talk about two types of encryption: Symmetric and Asymmetric.
With Symmetric encryption, the same key is used for both encryption and decryption. So far, everything we’ve discussed – all the way from Caesar Cipher to the white-noise in analog audio – has been symmetric.
In the Symmetric world, you’ll see things like DES, Triple DES, AES, Blowfish, Twofish, among others.
In the Asymmetric world you’ll see things like RSA, ECC, which is Elliptic Curve Cryptography, PKI, which is Public Key Infrastructure – that’s going to be your public/private key related stuff, and Certificates. Now, with the things I listed out over here on the right, there is some overlap, and I’ll get to that.
And here in the middle we have things like IPSec, HTTPS, SSL, & TLS. A lot of people will say “oh, well, SSL, that’s all about having an SSL Certificate, so it must be Asymmetric..
Well, yes, but actually no…. And I’ll come back to that.
Let’s start with DES, which stands for Data Encryption Standard. In the mid 70s it was selected as a FIPS algorithm. It became the de facto standard until the late 90s when a new standards were approved. It’s a Block Cipher with a key length of only 56 bits. A few things made DES weak, but the 56 bit key length drew a lot of criticism. It was effective in the mid 70s, but became vulnerable as Moore’s law meant that computers become faster and faster.
In 1995, Triple DES came along, which was basically DES times THREE! I have a picture of Triple Sec up there because I *desperately* want to invent an encryption algorithm just so I can name it Triple Sec. You’ll notice the Key Size is 168 – that’s 56 times 3. Well, technically, 3DES could use keys of either 56, 112, or 168 bits. I won’t go into the details, but it *basically* encrypted each block of 64 bits 3 times, each time with a different key.
AES hit the scene in 1998 and became a Standard in 2001. It’s significantly stronger than DES or 3DES. AES is not actually the name of the algorithm – it’s actually… well, I can’t pronounce that.
To reference this book again…. Do we have any Dutch speakers in the room?
AES is still a recommended standard and provides pretty good encryption. It’s not *the* strongest, but no-one will fault you for using AES. Let’s say you need to implement encryption and you select an algorithm that’s generally considered stronger than AES and… down the road, your application or system gets breached. In the subsequent investigation, you’ll most likely really have to defend your choice to use whatever algorithm. Or suppose that you work in a regulated industry, like banking or healthcare, and all of your stuff needs to pass a security audit before being released. No one will question your use of AES. Anything else, even if it’s strong, will most likely draw strong scrutiny.
So AES is still considered “Pretty Good” to this day. DES had weaknesses from the beginning. That’s largely due to the way in which it became a standard. When a replacement for DES was needed, a committee did not decide to design the replacement – instead, a request for proposals was published. AES or, really… that Dutch name that I can’t pronounce, was one of many submissions. TwoFish by Bruce Schneier was another submission. Regardless of the algorithm name, whatever was to be selected was destined to be called “AES”.
AES was selected because it was regarded as the best balance between the things that NIST thought were important – strength, speed, performance, etc…
A classic example of design-by-committee gone wrong is WEP, the wi-fi encryption that *you. Should. Never. Use*
Because it was part of an open competition and not part of a closed design-by-committee, AES – as well as the other submissions – received a ton of scrutiny. As a result, the cryptography and security communities have a pretty good understanding of its potential weaknesses. I say this because you should never trust encryption ciphers or algorithms that have not undergone public scrutiny.
So it would make sense that it’s a bad idea to “roll your own” cryptography. If you go with a well vetted standard, you should be good, right?
When I first needed to implement encryption in a project, I thought that “roll your own” meant inventing your own encryption cipher. Don’t invent your own cipher when there are some pretty solid ones already out there. It turns out that doing encryption the right way is much harder than that.
AES, and DES for that matter, are both what are called block ciphers. Let’s dig into that a little bit more to understand what that means, and how it fits into this concept of “don’t roll your own crypto”
If this is the binary data that you’d like encrypted, and you have a block size of, say 24 then
Then this is the first block, and it’s encrypted with the bits from the key. In the case of DES, you have a 56 bit key that would get blended with this. In the case of AES, the key size could be 128, 192, or 256.
Then the same key would be used for the second block of 24 bits
Third
Fourth
And so on. If the data isn’t evenly divisible by the block size -- see that last row? – then the data will be padded.
This is the illustration of TwoFish, a block cipher. I wanted to put AES up here, but couldn’t find a diagram similar to this one. I think you get the idea – the cipher itself is a bunch of “fancy math” – some xoring and that sort of thing. In the binary I showed previously, we assumed to have a block size of 24. In this diagram, we have a block size of 128. So, this illustration is performed for each block. Sounds pretty good, right?
You might be tempted to do something like this. Use a well known crypto library. I have some C# code here – I’m using the System.Security.Cryptography Library. If you’re not a C# developer, I’ve heard nothing but good things about LibSodium. I’ve never used it though, so that’s about as much as I can say about it.
Side Story – on a Windows Machine there is check-box somewhere deep down inside of Group Policy that says “use only FIPS compliant encryption”. If you check that the OS does its best to block any non FIPS approved algorithms. That means if you try to implement Rijndael (that Dutch algorithm) directly, windows will block execution of the application when that method tries to execute – and that’s despite it being a part of the .NET framework. Similarly, SSL will not work on that machine. TLS will, but SSL will not. If you’re writing an application and you want it to be deployed in a US Department of Defense setting, keep in mind that that group policy box is usually checked. I have no idea if LibSodium will work or not.
If you’d like to see a list of what those FIPS compliant algorithms are, you can see them here.
Coming back to this – it would not be unreasonable for a developer to think “Hey, this is AES, which is good and it’s FIPS approved… so this should be solid, right?”
I mean, I’ve been guilty of this before.
This isn’t good enough because if two blocks are the same, then their encrypted values will be the same as well.
For example, if this is the input to a block cipher, then the output might look something like this.
And speaking of predictable input…. An attacker who is trying to break your encryption might not know the entirety of the message, but they might know part of it. For example, some file types have large sections of them that are all zeros.
If I recall correctly, what helped Alan Turing and his team break the German Enigma encryption of the Enigma Machine during World War II is that there was some predictability to most of the encrypted messages – for example, many messages ended with the same predictable sign-off, “Heil Hitler”. This is a still from the movie The Imitation Game, which I highly recommend.
This is where the concept of Block Cipher Mode comes in. In this example, ECB, which stands for Electronic Code Book, was used. There are several other modes. A good mode will give you this.
Looking back at our C# example, you can specify the mode pretty easily by adding a line of code.
Here are 4 common Block Cipher Modes, and there are others. I’ll give that a moment to sink in.
You’ll notice that both OFB and CTR are considered Stream Ciphers. I’ll be honest …. I don’t fully understand the difference between Block Ciphers and Stream Ciphers. Some literature I read implies that they’re separate, but this would imply that Block Ciphers can be in Stream Cipher mode. If we use AES in OFB mode, is it no longer a block cipher? You probably don’t want to hear your presenter say “I don’t really understand this stuff”, but I hope that it drives home the point – DO NOT ROLL YOUR OWN CRYPTO.
But if you must…..don’t use ECB.
So which mode should you use? I’ll let you guess which one is ECB……
I’ve read both of these books, and remember – Crypto Engineering is just an updated version of Practical. They both discuss, in detail, the strengths and weakness of the 4 modes which I listed. In Practical Cryptography, which was published in 2003, they recommended CTR, but in Crypto, published in 2010, they recommended CBC.
I recently implemented some AES encryption for a project, and during its security audit, static code analysis flagged CBC as a “weak” mode – susceptible to something called a Padding Oracle Attack.
So which one to use……
So, I used to think that rolling your own crypto meant inventing your own cipher. Definitely don’t do that. But it’s so much more than that.
Even if you use the right cipher, you still must use it the right way…. And I didn’t even get into initialization vectors (which is basically the same thing as a salt), or key management issues.
If you need encryption, do your best to use an existing solution. If you must write code, do some research to find a reliable library that handles as much for you as possible, or consider bringing in a consultant. If you work in a large organization, you may have a product security team – reach out to them for guidance.
Okay, so…. That was all discussing Symmetric encryption. Let’s dig into Asymmetric. Remember, symmetric uses the same key to encrypt and decrypt. Asymmetric uses one key to encrypt and another to decrypt.
In Public Private key encryption, the idea is that everyone has two keys – one which they share with everybody and one which they keep only to themselves. So, if you want to send me a message that only I can read, you ask me for my public key. I give it you, and you use that to encrypt the message you’d like to send me. That message can then only be decrypted with the private key that I keep secret. This might sound like a great solution, but it’s not so simple. Asymmetric encryption such as this is significantly slower than Symmetric. There are also message size limits. For example, with RSA encryption, a key of 2048 bits can encrypt, at most, only 245 bytes.
Which brings us to Public Private key exchange. What happens here is that the asymmetric cryptography is used to agree upon a shared symmetric encryption key and algorithm. Perhaps Alice and Bob both agree to communicate from here on out using AES and a 256 bit key. This is how HTTPS, SSL, TLS, all work. Same with your VPN or some system using IPSec.
So when people ask if these things are symmetric or asymmetric…. They’re actually both. The communication starts off as asymmetric as part of a handshake and to establish symmetric.
So, suppose that I’m Alice in this scenario. With this key exchange, I can be sure that my communication with whoever claims to be Bob is secure. But how do I know that the person claiming to be Bob really is Bob? If I’m logging into Google over HTTPS, can I be sure that the server on the other end is actually Google and not just someone saying “yep, I’m Google?”
This is where a web of trust comes into play. If you know me and trust that I am who I say I am, and your friend trusts you, then they can trust my claim of identity by proxy of you. This can be done by way of signing people’s public keys – which is sometimes done at “key signing parties”. But this doesn’t scale well….
And not everyone is as ….careful… in signing public keys. I may know you and trust you to be who you say you are…. But can I trust you to be selective in who *you* trust.
Which brings us to public key infrastructure. There are 3 main types of PKI. We briefly spoke about the Web of Trust, which is what we see with PGP encryption. There’s SPKI, but with limited time left I’d like to discuss Certificate Authorities. Certificate Authorities are, in a broad sense, very similar to the Web of Trust, except with very specific individuals being designated as a trusted third party.
A Certificate Authority is trusted by everyone in the web of trust, but not everyone is trusted by everyone else. In this graph, the black lines with arrows on both ends indicate mutual trust. The blue lines with arrows at one end indicate one-way trust. Rando #1 trusts the Certificate Authority and Alice, but the CA and Alice don’t trust Rando #1. You may ask, why do we all trust the CA? How was that trust established in the first place? Let’s think about Windows, or your cell phone – when you get these devices, they come pre-packaged with a Public Certificate from the Certificate Authority. The Certificate Authority then issues (usually sells) Certificates to parties like Alice and Bob who can prove they are who they claim to be. This “proof” is usually done by non cryptographic means. I’ve had to go to a notary before to get an SSL Cert.
I co-founded a startup a while back, and we decided to get an EV Cert. EV stands for Extended Validation. With these certificates, you see the business name in the URL bar of your browser, not just the green lock. We had to get our company’s articles of incorporation, or some other business filings from the state in which we were incorporated, and then get our corporate lawyer to draft up a letter with the exact verbiage requested by the Certificate Authority and have him sign it, as well as have him provide his Attorney License number.
When the Certificate Authority is satisfied with the proof of identity, they will issue a certificate to Alice or Bob with the Certificate Authority’s signature on it. Because all these Randos trust the Certificate Authority, and they see that Alice and Bob have certificates with the CA’s signature, they therefore trust the identity of Alice and Bob.
I’d like to change gears a little bit. So far, we’ve gone over the implementation details of encryption. Let’s talk about how to integrate it.
Let’s pretend we have a network diagram like this. It’s got a cloud. Okay, good… gotta have a cloud. We have a couple of data base servers, a web server, and application server, some firewalls, and what looks like a user's laptop. By the way, I kind of hate diagrams like this don’t tell us very much on their own. But anyway… let’s suppose someone says “Can you put a lock icon everywhere there is encryption?”
I’ve literally seen network diagrams that look like this. Diagrams like this don’t help us understand the actual security posture of this system or network.
Let’s look at a simplified network. This is a user, like you or me with a laptop connected to a Wi-Fi access point. We’re visiting some website online, and the website had a SQL database with user info in it. Maybe there’s a lock on the database because we have a check list that says “you must encrypt user data at rest”. At rest means “on disk”. Okay, done. We good, right?
From this diagram, can we tell anything about that encryption? Maybe it’s full disk encryption? Maybe it’s Transparent Database Encryption? Or Column Level Encryption? It could be a combination. From this diagram, we have no idea, and thus it really says nothing about what we’re protecting ourselves against. Let’s suppose that it is disk level encryption.
If you have Disk Level encryption turned on, then all of your data on disk is encrypted. But if you open your SQL manager and run select * from customers, you’ll see this. This is what someone logged onto the machine will see, so your disk encryption doesn’t protect you in that case. This is certainly what your application, which is running in your web server, will see. This means that if your web app is vulnerable to SQL injection attacks, then your disk encryption won’t protect you either. The disk encryption protects you from someone stealing your hard drives and copying data off them.
Let’s suppose you don’t have disk encryption, but you are running Microsoft SQL Server Enterprise, which offers Transparent Data Encryption. We don’t typically think of databases as files, but at the end of the day, they are. Transparent Data Encryption will encrypt your whole database file on disk, but it will be transparent to you as an authenticated SQL user – again, this includes your web app, which will see this plaintext.
Let’s suppose that you have Column Level Encryption enabled and have encrypted the credit card column. If you open your SQL Browser, you’ll see something like this if you select * -- you can see the credit card numbers are encrypted. This also means that an attacker running arbitrary SQL commands via SQL Injection will also get this.
1 / 89
Some slides include movie stills and other images that belong to their owners. I use them here for commentary and teaching, which I believe is fair use. If you hold the rights to any of them and would like one removed, please send me a takedown request (for example, a DMCA notice) through LinkedIn (opens in a new tab), and I’ll review it and address it accordingly.
Related reading: Everything Leaks.