Socket
Socket
Sign inDemoInstall

browserify-aes

Package Overview
Dependencies
Maintainers
5
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browserify-aes - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

283

aes.js

@@ -7,181 +7,164 @@ // based on the aes implimentation in triple sec

var uint_max = Math.pow(2, 32);
function fixup_uint32(x) {
var ret, x_pos;
ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x;
return ret;
var uint_max = Math.pow(2, 32)
function fixup_uint32 (x) {
var ret, x_pos
ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x
return ret
}
function scrub_vec(v) {
var i, _i, _ref;
for (i = _i = 0, _ref = v.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
v[i] = 0;
function scrub_vec (v) {
for (var i = 0; i < v.length; v++) {
v[i] = 0
}
return false;
return false
}
function Global() {
var i;
this.SBOX = [];
this.INV_SBOX = [];
this.SUB_MIX = (function() {
var _i, _results;
_results = [];
for (i = _i = 0; _i < 4; i = ++_i) {
_results.push([]);
}
return _results;
})();
this.INV_SUB_MIX = (function() {
var _i, _results;
_results = [];
for (i = _i = 0; _i < 4; i = ++_i) {
_results.push([]);
}
return _results;
})();
this.init();
this.RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
function Global () {
this.SBOX = []
this.INV_SBOX = []
this.SUB_MIX = [[], [], [], []]
this.INV_SUB_MIX = [[], [], [], []]
this.init()
this.RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]
}
Global.prototype.init = function() {
var d, i, sx, t, x, x2, x4, x8, xi, _i;
d = (function() {
var _i, _results;
_results = [];
Global.prototype.init = function () {
var d, i, sx, t, x, x2, x4, x8, xi, _i
d = (function () {
var _i, _results
_results = []
for (i = _i = 0; _i < 256; i = ++_i) {
if (i < 128) {
_results.push(i << 1);
_results.push(i << 1)
} else {
_results.push((i << 1) ^ 0x11b);
_results.push((i << 1) ^ 0x11b)
}
}
return _results;
})();
x = 0;
xi = 0;
return _results
})()
x = 0
xi = 0
for (i = _i = 0; _i < 256; i = ++_i) {
sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
this.SBOX[x] = sx;
this.INV_SBOX[sx] = x;
x2 = d[x];
x4 = d[x2];
x8 = d[x4];
t = (d[sx] * 0x101) ^ (sx * 0x1010100);
this.SUB_MIX[0][x] = (t << 24) | (t >>> 8);
this.SUB_MIX[1][x] = (t << 16) | (t >>> 16);
this.SUB_MIX[2][x] = (t << 8) | (t >>> 24);
this.SUB_MIX[3][x] = t;
t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
this.INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8);
this.INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16);
this.INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24);
this.INV_SUB_MIX[3][sx] = t;
sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4)
sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63
this.SBOX[x] = sx
this.INV_SBOX[sx] = x
x2 = d[x]
x4 = d[x2]
x8 = d[x4]
t = (d[sx] * 0x101) ^ (sx * 0x1010100)
this.SUB_MIX[0][x] = (t << 24) | (t >>> 8)
this.SUB_MIX[1][x] = (t << 16) | (t >>> 16)
this.SUB_MIX[2][x] = (t << 8) | (t >>> 24)
this.SUB_MIX[3][x] = t
t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100)
this.INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8)
this.INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16)
this.INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24)
this.INV_SUB_MIX[3][sx] = t
if (x === 0) {
x = xi = 1;
x = xi = 1
} else {
x = x2 ^ d[d[d[x8 ^ x2]]];
xi ^= d[d[xi]];
x = x2 ^ d[d[d[x8 ^ x2]]]
xi ^= d[d[xi]]
}
}
return true;
};
return true
}
var G = new Global();
var G = new Global()
AES.blockSize = 4 * 4
AES.blockSize = 4 * 4;
AES.prototype.blockSize = AES.blockSize
AES.prototype.blockSize = AES.blockSize;
AES.keySize = 256 / 8
AES.keySize = 256 / 8;
AES.prototype.keySize = AES.keySize
AES.prototype.keySize = AES.keySize;
function bufferToArray(buf) {
var len = buf.length/4;
var out = new Array(len);
var i = -1;
function bufferToArray (buf) {
var len = buf.length / 4
var out = new Array(len)
var i = -1
while (++i < len) {
out[i] = buf.readUInt32BE(i * 4);
out[i] = buf.readUInt32BE(i * 4)
}
return out;
}
function AES(key) {
this._key = bufferToArray(key);
this._doReset();
return out
}
function AES (key) {
this._key = bufferToArray(key)
this._doReset()
}
AES.prototype._doReset = function() {
var invKsRow, keySize, keyWords, ksRow, ksRows, t, _i, _j;
keyWords = this._key;
keySize = keyWords.length;
this._nRounds = keySize + 6;
ksRows = (this._nRounds + 1) * 4;
this._keySchedule = [];
for (ksRow = _i = 0; 0 <= ksRows ? _i < ksRows : _i > ksRows; ksRow = 0 <= ksRows ? ++_i : --_i) {
this._keySchedule[ksRow] = ksRow < keySize ? keyWords[ksRow] : (t = this._keySchedule[ksRow - 1], (ksRow % keySize) === 0 ? (t = (t << 8) | (t >>> 24), t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff], t ^= G.RCON[(ksRow / keySize) | 0] << 24) : keySize > 6 && ksRow % keySize === 4 ? t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff] : void 0, this._keySchedule[ksRow - keySize] ^ t);
AES.prototype._doReset = function () {
var invKsRow, keySize, keyWords, ksRow, ksRows, t
keyWords = this._key
keySize = keyWords.length
this._nRounds = keySize + 6
ksRows = (this._nRounds + 1) * 4
this._keySchedule = []
for (ksRow = 0; ksRow < ksRows; ksRow++) {
this._keySchedule[ksRow] = ksRow < keySize ? keyWords[ksRow] : (t = this._keySchedule[ksRow - 1], (ksRow % keySize) === 0 ? (t = (t << 8) | (t >>> 24), t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff], t ^= G.RCON[(ksRow / keySize) | 0] << 24) : keySize > 6 && ksRow % keySize === 4 ? t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff] : void 0, this._keySchedule[ksRow - keySize] ^ t)
}
this._invKeySchedule = [];
for (invKsRow = _j = 0; 0 <= ksRows ? _j < ksRows : _j > ksRows; invKsRow = 0 <= ksRows ? ++_j : --_j) {
ksRow = ksRows - invKsRow;
t = this._keySchedule[ksRow - (invKsRow % 4 ? 0 : 4)];
this._invKeySchedule[invKsRow] = invKsRow < 4 || ksRow <= 4 ? t : G.INV_SUB_MIX[0][G.SBOX[t >>> 24]] ^ G.INV_SUB_MIX[1][G.SBOX[(t >>> 16) & 0xff]] ^ G.INV_SUB_MIX[2][G.SBOX[(t >>> 8) & 0xff]] ^ G.INV_SUB_MIX[3][G.SBOX[t & 0xff]];
this._invKeySchedule = []
for (invKsRow = 0; invKsRow < ksRows; invKsRow++) {
ksRow = ksRows - invKsRow
t = this._keySchedule[ksRow - (invKsRow % 4 ? 0 : 4)]
this._invKeySchedule[invKsRow] = invKsRow < 4 || ksRow <= 4 ? t : G.INV_SUB_MIX[0][G.SBOX[t >>> 24]] ^ G.INV_SUB_MIX[1][G.SBOX[(t >>> 16) & 0xff]] ^ G.INV_SUB_MIX[2][G.SBOX[(t >>> 8) & 0xff]] ^ G.INV_SUB_MIX[3][G.SBOX[t & 0xff]]
}
return true;
};
return true
}
AES.prototype.encryptBlock = function(M) {
M = bufferToArray(new Buffer(M));
var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX);
var buf = new Buffer(16);
buf.writeUInt32BE(out[0], 0);
buf.writeUInt32BE(out[1], 4);
buf.writeUInt32BE(out[2], 8);
buf.writeUInt32BE(out[3], 12);
return buf;
};
AES.prototype.encryptBlock = function (M) {
M = bufferToArray(new Buffer(M))
var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX)
var buf = new Buffer(16)
buf.writeUInt32BE(out[0], 0)
buf.writeUInt32BE(out[1], 4)
buf.writeUInt32BE(out[2], 8)
buf.writeUInt32BE(out[3], 12)
return buf
}
AES.prototype.decryptBlock = function(M) {
M = bufferToArray(new Buffer(M));
var temp = [M[3], M[1]];
M[1] = temp[0];
M[3] = temp[1];
var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX);
var buf = new Buffer(16);
buf.writeUInt32BE(out[0], 0);
buf.writeUInt32BE(out[3], 4);
buf.writeUInt32BE(out[2], 8);
buf.writeUInt32BE(out[1], 12);
return buf;
};
AES.prototype.decryptBlock = function (M) {
M = bufferToArray(new Buffer(M))
var temp = [M[3], M[1]]
M[1] = temp[0]
M[3] = temp[1]
var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX)
var buf = new Buffer(16)
buf.writeUInt32BE(out[0], 0)
buf.writeUInt32BE(out[3], 4)
buf.writeUInt32BE(out[2], 8)
buf.writeUInt32BE(out[1], 12)
return buf
}
AES.prototype.scrub = function() {
scrub_vec(this._keySchedule);
scrub_vec(this._invKeySchedule);
scrub_vec(this._key);
};
AES.prototype.scrub = function () {
scrub_vec(this._keySchedule)
scrub_vec(this._invKeySchedule)
scrub_vec(this._key)
}
AES.prototype._doCryptBlock = function(M, keySchedule, SUB_MIX, SBOX) {
var ksRow, round, s0, s1, s2, s3, t0, t1, t2, t3, _i, _ref;
AES.prototype._doCryptBlock = function (M, keySchedule, SUB_MIX, SBOX) {
var ksRow, s0, s1, s2, s3, t0, t1, t2, t3
s0 = M[0] ^ keySchedule[0];
s1 = M[1] ^ keySchedule[1];
s2 = M[2] ^ keySchedule[2];
s3 = M[3] ^ keySchedule[3];
ksRow = 4;
for (round = _i = 1, _ref = this._nRounds; 1 <= _ref ? _i < _ref : _i > _ref; round = 1 <= _ref ? ++_i : --_i) {
t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++];
t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++];
t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++];
t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++];
s0 = t0;
s1 = t1;
s2 = t2;
s3 = t3;
s0 = M[0] ^ keySchedule[0]
s1 = M[1] ^ keySchedule[1]
s2 = M[2] ^ keySchedule[2]
s3 = M[3] ^ keySchedule[3]
ksRow = 4
for (var round = 1; round < this._nRounds; round++) {
t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++]
t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++]
t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++]
t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++]
s0 = t0
s1 = t1
s2 = t2
s3 = t3
}
t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]
t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]
t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]
t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]
return [

@@ -192,9 +175,5 @@ fixup_uint32(t0),

fixup_uint32(t3)
];
]
}
};
exports.AES = AES;
exports.AES = AES

