diff --git a/package-lock.json b/package-lock.json index 8bcb070..649f942 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@stablelib/random": "^1.0.2", "@stablelib/sha256": "^1.0.1", "@stablelib/x25519": "^1.0.1", + "bn.js": "^5.2.1", "js-base64": "^3.7.3", "pkcs7-padding": "^0.1.1", "uint8arraylist": "^2.3.2", @@ -26,6 +27,7 @@ "@rollup/plugin-node-resolve": "^13.3.0", "@size-limit/preset-big-lib": "^8.0.0", "@types/app-root-path": "^1.2.4", + "@types/bn.js": "^5.1.1", "@types/chai": "^4.2.15", "@types/debug": "^4.1.7", "@types/mocha": "^9.1.0", @@ -2566,6 +2568,15 @@ "integrity": "sha512-yhURoXmWN/zfw2MXXcOdUTwe5CWhzRWtb4Rs2+JwGpuhm2hxLCzjASi/aIuyVeJB3Iyks1teaHyOEfNNmltdvQ==", "dev": true }, + "node_modules/@types/bn.js": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.1.tgz", + "integrity": "sha512-qNrYbZqMx0uJAfKnKclPh+dTwK33KfLHYqtyODwd5HnXOjnkhc4qgn3BrK6RWyGZm5+sIFE7Q7Vz6QQtJB7w7g==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/chai": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.4.tgz", @@ -3454,6 +3465,11 @@ "readable-stream": "^3.4.0" } }, + "node_modules/bn.js": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.2.1.tgz", + "integrity": "sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==" + }, "node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", @@ -14021,6 +14037,15 @@ "integrity": "sha512-yhURoXmWN/zfw2MXXcOdUTwe5CWhzRWtb4Rs2+JwGpuhm2hxLCzjASi/aIuyVeJB3Iyks1teaHyOEfNNmltdvQ==", "dev": true }, + "@types/bn.js": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.1.tgz", + "integrity": "sha512-qNrYbZqMx0uJAfKnKclPh+dTwK33KfLHYqtyODwd5HnXOjnkhc4qgn3BrK6RWyGZm5+sIFE7Q7Vz6QQtJB7w7g==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/chai": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.4.tgz", @@ -14716,6 +14741,11 @@ "readable-stream": "^3.4.0" } }, + "bn.js": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.2.1.tgz", + "integrity": "sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==" + }, "body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", diff --git a/package.json b/package.json index b018b65..66adecf 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@rollup/plugin-node-resolve": "^13.3.0", "@size-limit/preset-big-lib": "^8.0.0", "@types/app-root-path": "^1.2.4", + "@types/bn.js": "^5.1.1", "@types/chai": "^4.2.15", "@types/debug": "^4.1.7", "@types/mocha": "^9.1.0", @@ -119,6 +120,7 @@ "@stablelib/random": "^1.0.2", "@stablelib/sha256": "^1.0.1", "@stablelib/x25519": "^1.0.1", + "bn.js": "^5.2.1", "js-base64": "^3.7.3", "pkcs7-padding": "^0.1.1", "uint8arraylist": "^2.3.2", diff --git a/src/crypto.ts b/src/crypto.ts index 7a02439..e4bd455 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -35,6 +35,11 @@ export function getHKDF(ck: bytes32, ikm: Uint8Array): Hkdf { return [k1, k2, k3]; } +export function getHKDFRaw(ck: bytes32, ikm: Uint8Array, numBytes: number): Uint8Array { + const hkdf = new HKDF(SHA256, ikm, ck); + return hkdf.expand(numBytes); +} + export function generateX25519KeyPair(): KeyPair { const keypair = x25519.generateKeyPair(); diff --git a/src/handshake.ts b/src/handshake.ts index 94998a2..b944e74 100644 --- a/src/handshake.ts +++ b/src/handshake.ts @@ -1,9 +1,10 @@ +import { BN } from "bn.js"; import * as pkcs7 from "pkcs7-padding"; import { equals as uint8ArrayEquals } from "uint8arrays/equals"; import { bytes32 } from "./@types/basic"; import { KeyPair } from "./@types/keypair"; -//import { getHKDF } from "./crypto"; +import { getHKDFRaw } from "./crypto"; import { HandshakeState, NoisePaddingBlockSize } from "./handshake_state"; import { CipherState } from "./noise"; import { HandshakePattern, PayloadV2ProtocolIDs } from "./patterns"; @@ -158,11 +159,10 @@ export class Handshake { // Generates an 8 decimal digits authorization code using HKDF and the handshake state genAuthcode(): string { - //var output: array[1, array[8, byte]] - // const [output0] = getHKDF(this.hs.ss.h, new Uint8Array()); - // let code = cast[uint64](output[0]) mod 100_000_000 - // return $code - return "TODO: implement"; + const output0 = getHKDFRaw(this.hs.ss.h, new Uint8Array(), 8); + const bn = new BN(output0); + const code = bn.mod(new BN(100_000_000)); + return code.toString(); } // Advances 1 step in handshake