Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cryptochef #1592

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
27,155 changes: 17,100 additions & 10,055 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"babel-plugin-dynamic-import-node": "^2.3.3",
"babel-plugin-transform-builtin-extend": "1.1.2",
"base64-loader": "^1.0.0",
"chromedriver": "^110.0.0",
"chromedriver": "^114.0.2",
"cli-progress": "^3.12.0",
"colors": "^1.4.0",
"copy-webpack-plugin": "^11.0.0",
Expand Down Expand Up @@ -151,6 +151,7 @@
"node-md6": "^0.1.0",
"nodom": "^2.4.0",
"notepack.io": "^3.0.1",
"npm": "^8.19.4",
"ntlm": "^0.1.3",
"nwmatcher": "^1.4.4",
"otp": "0.1.3",
Expand Down Expand Up @@ -181,7 +182,7 @@
"build": "npx grunt prod",
"node": "npx grunt node",
"repl": "node --experimental-modules --experimental-json-modules --experimental-specifier-resolution=node --no-experimental-fetch --no-warnings src/node/repl.mjs",
"test": "npx grunt configTests && node --experimental-modules --experimental-json-modules --no-warnings --no-deprecation --openssl-legacy-provider --no-experimental-fetch tests/node/index.mjs && node --experimental-modules --experimental-json-modules --no-warnings --no-deprecation --openssl-legacy-provider --no-experimental-fetch tests/operations/index.mjs",
"test": "npx grunt configTests && node --experimental-modules --experimental-json-modules --no-warnings --no-deprecation --openssl-legacy-provider --no-experimental-fetch tests/node/index.mjs && node --experimental-modules --experimental-json-modules --no-warnings --no-deprecation --openssl-legacy-provider --no-experimental-fetch tests/operations/index.mjs",
"testnodeconsumer": "npx grunt testnodeconsumer",
"testui": "npx grunt testui",
"testuidev": "npx nightwatch --env=dev",
Expand Down
18 changes: 18 additions & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,24 @@
"Sleep"
]
},
{
"name": "Cryptocurrency",
"ops": [
"Extract Double SHA Artifacts",
"Extract Segwit Addresses",
"Extract Seedphrases",
"Deserialize Extended Key",
"Public Key To Cryptocurrency Address",
"To WIF Format",
"From WIF Format",
"Type Cryptocurrency Artifact",
"Private EC Key to Public Key",
"Seedphrase To Seed",
"Change Extended Key Version",
"Seed To Master Key",
"Decrypt Keystore File"
]
},
{
"name": "Extractors",
"ops": [
Expand Down
286 changes: 286 additions & 0 deletions src/core/lib/Bech32.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/**
* Bech32 Encoding and Decoding resources (BIP0173 and BIP0350)
*
* @author dgoldenberg [virtualcurrency@mitre.org]
* @copyright MITRE 2023, geco 2019
* @license MIT
*/


// ################################################ BEGIN SEGWIT DECODING FUNCTIONS #################################################

/**
* Javascript code below taken from:
* https://github.com/geco/bech32-js/blob/master/bech32-js.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This source includes an MIT license that wants to be copied over as well.

* Implements various segwit encoding / decoding functions.
*/

// Segwit alphabet
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";

const ALPHABET_MAP = {};
for (let z = 0; z < ALPHABET.length; z++) {
const x = ALPHABET.charAt(z);
ALPHABET_MAP[x] = z;
}

/**
* Polynomial multiply step.
* Input value is viewed as 32 bit int.
* Constants taken from the BIP0173 wiki: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
* They are part of the BCH code generator polynomial.
* @param {string} pre
* @returns
*/
function polymodStep (pre) {
const b = pre >> 25;
return ((pre & 0x1FFFFFF) << 5) ^
(-((b >> 0) & 1) & 0x3b6a57b2) ^
(-((b >> 1) & 1) & 0x26508e6d) ^
(-((b >> 2) & 1) & 0x1ea119fa) ^
(-((b >> 3) & 1) & 0x3d4233dd) ^
(-((b >> 4) & 1) & 0x2a1462b3);
}

/**
* Checks the prefix of a string.
* @param {*} prefix
* @returns
*/
function prefixChk (prefix) {
let chk = 1;
for (let i = 0; i < prefix.length; ++i) {
const c = prefix.charCodeAt(i);
if (c < 33 || c > 126) return "KO";
chk = polymodStep(chk) ^ (c >> 5);
}
chk = polymodStep(chk);

for (let i = 0; i < prefix.length; ++i) {
const v = prefix.charCodeAt(i);
chk = polymodStep(chk) ^ (v & 0x1f);
}
return chk;
}

/**
* Bech32 Checksum
* We check the entire string to see if its segwit encoded.
* Lengths and other constants taken from BIP 0173: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
*
* @param {*} str
* @returns
*/
function checkbech32 (str) {
const LIMIT = 90;
if (str.length < 8) return "KO";
if (str.length > LIMIT) return "KO";


const split = str.lastIndexOf("1");
if (split === -1) return "KO";
if (split === 0) return "KO";

const prefix = str.slice(0, split);
const wordChars = str.slice(split + 1);
if (wordChars.length < 6) return "KO";

let chk = prefixChk(prefix);
if (typeof chk === "string") return "KO";

const words = [];
for (let i = 0; i < wordChars.length; ++i) {
const c = wordChars.charAt(i);
const v = ALPHABET_MAP[c];
if (v === undefined) return "KO";
chk = polymodStep(chk) ^ v;
if (i + 6 >= wordChars.length) continue;
words.push(v);
}
// Second number is decimal representation of 0x2bc830a3
// Useful as P2TR addresses are segwit encoded, with different final checksum.
// Taken from https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
if (chk === 1 || chk === 734539939) {
return "OK";
} else {
return "KO";
}
}

// ################################################ END SEGWIT DECODING FUNCTIONS ###################################################

// ################################################ BEGIN MAIN CHECKSUM FUNCTIONS ###################################################

// Segwit Checksum
/**
* Segwit Checksum
* @param {*} str
* @returns
*/
export function segwitChecksum(str) {
return (checkbech32(str) === "OK");
}

// ################################################ END MAIN CHECKSUM FUNCTIONS #####################################################


// ################################################ BEGIN SEGWIT ENCODING FUNCTIONS #################################################

// Taken from https://github.com/sipa/bech32/blob/master/ref/javascript/bech32.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above, this source includes an MIT license that wants to be copied over as well.

// We use this to encode values into segwit encoding.
/**
* Expands the human readable part.
* @param {string} hrp
* @returns
*/
function hrpExpand (hrp) {
const ret = [];
let p;
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) >> 5);
}
ret.push(0);
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) & 31);
}
return ret;
}

