Rather than return strlen(res_f);
as the length of the output buffer, you need to return res_f_len
.
There are three things that could happen with strlen()
in your code:
- If the HMAC message digest has a byte that equals zero,
strlen
will return a length that's shorter than the HMAC message digest. - If the HMAC message digest has no zero bytes, then
strlen
will read beyond the message digest, leading to one of two outcomes:strlen
reads through garbage in memory after the message digest, eventually stopping on a random 0.strlen
returns a value that's way too large.- If you're exceedingly lucky, the first byte after the message digest is 0, in which case
strlen
returns the actual length of the message digest.
So, only in the last scenario will you get the right value. That pretty much seems wrong.
It appears tht HMAC_Final
does update the length argument you pass to it. (See the example in the top-voted answer to this StackOverflow question.) That's why you pass a pointer-to-int. You should just return this updated value.