Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding functions resistant to cache-timing attacks #1036

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Expand Up @@ -2657,6 +2657,7 @@ ZEND_END_ARG_INFO()
const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(constant, arginfo_constant)
PHP_FE(bin2hex, arginfo_bin2hex)
PHP_FE(bin2hex_ts, arginfo_bin2hex)
PHP_FE(hex2bin, arginfo_hex2bin)
PHP_FE(sleep, arginfo_sleep)
PHP_FE(usleep, arginfo_usleep)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_string.h
Expand Up @@ -78,6 +78,7 @@ PHP_FUNCTION(chunk_split);
PHP_FUNCTION(parse_str);
PHP_FUNCTION(str_getcsv);
PHP_FUNCTION(bin2hex);
PHP_FUNCTION(bin2hex_ts);
PHP_FUNCTION(hex2bin);
PHP_FUNCTION(similar_text);
PHP_FUNCTION(strip_tags);
Expand Down
42 changes: 42 additions & 0 deletions ext/standard/string.c
Expand Up @@ -186,6 +186,48 @@ static zend_string *php_hex2bin(const unsigned char *old, const size_t oldlen)
}
/* }}} */


/* {{{ php_ts_bin2hex
*/
static zend_string *php_bin2hex_ts(const unsigned char *old, const size_t oldlen)
{
zend_string *result;
size_t i, j;
int b = 0;

result = zend_string_safe_alloc(oldlen, 2 * sizeof(char), 0, 0);

for (i = j = 0; i < oldlen; i++) {
b = old[i] >> 4;
result->val[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
b = old[i] & 0xf;
result->val[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
}
result->val[j] = '\0';

return result;
}
/* }}} */

/* {{{ proto string ts_bin2hex(string data)
*/
PHP_FUNCTION(bin2hex_ts)
{
zend_string *result;
zend_string *data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &data) == FAILURE) {
return;
}
result = php_bin2hex_ts((unsigned char *)data->val, data->len);

if (!result) {
return;
}

RETURN_STR(result);
}
/* }}} */

#ifdef HAVE_LOCALECONV
/* {{{ localeconv_r
* glibc's localeconv is not reentrant, so lets make it so ... sorta */
Expand Down
4 changes: 4 additions & 0 deletions ext/standard/tests/strings/bin2hex.phpt
Expand Up @@ -8,7 +8,11 @@ for($i=0; $i<256; $i++) {
}
echo bin2hex($s)."\n";
echo bin2hex("abc")."\n";
echo bin2hex_ts($s)."\n";
echo bin2hex_ts("abc")."\n";
?>
--EXPECT--
000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff
616263
000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff
616263