knxultimate
Version:
KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.
81 lines • 4.03 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateMessageAuthenticationCodeCBC = calculateMessageAuthenticationCodeCBC;
exports.decryptCtr = decryptCtr;
exports.encryptDataCtr = encryptDataCtr;
exports.deriveDeviceAuthenticationPassword = deriveDeviceAuthenticationPassword;
exports.deriveUserPassword = deriveUserPassword;
exports.generateEcdhKeyPair = generateEcdhKeyPair;
const crypto = __importStar(require("crypto"));
const util_1 = require("./util");
function calculateMessageAuthenticationCodeCBC(key, additionalData, payload = Buffer.alloc(0), block0 = Buffer.alloc(16)) {
const additionalDataLength = Buffer.alloc(2);
additionalDataLength.writeUInt16BE(additionalData.length, 0);
const blocks = Buffer.from(Buffer.concat([block0, additionalDataLength, additionalData, payload]));
const paddedBlocks = (0, util_1.bytePad)(blocks, 16);
const iv = Buffer.alloc(16);
const cipher = crypto.createCipheriv('aes-128-cbc', key.slice(0, 16), iv);
cipher.setAutoPadding(false);
const encrypted = Buffer.from(Buffer.concat([cipher.update(paddedBlocks), cipher.final()]));
return Buffer.from(encrypted.slice(encrypted.length - 16));
}
function decryptCtr(key, counter0, mac, payload = Buffer.alloc(0)) {
const cipher = crypto.createDecipheriv('aes-128-ctr', key.slice(0, 16), counter0.slice(0, 16));
const macTr = Buffer.from(cipher.update(mac));
const decryptedData = Buffer.from(Buffer.concat([cipher.update(payload), cipher.final()]));
return [decryptedData, macTr];
}
function encryptDataCtr(key, counter0, macCbc, payload = Buffer.alloc(0)) {
const cipher = crypto.createCipheriv('aes-128-ctr', key.slice(0, 16), counter0.slice(0, 16));
const encryptedMac = Buffer.from(cipher.update(macCbc));
const encryptedData = Buffer.from(Buffer.concat([cipher.update(payload), cipher.final()]));
return [encryptedData, encryptedMac];
}
function deriveDeviceAuthenticationPassword(deviceAuthenticationPassword) {
return crypto.pbkdf2Sync(Buffer.from(deviceAuthenticationPassword, 'latin1'), 'device-authentication-code.1.secure.ip.knx.org', 65536, 16, 'sha256');
}
function deriveUserPassword(passwordString) {
return crypto.pbkdf2Sync(Buffer.from(passwordString, 'latin1'), 'user-password.1.secure.ip.knx.org', 65536, 16, 'sha256');
}
function generateEcdhKeyPair() {
const privateKey = crypto.generateKeyPairSync('x25519').privateKey;
const publicKey = crypto.createPublicKey(privateKey).export({
format: 'der',
type: 'spki',
});
return [privateKey, Buffer.from(publicKey)];
}
//# sourceMappingURL=security_primitives.js.map