mirror of
https://github.com/moparisthebest/mail
synced 2024-11-25 10:22:18 -05:00
added forge rsa bundle and wrote benchmarks tests successfully
This commit is contained in:
parent
8f0b698d27
commit
e3ce4df5bb
11658
src/lib/forge/forge.rsa.bundle.js
Normal file
11658
src/lib/forge/forge.rsa.bundle.js
Normal file
File diff suppressed because it is too large
Load Diff
1314
src/lib/forge/jsbn.js
Normal file
1314
src/lib/forge/jsbn.js
Normal file
File diff suppressed because it is too large
Load Diff
128
src/lib/forge/prime.worker.js
Normal file
128
src/lib/forge/prime.worker.js
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
/**
|
||||||
|
* RSA Key Generation Worker.
|
||||||
|
*
|
||||||
|
* @author Dave Longley
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013 Digital Bazaar, Inc.
|
||||||
|
*/
|
||||||
|
importScripts('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);
|
||||||
|
|
||||||
|
self.addEventListener('message', function(e) {
|
||||||
|
var result = findPrime(e.data);
|
||||||
|
self.postMessage(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
// start receiving ranges to check
|
||||||
|
self.postMessage({found: false});
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
|
||||||
|
function findPrime(data) {
|
||||||
|
// create BigInteger from given random bytes
|
||||||
|
var num = new BigInteger(data.hex, 16);
|
||||||
|
|
||||||
|
/* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The
|
||||||
|
number we are given is always aligned at 30k + 1. Each time the number is
|
||||||
|
determined not to be prime we add to get to the next 'i', eg: if the number
|
||||||
|
was at 30k + 1 we add 6. */
|
||||||
|
var deltaIdx = 0;
|
||||||
|
|
||||||
|
// find nearest prime
|
||||||
|
var workLoad = data.workLoad;
|
||||||
|
var e = new BigInteger(null);
|
||||||
|
e.fromInt(data.e);
|
||||||
|
for(var i = 0; i < workLoad; ++i) {
|
||||||
|
// do primality test
|
||||||
|
if(isProbablePrime(num, 1)) {
|
||||||
|
// ensure number is coprime with e
|
||||||
|
if(num.subtract(BigInteger.ONE).gcd(e).compareTo(BigInteger.ONE) === 0 &&
|
||||||
|
isProbablePrime(num, 10)) {
|
||||||
|
return {found: true, prime: num.toString(16)};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get next potential prime
|
||||||
|
num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {found: false};
|
||||||
|
}
|
||||||
|
|
||||||
|
function isProbablePrime(n, k) {
|
||||||
|
// divide by low primes, ignore even checks, etc (n alread aligned properly)
|
||||||
|
var i = 1;
|
||||||
|
while(i < LOW_PRIMES.length) {
|
||||||
|
var m = LOW_PRIMES[i];
|
||||||
|
var j = i + 1;
|
||||||
|
while(j < LOW_PRIMES.length && m < LP_LIMIT) {
|
||||||
|
m *= LOW_PRIMES[j++];
|
||||||
|
}
|
||||||
|
m = n.modInt(m);
|
||||||
|
while(i < j) {
|
||||||
|
if(m % LOW_PRIMES[i++] == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return runMillerRabin(n, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
// HAC 4.24, Miller-Rabin
|
||||||
|
function runMillerRabin(n, k) {
|
||||||
|
// n1 = n - 1
|
||||||
|
var n1 = n.subtract(BigInteger.ONE);
|
||||||
|
|
||||||
|
// get s and d such that n1 = 2^s * d
|
||||||
|
var s = n1.getLowestSetBit();
|
||||||
|
if(s <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var d = n1.shiftRight(s);
|
||||||
|
|
||||||
|
var a = new BigInteger(null);
|
||||||
|
for(var i = 0; i < k; ++i) {
|
||||||
|
// 'a' should be selected at random, but lower primes are picked for speed
|
||||||
|
a.fromInt(LOW_PRIMES[i]);
|
||||||
|
|
||||||
|
/* See if 'a' is a composite witness. */
|
||||||
|
|
||||||
|
// x = a^d mod n
|
||||||
|
var x = a.modPow(d, n);
|
||||||
|
|
||||||
|
// probably prime
|
||||||
|
if(x.compareTo(BigInteger.ONE) === 0 || x.compareTo(n1) === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var j = s;
|
||||||
|
while(--j) {
|
||||||
|
// x = x^2 mod a
|
||||||
|
x = x.modPowInt(2, n);
|
||||||
|
|
||||||
|
// 'n' is composite because no previous x == -1 mod n
|
||||||
|
if(x.compareTo(BigInteger.ONE) === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// x == -1 mod n, so probably prime
|
||||||
|
if(x.compareTo(n1) === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 'x' is first_x^(n1/2) and is not +/- 1, so 'n' is not prime
|
||||||
|
if(j === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
@ -2,7 +2,7 @@ module("PGP Crypto");
|
|||||||
|
|
||||||
var pgp_test = {
|
var pgp_test = {
|
||||||
keyID: null,
|
keyID: null,
|
||||||
keySize: 512
|
keySize: 1024
|
||||||
};
|
};
|
||||||
|
|
||||||
asyncTest("Init", 1, function() {
|
asyncTest("Init", 1, function() {
|
||||||
@ -11,7 +11,7 @@ asyncTest("Init", 1, function() {
|
|||||||
pgp_test.crypto = new app.crypto.PGP(window, openpgp, util, null);
|
pgp_test.crypto = new app.crypto.PGP(window, openpgp, util, null);
|
||||||
pgp_test.crypto.setPassphrase('asdf');
|
pgp_test.crypto.setPassphrase('asdf');
|
||||||
ok(pgp_test.crypto, 'PGP crypto');
|
ok(pgp_test.crypto, 'PGP crypto');
|
||||||
|
|
||||||
pgp_test.helperEncrDecr = function(crypto, keyId, plaintext) {
|
pgp_test.helperEncrDecr = function(crypto, keyId, plaintext) {
|
||||||
if (!crypto.getPublicKey()) {
|
if (!crypto.getPublicKey()) {
|
||||||
crypto.readKeys(keyId);
|
crypto.readKeys(keyId);
|
||||||
@ -28,14 +28,14 @@ asyncTest("Init", 1, function() {
|
|||||||
console.log('ciphertext size [bytes]: ' + ct.length);
|
console.log('ciphertext size [bytes]: ' + ct.length);
|
||||||
|
|
||||||
var decrStart = (new Date).getTime();
|
var decrStart = (new Date).getTime();
|
||||||
var pt = crypto.asymmetricDecrypt(ct);
|
var pt = crypto.asymmetricDecrypt(ct);
|
||||||
var decrDiff = (new Date).getTime() - decrStart;
|
var decrDiff = (new Date).getTime() - decrStart;
|
||||||
|
|
||||||
console.log('Time taken for decryption [ms]: ' + decrDiff);
|
console.log('Time taken for decryption [ms]: ' + decrDiff);
|
||||||
ok(pt, "decrypted: see console output for benchmark");
|
ok(pt, "decrypted: see console output for benchmark");
|
||||||
equal(pt, plaintext, "Decrypted should be the same as the plaintext");
|
equal(pt, plaintext, "Decrypted should be the same as the plaintext");
|
||||||
};
|
};
|
||||||
|
|
||||||
start();
|
start();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -52,11 +52,11 @@ asyncTest("Generate keypair, De/Encrypt", 7, function() {
|
|||||||
ok(pgp_test.crypto.getPrivateKey().indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----') === 0);
|
ok(pgp_test.crypto.getPrivateKey().indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----') === 0);
|
||||||
ok(pgp_test.crypto.getPublicKey());
|
ok(pgp_test.crypto.getPublicKey());
|
||||||
ok(pgp_test.crypto.getPublicKey().indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') === 0);
|
ok(pgp_test.crypto.getPublicKey().indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') === 0);
|
||||||
|
|
||||||
pgp_test.helperEncrDecr(pgp_test.crypto, pgp_test.keyID, "Hello, World!");
|
pgp_test.helperEncrDecr(pgp_test.crypto, pgp_test.keyID, '06a9214036b8a15b512e03d534120006');
|
||||||
|
|
||||||
start();
|
start();
|
||||||
|
|
||||||
// pgp_test.crypto.exportKeys(function(url) {
|
// pgp_test.crypto.exportKeys(function(url) {
|
||||||
// ok(url, 'export url');
|
// ok(url, 'export url');
|
||||||
//
|
//
|
||||||
|
105
test/unit/forge-test.js
Normal file
105
test/unit/forge-test.js
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
module("Forge Crypto");
|
||||||
|
|
||||||
|
var rsa_test = {
|
||||||
|
keySize: 1024,
|
||||||
|
test_message: '06a9214036b8a15b512e03d534120006'
|
||||||
|
};
|
||||||
|
|
||||||
|
asyncTest("Generate RSA Keypair", 1, function() {
|
||||||
|
|
||||||
|
forge.pki.rsa.generateKeyPair({
|
||||||
|
bits: rsa_test.keySize,
|
||||||
|
workerScript: app.config.workerPath + '/../lib/forge/prime.worker.js'
|
||||||
|
}, function(err, keypair) {
|
||||||
|
ok(!err && keypair);
|
||||||
|
console.log(keypair);
|
||||||
|
|
||||||
|
rsa_test.keypair = keypair;
|
||||||
|
|
||||||
|
var pkPem = forge.pki.publicKeyToPem(keypair.publicKey);
|
||||||
|
var pk = forge.pki.publicKeyFromPem(pkPem);
|
||||||
|
|
||||||
|
start();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test("RSA Encrypt", 1, function() {
|
||||||
|
rsa_test.ct = rsa_test.keypair.publicKey.encrypt(rsa_test.test_message);
|
||||||
|
ok(rsa_test.ct);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("RSA Decrypt", 1, function() {
|
||||||
|
var pt = rsa_test.keypair.privateKey.decrypt(rsa_test.ct);
|
||||||
|
equal(rsa_test.test_message, pt);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("SHA-256 Hash", 1, function() {
|
||||||
|
rsa_test.md = forge.md.sha256.create();
|
||||||
|
rsa_test.md.update(rsa_test.ct);
|
||||||
|
var digest = rsa_test.md.digest().data;
|
||||||
|
ok(digest);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("RSA Sign", 1, function() {
|
||||||
|
rsa_test.sig = rsa_test.keypair.privateKey.sign(rsa_test.md);
|
||||||
|
ok(rsa_test.sig);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("RSA Verify", 1, function() {
|
||||||
|
var res = rsa_test.keypair.publicKey.verify(rsa_test.md.digest().getBytes(), rsa_test.sig);
|
||||||
|
ok(res);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("HMAC SHA-256", 1, function() {
|
||||||
|
var util = new app.crypto.Util(window, uuid);
|
||||||
|
|
||||||
|
var key = util.base642Str(util.random(forge_aes_test.keySize));
|
||||||
|
var iv = util.base642Str(util.random(forge_aes_test.keySize));
|
||||||
|
|
||||||
|
var hmac = forge.hmac.create();
|
||||||
|
hmac.start('sha256', key);
|
||||||
|
hmac.update(iv);
|
||||||
|
hmac.update(rsa_test.test_message);
|
||||||
|
var result = hmac.digest().toHex();
|
||||||
|
|
||||||
|
ok(result);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("PBKDF2", 1, function() {
|
||||||
|
var util = new app.crypto.Util(window, uuid);
|
||||||
|
|
||||||
|
var salt = util.base642Str("vbhmLjC+Ub6MSbhS6/CkOwxB25wvwRkSLP2DzDtYb+4=");
|
||||||
|
var expect = '5223bd44b0523090b21e9d38a749b090';
|
||||||
|
|
||||||
|
var dk = forge.pkcs5.pbkdf2('password', salt, 1000, 16);
|
||||||
|
|
||||||
|
equal(expect, forge.util.bytesToHex(dk));
|
||||||
|
});
|
||||||
|
|
||||||
|
var forge_aes_test = {
|
||||||
|
keySize: 128,
|
||||||
|
test_message: new TestData().generateBigString(1000)
|
||||||
|
};
|
||||||
|
|
||||||
|
test("AES-128-CBC En/Decrypt", 1, function() {
|
||||||
|
var util = new app.crypto.Util(window, uuid);
|
||||||
|
|
||||||
|
var key = util.base642Str(util.random(forge_aes_test.keySize));
|
||||||
|
var iv = util.base642Str(util.random(forge_aes_test.keySize));
|
||||||
|
var input = forge_aes_test.test_message;
|
||||||
|
|
||||||
|
// encrypt
|
||||||
|
var enCipher = forge.aes.createEncryptionCipher(key);
|
||||||
|
enCipher.start(iv);
|
||||||
|
enCipher.update(forge.util.createBuffer(input));
|
||||||
|
enCipher.finish();
|
||||||
|
|
||||||
|
// decrypt
|
||||||
|
var deCipher = forge.aes.createDecryptionCipher(key);
|
||||||
|
deCipher.start(iv);
|
||||||
|
deCipher.update(forge.util.createBuffer(enCipher.output.data));
|
||||||
|
deCipher.finish();
|
||||||
|
|
||||||
|
equal(input, deCipher.output, 'En/Decrypt');
|
||||||
|
});
|
@ -44,10 +44,11 @@
|
|||||||
<script src="../lib/sjcl/ecc.js"></script>
|
<script src="../lib/sjcl/ecc.js"></script>
|
||||||
<script src="../lib/sjcl/convenience.js"></script>
|
<script src="../lib/sjcl/convenience.js"></script>
|
||||||
|
|
||||||
|
<script src="../lib/forge/forge.rsa.bundle.js"></script>
|
||||||
|
|
||||||
<script src="../lib/nacl.js"></script>
|
<script src="../lib/nacl.js"></script>
|
||||||
|
|
||||||
<script src="../lib/uuid.js"></script>
|
<script src="../lib/uuid.js"></script>
|
||||||
<script src="../lib/openpgp.min.js"></script>
|
|
||||||
|
|
||||||
<script src="../js/app-config.js"></script>
|
<script src="../js/app-config.js"></script>
|
||||||
<script>
|
<script>
|
||||||
@ -78,6 +79,7 @@
|
|||||||
<script src="../test-data.js"></script>
|
<script src="../test-data.js"></script>
|
||||||
<script src="util-test.js"></script>
|
<script src="util-test.js"></script>
|
||||||
<script src="aes-test.js"></script>
|
<script src="aes-test.js"></script>
|
||||||
|
<script src="forge-test.js"></script>
|
||||||
<script src="nacl-crypto-test.js"></script>
|
<script src="nacl-crypto-test.js"></script>
|
||||||
<script src="ecc-test.js"></script>
|
<script src="ecc-test.js"></script>
|
||||||
<script src="crypto-test.js"></script>
|
<script src="crypto-test.js"></script>
|
||||||
|
@ -10,7 +10,7 @@ test("Init", 1, function() {
|
|||||||
nacl_test.util = new app.crypto.Util(window, uuid);
|
nacl_test.util = new app.crypto.Util(window, uuid);
|
||||||
ok(nacl_test.util, 'Util');
|
ok(nacl_test.util, 'Util');
|
||||||
// generate test data
|
// generate test data
|
||||||
nacl_test.test_message = new TestData().generateBigString(1000);
|
nacl_test.test_message = '06a9214036b8a15b512e03d534120006';
|
||||||
nacl_test.crypto = new app.crypto.NaclCrypto(nacl, nacl_test.util);
|
nacl_test.crypto = new app.crypto.NaclCrypto(nacl, nacl_test.util);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -40,17 +40,25 @@ asyncTest("Generate Keypair", 2, function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Asymmetric En/Decrypt (Synchronous)", 3, function() {
|
test("Asymmetric Encrypt (Synchronous)", 2, function() {
|
||||||
var plaintext = nacl_test.test_message;
|
var plaintext = nacl_test.test_message;
|
||||||
|
|
||||||
var nonce = nacl_test.crypto.generateNonce();
|
var nonce = nacl_test.crypto.generateNonce();
|
||||||
ok(nonce, 'Nonce: ' + nonce);
|
ok(nonce, 'Nonce: ' + nonce);
|
||||||
|
nacl_test.nonce = nonce;
|
||||||
|
|
||||||
// encrypt
|
// encrypt
|
||||||
var ct = nacl_test.crypto.asymEncryptSync(plaintext, nonce, nacl_test.recipientKeypair.boxPk, nacl_test.senderKeypair.boxSk);
|
nacl_test.ct = nacl_test.crypto.asymEncryptSync(plaintext, nonce, nacl_test.recipientKeypair.boxPk, nacl_test.senderKeypair.boxSk);
|
||||||
ok(ct, 'Ciphertext length: ' + ct.length);
|
ok(nacl_test.ct, 'Ciphertext length: ' + nacl_test.ct.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Asymmetric Decrypt (Synchronous)", 1, function() {
|
||||||
|
var plaintext = nacl_test.test_message;
|
||||||
|
|
||||||
|
var nonce = nacl_test.nonce
|
||||||
|
|
||||||
// decrypt
|
// decrypt
|
||||||
var decrypted = nacl_test.crypto.asymDecryptSync(ct, nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
|
var decrypted = nacl_test.crypto.asymDecryptSync(nacl_test.ct, nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
|
||||||
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
|
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user