@@ -1,99 +0,97 @@

var aes = require('./aes');
var Transform = require('./cipherBase');
var inherits = require('inherits');
var GHASH = require('./ghash');
var xor = require('./xor');
inherits(StreamCipher, Transform);
module.exports = StreamCipher;
var aes = require('./aes')
var Transform = require('./cipherBase')
var inherits = require('inherits')
var GHASH = require('./ghash')
var xor = require('./xor')
inherits(StreamCipher, Transform)
module.exports = StreamCipher
function StreamCipher(mode, key, iv, decrypt) {
function StreamCipher (mode, key, iv, decrypt) {
if (!(this instanceof StreamCipher)) {
return new StreamCipher(mode, key, iv);
return new StreamCipher(mode, key, iv)
}
Transform.call(this);
this._finID = Buffer.concat([iv, new Buffer([0, 0, 0, 1])]);
iv = Buffer.concat([iv, new Buffer([0, 0, 0, 2])]);
this._cipher = new aes.AES(key);
this._prev = new Buffer(iv.length);
this._cache = new Buffer('');
this._secCache = new Buffer('');
this._decrypt = decrypt;
this._alen = 0;
this._len = 0;
iv.copy(this._prev);
this._mode = mode;
var h = new Buffer(4);
h.fill(0);
this._ghash = new GHASH(this._cipher.encryptBlock(h));
this._authTag = null;
this._called = false;
Transform.call(this)
this._finID = Buffer.concat([iv, new Buffer([0, 0, 0, 1])])
iv = Buffer.concat([iv, new Buffer([0, 0, 0, 2])])
this._cipher = new aes.AES(key)
this._prev = new Buffer(iv.length)
this._cache = new Buffer('')
this._secCache = new Buffer('')
this._decrypt = decrypt
this._alen = 0
this._len = 0
iv.copy(this._prev)
this._mode = mode
var h = new Buffer(4)
h.fill(0)
this._ghash = new GHASH(this._cipher.encryptBlock(h))
this._authTag = null
this._called = false
}
StreamCipher.prototype._update = function (chunk) {
if (!this._called && this._alen) {
var rump = 16 - (this._alen % 16);
if (rump <16) {
rump = new Buffer(rump);
rump.fill(0);
this._ghash.update(rump);
var rump = 16 - (this._alen % 16)
if (rump < 16) {
rump = new Buffer(rump)
rump.fill(0)
this._ghash.update(rump)
}
}
this._called = true;
var out = this._mode.encrypt(this, chunk);
this._called = true
var out = this._mode.encrypt(this, chunk)
if (this._decrypt) {
this._ghash.update(chunk);
this._ghash.update(chunk)
} else {
this._ghash.update(out);
this._ghash.update(out)
}
this._len += chunk.length;
return out;
};
this._len += chunk.length
return out
}
StreamCipher.prototype._final = function () {
if (this._decrypt && !this._authTag) {
throw new Error('Unsupported state or unable to authenticate data');
throw new Error('Unsupported state or unable to authenticate data')
}
var tag = xor(this._ghash.final(this._alen * 8, this._len * 8), this._cipher.encryptBlock(this._finID));
var tag = xor(this._ghash.final(this._alen * 8, this._len * 8), this._cipher.encryptBlock(this._finID))
if (this._decrypt) {
if (xorTest(tag, this._authTag)) {
throw new Error('Unsupported state or unable to authenticate data');
throw new Error('Unsupported state or unable to authenticate data')
}
} else {
this._authTag = tag;
this._authTag = tag
}
this._cipher.scrub();
};
this._cipher.scrub()
}
StreamCipher.prototype.getAuthTag = function getAuthTag () {
if (!this._decrypt && Buffer.isBuffer(this._authTag)) {
return this._authTag;
return this._authTag
} else {
throw new Error('Attempting to get auth tag in unsupported state');
throw new Error('Attempting to get auth tag in unsupported state')
}
};
}
StreamCipher.prototype.setAuthTag = function setAuthTag (tag) {
if (this._decrypt) {
this._authTag = tag;
this._authTag = tag
} else {
throw new Error('Attempting to set auth tag in unsupported state');
throw new Error('Attempting to set auth tag in unsupported state')
}
};
}
StreamCipher.prototype.setAAD = function setAAD (buf) {
if (!this._called) {
this._ghash.update(buf);
this._alen += buf.length;
this._ghash.update(buf)
this._alen += buf.length
} else {
throw new Error('Attempting to set AAD in unsupported state');
throw new Error('Attempting to set AAD in unsupported state')
}
};
function xorTest(a, b) {
var out = 0;
}
function xorTest (a, b) {
var out = 0
if (a.length !== b.length) {
out++;
out++
}
var len = Math.min(a.length, b.length);
var i = -1;
var len = Math.min(a.length, b.length)
var i = -1
while (++i < len) {
out += (a[i] ^ b[i]);
out += (a[i] ^ b[i])
}
return out;
return out
}

@@ -1,11 +0,11 @@

var ciphers = require('./encrypter');
exports.createCipher = exports.Cipher = ciphers.createCipher;
exports.createCipheriv = exports.Cipheriv = ciphers.createCipheriv;
var deciphers = require('./decrypter');
exports.createDecipher = exports.Decipher = deciphers.createDecipher;
exports.createDecipheriv = exports.Decipheriv = deciphers.createDecipheriv;
var modes = require('./modes');
var ciphers = require('./encrypter')
exports.createCipher = exports.Cipher = ciphers.createCipher
exports.createCipheriv = exports.Cipheriv = ciphers.createCipheriv
var deciphers = require('./decrypter')
exports.createDecipher = exports.Decipher = deciphers.createDecipher
exports.createDecipheriv = exports.Decipheriv = deciphers.createDecipheriv
var modes = require('./modes')
function getCiphers () {
return Object.keys(modes);
return Object.keys(modes)
}
exports.listCiphers = exports.getCiphers = getCiphers;
exports.listCiphers = exports.getCiphers = getCiphers

@@ -1,37 +0,37 @@

var Transform = require('stream').Transform;
var inherits = require('inherits');
var Transform = require('stream').Transform
var inherits = require('inherits')
module.exports = CipherBase;
inherits(CipherBase, Transform);
function CipherBase() {
Transform.call(this);
module.exports = CipherBase
inherits(CipherBase, Transform)
function CipherBase () {
Transform.call(this)
}
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
if (typeof data === 'string') {
data = new Buffer(data, inputEnc);
data = new Buffer(data, inputEnc)
}
var outData = this._update(data);
var outData = this._update(data)
if (outputEnc) {
outData = outData.toString(outputEnc);
outData = outData.toString(outputEnc)
}
return outData;
};
return outData
}
CipherBase.prototype._transform = function (data, _, next) {
this.push(this._update(data));
next();
};
this.push(this._update(data))
next()
}
CipherBase.prototype._flush = function (next) {
try {
this.push(this._final());
this.push(this._final())
} catch(e) {
return next(e);
return next(e)
}
next();
};
next()
}
CipherBase.prototype.final = function (outputEnc) {
var outData = this._final() || new Buffer('');
var outData = this._final() || new Buffer('')
if (outputEnc) {
outData = outData.toString(outputEnc);
outData = outData.toString(outputEnc)
}
return outData;
};
return outData
}

@@ -1,89 +0,89 @@

