From fd7a0f4051d5fd84ea30246e68a134d89963f3c5 Mon Sep 17 00:00:00 2001 From: Mega-Mewthree Date: Fri, 10 Aug 2018 09:56:55 -0700 Subject: [PATCH] Functions to create an ECDH key exchange Allows 2 users to securely obtain a shared key for usage with AES. --- client/src/builtin/E2EE.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/client/src/builtin/E2EE.js b/client/src/builtin/E2EE.js index a56d58e0..4a51f751 100644 --- a/client/src/builtin/E2EE.js +++ b/client/src/builtin/E2EE.js @@ -13,6 +13,7 @@ import { WebpackModules, ReactComponents, MonkeyPatch, Patcher } from 'modules'; import { VueInjector, Reflection } from 'ui'; import E2EEComponent from './E2EEComponent.vue'; import aes256 from 'aes256'; +import crypto from 'crypto'; export default new class E2EE extends BuiltinModule { @@ -36,6 +37,29 @@ export default new class E2EE extends BuiltinModule { handleSubmit(component, args, retVal) { component.props.value = aes256.encrypt('randomkey', component.props.value); } + + get ecdh() { + if (!this._ecdh) this._ecdh = {}; + return this._ecdh; + } + + createKeyExchange(userID) { + this.ecdh[userID] = crypto.createECDH('secp521r1'); + return this.ecdh[userID].generateKeys('base64'); + } + + publicKeyFor(userID) { + return this.ecdh[userID].getPublicKey('base64'); + } + + computeSecret(userID, otherKey) { + const secret = this.ecdh[userID].computeSecret(otherKey, 'base64', 'base64'); + delete this.ecdh[userID]; + // Hashing the shared secret future-proofs against some possible attacks. + const hash = crypto.createHash('sha256'); + hash.update(secret); + return hash.digest('base64'); + } disabled(e) { for (const patch of Patcher.getPatchesByCaller('BD:E2EE')) patch.unpatch();