Skip to content

Commit e0cc716

Browse files
author
Yasuo Ohgaki
committed
Add php_byte_compare2() which uses https://github.com/realityking/php-src/compare/timing_attack algorithm
1 parent f8cf388 commit e0cc716

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

ext/standard/basic_functions.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
27822782
PHP_FE(str_xxhash32_compare, arginfo_str_compare)
27832783
PHP_FE(str_md5_compare, arginfo_str_compare)
27842784
PHP_FE(str_byte_compare, arginfo_str_compare)
2785+
PHP_FE(str_byte_compare2, arginfo_str_compare)
27852786
PHP_FE(str_compare, arginfo_str_compare)
27862787

27872788
#ifdef HAVE_STRCOLL

ext/standard/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32.
603603
incomplete_class.c url_scanner_ex.c ftp_fopen_wrapper.c \
604604
http_fopen_wrapper.c php_fopen_wrapper.c credits.c css.c \
605605
var_unserializer.c ftok.c sha1.c user_filters.c uuencode.c \
606-
filters.c proc_open.c streamsfuncs.c http.c password.c)
606+
filters.c proc_open.c streamsfuncs.c http.c password.c xxhash.c siphash.c)
607607

608608
PHP_ADD_MAKEFILE_FRAGMENT
609609
PHP_INSTALL_HEADERS([ext/standard/])

ext/standard/php_string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ PHP_FUNCTION(str_siphash_compare);
9797
PHP_FUNCTION(str_xxhash32_compare);
9898
PHP_FUNCTION(str_md5_compare);
9999
PHP_FUNCTION(str_byte_compare);
100+
PHP_FUNCTION(str_byte_compare2);
100101
PHP_FUNCTION(str_compare);
101102
#ifdef HAVE_STRCOLL
102103
PHP_FUNCTION(strcoll);

ext/standard/string.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5714,6 +5714,7 @@ PHPAPI int php_byte_compare(const void *b1, const void *b2, size_t n) /* {{{ */
57145714
}
57155715
/* }}} */
57165716

5717+
57175718
/* {{{ proto bool str_compare(string str1, string str2)
57185719
Timing safe string compare */
57195720
PHP_FUNCTION(str_byte_compare)
@@ -5737,6 +5738,44 @@ PHP_FUNCTION(str_byte_compare)
57375738
}
57385739
/* }}} */
57395740

5741+
5742+
/* Timing safe compare */
5743+
PHPAPI int php_byte_compare2(const void *b1, size_t b1_len, const void *b2, size_t b2_len) /* {{{ */
5744+
{
5745+
const unsigned char *p1 = b1, *p2 = b2;
5746+
int ret = b1_len - b2_len;
5747+
int mod_len = MAX(b1_len, 1);
5748+
int n;
5749+
5750+
for (n = 0; n < b2_len ; n++) {
5751+
ret |= p1[n % mod_len] ^ p2[n];
5752+
}
5753+
return (ret == 0);
5754+
}
5755+
/* }}} */
5756+
5757+
/* {{{ proto bool str_compare2(string str1, string str2)
5758+
Timing safe string compare */
5759+
PHP_FUNCTION(str_byte_compare2)
5760+
{
5761+
zval *s1, *s2;
5762+
size_t mod_len;
5763+
5764+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &s1, &s2) == FAILURE) {
5765+
RETURN_FALSE;
5766+
}
5767+
5768+
if (Z_TYPE_P(s1) != IS_STRING || Z_TYPE_P(s2) != IS_STRING) {
5769+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Paremeters must be string");
5770+
RETURN_FALSE;
5771+
}
5772+
5773+
RETURN_BOOL(php_byte_compare2(Z_STRVAL_P(s1), Z_STRLEN_P(s1),
5774+
Z_STRVAL_P(s2), Z_STRLEN_P(s2)));
5775+
}
5776+
/* }}} */
5777+
5778+
57405779
/* {{{ proto bool str_compare(string str1, string str2)
57415780
strncmp string compare */
57425781
PHP_FUNCTION(str_compare)

0 commit comments

Comments
 (0)