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

Added Key derivation using Scrypt. #654

Merged
merged 1 commit into from
Nov 8, 2019

Conversation

minhoryang
Copy link
Contributor

@minhoryang minhoryang commented Nov 6, 2019

Related: #642

TODO:

  • Code Style
  • Comments
  • Variable Naming
  • Necessary Tests: ...?

Questions:

Copy link
Contributor

@dahlia dahlia left a comment

Choose a reason for hiding this comment

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

Scrypt require some criteria for parameters. Which exception should I fire for this?

Validating these parameters should be in the Scrypt() constructor, as any instance of Scrypt with invalid parameters cannot be made from the very first. According to .NET convention, you could use ArgumentOutOfRangeException. FYI its constructor takes a paramName and this parameter is usually used together with nameof() operator.

Plus, currently ProtectedPrivateKey.FromJson() method does not catch any exceptions during making an IKdf instance, but it would be good if we catch all ArgumentExceptions there and then throw them again by wrapping them with InvalidKeyJsonException. (Or it might be better to have another KeyJsonException subclass like InvalidKeyJsonParametersException?)

switch (kdfType)
{
case "pbkdf2":
kdf = Pbkdf2.FromJson(kdfParamsElement);
break;
case "scrypt":
kdf = Scrypt.FromJson(kdfParamsElement);
break;
default:
throw new UnsupportedKeyJsonException(
$"Unsupported cipher type: \"{kdfType}\"."
);
}

@minhoryang
Copy link
Contributor Author

minhoryang commented Nov 6, 2019

Q. Current implementation of ProtectedPrivateKey.Protect() use Pbkdf2 only. Do we add the option or selector for Scrypt?

@codecov
Copy link

codecov bot commented Nov 6, 2019

Codecov Report

Merging #654 into master will decrease coverage by 2.24%.
The diff coverage is 63.88%.

@@            Coverage Diff             @@
##           master     #654      +/-   ##
==========================================
- Coverage   88.21%   85.96%   -2.25%     
==========================================
  Files         216      194      -22     
  Lines       17127    16944     -183     
==========================================
- Hits        15108    14566     -542     
- Misses       1138     1326     +188     
- Partials      881     1052     +171
Impacted Files Coverage Δ
Libplanet/KeyStore/Kdfs/Scrypt.cs 100% <100%> (ø)
Libplanet/KeyStore/ProtectedPrivateKey.cs 99.55% <100%> (+2.79%) ⬆️
Libplanet.Tests/KeyStore/Kdfs/ScryptTest.cs 34.3% <34.3%> (ø)
...ibplanet.Tests/KeyStore/ProtectedPrivateKeyTest.cs 36.86% <79.5%> (+8.38%) ⬆️
Libplanet.Stun/Stun/Attributes/ErrorCode.cs 0% <0%> (-100%) ⬇️
Libplanet.Stun/Stun/Attributes/Nonce.cs 0% <0%> (-100%) ⬇️
Libplanet.Stun/Stun/Attributes/Realm.cs 0% <0%> (-100%) ⬇️
Libplanet.Stun/Stun/Attributes/MessageIntegrity.cs 0% <0%> (-100%) ⬇️
Libplanet.Stun/Stun/Messages/RefreshRequest.cs 0% <0%> (-100%) ⬇️
Libplanet.Stun/Stun/Attributes/Software.cs 0% <0%> (-100%) ⬇️
... and 34 more

@minhoryang minhoryang force-pushed the feats/scrypt branch 4 times, most recently from 93fd642 to 84deb7b Compare November 6, 2019 15:54
@minhoryang minhoryang changed the title [WIP] Added Key derivation using Scrypt. Added Key derivation using Scrypt. Nov 7, 2019
Copy link
Contributor

@earlbread earlbread left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I left some comments.

@@ -310,7 +406,7 @@ public void FromJsonInvalidCases()

Assert.Throws<InvalidKeyJsonException>(() =>
load(@"{
""address"": ""d80d933db45cc0cf69e9632090f8aaff635dc8e50"", // Invalid length
""address"": ""d80d933db45cc0cf69e9632090f8aaff635dc8e500"", // Invalid length
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this change for?

Copy link
Contributor Author

@minhoryang minhoryang Nov 7, 2019

Choose a reason for hiding this comment

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

I tried to increase the coverage of ProtectedPrivateKey.FromJson()'s address validation logic.

  • GetHexProperty() called ByteUtil.ParseHex() internally
    • ByteUtil.ParseHex() checked the length of input are even, if not throw ArgumentOutOfRangeException
  • when GetHexProperty() met Exception, rethrow it as InvalidKeyJsonException
  • and InvalidKeyJsonException wasn't covered with try {} catch (ArgumentException e) {}

Copy link
Contributor

@earlbread earlbread left a comment

Choose a reason for hiding this comment

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

Thanks, I left some trivial comments.

@dahlia
Copy link
Contributor

dahlia commented Nov 7, 2019

Current implementation of ProtectedPrivateKey.Protect() use Pbkdf2 only. Do we add the option or selector for Scrypt?

That seems a good idea. How about having an optional parameter IKdf kdf? Also, the current Pbkdf2 settings could be a good default IMHO.

Copy link
Contributor

@earlbread earlbread left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Member

@longfin longfin left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@dahlia dahlia left a comment

Choose a reason for hiding this comment

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

@minhoryang Would you add the IKdf kdf optional parameter to ProtectedPrivateKey.Protect() method in another pull request?

@minhoryang
Copy link
Contributor Author

@dahlia I'm totally agreed to finish this PR without IKdf kdf parameter. Now I'm hesitating to implement that or not. Go for merge!

@earlbread earlbread merged commit ecfba19 into planetarium:master Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants