chalos / crypto-js

Automatically exported from code.google.com/p/crypto-js
0 stars 0 forks source link

AES-CTR clone issue #74

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This is related to issue #73

Finally, a workaround is :

crypto.createCipheriv=function(algo,key,iv) {
                algo=algo.split('-');
                key=abv2wa(key);
                var params={mode:CryptoJS.mode[algo[2].toUpperCase()],iv:abv2wa(iv),padding:CryptoJS.pad.NoPadding};
                var enc=CryptoJS.algo.AES.createEncryptor(key,params);
                enc.update=function(data) {
                    var m=this._data.sigBytes*2;
                    var tmp=this.process(abv2wa(data)).toString(CryptoJS.enc.Hex);
                    var clone=this.clone();
                    var ocounter=this._mode._counter.slice(0);
                    var fin=clone.finalize().toString(CryptoJS.enc.Hex);
                    this._mode._counter=ocounter;
                    return [tmp.substr(m),fin].join('');
                };
                enc.final=function() {return this.finalize().toString(CryptoJS.enc.Hex);};
                return enc;
            };

If we do not copy the _mode._counter object, after clone.finalize the 
_mode._counter object of the initial object is the _mode._counter object of the 
clone, so updated by the clone.finalize operation.

The clone method should clone _counter (and maybe other properties that could 
behave the same, if any, for ctr or other modes)

Original issue reported on code.google.com by vitteaym...@gmail.com on 8 Feb 2013 at 11:16