-
Notifications
You must be signed in to change notification settings - Fork 83
chore(WIP): migrate to @noble/curves and eciesjs #1236
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
Draft
alexandre-abrioux
wants to merge
22
commits into
master
Choose a base branch
from
ecies-js
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.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
26e1c25
chore: migrate to ecies/js
alexandre-abrioux c48a00a
handle sign and recover
alexandre-abrioux b1e5626
Merge branch 'master' into ecies-js
alexandre-abrioux 4ecc262
remove corepack property
alexandre-abrioux 22371d6
fix some tests
alexandre-abrioux 351c3b7
wip
alexandre-abrioux c3fa356
working algo
alexandre-abrioux 431071a
test padded
alexandre-abrioux e7f8dff
wip
alexandre-abrioux c5c0f53
tests green
alexandre-abrioux ad422b8
add test
alexandre-abrioux 619c6e2
remove corepack field
alexandre-abrioux eac9c9a
sinplify
alexandre-abrioux 49f8b06
remove slice
alexandre-abrioux ffbdcc8
tests green
alexandre-abrioux 89d4798
fix parity
alexandre-abrioux 9f5d95e
simplify sign and recover
alexandre-abrioux b9023ba
undo arg name change
alexandre-abrioux 298dbd5
rename args
alexandre-abrioux c07908d
update dotenv
alexandre-abrioux b56af8c
fix build issues
alexandre-abrioux 84aab77
update @graphql-codegen/typescript-graphql-request
alexandre-abrioux 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 hidden or 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 |
---|---|---|
|
@@ -69,5 +69,6 @@ | |
"semver": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/197", | ||
"json-schema": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/51", | ||
"json5": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/165" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72" | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
import { PrivateKey, PublicKey } from 'eciesjs'; | ||
import { secp256k1 } from '@noble/curves/secp256k1'; | ||
import { sha256, sha512 } from '@noble/hashes/sha2'; | ||
import { aes256cbc } from '@ecies/ciphers/aes'; | ||
import { hmac } from '@noble/hashes/hmac'; | ||
|
||
/** | ||
* Decrypt the `eccrypto` way: using ECIES with AES-CBC-MAC and SHA-512 derivation. | ||
* Migrated from https://github.com/torusresearch/eccrypto/blob/923ebc03e5be016a7ee27a04d8c3b496ee949bfa/src/index.ts#L264 | ||
* but using `@noble/curves` instead of `elliptics` | ||
*/ | ||
export const ecDecryptLegacy = (privateKey: string, dataHex: string, padding = false): string => { | ||
const { iv, ephemPublicKey, mac, ciphertext } = legacyAes256CbcMacSplit(dataHex); | ||
const receiverPrivateKey = PrivateKey.fromHex(privateKey.replace(/^0x/, '')); | ||
const sharedKey = deriveSharedKeyWithSha512(receiverPrivateKey, ephemPublicKey, padding); | ||
const encryptionKey = sharedKey.subarray(0, 32); | ||
const macKey = sharedKey.subarray(32); | ||
const dataToMac = Buffer.concat([iv, ephemPublicKey.toBytes(false), ciphertext]); | ||
const macGood = hmacSha256Verify(macKey, dataToMac, mac); | ||
if (!macGood) { | ||
if (!padding) { | ||
return ecDecryptLegacy(privateKey, dataHex, true); | ||
} | ||
throw new Error('The encrypted data is not well formatted'); | ||
} | ||
const decrypted = aes256cbc(encryptionKey, iv).decrypt(ciphertext); | ||
return Buffer.from(decrypted).toString(); | ||
}; | ||
|
||
const hmacSha256Verify = (key: Uint8Array, msg: Uint8Array, sig: Uint8Array): boolean => { | ||
const expectedSig = hmac(sha256, key, msg); | ||
return equalConstTime(expectedSig, sig); | ||
}; | ||
|
||
// Compare two buffers in constant time to prevent timing attacks. | ||
const equalConstTime = (b1: Uint8Array, b2: Uint8Array): boolean => { | ||
if (b1.length !== b2.length) { | ||
return false; | ||
} | ||
let res = 0; | ||
for (let i = 0; i < b1.length; i++) { | ||
res |= b1[i] ^ b2[i]; | ||
} | ||
return res === 0; | ||
}; | ||
|
||
const deriveSharedKeyWithSha512 = ( | ||
privateKey: PrivateKey, | ||
publicKey: PublicKey, | ||
padding = false, | ||
): Uint8Array => { | ||
const sharedPoint = secp256k1.getSharedSecret(privateKey.secret, publicKey.toBytes()); | ||
const paddedBytes = padding ? sharedPoint.subarray(1) : sharedPoint.subarray(2); | ||
const hash = sha512.create().update(paddedBytes).digest(); | ||
return new Uint8Array(hash); | ||
}; | ||
|
||
/** | ||
* Split a legacy-encrypted hex string to its AES-CBC-MAC params. | ||
* See legacy way of generating an encrypted strings with the `@toruslabs/eccrypto` > `elliptic` library: | ||
* https://github.com/RequestNetwork/requestNetwork/blob/4597d373b0284787273471cf306dd9b849c9f76a/packages/utils/src/crypto/ec-utils.ts#L141 | ||
*/ | ||
const legacyAes256CbcMacSplit = (dataHex: string) => { | ||
const buffer = Buffer.from(dataHex, 'hex'); | ||
|
||
const ivSize = 16; | ||
const ephemPublicKeySize = 33; | ||
const ephemPublicKeyEnd = ivSize + ephemPublicKeySize; | ||
const macSize = 32; | ||
const macEnd = ephemPublicKeyEnd + macSize; | ||
|
||
const ephemPublicKeyStr = buffer.subarray(ivSize, ephemPublicKeyEnd); | ||
const ephemPublicKey = new PublicKey(ephemPublicKeyStr); | ||
|
||
return { | ||
iv: buffer.subarray(0, ivSize), | ||
ephemPublicKey, | ||
mac: buffer.subarray(ephemPublicKeyEnd, macEnd), | ||
ciphertext: buffer.subarray(macEnd), | ||
}; | ||
}; |
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.
By using the Web Crypto API (available in Node.js and in browsers) this whole code (both methods
hmacSha256Verify
+equalConstTime
) could be replaced by a single line:Pros:
@noble/hashes
)Cons
ecEncrypt
andecDecrypt
were already async in the previous implementation)- the Web Crypto API is only available in secure context (= HTTPS) (this could be a pain point in
localhost
environments)Let me know if you would prefer it, I can easily make the switch.