Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ES Modules #718

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve BitInteger usage.
  • Loading branch information
davidlehn committed Jan 5, 2022
commit 51af278b1b766fc5651cbcf513857a90c505c522
8 changes: 1 addition & 7 deletions lib/ed25519.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@
* https://github.com/dchest/tweetnacl-js
*/
import {default as forge} from './forge.js';
require('./jsbn');
require('./random');
import './sha512.js';
import {
ByteBuffer as forge_util_ByteBuffer
ByteBuffer
} from './util.js';
import {
privateKeyValidator,
publicKeyValidator
} from './asn1-validator.js';

if(typeof BigInteger === 'undefined') {
var BigInteger = forge.jsbn.BigInteger;
}

var ByteBuffer = forge_util_ByteBuffer;
var NativeBuffer = typeof Buffer === 'undefined' ? Uint8Array : Buffer;

/*
Expand Down
5 changes: 0 additions & 5 deletions lib/jsbn.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ Address all questions regarding this license to:
Tom Wu
[email protected]
*/
import {default as forge} from './forge.js';

forge.jsbn = forge.jsbn || {};

// Bits per digit
var dbits;

Expand All @@ -65,7 +61,6 @@ export function BigInteger(a,b,c) {
else if(b == null && "string" != typeof a) this.fromString(a,256);
else this.fromString(a,b);
}
forge.jsbn.BigInteger = BigInteger;

// return new, unset BigInteger
function nbi() { return new BigInteger(null); }
Expand Down
2 changes: 2 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export {debug};
require('./des');
require('./ed25519');
require('./hmac');
import * as jsbn from './jsbn.js';
export {jsbn};
require('./kem');
require('./log');
require('./md.all');
Expand Down
4 changes: 0 additions & 4 deletions lib/pbe.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import {
hexToBytes as forge_util_hexToBytes
} from './util.js';

//if(typeof BigInteger === 'undefined') {
// var BigInteger = forge.jsbn.BigInteger;
//}

// shortcut for asn.1 API
var asn1 = forge.asn1;

Expand Down
6 changes: 3 additions & 3 deletions lib/prime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* Copyright (c) 2014 Digital Bazaar, Inc.
*/
import forge from './forge.js';
require('./jsbn');
import {
BigInteger
} from './jsbn.js';
require('./random');
import {
estimateCores as forge_util_estimateCores,
Expand All @@ -24,8 +26,6 @@ if(forge.prime) {
/* PRIME API */
var prime = module.exports = forge.prime = forge.prime || {};

var BigInteger = forge.jsbn.BigInteger;

// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
var THIRTY = new BigInteger(null);
Expand Down
4 changes: 1 addition & 3 deletions lib/prime.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
*/
// worker is built using CommonJS syntax to include all code in one worker file
//importScripts('jsbn.js');
var forge = require('./forge');
require('./jsbn');
import {BigInteger} from './jsbn.js';

// prime constants
var LOW_PRIMES = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997];
var LP_LIMIT = (1 << 26) / LOW_PRIMES[LOW_PRIMES.length - 1];

var BigInteger = forge.jsbn.BigInteger;
var BIG_TWO = new BigInteger(null);
BIG_TWO.fromInt(2);

Expand Down
8 changes: 3 additions & 5 deletions lib/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
*/
import {default as forge} from './forge.js';
require('./asn1');
require('./jsbn');
import {
BigInteger
} from './jsbn.js';
require('./oids');
require('./pkcs1');
require('./pki');
Expand All @@ -84,10 +86,6 @@ import {
rsaPublicKeyValidator
} from './validators.js';

if(typeof BigInteger === 'undefined') {
var BigInteger = forge.jsbn.BigInteger;
}

var _crypto = forge_util_isNodejs ? require('crypto') : null;

// shortcut for asn.1 API
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/kem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {default as KEM} from '../../lib/kem.js';
import {default as MD} from '../../lib/md.all.js';
import {default as RSA} from '../../lib/rsa.js';
var UTIL = require('../../lib/util');
var JSBN = require('../../lib/jsbn');
import {BigInteger} from '../../lib/jsbn.js';

(function() {
function FixedSecureRandom(str) {
Expand Down Expand Up @@ -50,9 +50,9 @@ var JSBN = require('../../lib/jsbn');
var kem = KEM.rsa.create(kdf, {prng: rnd});

var rsaPublicKey = RSA.setPublicKey(
new JSBN.BigInteger(n), new JSBN.BigInteger(e));
new BigInteger(n), new BigInteger(e));
var rsaPrivateKey = RSA.setPrivateKey(
new JSBN.BigInteger(n), null, new JSBN.BigInteger(d));
new BigInteger(n), null, new BigInteger(d));

var result = kem.encrypt(rsaPublicKey, 128);
ASSERT.equal(UTIL.bytesToHex(result.encapsulation), C0);
Expand All @@ -75,9 +75,9 @@ var JSBN = require('../../lib/jsbn');
var kem = KEM.rsa.create(kdf, {prng: rnd});

var rsaPublicKey = RSA.setPublicKey(
new JSBN.BigInteger(n), new JSBN.BigInteger(e));
new BigInteger(n), new BigInteger(e));
var rsaPrivateKey = RSA.setPrivateKey(
new JSBN.BigInteger(n), null, new JSBN.BigInteger(d));
new BigInteger(n), null, new BigInteger(d));

