-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
dgoldenberg4143
wants to merge
12
commits into
gchq:master
Choose a base branch
from
mitre:cryptochef
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cryptochef #1592
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
415c59d
Revert "9.47.5"
dgoldenberg4143 f79ca6c
First Cryptochef branch commit.
dgoldenberg4143 3b29d2d
Update, merge from master branch.
dgoldenberg4143 e005dae
Update: Fixed the openssl-legacy-provder error by updating to new ver…
dgoldenberg4143 c322e1f
Update: Fixed typos
dgoldenberg4143 5c3f03a
Update: Moved back to node 18.
dgoldenberg4143 8cf46a2
Switched Node 18 and npm 8
dgoldenberg4143 742a2d6
Tried just updating the chromedriver.
dgoldenberg4143 f5bf07c
Added License and copyright information as needed.
dgoldenberg4143 fe99749
Updated package.json
dgoldenberg4143 810c948
Updated package.json again
dgoldenberg4143 f474ffa
Added BIP32Derive, which uses bip32 from bitcoinjs. Modified a few mo…
dgoldenberg4143 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ################################################### |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.