Bug #70777 [Opn->Csd]: cfb or ncfb gives wrong result
| From: | letsgolee at naver dot com | Date: | Fri, 23 Oct 2015 15:20:26 +0000 |
| Subject: | Bug #70777 [Opn->Csd]: cfb or ncfb gives wrong result | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196773@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70777&edit=1
ID: 70777
User updated by: letsgolee at naver dot com
Reported by: letsgolee at naver dot com
Summary: cfb or ncfb gives wrong result
-Status: Open
+Status: Closed
Type: Bug
Package: mcrypt related
Operating System: any
PHP Version: 5.4.45
Block user comment: N
Private report: N
New Comment:
Sorry. the test vectors are from one streaming string.
Previous Comments:
------------------------------------------------------------------------
[2015-10-23 14:14:44] letsgolee at naver dot com
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 this bug report at https://bugs.php.net/bug.php?id=70777&edit=1