* [PATCH] key: Auto-free the signature hash in l_key_verify
@ 2016-10-12 19:00 Mat Martineau
2016-10-12 19:06 ` Denis Kenzior
0 siblings, 1 reply; 2+ messages in thread
From: Mat Martineau @ 2016-10-12 19:00 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 2055 bytes --]
This streamlines the existing code by using direct returns rather than a
goto and gets rid of an extra variable. While it does possibly free some
memory immediately after allocation when an invalid checksum type is
requested, this is an uncommon case.
---
ell/key.c | 29 ++++++++++-------------------
1 file changed, 10 insertions(+), 19 deletions(-)
diff --git a/ell/key.c b/ell/key.c
index a71bdc8..0222b39 100644
--- a/ell/key.c
+++ b/ell/key.c
@@ -599,14 +599,13 @@ LIB_EXPORT bool l_key_verify(struct l_key *key,
enum l_key_cipher_type kernel_cipher;
ssize_t hash_len;
uint8_t *compare_hash;
- bool success = false;
- uint8_t *sig_hash = l_malloc(len_sig);
+ L_AUTO_FREE_VAR(uint8_t *, sig_hash);
+
+ sig_hash = l_malloc(len_sig);
/* Other checksum types are not yet supported */
- if (checksum != L_CHECKSUM_NONE) {
- success = false;
- goto done;
- }
+ if (checksum != L_CHECKSUM_NONE)
+ return false;
/* The keyctl verify implementation compares the verify results
* before we get a chance to unpad it. Instead, use the *encrypt*
@@ -621,10 +620,8 @@ LIB_EXPORT bool l_key_verify(struct l_key *key,
hash_len = eds_common(key, kernel_cipher, checksum, sig, sig_hash,
len_sig, len_sig, KEYCTL_PKEY_ENCRYPT);
- if (hash_len < 0) {
- success = false;
- goto done;
- }
+ if (hash_len < 0)
+ return false;
compare_hash = sig_hash;
@@ -632,21 +629,15 @@ LIB_EXPORT bool l_key_verify(struct l_key *key,
ssize_t unpad_len;
unpad_len = unpad(sig_hash, NULL, hash_len, 0, 0x01, false);
- if (unpad_len < 0) {
- success = false;
- goto done;
- }
+ if (unpad_len < 0)
+ return false;
compare_hash += hash_len - unpad_len;
hash_len = unpad_len;
}
- success = (len_data == (size_t)hash_len) &&
+ return (len_data == (size_t)hash_len) &&
(memcmp(data, compare_hash, hash_len) == 0);
-done:
- l_free(sig_hash);
-
- return success;
}
LIB_EXPORT struct l_keyring *l_keyring_new(enum l_keyring_type type,
--
2.10.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-10-12 19:06 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-12 19:00 [PATCH] key: Auto-free the signature hash in l_key_verify Mat Martineau
2016-10-12 19:06 ` Denis Kenzior
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.