var aes = require('./aes');
var Transform = require('./cipherBase');
var inherits = require('inherits');
var modes = require('./modes');
var StreamCipher = require('./streamCipher');
var AuthCipher = require('./authCipher');
var ebtk = require('./EVP_BytesToKey');
var aes = require('./aes')
var Transform = require('./cipherBase')
var inherits = require('inherits')
var modes = require('./modes')
var StreamCipher = require('./streamCipher')
var AuthCipher = require('./authCipher')
var ebtk = require('./EVP_BytesToKey')
inherits(Decipher, Transform);
function Decipher(mode, key, iv) {
inherits(Decipher, Transform)
function Decipher (mode, key, iv) {
if (!(this instanceof Decipher)) {
return new Decipher(mode, key, iv);
return new Decipher(mode, key, iv)
}
Transform.call(this);
this._cache = new Splitter();
this._last = void 0;
this._cipher = new aes.AES(key);
this._prev = new Buffer(iv.length);
iv.copy(this._prev);
this._mode = mode;
this._autopadding = true;
Transform.call(this)
this._cache = new Splitter()
this._last = void 0
this._cipher = new aes.AES(key)
this._prev = new Buffer(iv.length)
iv.copy(this._prev)
this._mode = mode
this._autopadding = true
}
Decipher.prototype._update = function (data) {
this._cache.add(data);
var chunk;
var thing;
var out = [];
this._cache.add(data)
var chunk
var thing
var out = []
while ((chunk = this._cache.get(this._autopadding))) {
thing = this._mode.decrypt(this, chunk);
out.push(thing);
thing = this._mode.decrypt(this, chunk)
out.push(thing)
}
return Buffer.concat(out);
};
return Buffer.concat(out)
}
Decipher.prototype._final = function () {
var chunk = this._cache.flush();
var chunk = this._cache.flush()
if (this._autopadding) {
return unpad(this._mode.decrypt(this, chunk));
return unpad(this._mode.decrypt(this, chunk))
} else if (chunk) {
throw new Error('data not multiple of block length');
throw new Error('data not multiple of block length')
}
};
}
Decipher.prototype.setAutoPadding = function (setTo) {
this._autopadding = !!setTo;
};
function Splitter() {
if (!(this instanceof Splitter)) {
return new Splitter();
this._autopadding = !!setTo
}
function Splitter () {
if (!(this instanceof Splitter)) {
return new Splitter()
}
this.cache = new Buffer('');
this.cache = new Buffer('')
}
Splitter.prototype.add = function (data) {
this.cache = Buffer.concat([this.cache, data]);
};
this.cache = Buffer.concat([this.cache, data])
}
Splitter.prototype.get = function (autoPadding) {
var out;
var out
if (autoPadding) {
if (this.cache.length > 16) {
out = this.cache.slice(0, 16);
this.cache = this.cache.slice(16);
return out;
out = this.cache.slice(0, 16)
this.cache = this.cache.slice(16)
return out
}
} else {
if (this.cache.length >= 16) {
out = this.cache.slice(0, 16);
this.cache = this.cache.slice(16);
return out;
out = this.cache.slice(0, 16)
this.cache = this.cache.slice(16)
return out
}
}
return null;
};
return null
}
Splitter.prototype.flush = function () {
if (this.cache.length) {
return this.cache;
return this.cache
}
};
function unpad(last) {
var padded = last[15];
var i = -1;
}
function unpad (last) {
var padded = last[15]
var i = -1
while (++i < padded) {
if (last[(i + (16 - padded))] !== padded) {
throw new Error('unable to decrypt data');
throw new Error('unable to decrypt data')
}
}
if (padded === 16) {
return;
return
}
return last.slice(0, 16 - padded);
return last.slice(0, 16 - padded)
}

@@ -100,39 +100,38 @@

GCM: require('./modes/ctr')
};
}
function createDecipheriv(suite, password, iv) {
var config = modes[suite.toLowerCase()];
function createDecipheriv (suite, password, iv) {
var config = modes[suite.toLowerCase()]
if (!config) {
throw new TypeError('invalid suite type');
throw new TypeError('invalid suite type')
}
if (typeof iv === 'string') {
iv = new Buffer(iv);
iv = new Buffer(iv)
}
if (typeof password === 'string') {
password = new Buffer(password);
password = new Buffer(password)
}
if (password.length !== config.key/8) {
throw new TypeError('invalid key length ' + password.length);
if (password.length !== config.key / 8) {
throw new TypeError('invalid key length ' + password.length)
}
if (iv.length !== config.iv) {
throw new TypeError('invalid iv length ' + iv.length);
throw new TypeError('invalid iv length ' + iv.length)
}
if (config.type === 'stream') {
return new StreamCipher(modelist[config.mode], password, iv, true);
return new StreamCipher(modelist[config.mode], password, iv, true)
} else if (config.type === 'auth') {
return new AuthCipher(modelist[config.mode], password, iv, true);
return new AuthCipher(modelist[config.mode], password, iv, true)
}
return new Decipher(modelist[config.mode], password, iv);
return new Decipher(modelist[config.mode], password, iv)
}
function createDecipher (suite, password) {
var config = modes[suite.toLowerCase()];
var config = modes[suite.toLowerCase()]
if (!config) {
throw new TypeError('invalid suite type');
throw new TypeError('invalid suite type')
}
var keys = ebtk(password, config.key, config.iv);
return createDecipheriv(suite, keys.key, keys.iv);
var keys = ebtk(password, config.key, config.iv)
return createDecipheriv(suite, keys.key, keys.iv)
}
exports.createDecipher = createDecipher;
exports.createDecipheriv = createDecipheriv;
exports.createDecipher = createDecipher
exports.createDecipheriv = createDecipheriv

@@ -1,76 +0,76 @@

var aes = require('./aes');
var Transform = require('./cipherBase');
var inherits = require('inherits');
var modes = require('./modes');
var ebtk = require('./EVP_BytesToKey');
var StreamCipher = require('./streamCipher');
var AuthCipher = require('./authCipher');
inherits(Cipher, Transform);
function Cipher(mode, key, iv) {
var aes = require('./aes')
var Transform = require('./cipherBase')
var inherits = require('inherits')
var modes = require('./modes')
var ebtk = require('./EVP_BytesToKey')
var StreamCipher = require('./streamCipher')
var AuthCipher = require('./authCipher')
inherits(Cipher, Transform)
function Cipher (mode, key, iv) {
if (!(this instanceof Cipher)) {
return new Cipher(mode, key, iv);
return new Cipher(mode, key, iv)
}
Transform.call(this);
this._cache = new Splitter();
this._cipher = new aes.AES(key);
this._prev = new Buffer(iv.length);
iv.copy(this._prev);
this._mode = mode;
this._autopadding = true;
Transform.call(this)
this._cache = new Splitter()
this._cipher = new aes.AES(key)
this._prev = new Buffer(iv.length)
iv.copy(this._prev)
this._mode = mode
this._autopadding = true
}
Cipher.prototype._update = function (data) {
this._cache.add(data);
var chunk;
var thing;
var out = [];
this._cache.add(data)
var chunk
var thing
var out = []
while ((chunk = this._cache.get())) {
thing = this._mode.encrypt(this, chunk);
out.push(thing);
thing = this._mode.encrypt(this, chunk)
out.push(thing)
}
return Buffer.concat(out);
};
return Buffer.concat(out)
}
Cipher.prototype._final = function () {
var chunk = this._cache.flush();
var chunk = this._cache.flush()
if (this._autopadding) {
chunk = this._mode.encrypt(this, chunk);
this._cipher.scrub();
return chunk;
chunk = this._mode.encrypt(this, chunk)
this._cipher.scrub()
return chunk
} else if (chunk.toString('hex') !== '10101010101010101010101010101010') {
this._cipher.scrub();
throw new Error('data not multiple of block length');
this._cipher.scrub()
throw new Error('data not multiple of block length')
}
};
}
Cipher.prototype.setAutoPadding = function (setTo) {
this._autopadding = !!setTo;
};
this._autopadding = !!setTo
}
function Splitter() {
if (!(this instanceof Splitter)) {
return new Splitter();
function Splitter () {
if (!(this instanceof Splitter)) {
return new Splitter()
}
this.cache = new Buffer('');
this.cache = new Buffer('')
}
Splitter.prototype.add = function (data) {
this.cache = Buffer.concat([this.cache, data]);
};
this.cache = Buffer.concat([this.cache, data])
}
Splitter.prototype.get = function () {
if (this.cache.length > 15) {
var out = this.cache.slice(0, 16);
this.cache = this.cache.slice(16);
return out;
var out = this.cache.slice(0, 16)
this.cache = this.cache.slice(16)
return out
}
return null;
};
return null
}
Splitter.prototype.flush = function () {
var len = 16 - this.cache.length;
var padBuff = new Buffer(len);
var len = 16 - this.cache.length
var padBuff = new Buffer(len)
var i = -1;
var i = -1
while (++i < len) {
padBuff.writeUInt8(len, i);
padBuff.writeUInt8(len, i)
}
var out = Buffer.concat([this.cache, padBuff]);
return out;
};
var out = Buffer.concat([this.cache, padBuff])
return out
}
var modelist = {

@@ -85,38 +85,38 @@ ECB: require('./modes/ecb'),

GCM: require('./modes/ctr')
};
}
function createCipheriv(suite, password, iv) {
var config = modes[suite.toLowerCase()];
function createCipheriv (suite, password, iv) {
var config = modes[suite.toLowerCase()]
if (!config) {
throw new TypeError('invalid suite type');
throw new TypeError('invalid suite type')
}
if (typeof iv === 'string') {
iv = new Buffer(iv);
iv = new Buffer(iv)
}
if (typeof password === 'string') {
password = new Buffer(password);
password = new Buffer(password)
}
if (password.length !== config.key/8) {
throw new TypeError('invalid key length ' + password.length);
if (password.length !== config.key / 8) {
throw new TypeError('invalid key length ' + password.length)
}
if (iv.length !== config.iv) {
throw new TypeError('invalid iv length ' + iv.length);
throw new TypeError('invalid iv length ' + iv.length)
}
if (config.type === 'stream') {
return new StreamCipher(modelist[config.mode], password, iv);
return new StreamCipher(modelist[config.mode], password, iv)
} else if (config.type === 'auth') {
return new AuthCipher(modelist[config.mode], password, iv);
return new AuthCipher(modelist[config.mode], password, iv)
}
return new Cipher(modelist[config.mode], password, iv);
return new Cipher(modelist[config.mode], password, iv)
}
function createCipher (suite, password) {
var config = modes[suite.toLowerCase()];
var config = modes[suite.toLowerCase()]
if (!config) {
throw new TypeError('invalid suite type');
throw new TypeError('invalid suite type')
}
var keys = ebtk(password, config.key, config.iv);
return createCipheriv(suite, keys.key, keys.iv);
var keys = ebtk(password, config.key, config.iv)
return createCipheriv(suite, keys.key, keys.iv)
}
exports.createCipheriv = createCipheriv;
exports.createCipher = createCipher;
exports.createCipheriv = createCipheriv
exports.createCipher = createCipher

@@ -1,57 +0,57 @@

var md5 = require('create-hash/md5');
module.exports = EVP_BytesToKey;
function EVP_BytesToKey(password, keyLen, ivLen) {
var md5 = require('create-hash/md5')
module.exports = EVP_BytesToKey
function EVP_BytesToKey (password, keyLen, ivLen) {
if (!Buffer.isBuffer(password)) {
password = new Buffer(password, 'binary');
password = new Buffer(password, 'binary')
}
keyLen = keyLen/8;
ivLen = ivLen || 0;
var ki = 0;
var ii = 0;
var key = new Buffer(keyLen);
var iv = new Buffer(ivLen);
var addmd = 0;
var md_buf;
var i;
var bufs = [];
keyLen = keyLen / 8
ivLen = ivLen || 0
var ki = 0
var ii = 0
var key = new Buffer(keyLen)
var iv = new Buffer(ivLen)
var addmd = 0
var md_buf
var i
var bufs = []
while (true) {
if(addmd++ > 0) {
bufs.push(md_buf);
if (addmd++ > 0) {
bufs.push(md_buf)
}
bufs.push(password);
md_buf = md5(Buffer.concat(bufs));
bufs = [];
i = 0;
if(keyLen > 0) {
while(true) {
if(keyLen === 0) {
break;
bufs.push(password)
md_buf = md5(Buffer.concat(bufs))
bufs = []
i = 0
if (keyLen > 0) {
while (true) {
if (keyLen === 0) {
break
}
if(i === md_buf.length) {
break;
if (i === md_buf.length) {
break
}
key[ki++] = md_buf[i];
keyLen--;
i++;
}
key[ki++] = md_buf[i]
keyLen--
i++
}
}
if(ivLen > 0 && i !== md_buf.length) {
while(true) {
if(ivLen === 0) {
break;
if (ivLen > 0 && i !== md_buf.length) {
while (true) {
if (ivLen === 0) {
break
}
if(i === md_buf.length) {
break;
if (i === md_buf.length) {
break
}
iv[ii++] = md_buf[i];
ivLen--;
i++;
}
}
if(keyLen === 0 && ivLen === 0) {
break;
iv[ii++] = md_buf[i]
ivLen--
i++
}
}
if (keyLen === 0 && ivLen === 0) {
break
}
}
for(i=0;i<md_buf.length;i++) {
md_buf[i] = 0;
for (i = 0; i < md_buf.length; i++) {
md_buf[i] = 0
}

@@ -61,3 +61,3 @@ return {

iv: iv
};
}
}
}

@@ -1,9 +0,9 @@

var zeros = new Buffer(16);
zeros.fill(0);
module.exports = GHASH;
function GHASH(key){
this.h = key;
this.state = new Buffer(16);
this.state.fill(0);
this.cache = new Buffer('');
var zeros = new Buffer(16)
zeros.fill(0)
module.exports = GHASH
function GHASH (key) {
this.h = key
this.state = new Buffer(16)
this.state.fill(0)
this.cache = new Buffer('')
}

@@ -13,58 +13,58 @@ // from http://bitwiseshiftleft.github.io/sjcl/doc/symbols/src/core_gcm.js.html

GHASH.prototype.ghash = function (block) {
var i = -1;
var i = -1
while (++i < block.length) {
this.state[i] ^= block[i];
this.state[i] ^= block[i]
}
this._multiply();
};
this._multiply()
}
GHASH.prototype._multiply = function () {
var Vi = toArray(this.h);
var Zi = [0, 0, 0, 0];
var j, xi, lsb_Vi;
var i = -1;
var Vi = toArray(this.h)
var Zi = [0, 0, 0, 0]
var j, xi, lsb_Vi
var i = -1
while (++i < 128) {
xi = (this.state[~~(i/8)] & (1 << (7-i%8))) !== 0;
xi = (this.state[~~(i / 8)] & (1 << (7 - i % 8))) !== 0
if (xi) {
// Z_i+1 = Z_i ^ V_i
Zi = xor(Zi, Vi);
Zi = xor(Zi, Vi)
}
// Store the value of LSB(V_i)
lsb_Vi = (Vi[3] & 1) !== 0;
lsb_Vi = (Vi[3] & 1) !== 0
// V_i+1 = V_i >> 1
for (j=3; j>0; j--) {
Vi[j] = (Vi[j] >>> 1) | ((Vi[j-1]&1) << 31);
for (j = 3; j > 0; j--) {
Vi[j] = (Vi[j] >>> 1) | ((Vi[j - 1] & 1) << 31)
}
Vi[0] = Vi[0] >>> 1;
Vi[0] = Vi[0] >>> 1
// If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R
if (lsb_Vi) {
Vi[0] = Vi[0] ^ (0xe1 << 24);
Vi[0] = Vi[0] ^ (0xe1 << 24)
}
}
this.state = fromArray(Zi);
};
this.state = fromArray(Zi)
}
GHASH.prototype.update = function (buf) {
this.cache = Buffer.concat([this.cache, buf]);
var chunk;
this.cache = Buffer.concat([this.cache, buf])
var chunk
while (this.cache.length >= 16) {
chunk = this.cache.slice(0, 16);
this.cache = this.cache.slice(16);
this.ghash(chunk);
chunk = this.cache.slice(0, 16)
this.cache = this.cache.slice(16)
this.ghash(chunk)
}
};
}
GHASH.prototype.final = function (abl, bl) {
if (this.cache.length) {
this.ghash(Buffer.concat([this.cache, zeros], 16));
this.ghash(Buffer.concat([this.cache, zeros], 16))
}
this.ghash(fromArray([
0, abl,
0, bl
]));
return this.state;
};
0, abl,
0, bl
]))
return this.state
}
function toArray(buf) {
function toArray (buf) {
return [

@@ -75,20 +75,20 @@ buf.readUInt32BE(0),

buf.readUInt32BE(12)
];
]
}
function fromArray(out) {
out = out.map(fixup_uint32);
var buf = new Buffer(16);
buf.writeUInt32BE(out[0], 0);
buf.writeUInt32BE(out[1], 4);
buf.writeUInt32BE(out[2], 8);
buf.writeUInt32BE(out[3], 12);
return buf;
function fromArray (out) {
out = out.map(fixup_uint32)
var buf = new Buffer(16)
buf.writeUInt32BE(out[0], 0)
buf.writeUInt32BE(out[1], 4)
buf.writeUInt32BE(out[2], 8)
buf.writeUInt32BE(out[3], 12)
return buf
}
var uint_max = Math.pow(2, 32);
function fixup_uint32(x) {
var ret, x_pos;
ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x;
return ret;
var uint_max = Math.pow(2, 32)
function fixup_uint32 (x) {
var ret, x_pos
ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x
return ret
}
function xor(a, b) {
function xor (a, b) {
return [

@@ -98,4 +98,4 @@ a[0] ^ b[0],

a[2] ^ b[2],
a[3] ^ b[3],
];
}
a[3] ^ b[3]
]
}

@@ -1,7 +0,7 @@

var crypto = require('crypto');
var crypto = require('crypto')
exports.createCipher = exports.Cipher = crypto.createCipher;
exports.createCipheriv = exports.Cipheriv = crypto.createCipheriv;
exports.createDecipher = exports.Decipher = crypto.createDecipher;
exports.createDecipheriv = exports.Decipheriv = crypto.createDecipheriv;
exports.listCiphers = exports.getCiphers = crypto.getCiphers;
exports.createCipher = exports.Cipher = crypto.createCipher
exports.createCipheriv = exports.Cipheriv = crypto.createCipheriv
exports.createDecipher = exports.Decipher = crypto.createDecipher
exports.createDecipheriv = exports.Decipheriv = crypto.createDecipheriv
exports.listCiphers = exports.getCiphers = crypto.getCiphers

@@ -7,3 +7,3 @@ exports['aes-128-ecb'] = {

type: 'block'
};
}
exports['aes-192-ecb'] = {

@@ -15,3 +15,3 @@ cipher: 'AES',

type: 'block'
};
}
exports['aes-256-ecb'] = {

@@ -23,3 +23,3 @@ cipher: 'AES',

type: 'block'
};
}
exports['aes-128-cbc'] = {

@@ -31,3 +31,3 @@ cipher: 'AES',

type: 'block'
};
}
exports['aes-192-cbc'] = {

@@ -39,3 +39,3 @@ cipher: 'AES',

type: 'block'
};
}
exports['aes-256-cbc'] = {

@@ -47,6 +47,6 @@ cipher: 'AES',

type: 'block'
};
exports['aes128'] = exports['aes-128-cbc'];
exports['aes192'] = exports['aes-192-cbc'];
exports['aes256'] = exports['aes-256-cbc'];
}
exports['aes128'] = exports['aes-128-cbc']
exports['aes192'] = exports['aes-192-cbc']
exports['aes256'] = exports['aes-256-cbc']
exports['aes-128-cfb'] = {

@@ -58,3 +58,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-192-cfb'] = {

@@ -66,3 +66,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-256-cfb'] = {

@@ -74,3 +74,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-128-cfb8'] = {

@@ -82,3 +82,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-192-cfb8'] = {

@@ -90,3 +90,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-256-cfb8'] = {

@@ -98,3 +98,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-128-cfb1'] = {

@@ -106,3 +106,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-192-cfb1'] = {

@@ -114,3 +114,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-256-cfb1'] = {

@@ -122,3 +122,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-128-ofb'] = {

@@ -130,3 +130,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-192-ofb'] = {

@@ -138,3 +138,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-256-ofb'] = {

@@ -146,3 +146,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-128-ctr'] = {

@@ -154,3 +154,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-192-ctr'] = {

@@ -162,3 +162,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-256-ctr'] = {

@@ -170,3 +170,3 @@ cipher: 'AES',

type: 'stream'
};
}
exports['aes-128-gcm'] = {

@@ -178,3 +178,3 @@ cipher: 'AES',

type: 'auth'
};
}
exports['aes-192-gcm'] = {

@@ -186,3 +186,3 @@ cipher: 'AES',

type: 'auth'
};
}
exports['aes-256-gcm'] = {

@@ -194,2 +194,2 @@ cipher: 'AES',

type: 'auth'
};
}

@@ -1,12 +0,12 @@

var xor = require('../xor');
var xor = require('../xor')
exports.encrypt = function (self, block) {
var data = xor(block, self._prev);
self._prev = self._cipher.encryptBlock(data);
return self._prev;
};
var data = xor(block, self._prev)
self._prev = self._cipher.encryptBlock(data)
return self._prev
}
exports.decrypt = function (self, block) {
var pad = self._prev;
self._prev = block;
var out = self._cipher.decryptBlock(block);
return xor(out, pad);
};
var pad = self._prev
self._prev = block
var out = self._cipher.decryptBlock(block)
return xor(out, pad)
}

@@ -1,27 +0,27 @@

