Showing with 279 additions and 51 deletions.
  1. +254 −40 lib/crypto.js
  2. +1 −1 test/simple/test-crypto-padding-aes256.js
  3. +24 −10 test/simple/test-crypto.js
@@ -23,14 +23,6 @@
try {
var binding = process.binding('crypto');
var SecureContext = binding.SecureContext;
var Hmac = binding.Hmac;
var Hash = binding.Hash;
var Cipher = binding.Cipher;
var Decipher = binding.Decipher;
var Sign = binding.Sign;
var Verify = binding.Verify;
var DiffieHellman = binding.DiffieHellman;
var DiffieHellmanGroup = binding.DiffieHellmanGroup;
var PBKDF2 = binding.PBKDF2;
var randomBytes = binding.randomBytes;
var pseudoRandomBytes = binding.pseudoRandomBytes;
@@ -40,6 +32,8 @@ try {
var crypto = false;
}

var assert = require('assert');
var StringDecoder = require('string_decoder').StringDecoder;

function Credentials(secureProtocol, flags, context) {
if (!(this instanceof Credentials)) {
@@ -127,65 +121,285 @@ exports.createCredentials = function(options, context) {
};


exports.Hash = Hash;
exports.createHash = function(hash) {
return new Hash(hash);
exports.createHash = exports.Hash = Hash;
function Hash(algorithm) {
if (!(this instanceof Hash))
return new Hash(algorithm);
this._binding = new binding.Hash(algorithm);
}

Hash.prototype.update = function(data, encoding) {
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof data === 'string')
data = new Buffer(data, encoding);
this._binding.update(data);
return this;
};

Hash.prototype.digest = function(outputEncoding) {
var result = this._binding.digest('buffer');
if (outputEncoding && outputEncoding !== 'buffer')
result = result.toString(outputEncoding);
return result;
};


exports.Hmac = Hmac;
exports.createHmac = function(hmac, key) {
return (new Hmac).init(hmac, key);
exports.createHmac = exports.Hmac = Hmac;

function Hmac(hmac, key) {
if (!(this instanceof Hmac))
return new Hmac(hmac, key);
this._binding = new binding.Hmac();
this._binding.init(hmac, key);
};

Hmac.prototype.update = Hash.prototype.update;
Hmac.prototype.digest = Hash.prototype.digest;


function getDecoder(decoder, encoding) {
decoder = decoder || new StringDecoder(encoding);
assert(decoder.encoding === encoding, 'Cannot change encoding');
return decoder;
}


exports.Cipher = Cipher;
exports.createCipher = function(cipher, password) {
return (new Cipher).init(cipher, password);
exports.createCipher = exports.Cipher = Cipher;
function Cipher(cipher, password) {
if (!(this instanceof Cipher))
return new Cipher(cipher, password);
this._binding = new binding.Cipher;
this._binding.init(cipher, password);
this._decoder = null;
};

Cipher.prototype.update = function(data, inputEncoding, outputEncoding) {
if (inputEncoding && inputEncoding !== 'buffer')
data = new Buffer(data, inputEncoding);

var ret = this._binding.update(data, 'buffer', 'buffer');

if (outputEncoding && outputEncoding !== 'buffer') {
this._decoder = getDecoder(this._decoder, outputEncoding);
ret = this._decoder.write(ret);
}

exports.createCipheriv = function(cipher, key, iv) {
return (new Cipher).initiv(cipher, key, iv);
return ret;
};

Cipher.prototype.final = function(outputEncoding) {
var ret = this._binding.final('buffer');

if (outputEncoding && outputEncoding !== 'buffer') {
this._decoder = getDecoder(this._decoder, outputEncoding);
ret = this._decoder.write(ret);
}

return ret;
};

exports.Decipher = Decipher;
exports.createDecipher = function(cipher, password) {
return (new Decipher).init(cipher, password);
Cipher.prototype.setAutoPadding = function(ap) {
this._binding.setAutoPadding(ap);
return this;
};


exports.createDecipheriv = function(cipher, key, iv) {
return (new Decipher).initiv(cipher, key, iv);

exports.createCipheriv = exports.Cipheriv = Cipheriv;
function Cipheriv(cipher, key, iv) {
if (!(this instanceof Cipheriv))
return new Cipheriv(cipher, key, iv);
this._binding = new binding.Cipher();
this._binding.initiv(cipher, key, iv);
this._decoder = null;
}

Cipheriv.prototype.update = Cipher.prototype.update;
Cipheriv.prototype.final = Cipher.prototype.final;
Cipheriv.prototype.setAutoPadding = Cipher.prototype.setAutoPadding;


exports.createDecipher = exports.Decipher = Decipher;
function Decipher(cipher, password) {
if (!(this instanceof Decipher))
return new Decipher(cipher, password);
this._binding = new binding.Decipher
this._binding.init(cipher, password);
this._decoder = null;
};

Decipher.prototype.update = Cipher.prototype.update;
Decipher.prototype.final = Cipher.prototype.final;
Decipher.prototype.finaltol = Cipher.prototype.final;
Decipher.prototype.setAutoPadding = Cipher.prototype.setAutoPadding;


exports.Sign = Sign;
exports.createSign = function(algorithm) {
return (new Sign).init(algorithm);
exports.createDecipheriv = exports.Decipheriv = Decipheriv;
function Decipheriv(cipher, key, iv) {
if (!(this instanceof Decipheriv))
return new Decipheriv(cipher, key, iv);
this._binding = new binding.Decipher;
this._binding.initiv(cipher, key, iv);
this._decoder = null;
};

exports.Verify = Verify;
exports.createVerify = function(algorithm) {
return (new Verify).init(algorithm);
Decipheriv.prototype.update = Cipher.prototype.update;
Decipheriv.prototype.final = Cipher.prototype.final;
Decipheriv.prototype.finaltol = Cipher.prototype.final;
Decipheriv.prototype.setAutoPadding = Cipher.prototype.setAutoPadding;


exports.createSign = exports.Sign = Sign;
function Sign(algorithm) {
if (!(this instanceof Sign))
return new Sign(algorithm);
this._binding = new binding.Sign();
this._binding.init(algorithm);
};

exports.DiffieHellman = DiffieHellman;
exports.createDiffieHellman = function(size_or_key, enc) {
if (!size_or_key) {
return new DiffieHellman();
} else if (!enc) {
return new DiffieHellman(size_or_key);
} else {
return new DiffieHellman(size_or_key, enc);
Sign.prototype.update = Hash.prototype.update;

Sign.prototype.sign = function(key, encoding) {
var ret = this._binding.sign(key, 'buffer');
if (encoding && encoding !== 'buffer')
ret = ret.toString(encoding);
return ret;
};


exports.createVerify = exports.Verify = Verify;
function Verify(algorithm) {
if (!(this instanceof Verify))
return new Verify(algorithm);

this._binding = new binding.Verify;
this._binding.init(algorithm);
}

Verify.prototype.update = Hash.prototype.update;

Verify.prototype.verify = function(object, signature, sigEncoding) {
if (sigEncoding === 'buffer')
sigEncoding = null;
if (sigEncoding || typeof signature === 'string')
signature = new Buffer(signature, sigEncoding);
return this._binding.verify(object, signature, 'buffer');
};

exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;

function DiffieHellman(sizeOrKey, encoding) {
if (!(this instanceof DiffieHellman))
return new DiffieHellman(sizeOrKey, encoding);

if (!sizeOrKey)
this._binding = new binding.DiffieHellman();
else {
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof sizeOrKey === 'string')
sizeOrKey = new Buffer(sizeOrKey, encoding);
this._binding = new binding.DiffieHellman(sizeOrKey, 'buffer');
}
}

DiffieHellman.prototype.generateKeys = function(encoding) {
var keys = this._binding.generateKeys('buffer');
if (encoding)
keys = keys.toString(encoding);
return keys;
};

DiffieHellman.prototype.computeSecret = function(key, inEnc, outEnc) {
if (inEnc === 'buffer')
inEnc = null;
if (outEnc === 'buffer')
outEnc = null;
if (inEnc || typeof key === 'string')
key = new Buffer(key, inEnc);
var ret = this._binding.computeSecret(key, 'buffer', 'buffer');
if (outEnc)
ret = ret.toString(outEnc);
return ret;
};

DiffieHellman.prototype.getPrime = function(encoding) {
var prime = this._binding.getPrime('buffer');
if (encoding && encoding !== 'buffer')
prime = prime.toString(encoding);
return prime;
};

DiffieHellman.prototype.getGenerator = function(encoding) {
var generator = this._binding.getGenerator('buffer');
if (encoding && encoding !== 'buffer')
generator = generator.toString(encoding);
return generator;
};

DiffieHellman.prototype.getPublicKey = function(encoding) {
var key = this._binding.getPublicKey('buffer');
if (encoding && encoding !== 'buffer')
key = key.toString(encoding);
return key;
};
exports.getDiffieHellman = function(group_name) {
return new DiffieHellmanGroup(group_name);

DiffieHellman.prototype.getPrivateKey = function(encoding) {
var key = this._binding.getPrivateKey('buffer');
if (encoding && encoding !== 'buffer')
key = key.toString(encoding);
return key;
};

DiffieHellman.prototype.setPublicKey = function(key, encoding) {
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof key === 'string')
key = new Buffer(key, encoding);
this._binding.setPublicKey(key, 'buffer');
return this;
};

DiffieHellman.prototype.setPrivateKey = function(key, encoding) {
if (encoding === 'buffer')
encoding = null;
if (encoding || typeof key === 'string')
key = new Buffer(key, encoding);
this._binding.setPrivateKey(key, 'buffer');
return this;
};



exports.DiffieHellmanGroup =
exports.createDiffieHellmanGroup =
exports.getDiffieHellman = DiffieHellmanGroup;

function DiffieHellmanGroup(name) {
if (!(this instanceof DiffieHellmanGroup))
return new DiffieHellmanGroup(name);
this._binding = new binding.DiffieHellmanGroup(name);
};

DiffieHellmanGroup.prototype.generateKeys =
DiffieHellman.prototype.generateKeys;

DiffieHellmanGroup.prototype.computeSecret =
DiffieHellman.prototype.computeSecret;

DiffieHellmanGroup.prototype.getPrime =
DiffieHellman.prototype.getPrime;

DiffieHellmanGroup.prototype.getGenerator =
DiffieHellman.prototype.getGenerator;

DiffieHellmanGroup.prototype.getPublicKey =
DiffieHellman.prototype.getPublicKey;

DiffieHellmanGroup.prototype.getPrivateKey =
DiffieHellman.prototype.getPrivateKey;

exports.pbkdf2 = PBKDF2;

exports.randomBytes = randomBytes;
@@ -43,7 +43,7 @@ function aes256(decipherFinal) {
function decrypt(val, pad) {
var c = crypto.createDecipheriv('aes256', key, iv);
c.setAutoPadding(pad);
return c.update(val, 'binary', 'binary') + c[decipherFinal]('utf8');
return c.update(val, 'binary', 'utf8') + c[decipherFinal]('utf8');
}

// echo 0123456789abcdef0123456789abcdef \