|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-02-25 18:01 UTC] zelnaga at gmail dot com
Description: ------------ Correct me if I'm wrong, but shouldn't an ECB decryption of an OFB encrypted string of null bytes produce a string whose first eight bytes (assuming that that's the block size) are equal to the IV? Certainly that's the impression I get from wikipedia.org's discussion of OFB. http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29 Reproduce code: --------------- <?php $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_OFB, ''); mcrypt_generic_init($td, 'aaaaaaaa', 'bbbbbbbb'); $ciphertext = mcrypt_generic($td, "\0\0\0\0\0\0\0\0"); $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, 'aaaaaaaa', "\0\0\0\0\0\0\0\0"); echo urlencode(mdecrypt_generic($td, $ciphertext)); ?> Expected result: ---------------- bbbbbbbb Actual result: -------------- 5%FBdq%C7Y7%13 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 06:00:01 2025 UTC |
mcrypt also seems to be implementing OFB and CFB modes identically. Although the first block produced by either mode should be the same, subsequent blocks should be different. ie. in CFB, the second block is XOR'd with the previous ciphertext, reencrypted with the key, whereas in OFB, the second block is XOR'd with that which the previous text was previously XOR'd with. Example code: <?php $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_OFB, ''); mcrypt_generic_init($td, 'aaaaaaaa', 'bbbbbbbb'); echo urlencode(mcrypt_generic($td, str_repeat("\0", 16))) . "\r\n"; $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CFB, ''); mcrypt_generic_init($td, 'aaaaaaaa', 'bbbbbbbb'); echo urlencode(mcrypt_generic($td, str_repeat("\0", 16))); ?>Filing a bug report is going to be a little difficult giving that, near as I can tell, the command line version of mcrypt randomly generates IV's. My first example requires the IV's be of a known value and my second example requires encrypting the same string with two different modes and with the same IV. Also, to be honest, I don't know at all how to intreprete the data the command line version of mcrypt is giving me, anyway. I do the following: mcrypt --algorithm des --mode ecb --no-openpgp test.txt --verbose --bare And I get a 100 byte file. Given that the source file was 16 bytes ("-" repeated sixteen times), that's a bit odd. Curious to see what the remaining 84 bytes are, I do the following: <?php $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, 'test', "\0\0\0\0\0\0\0\0"); echo mdecrypt_generic($td, file_get_contents('test.txt.nc')); ?> And that doesn't produce anything even remotely resembling the source text. A while ago, there was a bug report filed on the mcrypt PHP extension (49561) where someone reproduced the problem in C, using the mcrypt libraries, and filed the bug report themselves. Can't that be done here? I don't have the ability to compile PHP or PHP extensions such as mcrypt and if no one reports the bug to the mcrypt developers than both PHP and mcrypt will have this bug. Of course, then again, given that bug # 49561 hasn't even been touched by the mcrypt developers, it seems safe to assume that any bug report that's filed - by me or anyone else - will be ignored. If mcrypt has been abandoned by its developers when does PHP abandon mcrypt?I was comparing mcrypt against openssl_encrypt() and... well, either OpenSSL is wrong or mcrypt is wrong: <?php $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_OFB, ''); mcrypt_generic_init($td, 'aaaaaaaa', "\0\0\0\0\0\0\0\0"); echo bin2hex(mcrypt_generic($td, "\0\0\0\0\0\0\0\0")); echo "\r\n"; $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CFB, ''); mcrypt_generic_init($td, 'aaaaaaaa', "\0\0\0\0\0\0\0\0"); echo bin2hex(mcrypt_generic($td, "\0\0\0\0\0\0\0\0")); echo "\r\n"; echo bin2hex(openssl_encrypt("\0\0\0\0\0\0\0\0", 'DES-OFB', 'aaaaaaaa', true)) . "\r\n"; echo bin2hex(openssl_encrypt("\0\0\0\0\0\0\0\0", 'DES-CFB', 'aaaaaaaa', true)) . "\r\n"; $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, 'aaaaaaaa', "\0\0\0\0\0\0\0\0"); echo bin2hex(mcrypt_generic($td, "\0\0\0\0\0\0\0\0")); echo "\r\n"; $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($td, 'aaaaaaaa', "\0\0\0\0\0\0\0\0"); echo bin2hex(mcrypt_generic($td, "\0\0\0\0\0\0\0\0")); echo "\r\n"; $td = mcrypt_module_open(MCRYPT_DES, '', 'ctr', ''); mcrypt_generic_init($td, 'aaaaaaaa', "\0\0\0\0\0\0\0\0"); echo bin2hex(mcrypt_generic($td, "\0\0\0\0\0\0\0\0")); ?> ie. mcrypt, in CTR, CBC and ECB modes equal OpenSSL in OFB and CFB modes but not mcrypt in OFB and CFB modes. In other words, OpenSSL's OFB != mcrypt's OFB and they should.To answer your first question, performing the encryption per-byte is kind of odd, I haven't done much testing but I don't think it actually consumes the entire input vector, only the first byte, just think of it as running with an 8-bit wide state, rather than the more typical 128-bit wide state. I've never had the time to confirm that this is the exact case, though, but I believe it's compatible with other cryptography solutions that can operate per-byte. For your second point; sorry I should have pointed out that there is no predefined constant for MCRYPT_MODE_NCFB, you can however just enter it manually as 'ncfb' like so: mcrypt_module_open('rijndael-128', '', 'ncfb', ''); So if there's a failing on the PHP side it's that we need an MCRYPT_MODE_NCFB, and that MCRYPT_MODE_CFB and MCRYPT_MODE_OFB should be properly documented so people don't keep using them expecting different results!This addition might come as very low quality as I am still trying to understand AES128 and CFB, but I think it is important to have the additional information one can gather. I hope it helps. I was stuck trying to get mcrypt to cipher a string with AES128 in CFB mode, to be used by the corresponding code in ASP.NET. I first thought ASP was the erroneous part, but apparently not. This is my code snippet (note that padding is made with PKCS7 method). I use an IV identical to my key, which is OK considering the key is 16 bytes long: $password = 'not ciphered'; $key = '-+*%$({..})$%*+-'; $blockSize = 16; $padding = $blockSize - (strlen($password)%$blockSize); $password .= str_repeat(chr($padding),$padding); $cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $password, MCRYPT_MODE_CFB, $key); $arr = preg_split('//', $cipher, -1, PREG_SPLIT_NO_EMPTY); foreach ($arr as $char) { echo ord($char).','; } The generated $cipher is the following sequence of (16) bytes: 179,90,167,188,65,82,212,108,133,15,161,49,142,222,207,167 However, the correct sequence of bytes should be as defined below, and can easily be generated using phpseclib: $password = 'not ciphered'; $key = '-+*%$({..})$%*+-'; $blockSize = 16; $padding = $blockSize - (strlen($password)%$blockSize); $password .= str_repeat(chr($padding),$padding); $cipher = new Crypt_AES(CRYPT_AES_MODE_CFB); $cipher->setKeyLength(128); $cipher->setKey($key); $cipher->setIV($key); $cipheredPass = $cipher->encrypt($password); $arr = preg_split('//', $cipheredPass, -1, PREG_SPLIT_NO_EMPTY); foreach ($arr as $char) { echo ord($char).','; } This last code generates the following sequence of (16) bytes: 179,15,83,71,111,104,118,215,159,221,153,228,153,148,69,164 Now, as I said, I'm still wrapping my brains around the whole AES128 (and in particular CFB), but this last result is also what you get in ASP (and apparently in C, C++, etc). There's a guy who apparently understands the problem better and put me in the right direction here: http://stackoverflow.com/a/4054017/1406662 I found a few other references linking to mcrypt, but not specifically with CFB (for some reason people seen to try CBC and NOFB first). Apparently, the problem comes down to the default "feedback" size, which would not be set properly in either PHP or mcrypt. Also, somehow, phpseclib got it right, and it's MIT-licensed, so maybe a source of inspiration? http://phpseclib.sourceforge.net/index.html The problem might effectively come from mcrypt, and I'd be happy to report there if confirmed and if you point me in the right direction as to where that is done most efficiently.