Skip to content

Update safe_bcmp.c #18739

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 38 additions & 31 deletions main/safe_bcmp.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: David Carlier <[email protected]> |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: David Carlier <[email protected]> |
+----------------------------------------------------------------------+
*/

#include "php.h"

#include <string.h>

/*
* Returns 0 if both inputs match, non-zero if they don't.
* Returns -1 early if inputs do not have the same lengths.
*
/**
* Constant-time binary comparison of two zend_strings.
*
* @param a First string to compare
* @param b Second string to compare
* @return 0 if strings are identical, -1 if lengths differ, non-zero otherwise
*
* @note This is security-sensitive code. Timing attacks are mitigated by:
* - Using volatile to prevent compiler optimizations
* - Constant-time comparison regardless of input
* - Complete length comparison before content comparison
*/
PHPAPI int php_safe_bcmp(const zend_string *a, const zend_string *b)
{
const volatile unsigned char *ua = (const volatile unsigned char *)ZSTR_VAL(a);
const volatile unsigned char *ub = (const volatile unsigned char *)ZSTR_VAL(b);
size_t i = 0;
int r = 0;
const volatile unsigned char *ua = (const volatile unsigned char *)ZSTR_VAL(a);
const volatile unsigned char *ub = (const volatile unsigned char *)ZSTR_VAL(b);
const size_t len_a = ZSTR_LEN(a);
const size_t len_b = ZSTR_LEN(b);
int r = 0;

if (ZSTR_LEN(a) != ZSTR_LEN(b)) {
return -1;
}
// Early length check (safe as lengths are not secret)
if (len_a != len_b) {
return -1;
}

/* This is security sensitive code. Do not optimize this for speed. */
while (i < ZSTR_LEN(a)) {
r |= ua[i] ^ ub[i];
++i;
}
// Constant-time comparison
for (size_t i = 0; i < len_a; ++i) {
r |= ua[i] ^ ub[i];
}

return r;
return r;
}