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));
data += util.Uint8Array_to_str(this.sessionKey);
const checksum = util.calc_checksum(this.sessionKey);
data += util.Uint8Array_to_str(util.writeNumber(checksum, 2));
data += util.Uint8Array_to_str(util.write_checksum(this.sessionKey));
let toEncrypt;
const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm);
@ -142,15 +141,15 @@ PublicKeyEncryptedSessionKey.prototype.decrypt = async function (key) {
let decoded;
if (algo === enums.publicKey.ecdh) {
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 {
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));
if (checksum !== util.calc_checksum(key)) {
if (!util.equalsUint8Array(checksum, util.write_checksum(key))) {
throw new Error('Checksum mismatch');
} else {
this.sessionKey = key;

View File

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

View File

@ -378,20 +378,14 @@ export default {
* Calculates a 16bit sum of a Uint8Array by adding each character
* codes modulus 65535
* @param {Uint8Array} Uint8Array to create a sum of
* @returns {Integer} An integer containing the sum of all character
* codes % 65535
* @returns {Uint8Array} 2 bytes containing the sum of all charcodes % 65535
*/
calc_checksum: function (text) {
const checksum = {
s: 0,
add: function (sadd) {
this.s = (this.s + sadd) % 65536;
}
};
write_checksum: function (text) {
let s = 0;
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);
},
/**