Skip to content

Commit

Permalink
Appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
twiss committed Feb 9, 2021
1 parent aea7aa9 commit 17fc32b
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 122 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
"extends": "airbnb-base",
"parser": "babel-eslint",
"parserOptions": { "sourceType": "module" },

"env": {
Expand Down
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.2",
"asmcrypto.js": "github:openpgpjs/asmcrypto#475cffa5ccb2cf2556427056679414acf3610d1b",
"babel-eslint": "^10.1.0",
"bn.js": "^4.11.8",
"buffer": "^5.0.8",
"chai": "^4.1.2",
Expand Down
98 changes: 49 additions & 49 deletions src/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,6 @@ import util from '../util';
// Do not use util.getNodeCrypto because we need this regardless of useNative setting
const nodeCrypto = util.detectNode() && require('crypto');

export default {
/**
* Retrieve secure random byte array of the specified length
* @param {Integer} length Length in bytes to generate
* @returns {Uint8Array} Random byte array
* @async
*/
getRandomBytes: async function(length) {
const buf = new Uint8Array(length);
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
crypto.getRandomValues(buf);
} else if (typeof globalThis !== 'undefined' && typeof globalThis.msCrypto === 'object' && typeof globalThis.msCrypto.getRandomValues === 'function') {
globalThis.msCrypto.getRandomValues(buf);
} else if (nodeCrypto) {
const bytes = nodeCrypto.randomBytes(buf.length);
buf.set(bytes);
} else if (this.randomBuffer.buffer) {
await this.randomBuffer.get(buf);
} else {
throw new Error('No secure random number generator available.');
}
return buf;
},

/**
* Create a secure random MPI that is greater than or equal to min and less than max.
* @param {module:type/mpi} min Lower bound, included
* @param {module:type/mpi} max Upper bound, excluded
* @returns {module:BN} Random MPI
* @async
*/
getRandomBN: async function(min, max) {
if (max.cmp(min) <= 0) {
throw new Error('Illegal parameter value: max <= min');
}

const modulus = max.sub(min);
const bytes = modulus.byteLength();

// Using a while loop is necessary to avoid bias introduced by the mod operation.
// However, we request 64 extra random bits so that the bias is negligible.
// Section B.1.1 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
const r = new BN(await this.getRandomBytes(bytes + 8));
return r.mod(modulus).add(min);
},

randomBuffer: new RandomBuffer()
};

/**
* Buffer for secure random numbers
*/
Expand Down Expand Up @@ -145,3 +96,52 @@ class RandomBuffer {
}
}
}

export default {
/**
* Retrieve secure random byte array of the specified length
* @param {Integer} length Length in bytes to generate
* @returns {Uint8Array} Random byte array
* @async
*/
getRandomBytes: async function(length) {
const buf = new Uint8Array(length);
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
crypto.getRandomValues(buf);
} else if (typeof globalThis !== 'undefined' && typeof globalThis.msCrypto === 'object' && typeof globalThis.msCrypto.getRandomValues === 'function') {
globalThis.msCrypto.getRandomValues(buf);
} else if (nodeCrypto) {
const bytes = nodeCrypto.randomBytes(buf.length);
buf.set(bytes);
} else if (this.randomBuffer.buffer) {
await this.randomBuffer.get(buf);
} else {
throw new Error('No secure random number generator available.');
}
return buf;
},

/**
* Create a secure random MPI that is greater than or equal to min and less than max.
* @param {module:type/mpi} min Lower bound, included
* @param {module:type/mpi} max Upper bound, excluded
* @returns {module:BN} Random MPI
* @async
*/
getRandomBN: async function(min, max) {
if (max.cmp(min) <= 0) {
throw new Error('Illegal parameter value: max <= min');
}

const modulus = max.sub(min);
const bytes = modulus.byteLength();

// Using a while loop is necessary to avoid bias introduced by the mod operation.
// However, we request 64 extra random bits so that the bias is negligible.
// Section B.1.1 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
const r = new BN(await this.getRandomBytes(bytes + 8));
return r.mod(modulus).add(min);
},

randomBuffer: new RandomBuffer()
};
146 changes: 73 additions & 73 deletions src/keyring/keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,79 +25,6 @@
import { readAllArmored } from '../key';
import LocalStore from './localstore';

