Test cleanup, and fall out from that. test/crypto/openpgp.crypto.js is

still broken because DSA still has issues. I'm guessing elgamal will too
once the DSA ones are fixed.
This commit is contained in:
seancolyer 2013-08-18 17:37:37 -04:00
parent 844ea22d99
commit b7d0322b8e
13 changed files with 19340 additions and 18679 deletions

File diff suppressed because one or more lines are too long

View File

@ -191,8 +191,9 @@ generateSessionKey: function(algo) {
* @return {BigInteger} Resulting big integer * @return {BigInteger} Resulting big integer
*/ */
getRandomBigInteger: function(bits) { getRandomBigInteger: function(bits) {
if (bits < 0) if (bits < 0) {
return null; return null;
}
var numBytes = Math.floor((bits+7)/8); var numBytes = Math.floor((bits+7)/8);
var randomBits = random.getRandomBytes(numBytes); var randomBits = random.getRandomBytes(numBytes);
@ -203,12 +204,15 @@ getRandomBigInteger: function(bits) {
randomBits.charCodeAt(0)) + randomBits.charCodeAt(0)) +
randomBits.substring(1); randomBits.substring(1);
} }
return new type_mpi().create(randomBits).toBigInteger(); var mpi = new type_mpi();
mpi.fromBytes(randomBits);
return mpi.toBigInteger();
}, },
getRandomBigIntegerInRange: function(min, max) { getRandomBigIntegerInRange: function(min, max) {
if (max.compareTo(min) <= 0) if (max.compareTo(min) <= 0) {
return; return;
}
var range = max.subtract(min); var range = max.subtract(min);
var r = this.getRandomBigInteger(range.bitLength()); var r = this.getRandomBigInteger(range.bitLength());

View File

@ -17,6 +17,11 @@
// //
// A Digital signature algorithm implementation // A Digital signature algorithm implementation
var BigInteger = require('./jsbn.js'),
crypto = require('../crypto.js'),
hashModule = require('../hash'),
util = require('../../util');
function DSA() { function DSA() {
// s1 = ((g**s) mod p) mod q // s1 = ((g**s) mod p) mod q
// s1 = ((s**-1)*(sha-1(m)+(s1*x) mod q) // s1 = ((s**-1)*(sha-1(m)+(s1*x) mod q)
@ -26,9 +31,9 @@ function DSA() {
// of leftmost bits equal to the number of bits of q. This (possibly // of leftmost bits equal to the number of bits of q. This (possibly
// truncated) hash function result is treated as a number and used // truncated) hash function result is treated as a number and used
// directly in the DSA signature algorithm. // directly in the DSA signature algorithm.
var hashed_data = util.getLeftNBits(openpgp_crypto_hashData(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 = openpgp_crypto_getRandomBigIntegerInRange(BigInteger.ONE.add(BigInteger.ONE), q.subtract(BigInteger.ONE)); var k = crypto.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();
@ -71,7 +76,7 @@ function DSA() {
this.select_hash_algorithm = select_hash_algorithm; this.select_hash_algorithm = select_hash_algorithm;
function verify(hashalgo, s1,s2,m,p,q,g,y) { function verify(hashalgo, s1,s2,m,p,q,g,y) {
var hashed_data = util.getLeftNBits(openpgp_crypto_hashData(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);
if (BigInteger.ZERO.compareTo(s1) > 0 || if (BigInteger.ZERO.compareTo(s1) > 0 ||
s1.compareTo(q) > 0 || s1.compareTo(q) > 0 ||

View File

@ -34,8 +34,6 @@ module.exports = function packet_symmetrically_encrypted() {
* @type {openpgp_packetlist} */ * @type {openpgp_packetlist} */
this.packets; this.packets;
this.read = function(bytes) { this.read = function(bytes) {
this.encrypted = bytes; this.encrypted = bytes;
} }

View File

@ -1,7 +1,7 @@
var openpgp = require('openpgp') var unit = require('../../unit.js');
unit.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt", function() {
unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt", function() { var openpgp = require('../../../');
var util = openpgp.util; var util = openpgp.util;
var result = new Array(); var result = new Array();
@ -68,7 +68,7 @@ unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt"
var res2 = test_aes(testvectors128[i][1],testvectors128[i][0],testvectors128[i][2]); var res2 = test_aes(testvectors128[i][1],testvectors128[i][0],testvectors128[i][2]);
res &= res2; res &= res2;
if (!res2) { if (!res2) {
result[j] = new test_result("Testing 128 bit key vector with block "+ result[j] = new unit.result("Testing 128 bit key vector with block "+
util.hexidump(testvectors128[i][1])+ util.hexidump(testvectors128[i][1])+
" and key "+util.hexidump(testvectors128[i][0])+ " and key "+util.hexidump(testvectors128[i][0])+
" should be "+util.hexidump(testvectors128[i][2]), " should be "+util.hexidump(testvectors128[i][2]),
@ -77,7 +77,7 @@ unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt"
} }
} }
if (res) { if (res) {
result[j] = new test_result("128 bit key test vectors completed.",true) result[j] = new unit.result("128 bit key test vectors completed.",true)
j++; j++;
} }
@ -86,7 +86,7 @@ unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt"
var res2 = test_aes(testvectors192[i][1],testvectors192[i][0],testvectors192[i][2]); var res2 = test_aes(testvectors192[i][1],testvectors192[i][0],testvectors192[i][2]);
res &= res2; res &= res2;
if (!res2) { if (!res2) {
result[j] = new test_result("Testing 192 bit key vector with block "+ result[j] = new unit.result("Testing 192 bit key vector with block "+
util.hexidump(testvectors192[i][1])+ util.hexidump(testvectors192[i][1])+
" and key "+util.hexidump(testvectors192[i][0])+ " and key "+util.hexidump(testvectors192[i][0])+
" should be "+util.hexidump(testvectors192[i][2]), " should be "+util.hexidump(testvectors192[i][2]),
@ -95,7 +95,7 @@ unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt"
} }
} }
if (res) { if (res) {
result[j] = new test_result("192 bit key test vectors completed.",true) result[j] = new unit.result("192 bit key test vectors completed.",true)
j++; j++;
} }
@ -104,7 +104,7 @@ unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt"
var res2 = test_aes(testvectors256[i][1],testvectors256[i][0],testvectors256[i][2]); var res2 = test_aes(testvectors256[i][1],testvectors256[i][0],testvectors256[i][2]);
res &= res2; res &= res2;
if (!res2) { if (!res2) {
result[j] = new test_result("Testing 256 bit key vector with block "+ result[j] = new unit.result("Testing 256 bit key vector with block "+
util.hexidump(testvectors256[i][1])+ util.hexidump(testvectors256[i][1])+
" and key "+util.hexidump(testvectors256[i][0])+ " and key "+util.hexidump(testvectors256[i][0])+
" should be "+util.hexidump(testvectors256[i][2]), " should be "+util.hexidump(testvectors256[i][2]),
@ -113,7 +113,7 @@ unittests.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt"
} }
} }
if (res) { if (res) {
result[j] = new test_result("256 bit key test vectors completed.", true) result[j] = new unit.result("256 bit key test vectors completed.", true)
j++; j++;
} }

View File

@ -1,16 +1,17 @@
var unit = require('../../unit.js');
unittests.register("Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt", function() { unit.register("Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt", function() {
var openpgp = require('openpgp'), var openpgp = require('../../../'),
util = openpgp.util, util = openpgp.util,
BFencrypt = openpgp.cipher.blowfish; BFencrypt = openpgp.cipher.blowfish;
var result = new Array(); var result = [];
function test_bf(input, key, output) { function test_bf(input, key, output) {
var blowfish = new openpgp.cipher.blowfish(util.bin2str(key)); var blowfish = new openpgp.cipher.blowfish(util.bin2str(key));
var result = util.bin2str(blowfish.encrypt(input)); var result = util.bin2str(blowfish.encrypt(input));
return (util.hexstrdump(result) == util.hexstrdump(util.bin2str(output))); return (util.hexstrdump(result) == util.hexstrdump(util.bin2str(output)));
}; }
var testvectors = [[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78]], var testvectors = [[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78]],
[[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0x51,0x86,0x6F,0xD5,0xB8,0x5E,0xCB,0x8A]], [[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0x51,0x86,0x6F,0xD5,0xB8,0x5E,0xCB,0x8A]],
[[0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01],[0x7D,0x85,0x6F,0x9A,0x61,0x30,0x63,0xF2]], [[0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01],[0x7D,0x85,0x6F,0x9A,0x61,0x30,0x63,0xF2]],
@ -52,7 +53,7 @@ unittests.register("Blowfish cipher test with test vectors from http://www.schne
var res2 = test_bf(testvectors[i][1],testvectors[i][0],testvectors[i][2]); var res2 = test_bf(testvectors[i][1],testvectors[i][0],testvectors[i][2]);
res &= res2; res &= res2;
if (!res2) { if (!res2) {
result[j] = new test_result("Testing vector "+i+" with block "+ result[j] = new unit.result("Testing vector "+i+" with block "+
util.hexidump(testvectors[i][0])+ util.hexidump(testvectors[i][0])+
" and key "+util.hexidump(testvectors[i][1])+ " and key "+util.hexidump(testvectors[i][1])+
" should be "+util.hexidump(testvectors[i][2]), false); " should be "+util.hexidump(testvectors[i][2]), false);
@ -60,7 +61,7 @@ unittests.register("Blowfish cipher test with test vectors from http://www.schne
} }
} }
if (res) { if (res) {
result[j] = new test_result("34 test vectors completed ", true); result[j] = new unit.result("34 test vectors completed ", true);
} }
return result; return result;
}); });

View File

@ -1,20 +1,21 @@
var unit = require('../../unit.js');
unittests.register("CAST-128 cipher test with test vectors from RFC2144", function() { unit.register("CAST-128 cipher test with test vectors from RFC2144", function() {
var openpgp = require('openpgp'), var openpgp = require('../../../'),
util = openpgp.util; util = openpgp.util;
var result = new Array(); var result = [];
function test_cast(input, key, output) { function test_cast(input, key, output) {
var cast5 = new openpgp.cipher.cast5(util.bin2str(key)); var cast5 = new openpgp.cipher.cast5(util.bin2str(key));
var result = util.bin2str(cast5.encrypt(input)); var result = util.bin2str(cast5.encrypt(input));
return util.hexstrdump(result) == util.hexstrdump(util.bin2str(output)); return util.hexstrdump(result) == util.hexstrdump(util.bin2str(output));
}; }
var testvectors = [[[0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A],[0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF],[0x23,0x8B,0x4F,0xE5,0x84,0x7E,0x44,0xB2]]]; var testvectors = [[[0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A],[0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF],[0x23,0x8B,0x4F,0xE5,0x84,0x7E,0x44,0xB2]]];
for (var i = 0; i < testvectors.length; i++) { for (var i = 0; i < testvectors.length; i++) {
result[i] = new test_result("Testing vector with block "+ result[i] = new unit.result("Testing vector with block "+
util.hexidump(testvectors[i][0])+ util.hexidump(testvectors[i][0])+
" and key "+util.hexidump(testvectors[i][1])+ " and key "+util.hexidump(testvectors[i][1])+
" should be "+util.hexidump(testvectors[i][2]), " should be "+util.hexidump(testvectors[i][2]),

View File

@ -1,9 +1,11 @@
var unit = require('../../unit.js');
unittests.register("TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf", function() { //TODO SC 8/2013 -- This test is failing the first test because of truncating the last 0x00, all other tests are passing. Should investigate.
var openpgp = require('openpgp'), unit.register("TripleDES (EDE) cipher test with test vectors from http://csrc.nist.gov/publications/nistpubs/800-20/800-20.pdf", function() {
var openpgp = require('../../../'),
util = openpgp.util; util = openpgp.util;
var result = new Array(); var result = [];
var key = util.bin2str([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]); var key = util.bin2str([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]);
var testvectors = [[[0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x95,0xF8,0xA5,0xE5,0xDD,0x31,0xD9,0x00]], var testvectors = [[[0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x95,0xF8,0xA5,0xE5,0xDD,0x31,0xD9,0x00]],
[[0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0xDD,0x7F,0x12,0x1C,0xA5,0x01,0x56,0x19]], [[0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0xDD,0x7F,0x12,0x1C,0xA5,0x01,0x56,0x19]],
@ -39,19 +41,17 @@ unittests.register("TripleDES (EDE) cipher test with test vectors from http://cs
res &= res2; res &= res2;
if (!res2) { if (!res2) {
result[j] = new test_result("Testing vector with block "+ result[j] = new unit.result("Testing vector with block " +
util.hexidump(testvectors[i][0]) + util.hexidump(testvectors[i][0]) +
" and key " + util.hexstrdump(key) + " and key " + util.hexstrdump(key) +
" should be "+util.hexidump(testvectors[i][1])+" != " " should be " + util.hexidump(testvectors[i][1]) + " != " +
+util.hexidump(encr), util.hexidump(encr),
false); false);
j++; j++;
} }
} }
if (res) { if (res) {
result[j] = new test_result("All 3DES EDE test vectors completed", true); result[j] = new unit.result("All 3DES EDE test vectors completed", true);
} }
return result; return result;
}); });

View File

@ -1,6 +1,7 @@
var unit = require('../../unit.js');
unittests.register("Twofish test with test vectors from http://www.schneier.com/code/ecb_ival.txt", function() { unit.register("Twofish test with test vectors from http://www.schneier.com/code/ecb_ival.txt", function() {
var openpgp = require('openpgp'), var openpgp = require('../../../'),
util = openpgp.util; util = openpgp.util;
function TFencrypt(block, key) { function TFencrypt(block, key) {
@ -10,7 +11,7 @@ unittests.register("Twofish test with test vectors from http://www.schneier.com/
} }
var result = new Array(); var result = [];
var start = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; var start = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var start_short = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; var start_short = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var testvectors = [[0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F], var testvectors = [[0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F],
@ -30,17 +31,17 @@ unittests.register("Twofish test with test vectors from http://www.schneier.com/
for (var i = 0; i < 49; i++) { for (var i = 0; i < 49; i++) {
var res2 = false; var res2 = false;
var blk, key, ct; var blk, key, ct;
if (i == 0) { if (i === 0) {
blk = start_short; blk = start_short;
key = util.bin2str(start); key = util.bin2str(start);
ct = testvectors[0]; ct = testvectors[0];
res2 = (util.bin2str(TFencrypt(blk,key)) == util.bin2str(ct)); res2 = (util.bin2str(TFencrypt(blk,key)) == util.bin2str(ct));
} else if (i == 1) { } else if (i === 1) {
blk = testvectors[0]; blk = testvectors[0];
key = util.bin2str(start); key = util.bin2str(start);
ct = testvectors[1]; ct = testvectors[1];
res2 = (util.bin2str(TFencrypt(blk,key)) == util.bin2str(ct)); res2 = (util.bin2str(TFencrypt(blk,key)) == util.bin2str(ct));
} else if (i == 2) { } else if (i === 2) {
blk = testvectors[i-1]; blk = testvectors[i-1];
key = util.bin2str(testvectors[i-2].concat(start_short)); key = util.bin2str(testvectors[i-2].concat(start_short));
ct = testvectors[i]; ct = testvectors[i];
@ -56,12 +57,12 @@ unittests.register("Twofish test with test vectors from http://www.schneier.com/
} }
res &= res2; res &= res2;
if (!res2) { if (!res2) {
result[j] = new test_result("Testing vector with block "+util.hexidump(blk)+" with key "+ util.hexstrdump(key) +" should be "+util.hexidump(ct)+" but is "+util.hexidump(TFencrypt(blk,key)), false); result[j] = new unit.result("Testing vector with block "+util.hexidump(blk)+" with key "+ util.hexstrdump(key) +" should be "+util.hexidump(ct)+" but is "+util.hexidump(TFencrypt(blk,key)), false);
j++; j++;
} }
} }
if (res) { if (res) {
result[j] = new test_result("49 test vectors completed", true); result[j] = new unit.result("49 test vectors completed", true);
} }
return result; return result;
}); });

View File

@ -1,6 +1,4 @@
var unit = require('../../unit.js') var unit = require('../../unit.js');
unit.register("MD5 test with test vectors from RFC 1321", function() { unit.register("MD5 test with test vectors from RFC 1321", function() {
var openpgp = require('../../../'), var openpgp = require('../../../'),

View File

@ -1,6 +1,9 @@
var unit = require('../unit.js');
unittests.register("Functional testing of openpgp_crypto_* methods", function() { unit.register("Functional testing of openpgp_crypto_* methods", function() {
var result = new Array(); var openpgp = require('../../');
var util = openpgp.util;
var result = [];
var RSApubMPIstrs = [ var RSApubMPIstrs = [
util.bin2str([0x08,0x00,0xac,0x15,0xb3,0xd6,0xd2,0x0f,0xf0,0x7a,0xdd,0x21,0xb7, util.bin2str([0x08,0x00,0xac,0x15,0xb3,0xd6,0xd2,0x0f,0xf0,0x7a,0xdd,0x21,0xb7,
0xbf,0x61,0xfa,0xca,0x93,0x86,0xc8,0x55,0x5a,0x4b,0xa6,0xa4,0x1a, 0xbf,0x61,0xfa,0xca,0x93,0x86,0xc8,0x55,0x5a,0x4b,0xa6,0xa4,0x1a,
@ -184,84 +187,88 @@ unittests.register("Functional testing of openpgp_crypto_* methods", function()
0xdf,0x48,0x12,0x1b,0x06,0x7d,0x13,0xbc,0x3b,0x49,0xf9,0x86,0xd4,0x53, 0xdf,0x48,0x12,0x1b,0x06,0x7d,0x13,0xbc,0x3b,0x49,0xf9,0x86,0xd4,0x53,
0xed,0x2d,0x68])]; 0xed,0x2d,0x68])];
var RSApubMPIs = new Array(); var RSApubMPIs = [];
for (var i = 0; i < 2; i++) { var i;
RSApubMPIs[i] = new openpgp_type_mpi(); for (i = 0; i < 2; i++) {
RSApubMPIs[i] = new openpgp.mpi();
RSApubMPIs[i].read(RSApubMPIstrs[i],0,RSApubMPIstrs[i].length); RSApubMPIs[i].read(RSApubMPIstrs[i],0,RSApubMPIstrs[i].length);
} }
var RSAsecMPIs = new Array(); var RSAsecMPIs = [];
for (var i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
RSAsecMPIs[i] = new openpgp_type_mpi(); RSAsecMPIs[i] = new openpgp.mpi();
RSAsecMPIs[i].read(RSAsecMPIstrs[i],0,RSAsecMPIstrs[i].length); RSAsecMPIs[i].read(RSAsecMPIstrs[i],0,RSAsecMPIstrs[i].length);
} }
var DSAsecMPIs = new Array(); var DSAsecMPIs = [];
for (var i = 0; i < 1; i++) { for (i = 0; i < 1; i++) {
DSAsecMPIs[i] = new openpgp_type_mpi(); DSAsecMPIs[i] = new openpgp.mpi();
DSAsecMPIs[i].read(DSAsecMPIstrs[i],0,DSAsecMPIstrs[i].length); DSAsecMPIs[i].read(DSAsecMPIstrs[i],0,DSAsecMPIstrs[i].length);
} }
var DSApubMPIs = new Array(); var DSApubMPIs = [];
for (var i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
DSApubMPIs[i] = new openpgp_type_mpi(); DSApubMPIs[i] = new openpgp.mpi();
DSApubMPIs[i].read(DSApubMPIstrs[i],0,DSApubMPIstrs[i].length); DSApubMPIs[i].read(DSApubMPIstrs[i],0,DSApubMPIstrs[i].length);
} }
var ElgamalsecMPIs = new Array(); var ElgamalsecMPIs = [];
for (var i = 0; i < 1; i++) { for (i = 0; i < 1; i++) {
ElgamalsecMPIs[i] = new openpgp_type_mpi(); ElgamalsecMPIs[i] = new openpgp.mpi();
ElgamalsecMPIs[i].read(ElgamalsecMPIstrs[i],0,ElgamalsecMPIstrs[i].length); ElgamalsecMPIs[i].read(ElgamalsecMPIstrs[i],0,ElgamalsecMPIstrs[i].length);
} }
var ElgamalpubMPIs = new Array(); var ElgamalpubMPIs = [];
for (var i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
ElgamalpubMPIs[i] = new openpgp_type_mpi(); ElgamalpubMPIs[i] = new openpgp.mpi();
ElgamalpubMPIs[i].read(ElgamalpubMPIstrs[i],0,ElgamalpubMPIstrs[i].length); ElgamalpubMPIs[i].read(ElgamalpubMPIstrs[i],0,ElgamalpubMPIstrs[i].length);
} }
//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_crypto_signData(2,1,RSApubMPIs, RSAsecMPIs, "foobar"); var RSAsignedData = openpgp.signature.sign(2, 1, RSApubMPIs.concat(RSAsecMPIs), "foobar");
var RSAsignedDataMPI = new openpgp_type_mpi(); var RSAsignedDataMPI = new openpgp.mpi();
result[0] = new test_result("Testing RSA Sign and Verify", RSAsignedDataMPI.read(RSAsignedData, 0, RSAsignedData.length);
openpgp_crypto_verifySignature(1, 2, [ RSAsignedDataMPI.read(RSAsignedData, 0, RSAsignedData.length)], RSApubMPIs, "foobar")); result[0] = new unit.result("Testing RSA Sign and Verify",
openpgp.signature.verify(1, 2, [RSAsignedDataMPI], RSApubMPIs, "foobar"));
// DSA // DSA
var DSAsignedData = openpgp_crypto_signData(2, 17, DSApubMPIs, DSAsecMPIs, "foobar"); var DSAsignedData = openpgp.signature.sign(2, 17, DSApubMPIs.concat(DSAsecMPIs), "foobar");
var DSAmsgMPIs = new Array(); var DSAmsgMPIs = [];
DSAmsgMPIs[0] = new openpgp_type_mpi(); DSAmsgMPIs[0] = new openpgp.mpi();
DSAmsgMPIs[1] = new openpgp_type_mpi(); DSAmsgMPIs[1] = new openpgp.mpi();
DSAmsgMPIs[0].read(DSAsignedData, 0, DSAsignedData.length); DSAmsgMPIs[0].read(DSAsignedData, 0, DSAsignedData.length);
DSAmsgMPIs[1].read(DSAsignedData, DSAmsgMPIs[0].packetLength, DSAsignedData.length); DSAmsgMPIs[1].read(DSAsignedData, DSAmsgMPIs[0].packetLength, DSAsignedData.length);
result[1] = new test_result("Testing DSA Sign and Verify", result[1] = new unit.result("Testing DSA Sign and Verify",
openpgp_crypto_verifySignature(17, 2, DSAmsgMPIs, DSApubMPIs, "foobar")); openpgp.signature.verify(17, 2, DSAmsgMPIs, DSApubMPIs, "foobar"));
var symmAlgo = 9; // AES256 var symmAlgo = 9; // AES256
var symmKey = openpgp_crypto_generateSessionKey(symmAlgo); var symmKey = openpgp.generateSessionKey(symmAlgo);
var symmencDataOCFB = openpgp_crypto_symmetricEncrypt(openpgp_crypto_getPrefixRandom(symmAlgo),symmAlgo, symmKey, "foobar",true); var symmencDataOCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo),symmAlgo, symmKey, "foobar",true);
var symmencDataCFB = openpgp_crypto_symmetricEncrypt(openpgp_crypto_getPrefixRandom(symmAlgo),symmAlgo, symmKey, "foobar",false); var symmencDataCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo),symmAlgo, symmKey, "foobar",false);
result[2] = new test_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_crypto_symmetricDecrypt(symmAlgo,symmKey,symmencDataOCFB,true) == "foobar"); openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataOCFB,true) == "foobar");
result[3] = new test_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_crypto_symmetricDecrypt(symmAlgo,symmKey,symmencDataCFB,false) == "foobar"); openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataCFB,false) == "foobar");
var RSAEncryptedData = openpgp_crypto_asymetricEncrypt(1, RSApubMPIs, new openpgp_type_mpi().create(openpgp_encoding_eme_pkcs1_encode(symmKey, RSApubMPIs[0].mpiByteLength))); var RSAEncryptedData = openpgp.cfb.encrypt(1, RSApubMPIs, new openpgp.mpi().create(openpgp_encoding_eme_pkcs1_encode(symmKey, RSApubMPIs[0].mpiByteLength)));
var RSAEncryptedDataMPI = new openpgp_type_mpi(); var RSAEncryptedDataMPI = new openpgp.mpi();
RSAEncryptedDataMPI.read(RSAEncryptedData, 0,RSAEncryptedData.length); RSAEncryptedDataMPI.read(RSAEncryptedData, 0,RSAEncryptedData.length);
result[4] = new test_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_crypto_asymetricDecrypt(1,RSApubMPIs,RSAsecMPIs,[RSAEncryptedDataMPI]).toMPI().substring(2), RSApubMPIs[0].mpiByteLength) == symmKey); openpgp_encoding_eme_pkcs1_decode(openpgp.cfb.decrypt(1, RSApubMPIs.concat(RSAsecMPIs), [RSAEncryptedDataMPI]).toMPI().substring(2), RSApubMPIs[0].mpiByteLength) == symmKey);
var ElgamalEncryptedData = openpgp_crypto_asymetricEncrypt(16, ElgamalpubMPIs, new openpgp_type_mpi().create(openpgp_encoding_eme_pkcs1_encode(symmKey, ElgamalpubMPIs[0].mpiByteLength))); var ElgamalEncryptedData = openpgp.cfb.encrypt(16, ElgamalpubMPIs, new openpgp.mpi().create(openpgp_encoding_eme_pkcs1_encode(symmKey, ElgamalpubMPIs[0].mpiByteLength)));
var ElgamalEncryptedDataMPIs = new Array(); var ElgamalEncryptedDataMPIs = [];
ElgamalEncryptedDataMPIs[0] = new openpgp_type_mpi(); ElgamalEncryptedDataMPIs[0] = new openpgp.mpi();
ElgamalEncryptedDataMPIs[0].read(ElgamalEncryptedData[0], 0, ElgamalEncryptedData[0].length); ElgamalEncryptedDataMPIs[0].read(ElgamalEncryptedData[0], 0, ElgamalEncryptedData[0].length);
ElgamalEncryptedDataMPIs[1] = new openpgp_type_mpi(); ElgamalEncryptedDataMPIs[1] = new openpgp.mpi();
ElgamalEncryptedDataMPIs[1].read(ElgamalEncryptedData[1], 0, ElgamalEncryptedData[1].length); ElgamalEncryptedDataMPIs[1].read(ElgamalEncryptedData[1], 0, ElgamalEncryptedData[1].length);
result[5] = new test_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_crypto_asymetricDecrypt(16,ElgamalpubMPIs,ElgamalsecMPIs,ElgamalEncryptedDataMPIs).toMPI().substring(2), ElgamalpubMPIs[0].mpiByteLength) == symmKey); openpgp_encoding_eme_pkcs1_decode(openpgp.cfb.decrypt(16, ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedDataMPIs).toMPI().substring(2), ElgamalpubMPIs[0].mpiByteLength) == symmKey);
return result; return result;
}); });

View File

@ -5,3 +5,10 @@ require('./crypto/hash/sha.js');
require('./crypto/hash/md5.js'); require('./crypto/hash/md5.js');
require('./crypto/hash/ripemd.js'); require('./crypto/hash/ripemd.js');
require('./crypto/cipher/aes.js');
require('./crypto/cipher/blowfish.js');
require('./crypto/cipher/cast5.js');
require('./crypto/cipher/des.js');
require('./crypto/cipher/twofish.js');
require('./crypto/openpgp.crypto.js');

File diff suppressed because one or more lines are too long