Compute alternative MAC

This commit is contained in:
Lee Miller 2022-12-23 18:50:33 +02:00
parent 51b1b31ef1
commit c4536dc4bd

View File

@ -3,6 +3,7 @@
// var objects = require('bitmessage').objects; // var objects = require('bitmessage').objects;
// var Address = require('bitmessage').Address; // var Address = require('bitmessage').Address;
// var TcpTransport = require('bitmessage-transports').TcpTransport; // var TcpTransport = require('bitmessage-transports').TcpTransport;
var crypto = require("crypto");
var eccrypto = require("eccrypto"); var eccrypto = require("eccrypto");
var assert = exports.assert = function(condition, message) { var assert = exports.assert = function(condition, message) {
@ -16,6 +17,9 @@ var assert = exports.assert = function(condition, message) {
'0409d4e5c0ab3d25fe048c64c9da1a242c7f19417e9517cd266950d72c755713585c6178e97fe092fc897c9a1f1720d5770ae8eaad2fa8fcbd08e9324a5dde1857', 'hex' '0409d4e5c0ab3d25fe048c64c9da1a242c7f19417e9517cd266950d72c755713585c6178e97fe092fc897c9a1f1720d5770ae8eaad2fa8fcbd08e9324a5dde1857', 'hex'
); );
const message = Buffer.from('The quick brown fox jumps over the lazy dog.'); const message = Buffer.from('The quick brown fox jumps over the lazy dog.');
const ephemPublicKey = Buffer.from(
'02ca00200293213dcf1388b61c2ae5cf80fee6ffffc049a2f9fe7365fe3867813ca812920020df94686c6afb565ac6149b153d61b3b287ee2c7f997c14238796c12b43a3865a', 'hex'
);
const sampleOpts = { const sampleOpts = {
iv: Buffer.from('bddb7c2829b08038753084a2f3991681', 'hex'), iv: Buffer.from('bddb7c2829b08038753084a2f3991681', 'hex'),
ephemPrivateKey: Buffer.from( ephemPrivateKey: Buffer.from(
@ -24,8 +28,29 @@ var assert = exports.assert = function(condition, message) {
) )
}; };
const sampleCiphertext = '64203d5b24688e2547bba345fa139a5a1d962220d4d48a0cf3b1572c0d95b61643a6f9a0d75af7eacc1bd957147bf723'; const sampleCiphertext = '64203d5b24688e2547bba345fa139a5a1d962220d4d48a0cf3b1572c0d95b61643a6f9a0d75af7eacc1bd957147bf723';
const sampleMACKey = 'f83f1e9cc5d6b8448d39dc6a9d5f5b7f460e4a78e9286ee8d91ce1660a53eacd';
const sampleMAC = 'f2526d61b4851fb23409863826fd206165edc021368c7946571cead69046e619'; const sampleMAC = 'f2526d61b4851fb23409863826fd206165edc021368c7946571cead69046e619';
var mackey = await eccrypto.derive(
sampleOpts.ephemPrivateKey, encPublicKey
).then(function(Px) {
var hash = crypto.createHash("sha512").update(Px).digest();
// var encryptionKey = hash.slice(0, 32);
var macKey = hash.slice(32);
return macKey
});
console.log('[mackey]', mackey.toString('hex'));
assert(mackey.toString('hex') == sampleMACKey, 'Bad MAC key!')
var alt_mac = crypto.createHmac("sha256", mackey).update(
Buffer.concat([
sampleOpts.iv, ephemPublicKey, Buffer.from(sampleCiphertext, 'hex')])
).digest()
console.log('[alt mac]', alt_mac.toString('hex'));
var encrypted = await eccrypto.encrypt(encPublicKey, message, sampleOpts); var encrypted = await eccrypto.encrypt(encPublicKey, message, sampleOpts);
console.log('[ephem pubkey]', encrypted.ephemPublicKey.toString('hex'));
console.log('[ciphertext]', encrypted.ciphertext.toString('hex')); console.log('[ciphertext]', encrypted.ciphertext.toString('hex'));
console.log('[mac]', encrypted.mac.toString('hex')); console.log('[mac]', encrypted.mac.toString('hex'));