svn commit: r1936803 - apr/apr/trunk/crypto
[email protected] Mon, 03 Aug 2026 12:07:22 -0000
| Newsgroups | gmane.comp.apache.apr.cvs |
|---|---|
| Message-ID | <178575884228.1819518.18333136961582673921@svn03-he-fi> |
Author: covener
Date: Mon Aug 3 12:07:22 2026
New Revision: 1936803
Log:
use timing safe comparison
Submitted By: ylavic
Reviewed By: ylavic, rpluem, covener
Modified:
apr/apr/trunk/crypto/apr_crypto.c
apr/apr/trunk/crypto/apr_passwd.c
Modified: apr/apr/trunk/crypto/apr_crypto.c
==============================================================================
--- apr/apr/trunk/crypto/apr_crypto.c Mon Aug 3 11:45:36 2026 (r1936802)
+++ apr/apr/trunk/crypto/apr_crypto.c Mon Aug 3 12:07:22 2026 (r1936803)
@@ -149,16 +149,7 @@ APR_DECLARE(apr_status_t) apr_crypto_mem
APR_DECLARE(int) apr_crypto_equals(const void *buf1, const void *buf2,
apr_size_t size)
{
- const unsigned char *p1 = buf1;
- const unsigned char *p2 = buf2;
- unsigned char diff = 0;
- apr_size_t i;
-
- for (i = 0; i < size; ++i) {
- diff |= p1[i] ^ p2[i];
- }
-
- return 1 & ((diff - 1) >> 8);
+ return apr_memeq_timingsafe(buf1, buf2, size);
}
APR_DECLARE(apr_crypto_key_rec_t *) apr_crypto_key_rec_make(
Modified: apr/apr/trunk/crypto/apr_passwd.c
==============================================================================
--- apr/apr/trunk/crypto/apr_passwd.c Mon Aug 3 11:45:36 2026 (r1936802)
+++ apr/apr/trunk/crypto/apr_passwd.c Mon Aug 3 12:07:22 2026 (r1936803)
@@ -112,28 +112,33 @@ APR_DECLARE(apr_status_t) apr_password_v
#if !CRYPT_MISSING
char *crypt_pw;
#endif
- if (hash[0] == '$'
- && hash[1] == '2'
- && (hash[2] == 'a' || hash[2] == 'y')
- && hash[3] == '$') {
+
+ if ((apr_strneq_timingsafe(hash, "$2a$", 4) | /* test both */
+ apr_strneq_timingsafe(hash, "$2y$", 4))) {
+ /*
+ * The hash was created using [apr_]bcrypt encoding.
+ */
if (_crypt_blowfish_rn(passwd, hash, sample, sizeof(sample)) == NULL)
return APR_FROM_OS_ERROR(errno);
}
- else if (!strncmp(hash, apr1_id, strlen(apr1_id))) {
+ else if (apr_strneq_timingsafe(hash, apr1_id, strlen(apr1_id))) {
/*
* The hash was created using our custom algorithm.
*/
apr_md5_encode(passwd, hash, sample, sizeof(sample));
}
- else if (!strncmp(hash, APR_SHA1PW_ID, APR_SHA1PW_IDLEN)) {
- apr_sha1_base64(passwd, (int)strlen(passwd), sample);
+ else if (apr_strneq_timingsafe(hash, APR_SHA1PW_ID, APR_SHA1PW_IDLEN)) {
+ /*
+ * The hash is a (naked) SHA1.
+ */
+ apr_sha1_base64(passwd, (int)strlen(passwd), sample);
}
else {
/*
* It's not our algorithm, so feed it to crypt() if possible.
*/
#if CRYPT_MISSING
- return (strcmp(passwd, hash) == 0) ? APR_SUCCESS : APR_EMISMATCH;
+ return apr_streq_timingsafe(hash, passwd) ? APR_SUCCESS : APR_EMISMATCH;
#elif defined(CRYPT_R_CRYPTD)
apr_status_t rv;
CRYPTD *buffer = malloc(sizeof(*buffer));
@@ -144,7 +149,7 @@ APR_DECLARE(apr_status_t) apr_password_v
if (!crypt_pw)
rv = APR_EMISMATCH;
else
- rv = (strcmp(crypt_pw, hash) == 0) ? APR_SUCCESS : APR_EMISMATCH;
+ rv = apr_streq_timingsafe(hash, crypt_pw) ? APR_SUCCESS : APR_EMISMATCH;
free(buffer);
return rv;
#elif defined(CRYPT_R_STRUCT_CRYPT_DATA)
@@ -175,7 +180,7 @@ APR_DECLARE(apr_status_t) apr_password_v
if (!crypt_pw)
rv = APR_EMISMATCH;
else
- rv = (strcmp(crypt_pw, hash) == 0) ? APR_SUCCESS : APR_EMISMATCH;
+ rv = apr_streq_timingsafe(hash, crypt_pw) ? APR_SUCCESS : APR_EMISMATCH;
free(buffer);
return rv;
#else
@@ -199,14 +204,14 @@ APR_DECLARE(apr_status_t) apr_password_v
rv = APR_EMISMATCH;
}
else {
- rv = (strcmp(crypt_pw, hash) == 0) ? APR_SUCCESS : APR_EMISMATCH;
+ rv = apr_streq_timingsafe(hash, crypt_pw) ? APR_SUCCESS : APR_EMISMATCH;
}
crypt_mutex_unlock();
return rv;
}
#endif
}
- return (strcmp(sample, hash) == 0) ? APR_SUCCESS : APR_EMISMATCH;
+ return apr_streq_timingsafe(hash, sample) ? APR_SUCCESS : APR_EMISMATCH;
}
static const char * const bcrypt_id = "$2y$";