asmcrypto / asmcrypto.js

JavaScript Cryptographic Library with performance in mind.
MIT License
660 stars 182 forks source link

How can I use asmcrypto to implement aes-cfb stream? #68

Closed hackwaly closed 6 years ago

hackwaly commented 9 years ago

AES_CFB.encrypt doesn't return the iv for next encrypt. Maybe it will preserve iv internally for next encrypt, but it looks like all aes encrypt or decrypt operation share one asm module. It will conflict when more than one cfb stream encrypt process with out of order. So, how can I use asmcrypto to implement aes-cfb stream?

vibornoff commented 9 years ago

Hi, starting from https://github.com/vibornoff/asmcrypto.js/commit/4e64ff14cb91621e0f9900a8eb9e98e5a8325fff you can use progressive cipher interface.

var encryptor = new asmCrypto.AES_CFB.Encrypt( { key: ..., iv: ... } );

var ciphertext1 = encryptor.process(cleartext1).result;
var ciphertext2 = encryptor.process(cleartext2).result;
...
var ciphertextN = encryptor.finish().result;

This is quite new feature and it hasn't been release yet, so you have to build asmcrypto.js from the source.

hackwaly commented 9 years ago

It's pretty good! I've tried it yesterday. But it doesn't cover my needs. CFB mode do not need padding, and it can encrypt any length plaintext. Somehow I need this feature: The process method in CFB mode should returns ciphertext as same length as plaintext. So I can use it for socket encryption with no delay and no wrappings.

I'm trying to adapt this library to gopherjs to get huge performance improvement. In gopherjs, AES encryption takes nearly 1 second when encrypt 1MB text.

Thanks for your awesome works!

vibornoff commented 9 years ago

CFB mode do not need padding

Yup, it doesn't. Just a doc error.

The process method in CFB mode should returns ciphertext as same length as plaintext.

Unfortunely it's not possible for now. I'm working on this. That relates to CTR, CCM, GCM and OBF modes too.

hackwaly commented 9 years ago

Thanks. That's will be excited!

alippai commented 9 years ago

You can check pako.js' inflate/deflate stream interface, I think we could apply it here.

vibornoff commented 9 years ago

You can check pako.js' inflate/deflate stream interface, I think we could apply it here.

Bad idea, take a look to pako.js README:

var inflator = new pako.Inflate();

inflator.push(chunk1, false);
inflator.push(chunk2, false);
...
inflator.push(chunkN, true); // true -> last chunk
...
var output = inflator.result;

Great, we end up with a gigabyte of inflator.result held in memory.

alippai commented 9 years ago

Sorry, I didn't mean the default API: http://nodeca.github.io/pako/#Deflate.prototype.onData