Further test cleaning. openpgp.crypto.js test runs now, next step, make
it pass.
This commit is contained in:
parent
b7d0322b8e
commit
ae1cb14bfb
File diff suppressed because one or more lines are too long
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
var random = require('./random.js'),
|
var random = require('./random.js'),
|
||||||
cipher = require('./cipher'),
|
cipher = require('./cipher'),
|
||||||
cfb = require('./cfb.js'),
|
|
||||||
publicKey = require('./public_key'),
|
publicKey = require('./public_key'),
|
||||||
type_mpi = require('../type/mpi.js');
|
type_mpi = require('../type/mpi.js');
|
||||||
|
|
||||||
|
@ -182,44 +181,7 @@ getPrefixRandom: function(algo) {
|
||||||
* @return {String} Random bytes as a string to be used as a key
|
* @return {String} Random bytes as a string to be used as a key
|
||||||
*/
|
*/
|
||||||
generateSessionKey: function(algo) {
|
generateSessionKey: function(algo) {
|
||||||
return random.getRandomBytes(this.getKeyLength(algo));
|
return random.getRandomBytes(cipher[algo].keySize);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
};
|
||||||
* Create a secure random big integer of bits length
|
|
||||||
* @param {Integer} bits Bit length of the MPI to create
|
|
||||||
* @return {BigInteger} Resulting big integer
|
|
||||||
*/
|
|
||||||
getRandomBigInteger: function(bits) {
|
|
||||||
if (bits < 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
var numBytes = Math.floor((bits+7)/8);
|
|
||||||
|
|
||||||
var randomBits = random.getRandomBytes(numBytes);
|
|
||||||
if (bits % 8 > 0) {
|
|
||||||
|
|
||||||
randomBits = String.fromCharCode(
|
|
||||||
(Math.pow(2,bits % 8)-1) &
|
|
||||||
randomBits.charCodeAt(0)) +
|
|
||||||
randomBits.substring(1);
|
|
||||||
}
|
|
||||||
var mpi = new type_mpi();
|
|
||||||
mpi.fromBytes(randomBits);
|
|
||||||
return mpi.toBigInteger();
|
|
||||||
},
|
|
||||||
|
|
||||||
getRandomBigIntegerInRange: function(min, max) {
|
|
||||||
if (max.compareTo(min) <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var range = max.subtract(min);
|
|
||||||
var r = this.getRandomBigInteger(range.bitLength());
|
|
||||||
while (r > range) {
|
|
||||||
r = this.getRandomBigInteger(range.bitLength());
|
|
||||||
}
|
|
||||||
return min.add(r);
|
|
||||||
},
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
// A Digital signature algorithm implementation
|
// A Digital signature algorithm implementation
|
||||||
|
|
||||||
var BigInteger = require('./jsbn.js'),
|
var BigInteger = require('./jsbn.js'),
|
||||||
crypto = require('../crypto.js'),
|
random = require('../random.js'),
|
||||||
hashModule = require('../hash'),
|
hashModule = require('../hash'),
|
||||||
util = require('../../util');
|
util = require('../../util');
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ function DSA() {
|
||||||
// directly in the DSA signature algorithm.
|
// directly in the DSA signature algorithm.
|
||||||
var hashed_data = util.getLeftNBits(hashModule.digest(hashalgo,m),q.bitLength());
|
var hashed_data = util.getLeftNBits(hashModule.digest(hashalgo,m),q.bitLength());
|
||||||
var hash = new BigInteger(util.hexstrdump(hashed_data), 16);
|
var hash = new BigInteger(util.hexstrdump(hashed_data), 16);
|
||||||
var k = crypto.getRandomBigIntegerInRange(BigInteger.ONE.add(BigInteger.ONE), q.subtract(BigInteger.ONE));
|
var k = random.getRandomBigIntegerInRange(BigInteger.ONE.add(BigInteger.ONE), q.subtract(BigInteger.ONE));
|
||||||
var s1 = (g.modPow(k,p)).mod(q);
|
var s1 = (g.modPow(k,p)).mod(q);
|
||||||
var s2 = (k.modInverse(q).multiply(hash.add(x.multiply(s1)))).mod(q);
|
var s2 = (k.modInverse(q).multiply(hash.add(x.multiply(s1)))).mod(q);
|
||||||
var result = new Array();
|
var result = new Array();
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
// ElGamal implementation
|
// ElGamal implementation
|
||||||
|
|
||||||
var BigInteger = require('./jsbn.js'),
|
var BigInteger = require('./jsbn.js'),
|
||||||
|
random = require('../random.js'),
|
||||||
util = require('../../util');
|
util = require('../../util');
|
||||||
|
|
||||||
function Elgamal() {
|
function Elgamal() {
|
||||||
|
@ -26,12 +27,11 @@ function Elgamal() {
|
||||||
// choose k in {2,...,p-2}
|
// choose k in {2,...,p-2}
|
||||||
var two = BigInteger.ONE.add(BigInteger.ONE);
|
var two = BigInteger.ONE.add(BigInteger.ONE);
|
||||||
var pMinus2 = p.subtract(two);
|
var pMinus2 = p.subtract(two);
|
||||||
var k = openpgp_crypto_getRandomBigIntegerInRange(two, pMinus2);
|
var k = random.getRandomBigIntegerInRange(two, pMinus2);
|
||||||
var k = k.mod(pMinus2).add(BigInteger.ONE);
|
k = k.mod(pMinus2).add(BigInteger.ONE);
|
||||||
var c = new Array();
|
var c = [];
|
||||||
c[0] = g.modPow(k, p);
|
c[0] = g.modPow(k, p);
|
||||||
c[1] = y.modPow(k, p).multiply(m).mod(p).toMPI();
|
c[1] = y.modPow(k, p).multiply(m).mod(p);
|
||||||
c[0] = c[0].toMPI();
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
rsa: require('./rsa.js'),
|
rsa: require('./rsa.js'),
|
||||||
elgamal: require('./elgamal.js'),
|
elgamal: require('./elgamal.js'),
|
||||||
dsa: require('./dsa.js')
|
dsa: require('./dsa.js')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
// The GPG4Browsers crypto interface
|
// The GPG4Browsers crypto interface
|
||||||
|
|
||||||
|
var type_mpi = require('../type/mpi.js');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
/**
|
/**
|
||||||
* Retrieve secure random byte string of the specified length
|
* Retrieve secure random byte string of the specified length
|
||||||
|
@ -60,5 +62,43 @@ module.exports = {
|
||||||
var buf = new Uint32Array(1);
|
var buf = new Uint32Array(1);
|
||||||
window.crypto.getRandomValues(buf);
|
window.crypto.getRandomValues(buf);
|
||||||
return buf[0] & 0xFF;
|
return buf[0] & 0xFF;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a secure random big integer of bits length
|
||||||
|
* @param {Integer} bits Bit length of the MPI to create
|
||||||
|
* @return {BigInteger} Resulting big integer
|
||||||
|
*/
|
||||||
|
getRandomBigInteger: function(bits) {
|
||||||
|
if (bits < 0) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
var numBytes = Math.floor((bits+7)/8);
|
||||||
|
|
||||||
|
var randomBits = this.getRandomBytes(numBytes);
|
||||||
|
if (bits % 8 > 0) {
|
||||||
|
|
||||||
|
randomBits = String.fromCharCode(
|
||||||
|
(Math.pow(2,bits % 8)-1) &
|
||||||
|
randomBits.charCodeAt(0)) +
|
||||||
|
randomBits.substring(1);
|
||||||
}
|
}
|
||||||
|
var mpi = new type_mpi();
|
||||||
|
mpi.fromBytes(randomBits);
|
||||||
|
return mpi.toBigInteger();
|
||||||
|
},
|
||||||
|
|
||||||
|
getRandomBigIntegerInRange: function(min, max) {
|
||||||
|
if (max.compareTo(min) <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var range = max.subtract(min);
|
||||||
|
var r = this.getRandomBigInteger(range.bitLength());
|
||||||
|
while (r > range) {
|
||||||
|
r = this.getRandomBigInteger(range.bitLength());
|
||||||
|
}
|
||||||
|
return min.add(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
module.exports = {
|
var enums = {
|
||||||
|
|
||||||
|
|
||||||
/** A string to key specifier type
|
/** A string to key specifier type
|
||||||
* @enum {Integer}
|
* @enum {Integer}
|
||||||
|
@ -229,6 +228,4 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = enums;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var crypto = require('./crypto');
|
var crypto = require('./crypto');
|
||||||
|
|
||||||
module.exports = require('./openpgp.js');
|
module.exports = require('./openpgp.js');
|
||||||
|
@ -10,6 +7,7 @@ module.exports.mpi = require('./type/mpi.js');
|
||||||
module.exports.s2k = require('./type/s2k.js');
|
module.exports.s2k = require('./type/s2k.js');
|
||||||
module.exports.keyid = require('./type/keyid.js');
|
module.exports.keyid = require('./type/keyid.js');
|
||||||
module.exports.armor = require('./encoding/armor.js');
|
module.exports.armor = require('./encoding/armor.js');
|
||||||
|
module.exports.enums = require('./enums.js');
|
||||||
|
|
||||||
for(var i in crypto)
|
for(var i in crypto)
|
||||||
module.exports[i] = crypto[i];
|
module.exports[i] = crypto[i];
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var unit = require('../unit.js');
|
var unit = require('../unit.js');
|
||||||
|
|
||||||
unit.register("Functional testing of openpgp_crypto_* methods", function() {
|
unit.register("Functional testing of openpgp.crypto.* methods", function() {
|
||||||
var openpgp = require('../../');
|
var openpgp = require('../../');
|
||||||
var util = openpgp.util;
|
var util = openpgp.util;
|
||||||
var result = [];
|
var result = [];
|
||||||
|
@ -224,7 +224,6 @@ unit.register("Functional testing of openpgp_crypto_* methods", function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Originally we passed public and secret MPI separately, now they are joined. Is this what we want to do long term?
|
//Originally we passed public and secret MPI separately, now they are joined. Is this what we want to do long term?
|
||||||
debugger;
|
|
||||||
// RSA
|
// RSA
|
||||||
var RSAsignedData = openpgp.signature.sign(2, 1, RSApubMPIs.concat(RSAsecMPIs), "foobar");
|
var RSAsignedData = openpgp.signature.sign(2, 1, RSApubMPIs.concat(RSAsecMPIs), "foobar");
|
||||||
var RSAsignedDataMPI = new openpgp.mpi();
|
var RSAsignedDataMPI = new openpgp.mpi();
|
||||||
|
@ -243,32 +242,29 @@ unit.register("Functional testing of openpgp_crypto_* methods", function() {
|
||||||
result[1] = new unit.result("Testing DSA Sign and Verify",
|
result[1] = new unit.result("Testing DSA Sign and Verify",
|
||||||
openpgp.signature.verify(17, 2, DSAmsgMPIs, DSApubMPIs, "foobar"));
|
openpgp.signature.verify(17, 2, DSAmsgMPIs, DSApubMPIs, "foobar"));
|
||||||
|
|
||||||
var symmAlgo = 9; // AES256
|
var symmAlgo = "aes256"; // AES256
|
||||||
var symmKey = openpgp.generateSessionKey(symmAlgo);
|
var symmKey = openpgp.generateSessionKey(symmAlgo);
|
||||||
var symmencDataOCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo),symmAlgo, symmKey, "foobar",true);
|
var symmencDataOCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo), symmAlgo, "foobar", symmKey, true);
|
||||||
var symmencDataCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo),symmAlgo, symmKey, "foobar",false);
|
var symmencDataCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo), symmAlgo, "foobar", symmKey, false);
|
||||||
|
|
||||||
result[2] = new unit.result("Testing symmetric encrypt and decrypt with OpenPGP CFB resync",
|
result[2] = new unit.result("Testing symmetric encrypt and decrypt with OpenPGP CFB resync",
|
||||||
openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataOCFB,true) == "foobar");
|
openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataOCFB,true) == "foobar");
|
||||||
result[3] = new unit.result("Testing symmetric encrypt and decrypt without OpenPGP CFB resync (used in modification detection code \"MDC\" packets)",
|
result[3] = new unit.result("Testing symmetric encrypt and decrypt without OpenPGP CFB resync (used in modification detection code \"MDC\" packets)",
|
||||||
openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataCFB,false) == "foobar");
|
openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataCFB,false) == "foobar");
|
||||||
|
|
||||||
var RSAEncryptedData = openpgp.cfb.encrypt(1, RSApubMPIs, new openpgp.mpi().create(openpgp_encoding_eme_pkcs1_encode(symmKey, RSApubMPIs[0].mpiByteLength)));
|
var RSAUnencryptedData = new openpgp.mpi();
|
||||||
var RSAEncryptedDataMPI = new openpgp.mpi();
|
RSAUnencryptedData.fromBytes(openpgp.pkcs1.eme.encode(symmKey, RSApubMPIs[0].mpiByteLength));
|
||||||
RSAEncryptedDataMPI.read(RSAEncryptedData, 0,RSAEncryptedData.length);
|
var RSAEncryptedData = openpgp.publicKeyEncrypt("rsa_encrypt_sign", RSApubMPIs, RSAUnencryptedData);
|
||||||
|
|
||||||
result[4] = new unit.result("Testing asymmetric encrypt and decrypt using RSA with eme_pkcs1 padding",
|
result[4] = new unit.result("Testing asymmetric encrypt and decrypt using RSA with eme_pkcs1 padding",
|
||||||
openpgp_encoding_eme_pkcs1_decode(openpgp.cfb.decrypt(1, RSApubMPIs.concat(RSAsecMPIs), [RSAEncryptedDataMPI]).toMPI().substring(2), RSApubMPIs[0].mpiByteLength) == symmKey);
|
openpgp.pkcs1.eme.decode(openpgp.publicKeyDecrypt("rsa_encrypt_sign", RSApubMPIs.concat(RSAsecMPIs), RSAEncryptedData).write().substring(2), RSApubMPIs[0].mpiByteLength) == symmKey);
|
||||||
|
|
||||||
var ElgamalEncryptedData = openpgp.cfb.encrypt(16, ElgamalpubMPIs, new openpgp.mpi().create(openpgp_encoding_eme_pkcs1_encode(symmKey, ElgamalpubMPIs[0].mpiByteLength)));
|
var ElgamalUnencryptedData = new openpgp.mpi();
|
||||||
var ElgamalEncryptedDataMPIs = [];
|
ElgamalUnencryptedData.fromBytes(openpgp.pkcs1.eme.encode(symmKey, ElgamalpubMPIs[0].mpiByteLength));
|
||||||
ElgamalEncryptedDataMPIs[0] = new openpgp.mpi();
|
var ElgamalEncryptedData = openpgp.publicKeyEncrypt("elgamal", ElgamalpubMPIs, ElgamalUnencryptedData);
|
||||||
ElgamalEncryptedDataMPIs[0].read(ElgamalEncryptedData[0], 0, ElgamalEncryptedData[0].length);
|
|
||||||
ElgamalEncryptedDataMPIs[1] = new openpgp.mpi();
|
|
||||||
ElgamalEncryptedDataMPIs[1].read(ElgamalEncryptedData[1], 0, ElgamalEncryptedData[1].length);
|
|
||||||
|
|
||||||
result[5] = new unit.result("Testing asymmetric encrypt and decrypt using Elgamal with eme_pkcs1 padding",
|
result[5] = new unit.result("Testing asymmetric encrypt and decrypt using Elgamal with eme_pkcs1 padding",
|
||||||
openpgp_encoding_eme_pkcs1_decode(openpgp.cfb.decrypt(16, ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedDataMPIs).toMPI().substring(2), ElgamalpubMPIs[0].mpiByteLength) == symmKey);
|
openpgp.pkcs1.eme.decode(openpgp.publicKeyDecrypt("elgamal", ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedData).write().substring(2), ElgamalpubMPIs[0].mpiByteLength) == symmKey);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user