From 68d298c948541765e1dacb8ef51a11b904bc7e89 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 3 Feb 2016 16:05:25 +0700 Subject: [PATCH] Fix node.js detection --- src/crypto/hash/index.js | 2 +- src/crypto/random.js | 20 ++++++------- .../sym_encrypted_integrity_protected.js | 29 ++++++++++--------- src/util.js | 7 +++++ 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/crypto/hash/index.js b/src/crypto/hash/index.js index bb783c62..295d536b 100644 --- a/src/crypto/hash/index.js +++ b/src/crypto/hash/index.js @@ -23,7 +23,7 @@ function node_hash(type) { } var hash_fns; -if(typeof module !== 'undefined' && module.exports && config.useNative) { // Use Node native crypto +if(util.detectNode() && config.useNative) { // Use Node native crypto hash_fns = { md5: node_hash('md5'), sha1: node_hash('sha1'), diff --git a/src/crypto/random.js b/src/crypto/random.js index 1224bfae..1f26d67d 100644 --- a/src/crypto/random.js +++ b/src/crypto/random.js @@ -1,19 +1,19 @@ // GPG4Browsers - An OpenPGP implementation in javascript // Copyright (C) 2011 Recurity Labs GmbH -// +// // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3.0 of the License, or (at your option) any later version. -// +// // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. -// +// // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // The GPG4Browsers crypto interface @@ -23,13 +23,11 @@ * @module crypto/random */ -var type_mpi = require('../type/mpi.js'), - util = require('../util.js'); -var nodeCrypto = null; +'use strict'; -if (typeof window === 'undefined') { - nodeCrypto = require('crypto'); -} +var TypeMPI = require('../type/mpi.js'), + util = require('../util.js'), + nodeCrypto = util.detectNode() && require('crypto'); module.exports = { /** @@ -114,7 +112,7 @@ module.exports = { randomBits.charCodeAt(0)) + randomBits.substring(1); } - var mpi = new type_mpi(); + var mpi = new TypeMPI(); mpi.fromBytes(randomBits); return mpi.toBigInteger(); }, diff --git a/src/packet/sym_encrypted_integrity_protected.js b/src/packet/sym_encrypted_integrity_protected.js index 66c75312..0ffc8efc 100644 --- a/src/packet/sym_encrypted_integrity_protected.js +++ b/src/packet/sym_encrypted_integrity_protected.js @@ -1,16 +1,16 @@ // GPG4Browsers - An OpenPGP implementation in javascript // Copyright (C) 2011 Recurity Labs GmbH -// +// // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3.0 of the License, or (at your option) any later version. -// +// // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. -// +// // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -32,6 +32,8 @@ * @module packet/sym_encrypted_integrity_protected */ +'use strict'; + module.exports = SymEncryptedIntegrityProtected; var util = require('../util.js'), @@ -60,7 +62,7 @@ SymEncryptedIntegrityProtected.prototype.read = function (bytes) { // - A one-octet version number. The only currently defined value is 1. var version = bytes[0]; - if (version != 1) { + if (version !== 1) { throw new Error('Invalid packet version.'); } @@ -71,7 +73,6 @@ SymEncryptedIntegrityProtected.prototype.read = function (bytes) { }; SymEncryptedIntegrityProtected.prototype.write = function () { - // 1 = Version return util.concatUint8Array([new Uint8Array([1]), this.encrypted]); }; @@ -97,13 +98,13 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm if(sessionKeyAlgorithm.substr(0,3) === 'aes') { var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize; // Node crypto library. Not clear that it is faster than asmCrypto - if(typeof module !== 'undefined' && module.exports && config.useNative) { + if(util.detectNode() && config.useNative) { var nodeCrypto = require('crypto'); var Buffer = require('buffer').Buffer; - var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', + var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', new Buffer(key), new Buffer(new Uint8Array(blockSize))); this.encrypted = new Uint8Array(cipherObj.update(new Buffer(util.concatUint8Array([prefix, tohash])))); - //var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', + //var cipherObj = new nodeCrypto.createCipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', // util.Uint8Array2str(key), util.Uint8Array2str(new Uint8Array(blockSize))); //this.encrypted = new Uint8Array(cipherObj.update(util.Uint8Array2str(util.concatUint8Array([prefix, tohash])))); } @@ -128,16 +129,15 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm * @return {String} The decrypted data of this packet */ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) { - var decrypted; // AES optimizations. Native code for node, asmCrypto for browser. if(sessionKeyAlgorithm.substr(0,3) === 'aes') { var blockSize = crypto.cipher[sessionKeyAlgorithm].blockSize; // Node crypto library. Not clear that it is faster than asmCrypto - if(typeof module !== 'undefined' && module.exports && config.useNative) { + if(util.detectNode() && config.useNative) { var nodeCrypto = require('crypto'); var Buffer = require('buffer').Buffer; - var decipherObj = new nodeCrypto.createDecipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', + var decipherObj = new nodeCrypto.createDecipheriv('aes-' + sessionKeyAlgorithm.substr(3,3) + '-cfb', new Buffer(key), new Buffer(new Uint8Array(blockSize))); decrypted = new Uint8Array(decipherObj.update(new Buffer(this.encrypted))); } @@ -154,13 +154,14 @@ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm // there must be a modification detection code packet as the // last packet and everything gets hashed except the hash itself - this.hash = util.Uint8Array2str(crypto.hash.sha1(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted), + this.hash = util.Uint8Array2str(crypto.hash.sha1(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted), decrypted.subarray(0, decrypted.length - 20)]))); var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length)); - if (this.hash != mdc) { + if (this.hash !== mdc) { throw new Error('Modification detected.'); - } else + } else { this.packets.read(decrypted.subarray(0, decrypted.length - 22)); + } }; diff --git a/src/util.js b/src/util.js index 73427e8d..64b0f0f1 100644 --- a/src/util.js +++ b/src/util.js @@ -458,6 +458,13 @@ module.exports = { resolve(e.target.result); }; }); + }, + + /** + * Detect Node.js runtime. + */ + detectNode: function() { + return typeof window === 'undefined'; } };