var xor = require('../xor');
var xor = require('../xor')
exports.encrypt = function (self, data, decrypt) {
var out = new Buffer('');
var len;
var out = new Buffer('')
var len
while (data.length) {
if (self._cache.length === 0) {
self._cache = self._cipher.encryptBlock(self._prev);
self._prev = new Buffer('');
self._cache = self._cipher.encryptBlock(self._prev)
self._prev = new Buffer('')
}
if (self._cache.length <= data.length) {
len = self._cache.length;
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)]);
data = data.slice(len);
len = self._cache.length
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)])
data = data.slice(len)
} else {
out = Buffer.concat([out, encryptStart(self, data, decrypt)]);
break;
out = Buffer.concat([out, encryptStart(self, data, decrypt)])
break
}
}
return out;
};
function encryptStart(self, data, decrypt) {
var len = data.length;
var out = xor(data, self._cache);
self._cache = self._cache.slice(len);
self._prev = Buffer.concat([self._prev, decrypt?data:out]);
return out;
}
return out
}
function encryptStart (self, data, decrypt) {
var len = data.length
var out = xor(data, self._cache)
self._cache = self._cache.slice(len)
self._prev = Buffer.concat([self._prev, decrypt ? data : out])
return out
}

@@ -1,35 +0,34 @@

function encryptByte(self, byte, decrypt) {
var pad;
var i = -1;
var len = 8;
var out = 0;
var bit, value;
function encryptByte (self, byteParam, decrypt) {
var pad
var i = -1
var len = 8
var out = 0
var bit, value
while (++i < len) {
pad = self._cipher.encryptBlock(self._prev);
bit = (byte & (1 << (7-i))) ? 0x80:0;
value = pad[0] ^ bit;
out += ((value&0x80) >> (i%8));
self._prev = shiftIn(self._prev, decrypt?bit:value);
pad = self._cipher.encryptBlock(self._prev)
bit = (byteParam & (1 << (7 - i))) ? 0x80 : 0
value = pad[0] ^ bit
out += ((value & 0x80) >> (i % 8))
self._prev = shiftIn(self._prev, decrypt ? bit : value)
}
return out;
return out
}
exports.encrypt = function (self, chunk, decrypt) {
var len = chunk.length;
var out = new Buffer(len);
var i = -1;
var len = chunk.length
var out = new Buffer(len)
var i = -1
while (++i < len) {
out[i] = encryptByte(self, chunk[i], decrypt);
out[i] = encryptByte(self, chunk[i], decrypt)
}
return out;
};
function shiftIn(buffer, value) {
var len = buffer.length;
var i = -1;
var out = new Buffer(buffer.length);
buffer = Buffer.concat([buffer, new Buffer([value])]);
while(++i < len) {
out[i] = buffer[i]<<1 | buffer[i+1]>>(7);
return out
}
function shiftIn (buffer, value) {
var len = buffer.length
var i = -1
var out = new Buffer(buffer.length)
buffer = Buffer.concat([buffer, new Buffer([value])])
while (++i < len) {
out[i] = buffer[i] << 1 | buffer[i + 1] >> (7)
}
return out;
}
return out
}

@@ -1,15 +0,15 @@

function encryptByte(self, byte, decrypt) {
var pad = self._cipher.encryptBlock(self._prev);
var out = pad[0] ^ byte;
self._prev = Buffer.concat([self._prev.slice(1), new Buffer([decrypt?byte:out])]);
return out;
function encryptByte (self, byteParam, decrypt) {
var pad = self._cipher.encryptBlock(self._prev)
var out = pad[0] ^ byteParam
self._prev = Buffer.concat([self._prev.slice(1), new Buffer([decrypt ? byteParam : out])])
return out
}
exports.encrypt = function (self, chunk, decrypt) {
var len = chunk.length;
var out = new Buffer(len);
var i = -1;
var len = chunk.length
var out = new Buffer(len)
var i = -1
while (++i < len) {
out[i] = encryptByte(self, chunk[i], decrypt);
out[i] = encryptByte(self, chunk[i], decrypt)
}
return out;
};
return out
}

@@ -1,28 +0,28 @@

var xor = require('../xor');
function getBlock(self) {
var out = self._cipher.encryptBlock(self._prev);
incr32(self._prev);
return out;
var xor = require('../xor')
function getBlock (self) {
var out = self._cipher.encryptBlock(self._prev)
incr32(self._prev)
return out
}
exports.encrypt = function (self, chunk) {
while (self._cache.length < chunk.length) {
self._cache = Buffer.concat([self._cache, getBlock(self)]);
self._cache = Buffer.concat([self._cache, getBlock(self)])
}
var pad = self._cache.slice(0, chunk.length);
self._cache = self._cache.slice(chunk.length);
return xor(chunk, pad);
};
function incr32(iv) {
var len = iv.length;
var item;
var pad = self._cache.slice(0, chunk.length)
self._cache = self._cache.slice(chunk.length)
return xor(chunk, pad)
}
function incr32 (iv) {
var len = iv.length
var item
while (len--) {
item = iv.readUInt8(len);
item = iv.readUInt8(len)
if (item === 255) {
iv.writeUInt8(0, len);
iv.writeUInt8(0, len)
} else {
item++;
iv.writeUInt8(item, len);
break;
item++
iv.writeUInt8(item, len)
break
}
}
}
}
exports.encrypt = function (self, block) {
return self._cipher.encryptBlock(block);
};
return self._cipher.encryptBlock(block)
}
exports.decrypt = function (self, block) {
return self._cipher.decryptBlock(block);
};
return self._cipher.decryptBlock(block)
}

@@ -1,13 +0,13 @@

var xor = require('../xor');
function getBlock(self) {
self._prev = self._cipher.encryptBlock(self._prev);
return self._prev;
var xor = require('../xor')
function getBlock (self) {
self._prev = self._cipher.encryptBlock(self._prev)
return self._prev
}
exports.encrypt = function (self, chunk) {
while (self._cache.length < chunk.length) {
self._cache = Buffer.concat([self._cache, getBlock(self)]);
self._cache = Buffer.concat([self._cache, getBlock(self)])
}
var pad = self._cache.slice(0, chunk.length);
self._cache = self._cache.slice(chunk.length);
return xor(chunk, pad);
};
var pad = self._cache.slice(0, chunk.length)
self._cache = self._cache.slice(chunk.length)
return xor(chunk, pad)
}
{
"name": "browserify-aes",
"version": "1.0.0",
"version": "1.0.1",
"description": "aes, for browserify",

@@ -11,3 +11,3 @@ "browser": "browser.js",

"scripts": {
"test": "node test/index.js|tspec"
"test": "standard && node test/index.js|tspec"
},

@@ -34,5 +34,6 @@ "repository": {

"devDependencies": {
"tape": "^3.0.0",
"tap-spec": "^1.0.0"
"standard": "^3.7.3",
"tap-spec": "^1.0.0",
"tape": "^3.0.0"
}
}

@@ -1,25 +0,25 @@

var modes = require('./modes');
var fixtures = require('./test/fixtures.json');
var crypto = require('crypto');
var types = ['aes-128-cfb1','aes-192-cfb1','aes-256-cfb1'];
var ebtk = require('./EVP_BytesToKey');
var fs = require('fs');
var modes = require('./modes')
var fixtures = require('./test/fixtures.json')
var crypto = require('crypto')
var types = ['aes-128-cfb1', 'aes-192-cfb1', 'aes-256-cfb1']
var ebtk = require('./EVP_BytesToKey')
var fs = require('fs')
fixtures.forEach(function (fixture) {
types.forEach(function (cipher) {
var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))]);
buf = Buffer.concat([buf, suite.final()]);
fixture.results.ciphers[cipher] = buf.toString('hex');
var suite = crypto.createCipher(cipher, new Buffer(fixture.password))
var buf = new Buffer('')
buf = Buffer.concat([buf, suite.update(new Buffer(fixture.text))])
buf = Buffer.concat([buf, suite.final()])
fixture.results.ciphers[cipher] = buf.toString('hex')
if (modes[cipher].mode === 'ECB') {
return;
return
}
var suite2 = crypto.createCipheriv(cipher, ebtk(crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'));
var buf2 = new Buffer('');
buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))]);
buf2 = Buffer.concat([buf2, suite2.final()]);
fixture.results.cipherivs[cipher] = buf2.toString('hex');
});
});
fs.writeFileSync('./test/fixturesNew.json', JSON.stringify(fixtures, false, 4));
var suite2 = crypto.createCipheriv(cipher, ebtk(crypto, fixture.password, modes[cipher].key).key, new Buffer(fixture.iv, 'hex'))
var buf2 = new Buffer('')
buf2 = Buffer.concat([buf2, suite2.update(new Buffer(fixture.text))])
buf2 = Buffer.concat([buf2, suite2.final()])
fixture.results.cipherivs[cipher] = buf2.toString('hex')
})
})
fs.writeFileSync('./test/fixturesNew.json', JSON.stringify(fixtures, false, 4))

@@ -1,25 +0,25 @@

var aes = require('./aes');
var Transform = require('./cipherBase');
var inherits = require('inherits');
var aes = require('./aes')
var Transform = require('./cipherBase')
var inherits = require('inherits')
inherits(StreamCipher, Transform);
module.exports = StreamCipher;
function StreamCipher(mode, key, iv, decrypt) {
inherits(StreamCipher, Transform)
module.exports = StreamCipher
function StreamCipher (mode, key, iv, decrypt) {
if (!(this instanceof StreamCipher)) {
return new StreamCipher(mode, key, iv);
return new StreamCipher(mode, key, iv)
}
Transform.call(this);
this._cipher = new aes.AES(key);
this._prev = new Buffer(iv.length);
this._cache = new Buffer('');
this._secCache = new Buffer('');
this._decrypt = decrypt;
iv.copy(this._prev);
this._mode = mode;
Transform.call(this)
this._cipher = new aes.AES(key)
this._prev = new Buffer(iv.length)
this._cache = new Buffer('')
this._secCache = new Buffer('')
this._decrypt = decrypt
iv.copy(this._prev)
this._mode = mode
}
StreamCipher.prototype._update = function (chunk) {
return this._mode.encrypt(this, chunk, this._decrypt);
};
return this._mode.encrypt(this, chunk, this._decrypt)
}
StreamCipher.prototype._final = function () {
this._cipher.scrub();
};
this._cipher.scrub()
}

