crypto: Optimize hkdf_expand slightly

Remove an unneeded buffer and its memcpy, remove the now unneeded use of
l_checksum_digest_length and use l_checksum_reset instead of creating a
new l_checksum for each chunk.
This commit is contained in:
Andrew Zaborowski 2019-01-22 07:16:43 +01:00 committed by Denis Kenzior
parent 8956451b26
commit c9490ce657
1 changed files with 14 additions and 14 deletions

View File

@ -462,21 +462,19 @@ bool hkdf_expand(enum l_checksum_type type, const uint8_t *key, size_t key_len,
const char *info, size_t info_len, void *out, const char *info, size_t info_len, void *out,
size_t out_len) size_t out_len)
{ {
uint8_t t[64]; uint8_t *t = out;
size_t t_len = 0; size_t t_len = 0;
struct iovec iov[3];
struct l_checksum *hmac; struct l_checksum *hmac;
uint8_t count = 1; uint8_t count = 1;
uint8_t *out_ptr = out; uint8_t *out_ptr = out;
size_t dlen = l_checksum_digest_length(type);
if (dlen <= 0) hmac = l_checksum_new_hmac(type, key, key_len);
if (!hmac)
return false; return false;
while (out_len > 0) { while (out_len > 0) {
ssize_t ret; ssize_t ret;
struct iovec iov[3];
hmac = l_checksum_new_hmac(type, key, key_len);
iov[0].iov_base = t; iov[0].iov_base = t;
iov[0].iov_len = t_len; iov[0].iov_len = t_len;
@ -490,27 +488,29 @@ bool hkdf_expand(enum l_checksum_type type, const uint8_t *key, size_t key_len,
return false; return false;
} }
ret = l_checksum_get_digest(hmac, t, ret = l_checksum_get_digest(hmac, out_ptr, out_len);
(out_len > dlen) ? dlen : out_len);
if (ret < 0) { if (ret < 0) {
l_checksum_free(hmac); l_checksum_free(hmac);
return false; return false;
} }
memcpy(out_ptr, t, ret);
out_len -= ret;
out_ptr += ret;
/* /*
* RFC specifies that T(0) = empty string, so after the first * RFC specifies that T(0) = empty string, so after the first
* iteration we update the length for T(1)...T(N) * iteration we update the length for T(1)...T(N)
*/ */
t_len = dlen; t_len = ret;
t = out_ptr;
count++; count++;
l_checksum_free(hmac); out_len -= ret;
out_ptr += ret;
if (out_len)
l_checksum_reset(hmac);
} }
l_checksum_free(hmac);
return true; return true;
} }