class Keyring {
/**
* Initialization routine for the keyring.
* @param {keyring/localstore} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods
*/
constructor(storeHandler) {
this.storeHandler = storeHandler || new LocalStore();
}

/**
* Calls the storeHandler to load the keys
* @async
*/
async load() {
this.publicKeys = new KeyArray(await this.storeHandler.loadPublic());
this.privateKeys = new KeyArray(await this.storeHandler.loadPrivate());
}

/**
* Calls the storeHandler to save the keys
* @async
*/
async store() {
await Promise.all([
this.storeHandler.storePublic(this.publicKeys.keys),
this.storeHandler.storePrivate(this.privateKeys.keys)
]);
}

/**
* Clear the keyring - erase all the keys
*/
clear() {
this.publicKeys.keys = [];
this.privateKeys.keys = [];
}

/**
* Searches the keyring for keys having the specified key id
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @param {Boolean} deep if true search also in subkeys
* @returns {Array<module:key.Key>|null} keys found or null
*/
getKeysForId(keyId, deep) {
let result = [];
result = result.concat(this.publicKeys.getForId(keyId, deep) || []);
result = result.concat(this.privateKeys.getForId(keyId, deep) || []);
return result.length ? result : null;
}

/**
* Removes keys having the specified key id from the keyring
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @returns {Array<module:key.Key>|null} keys found or null
*/
removeKeysForId(keyId) {
let result = [];
result = result.concat(this.publicKeys.removeForId(keyId) || []);
result = result.concat(this.privateKeys.removeForId(keyId) || []);
return result.length ? result : null;
}

/**
* Get all public and private keys
* @returns {Array<module:key.Key>} all keys
*/
getAllKeys() {
return this.publicKeys.keys.concat(this.privateKeys.keys);
}
}

/**
* Array of keys
*/
Expand Down Expand Up @@ -192,6 +119,79 @@ class KeyArray {
}
}

class Keyring {
/**
* Initialization routine for the keyring.
* @param {keyring/localstore} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods
*/
constructor(storeHandler) {
this.storeHandler = storeHandler || new LocalStore();
}

/**
* Calls the storeHandler to load the keys
* @async
*/
async load() {
this.publicKeys = new KeyArray(await this.storeHandler.loadPublic());
this.privateKeys = new KeyArray(await this.storeHandler.loadPrivate());
}

/**
* Calls the storeHandler to save the keys
* @async
*/
async store() {
await Promise.all([
this.storeHandler.storePublic(this.publicKeys.keys),
this.storeHandler.storePrivate(this.privateKeys.keys)
]);
}

/**
* Clear the keyring - erase all the keys
*/
clear() {
this.publicKeys.keys = [];
this.privateKeys.keys = [];
}

/**
* Searches the keyring for keys having the specified key id
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @param {Boolean} deep if true search also in subkeys
* @returns {Array<module:key.Key>|null} keys found or null
*/
getKeysForId(keyId, deep) {
let result = [];
result = result.concat(this.publicKeys.getForId(keyId, deep) || []);
result = result.concat(this.privateKeys.getForId(keyId, deep) || []);
return result.length ? result : null;
}

/**
* Removes keys having the specified key id from the keyring
* @param {String} keyId provided as string of lowercase hex number
* withouth 0x prefix (can be 16-character key ID or fingerprint)
* @returns {Array<module:key.Key>|null} keys found or null
*/
removeKeysForId(keyId) {
let result = [];
result = result.concat(this.publicKeys.removeForId(keyId) || []);
result = result.concat(this.privateKeys.removeForId(keyId) || []);
return result.length ? result : null;
}

/**
* Get all public and private keys
* @returns {Array<module:key.Key>} all keys
*/
getAllKeys() {
return this.publicKeys.keys.concat(this.privateKeys.keys);
}
}

/**
* Checks a key to see if it matches the specified email address
* @private
Expand Down
2 changes: 2 additions & 0 deletions src/packet/marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

/* eslint class-methods-use-this: ["error", { "exceptMethods": ["read"] }] */

/**
* @requires enums
*/
Expand Down
2 changes: 2 additions & 0 deletions src/packet/public_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

/* eslint class-methods-use-this: ["error", { "exceptMethods": ["isDecrypted"] }] */

/**
* @requires type/keyid
* @requires type/mpi
Expand Down
2 changes: 2 additions & 0 deletions src/packet/trust.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["read"] }] */

/**
* @requires enums
*/
Expand Down

0 comments on commit 17fc32b

Please sign in to comment.