27 points · by wilsonfiifi · 11y ago · 34 comments
Also:
* Do not use string/buffer comparison functions to verify HMAC. In incredibly common error.
* Do not use human-intelligible strings as HMAC keys. HMAC keys are like AES keys: fully random; they should come from /dev/urandom.
* Do not embed HMAC keys in your source code!
Ultimately: I'm not sure I understand the impulse behind pages like this. It's clear that the authors aren't domain experts (that's fine; they're probably better programmers than the crypto domain experts are). They know they aren't getting everything perfect. In fact: they know some of the examples on their own page have flaws, because they were corrected about timing attacks and only fixed Python.
Crypto bugs in your own code are one thing, but propagating crypto bugs to other people's code seems much worse.
I've been using pwgen -s: based on /dev/urandom, but typeable. My understanding was that it first took a hash of the key, so as long as the total entropy was high it was fine for it to be ASCII.
Where else would you put it? A separate configuration file is still your code (or is that what you meant?)
But don't check the key into source control.
exports.passwdHmacKey = 'awaibaeSeingoo4phahjeekee6Ufetev';
Put secrets.js in .gitignore, and copy it where needed manually.
(Just using your comment to riff now:)
As long as you're not (a) checking it into VCS and (b) having every developer hold a copy on their laptop, whatever you're doing is fine.
What this site does, though, means that any time someone loses their laptop, or really just leaves their laptop unattended somewhere unsafe, everything needs to be reprovisioned. In fact: if you don't have audit controls (who's used which key when), technically, any time that happens, you need to disclose that it happened to your users. A nightmare.
Or even better perf/safety wise:
type Payload struct {
Name string `json:"name"`
Category string `json:"category"`
Action string `json:"action"`
Where string `json:"where"`
Timestamp int64 `json:"timestamp,string"`
}
payload := Payload{
Name: "joe smith",
Category: "people",
Action: "transport"
Where: "pluto",
Timestamp: time.Now().Unix(),
}
jsonBytes, err := json.Marshal(&payload)
This code doesn't allocate heap for payload, is much faster and typesafe...P.S. And could be shorter, but I was trying to be clear.
The original reasoning behind this post was to provide a single reference for signature generation and verification in some common languages -- something I struggled to locate myself. Admittedly, I should've provided warnings about using simple and hardcoded keys in the examples, which were done that way for readability.
While there is still a lot of debate about the ability to perform true constant-time comparisons in many of these languages (https://bugs.python.org/issue15061, https://github.com/joyent/node/issues/8560) I agree that for those who would be otherwise using the unsafe string comparisons, the benefits certainly outweigh the slightly added complexity.
The updated gists are available in the post to anyone with comments or improvements.
Cheers!
return new Buffer(sig).toString('base64') == signature;
PHP example: return hash_hmac("sha512", $string_to_verify, $shared_secret) == $signature;
Yeah, like fdomig said: timing attacks. The Python example included a mitigation. PHP includes hash_equals() and a constant time comparison in Javascript isn't difficult to write.Possibly also, the Ruby example:
return OpenSSL::HMAC.digest('sha512', shared_secret, string_to_verify) == signature
(I don't write Ruby so I don't know if this is overloadable somewhere.)Ruby allows overriding the == operator[1], but OpenSSL::HMAC.digest returns an instance of String[2], rather than returning a special subclass of string or some other kind of special HMAC-representing class overloading ==.
[1] http://docs.ruby-lang.org/en/2.2.0/syntax/methods_rdoc.html#...
[2] http://ruby-doc.org/stdlib-2.0.0/libdoc/openssl/rdoc/OpenSSL...
(And maybe Golang?)
Unnecessarily so too, Python provides hmac.compare_digests since Python 3.3 and it was backported to Python 2.7.7, which was released almost a year ago.
In theory openssl could return a String-like object with an overridden ==, but it doesn't.
36D5F4EA999342FED17D7488CB260FC92926 36D5F4EA999342FED18D7488CB260FC92926
The code would compare only up to the difference and return, indicating through the time spent analyzing the HMAC how many characters are the same. The attacker can then work the HMAC like they would a combination lock, till they reproduce the key used.
That's why a constant time comparison is so important: it leaks far less information.