php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70777 cfb or ncfb gives wrong result
Submitted: 2015-10-23 14:14 UTC Modified: 2015-10-23 15:20 UTC
From: letsgolee at naver dot com Assigned:
Status: Closed Package: mcrypt related
PHP Version: 5.4.45 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: letsgolee at naver dot com
New email:
PHP Version: OS:

 

 [2015-10-23 14:14 UTC] 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 &nbsp;: ".$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&nbsp;: <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&nbsp;: <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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-23 15:20 UTC] letsgolee at naver dot com
-Status: Open +Status: Closed
 [2015-10-23 15:20 UTC] letsgolee at naver dot com
Sorry. the test vectors are from one streaming string.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 23:01:30 2024 UTC