const encodings = {
BECH32: "bech32",
BECH32M: "bech32m",
};


/**
* We get the encoding constant.
* Differentiates between Segwit and P2TR.
* Constants found in BIP0173: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
* Also BIP0350: https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
* @param {string} enc
* @returns
*/
function getEncodingConst (enc) {
if (enc === encodings.BECH32) {
return 1;
} else if (enc === encodings.BECH32M) {
return 0x2bc830a3;
} else {
return null;
}
}

// Constants for the BIP0173 BCH Generator polynomial.
const GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];

/**
* Separate version of the polymod step. Taken from https://github.com/sipa/bech32/blob/master/ref/javascript/bech32.js
* Here its an array of values.
* @param {} values
* @returns
*/
function polymod (values) {
let chk = 1;
for (let p = 0; p < values.length; ++p) {
const top = chk >> 25;
chk = (chk & 0x1ffffff) << 5 ^ values[p];
for (let i = 0; i < 5; ++i) {
if ((top >> i) & 1) {
chk ^= GENERATOR[i];
}
}
}
return chk;
}

/**
* Creates the Segwit checksum
* @param {string} hrp
* @param {string} data
* @param {string} enc
* @returns
*/
function createChecksum (hrp, data, enc) {
const values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
const mod = polymod(values) ^ getEncodingConst(enc);
const ret = [];
for (let p = 0; p < 6; ++p) {
ret.push((mod >> 5 * (5 - p)) & 31);
}
return ret;
}

/**
* Converts bits from base 5 to base 8 or back again as appropriate.
* @param {*} data
* @param {*} frombits
* @param {*} tobits
* @param {*} pad
* @returns
*/
function convertbits (data, frombits, tobits, pad) {
let acc = 0;
let bits = 0;
const ret = [];
const maxv = (1 << tobits) - 1;
for (let p = 0; p < data.length; ++p) {
const value = data[p];
if (value < 0 || (value >> frombits) !== 0) {
return null;
}
acc = (acc << frombits) | value;
bits += frombits;
while (bits >= tobits) {
bits -= tobits;
ret.push((acc >> bits) & maxv);
}
}
if (pad) {
if (bits > 0) {
ret.push((acc << (tobits - bits)) & maxv);
}
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
return null;
}
return ret;
}

/**
* Function to encode data into a segwit address.
* We take in the human readable part, the data, and whether its P2TR or Segwit.
* @param {string} hrp
* @param {string} data
* @param {string} enc
* @returns
*/
function segwitEncode (hrp, data, enc) {
const combined = data.concat(createChecksum(hrp, data, enc));
let ret = hrp + "1";
for (let p = 0; p < combined.length; ++p) {
ret += ALPHABET.charAt(combined[p]);
}
return ret;
}

/**
* Turns the public key (as 'program') into the address.
* @param {*} hrp
* @param {*} version
* @param {*} program
* @returns
*/
export function encodeProgramToSegwit (hrp, version, program) {
let enc;
if (version > 0) {
enc = encodings.BECH32M;
} else {
enc = encodings.BECH32;
}
const convertedbits = convertbits(program, 8, 5, true);
const intermediate = [version].concat(convertedbits);
const ret = segwitEncode(hrp, intermediate, enc);
return ret;
}


// ################################################ END SEGWIT ENCODING FUNCTIONS ###################################################
Loading