Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/crypto.js
1129 views
"use strict";1var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {2if (k2 === undefined) k2 = k;3var desc = Object.getOwnPropertyDescriptor(m, k);4if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {5desc = { enumerable: true, get: function() { return m[k]; } };6}7Object.defineProperty(o, k2, desc);8}) : (function(o, m, k, k2) {9if (k2 === undefined) k2 = k;10o[k2] = m[k];11}));12var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {13Object.defineProperty(o, "default", { enumerable: true, value: v });14}) : function(o, v) {15o["default"] = v;16});17var __importStar = (this && this.__importStar) || function (mod) {18if (mod && mod.__esModule) return mod;19var result = {};20if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);21__setModuleDefault(result, mod);22return result;23};24var __importDefault = (this && this.__importDefault) || function (mod) {25return (mod && mod.__esModule) ? mod : { "default": mod };26};27Object.defineProperty(exports, "__esModule", { value: true });28exports.hkdf = exports.sha256 = exports.hmacSign = exports.aesEncrypWithIV = exports.aesEncrypt = exports.aesDecryptWithIV = exports.aesDecrypt = exports.aesDecryptGCM = exports.aesEncryptGCM = exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;29const crypto_1 = require("crypto");30const futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));31const libsignal = __importStar(require("libsignal"));32const Defaults_1 = require("../Defaults");33/** prefix version byte to the pub keys, required for some curve crypto functions */34const generateSignalPubKey = (pubKey) => (pubKey.length === 3335? pubKey36: Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey]));37exports.generateSignalPubKey = generateSignalPubKey;38exports.Curve = {39generateKeyPair: () => {40const { pubKey, privKey } = libsignal.curve.generateKeyPair();41return {42private: Buffer.from(privKey),43// remove version byte44public: Buffer.from(pubKey.slice(1))45};46},47sharedKey: (privateKey, publicKey) => {48const shared = libsignal.curve.calculateAgreement((0, exports.generateSignalPubKey)(publicKey), privateKey);49return Buffer.from(shared);50},51sign: (privateKey, buf) => (libsignal.curve.calculateSignature(privateKey, buf)),52verify: (pubKey, message, signature) => {53try {54libsignal.curve.verifySignature((0, exports.generateSignalPubKey)(pubKey), message, signature);55return true;56}57catch (error) {58return false;59}60}61};62const signedKeyPair = (identityKeyPair, keyId) => {63const preKey = exports.Curve.generateKeyPair();64const pubKey = (0, exports.generateSignalPubKey)(preKey.public);65const signature = exports.Curve.sign(identityKeyPair.private, pubKey);66return { keyPair: preKey, signature, keyId };67};68exports.signedKeyPair = signedKeyPair;69const GCM_TAG_LENGTH = 128 >> 3;70/**71* encrypt AES 256 GCM;72* where the tag tag is suffixed to the ciphertext73* */74function aesEncryptGCM(plaintext, key, iv, additionalData) {75const cipher = (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);76cipher.setAAD(additionalData);77return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);78}79exports.aesEncryptGCM = aesEncryptGCM;80/**81* decrypt AES 256 GCM;82* where the auth tag is suffixed to the ciphertext83* */84function aesDecryptGCM(ciphertext, key, iv, additionalData) {85const decipher = (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);86// decrypt additional adata87const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);88const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);89// set additional data90decipher.setAAD(additionalData);91decipher.setAuthTag(tag);92return Buffer.concat([decipher.update(enc), decipher.final()]);93}94exports.aesDecryptGCM = aesDecryptGCM;95/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */96function aesDecrypt(buffer, key) {97return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));98}99exports.aesDecrypt = aesDecrypt;100/** decrypt AES 256 CBC */101function aesDecryptWithIV(buffer, key, IV) {102const aes = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, IV);103return Buffer.concat([aes.update(buffer), aes.final()]);104}105exports.aesDecryptWithIV = aesDecryptWithIV;106// encrypt AES 256 CBC; where a random IV is prefixed to the buffer107function aesEncrypt(buffer, key) {108const IV = (0, crypto_1.randomBytes)(16);109const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);110return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer111}112exports.aesEncrypt = aesEncrypt;113// encrypt AES 256 CBC with a given IV114function aesEncrypWithIV(buffer, key, IV) {115const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);116return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer117}118exports.aesEncrypWithIV = aesEncrypWithIV;119// sign HMAC using SHA 256120function hmacSign(buffer, key, variant = 'sha256') {121return (0, crypto_1.createHmac)(variant, key).update(buffer).digest();122}123exports.hmacSign = hmacSign;124function sha256(buffer) {125return (0, crypto_1.createHash)('sha256').update(buffer).digest();126}127exports.sha256 = sha256;128// HKDF key expansion129function hkdf(buffer, expandedLength, info) {130return (0, futoin_hkdf_1.default)(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info);131}132exports.hkdf = hkdf;133134135