Skip to content

Commit

Permalink
util/crypto.js classed
Browse files Browse the repository at this point in the history
  • Loading branch information
Snafkin547 committed May 23, 2024
1 parent 363044a commit b35e518
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
7 changes: 4 additions & 3 deletions lib/client/arch/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* @namespace
*/

const crypto = require('../util/crypto.js');
const Crypto = require('../util/crypto.js');
const cryptoInstance = new Crypto();
const shamir_share = require('../protocols/shamir/share.js');
const shamir_open = require('../protocols/shamir/open.js');

Expand Down Expand Up @@ -48,15 +49,15 @@ Hooks.prototype.reconstructShare = shamir_open.jiff_lagrange;
// Crypto hooks
Hooks.prototype.encryptSign = function (jiffClient, message) {
if (jiffClient.sodium_ !== false) {
return crypto.encrypt_and_sign.apply(null, arguments);
return cryptoInstance.encrypt_and_sign.apply(null, arguments);
} else {
return message;
}
};

Hooks.prototype.decryptSign = function (jiffClient, cipher) {
if (jiffClient.sodium_ !== false) {
return crypto.decrypt_and_sign.apply(null, arguments);
return cryptoInstance.decrypt_and_sign.apply(null, arguments);
} else {
return cipher;
}
Expand Down
73 changes: 38 additions & 35 deletions lib/client/util/crypto.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
/**
* Encrypts and signs the given message.
* @ignore
* @memberof jiff.utils
* @param {number|string} message - the message to encrypt.
* @param {Uint8Array} encryption_public_key - ascii-armored public key to encrypt with.
* @param {Uint8Array} signing_private_key - the private key of the encrypting party to sign with.
* @returns {object} the signed cipher, includes two properties: 'cipher' and 'nonce'.
*/
exports.encrypt_and_sign = function (jiff, message, encryption_public_key, signing_private_key) {
const nonce = jiff.sodium_.randombytes_buf(jiff.sodium_.crypto_box_NONCEBYTES);
const cipher = jiff.sodium_.crypto_box_easy(message, nonce, encryption_public_key, signing_private_key);
class Crypto {
/**
* Encrypts and signs the given message.
* @ignore
* @memberof jiff.utils
* @param {number|string} message - the message to encrypt.
* @param {Uint8Array} encryption_public_key - ascii-armored public key to encrypt with.
* @param {Uint8Array} signing_private_key - the private key of the encrypting party to sign with.
* @returns {object} the signed cipher, includes two properties: 'cipher' and 'nonce'.
*/
encrypt_and_sign(jiff, message, encryption_public_key, signing_private_key) {
const nonce = jiff.sodium_.randombytes_buf(jiff.sodium_.crypto_box_NONCEBYTES);
const cipher = jiff.sodium_.crypto_box_easy(message, nonce, encryption_public_key, signing_private_key);

const result = { nonce: '[' + nonce.toString() + ']', cipher: '[' + cipher.toString() + ']' };
return result;
};
const result = { nonce: '[' + nonce.toString() + ']', cipher: '[' + cipher.toString() + ']' };
return result;
}

/**
* Decrypts and checks the signature of the given cipher text.
* @ignore
* @memberof jiff.utils
* @param {object} cipher_text - the cipher text to decrypt, includes two properties: 'cipher' and 'nonce'.
* @param {Uint8Array} decryption_secret_key - the secret key to decrypt with.
* @param {Uint8Array} signing_public_key - ascii-armored public key to verify against signature.
* @returns {number|string} the decrypted message if the signature was correct, the decrypted message type should
* the type of operation, such that the returned value has the appropriate type and does
* not need any type modifications.
* @throws error if signature or nonce was forged/incorrect.
*/
exports.decrypt_and_sign = function (jiff, cipher_text, decryption_secret_key, signing_public_key) {
const nonce = new Uint8Array(JSON.parse(cipher_text.nonce));
cipher_text = new Uint8Array(JSON.parse(cipher_text.cipher));
/**
* Decrypts and checks the signature of the given cipher text.
* @ignore
* @memberof jiff.utils
* @param {object} cipher_text - the cipher text to decrypt, includes two properties: 'cipher' and 'nonce'.
* @param {Uint8Array} decryption_secret_key - the secret key to decrypt with.
* @param {Uint8Array} signing_public_key - ascii-armored public key to verify against signature.
* @returns {number|string} the decrypted message if the signature was correct, the decrypted message type should
* the type of operation, such that the returned value has the appropriate type and does
* not need any type modifications.
* @throws error if signature or nonce was forged/incorrect.
*/
decrypt_and_sign(jiff, cipher_text, decryption_secret_key, signing_public_key) {
const nonce = new Uint8Array(JSON.parse(cipher_text.nonce));
cipher_text = new Uint8Array(JSON.parse(cipher_text.cipher));

try {
return jiff.sodium_.crypto_box_open_easy(cipher_text, nonce, signing_public_key, decryption_secret_key, 'text');
} catch (_) {
throw new Error('Bad signature or Bad nonce: Cipher: ' + cipher_text + '. DecSKey: ' + decryption_secret_key + '. SignPKey: ' + signing_public_key);
try {
return jiff.sodium_.crypto_box_open_easy(cipher_text, nonce, signing_public_key, decryption_secret_key, 'text');
} catch (_) {
throw new Error('Bad signature or Bad nonce: Cipher: ' + cipher_text + '. DecSKey: ' + decryption_secret_key + '. SignPKey: ' + signing_public_key);
}
}
};
}
module.exports = Crypto;

0 comments on commit b35e518

Please sign in to comment.