From cd37ffab48049142a64a705dd4ac4e1b8c022d43 Mon Sep 17 00:00:00 2001 From: "Wright, Jesse C (UG - Computer Science)" <jw02030@surrey.ac.uk> Date: Sun, 8 May 2022 17:39:40 +0000 Subject: [PATCH] Add ed25519 --- cw/package.json | 3 ++- cw/src/messages.ts | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/cw/package.json b/cw/package.json index 25a1610..6cf4f41 100644 --- a/cw/package.json +++ b/cw/package.json @@ -14,6 +14,7 @@ "async-retry": "^1.3.3", "commander": "^9.2.0", "eciesjs": "^0.3.14", - "koa-compose": "^4.1.0" + "koa-compose": "^4.1.0", + "tweetnacl": "^1.0.3" } } diff --git a/cw/src/messages.ts b/cw/src/messages.ts index 29d012a..182f6b7 100644 --- a/cw/src/messages.ts +++ b/cw/src/messages.ts @@ -5,6 +5,7 @@ import { encrypt, decrypt } from 'eciesjs' import { randomBytes } from "crypto"; import { utils, sign, verify } from "@noble/secp256k1"; import { Readable } from "stream"; +import nacl from "tweetnacl"; export function genIntro(): Buffer { let t = Buffer.alloc(7) @@ -113,6 +114,48 @@ export async function receiveEncCheck(write: write, waitForData: waitForData, de } + + + +export async function sendEncCheckED25519(write: write, waitForData: waitForData, destroy: destroy, clientPubKey: Buffer) { + const byteCount = Crypto.randomInt(1_024, (65_536 - 97)) + + const bytes = Crypto.randomBytes(byteCount) + const message = setMessageID(Buffer.alloc(byteCount + 97 + 2), 3) + //const enc = encrypt(clientPubKey, bytes) + const enc = Buffer.from(nacl.box.after(message, clientPubKey.subarray(clientPubKey.length - 1, clientPubKey.length), clientPubKey.subarray(0, clientPubKey.length - 1))) + enc.copy(message, 2) + + await write(message) + + const data = await waitForData(); + + if (getMessageID(data) !== 4) destroy("Expected message ID 4") + + if (Buffer.compare(bytes, data.subarray(2)) !== 0) destroy("Encryption ID check buffer content mismatch") + + await sendConfirmation(write) + +} + +export async function receiveEncCheckED25519(write: write, waitForData: waitForData, destroy: destroy, privKey: Buffer) { + let d = await waitForData(); + if (getMessageID(d) !== 3) destroy("Expected message ID 3") + + const data = nacl.box.open.after(d.subarray(2), privKey.subarray(privKey.length - 1, privKey.length), privKey.subarray(0, privKey.length - 1)) + if (!data) { destroy("enc check no data"); return } + let msg = setMessageID(Buffer.alloc(data.length + 2), 4) + Buffer.from(data).copy(msg, 2) + + await write(msg) + + d = await waitForData(); + + if (getMessageID(d) !== 1) destroy("Expected message ID 1") + +} + + export async function genAndSendSymKey(write: write, waitForData: waitForData, destroy: destroy, privKey: Buffer, clientPubKey: Buffer) { @@ -221,4 +264,4 @@ export async function sendMsg(write: write, waitForData: waitForData, data: Read } console.log("done") -} \ No newline at end of file +} -- GitLab