Bug #70777 [NEW]: cfb or ncfb gives wrong result
| From: | letsgolee at naver dot com | Date: | Fri, 23 Oct 2015 14:14:46 +0000 |
| Subject: | Bug #70777 [NEW]: cfb or ncfb gives wrong result | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-196772@lists.php.net to get a copy of this message | ||
From: letsgolee at naver dot com
Operating system: any
PHP version: 5.4.45
Package: mcrypt related
Bug Type: Bug
Bug description:cfb or ncfb gives wrong result
Description:
------------
the test vectors come from
http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf.
the plaintexts are all 128 bits so ncfb should be matched even though
cfb could give different results.
(I'm just wondering what is the difference between cfb and ncfb when the
bits is the same with the cipher block size.)
the results shows neither cfb or ncfb gives the right result.
only one time ncfb gives the right one.
Test script:
---------------
//
// ECB, CBC, CFB, OFB, CTR test vectors got from here:
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
//
// test for cfb 128 bits
$key = "2b7e151628aed2a6abf7158809cf4f3c";
$iv = "000102030405060708090a0b0c0d0e0f";
$testVectors = array(
array(
"6bc1bee22e409f96e93d7e117393172a",
"3b3fd92eb72dad20333449f8e83cfb4a"
),
array(
"ae2d8a571e03ac9c9eb76fac45af8e51",
"c8a64537a0b3a93fcde3cdad9f1ce58b"
),
array(
"30c81c46a35ce411e5fbc1191a0a52ef",
"26751f67a3cbb140b1808cf187a4f4df"
),
array(
"f69f2445df4f9b17ad2b417be66c3710",
"c04b05357c5d1c0eeac4c66f9ff7f2e6"
)
);
define('ENDLINE', "<br/>".PHP_EOL);
echo "Key : ".$key.ENDLINE;
echo "IV : ".$iv.ENDLINE;
$key = hex2bin($key);
$iv = hex2bin($iv);
echo ENDLINE."1. Test for CFB mode".ENDLINE.ENDLINE;
foreach ($testVectors as $i=>$vector) {
$pt = hex2bin($vector[0]);
$expected = hex2bin($vector[1]);
$ct = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $pt, 'cfb', $iv);
$dt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ct, 'cfb', $iv);
echo ($i+1).'. Test:'.ENDLINE;
echo "Plaintext: ".$vector[0].ENDLINE;
echo "Encrypted: ".bin2hex($ct).ENDLINE;
$color = (bin2hex($ct) == $vector[1]) ? 'green' : 'red';
echo "Expected : <font
color='".$color."'>".$vector[1].'</font>'.ENDLINE;
echo "Decrypted: ".bin2hex($dt).ENDLINE;
echo ENDLINE;
}
echo ENDLINE."2. Test for NCFB mode".ENDLINE.ENDLINE;
foreach ($testVectors as $i=>$vector) {
$pt = hex2bin($vector[0]);
$expected = hex2bin($vector[1]);
$ct = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $pt, 'ncfb', $iv);
$dt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ct, 'ncfb', $iv);
echo ($i+1).'. Test:'.ENDLINE;
echo "Plaintext: ".$vector[0].ENDLINE;
echo "Encrypted: ".bin2hex($ct).ENDLINE;
$color = (bin2hex($ct) == $vector[1]) ? 'green' : 'red';
echo "Expected : <font
color='".$color."'>".$vector[1].'</font>'.ENDLINE;
echo "Decrypted: ".bin2hex($dt).ENDLINE;
echo ENDLINE;
}
Expected result:
----------------
$testVectors = array(
array(
"6bc1bee22e409f96e93d7e117393172a",
"3b3fd92eb72dad20333449f8e83cfb4a"
),
array(
"ae2d8a571e03ac9c9eb76fac45af8e51",
"c8a64537a0b3a93fcde3cdad9f1ce58b"
),
array(
"30c81c46a35ce411e5fbc1191a0a52ef",
"26751f67a3cbb140b1808cf187a4f4df"
),
array(
"f69f2445df4f9b17ad2b417be66c3710",
"c04b05357c5d1c0eeac4c66f9ff7f2e6"
)
);
Actual result:
--------------
Key : 2b7e151628aed2a6abf7158809cf4f3c
IV : 000102030405060708090a0b0c0d0e0f
1. Test for CFB mode
1. Test:
Plaintext: 6bc1bee22e409f96e93d7e117393172a
Encrypted: 3b79424c9c0dd436bace9e0ed4586a4f <!-- not matched
Expected : 3b3fd92eb72dad20333449f8e83cfb4a
Decrypted: 6bc1bee22e409f96e93d7e117393172a
2. Test:
Plaintext: ae2d8a571e03ac9c9eb76fac45af8e51
Encrypted: fed65b2a0a203b682640f5ca09a2d410 <!-- not matched
Expected : c8a64537a0b3a93fcde3cdad9f1ce58b
Decrypted: ae2d8a571e03ac9c9eb76fac45af8e51
3. Test:
Plaintext: 30c81c46a35ce411e5fbc1191a0a52ef
Encrypted: 60dc808f45cee759327f8ff1b899f29b <!-- not matched
Expected : 26751f67a3cbb140b1808cf187a4f4df
Decrypted: 30c81c46a35ce411e5fbc1191a0a52ef
4. Test:
Plaintext: f69f2445df4f9b17ad2b417be66c3710
Encrypted: a628b00b5c630691a08a992332765451 <!-- not matched
Expected : c04b05357c5d1c0eeac4c66f9ff7f2e6
Decrypted: f69f2445df4f9b17ad2b417be66c3710
2. Test for NCFB mode
1. Test:
Plaintext: 6bc1bee22e409f96e93d7e117393172a
Encrypted: 3b3fd92eb72dad20333449f8e83cfb4a <!-- matched
Expected : 3b3fd92eb72dad20333449f8e83cfb4a
Decrypted: 6bc1bee22e409f96e93d7e117393172a
2. Test:
Plaintext: ae2d8a571e03ac9c9eb76fac45af8e51
Encrypted: fed3ed9b876e9e2a44be5845de006231 <!-- not matched
Expected : c8a64537a0b3a93fcde3cdad9f1ce58b
Decrypted: ae2d8a571e03ac9c9eb76fac45af8e51
3. Test:
Plaintext: 30c81c46a35ce411e5fbc1191a0a52ef
Encrypted: 60367b8a3a31d6a73ff2f6f081a5be8f <!-- not matched
Expected : 26751f67a3cbb140b1808cf187a4f4df
Decrypted: 30c81c46a35ce411e5fbc1191a0a52ef
4. Test:
Plaintext: f69f2445df4f9b17ad2b417be66c3710
Encrypted: a66143894622a9a1772276927dc3db70 <!-- not matched
Expected : c04b05357c5d1c0eeac4c66f9ff7f2e6
Decrypted: f69f2445df4f9b17ad2b417be66c3710
--
Edit bug report at https://bugs.php.net/bug.php?id=70777&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70777&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70777&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70777&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70777&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70777&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70777&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70777&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70777&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70777&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70777&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70777&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70777&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70777&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70777&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70777&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70777&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70777&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70777&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70777&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70777&r=mysqlcfg