Skip to content

Commit

Permalink
[v5] Unexport openpgp.util, openpgp.crypto, and low-level types (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
twiss committed Feb 9, 2021
1 parent 479d826 commit 2382482
Show file tree
Hide file tree
Showing 39 changed files with 396 additions and 413 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
"dist/",
"lightweight/"
],
"esm": {
"cjs": {
"dedefault": true
}
},
"scripts": {
"build": "rollup --config",
"build-test": "npm run build --build-only=test",
"prepare": "npm run build",
"test": "mocha --timeout 120000 test/unittests.js",
"test": "mocha --require esm --timeout 120000 test/unittests.js",
"start": "http-server",
"prebrowsertest": "npm run build-test",
"browsertest": "npm start -- -o test/unittests.html",
Expand Down Expand Up @@ -59,6 +64,7 @@
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-chai-friendly": "^0.5.0",
"eslint-plugin-import": "^2.8.0",
"esm": "^3.2.25",
"hash.js": "^1.1.3",
"http-server": "^0.12.3",
"jsdoc": "github:openpgpjs/jsdoc#0f1816eb4553856647b4ca9561b9307b11ec4f9e",
Expand Down
5 changes: 5 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ export default Object.assign([
output: [
{ file: 'test/lib/unittests-bundle.js', format: 'es', sourcemap: true },
],
inlineDynamicImports: true,
plugins: [
resolve({
browser: true
}),
commonjs({
ignore: builtinModules.concat(nodeDependencies).concat(['../..', '../../..'])
}),
replace({
'require(': 'void(',
delimiters: ['', '']
})
]
}
Expand Down
13 changes: 7 additions & 6 deletions src/crypto/public_key/elliptic/curves.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import nacl from 'tweetnacl/nacl-fast-light.js';
import { getRandomBytes } from '../../random';
import enums from '../../../enums';
import util from '../../../util';
import { uint8ArrayToB64, b64ToUint8Array } from '../../../encoding/base64';
import OID from '../../../type/oid';
import { keyFromPublic, keyFromPrivate, getIndutnyCurve } from './indutnyKey';

Expand Down Expand Up @@ -311,7 +312,7 @@ async function webGenKeyPair(name) {

return {
publicKey: jwkToRawPublic(publicKey),
privateKey: util.b64ToUint8Array(privateKey.d, true)
privateKey: b64ToUint8Array(privateKey.d, true)
};
}

Expand All @@ -337,8 +338,8 @@ async function nodeGenKeyPair(name) {
* @returns {Uint8Array} raw public key
*/
function jwkToRawPublic(jwk) {
const bufX = util.b64ToUint8Array(jwk.x);
const bufY = util.b64ToUint8Array(jwk.y);
const bufX = b64ToUint8Array(jwk.x);
const bufY = b64ToUint8Array(jwk.y);
const publicKey = new Uint8Array(bufX.length + bufY.length + 1);
publicKey[0] = 0x04;
publicKey.set(bufX, 1);
Expand All @@ -361,8 +362,8 @@ function rawPublicToJwk(payloadSize, name, publicKey) {
const jwk = {
kty: "EC",
crv: name,
x: util.uint8ArrayToB64(bufX, true),
y: util.uint8ArrayToB64(bufY, true),
x: uint8ArrayToB64(bufX, true),
y: uint8ArrayToB64(bufY, true),
ext: true
};
return jwk;
Expand All @@ -378,6 +379,6 @@ function rawPublicToJwk(payloadSize, name, publicKey) {
*/
function privateToJwk(payloadSize, name, publicKey, privateKey) {
const jwk = rawPublicToJwk(payloadSize, name, publicKey);
jwk.d = util.uint8ArrayToB64(privateKey, true);
jwk.d = uint8ArrayToB64(privateKey, true);
return jwk;
}
3 changes: 2 additions & 1 deletion src/crypto/public_key/elliptic/ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { getRandomBytes } from '../../random';
import hash from '../../hash';
import enums from '../../../enums';
import util from '../../../util';
import { b64ToUint8Array } from '../../../encoding/base64';
import * as pkcs5 from '../../pkcs5';
import { keyFromPublic, keyFromPrivate, getIndutnyCurve } from './indutnyKey';

Expand Down Expand Up @@ -262,7 +263,7 @@ async function webPrivateEphemeralKey(curve, V, Q, d) {
);
[S, secret] = await Promise.all([S, secret]);
const sharedKey = new Uint8Array(S);
const secretKey = util.b64ToUint8Array(secret.d, true);
const secretKey = b64ToUint8Array(secret.d, true);
return { secretKey, sharedKey };
}

Expand Down
31 changes: 16 additions & 15 deletions src/crypto/public_key/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { randomProbablePrime } from './prime';
import { getRandomBigInteger } from '../random';
import config from '../../config';
import util from '../../util';
import { uint8ArrayToB64, b64ToUint8Array } from '../../encoding/base64';
import { emsaEncode, emeEncode, emeDecode } from '../pkcs1';
import enums from '../../enums';

Expand Down Expand Up @@ -221,14 +222,14 @@ export async function generate(bits, e) {
}
// map JWK parameters to corresponding OpenPGP names
return {
n: util.b64ToUint8Array(jwk.n),
n: b64ToUint8Array(jwk.n),
e: e.toUint8Array(),
d: util.b64ToUint8Array(jwk.d),
d: b64ToUint8Array(jwk.d),
// switch p and q
p: util.b64ToUint8Array(jwk.q),
q: util.b64ToUint8Array(jwk.p),
p: b64ToUint8Array(jwk.q),
q: b64ToUint8Array(jwk.p),
// Since p and q are switched in places, u is the inverse of jwk.q
u: util.b64ToUint8Array(jwk.qi)
u: b64ToUint8Array(jwk.qi)
};
} else if (util.getNodeCrypto() && nodeCrypto.generateKeyPair && RSAPrivateKey) {
const opts = {
Expand Down Expand Up @@ -570,16 +571,16 @@ async function privateToJwk(n, e, d, p, q, u) {
dq = dq.toUint8Array();
return {
kty: 'RSA',
n: util.uint8ArrayToB64(n, true),
e: util.uint8ArrayToB64(e, true),
d: util.uint8ArrayToB64(d, true),
n: uint8ArrayToB64(n, true),
e: uint8ArrayToB64(e, true),
d: uint8ArrayToB64(d, true),
// switch p and q
p: util.uint8ArrayToB64(q, true),
q: util.uint8ArrayToB64(p, true),
p: uint8ArrayToB64(q, true),
q: uint8ArrayToB64(p, true),
// switch dp and dq
dp: util.uint8ArrayToB64(dq, true),
dq: util.uint8ArrayToB64(dp, true),
qi: util.uint8ArrayToB64(u, true),
dp: uint8ArrayToB64(dq, true),
dq: uint8ArrayToB64(dp, true),
qi: uint8ArrayToB64(u, true),
ext: true
};
}
Expand All @@ -593,8 +594,8 @@ async function privateToJwk(n, e, d, p, q, u) {
function publicToJwk(n, e) {
return {
kty: 'RSA',
n: util.uint8ArrayToB64(n, true),
e: util.uint8ArrayToB64(e, true),
n: uint8ArrayToB64(n, true),
e: uint8ArrayToB64(e, true),
ext: true
};
}
25 changes: 25 additions & 0 deletions src/encoding/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ export function decode(data) {
return decoded;
}, () => decodeChunk(buf));
}

/**
* Convert a Base-64 encoded string an array of 8-bit integer
*
* Note: accepts both Radix-64 and URL-safe strings
* @param {String} base64 Base-64 encoded string to convert
* @returns {Uint8Array} An array of 8-bit integers
*/
export function b64ToUint8Array(base64) {
return decode(base64.replace(/-/g, '+').replace(/_/g, '/'));
}

/**
* Convert an array of 8-bit integer to a Base-64 encoded string
* @param {Uint8Array} bytes An array of 8-bit integers to convert
* @param {bool} url If true, output is URL-safe
* @returns {String} Base-64 encoded string
*/
export function uint8ArrayToB64(bytes, url) {
let encoded = encode(bytes).replace(/[\r\n]/g, '');
if (url) {
encoded = encoded.replace(/[+]/g, '-').replace(/[/]/g, '_').replace(/[=]/g, '');
}
return encoded;
}
42 changes: 0 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,12 @@ export {
*/
export * from './cleartext';

/**
* @see module:util
* @name module:openpgp.util
*/
export { default as util } from './util';

/**
* @see module:packet
* @name module:openpgp.packet
*/
export * from './packet';

/**
* @see module:type/s2k
* @name module:openpgp.S2K
*/
export { default as S2K } from './type/s2k';

/**
* @see module:type/keyid
* @name module:openpgp.Keyid
*/
export { default as Keyid } from './type/keyid';

/**
* @see module:type/ecdh_symkey
* @name module:openpgp.ECDHSymmetricKey
*/
export { default as ECDHSymmetricKey } from './type/ecdh_symkey';

/**
* @see module:type/kdf_params
* @name module:openpgp.KDFParams
*/
export { default as KDFParams } from './type/kdf_params';

/**
* @see module:type/oid
* @name module:openpgp.OID
*/
export { default as OID } from './type/oid';

/**
* @see streams
* @name module:openpgp.stream
Expand All @@ -106,12 +70,6 @@ export { default as enums } from './enums';
*/
export { default as config } from './config/config';

/**
* @see module:crypto
* @name module:openpgp.crypto
*/
export { default as crypto } from './crypto';

/**
* @see module:keyring
* @name module:openpgp.Keyring
Expand Down
26 changes: 0 additions & 26 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import emailAddresses from 'email-addresses';
import stream from 'web-stream-tools';
import config from './config';
import util from './util'; // re-import module to access util functions
import * as b64 from './encoding/base64';
import { getBigInteger } from './biginteger';

export default {
Expand Down Expand Up @@ -203,31 +202,6 @@ export default {
return util.concatUint8Array([prefix, bin]);
},

/**
* Convert a Base-64 encoded string an array of 8-bit integer
*
* Note: accepts both Radix-64 and URL-safe strings
* @param {String} base64 Base-64 encoded string to convert
* @returns {Uint8Array} An array of 8-bit integers
*/
b64ToUint8Array: function (base64) {
return b64.decode(base64.replace(/-/g, '+').replace(/_/g, '/'));
},

/**
* Convert an array of 8-bit integer to a Base-64 encoded string
* @param {Uint8Array} bytes An array of 8-bit integers to convert
* @param {bool} url If true, output is URL-safe
* @returns {String} Base-64 encoded string
*/
uint8ArrayToB64: function (bytes, url) {
let encoded = b64.encode(bytes).replace(/[\r\n]/g, '');
if (url) {
encoded = encoded.replace(/[+]/g, '-').replace(/[/]/g, '_').replace(/[=]/g, '');
}
return encoded;
},

/**
* Convert a hex string to an array of 8-bit integers
* @param {String} hex A hex string to convert
Expand Down
12 changes: 7 additions & 5 deletions test/crypto/aes_kw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../..');
const aes_kw = require('../../src/crypto/aes_kw');
const util = require('../../src/util');

const expect = require('chai').expect;

Expand Down Expand Up @@ -44,13 +46,13 @@ module.exports = () => describe('AES Key Wrap and Unwrap', function () {

test_vectors.forEach(function(test) {
it(test[0], function(done) {
const kek = openpgp.util.hexToUint8Array(test[1]);
const kek = util.hexToUint8Array(test[1]);
const input = test[2].replace(/\s/g, "");
const input_bin = openpgp.util.hexToStr(input);
const input_bin = util.hexToStr(input);
const output = test[3].replace(/\s/g, "");
const output_bin = openpgp.util.hexToStr(output);
expect(openpgp.util.uint8ArrayToHex(openpgp.crypto.aes_kw.wrap(kek, input_bin)).toUpperCase()).to.equal(output);
expect(openpgp.util.uint8ArrayToHex(openpgp.crypto.aes_kw.unwrap(kek, output_bin)).toUpperCase()).to.equal(input);
const output_bin = util.hexToStr(output);
expect(util.uint8ArrayToHex(aes_kw.wrap(kek, input_bin)).toUpperCase()).to.equal(output);
expect(util.uint8ArrayToHex(aes_kw.unwrap(kek, output_bin)).toUpperCase()).to.equal(input);
done();
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/crypto/cipher/aes.js

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

6 changes: 3 additions & 3 deletions test/crypto/cipher/blowfish.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../..');
const BF = require('../../../src/crypto/cipher/blowfish');
const util = require('../../../src/util');

const chai = require('chai');

const { util } = openpgp;
const { expect } = chai;

module.exports = () => it('Blowfish cipher test with test vectors from https://www.schneier.com/code/vectors.txt', function(done) {
function test_bf(input, key, output) {
const blowfish = new openpgp.crypto.cipher.blowfish(util.uint8ArrayToStr(key));
const blowfish = new BF(util.uint8ArrayToStr(key));
const result = util.uint8ArrayToStr(blowfish.encrypt(input));

return (util.strToHex(result) === util.strToHex(util.uint8ArrayToStr(output)));
Expand Down
6 changes: 3 additions & 3 deletions test/crypto/cipher/cast5.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../..');
const Cast5 = require('../../../src/crypto/cipher/cast5');
const util = require('../../../src/util');

const chai = require('chai');

const { util } = openpgp;
const { expect } = chai;

module.exports = () => it('CAST-128 cipher test with test vectors from RFC2144', function (done) {
function test_cast(input, key, output) {
const cast5 = new openpgp.crypto.cipher.cast5(key);
const cast5 = new Cast5(key);
const result = util.uint8ArrayToStr(cast5.encrypt(input));

return util.strToHex(result) === util.strToHex(util.uint8ArrayToStr(output));
Expand Down
Loading

0 comments on commit 2382482

Please sign in to comment.