Further clean up in message and key class. Make tests run.

This commit is contained in:
Thomas Oberndörfer 2013-10-22 20:15:05 +02:00
parent b1c9eb71d2
commit b544343c63
19 changed files with 1368 additions and 1015 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
module.exports = require('./openpgp.js');
module.exports.key = require('./key.js');
module.exports.keyring = require('./keyring.js');
module.exports.message = require('./message.js');
module.exports.util = require('./util');
module.exports.packet = require('./packet');

View File

@ -18,6 +18,7 @@
var packet = require('./packet');
var enums = require('./enums.js');
var armor = require('./encoding/armor.js');
var config = require('./config');
/**
* @class
@ -32,9 +33,11 @@ var armor = require('./encoding/armor.js');
this.packets = packetlist || new packet.list();
/** Returns the primary key (secret or public)
* @returns {packet_secret_key|packet_public_key|null} */
this.getKey = function() {
/**
* Returns the primary key packet (secret or public)
* @returns {packet_secret_key|packet_public_key|null}
*/
this.getKeyPacket = function() {
for (var i = 0; i < this.packets.length; i++) {
if (this.packets[i].tag == enums.packet.public_key ||
this.packets[i].tag == enums.packet.secret_key) {
@ -42,11 +45,13 @@ var armor = require('./encoding/armor.js');
}
}
return null;
};
}
/** Returns all the private and public subkeys
* @returns {[public_subkey|secret_subkey]} */
this.getSubkeys = function() {
/**
* Returns all the private and public subkey packets
* @returns {[public_subkey|secret_subkey]}
*/
this.getSubkeyPackets = function() {
var subkeys = [];
@ -58,31 +63,81 @@ var armor = require('./encoding/armor.js');
}
return subkeys;
};
this.getAllKeys = function() {
return [this.getKey()].concat(this.getSubkeys());
};
this.getKeyids = function() {
var keyids = [];
var keys = this.getAllKeys();
for (var i = 0; i < keys.length; i++) {
keyids.push(keys[i].getKeyId());
}
return keyids;
};
this.getKeyById = function(keyid) {
var keys = this.getAllKeys();
for (var i = 0; i < keys.length; i++) {
if (keys[i].getKeyId().equals(keyid)) {
return keys[i];
}
}
}
this.getSigningKey = function() {
/**
* Returns all the private and public key and subkey packets
* @returns {[public_subkey|secret_subkey|packet_secret_key|packet_public_key]}
*/
this.getAllKeyPackets = function() {
return [this.getKeyPacket()].concat(this.getSubkeyPackets());
}
/**
* Returns key IDs of all key packets
* @returns {[keyid]}
*/
this.getKeyIds = function() {
var keyIds = [];
var keys = this.getAllKeyPackets();
for (var i = 0; i < keys.length; i++) {
keyIds.push(keys[i].getKeyId());
}
return keyIds;
}
/**
* Returns key IDs of all key packets in hex
* @returns {[String]}
*/
this.getKeyIdsHex = function() {
return this.getKeyIds().map(function(keyId) {
return keyId.toHex();
});
}
/**
* Returns first key packet which match to an array of key IDs
* @param {[keyid]} keyIds
* @return {public_subkey|secret_subkey|packet_secret_key|packet_public_key|null}
*/
this.getKeyPacketByIds = function(keyIds) {
var keys = this.getAllKeyPackets();
for (var i = 0; i < keys.length; i++) {
var keyId = keys[i].getKeyId();
for (var j = 0; j < keyIds.length; j++) {
if (keyId.equals(keyIds[j])) {
//TODO return only verified keys
return keys[i];
}
}
}
return null;
}
/**
* Returns true if this is a public key
* @return {Boolean}
*/
this.isPublic = function() {
var publicKeyPackets = this.packets.filterByTag(enums.packet.public_key);
return publicKeyPackets.length !== 0 ? true : false;
}
/**
* Returns true if this is a private key
* @return {Boolean}
*/
this.isPrivate = function() {
var privateKeyPackets = this.packets.filterByTag(enums.packet.private_key);
return privateKeyPackets.length !== 0 ? true : false;
}
/**
* Returns first key packet that is available for signing
* @return {public_subkey|secret_subkey|packet_secret_key|packet_public_key|null}
*/
this.getSigningKeyPacket = function() {
var signing = [ enums.publicKey.rsa_encrypt_sign, enums.publicKey.rsa_sign, enums.publicKey.dsa];
@ -90,7 +145,7 @@ var armor = require('./encoding/armor.js');
return enums.read(enums.publicKey, s);
});
var keys = this.getAllKeys();
var keys = this.getAllKeyPackets();
for (var i = 0; i < keys.length; i++) {
if (signing.indexOf(keys[i].algorithm) !== -1) {
@ -99,38 +154,34 @@ var armor = require('./encoding/armor.js');
}
return null;
};
function getPreferredSignatureHashAlgorithm() {
var pkey = this.getSigningKey();
if (pkey === null) {
util.print_error("private key is for encryption only! Cannot create a signature.");
return null;
}
if (pkey.publicKey.publicKeyAlgorithm == 17) {
var dsa = new DSA();
return dsa.select_hash_algorithm(pkey.publicKey.MPIs[1].toBigInteger()); // q
}
//TODO implement: https://tools.ietf.org/html/rfc4880#section-5.2.3.8
//separate private key preference from digest preferences
return openpgp.config.config.prefer_hash_algorithm;
}
/**
* Finds an encryption key for this key
* @returns null if no encryption key has been found
* Returns preferred signature hash algorithm of this key
* @return {String}
*/
this.getEncryptionKey = function() {
function getPreferredSignatureHashAlgorithm() {
//TODO implement: https://tools.ietf.org/html/rfc4880#section-5.2.3.8
//separate private key preference from digest preferences
return config.prefer_hash_algorithm;
}
/**
* Returns the first valid encryption key packet for this key
* @returns {public_subkey|secret_subkey|packet_secret_key|packet_public_key|null} key packet or null if no encryption key has been found
*/
this.getEncryptionKeyPacket = function() {
// V4: by convention subkeys are prefered for encryption service
// V3: keys MUST NOT have subkeys
var isValidEncryptionKey = function(key) {
//TODO evaluate key flags: http://tools.ietf.org/html/rfc4880#section-5.2.3.21
return key.algorithm != enums.read(enums.publicKey, enums.publicKey.dsa) && key.algorithm != enums.read(enums.publicKey,
enums.publicKey.rsa_sign);
//TODO verify key
//&& keys.verifyKey()
};
var subkeys = this.getSubkeys();
var subkeys = this.getSubkeyPackets();
for (var j = 0; j < subkeys.length; j++) {
if (isValidEncryptionKey(subkeys[j])) {
@ -138,31 +189,40 @@ var armor = require('./encoding/armor.js');
}
}
// if no valid subkey for encryption, use primary key
var primaryKey = this.getKey();
var primaryKey = this.getKeyPacket();
if (isValidEncryptionKey(primaryKey)) {
return primaryKey;
}
return null;
};
}
/**
* Decrypts all secret key and subkey packets
* @param {String} passphrase
* @return {undefined}
*/
this.decrypt = function(passphrase) {
var keys = this.getAllKeys();
//TODO boolean return value
var keys = this.getAllKeyPackets();
for (var i in keys)
if (keys[i].tag == enums.packet.secret_subkey ||
keys[i].tag == enums.packet.secret_key) {
keys[i].decrypt(passphrase);
}
};
}
// TODO need to implement this
// TODO
this.verify = function() {
function revoke() {
}
// TODO
this.revoke = function() {
}
};
}
/**
* reads an OpenPGP armored text and returns a key object
@ -171,6 +231,7 @@ var armor = require('./encoding/armor.js');
*/
key.readArmored = function(armoredText) {
//TODO how do we want to handle bad text? Exception throwing
//TODO don't accept non-key armored texts
var input = armor.decode(armoredText).openpgp;
var packetlist = new packet.list();
packetlist.read(input);

View File

@ -30,67 +30,60 @@ function message(packetlist) {
/**
* Returns the key IDs of the public keys to which the session key is encrypted
* @return {[keyId]} keyId provided as string of hex number (lowercase)
* @return {[keyId]} array of keyid objects
*/
this.getKeyIds = function() {
var keyIds = [];
var pkESKeyPacketlist = this.packets.filterByType(enums.packet.public_key_encrypted_session_key);
pkESKeyPacketlist.forEach(function(packet) {
keyIds.push(packet.publicKeyId.toHex());
keyIds.push(packet.publicKeyId);
});
return keyIds;
}
function generic_decrypt(packets, passphrase) {
var sessionkey;
for (var i = 0; i < packets.length; i++) {
if (packets[i].tag == openpgp_packet.tags.public_key_encrypted_session_key) {
var key = openpgp.keyring.getKeyById(packets[i].public_key_id);
}
}
/**
* Returns the key IDs in hex of the public keys to which the session key is encrypted
* @return {[String]} keyId provided as string of hex numbers (lowercase)
*/
this.getKeyIdsHex = function() {
return this.getKeyIds().map(function(keyId) {
return keyId.toHex();
});
}
/**
* Decrypts a message and generates user interface message out of the found.
* MDC will be verified as well as message signatures
* @param {openpgp_msg_privatekey} private_key the private the message is encrypted with (corresponding to the session key)
* @param {openpgp_packet_encryptedsessionkey} sessionkey the session key to be used to decrypt the message
* @return {} plaintext of the message or null on error
* Decrypts the message
* @param {secret_subkey|packet_secret_key} privateKeyPacket the private key packet (with decrypted secret part) the message is encrypted with (corresponding to the session key)
* @return {[String]} array with plaintext of decrypted messages
*/
this.decrypt = function(key) {
this.decrypt = function(privateKeyPacket) {
var decryptedMessages = [];
var keyids = key.getKeyids();
var pkESKeyPacketlist = this.packets.filterByType(enums.packet.public_key_encrypted_session_key);
outer: for (var i = 0; i < pkESKeyPacketlist.length; i++) {
for (var i = 0; i < pkESKeyPacketlist.length; i++) {
var pkESKeyPacket = pkESKeyPacketlist[i];
for (var j = 0; j < keyids.length; j++) {
if (pkESKeyPacket.publicKeyId.equals(keyids[j])) {
pkESKeyPacket.decrypt(key.getKeyById(pkESKeyPacket.publicKeyId));
var symEncryptedPacketlist = this.packets.filter(function(packet) {
return packet.tag == enums.packet.symmetrically_encrypted || packet.tag == enums.packet.sym_encrypted_integrity_protected;
});
for (var k = 0; k < symEncryptedPacketlist.length; k++) {
var symEncryptedPacket = symEncryptedPacketlist[k];
symEncryptedPacket.decrypt(pkESKeyPacket.sessionKeyAlgorithm, pkESKeyPacket.sessionKey);
for (var l = 0; l < symEncryptedPacket.packets.length; l++) {
var dataPacket = symEncryptedPacket.packets[l];
switch (dataPacket.tag) {
case enums.packet.literal:
decryptedMessages.push(dataPacket.getBytes());
break;
case enums.packet.compressed:
//TODO
break;
default:
//TODO
}
if (pkESKeyPacket.publicKeyId.equals(privateKeyPacket.getKeyId())) {
pkESKeyPacket.decrypt(privateKeyPacket);
var symEncryptedPacketlist = this.packets.filter(function(packet) {
return packet.tag == enums.packet.symmetrically_encrypted || packet.tag == enums.packet.sym_encrypted_integrity_protected;
});
for (var k = 0; k < symEncryptedPacketlist.length; k++) {
var symEncryptedPacket = symEncryptedPacketlist[k];
symEncryptedPacket.decrypt(pkESKeyPacket.sessionKeyAlgorithm, pkESKeyPacket.sessionKey);
for (var l = 0; l < symEncryptedPacket.packets.length; l++) {
var dataPacket = symEncryptedPacket.packets[l];
switch (dataPacket.tag) {
case enums.packet.literal:
decryptedMessages.push(dataPacket.getBytes());
break;
case enums.packet.compressed:
//TODO
break;
default:
//TODO
}
}
break outer;
}
break;
}
}
return decryptedMessages;
@ -181,6 +174,7 @@ function message(packetlist) {
*/
message.readArmored = function(armoredText) {
//TODO how do we want to handle bad text? Exception throwing
//TODO don't accept non-message armored texts
var input = armor.decode(armoredText).openpgp;
var packetlist = new packet.list();
packetlist.read(input);

View File

@ -52,15 +52,15 @@ function _openpgp() {
var sessionKey = crypto.generateSessionKey(enums.read(enums.symmetric, config.encryption_cipher));
keys.forEach(function(key) {
var encryptionKey = key.getEncryptionKey();
if (encryptionKey) {
var encryptionKeyPacket = key.getEncryptionKeyPacket();
if (encryptionKeyPacket) {
var pkESKeyPacket = new packet.public_key_encrypted_session_key();
pkESKeyPacket.publicKeyId = encryptionKey.getKeyId();
pkESKeyPacket.publicKeyAlgorithm = encryptionKey.algorithm;
pkESKeyPacket.publicKeyId = encryptionKeyPacket.getKeyId();
pkESKeyPacket.publicKeyAlgorithm = encryptionKeyPacket.algorithm;
pkESKeyPacket.sessionKey = sessionKey;
//TODO get preferred algo from signature
pkESKeyPacket.sessionKeyAlgorithm = enums.read(enums.symmetric, config.encryption_cipher);
pkESKeyPacket.encrypt(encryptionKey);
pkESKeyPacket.encrypt(encryptionKeyPacket);
messagePacketlist.push(pkESKeyPacket);
}
});
@ -91,12 +91,12 @@ function _openpgp() {
/**
* decrypts message
* @param {[key]} decrypted privateKey
* @param {secret_subkey|packet_secret_key} privateKeyPacket the private key packet (with decrypted secret part) the message is encrypted with
* @param {message} message the message object with the encrypted data
* @return {String} decrypted message as as native JavaScript string
*/
function decryptMessage(privateKey, message) {
return message.decrypt(privateKey);
function decryptMessage(privateKeyPacket, message) {
return message.decrypt(privateKeyPacket);
}
function decryptAndVerifyMessage(privateKey, publicKeys, messagePacketlist) {

View File

@ -140,7 +140,11 @@ var Util = function() {
* @return {String} A native javascript string
*/
this.decode_utf8 = function(utf8) {
return decodeURIComponent(escape(utf8));
try {
return decodeURIComponent(escape(utf8));
} catch (e) {
return utf8;
}
};
var str2bin = function(str, result) {

View File

@ -7,7 +7,7 @@ unit.register("AES Rijndael cipher test with test vectors from ecb_tbl.txt", fun
var result = new Array();
function test_aes(input, key, output) {
var aes = new openpgp.cipher.aes128(util.bin2str(key));
var aes = new openpgp.crypto.cipher.aes128(util.bin2str(key));
var result = util.bin2str(aes.encrypt(input));

View File

@ -3,11 +3,11 @@ var unit = require('../../unit.js');
unit.register("Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt", function() {
var openpgp = require('../../../'),
util = openpgp.util,
BFencrypt = openpgp.cipher.blowfish;
BFencrypt = openpgp.crypto.cipher.blowfish;
var result = [];
function test_bf(input, key, output) {
var blowfish = new openpgp.cipher.blowfish(util.bin2str(key));
var blowfish = new openpgp.crypto.cipher.blowfish(util.bin2str(key));
var result = util.bin2str(blowfish.encrypt(input));
return (util.hexstrdump(result) == util.hexstrdump(util.bin2str(output)));

View File

@ -6,7 +6,7 @@ unit.register("CAST-128 cipher test with test vectors from RFC2144", function()
var result = [];
function test_cast(input, key, output) {
var cast5 = new openpgp.cipher.cast5(util.bin2str(key));
var cast5 = new openpgp.crypto.cipher.cast5(util.bin2str(key));
var result = util.bin2str(cast5.encrypt(input));
return util.hexstrdump(result) == util.hexstrdump(util.bin2str(output));

View File

@ -1,33 +1,33 @@
var unit = require('../../unit.js');
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;
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 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]],
[[0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x2E,0x86,0x53,0x10,0x4F,0x38,0x34,0xEA]],
[[0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x4B,0xD3,0x88,0xFF,0x6C,0xD8,0x1D,0x4F]],
[[0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x20,0xB9,0xE7,0x67,0xB2,0xFB,0x14,0x56]],
[[0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x55,0x57,0x93,0x80,0xD7,0x71,0x38,0xEF]],
[[0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x6C,0xC5,0xDE,0xFA,0xAF,0x04,0x51,0x2F]],
[[0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x0D,0x9F,0x27,0x9B,0xA5,0xD8,0x72,0x60]],
[[0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00],[0xD9,0x03,0x1B,0x02,0x71,0xBD,0x5A,0x0A]],
[[0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00],[0x42,0x42,0x50,0xB3,0x7C,0x3D,0xD9,0x51]],
[[0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00],[0xB8,0x06,0x1B,0x7E,0xCD,0x9A,0x21,0xE5]],
[[0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00],[0xF1,0x5D,0x0F,0x28,0x6B,0x65,0xBD,0x28]],
[[0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00],[0xAD,0xD0,0xCC,0x8D,0x6E,0x5D,0xEB,0xA1]],
[[0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00],[0xE6,0xD5,0xF8,0x27,0x52,0xAD,0x63,0xD1]],
[[0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00],[0xEC,0xBF,0xE3,0xBD,0x3F,0x59,0x1A,0x5E]],
[[0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00],[0xF3,0x56,0x83,0x43,0x79,0xD1,0x65,0xCD]],
[[0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00],[0x2B,0x9F,0x98,0x2F,0x20,0x03,0x7F,0xA9]],
[[0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00],[0x88,0x9D,0xE0,0x68,0xA1,0x6F,0x0B,0xE6]],
[[0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00],[0xE1,0x9E,0x27,0x5D,0x84,0x6A,0x12,0x98]],
[[0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00],[0x32,0x9A,0x8E,0xD5,0x23,0xD7,0x1A,0xEC]],
[[0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00],[0xE7,0xFC,0xE2,0x25,0x57,0xD2,0x3C,0x97]],
[[0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00],[0x12,0xA9,0xF5,0x81,0x7F,0xF2,0xD6,0x5D]],
var unit = require('../../unit.js');
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;
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 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]],
[[0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x2E,0x86,0x53,0x10,0x4F,0x38,0x34,0xEA]],
[[0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x4B,0xD3,0x88,0xFF,0x6C,0xD8,0x1D,0x4F]],
[[0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x20,0xB9,0xE7,0x67,0xB2,0xFB,0x14,0x56]],
[[0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x55,0x57,0x93,0x80,0xD7,0x71,0x38,0xEF]],
[[0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x6C,0xC5,0xDE,0xFA,0xAF,0x04,0x51,0x2F]],
[[0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x0D,0x9F,0x27,0x9B,0xA5,0xD8,0x72,0x60]],
[[0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00],[0xD9,0x03,0x1B,0x02,0x71,0xBD,0x5A,0x0A]],
[[0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00],[0x42,0x42,0x50,0xB3,0x7C,0x3D,0xD9,0x51]],
[[0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00],[0xB8,0x06,0x1B,0x7E,0xCD,0x9A,0x21,0xE5]],
[[0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00],[0xF1,0x5D,0x0F,0x28,0x6B,0x65,0xBD,0x28]],
[[0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00],[0xAD,0xD0,0xCC,0x8D,0x6E,0x5D,0xEB,0xA1]],
[[0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00],[0xE6,0xD5,0xF8,0x27,0x52,0xAD,0x63,0xD1]],
[[0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00],[0xEC,0xBF,0xE3,0xBD,0x3F,0x59,0x1A,0x5E]],
[[0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00],[0xF3,0x56,0x83,0x43,0x79,0xD1,0x65,0xCD]],
[[0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00],[0x2B,0x9F,0x98,0x2F,0x20,0x03,0x7F,0xA9]],
[[0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00],[0x88,0x9D,0xE0,0x68,0xA1,0x6F,0x0B,0xE6]],
[[0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00],[0xE1,0x9E,0x27,0x5D,0x84,0x6A,0x12,0x98]],
[[0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00],[0x32,0x9A,0x8E,0xD5,0x23,0xD7,0x1A,0xEC]],
[[0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00],[0xE7,0xFC,0xE2,0x25,0x57,0xD2,0x3C,0x97]],
[[0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00],[0x12,0xA9,0xF5,0x81,0x7F,0xF2,0xD6,0x5D]],
[[0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00],[0xA4,0x84,0xC3,0xAD,0x38,0xDC,0x9C,0x19]],
[[0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00],[0xFB,0xE0,0x0A,0x8A,0x1E,0xF8,0xAD,0x72]],
[[0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00],[0x75,0x0D,0x07,0x94,0x07,0x52,0x13,0x63]],
@ -70,38 +70,38 @@ unit.register("TripleDES (EDE) cipher test with test vectors from http://csrc.ni
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04],[0xD2,0xFD,0x88,0x67,0xD5,0x0D,0x2D,0xFE]],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02],[0x06,0xE7,0xEA,0x22,0xCE,0x92,0x70,0x8F]],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01],[0x16,0x6B,0x40,0xB4,0x4A,0xBA,0x4B,0xD6]]];
var res = true;
var j = 0;
for (var i = 0; i < testvectors.length; i++) {
var des = new openpgp.cipher.des(key);
var encr = util.bin2str(des.encrypt(testvectors[i][0], key));
var res2 = encr == util.bin2str(testvectors[i][1]);
res &= res2;
if (!res2) {
result[j] = new unit.result("Testing vector with block " +
util.hexidump(testvectors[i][0]) +
" and key " + util.hexstrdump(key) +
" should be " + util.hexidump(testvectors[i][1]) + " != " +
util.hexidump(encr),
false);
j++;
}
}
if (res) {
result[j] = new unit.result("All 3DES EDE test vectors completed", true);
}
return result;
});
var res = true;
var j = 0;
for (var i = 0; i < testvectors.length; i++) {
var des = new openpgp.crypto.cipher.des(key);
var encr = util.bin2str(des.encrypt(testvectors[i][0], key));
var res2 = encr == util.bin2str(testvectors[i][1]);
res &= res2;
if (!res2) {
result[j] = new unit.result("Testing vector with block " +
util.hexidump(testvectors[i][0]) +
" and key " + util.hexstrdump(key) +
" should be " + util.hexidump(testvectors[i][1]) + " != " +
util.hexidump(encr),
false);
j++;
}
}
if (res) {
result[j] = new unit.result("All 3DES EDE test vectors completed", true);
}
return result;
});
unit.register("DES encrypt/decrypt padding tests", function () {
var openpgp = require('../../../'),
util = openpgp.util;
var openpgp = require('../../../'),
util = openpgp.util;
var result = [];
var key = util.bin2str([0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
var testvectors = new Array();
@ -130,7 +130,7 @@ unit.register("DES encrypt/decrypt padding tests", function () {
[[0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D], [0xCA, 0x59, 0x61, 0x3A, 0x83, 0x23, 0x26, 0xDD]],
[[0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F], [0x83, 0x25, 0x79, 0x06, 0x54, 0xA4, 0x44, 0xD9]]];
var des = new openpgp.cipher.originalDes(key);
var des = new openpgp.crypto.cipher.originalDes(key);
var res = true;
var j = 0;

View File

@ -5,7 +5,7 @@ unit.register("Twofish test with test vectors from http://www.schneier.com/code/
util = openpgp.util;
function TFencrypt(block, key) {
var tf = new openpgp.cipher.twofish(key);
var tf = new openpgp.crypto.cipher.twofish(key);
return tf.encrypt(block);
}

View File

@ -3,7 +3,7 @@ var unit = require('../../unit.js');
unit.register("MD5 test with test vectors from RFC 1321", function() {
var openpgp = require('../../../'),
util = openpgp.util,
MD5 = openpgp.hash.md5;
MD5 = openpgp.crypto.hash.md5;
var result = new Array();
result[0] = new unit.result("MD5 (\"\") = d41d8cd98f00b204e9800998ecf8427e",

View File

@ -4,7 +4,7 @@ unit.register("RIPE-MD 160 bits test with test vectors from http://homes.esat.ku
var openpgp = require('../../../'),
util = openpgp.util,
RMDstring = openpgp.hash.ripemd;
RMDstring = openpgp.crypto.hash.ripemd;
var result = new Array();
result[0] = new unit.result("RMDstring (\"\") = 9c1185a5c5e9fc54612808977ee8f548b2258d31",

View File

@ -2,9 +2,9 @@ var unit = require('../../unit.js');
unit.register("SHA* test with test vectors from NIST FIPS 180-2", function() {
var openpgp = require('../../../src'),
var openpgp = require('../../../'),
util = openpgp.util,
hash = openpgp.hash;
hash = openpgp.crypto.hash;
var result = new Array();

View File

@ -225,14 +225,14 @@ 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?
// RSA
var RSAsignedData = openpgp.signature.sign(2, 1, RSApubMPIs.concat(RSAsecMPIs), "foobar");
var RSAsignedData = openpgp.crypto.signature.sign(2, 1, RSApubMPIs.concat(RSAsecMPIs), "foobar");
var RSAsignedDataMPI = new openpgp.mpi();
RSAsignedDataMPI.read(RSAsignedData);
result[0] = new unit.result("Testing RSA Sign and Verify",
openpgp.signature.verify(1, 2, [RSAsignedDataMPI], RSApubMPIs, "foobar"));
openpgp.crypto.signature.verify(1, 2, [RSAsignedDataMPI], RSApubMPIs, "foobar"));
// DSA
var DSAsignedData = openpgp.signature.sign(2, 17, DSApubMPIs.concat(DSAsecMPIs), "foobar");
var DSAsignedData = openpgp.crypto.signature.sign(2, 17, DSApubMPIs.concat(DSAsecMPIs), "foobar");
var DSAmsgMPIs = [];
DSAmsgMPIs[0] = new openpgp.mpi();
@ -240,31 +240,31 @@ unit.register("Functional testing of openpgp.crypto.* methods", function() {
DSAmsgMPIs[0].read(DSAsignedData.substring(0,34));
DSAmsgMPIs[1].read(DSAsignedData.substring(34,68));
result[1] = new unit.result("Testing DSA Sign and Verify",
openpgp.signature.verify(17, 2, DSAmsgMPIs, DSApubMPIs, "foobar"));
openpgp.crypto.signature.verify(17, 2, DSAmsgMPIs, DSApubMPIs, "foobar"));
var symmAlgo = "aes256"; // AES256
var symmKey = openpgp.generateSessionKey(symmAlgo);
var symmencDataOCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo), symmAlgo, "foobarfoobar1234567890", symmKey, true);
var symmencDataCFB = openpgp.cfb.encrypt(openpgp.getPrefixRandom(symmAlgo), symmAlgo, "foobarfoobar1234567890", symmKey, false);
var symmKey = openpgp.crypto.generateSessionKey(symmAlgo);
var symmencDataOCFB = openpgp.crypto.cfb.encrypt(openpgp.crypto.getPrefixRandom(symmAlgo), symmAlgo, "foobarfoobar1234567890", symmKey, true);
var symmencDataCFB = openpgp.crypto.cfb.encrypt(openpgp.crypto.getPrefixRandom(symmAlgo), symmAlgo, "foobarfoobar1234567890", symmKey, false);
result[2] = new unit.result("Testing symmetric encrypt and decrypt with OpenPGP CFB resync",
openpgp.cfb.decrypt(symmAlgo,symmKey,symmencDataOCFB,true) == "foobarfoobar1234567890");
openpgp.crypto.cfb.decrypt(symmAlgo,symmKey,symmencDataOCFB,true) == "foobarfoobar1234567890");
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) == "foobarfoobar1234567890");
openpgp.crypto.cfb.decrypt(symmAlgo,symmKey,symmencDataCFB,false) == "foobarfoobar1234567890");
var RSAUnencryptedData = new openpgp.mpi();
RSAUnencryptedData.fromBytes(openpgp.pkcs1.eme.encode(symmKey, RSApubMPIs[0].byteLength()));
var RSAEncryptedData = openpgp.publicKeyEncrypt("rsa_encrypt_sign", RSApubMPIs, RSAUnencryptedData);
RSAUnencryptedData.fromBytes(openpgp.crypto.pkcs1.eme.encode(symmKey, RSApubMPIs[0].byteLength()));
var RSAEncryptedData = openpgp.crypto.publicKeyEncrypt("rsa_encrypt_sign", RSApubMPIs, RSAUnencryptedData);
result[4] = new unit.result("Testing asymmetric encrypt and decrypt using RSA with eme_pkcs1 padding",
openpgp.pkcs1.eme.decode(openpgp.publicKeyDecrypt("rsa_encrypt_sign", RSApubMPIs.concat(RSAsecMPIs), RSAEncryptedData).write().substring(2), RSApubMPIs[0].byteLength()) == symmKey);
openpgp.crypto.pkcs1.eme.decode(openpgp.crypto.publicKeyDecrypt("rsa_encrypt_sign", RSApubMPIs.concat(RSAsecMPIs), RSAEncryptedData).write().substring(2), RSApubMPIs[0].byteLength()) == symmKey);
var ElgamalUnencryptedData = new openpgp.mpi();
ElgamalUnencryptedData.fromBytes(openpgp.pkcs1.eme.encode(symmKey, ElgamalpubMPIs[0].byteLength()));
var ElgamalEncryptedData = openpgp.publicKeyEncrypt("elgamal", ElgamalpubMPIs, ElgamalUnencryptedData);
ElgamalUnencryptedData.fromBytes(openpgp.crypto.pkcs1.eme.encode(symmKey, ElgamalpubMPIs[0].byteLength()));
var ElgamalEncryptedData = openpgp.crypto.publicKeyEncrypt("elgamal", ElgamalpubMPIs, ElgamalUnencryptedData);
result[5] = new unit.result("Testing asymmetric encrypt and decrypt using Elgamal with eme_pkcs1 padding",
openpgp.pkcs1.eme.decode(openpgp.publicKeyDecrypt("elgamal", ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedData).write().substring(2), ElgamalpubMPIs[0].byteLength()) == symmKey);
openpgp.crypto.pkcs1.eme.decode(openpgp.crypto.publicKeyDecrypt("elgamal", ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedData).write().substring(2), ElgamalpubMPIs[0].byteLength()) == symmKey);
return result;
});

View File

@ -2,9 +2,9 @@ var unit = require('../unit.js');
unit.register("Testing of binary signature checking", function() {
var openpgp = require('../../');
var keyring = require('../../src/openpgp.keyring.js');
var keyring = openpgp.keyring;
var result = [];
var priv_key = openpgp.readArmoredPackets([
var priv_key = openpgp.key.readArmored([
'-----BEGIN PGP PRIVATE KEY BLOCK-----',
'Version: GnuPG v1.4.11 (GNU/Linux)',
'',
@ -32,8 +32,8 @@ unit.register("Testing of binary signature checking", function() {
'AKC8omYPPomN1E/UJFfXdLDIMi5LoA==',
'=LSrW',
'-----END PGP PRIVATE KEY BLOCK-----'
].join("\n"));
var pub_key = openpgp.readArmoredPackets(
].join("\n")).packets;
var pub_key = openpgp.key.readArmored(
[ '-----BEGIN PGP PUBLIC KEY BLOCK-----',
'Version: GnuPG v1.4.11 (GNU/Linux)',
'',
@ -51,8 +51,8 @@ unit.register("Testing of binary signature checking", function() {
'EplqEakMckCtikEnpxYe',
'=b2Ln',
'-----END PGP PUBLIC KEY BLOCK-----'
].join("\n"));
var msg = openpgp.readArmoredPackets([
].join("\n")).packets;
var msg = openpgp.message.readArmored([
'-----BEGIN PGP MESSAGE-----',
'Version: GnuPG v1.4.11 (GNU/Linux)',
'',
@ -68,7 +68,7 @@ unit.register("Testing of binary signature checking", function() {
'aOU=',
'=4iGt',
'-----END PGP MESSAGE-----'
].join("\n"));
].join("\n")).packets;
//TODO need both?
priv_key[0].decrypt("abcd");
priv_key[3].decrypt("abcd");
@ -80,7 +80,7 @@ unit.register("Testing of binary signature checking", function() {
// exercises the GnuPG s2k type 1001 extension:
// the secrets on the primary key have been stripped.
var priv_key_gnupg_ext = openpgp.readArmoredPackets([
var priv_key_gnupg_ext = openpgp.key.readArmored([
'-----BEGIN PGP PRIVATE KEY BLOCK-----',
'Version: GnuPG v1.4.11 (GNU/Linux)',
'',
@ -107,7 +107,7 @@ unit.register("Testing of binary signature checking", function() {
'iY3UT9QkV9d0sMgyLkug',
'=GQsY',
'-----END PGP PRIVATE KEY BLOCK-----',
].join("\n"));
].join("\n")).packets;
priv_key_gnupg_ext[3].decrypt("abcd");
msg[0].decrypt(priv_key_gnupg_ext[3]);
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
@ -149,7 +149,7 @@ unit.register("Testing of binary signature checking", function() {
'-----END PGP PUBLIC KEY BLOCK-----'
].join("\n"));
var msg2 = openpgp.readArmoredPackets([
var msg2 = openpgp.message.readArmored([
'-----BEGIN PGP MESSAGE-----',
'Version: GnuPG v1.4.11 (GNU/Linux)',
'',
@ -158,7 +158,7 @@ unit.register("Testing of binary signature checking", function() {
'AJ9zh4zsK4GIPuEu81YPNmHsju7DYg==',
'=WaSx',
'-----END PGP MESSAGE-----'
].join("\n"));
].join("\n")).packets;
var packetlists = keyring.getPacketlistsForKeyId(msg2[0].signingKeyId.write());
var pubKey = packetlists[0];
msg2[2].verify(pubKey[3], msg2[1]);

View File

@ -2,7 +2,7 @@ var unit = require('../unit.js');
unit.register("Key generation/encryption/decryption", function() {
var openpgp = require('../../');
var keyring = require('../../src/openpgp.keyring.js');
var keyring = openpgp.keyring;
var result = [];
var testHelper = function(passphrase, userid, message) {
var key = openpgp.generateKeyPair(openpgp.enums.publicKey.rsa_encrypt_sign, 512,
@ -12,46 +12,31 @@ unit.register("Key generation/encryption/decryption", function() {
+ 'userid: ' + userid + '\n'
+ 'message: ' + message;
var keyPacketlist = openpgp.readArmoredPackets(key);
debugger;
var privKey = openpgp.getKeyFromPacketlist(keyPacketlist, passphrase);
var privKey = openpgp.key.readArmored(key);
var encrypted = openpgp.encryptMessage(keyPacketlist, message);
var encrypted = openpgp.encryptMessage([privKey], message);
openpgp.keyring.importPublicKey(key.publicKeyArmored);
var msg = openpgp.read_message(encrypted);
var keymat = null;
var sesskey = null;
var msg = openpgp.message.readArmored(encrypted);
// Find the private (sub)key for the session key of the message
for (var i = 0; i< msg[0].sessionKeys.length; i++) {
if (priv_key.privateKeyPacket.publicKey.getKeyId().write() == msg[0].sessionKeys[i].keyId.bytes) {
keymat = { key: priv_key, keymaterial: priv_key.privateKeyPacket};
sesskey = msg[0].sessionKeys[i];
break;
}
for (var j = 0; j < priv_key.subKeys.length; j++) {
if (priv_key.subKeys[j].publicKey.getKeyId().write() == msg[0].sessionKeys[i].keyId.bytes) {
keymat = { key: priv_key, keymaterial: priv_key.subKeys[j]};
sesskey = msg[0].sessionKeys[i];
break;
}
}
var privKeyPacket = privKey.getKeyPacketByIds(msg.getKeyIds());
if (!privKeyPacket) {
return new unit.result("No private key found!" + info, false);
}
var decrypted = '';
if (keymat !== null) {
if (!keymat.keymaterial.decryptSecretMPIs(passphrase)) {
return new test_result("Password for secrect key was incorrect!",
+ info, false);
try {
if (!privKeyPacket.decrypt(passphrase)) {
return new unit.result("Password for secrect key was incorrect!" + info, false);
}
decrypted = msg[0].decrypt(keymat, sesskey);
} else {
return new test_result("No private key found!" + info, false);
} catch (e) {
return new unit.result("Exception on decrypt of private key packet!" + info, false);
}
return new test_result(message + ' == ' + decrypted + info, message == decrypted);
var decrypted = msg.decrypt(privKeyPacket)[0];
return new unit.result(message + ' == ' + decrypted + info, message == decrypted);
};
result.push(testHelper('password', 'Test McTestington <test@example.com>', 'hello world'));
@ -61,7 +46,9 @@ unit.register("Key generation/encryption/decryption", function() {
});
unit.register("Encryption/decryption", function() {
var openpgp = require('../../src');
var openpgp = require('../../');
var result = [];
var pub_key =
['-----BEGIN PGP PUBLIC KEY BLOCK-----',
@ -155,10 +142,14 @@ unit.register("Encryption/decryption", function() {
var privKey = openpgp.key.readArmored(priv_key);
privKey.decrypt('hello world');
var privKeyPacket = privKey.getKeyPacketByIds(message.getKeyIds());
var decrypted = openpgp.decryptMessage(privKey, message);
privKeyPacket.decrypt('hello world');
return new test_result('Encrypt plain text and afterwards decrypt leads to same result', plaintext == decrypted);
var decrypted = openpgp.decryptMessage(privKeyPacket, message);
result[0] = new unit.result('Encrypt plain text and afterwards decrypt leads to same result', plaintext == decrypted);
return result;
});

File diff suppressed because one or more lines are too long