Definitely an interesting read even for crypto dilettantes. Perhaps, especially for crypto dilettantes.
I took Rivest's Computer and Network Security class in college and the most important takeaway for me, far outstripping all of the interesting technical content, was "Don't implement crypto."
[edit: Thomas, thanks for the book recommendation below, I'll definitely grab a copy]
A place like this exists? All I can think of is Intelligensia.
Our office is in fact a few floors up from the Intelligentsia in the Monadnock building.
I assume my hash comparison function is constant time (eg. XOR(a[x],b[x])==0), rather than comparing of char-by-char.
Often you don't need confidentiality, and in those cases, HMAC can be a safer bet than a secure encrypted message format. You want to use HMAC though, not a simple hash with a secret key in it.
What's the best battle-tested library (and call) for implementing exactly that, without making any of the common mistakes?
You've made me so nervous about everything I thought was true that I did a hg revert and am looking at gpgme bindings.
You've done a good deed, I think.
http://www.matasano.com/log/962/adam-bozanovich-did-not-unco...
Keyczar and cryptlib are two libraries that offer a high-level interface that is deliberately hard to screw up. But Google Keyczar is very new (it recently had a really horrible flaw) and cryptlib is not free unless you GPL your code.
But I actually kind of agree (at the very least, it's visually noisier than the prose wall of text). There's a backstory to this; I haven't blogged in almost a year, and my most infamous blog post from before that was also a screenplay, so doing this post in this style was also an in-joke:
http://www.matasano.com/log/248/metafuzzing/
(This post is funnier than today's post).
I'm sure you're glad you know this now.
all that is a long way of saying, in spite of the visual formatting, it's a very interesting article. and personally, i think the the screenplay itself works.
Just have a server side session store and all of this cookie encryption crap just vanishes. Of course that won't help you with session fixation etc, but the post doesn't address that stuff either. Do it over TLS and you're pretty safe though.
And let's not forget the author's suggestion to pad out the cookie with 1000 bytes to make it harder to falsify. That cookie gets send with every single request. 20 images on the page, you're sending 20KB of junk up just to load the page. On a connection with slow upstream, like say ADSL, you can easily add a second or two of request latency. You might not care but some people sure do.
And come on, I read until the very end expecting to hear what "processes or threads" have to do with security, so tell us already!
Also processes vs threads was just another interview question, it was only slightly more relevant to the story than unicorns in space.
1. Use a blocks-size unique prefix (IV) for each message (random will do as well)
2. SHA-256 your entire message before encryption and add the hash value at the end to prevent tampering
3. Use AES-256 with chaining to encrypt
4. Use SHA-256 to turn a password into a key. If your key space is small harden it with hashing 1000 times or so.
5. The time your algorithm takes should be always the same (or at worst one time for success and one for failure)
6. Don't use any other symmteric crypto algorithm.
1. Has a well-known problem, which is why "Practical" suggests using a nonce.
2. SHA-256'ing a known plaintext doesn't authenticate a messge. In fact, even simply taking a secret key and appending it to your message before you SHA-256 the message isn't secure; there's a reason HMAC is as complicated as it is.
3. This whole blog post was about things that go wrong with CBC mode. For instance, nothing you wrote addressed padding checks --- btw, not strictly a "timing" attack.
4. Using a password as a crypto key is bad for reasons illustrated in the post, which is why secure keystores use random bytes. Hashing 1000 times has nothing to do with your key space.
5. This is like saying "your algorithm should be secure". Easy to say.
6. Everything we're talking about going wrong goes wrong even when you're using AES.
So, yes, I believe you don't understand what's so hard about encryption. You're obviously smart and you've taken some time with this material, and I still don't believe you'd get this right in your first fielded version.
Anyways, re padding - what if I hash the padding as well? surely an attacker would not get anything of value by playing with it?
Thanks
Just put a “for” loop around SHA-1 and run it 1000 times to generate the key; that’ll at least slow down a brute force attack. SHA-1 is lightning fast. By itself, it’s a crappy way to generate a key.
I see the purpose of this, but doesn't it also provide 999 more opportunities for a hash collision to happen? Are you so confident in the collision resistance of SHA-1 that this isn't an issue?
My (probably naive) proposal would be to keep the result of the first hash and XOR it with the result of each subsequent hash. Bad idea?
I'll try explaining my concern a little better. Suppose you have a hash function h. Let p be the probability that h(x) == h(y) for any unique x and y.
Now suppose you have some arbitrary inputs a and b.
h(a) = h(b) with probability p
h(h(a)) = h(h(b)) with probability 2p
h(h(h(a))) = h(h(h(b))) with probability 3p
etc.
The probability at each step increases because a collision is possible at each step, but a collision in a previous step guarantees a collision in the current one.With a good hash function, p should be astronomically low, so I guess that's why it isn't a problem in this case? Or am I completely off-base in my assumptions?
Is the "AES" line a tptacek catchphrase? Or is he quoting someone else?
A question to start up founders: How do you guys handle security? Do you hire consultants/pen-testers? Do you just copy+paste code from the internet? Do you use whatever framework's components for it?
Gotta love putting a lot of effort into an HN comment thread on a submission that ends up flagged. =)
Set up an encryptor:
irb> e = OpenSSL::Cipher::Cipher.new('aes-256-ofb')
=> #<OpenSSL::Cipher::Cipher:0x647100>
irb> e.key = "\x11" * 32
irb> e.iv = "\x00" * 16
A decryptor: irb> d = OpenSSL::Cipher::Cipher.new('aes-256-ofb')
=> #<OpenSSL::Cipher::Cipher:0x647120>
irb> d.decrypt
irb> d.key = "\x11" * 32
irb> d.iv = "\x00" * 16
Encrypt something: irb> ciphertext = (e << "A * 40")
=> "a\255N\211XEn\001\347$\275)\311%Ht\2356\254m\b\234z\375\311\006\335\305F\231~\201\243\236\3628w\267\3454"
Make an XOR mask: irb> mask = ("187 she wrote".to_bignum ^ ("A" * 13).to_bignum).to_rawstring
=> "pyva2)$a63.5$"
XOR it into the ciphertext: irb> new_ciphertext = (ciphertext.to_bignum ^ mask.to_bignum).to_rawstring
NOW decrypt it: irb> d << nct
=> "AAAAAAAAAAAAAAAAAAAAAAAAAAA187 she wrote"Unfortunately, there's seems to be a huge gap between the average working programmer, who often has little idea how to do security, and the security guru's who are often barely intelligible to the rest of us. :-)
Sadly, the result of most programmer's poor understanding of security is even worse than the bad crypto implementation in the article. It's often something as basic as thinking it's O.K. to store user passwords un-hashed. (http://news.ycombinator.com/item?id=628680)
But it allows everyone to concentrate on how you're managing the secure context instead of what algorithm has been shown to cause cancer in rats.