diff options
author | Joseph Sutton <josephsutton@catalyst.net.nz> | 2022-02-17 15:35:42 +1300 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2022-06-09 22:49:29 +0000 |
commit | ae6634c78774d2368e815dea650ba71650dd1861 (patch) | |
tree | bf361a9acf1e5eb9595b25adea734d303bad31c4 /lib/util/data_blob.c | |
parent | 87f68500ed651f393e2fc6c514ab08b561a60a9b (diff) | |
download | samba-ae6634c78774d2368e815dea650ba71650dd1861.tar.gz |
auth: Use constant-time memcmp when comparing sensitive buffers
This helps to avoid timing attacks.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15010
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'lib/util/data_blob.c')
-rw-r--r-- | lib/util/data_blob.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/util/data_blob.c b/lib/util/data_blob.c index da1730dccf5..1b05809119d 100644 --- a/lib/util/data_blob.c +++ b/lib/util/data_blob.c @@ -21,6 +21,7 @@ #include "replace.h" #include "attr.h" #include "data_blob.h" +#include "lib/util/samba_util.h" const DATA_BLOB data_blob_null = { NULL, 0 }; @@ -130,6 +131,29 @@ _PUBLIC_ int data_blob_cmp(const DATA_BLOB *d1, const DATA_BLOB *d2) } /** +check if two data blobs are equal, where the time taken should not depend on the +contents of either blob. +**/ +_PUBLIC_ int data_blob_cmp_const_time(const DATA_BLOB *d1, const DATA_BLOB *d2) +{ + int ret; + if (d1->data == NULL && d2->data != NULL) { + return -1; + } + if (d1->data != NULL && d2->data == NULL) { + return 1; + } + if (d1->data == d2->data) { + return d1->length - d2->length; + } + ret = memcmp_const_time(d1->data, d2->data, MIN(d1->length, d2->length)); + if (ret == 0) { + return d1->length - d2->length; + } + return ret; +} + +/** print the data_blob as hex string **/ _PUBLIC_ char *data_blob_hex_string_lower(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob) |