aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2024-01-20 14:03:51 +0100
committerChristian Göttsche <cgzones@googlemail.com>2024-04-13 22:02:13 +0200
commitc11ccdfad1596199713f75a61f34672f7529ab73 (patch)
treee61136b16d2db9902f8373c2adfda101b9ea27ed
parent9438e084e2b318bf91c3912c0b8ff056e1835486 (diff)
downloadpam-c11ccdfad1596199713f75a61f34672f7529ab73.tar.gz
pam-c11ccdfad1596199713f75a61f34672f7529ab73.tar.bz2
pam-c11ccdfad1596199713f75a61f34672f7529ab73.zip
libpam: add helper to compare strings in constant time
Add a helper function to compare two strings for equality, that performs the same amount of operations based on the first argument, regardless of the length of the second argument, or the position of the first difference. This can be used as defense-in-depth mitigation against timing attacks of password comparisons.
-rw-r--r--libpam/include/pam_inline.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/libpam/include/pam_inline.h b/libpam/include/pam_inline.h
index cf04c9af..a5aceb8a 100644
--- a/libpam/include/pam_inline.h
+++ b/libpam/include/pam_inline.h
@@ -175,4 +175,18 @@ pam_read_passwords(int fd, int npass, char **passwords)
return i;
}
+static inline int
+pam_consttime_streq(const char *userinput, const char *secret) {
+ volatile const char *u = userinput, *s = secret;
+ volatile int ret = 0;
+
+ do {
+ ret |= *u ^ *s;
+
+ s += !!*s;
+ } while (*u++ != '\0');
+
+ return ret == 0;
+}
+
#endif /* PAM_INLINE_H */