231 points · by lxm · 4y ago · 89 comments
And also good that they mention right from the start that you shouldn't use your (or their) self-made cryptography in production (although they could have emphasized it a bit more).
Absolutely, and that was the very first thing I thought when I saw the title. For some reason I've got the "don't roll your own crypto" commandment heavily ingrained into my brain (even though I've never been in any context where I might have tried), together with "don't let the frying pan handle stick out over the edge of the cooker in case a kid grabs it" (even though I have no kids, nor nephews etc), as well as trigger discipline and "every gun is loaded" (even though I've never touched a gun in my life and have never lived in the USA).
I did some deep dive into AES and RSA at some point in a distant past, it was a learning experience that to this day allows me to make much better decisions when choosing algorithms.
It's essentially a waste of your time. Because of Schneier's Law: "Any person can invent a security system so clever that she or he can't think of how to break it".
The thing that you might learn from, if you put the work in, would be breaking other people's stuff. Ideally you would find something that's actually in use and vulnerable enough that with some time you'll be able to break it, but that's tricky.
So, if you're a programmer try something like "Cryptopals": https://cryptopals.com/
I think Thomas Ptacek is wrong about a bunch of stuff when it comes to security (there's presumably some way to find HN back-and-forth between us if you decide you care about that), but he wasn't wrong about the Cryptopals exercises. By the time you're doing Set 2 exercises this is stuff real people, who were getting paid and thought they knew what they were doing, got wrong.
The real issue is that cryptography is an adversarial field. The human / systems / software aspects are the hard parts. The crypto you wrote yourself doesn't fail because you got the crypto wrong but because your adversary cheats. (I eventually lost interest in cryptography due to the adversarial nature.)
What you probably shouldn't do is try to come up with your own encryption scheme/mode of operation/padding scheme and think you've learned something valuable. By all means, try that as well, but know that you've now entered the really dangerous territory.
No. If you want to "learn from it", the first thing you should do is buy a copy of Bruce Schneier's Applied Cryptography.
Just reading (and fully understanding !) that book will alone put you in a position where you already know more about cryptography than 90% of other people.
If after that you still want to play around with rolling your own crypto, then fine, go for it. But be aware you are very much making your own bed and should be prepared to lie in the inevitable mucky consequences.
For the rest of us, frankly, no matter what your choice of programming language is, there will be at least one if not many more well respected long-established crypto libraries. Most people should just do themselves a favour and just use those libraries.
As the saying goes ... crypto is hard. A tiny, easily overlooked, mistake in your crypto code can have major consequences.
The standards are either the outcome of competitions the US government (through NIST) has held over the years, or they're de-facto standards because they're provably better for certain use cases than other algorithms.
May be true, but not exactly a great message.
In general, I think DRYAC is excellent advice, and that we should apply similar reasoning to unsafe programming languages. And that is not to say that people shouldn't use them; only that we, as a community, should be making ourselves more half-ass-resistant.
It's more along the lines of, "you absolutely can understand this if you try, buy trying will take years of your life to learn and apply all attack vectors and their mitigations."
The admonition not to roll your own crypto isn't so that people don't look at crypto. Take a look, fiddle, and hack all you want - just not in production. The saying comes from crypto people noticing avoidable security issues that get created many times people have touched or used crypto code - because even 100% correct crypto code is often also broken in practice.
Do roll your own crypto if you have nation state actors as potential attackers. Make life hard for them.
:)
The author might be surprised at how often someone's random "learn 2 crypto" ends up in use in production. Kudos for the warnings, though I doubt that actually accomplish much: People copying and pasting code usually have tremendous tunnel vision.
See https://cr.yp.to/antiforgery/cachetiming-20050414.pdf
Here's an implementation that doesn't use tables: https://github.com/openbsd/src/blob/master/sys/crypto/aes.c
A constant time implementation has to avoid tables & compute the values directly (and slowly) or take special care to ensure that all of the table is hit/cached on each access.
For example, by using an oblivious read: Access each row, AND it with a mask that is either all 1s or all 0s depending on if its the row you want (which you must set without branching) and OR all the results together.
Very slow.
If you are going to dabble in cryptography it might be wise to approach it via the theory, work in the domain symbolically, and generate code that provably "works as designed" so that you can focus on attacks on the design in a symbolic domain.
You then have a secure element in a security eco-system ... which leaves the issue of wether the end-points of that element are insecure or indeed whether the entire security locale can be avoided [pi]
[-1] https://en.wikipedia.org/wiki/Magma_(computer_algebra_system...
[pi] insert picture of padlocked gate with no fence in otherwise open field.
The article and code is more about understanding the math, which is great and fundamental, but it is not really about the implementation side of things.
https://www.intel.com/content/www/us/en/docs/intrinsics-guid...
https://developer.arm.com/documentation/ddi0596/2020-12/SIMD...
https://github.com/DavidBuchanan314/aes-playground/blob/mast...
(I left comments quoting the spec as much as possible, so it should be possible to map it back onto the spec, for anyone interested)
https://en.wikipedia.org/wiki/Vancouver_Stock_Exchange#Round...
The bug caused the index to creep up to twice the real index.
Constructions such as AES-CMAC, but also recent ones such as AEGIS only require the AES round function.
I'd be interested in doing the same with some post-quantum ciphers, especially since two of them have been officially adopted by the NIST.
From a binary trust perspective, this is maybe a good way of ensuring that you're running the code you really think you are - but really you just kick the can down the road. You didn't build the hardware, and thus you don't know it's doing with your data once the instructions start executing. Is it storing them off to a side-buffer? Is the CPU detecting AES-like behaviour and triggering some surreptitious path? Who knows. This is where projects like precursor (https://player.vimeo.com/video/677854277?h=8ad58eece9) are really interesting. To be really, super sure that your code is running as expected, you have to build the world from the ground up.
I'm stealing that!
Then I gave up and implemented OCB(3) mode. And I was happy to see that it is now free for all. The parents are lapsed.
Also, "Software AES bad"