@@ -1,512 +0,502 @@

var test = require('tape');
var fixtures = require('./fixtures.json');
var _crypto = require('crypto');
var crypto = require('../browser.js');
var modes = require('../modes');
var types = Object.keys(modes);
var ebtk = require('../EVP_BytesToKey');
function isGCM(cipher) {
return modes[cipher].mode === 'GCM';
var test = require('tape')
var fixtures = require('./fixtures.json')
var _crypto = require('crypto')
var crypto = require('../browser.js')
var modes = require('../modes')
var types = Object.keys(modes)
var ebtk = require('../EVP_BytesToKey')
function isGCM (cipher) {
return modes[cipher].mode === 'GCM'
}
function isNode10() {
return process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) <= 10;
function isNode10 () {
return process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) <= 10
}
fixtures.forEach(function (fixture, i) {
//var ciphers = fixture.results.ciphers = {};
// var ciphers = fixture.results.ciphers = {}
types.forEach(function (cipher) {
if (isGCM(cipher)) {
return;
return
}
test('fixture ' + i + ' ' + cipher, function (t) {
t.plan(1);
var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
t.plan(1)
var suite = crypto.createCipher(cipher, new Buffer(fixture.password))
var buf = new Buffer('')
suite.on('data', function (d) {
buf = Buffer.concat([buf, d]);
});
buf = Buffer.concat([buf, d])
})
suite.on('error', function (e) {
console.log(e);
});
suite.on("end", function () {
// console.log(fixture.text);
// decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a');
// decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b');
t.equals(buf.toString('hex'), fixture.results.ciphers[cipher]);
});
suite.write(new Buffer(fixture.text));
suite.end();
});
console.log(e)
})
suite.on('end', function () {
// console.log(fixture.text)
// decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a')
// decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b')
t.equals(buf.toString('hex'), fixture.results.ciphers[cipher])
})
suite.write(new Buffer(fixture.text))
suite.end()
})
test('fixture ' + i + ' ' + cipher + '-legacy', function (t) {
t.plan(3);
var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password));
var buf2 = new Buffer('');
var inbuf = new Buffer(fixture.text);
var mid = ~~(inbuf.length/2);
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate');
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2');
buf = Buffer.concat([buf, suite.final()]);
buf2 = Buffer.concat([buf2, suite2.final()]);
t.equals(buf.toString('hex'), buf2.toString('hex'), 'final');
});
t.plan(3)
var suite = crypto.createCipher(cipher, new Buffer(fixture.password))
var buf = new Buffer('')
var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password))
var buf2 = new Buffer('')
var inbuf = new Buffer(fixture.text)
var mid = ~~(inbuf.length / 2)
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate')
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2')
buf = Buffer.concat([buf, suite.final()])
buf2 = Buffer.concat([buf2, suite2.final()])
t.equals(buf.toString('hex'), buf2.toString('hex'), 'final')
})
test('fixture ' + i + ' ' + cipher + '-decrypt', function (t) {
t.plan(1);
var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
t.plan(1)
var suite = crypto.createDecipher(cipher, new Buffer(fixture.password))
var buf = new Buffer('')
suite.on('data', function (d) {
buf = Buffer.concat([buf, d]);
});
buf = Buffer.concat([buf, d])
})
suite.on('error', function (e) {
console.log(e);
});
suite.on("end", function () {
// console.log(fixture.text);
// decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a');
// decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b');
t.equals(buf.toString('utf8'), fixture.text);
});
suite.write(new Buffer(fixture.results.ciphers[cipher], 'hex'));
suite.end();
});
console.log(e)
})
suite.on('end', function () {
// console.log(fixture.text)
// decriptNoPadding(cipher, new Buffer(fixture.password), buf.toString('hex'), 'a')
// decriptNoPadding(cipher, new Buffer(fixture.password), fixture.results.ciphers[cipher], 'b')
t.equals(buf.toString('utf8'), fixture.text)
})
suite.write(new Buffer(fixture.results.ciphers[cipher], 'hex'))
suite.end()
})
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
t.plan(4);
var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
var buf = new Buffer('');
var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password));
var buf2 = new Buffer('');
var inbuf = new Buffer(fixture.results.ciphers[cipher], 'hex');
var mid = ~~(inbuf.length/2);
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate');
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2');
buf = Buffer.concat([buf, suite.final()]);
buf2 = Buffer.concat([buf2, suite2.final()]);
t.equals(buf.toString('utf8'), fixture.text);
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final');
});
//var cipherivs = fixture.results.cipherivs = {};
types.forEach(function (cipher) {
if (modes[cipher].mode === 'ECB') {
return;
t.plan(4)
var suite = crypto.createDecipher(cipher, new Buffer(fixture.password))
var buf = new Buffer('')
var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password))
var buf2 = new Buffer('')
var inbuf = new Buffer(fixture.results.ciphers[cipher], 'hex')
var mid = ~~(inbuf.length / 2)
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate')
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2')
buf = Buffer.concat([buf, suite.final()])
buf2 = Buffer.concat([buf2, suite2.final()])
t.equals(buf.toString('utf8'), fixture.text)
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final')
})
})
types.forEach(function (cipher) {
if (modes[cipher].mode === 'ECB') {
return
}
if (isGCM(cipher) && isNode10()) {
return
}
test('fixture ' + i + ' ' + cipher + '-iv', function (t) {
if (isGCM(cipher)) {
t.plan(4)
} else {
t.plan(2)
}
if (isGCM(cipher) && isNode10()) {
return;
}
test('fixture ' + i + ' ' + cipher + '-iv', function (t) {
var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var buf = new Buffer('')
var buf2 = new Buffer('')
suite.on('data', function (d) {
buf = Buffer.concat([buf, d])
})
suite.on('error', function (e) {
console.log(e)
})
suite2.on('data', function (d) {
buf2 = Buffer.concat([buf2, d])
})
suite2.on('error', function (e) {
console.log(e)
})
suite.on('end', function () {
t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher], 'vs fixture')
t.equals(buf.toString('hex'), buf2.toString('hex'), 'vs node')
if (isGCM(cipher)) {
t.plan(4);
} else {
t.plan(2);
t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture')
t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node')
}
var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var buf = new Buffer('');
var buf2 = new Buffer('');
suite.on('data', function (d) {
buf = Buffer.concat([buf, d]);
});
suite.on('error', function (e) {
console.log(e);
});
suite2.on('data', function (d) {
buf2 = Buffer.concat([buf2, d]);
});
suite2.on('error', function (e) {
console.log(e);
});
suite.on("end", function () {
t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher], 'vs fixture');
t.equals(buf.toString('hex'), buf2.toString('hex'), 'vs node');
if (isGCM(cipher)) {
t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture');
t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node');
}
});
if (isGCM(cipher)) {
suite.setAAD(new Buffer(fixture.aad, 'hex'));
suite2.setAAD(new Buffer(fixture.aad, 'hex'));
}
suite2.write(new Buffer(fixture.text));
suite2.end();
suite.write(new Buffer(fixture.text));
suite.end();
});
test('fixture ' + i + ' ' + cipher + '-legacy-iv', function (t) {
if (isGCM(cipher)) {
t.plan(6);
} else {
t.plan(4);
}
var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var buf = new Buffer('');
var buf2 = new Buffer('');
var inbuf = new Buffer(fixture.text);
var mid = ~~(inbuf.length/2);
if (isGCM(cipher)) {
suite.setAAD(new Buffer(fixture.aad, 'hex'));
suite2.setAAD(new Buffer(fixture.aad, 'hex'));
}
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate');
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2');
buf = Buffer.concat([buf, suite.final()]);
buf2 = Buffer.concat([buf2, suite2.final()]);
t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher]);
t.equals(buf.toString('hex'), buf2.toString('hex'), 'final');
if (isGCM(cipher)) {
t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture');
t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node');
}
});
test('fixture ' + i + ' ' + cipher + '-iv-decrypt', function (t) {
t.plan(2);
var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var buf = new Buffer('');
var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var buf2 = new Buffer('');
suite.on('data', function (d) {
buf = Buffer.concat([buf, d]);
});
suite.on('error', function (e) {
t.notOk(e);
});
suite2.on('data', function (d) {
buf2 = Buffer.concat([buf2, d]);
});
suite2.on('error', function (e) {
t.notOk(e);
});
suite.on("end", function () {
t.equals(buf.toString('utf8'), fixture.text, 'correct text vs fixture');
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'correct text vs node');
});
if (isGCM(cipher)) {
suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
suite.setAAD(new Buffer(fixture.aad, 'hex'));
suite2.setAAD(new Buffer(fixture.aad, 'hex'));
}
suite2.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
suite.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
suite2.end();
suite.end();
});
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
t.plan(4);
var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var buf = new Buffer('');
var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
var buf2 = new Buffer('');
var inbuf = new Buffer(fixture.results.cipherivs[cipher], 'hex');
var mid = ~~(inbuf.length/2);
if (isGCM(cipher)) {
suite.setAAD(new Buffer(fixture.aad, 'hex'));
suite2.setAAD(new Buffer(fixture.aad, 'hex'));
suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
}
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
})
if (isGCM(cipher)) {
suite.setAAD(new Buffer(fixture.aad, 'hex'))
suite2.setAAD(new Buffer(fixture.aad, 'hex'))
}
suite2.write(new Buffer(fixture.text))
suite2.end()
suite.write(new Buffer(fixture.text))
suite.end()
})
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate');
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2');
buf = Buffer.concat([buf, suite.final()]);
buf2 = Buffer.concat([buf2, suite2.final()]);
t.equals(buf.toString('utf8'), fixture.text);
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final');
});
});
});
});
test('fixture ' + i + ' ' + cipher + '-legacy-iv', function (t) {
if (isGCM(cipher)) {
t.plan(6)
} else {
t.plan(4)
}
var suite = crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var suite2 = _crypto.createCipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var buf = new Buffer('')
var buf2 = new Buffer('')
var inbuf = new Buffer(fixture.text)
var mid = ~~(inbuf.length / 2)
if (isGCM(cipher)) {
suite.setAAD(new Buffer(fixture.aad, 'hex'))
suite2.setAAD(new Buffer(fixture.aad, 'hex'))
}
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate')
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2')
buf = Buffer.concat([buf, suite.final()])
buf2 = Buffer.concat([buf2, suite2.final()])
t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher])
t.equals(buf.toString('hex'), buf2.toString('hex'), 'final')
if (isGCM(cipher)) {
t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture')
t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node')
}
})
test('fixture ' + i + ' ' + cipher + '-iv-decrypt', function (t) {
t.plan(2)
var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var buf = new Buffer('')
var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var buf2 = new Buffer('')
suite.on('data', function (d) {
buf = Buffer.concat([buf, d])
})
suite.on('error', function (e) {
t.notOk(e)
})
suite2.on('data', function (d) {
buf2 = Buffer.concat([buf2, d])
})
suite2.on('error', function (e) {
t.notOk(e)
})
suite.on('end', function () {
t.equals(buf.toString('utf8'), fixture.text, 'correct text vs fixture')
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'correct text vs node')
})
if (isGCM(cipher)) {
suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
suite.setAAD(new Buffer(fixture.aad, 'hex'))
suite2.setAAD(new Buffer(fixture.aad, 'hex'))
}
suite2.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'))
suite.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'))
suite2.end()
suite.end()
})
test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
t.plan(4)
var suite = crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var buf = new Buffer('')
var suite2 = _crypto.createDecipheriv(cipher, ebtk(fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')))
var buf2 = new Buffer('')
var inbuf = new Buffer(fixture.results.cipherivs[cipher], 'hex')
var mid = ~~(inbuf.length / 2)
if (isGCM(cipher)) {
suite.setAAD(new Buffer(fixture.aad, 'hex'))
suite2.setAAD(new Buffer(fixture.aad, 'hex'))
suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'))
}
buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))])
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate')
buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))])
buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))])
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2')
buf = Buffer.concat([buf, suite.final()])
buf2 = Buffer.concat([buf2, suite2.final()])
t.equals(buf.toString('utf8'), fixture.text)
t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final')
})
})
})
if (!isNode10()) {
test('node tests', function (t) {
var TEST_CASES = [
{ algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
iv: '583673497131313748307652', plain: 'Hello World!',
ct: '4BE13896F64DFA2C2D0F2C76',
tag: '272B422F62EB545EAA15B5FF84092447', tampered: false },
{ algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
iv: '583673497131313748307652', plain: 'Hello World!',
ct: '4BE13896F64DFA2C2D0F2C76', aad: '000000FF',
tag: 'BA2479F66275665A88CB7B15F43EB005', tampered: false },
{ algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
iv: '583673497131313748307652', plain: 'Hello World!',
ct: '4BE13596F64DFA2C2D0FAC76',
tag: '272B422F62EB545EAA15B5FF84092447', tampered: true },
{ algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false },
{ algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true },
{ algo: 'aes-192-gcm', key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
iv: '0e1791e9db3bd21a9122c416', plain: 'Hello node.js world!',
password: 'very bad password', aad: '63616c76696e',
ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
tag: 'D2A35E5C611E5E3D2258360241C5B045', tampered: false }
];
var TEST_CASES = [
{ algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
iv: '583673497131313748307652', plain: 'Hello World!',
ct: '4BE13896F64DFA2C2D0F2C76',
tag: '272B422F62EB545EAA15B5FF84092447', tampered: false },
{ algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
iv: '583673497131313748307652', plain: 'Hello World!',
ct: '4BE13896F64DFA2C2D0F2C76', aad: '000000FF',
tag: 'BA2479F66275665A88CB7B15F43EB005', tampered: false },
{ algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
iv: '583673497131313748307652', plain: 'Hello World!',
ct: '4BE13596F64DFA2C2D0FAC76',
tag: '272B422F62EB545EAA15B5FF84092447', tampered: true },
{ algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false },
{ algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true },
{ algo: 'aes-192-gcm', key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
iv: '0e1791e9db3bd21a9122c416', plain: 'Hello node.js world!',
password: 'very bad password', aad: '63616c76696e',
ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
tag: 'D2A35E5C611E5E3D2258360241C5B045', tampered: false }
]
var ciphers = Object.keys(modes);
function testIt(i) {
t.test('test case ' + i, function (t) {
var test = TEST_CASES[i];
var ciphers = Object.keys(modes)
function testIt (i) {
t.test('test case ' + i, function (t) {
var test = TEST_CASES[i]
if (ciphers.indexOf(test.algo) == -1) {
console.log('skipping unsupported ' + test.algo + ' test');
return;
}
if (ciphers.indexOf(test.algo) === -1) {
console.log('skipping unsupported ' + test.algo + ' test')
return
}
(function() {
var encrypt = crypto.createCipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
if (test.aad)
encrypt.setAAD(new Buffer(test.aad, 'hex'));
var hex = encrypt.update(test.plain, 'ascii', 'hex');
hex += encrypt.final('hex');
var auth_tag = encrypt.getAuthTag();
// only test basic encryption run if output is marked as tampered.
if (!test.tampered) {
t.equal(hex.toUpperCase(), test.ct);
t.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
}
})();
(function () {
var encrypt = crypto.createCipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
if (test.aad) encrypt.setAAD(new Buffer(test.aad, 'hex'))
(function() {
var decrypt = crypto.createDecipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
if (test.aad)
decrypt.setAAD(new Buffer(test.aad, 'hex'));
var msg = decrypt.update(test.ct, 'hex', 'ascii');
if (!test.tampered) {
msg += decrypt.final('ascii');
t.equal(msg, test.plain);
} else {
// assert that final throws if input data could not be verified!
t.throws(function() { decrypt.final('ascii'); }, / auth/);
}
})();
var hex = encrypt.update(test.plain, 'ascii', 'hex')
hex += encrypt.final('hex')
var auth_tag = encrypt.getAuthTag()
// only test basic encryption run if output is marked as tampered.
if (!test.tampered) {
t.equal(hex.toUpperCase(), test.ct)
t.equal(auth_tag.toString('hex').toUpperCase(), test.tag)
}
})()
;(function () {
var decrypt = crypto.createDecipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
decrypt.setAuthTag(new Buffer(test.tag, 'hex'))
if (test.aad) decrypt.setAAD(new Buffer(test.aad, 'hex'))
var msg = decrypt.update(test.ct, 'hex', 'ascii')
if (!test.tampered) {
msg += decrypt.final('ascii')
t.equal(msg, test.plain)
} else {
// assert that final throws if input data could not be verified!
t.throws(function () { decrypt.final('ascii') }, / auth/)
}
})()
;(function () {
if (!test.password) return
var encrypt = crypto.createCipher(test.algo, test.password)
if (test.aad) encrypt.setAAD(new Buffer(test.aad, 'hex'))
var hex = encrypt.update(test.plain, 'ascii', 'hex')
hex += encrypt.final('hex')
var auth_tag = encrypt.getAuthTag()
// only test basic encryption run if output is marked as tampered.
if (!test.tampered) {
t.equal(hex.toUpperCase(), test.ct)
t.equal(auth_tag.toString('hex').toUpperCase(), test.tag)
}
})()
;(function () {
if (!test.password) return
var decrypt = crypto.createDecipher(test.algo, test.password)
decrypt.setAuthTag(new Buffer(test.tag, 'hex'))
if (test.aad) decrypt.setAAD(new Buffer(test.aad, 'hex'))
var msg = decrypt.update(test.ct, 'hex', 'ascii')
if (!test.tampered) {
msg += decrypt.final('ascii')
t.equal(msg, test.plain)
} else {
// assert that final throws if input data could not be verified!
t.throws(function () { decrypt.final('ascii') }, / auth/)
}
})()
(function() {
if (!test.password) return;
var encrypt = crypto.createCipher(test.algo, test.password);
if (test.aad)
encrypt.setAAD(new Buffer(test.aad, 'hex'));
var hex = encrypt.update(test.plain, 'ascii', 'hex');
hex += encrypt.final('hex');
var auth_tag = encrypt.getAuthTag();
// only test basic encryption run if output is marked as tampered.
if (!test.tampered) {
t.equal(hex.toUpperCase(), test.ct);
t.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
}
})();
// after normal operation, test some incorrect ways of calling the API:
// it's most certainly enough to run these tests with one algorithm only.
(function() {
if (!test.password) return;
var decrypt = crypto.createDecipher(test.algo, test.password);
decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
if (test.aad)
decrypt.setAAD(new Buffer(test.aad, 'hex'));
var msg = decrypt.update(test.ct, 'hex', 'ascii');
if (!test.tampered) {
msg += decrypt.final('ascii');
t.equal(msg, test.plain);
} else {
// assert that final throws if input data could not be verified!
t.throws(function() { decrypt.final('ascii'); }, / auth/);
if (i > 0) {
t.end()
return
}
(function () {
// non-authenticating mode:
var encrypt = crypto.createCipheriv('aes-128-cbc',
'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC')
encrypt.update('blah', 'ascii')
encrypt.final()
t.throws(function () { encrypt.getAuthTag() })
t.throws(function () {
encrypt.setAAD(new Buffer('123', 'ascii'))
})
})()
;(function () {
// trying to get tag before inputting all data:
var encrypt = crypto.createCipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
encrypt.update('blah', 'ascii')
t.throws(function () { encrypt.getAuthTag() }, / state/)
})()
;(function () {
// trying to set tag on encryption object:
var encrypt = crypto.createCipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
t.throws(function () {
encrypt.setAuthTag(new Buffer(test.tag, 'hex'))
}, / state/)
})()
;(function () {
// trying to read tag from decryption object:
var decrypt = crypto.createDecipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'))
t.throws(function () { decrypt.getAuthTag() }, / state/)
})()
t.end()
})
}
})();
// after normal operation, test some incorrect ways of calling the API:
// it's most certainly enough to run these tests with one algorithm only.
if (i > 0) {
t.end();
return;
}
(function() {
// non-authenticating mode:
var encrypt = crypto.createCipheriv('aes-128-cbc',
'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC');
encrypt.update('blah', 'ascii');
encrypt.final();
t.throws(function() { encrypt.getAuthTag(); });
t.throws(function() {
encrypt.setAAD(new Buffer('123', 'ascii')); });
})();
(function() {
// trying to get tag before inputting all data:
var encrypt = crypto.createCipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
encrypt.update('blah', 'ascii');
t.throws(function() { encrypt.getAuthTag(); }, / state/);
})();
(function() {
// trying to set tag on encryption object:
var encrypt = crypto.createCipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
t.throws(function() {
encrypt.setAuthTag(new Buffer(test.tag, 'hex')); }, / state/);
})();
(function() {
// trying to read tag from decryption object:
var decrypt = crypto.createDecipheriv(test.algo,
new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
t.throws(function() { decrypt.getAuthTag(); }, / state/);
})();
t.end();
});
for (var i in TEST_CASES) {
testIt(i)
}
})
}
for (var i in TEST_CASES) {
testIt(i);
}
});
}
function corectPaddingWords(padding, result) {
function corectPaddingWords (padding, result) {
test('correct padding ' + padding.toString('hex'), function (t) {
t.plan(1);
var block1 = new Buffer(16);
block1.fill(4);
result = block1.toString('hex') + result.toString('hex');
var cipher = _crypto.createCipher('aes128', new Buffer('password'));
cipher.setAutoPadding(false);
var decipher = crypto.createDecipher('aes128', new Buffer('password'));
var out = new Buffer('');
out = Buffer.concat([out, cipher.update(block1)]);
out = Buffer.concat([out, cipher.update(padding)]);
var deciphered = decipher.update(out);
deciphered = Buffer.concat([deciphered, decipher.final()]);
t.equals(deciphered.toString('hex'), result);
});
t.plan(1)
var block1 = new Buffer(16)
block1.fill(4)
result = block1.toString('hex') + result.toString('hex')
var cipher = _crypto.createCipher('aes128', new Buffer('password'))
cipher.setAutoPadding(false)
var decipher = crypto.createDecipher('aes128', new Buffer('password'))
var out = new Buffer('')
out = Buffer.concat([out, cipher.update(block1)])
out = Buffer.concat([out, cipher.update(padding)])
var deciphered = decipher.update(out)
deciphered = Buffer.concat([deciphered, decipher.final()])
t.equals(deciphered.toString('hex'), result)
})
}
var sixteens = new Buffer(16);
sixteens.fill(16);
corectPaddingWords(sixteens, new Buffer(''));
var fifteens = new Buffer(16);
fifteens.fill(15);
fifteens[0] = 5;
corectPaddingWords(fifteens, new Buffer([5]));
var one = _crypto.randomBytes(16);
one[15] = 1;
corectPaddingWords(one, one.slice(0, -1));
function incorectPaddingthrows(padding) {
var sixteens = new Buffer(16)
sixteens.fill(16)
corectPaddingWords(sixteens, new Buffer(''))
var fifteens = new Buffer(16)
fifteens.fill(15)
fifteens[0] = 5
corectPaddingWords(fifteens, new Buffer([5]))
var one = _crypto.randomBytes(16)
one[15] = 1
corectPaddingWords(one, one.slice(0, -1))
function incorectPaddingthrows (padding) {
test('incorrect padding ' + padding.toString('hex'), function (t) {
t.plan(2);
var block1 = new Buffer(16);
block1.fill(4);
var cipher = crypto.createCipher('aes128', new Buffer('password'));
cipher.setAutoPadding(false);
var decipher = crypto.createDecipher('aes128', new Buffer('password'));
var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'));
var out = new Buffer('');
out = Buffer.concat([out, cipher.update(block1)]);
out = Buffer.concat([out, cipher.update(padding)]);
decipher.update(out);
decipher2.update(out);
t.plan(2)
var block1 = new Buffer(16)
block1.fill(4)
var cipher = crypto.createCipher('aes128', new Buffer('password'))
cipher.setAutoPadding(false)
var decipher = crypto.createDecipher('aes128', new Buffer('password'))
var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'))
var out = new Buffer('')
out = Buffer.concat([out, cipher.update(block1)])
out = Buffer.concat([out, cipher.update(padding)])
decipher.update(out)
decipher2.update(out)
t.throws(function () {
decipher.final();
}, 'mine');
decipher.final()
}, 'mine')
t.throws(function () {
decipher2.final();
}, 'node');
});
decipher2.final()
}, 'node')
})
}
function incorectPaddingDoesNotThrow(padding) {
function incorectPaddingDoesNotThrow (padding) {
test('stream incorrect padding ' + padding.toString('hex'), function (t) {
t.plan(2);
var block1 = new Buffer(16);
block1.fill(4);
var cipher = crypto.createCipher('aes128', new Buffer('password'));
cipher.setAutoPadding(false);
var decipher = crypto.createDecipher('aes128', new Buffer('password'));
var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'));
cipher.pipe(decipher);
cipher.pipe(decipher2);
cipher.write(block1);
cipher.write(padding);
t.plan(2)
var block1 = new Buffer(16)
block1.fill(4)
var cipher = crypto.createCipher('aes128', new Buffer('password'))
cipher.setAutoPadding(false)
var decipher = crypto.createDecipher('aes128', new Buffer('password'))
var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'))
cipher.pipe(decipher)
cipher.pipe(decipher2)
cipher.write(block1)
cipher.write(padding)
decipher.on('error', function (e) {
t.ok(e, 'mine');
});
t.ok(e, 'mine')
})
decipher2.on('error', function (e) {
t.ok(e, 'node');
});
cipher.end();
});
t.ok(e, 'node')
})
cipher.end()
})
}
var sixteens2 = new Buffer(16);
sixteens2.fill(16);
sixteens2[3] = 5;
incorectPaddingthrows(sixteens2);
incorectPaddingDoesNotThrow(sixteens2);
var fifteens2 = new Buffer(16);
fifteens2.fill(15);
fifteens2[0] = 5;
fifteens2[1] = 6;
incorectPaddingthrows(fifteens2);
incorectPaddingDoesNotThrow(fifteens2);
var two = _crypto.randomBytes(16);
two[15] = 2;
two[14] = 1;
incorectPaddingthrows(two);
incorectPaddingDoesNotThrow(two);
var sixteens2 = new Buffer(16)
sixteens2.fill(16)
sixteens2[3] = 5
incorectPaddingthrows(sixteens2)
incorectPaddingDoesNotThrow(sixteens2)
var fifteens2 = new Buffer(16)
fifteens2.fill(15)
fifteens2[0] = 5
fifteens2[1] = 6
incorectPaddingthrows(fifteens2)
incorectPaddingDoesNotThrow(fifteens2)
var two = _crypto.randomBytes(16)
two[15] = 2
two[14] = 1
incorectPaddingthrows(two)
incorectPaddingDoesNotThrow(two)
test('autopadding false decipher', function (t) {
t.plan(2);
var mycipher = crypto.createCipher('AES-128-ECB', new Buffer('password'));
var nodecipher = _crypto.createCipher('AES-128-ECB', new Buffer('password'));
var myEnc = mycipher.final();
var nodeEnc = nodecipher.final();
t.equals(myEnc.toString('hex'), nodeEnc.toString('hex'), 'same encryption');
var decipher = crypto.createDecipher('aes-128-ecb', new Buffer('password'));
decipher.setAutoPadding(false);
var decipher2 = _crypto.createDecipher('aes-128-ecb', new Buffer('password'));
decipher2.setAutoPadding(false);
t.equals(decipher.update(myEnc).toString('hex'), decipher2.update(nodeEnc).toString('hex'), 'same decryption');
});
t.plan(2)
var mycipher = crypto.createCipher('AES-128-ECB', new Buffer('password'))
var nodecipher = _crypto.createCipher('AES-128-ECB', new Buffer('password'))
var myEnc = mycipher.final()
var nodeEnc = nodecipher.final()
t.equals(myEnc.toString('hex'), nodeEnc.toString('hex'), 'same encryption')
var decipher = crypto.createDecipher('aes-128-ecb', new Buffer('password'))
decipher.setAutoPadding(false)
var decipher2 = _crypto.createDecipher('aes-128-ecb', new Buffer('password'))
decipher2.setAutoPadding(false)
t.equals(decipher.update(myEnc).toString('hex'), decipher2.update(nodeEnc).toString('hex'), 'same decryption')
})
test('autopadding false cipher throws', function (t) {
t.plan(2);
var mycipher = crypto.createCipher('aes-128-ecb', new Buffer('password'));
mycipher.setAutoPadding(false);
var nodecipher = _crypto.createCipher('aes-128-ecb', new Buffer('password'));
nodecipher.setAutoPadding(false);
mycipher.update('foo');
nodecipher.update('foo');
t.plan(2)
var mycipher = crypto.createCipher('aes-128-ecb', new Buffer('password'))
mycipher.setAutoPadding(false)
var nodecipher = _crypto.createCipher('aes-128-ecb', new Buffer('password'))
nodecipher.setAutoPadding(false)
mycipher.update('foo')
nodecipher.update('foo')
t.throws(function () {
mycipher.final();
}, 'mine');
mycipher.final()
}, 'mine')
t.throws(function () {
nodecipher.final();
}, 'node');
});
nodecipher.final()
}, 'node')
})
test('getCiphers works', function (t) {
t.plan(1);
t.ok(crypto.getCiphers().length, 'get some ciphers');
})
t.plan(1)
t.ok(crypto.getCiphers().length, 'get some ciphers')
})

@@ -1,10 +0,10 @@

module.exports = xor;
function xor(a, b) {
var len = Math.min(a.length, b.length);
var out = new Buffer(len);
var i = -1;
module.exports = xor
function xor (a, b) {
var len = Math.min(a.length, b.length)
var out = new Buffer(len)
var i = -1
while (++i < len) {
out.writeUInt8(a[i] ^ b[i], i);
out.writeUInt8(a[i] ^ b[i], i)
}
return out;
}
return out
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc