I was reading the source code of several BCrypt implementations and found that two common c implementations have a difference in their base64 encoding of for the salt.
What is the effect, if any, of the differences on line 18 and line 22 of the two base64 encoding implementations below.
Original implementation
/**
* Original BCrypt implementation
* http://mail-index.netbsd.org/tech-crypto/2002/05/24/msg000204.html
*/
function encodeBase64_a($input) {
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$i = 0;
$output = '';
while (true) {
$c1 = ord($input{$i++});
$output .= $itoa64{$c1 >> 2};
$c1 = ($c1 & 0x03) << 4;
if ($i >= 16) {
$output .= $itoa64{$c1};
break;
}
$c2 = ord($input{$i++});
$c1 |= $c2 >> 4 & 0x0f;
$output .= $itoa64{$c1};
$c1 = ($c2 & 0x0f) << 2;
$c2 = ord($input{$i++});
$c1 |= $c2 >> 6 & 0x03;
$output .= $itoa64{$c1};
$output .= $itoa64{$c2 & 0x3f};
}
return $output;
}
OpenWall implementation
/**
* OpenWall implementation
* crypt_blowfish-1.2
* source: http://www.openwall.com/crypt/
*/
function encodeBase64_b($input) {
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$i = 0;
$output = '';
while (true) {
$c1 = ord($input{$i++});
$output .= $itoa64{$c1 >> 2};
$c1 = ($c1 & 0x03) << 4;
if ($i >= 16) {
$output .= $itoa64{$c1};
break;
}
$c2 = ord($input{$i++});
$c1 |= $c2 >> 4;
$output .= $itoa64{$c1};
$c1 = ($c2 & 0x0f) << 2;
$c2 = ord($input{$i++});
$c1 |= $c2 >> 6;
$output .= $itoa64{$c1};
$output .= $itoa64{$c2 & 0x3f};
}
return $output;
}
Differences side by side:
+------+-------------------------+------------------+
| Line | Niels Provos | OpenWall |
+------+-------------------------+------------------+
| 18 | $c1 |= $c2 >> 4 & 0x0f; | $c1 |= $c2 >> 4; |
| | | |
| 22 | $c1 |= $c2 >> 6 & 0x03; | $c1 |= $c2 >> 6; |
+------+-------------------------+------------------+
Do the differences have any effect? do they affect compatibility?
