Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Nem cipher encode and decode. #59

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Nem cipher encode and decode. #59

wants to merge 16 commits into from

Conversation

AnthonyLaw
Copy link
Member

@AnthonyLaw AnthonyLaw commented Sep 6, 2021

Added

  • shared key hash method on nacl-fast.
  • encode and decode message method.
  • refactor unpack
    • add isNegate (bool) params in unpack method

@AnthonyLaw AnthonyLaw marked this pull request as ready for review September 7, 2021 07:27
*
* @returns Keccak-256 hash
*/
private static key_derive(shared: Uint8Array, salt: Uint8Array, privateKey: Key, publicKey: Key): number[] {
Copy link
Contributor

@rg911 rg911 Sep 7, 2021

Choose a reason for hiding this comment

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

I guess we use camel rather than snake case elsewhere?

Also, can the shared be an internal var as only zero bytes (new Uint8Array(32)) get passed in ?

Copy link
Member

Choose a reason for hiding this comment

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

y, don't see reason not to.

@@ -651,6 +651,22 @@
return 0;
}

function crypto_shared_key_hash(shared, pk, sk, hash) {
Copy link
Contributor

Choose a reason for hiding this comment

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

shared_key => shared_secret? or perhaps just call it get_scalar_mult_result?

Copy link
Member Author

Choose a reason for hiding this comment

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

I copied this from the old SDK.
All custom methods added crypto_ as a prefix.

maybe call crypto_scalar_mult_result ?

@@ -811,6 +827,55 @@
return 0;
}

function unpack(r, p) {
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps export unpackneg from js script rather than duplicating it?

Copy link
Member

Choose a reason for hiding this comment

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

yup

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, make sense 👍🏼

Copy link
Member Author

Choose a reason for hiding this comment

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

I did a double-check on the code, compare with unpackneg vs unpack most of the code is the same but here is the different part.

in unpackneg
if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);

in unpack
if (par25519(r[0]) !== (p[31]>>7)) Z(r[0], gf0, r[0]);

Copy link
Member

Choose a reason for hiding this comment

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

ah dang, right unpackneg() = unpack AND negate, technically we could extract and/or parametrize this, but not sure if's good idea. cc @Jaguar0625

Choose a reason for hiding this comment

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

yea, i'd recommend refactoring unpack and unpackneg

Choose a reason for hiding this comment

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

@gimre-xymcity or could we negate after calling unpackneg?

const message = payload.slice(48);

const shared = new Uint8Array(32);
const key = this.key_derive(shared, salt, privateKey, publicKey);
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above comments

*
* @returns Keccak-256 hash
*/
private static key_derive(shared: Uint8Array, salt: Uint8Array, privateKey: Key, publicKey: Key): number[] {
Copy link
Member

Choose a reason for hiding this comment

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

y, don't see reason not to.

@@ -811,6 +827,55 @@
return 0;
}

function unpack(r, p) {
Copy link
Member

Choose a reason for hiding this comment

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

yup

expect(Converter.uint8ToUtf8(decrypted)).equal(message);
});

it('Encoding throw error if message exceed 976 btyes', () => {
Copy link
Member

Choose a reason for hiding this comment

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

add a tests checking that 976 is actually passing ;)

Choose a reason for hiding this comment

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

'bytes' (multiple)

@@ -811,6 +827,55 @@
return 0;
}

function unpack(r, p) {

Choose a reason for hiding this comment

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

yea, i'd recommend refactoring unpack and unpackneg

@@ -811,6 +827,55 @@
return 0;
}

function unpack(r, p) {

Choose a reason for hiding this comment

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

@gimre-xymcity or could we negate after calling unpackneg?

const salt = payload.slice(0, 32);
// 16 byte for iv
const iv = payload.slice(32, 48);
// 32 byte for payload

Choose a reason for hiding this comment

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

comment is wrong

Comment on lines 118 to 124
const encoded = salt
? NemCrypto.encode(privateKey, otherPublicKey, clearText, iv, salt)
: encode(privateKey, otherPublicKey, clearText, iv);

const decoded = salt
? NemCrypto.decode(privateKey, otherPublicKey, Converter.hexToUint8(item.salt + item.iv + item.cipherText))
: decode(privateKey, otherPublicKey, encoded);

Choose a reason for hiding this comment

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

why is all this conditional based on salt?

Copy link
Member Author

Choose a reason for hiding this comment

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

So this vector test is shared between Symbol and Nem.

salt is only used in nem.

Choose a reason for hiding this comment

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

all the conditionals look awkward to me, especially for tests, so i'd prefer two functions. but ask @gimre-xymcity his opinion.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with what @Jaguar0625 has written, would rather split in 2

expect(Converter.uint8ToUtf8(decrypted)).equal(message);
});

it('Encoding throw error if message exceed 976 btyes', () => {

Choose a reason for hiding this comment

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

'bytes' (multiple)

@AnthonyLaw AnthonyLaw force-pushed the nem-cipher branch 2 times, most recently from 4c79e29 to 102141d Compare January 7, 2022 20:53
@AnthonyLaw AnthonyLaw requested a review from Jaguar0625 January 7, 2022 20:53
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Member

@gimre-xymcity gimre-xymcity left a comment

Choose a reason for hiding this comment

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

please revert "isNegate" as discussed on slack

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants