diff --git a/compat/timingsafe_memcmp.c b/compat/timingsafe_memcmp.c index bb210a3..d6988c9 100644 --- a/compat/timingsafe_memcmp.c +++ b/compat/timingsafe_memcmp.c @@ -1,46 +1,13 @@ -/* $OpenBSD: timingsafe_memcmp.c,v 1.2 2015/08/31 02:53:57 guenther Exp $ */ -/* - * Copyright (c) 2014 Google Inc. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ +#include -#include -#include - -int -timingsafe_memcmp(const void *b1, const void *b2, size_t len) +int timingsafe_memcmp(const void *s1, const void *s2, size_t n) { - const unsigned char *p1 = b1, *p2 = b2; - size_t i; - int res = 0, done = 0; - - for (i = 0; i < len; i++) { - /* lt is -1 if p1[i] < p2[i]; else 0. */ - int lt = (p1[i] - p2[i]) >> CHAR_BIT; - - /* gt is -1 if p1[i] > p2[i]; else 0. */ - int gt = (p2[i] - p1[i]) >> CHAR_BIT; - - /* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */ - int cmp = lt - gt; - - /* set res = cmp if !done. */ - res |= cmp & ~done; + unsigned int i, ret = -1; + const unsigned char *b1 = s1; + const unsigned char *b2 = s2; - /* set done if p1[i] != p2[i]. */ - done |= lt | gt; - } + for(i = 0; i < n; i++) + ret |= (b1[i] ^ b2[i]); - return (res); + return ret; } diff --git a/login.c b/login.c index 051b6b6..cbf8859 100644 --- a/login.c +++ b/login.c @@ -169,8 +169,7 @@ int main(int argc, char **argv) /* TODO: shadow and locked account support */ if(!(*pwd->pw_passwd == '\0' && !strlen(pw))) { char *pw_encrypted = crypt(pw, pwd->pw_passwd); - /* if(timingsafe_memcmp(pw_encrypted, pwd->pw_passwd, strlen(pw_encrypted))) { */ - if(memcmp(pw_encrypted, pwd->pw_passwd, strlen(pw_encrypted)) != 0) { + if(timingsafe_memcmp(pw_encrypted, pwd->pw_passwd, strlen(pw_encrypted)) != 0) { puts("Login incorrect."); explicit_bzero(pw, strlen(pw)); return 1;