Skip to content

Commit

Permalink
crypto: add createCipher/WithIV functions
Browse files Browse the repository at this point in the history
This commit extracts the common code from the Cipher/Cipheriv and
Decipher/Decipheriv constructors into a separate function to avoid
code duplication.

Backport-PR-URL: #20706
PR-URL: #20164
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
danbev authored and addaleax committed May 14, 2018
1 parent bdd2856 commit 72029b8
Showing 1 changed file with 46 additions and 83 deletions.
129 changes: 46 additions & 83 deletions lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,21 @@ function getUIntOption(options, key) {
return -1;
}

function Cipher(cipher, password, options) {
if (!(this instanceof Cipher))
return new Cipher(cipher, password, options);
function createCipherBase(cipher, credential, options, decipher, iv) {
const authTagLength = getUIntOption(options, 'authTagLength');

this._handle = new CipherBase(decipher);
if (iv === undefined) {
this._handle.init(cipher, credential, authTagLength);
} else {
this._handle.initiv(cipher, credential, iv, authTagLength);
}
this._decoder = null;

LazyTransform.call(this, options);
}

function createCipher(cipher, password, options, decipher) {
if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

Expand All @@ -89,14 +100,38 @@ function Cipher(cipher, password, options) {
);
}

const authTagLength = getUIntOption(options, 'authTagLength');
createCipherBase.call(this, cipher, password, options, decipher);
}

this._handle = new CipherBase(true);
function createCipherWithIV(cipher, key, options, decipher, iv) {
if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

this._handle.init(cipher, password, authTagLength);
this._decoder = null;
key = toBuf(key);
if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(
'key',
['string', 'Buffer', 'TypedArray', 'DataView'],
key
);
}

LazyTransform.call(this, options);
iv = toBuf(iv);
if (iv !== null && !isArrayBufferView(iv)) {
throw new ERR_INVALID_ARG_TYPE(
'iv',
['string', 'Buffer', 'TypedArray', 'DataView'],
iv
);
}
createCipherBase.call(this, cipher, key, options, decipher, iv);
}

function Cipher(cipher, password, options) {
if (!(this instanceof Cipher))
return new Cipher(cipher, password, options);

createCipher.call(this, cipher, password, options, true);
}

inherits(Cipher, LazyTransform);
Expand Down Expand Up @@ -198,34 +233,7 @@ function Cipheriv(cipher, key, iv, options) {
if (!(this instanceof Cipheriv))
return new Cipheriv(cipher, key, iv, options);

if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

key = toBuf(key);
if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(
'key',
['string', 'Buffer', 'TypedArray', 'DataView'],
key
);
}

iv = toBuf(iv);
if (iv !== null && !isArrayBufferView(iv)) {
throw new ERR_INVALID_ARG_TYPE(
'iv',
['string', 'Buffer', 'TypedArray', 'DataView'],
iv
);
}

const authTagLength = getUIntOption(options, 'authTagLength');

this._handle = new CipherBase(true);
this._handle.initiv(cipher, key, iv, authTagLength);
this._decoder = null;

LazyTransform.call(this, options);
createCipherWithIV.call(this, cipher, key, options, true, iv);
}

inherits(Cipheriv, LazyTransform);
Expand All @@ -248,25 +256,7 @@ function Decipher(cipher, password, options) {
if (!(this instanceof Decipher))
return new Decipher(cipher, password, options);

if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

password = toBuf(password);
if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE(
'password',
['string', 'Buffer', 'TypedArray', 'DataView'],
password
);
}

const authTagLength = getUIntOption(options, 'authTagLength');

this._handle = new CipherBase(false);
this._handle.init(cipher, password, authTagLength);
this._decoder = null;

LazyTransform.call(this, options);
createCipher.call(this, cipher, password, options, false);
}

inherits(Decipher, LazyTransform);
Expand All @@ -286,34 +276,7 @@ function Decipheriv(cipher, key, iv, options) {
if (!(this instanceof Decipheriv))
return new Decipheriv(cipher, key, iv, options);

if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

key = toBuf(key);
if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(
'key',
['string', 'Buffer', 'TypedArray', 'DataView'],
key
);
}

iv = toBuf(iv);
if (iv !== null && !isArrayBufferView(iv)) {
throw new ERR_INVALID_ARG_TYPE(
'iv',
['string', 'Buffer', 'TypedArray', 'DataView'],
iv
);
}

const authTagLength = getUIntOption(options, 'authTagLength');

this._handle = new CipherBase(false);
this._handle.initiv(cipher, key, iv, authTagLength);
this._decoder = null;

LazyTransform.call(this, options);
createCipherWithIV.call(this, cipher, key, options, false, iv);
}

inherits(Decipheriv, LazyTransform);
Expand Down

0 comments on commit 72029b8

Please sign in to comment.