var result = kem.encrypt(rsaPublicKey, 128);
ASSERT.equal(UTIL.bytesToHex(result.encapsulation), C0);
Expand All @@ -100,9 +100,9 @@ var JSBN = require('../../lib/jsbn');
var kem = KEM.rsa.create(kdf, {prng: rnd});

var rsaPublicKey = RSA.setPublicKey(
new JSBN.BigInteger(n), new JSBN.BigInteger(e));
new BigInteger(n), new BigInteger(e));
var rsaPrivateKey = RSA.setPrivateKey(
new JSBN.BigInteger(n), null, new JSBN.BigInteger(d));
new BigInteger(n), null, new BigInteger(d));

var result = kem.encrypt(rsaPublicKey, 128);
ASSERT.equal(UTIL.bytesToHex(result.encapsulation), C0);
Expand All @@ -125,9 +125,9 @@ var JSBN = require('../../lib/jsbn');
var kem = KEM.rsa.create(kdf, {prng: rnd});

var rsaPublicKey = RSA.setPublicKey(
new JSBN.BigInteger(n), new JSBN.BigInteger(e));
new BigInteger(n), new BigInteger(e));
var rsaPrivateKey = RSA.setPrivateKey(
new JSBN.BigInteger(n), null, new JSBN.BigInteger(d));
new BigInteger(n), null, new BigInteger(d));

var result = kem.encrypt(rsaPublicKey, 128);
ASSERT.equal(UTIL.bytesToHex(result.encapsulation), C0);
Expand All @@ -152,9 +152,9 @@ var JSBN = require('../../lib/jsbn');
var kem = KEM.rsa.create(kdf, {prng: rnd});

var rsaPublicKey = RSA.setPublicKey(
new JSBN.BigInteger(n), new JSBN.BigInteger(e));
new BigInteger(n), new BigInteger(e));
var rsaPrivateKey = RSA.setPrivateKey(
new JSBN.BigInteger(n), null, new JSBN.BigInteger(d));
new BigInteger(n), null, new BigInteger(d));

var result = kem.encrypt(rsaPublicKey, 128);
ASSERT.equal(UTIL.bytesToHex(result.encapsulation), C0);
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/pkcs1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
var ASSERT = require('assert');
var JSBN = require('../../lib/jsbn');
import {BigInteger} from '../../lib/jsbn.js';
import {default as MD} from '../../lib/md.all.js';
var PKCS1 = require('../../lib/pkcs1');
import {default as PKI} from '../../lib/pki.js';
var UTIL = require('../../lib/util');

(function() {
var BigInteger = JSBN.BigInteger;

// RSA's test vectors for Forge's RSA-OAEP implementation:
// http:https://www.rsa.com/rsalabs/node.asp?id=2125
describe('pkcs1', function() {
Expand Down