123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999 |
- var forge = require('./forge');
- require('./util');
- forge.cipher = forge.cipher || {};
- var modes = module.exports = forge.cipher.modes = forge.cipher.modes || {};
- modes.ecb = function(options) {
- options = options || {};
- this.name = 'ECB';
- this.cipher = options.cipher;
- this.blockSize = options.blockSize || 16;
- this._ints = this.blockSize / 4;
- this._inBlock = new Array(this._ints);
- this._outBlock = new Array(this._ints);
- };
- modes.ecb.prototype.start = function(options) {};
- modes.ecb.prototype.encrypt = function(input, output, finish) {
-
- if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
- return true;
- }
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = input.getInt32();
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(this._outBlock[i]);
- }
- };
- modes.ecb.prototype.decrypt = function(input, output, finish) {
-
- if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
- return true;
- }
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = input.getInt32();
- }
-
- this.cipher.decrypt(this._inBlock, this._outBlock);
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(this._outBlock[i]);
- }
- };
- modes.ecb.prototype.pad = function(input, options) {
-
-
- var padding = (input.length() === this.blockSize ?
- this.blockSize : (this.blockSize - input.length()));
- input.fillWithByte(padding, padding);
- return true;
- };
- modes.ecb.prototype.unpad = function(output, options) {
-
- if(options.overflow > 0) {
- return false;
- }
-
- var len = output.length();
- var count = output.at(len - 1);
- if(count > (this.blockSize << 2)) {
- return false;
- }
-
- output.truncate(count);
- return true;
- };
- modes.cbc = function(options) {
- options = options || {};
- this.name = 'CBC';
- this.cipher = options.cipher;
- this.blockSize = options.blockSize || 16;
- this._ints = this.blockSize / 4;
- this._inBlock = new Array(this._ints);
- this._outBlock = new Array(this._ints);
- };
- modes.cbc.prototype.start = function(options) {
-
-
- if(options.iv === null) {
-
- if(!this._prev) {
- throw new Error('Invalid IV parameter.');
- }
- this._iv = this._prev.slice(0);
- } else if(!('iv' in options)) {
- throw new Error('Invalid IV parameter.');
- } else {
-
- this._iv = transformIV(options.iv, this.blockSize);
- this._prev = this._iv.slice(0);
- }
- };
- modes.cbc.prototype.encrypt = function(input, output, finish) {
-
- if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
- return true;
- }
-
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = this._prev[i] ^ input.getInt32();
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(this._outBlock[i]);
- }
- this._prev = this._outBlock;
- };
- modes.cbc.prototype.decrypt = function(input, output, finish) {
-
- if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
- return true;
- }
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = input.getInt32();
- }
-
- this.cipher.decrypt(this._inBlock, this._outBlock);
-
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(this._prev[i] ^ this._outBlock[i]);
- }
- this._prev = this._inBlock.slice(0);
- };
- modes.cbc.prototype.pad = function(input, options) {
-
-
- var padding = (input.length() === this.blockSize ?
- this.blockSize : (this.blockSize - input.length()));
- input.fillWithByte(padding, padding);
- return true;
- };
- modes.cbc.prototype.unpad = function(output, options) {
-
- if(options.overflow > 0) {
- return false;
- }
-
- var len = output.length();
- var count = output.at(len - 1);
- if(count > (this.blockSize << 2)) {
- return false;
- }
-
- output.truncate(count);
- return true;
- };
- modes.cfb = function(options) {
- options = options || {};
- this.name = 'CFB';
- this.cipher = options.cipher;
- this.blockSize = options.blockSize || 16;
- this._ints = this.blockSize / 4;
- this._inBlock = null;
- this._outBlock = new Array(this._ints);
- this._partialBlock = new Array(this._ints);
- this._partialOutput = forge.util.createBuffer();
- this._partialBytes = 0;
- };
- modes.cfb.prototype.start = function(options) {
- if(!('iv' in options)) {
- throw new Error('Invalid IV parameter.');
- }
-
- this._iv = transformIV(options.iv, this.blockSize);
- this._inBlock = this._iv.slice(0);
- this._partialBytes = 0;
- };
- modes.cfb.prototype.encrypt = function(input, output, finish) {
-
- var inputLength = input.length();
- if(inputLength === 0) {
- return true;
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- if(this._partialBytes === 0 && inputLength >= this.blockSize) {
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = input.getInt32() ^ this._outBlock[i];
- output.putInt32(this._inBlock[i]);
- }
- return;
- }
-
- var partialBytes = (this.blockSize - inputLength) % this.blockSize;
- if(partialBytes > 0) {
- partialBytes = this.blockSize - partialBytes;
- }
-
- this._partialOutput.clear();
- for(var i = 0; i < this._ints; ++i) {
- this._partialBlock[i] = input.getInt32() ^ this._outBlock[i];
- this._partialOutput.putInt32(this._partialBlock[i]);
- }
- if(partialBytes > 0) {
-
- input.read -= this.blockSize;
- } else {
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = this._partialBlock[i];
- }
- }
-
- if(this._partialBytes > 0) {
- this._partialOutput.getBytes(this._partialBytes);
- }
- if(partialBytes > 0 && !finish) {
- output.putBytes(this._partialOutput.getBytes(
- partialBytes - this._partialBytes));
- this._partialBytes = partialBytes;
- return true;
- }
- output.putBytes(this._partialOutput.getBytes(
- inputLength - this._partialBytes));
- this._partialBytes = 0;
- };
- modes.cfb.prototype.decrypt = function(input, output, finish) {
-
- var inputLength = input.length();
- if(inputLength === 0) {
- return true;
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- if(this._partialBytes === 0 && inputLength >= this.blockSize) {
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = input.getInt32();
- output.putInt32(this._inBlock[i] ^ this._outBlock[i]);
- }
- return;
- }
-
- var partialBytes = (this.blockSize - inputLength) % this.blockSize;
- if(partialBytes > 0) {
- partialBytes = this.blockSize - partialBytes;
- }
-
- this._partialOutput.clear();
- for(var i = 0; i < this._ints; ++i) {
- this._partialBlock[i] = input.getInt32();
- this._partialOutput.putInt32(this._partialBlock[i] ^ this._outBlock[i]);
- }
- if(partialBytes > 0) {
-
- input.read -= this.blockSize;
- } else {
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = this._partialBlock[i];
- }
- }
-
- if(this._partialBytes > 0) {
- this._partialOutput.getBytes(this._partialBytes);
- }
- if(partialBytes > 0 && !finish) {
- output.putBytes(this._partialOutput.getBytes(
- partialBytes - this._partialBytes));
- this._partialBytes = partialBytes;
- return true;
- }
- output.putBytes(this._partialOutput.getBytes(
- inputLength - this._partialBytes));
- this._partialBytes = 0;
- };
- modes.ofb = function(options) {
- options = options || {};
- this.name = 'OFB';
- this.cipher = options.cipher;
- this.blockSize = options.blockSize || 16;
- this._ints = this.blockSize / 4;
- this._inBlock = null;
- this._outBlock = new Array(this._ints);
- this._partialOutput = forge.util.createBuffer();
- this._partialBytes = 0;
- };
- modes.ofb.prototype.start = function(options) {
- if(!('iv' in options)) {
- throw new Error('Invalid IV parameter.');
- }
-
- this._iv = transformIV(options.iv, this.blockSize);
- this._inBlock = this._iv.slice(0);
- this._partialBytes = 0;
- };
- modes.ofb.prototype.encrypt = function(input, output, finish) {
-
- var inputLength = input.length();
- if(input.length() === 0) {
- return true;
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- if(this._partialBytes === 0 && inputLength >= this.blockSize) {
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(input.getInt32() ^ this._outBlock[i]);
- this._inBlock[i] = this._outBlock[i];
- }
- return;
- }
-
- var partialBytes = (this.blockSize - inputLength) % this.blockSize;
- if(partialBytes > 0) {
- partialBytes = this.blockSize - partialBytes;
- }
-
- this._partialOutput.clear();
- for(var i = 0; i < this._ints; ++i) {
- this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]);
- }
- if(partialBytes > 0) {
-
- input.read -= this.blockSize;
- } else {
-
- for(var i = 0; i < this._ints; ++i) {
- this._inBlock[i] = this._outBlock[i];
- }
- }
-
- if(this._partialBytes > 0) {
- this._partialOutput.getBytes(this._partialBytes);
- }
- if(partialBytes > 0 && !finish) {
- output.putBytes(this._partialOutput.getBytes(
- partialBytes - this._partialBytes));
- this._partialBytes = partialBytes;
- return true;
- }
- output.putBytes(this._partialOutput.getBytes(
- inputLength - this._partialBytes));
- this._partialBytes = 0;
- };
- modes.ofb.prototype.decrypt = modes.ofb.prototype.encrypt;
- modes.ctr = function(options) {
- options = options || {};
- this.name = 'CTR';
- this.cipher = options.cipher;
- this.blockSize = options.blockSize || 16;
- this._ints = this.blockSize / 4;
- this._inBlock = null;
- this._outBlock = new Array(this._ints);
- this._partialOutput = forge.util.createBuffer();
- this._partialBytes = 0;
- };
- modes.ctr.prototype.start = function(options) {
- if(!('iv' in options)) {
- throw new Error('Invalid IV parameter.');
- }
-
- this._iv = transformIV(options.iv, this.blockSize);
- this._inBlock = this._iv.slice(0);
- this._partialBytes = 0;
- };
- modes.ctr.prototype.encrypt = function(input, output, finish) {
-
- var inputLength = input.length();
- if(inputLength === 0) {
- return true;
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- if(this._partialBytes === 0 && inputLength >= this.blockSize) {
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(input.getInt32() ^ this._outBlock[i]);
- }
- } else {
-
- var partialBytes = (this.blockSize - inputLength) % this.blockSize;
- if(partialBytes > 0) {
- partialBytes = this.blockSize - partialBytes;
- }
-
- this._partialOutput.clear();
- for(var i = 0; i < this._ints; ++i) {
- this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]);
- }
- if(partialBytes > 0) {
-
- input.read -= this.blockSize;
- }
-
- if(this._partialBytes > 0) {
- this._partialOutput.getBytes(this._partialBytes);
- }
- if(partialBytes > 0 && !finish) {
- output.putBytes(this._partialOutput.getBytes(
- partialBytes - this._partialBytes));
- this._partialBytes = partialBytes;
- return true;
- }
- output.putBytes(this._partialOutput.getBytes(
- inputLength - this._partialBytes));
- this._partialBytes = 0;
- }
-
- inc32(this._inBlock);
- };
- modes.ctr.prototype.decrypt = modes.ctr.prototype.encrypt;
- modes.gcm = function(options) {
- options = options || {};
- this.name = 'GCM';
- this.cipher = options.cipher;
- this.blockSize = options.blockSize || 16;
- this._ints = this.blockSize / 4;
- this._inBlock = new Array(this._ints);
- this._outBlock = new Array(this._ints);
- this._partialOutput = forge.util.createBuffer();
- this._partialBytes = 0;
-
-
-
- this._R = 0xE1000000;
- };
- modes.gcm.prototype.start = function(options) {
- if(!('iv' in options)) {
- throw new Error('Invalid IV parameter.');
- }
-
- var iv = forge.util.createBuffer(options.iv);
-
- this._cipherLength = 0;
-
- var additionalData;
- if('additionalData' in options) {
- additionalData = forge.util.createBuffer(options.additionalData);
- } else {
- additionalData = forge.util.createBuffer();
- }
-
- if('tagLength' in options) {
- this._tagLength = options.tagLength;
- } else {
- this._tagLength = 128;
- }
-
- this._tag = null;
- if(options.decrypt) {
-
- this._tag = forge.util.createBuffer(options.tag).getBytes();
- if(this._tag.length !== (this._tagLength / 8)) {
- throw new Error('Authentication tag does not match tag length.');
- }
- }
-
- this._hashBlock = new Array(this._ints);
-
- this.tag = null;
-
-
- this._hashSubkey = new Array(this._ints);
- this.cipher.encrypt([0, 0, 0, 0], this._hashSubkey);
-
-
-
-
- this.componentBits = 4;
- this._m = this.generateHashTable(this._hashSubkey, this.componentBits);
-
-
-
- var ivLength = iv.length();
- if(ivLength === 12) {
-
- this._j0 = [iv.getInt32(), iv.getInt32(), iv.getInt32(), 1];
- } else {
-
- this._j0 = [0, 0, 0, 0];
- while(iv.length() > 0) {
- this._j0 = this.ghash(
- this._hashSubkey, this._j0,
- [iv.getInt32(), iv.getInt32(), iv.getInt32(), iv.getInt32()]);
- }
- this._j0 = this.ghash(
- this._hashSubkey, this._j0, [0, 0].concat(from64To32(ivLength * 8)));
- }
-
- this._inBlock = this._j0.slice(0);
- inc32(this._inBlock);
- this._partialBytes = 0;
-
- additionalData = forge.util.createBuffer(additionalData);
-
- this._aDataLength = from64To32(additionalData.length() * 8);
-
- var overflow = additionalData.length() % this.blockSize;
- if(overflow) {
- additionalData.fillWithByte(0, this.blockSize - overflow);
- }
- this._s = [0, 0, 0, 0];
- while(additionalData.length() > 0) {
- this._s = this.ghash(this._hashSubkey, this._s, [
- additionalData.getInt32(),
- additionalData.getInt32(),
- additionalData.getInt32(),
- additionalData.getInt32()
- ]);
- }
- };
- modes.gcm.prototype.encrypt = function(input, output, finish) {
-
- var inputLength = input.length();
- if(inputLength === 0) {
- return true;
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- if(this._partialBytes === 0 && inputLength >= this.blockSize) {
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(this._outBlock[i] ^= input.getInt32());
- }
- this._cipherLength += this.blockSize;
- } else {
-
- var partialBytes = (this.blockSize - inputLength) % this.blockSize;
- if(partialBytes > 0) {
- partialBytes = this.blockSize - partialBytes;
- }
-
- this._partialOutput.clear();
- for(var i = 0; i < this._ints; ++i) {
- this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]);
- }
- if(partialBytes <= 0 || finish) {
-
- if(finish) {
-
- var overflow = inputLength % this.blockSize;
- this._cipherLength += overflow;
-
- this._partialOutput.truncate(this.blockSize - overflow);
- } else {
- this._cipherLength += this.blockSize;
- }
-
- for(var i = 0; i < this._ints; ++i) {
- this._outBlock[i] = this._partialOutput.getInt32();
- }
- this._partialOutput.read -= this.blockSize;
- }
-
- if(this._partialBytes > 0) {
- this._partialOutput.getBytes(this._partialBytes);
- }
- if(partialBytes > 0 && !finish) {
-
-
- input.read -= this.blockSize;
- output.putBytes(this._partialOutput.getBytes(
- partialBytes - this._partialBytes));
- this._partialBytes = partialBytes;
- return true;
- }
- output.putBytes(this._partialOutput.getBytes(
- inputLength - this._partialBytes));
- this._partialBytes = 0;
- }
-
- this._s = this.ghash(this._hashSubkey, this._s, this._outBlock);
-
- inc32(this._inBlock);
- };
- modes.gcm.prototype.decrypt = function(input, output, finish) {
-
- var inputLength = input.length();
- if(inputLength < this.blockSize && !(finish && inputLength > 0)) {
- return true;
- }
-
- this.cipher.encrypt(this._inBlock, this._outBlock);
-
- inc32(this._inBlock);
-
- this._hashBlock[0] = input.getInt32();
- this._hashBlock[1] = input.getInt32();
- this._hashBlock[2] = input.getInt32();
- this._hashBlock[3] = input.getInt32();
- this._s = this.ghash(this._hashSubkey, this._s, this._hashBlock);
-
- for(var i = 0; i < this._ints; ++i) {
- output.putInt32(this._outBlock[i] ^ this._hashBlock[i]);
- }
-
- if(inputLength < this.blockSize) {
- this._cipherLength += inputLength % this.blockSize;
- } else {
- this._cipherLength += this.blockSize;
- }
- };
- modes.gcm.prototype.afterFinish = function(output, options) {
- var rval = true;
-
- if(options.decrypt && options.overflow) {
- output.truncate(this.blockSize - options.overflow);
- }
-
- this.tag = forge.util.createBuffer();
-
- var lengths = this._aDataLength.concat(from64To32(this._cipherLength * 8));
-
- this._s = this.ghash(this._hashSubkey, this._s, lengths);
-
- var tag = [];
- this.cipher.encrypt(this._j0, tag);
- for(var i = 0; i < this._ints; ++i) {
- this.tag.putInt32(this._s[i] ^ tag[i]);
- }
-
- this.tag.truncate(this.tag.length() % (this._tagLength / 8));
-
- if(options.decrypt && this.tag.bytes() !== this._tag) {
- rval = false;
- }
- return rval;
- };
- modes.gcm.prototype.multiply = function(x, y) {
- var z_i = [0, 0, 0, 0];
- var v_i = y.slice(0);
-
- for(var i = 0; i < 128; ++i) {
-
-
-
- var x_i = x[(i / 32) | 0] & (1 << (31 - i % 32));
- if(x_i) {
- z_i[0] ^= v_i[0];
- z_i[1] ^= v_i[1];
- z_i[2] ^= v_i[2];
- z_i[3] ^= v_i[3];
- }
-
-
- this.pow(v_i, v_i);
- }
- return z_i;
- };
- modes.gcm.prototype.pow = function(x, out) {
-
-
- var lsb = x[3] & 1;
-
-
-
-
- for(var i = 3; i > 0; --i) {
- out[i] = (x[i] >>> 1) | ((x[i - 1] & 1) << 31);
- }
-
- out[0] = x[0] >>> 1;
-
-
-
- if(lsb) {
- out[0] ^= this._R;
- }
- };
- modes.gcm.prototype.tableMultiply = function(x) {
-
- var z = [0, 0, 0, 0];
- for(var i = 0; i < 32; ++i) {
- var idx = (i / 8) | 0;
- var x_i = (x[idx] >>> ((7 - (i % 8)) * 4)) & 0xF;
- var ah = this._m[i][x_i];
- z[0] ^= ah[0];
- z[1] ^= ah[1];
- z[2] ^= ah[2];
- z[3] ^= ah[3];
- }
- return z;
- };
- modes.gcm.prototype.ghash = function(h, y, x) {
- y[0] ^= x[0];
- y[1] ^= x[1];
- y[2] ^= x[2];
- y[3] ^= x[3];
- return this.tableMultiply(y);
-
- };
- modes.gcm.prototype.generateHashTable = function(h, bits) {
-
-
-
- var multiplier = 8 / bits;
- var perInt = 4 * multiplier;
- var size = 16 * multiplier;
- var m = new Array(size);
- for(var i = 0; i < size; ++i) {
- var tmp = [0, 0, 0, 0];
- var idx = (i / perInt) | 0;
- var shft = ((perInt - 1 - (i % perInt)) * bits);
- tmp[idx] = (1 << (bits - 1)) << shft;
- m[i] = this.generateSubHashTable(this.multiply(tmp, h), bits);
- }
- return m;
- };
- modes.gcm.prototype.generateSubHashTable = function(mid, bits) {
-
-
-
- var size = 1 << bits;
- var half = size >>> 1;
- var m = new Array(size);
- m[half] = mid.slice(0);
- var i = half >>> 1;
- while(i > 0) {
-
- this.pow(m[2 * i], m[i] = []);
- i >>= 1;
- }
- i = 2;
- while(i < half) {
- for(var j = 1; j < i; ++j) {
- var m_i = m[i];
- var m_j = m[j];
- m[i + j] = [
- m_i[0] ^ m_j[0],
- m_i[1] ^ m_j[1],
- m_i[2] ^ m_j[2],
- m_i[3] ^ m_j[3]
- ];
- }
- i *= 2;
- }
- m[0] = [0, 0, 0, 0];
-
- for(i = half + 1; i < size; ++i) {
- var c = m[i ^ half];
- m[i] = [mid[0] ^ c[0], mid[1] ^ c[1], mid[2] ^ c[2], mid[3] ^ c[3]];
- }
- return m;
- };
- function transformIV(iv, blockSize) {
- if(typeof iv === 'string') {
-
- iv = forge.util.createBuffer(iv);
- }
- if(forge.util.isArray(iv) && iv.length > 4) {
-
- var tmp = iv;
- iv = forge.util.createBuffer();
- for(var i = 0; i < tmp.length; ++i) {
- iv.putByte(tmp[i]);
- }
- }
- if(iv.length() < blockSize) {
- throw new Error(
- 'Invalid IV length; got ' + iv.length() +
- ' bytes and expected ' + blockSize + ' bytes.');
- }
- if(!forge.util.isArray(iv)) {
-
- var ints = [];
- var blocks = blockSize / 4;
- for(var i = 0; i < blocks; ++i) {
- ints.push(iv.getInt32());
- }
- iv = ints;
- }
- return iv;
- }
- function inc32(block) {
-
- block[block.length - 1] = (block[block.length - 1] + 1) & 0xFFFFFFFF;
- }
- function from64To32(num) {
-
- return [(num / 0x100000000) | 0, num & 0xFFFFFFFF];
- }
|