Unnecessary extra HMAC calls in parse_vals?
I'm working on porting the duo_ruby library over to Elixir, and I noticed what I believe to be a bug in the parse_vals method. Actually, I shouldn't call it a bug per se since it has no effect on functionality, but it appears to be doing three times the number of necessary crypto operations.
On line 91 of duo_web.rb, we compare hmac_sha1(key, sig) to hmac_sha1(key, u_sig) to verify that the signatures are identical. However, immediately prior to that, on line 89, we set sig equal to hmac_sha1(key, [u_prefix, u_b64].join('|')), so sig is already an HMAC-SHA1 signature using key as the key. And of course the u_sig value, which was extracted from the provided sig_response, is already an HMAC-SHA1 signature which should (if valid) have been signed with the same key. So there should be no reason to spend the CPU time to put both values through hmac_sha1() again. Or, in other words, hmac_sha1(key, sig) != hmac_sha1(key, u_sig) should be logically equivalent to sig != u_sig when both sig and u_sig are already HMAC-SHA1 signatures.
I want to be sure that my port is functionally equivalent, so I ask: Is there a purpose to running the signatures through HMAC an additional time like this, and if so, what is that purpose? If not, then maybe line 91 could be altered as above to speed up the parse_vals method?
Good catch! Comparing double HMACs helps protect against timing attacks without adding much complexity to the function. While not strictly necessary, I recommend you evaluate what side-channel attack protection makes sense for your environment. Many languages and libraries offer ready-made constant-time comparison functions.
Aha, so forcing both the generated sig and the provided u_sig through HMAC again removes the ability of an attacker to control what the first few bytes of the compared value might be. I hadn't seen HMAC used for that purpose before, but it makes sense. Excellent!
(Might I suggest a code comment indicating that the above is intentional?)
Thanks again!