Clean up checksum calculation

This commit is contained in:
Daniel Huigens 2018-11-01 14:39:03 +01:00
parent e8a2c45390
commit a250ee9f91
3 changed files with 12 additions and 23 deletions

View File

@ -108,8 +108,7 @@ PublicKeyEncryptedSessionKey.prototype.encrypt = async function (key) {
let data = String.fromCharCode(enums.write(enums.symmetric, this.sessionKeyAlgorithm)); let data = String.fromCharCode(enums.write(enums.symmetric, this.sessionKeyAlgorithm));
data += util.Uint8Array_to_str(this.sessionKey); data += util.Uint8Array_to_str(this.sessionKey);
const checksum = util.calc_checksum(this.sessionKey); data += util.Uint8Array_to_str(util.write_checksum(this.sessionKey));
data += util.Uint8Array_to_str(util.writeNumber(checksum, 2));
let toEncrypt; let toEncrypt;
const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm); const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm);
@ -142,15 +141,15 @@ PublicKeyEncryptedSessionKey.prototype.decrypt = async function (key) {
let decoded; let decoded;
if (algo === enums.publicKey.ecdh) { if (algo === enums.publicKey.ecdh) {
decoded = crypto.pkcs5.decode(result.toString()); decoded = crypto.pkcs5.decode(result.toString());
checksum = util.readNumber(util.str_to_Uint8Array(decoded.substr(decoded.length - 2))); checksum = util.str_to_Uint8Array(decoded.substr(decoded.length - 2));
} else { } else {
decoded = crypto.pkcs1.eme.decode(result.toString()); decoded = crypto.pkcs1.eme.decode(result.toString());
checksum = util.readNumber(result.toUint8Array().slice(result.byteLength() - 2)); checksum = result.toUint8Array().slice(result.byteLength() - 2);
} }
key = util.str_to_Uint8Array(decoded.substring(1, decoded.length - 2)); key = util.str_to_Uint8Array(decoded.substring(1, decoded.length - 2));
if (checksum !== util.calc_checksum(key)) { if (!util.equalsUint8Array(checksum, util.write_checksum(key))) {
throw new Error('Checksum mismatch'); throw new Error('Checksum mismatch');
} else { } else {
this.sessionKey = key; this.sessionKey = key;

View File

@ -59,10 +59,6 @@ function SecretKey(date=new Date()) {
SecretKey.prototype = new publicKey(); SecretKey.prototype = new publicKey();
SecretKey.prototype.constructor = SecretKey; SecretKey.prototype.constructor = SecretKey;
function write_checksum(c) {
return util.writeNumber(util.calc_checksum(c), 2);
}
// Helper function // Helper function
function parse_cleartext_params(cleartext, algorithm) { function parse_cleartext_params(cleartext, algorithm) {
@ -122,7 +118,7 @@ SecretKey.prototype.read = function (bytes) {
// key data. These algorithm-specific fields are as described // key data. These algorithm-specific fields are as described
// below. // below.
const cleartext = bytes.subarray(1, -2); const cleartext = bytes.subarray(1, -2);
if (!util.equalsUint8Array(write_checksum(cleartext), bytes.subarray(-2))) { if (!util.equalsUint8Array(util.write_checksum(cleartext), bytes.subarray(-2))) {
throw new Error('Key checksum mismatch'); throw new Error('Key checksum mismatch');
} }
const privParams = parse_cleartext_params(cleartext, this.algorithm); const privParams = parse_cleartext_params(cleartext, this.algorithm);
@ -142,7 +138,7 @@ SecretKey.prototype.write = function () {
arr.push(new Uint8Array([0])); arr.push(new Uint8Array([0]));
const cleartextParams = write_cleartext_params(this.params, this.algorithm); const cleartextParams = write_cleartext_params(this.params, this.algorithm);
arr.push(cleartextParams); arr.push(cleartextParams);
arr.push(write_checksum(cleartextParams)); arr.push(util.write_checksum(cleartextParams));
} else { } else {
arr.push(this.encrypted); arr.push(this.encrypted);
} }
@ -304,7 +300,7 @@ SecretKey.prototype.decrypt = async function (passphrase) {
if (s2k_usage === 255) { if (s2k_usage === 255) {
hashlen = 2; hashlen = 2;
cleartext = cleartextWithHash.subarray(0, -hashlen); cleartext = cleartextWithHash.subarray(0, -hashlen);
hash = write_checksum(cleartext); hash = util.write_checksum(cleartext);
} else { } else {
hashlen = 20; hashlen = 20;
cleartext = cleartextWithHash.subarray(0, -hashlen); cleartext = cleartextWithHash.subarray(0, -hashlen);

View File

@ -378,20 +378,14 @@ export default {
* Calculates a 16bit sum of a Uint8Array by adding each character * Calculates a 16bit sum of a Uint8Array by adding each character
* codes modulus 65535 * codes modulus 65535
* @param {Uint8Array} Uint8Array to create a sum of * @param {Uint8Array} Uint8Array to create a sum of
* @returns {Integer} An integer containing the sum of all character * @returns {Uint8Array} 2 bytes containing the sum of all charcodes % 65535
* codes % 65535
*/ */
calc_checksum: function (text) { write_checksum: function (text) {
const checksum = { let s = 0;
s: 0,
add: function (sadd) {
this.s = (this.s + sadd) % 65536;
}
};
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
checksum.add(text[i]); s = (s + text[i]) & 0xFFFF;
} }
return checksum.s; return util.writeNumber(s, 2);
}, },
/** /**