Fix the PRF function when compiling without OpenSSL.
This commit is contained in:
parent
3c54765bcd
commit
9ca1750245
1 changed files with 12 additions and 10 deletions
|
@ -28,36 +28,38 @@ static void memxor(char *buf, char c, size_t len) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static const size_t mdlen = 64;
|
static const size_t mdlen = 64;
|
||||||
|
static const size_t blklen = 128;
|
||||||
|
|
||||||
static bool hmac_sha512(const char *key, size_t keylen, const char *msg, size_t msglen, char *out) {
|
static bool hmac_sha512(const char *key, size_t keylen, const char *msg, size_t msglen, char *out) {
|
||||||
char tmp[2 * mdlen];
|
char tmp[blklen + mdlen];
|
||||||
sha512_context md;
|
sha512_context md;
|
||||||
|
|
||||||
if(keylen <= mdlen) {
|
if(keylen <= blklen) {
|
||||||
memcpy(tmp, key, keylen);
|
memcpy(tmp, key, keylen);
|
||||||
memset(tmp + keylen, 0, mdlen - keylen);
|
memset(tmp + keylen, 0, blklen - keylen);
|
||||||
} else {
|
} else {
|
||||||
if(sha512(key, keylen, tmp) != 0)
|
if(sha512(key, keylen, tmp) != 0)
|
||||||
return false;
|
return false;
|
||||||
|
memset(tmp + mdlen, 0, blklen - mdlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sha512_init(&md) != 0)
|
if(sha512_init(&md) != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// ipad
|
// ipad
|
||||||
memxor(tmp, 0x36, mdlen);
|
memxor(tmp, 0x36, blklen);
|
||||||
if(sha512_update(&md, tmp, mdlen) != 0)
|
if(sha512_update(&md, tmp, blklen) != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// message
|
// message
|
||||||
if(sha512_update(&md, msg, msglen) != 0)
|
if(sha512_update(&md, msg, msglen) != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if(sha512_final(&md, tmp + mdlen) != 0)
|
if(sha512_final(&md, tmp + blklen) != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// opad
|
// opad
|
||||||
memxor(tmp, 0x36 ^ 0x5c, mdlen);
|
memxor(tmp, 0x36 ^ 0x5c, blklen);
|
||||||
if(sha512(tmp, sizeof tmp, out) != 0)
|
if(sha512(tmp, sizeof tmp, out) != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -82,17 +84,17 @@ bool prf(const char *secret, size_t secretlen, char *seed, size_t seedlen, char
|
||||||
|
|
||||||
while(outlen > 0) {
|
while(outlen > 0) {
|
||||||
/* Inner HMAC */
|
/* Inner HMAC */
|
||||||
if(!hmac_sha512(data, sizeof data, secret, secretlen, data))
|
if(!hmac_sha512(secret, secretlen, data, sizeof data, data))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* Outer HMAC */
|
/* Outer HMAC */
|
||||||
if(outlen >= mdlen) {
|
if(outlen >= mdlen) {
|
||||||
if(!hmac_sha512(data, sizeof data, secret, secretlen, out))
|
if(!hmac_sha512(secret, secretlen, data, sizeof data, out))
|
||||||
return false;
|
return false;
|
||||||
out += mdlen;
|
out += mdlen;
|
||||||
outlen -= mdlen;
|
outlen -= mdlen;
|
||||||
} else {
|
} else {
|
||||||
if(!hmac_sha512(data, sizeof data, secret, secretlen, hash))
|
if(!hmac_sha512(secret, secretlen, data, sizeof data, hash))
|
||||||
return false;
|
return false;
|
||||||
memcpy(out, hash, outlen);
|
memcpy(out, hash, outlen);
|
||||||
out += outlen;
|
out += outlen;
|
||||||
|
|
Loading…
Reference in a new issue