merged crypto lib changes

This commit is contained in:
Tankred Hase 2013-06-25 16:45:29 +02:00
parent 69d7b2a949
commit b5912a1515
2 changed files with 91 additions and 31 deletions

View File

@ -7,6 +7,6 @@ HTML5 Mail App with Client-side Encryption
Required packages: nodejs, npm
npm install
node server.js
grunt dev
browse to http://localhost:8585

View File

@ -6,6 +6,10 @@
*/
var CryptoBatch = function(aes, rsa, util, _) {
//
// Encrypt batch
//
/**
* Encrypt and sign an item using AES and RSA
* @param i [Object] The item to encrypt
@ -32,32 +36,6 @@
delete i.receiverPk;
};
/**
* Decrypt and verify an item using AES and RSA
* @param i [Object] The item to decrypt
* @param senderPubkey [String] A public key used to verify
*/
this.decryptItemForUser = function(i, senderPubkey) {
// set rsa public key used to verify
rsa.init(senderPubkey);
// verify signature
if (!rsa.verify([i.iv, util.str2Base64(i.id), util.str2Base64(i.senderPk), i.encryptedKey, i.ciphertext], i.signature)) {
throw new Error('Verifying RSA signature failed!');
}
// decrypt symmetric item key for user
i.key = rsa.decrypt(i.encryptedKey);
// symmetrically decrypt JSON and parse to object literal
i.plaintext = JSON.parse(aes.decrypt(i.ciphertext, i.key, i.iv));
// delete ciphertext values
delete i.signature;
delete i.encryptedKey;
delete i.senderPk;
delete i.ciphertext;
};
/**
* Encrypt and sign a list of items using AES and RSA
* @param list [Array] The list of items to encrypt
@ -85,14 +63,40 @@
return list;
};
//
// Decrypt batch
//
/**
* Decrypt and verify a list of items using AES and RSA
* Verfiy an item and decrypt its item key using RSA
* @param i [Object] The item to decrypt
* @param senderPubkey [String] A public key used to verify
*/
this.decryptItemKeyForUser = function(i, senderPubkey) {
// set rsa public key used to verify
rsa.init(senderPubkey);
// verify signature
if (!rsa.verify([i.iv, util.str2Base64(i.id), util.str2Base64(i.senderPk), i.encryptedKey, i.ciphertext], i.signature)) {
throw new Error('Verifying RSA signature failed!');
}
// decrypt symmetric item key for user
i.key = rsa.decrypt(i.encryptedKey);
// delete ciphertext values
delete i.signature;
delete i.encryptedKey;
delete i.senderPk;
};
/**
* Decrypt and verify a list of item keys using RSA
* @param list [Array] The list of items to decrypt
* @param senderPubkeys [Array] A list of public keys used to verify
* @param receiverPrivkey [Array] The receiver's private key used to decrypt
*/
this.decryptListForUser = function(list, senderPubkeys, receiverPrivkey) {
var senderPk, j,
this.decryptListKeysForUser = function(list, senderPubkeys, receiverPrivkey) {
var senderPk,
self = this;
// set receiver private key
@ -106,9 +110,65 @@
});
// decrypt item for user
self.decryptItemForUser(i, senderPk.publicKey);
self.decryptItemKeyForUser(i, senderPk.publicKey);
});
return list;
};
/**
* Decrypt an item using AES
* @param i [Object] The item to decrypt
*/
this.decryptItem = function(i) {
// symmetrically decrypt JSON and parse to object literal
i.plaintext = JSON.parse(aes.decrypt(i.ciphertext, i.key, i.iv));
// delete ciphertext values
delete i.ciphertext;
};
/**
* Decrypt a list of items using AES
* @param i [Object] The item to decrypt
*/
this.decryptList = function(list) {
var self = this;
list.forEach(function(i) {
// decrypt item for user
self.decryptItem(i);
});
};
/**
* Decrypt and verify an item using AES and RSA
* @param i [Object] The item to decrypt
* @param senderPubkey [String] A public key used to verify
*/
this.decryptItemForUser = function(i, senderPubkey) {
// verfiy signature and decrypt item key
this.decryptItemKeyForUser(i, senderPubkey);
// symmetrically decrypt JSON and parse to object literal
this.decryptItem(i);
};
/**
* Decrypt and verify a list of items using AES and RSA
* @param list [Array] The list of items to decrypt
* @param senderPubkeys [Array] A list of public keys used to verify
* @param receiverPrivkey [Array] The receiver's private key used to decrypt
*/
this.decryptListForUser = function(list, senderPubkeys, receiverPrivkey) {
var j;
// verify and decrypt a list of items using RSA
this.decryptListKeysForUser(list, senderPubkeys, receiverPrivkey);
// decrypt a list of items
this.decryptList(list);
// set plaintext as list item
for (j = 0; j < list.length; j++) {
list[j] = list[j].plaintext;