|
|
@ -60,9 +60,9 @@ int hkdf(SHAversion whichSha, |
|
|
uint8_t okm[], size_t okm_len) |
|
|
uint8_t okm[], size_t okm_len) |
|
|
{ |
|
|
{ |
|
|
uint8_t prk[USHAMaxHashSize]; |
|
|
uint8_t prk[USHAMaxHashSize]; |
|
|
return hkdfExtract(whichSha, salt, salt_len, ikm, ikm_len, prk) || |
|
|
int ret; |
|
|
hkdfExpand(whichSha, prk, USHAHashSize(whichSha), info, |
|
|
if ((ret=hkdfExtract(whichSha, salt, salt_len, ikm, ikm_len, prk))) return ret; |
|
|
info_len, okm, okm_len); |
|
|
return hkdfExpand(whichSha, prk, USHAHashSize(whichSha), info, info_len, okm, okm_len); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/*
|
|
|
/*
|
|
|
@ -103,9 +103,6 @@ int hkdfExtract(SHAversion whichSha, |
|
|
salt_len = USHAHashSize(whichSha); |
|
|
salt_len = USHAHashSize(whichSha); |
|
|
memset(nullSalt, '\0', salt_len); |
|
|
memset(nullSalt, '\0', salt_len); |
|
|
} |
|
|
} |
|
|
else if (salt_len < 0) { |
|
|
|
|
|
return shaBadParam; |
|
|
|
|
|
} |
|
|
|
|
|
return hmac(whichSha, ikm, ikm_len, salt, salt_len, prk); |
|
|
return hmac(whichSha, ikm, ikm_len, salt, salt_len, prk); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -149,16 +146,13 @@ int hkdfExpand(SHAversion whichSha, const uint8_t prk[], size_t prk_len, |
|
|
size_t hash_len, N; |
|
|
size_t hash_len, N; |
|
|
unsigned char T[USHAMaxHashSize]; |
|
|
unsigned char T[USHAMaxHashSize]; |
|
|
size_t Tlen, where, i; |
|
|
size_t Tlen, where, i; |
|
|
|
|
|
int ret; |
|
|
|
|
|
|
|
|
if (info == 0) { |
|
|
if (info == 0) { |
|
|
info = (const unsigned char *)""; |
|
|
info = (const unsigned char *)""; |
|
|
info_len = 0; |
|
|
info_len = 0; |
|
|
} |
|
|
} |
|
|
else if (info_len < 0) { |
|
|
if (!okm || !okm_len) return shaBadParam; |
|
|
return shaBadParam; |
|
|
|
|
|
} |
|
|
|
|
|
if (okm_len <= 0) return shaBadParam; |
|
|
|
|
|
if (!okm) return shaBadParam; |
|
|
|
|
|
|
|
|
|
|
|
hash_len = USHAHashSize(whichSha); |
|
|
hash_len = USHAHashSize(whichSha); |
|
|
if (prk_len < hash_len) return shaBadParam; |
|
|
if (prk_len < hash_len) return shaBadParam; |
|
|
@ -171,12 +165,11 @@ int hkdfExpand(SHAversion whichSha, const uint8_t prk[], size_t prk_len, |
|
|
for (i = 1; i <= N; i++) { |
|
|
for (i = 1; i <= N; i++) { |
|
|
HMACContext context; |
|
|
HMACContext context; |
|
|
unsigned char c = i; |
|
|
unsigned char c = i; |
|
|
int ret = hmacReset(&context, whichSha, prk, prk_len) || |
|
|
if ((ret=hmacReset(&context, whichSha, prk, prk_len))) return ret; |
|
|
hmacInput(&context, T, Tlen) || |
|
|
if ((ret=hmacInput(&context, T, Tlen))) return ret; |
|
|
hmacInput(&context, info, info_len) || |
|
|
if ((ret=hmacInput(&context, info, info_len))) return ret; |
|
|
hmacInput(&context, &c, 1) || |
|
|
if ((ret=hmacInput(&context, &c, 1))) return ret; |
|
|
hmacResult(&context, T); |
|
|
if ((ret=hmacResult(&context, T))) return ret; |
|
|
if (ret != shaSuccess) return ret; |
|
|
|
|
|
memcpy(okm + where, T, |
|
|
memcpy(okm + where, T, |
|
|
(i != N) ? hash_len : (okm_len - where)); |
|
|
(i != N) ? hash_len : (okm_len - where)); |
|
|
where += hash_len; |
|
|
where += hash_len; |
|
|
@ -328,9 +321,8 @@ int hkdfResult(HKDFContext *context, |
|
|
if (!okm) return context->Corrupted = shaBadParam; |
|
|
if (!okm) return context->Corrupted = shaBadParam; |
|
|
if (!prk) prk = prkbuf; |
|
|
if (!prk) prk = prkbuf; |
|
|
|
|
|
|
|
|
ret = hmacResult(&context->hmacContext, prk) || |
|
|
if (!(ret = hmacResult(&context->hmacContext, prk))) |
|
|
hkdfExpand(context->whichSha, prk, context->hashSize, info, |
|
|
ret = hkdfExpand(context->whichSha, prk, context->hashSize, info, info_len, okm, okm_len); |
|
|
info_len, okm, okm_len); |
|
|
|
|
|
context->Computed = 1; |
|
|
context->Computed = 1; |
|
|
return context->Corrupted = ret; |
|
|
return context->Corrupted = ret; |
|
|
} |
|
|
} |
|
|
|