Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cfb mode for AES #61

Closed
cadorn opened this issue Jul 30, 2013 · 10 comments
Closed

cfb mode for AES #61

cadorn opened this issue Jul 30, 2013 · 10 comments

Comments

@cadorn
Copy link
Contributor

cadorn commented Jul 30, 2013

Are there plans to implement cfb mode for AES?

I am trying to move to forge exclusively and am missing this.

Currently using: https://github.com/openpeer/cifre/blob/master/aes.js (which has a bug with data > 15 chars)

dlongley added a commit that referenced this issue Jul 30, 2013
dlongley added a commit that referenced this issue Jul 30, 2013
@dlongley
Copy link
Member

@cadorn -- I added CFB mode support and examples for using it. Let me know if there are any issues.

dlongley added a commit that referenced this issue Jul 30, 2013
@dlongley
Copy link
Member

@cadorn, while I was at it, I added CFB, OFB, and CTR cipher modes.

@cadorn
Copy link
Contributor Author

cadorn commented Jul 30, 2013

@dlongley Wow. Thanks for the quick addition.

Is the AES encryption/decryption in CFB mode compatible with the following?:

AES (Rijndael- 128) in CFB mode with a 32 byte key size, 16 byte block size, 16 byte feedback size

@cadorn
Copy link
Contributor Author

cadorn commented Jul 30, 2013

Given:

key: 861009ec4d599fab1f40abc76e6f89880cff5833c79c548c99f9045f191cd90b
iv: d927ad81199aa7dcadfdb4e47b6dc694
data: MY-DATA-AND-HERE-IS-MORE-DATA

After encryption I get:

80eb666a9fc9e263faf71e87ffc94451d7d8df7cfcf2606470351dd5ac3f70bd

While my existing JS implementation (cifre) and PHP implementation give me:

80eb666a9fc9e263faf71e87ffc94451d7d8df7cfcf2606470351dd5ac

Notice the extra 3f70bd at the end of the forge result.

If I decrypt the forge result with cifre I get:

MY-DATA-AND-HERE-IS-MORE-DATA

When I decrypt result from cifre with forge I get:

MY-DATA-AND-HERE

Any idea what the extra characters at the end are and how they can be removed from the forge result?

@dlongley
Copy link
Member

It sounds like you're having trouble with the padding. There may or may not be a bug in forge related to this, but the padding shouldn't be affected by the cipher block mode (correction, the default padding should not be affected but stream mode shouldn't be using it). Here's some code below that seems to be working just fine for me:

var key = '861009ec4d599fab1f40abc76e6f89880cff5833c79c548c99f9045f191cd90b';
var iv = 'd927ad81199aa7dcadfdb4e47b6dc694';
var data = 'MY-DATA-AND-HERE-IS-MORE-DATA';

key = forge.util.hexToBytes(key);
iv = forge.util.hexToBytes(iv);

var cipher = forge.aes.createEncryptionCipher(key, 'CFB');
cipher.start(iv);
cipher.update(forge.util.createBuffer(data));
cipher.finish();
var encrypted = cipher.output;
console.log('encrypted: ' + encrypted.toHex());

cipher = forge.aes.createDecryptionCipher(key, 'CFB');
cipher.start(iv);
cipher.update(encrypted);
cipher.finish();
var decrypted = cipher.output.getBytes();
console.log('decrypted: ' + decrypted);

assert.equal(data, decrypted);

The above code prints this for me:

encrypted: 80eb666a9fc9e263faf71e87ffc94451d7d8df7cfcf2606470351dd5ac3f70bd
decrypted: MY-DATA-AND-HERE-IS-MORE-DATA

What are the differences with your code?

@dlongley
Copy link
Member

Wow. Thanks for the quick addition.

Sure! It wasn't too difficult to add ... and I had been meaning to for quite a while so I figured that since someone else needed it I'd see if it could be done quickly.

Is the AES encryption/decryption in CFB mode compatible with the following?:

AES (Rijndael- 128) in CFB mode with a 32 byte key size, 16 byte block size, 16 byte feedback size

Yes.

@dlongley
Copy link
Member

@cadorn, it looks like the cifre and PHP code you're using is not padding the output; you should always get multiples of the block size and that output is only 29 bytes long (58 in hex) when it should be 32.

@dlongley
Copy link
Member

@cadorn, it looks like I need to change the default padding for CFB (none) -- let me look into that.

dlongley added a commit that referenced this issue Jul 30, 2013
@dlongley
Copy link
Member

@cadorn, padding should have been turned off for any cipher stream modes (the new ones just added) with automatic truncation rather than default PKCS#7 padding. This has been corrected in the latest commit.

@cadorn
Copy link
Contributor Author

cadorn commented Jul 30, 2013

Great. Thanks!

@cadorn cadorn closed this as completed Jul 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants