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

Sanity check in PrivateKey constructor using bytes #438

Merged
merged 6 commits into from Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -82,6 +82,7 @@ To be released.
the `Transaction<T>` more than once. [[#413]]
- `BlockChain<T>.Swap()` became to omit common block finding when `render` is
`false`. [[#423]]
- `PrivateKey(bytes)` became to valid check. [[#438]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- `PrivateKey(bytes)` became to valid check. [[#438]]
- `PrivateKey(byte[])` constructor became to check validity. [[#438]]


### Bug fixes

Expand Down Expand Up @@ -144,6 +145,7 @@ To be released.
[#424]: https://github.com/planetarium/libplanet/pull/424
[#426]: https://github.com/planetarium/libplanet/pull/426
[#434]: https://github.com/planetarium/libplanet/pull/434
[#438]: https://github.com/planetarium/libplanet/pull/438
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268
[floating-point determinism]: https://wp.me/p1fTCO-kT

Expand Down
12 changes: 12 additions & 0 deletions Libplanet.Tests/Crypto/PrivateKeyTest.cs
@@ -1,3 +1,4 @@
using System;
using System.Text;
using Libplanet.Crypto;
using Xunit;
Expand All @@ -20,6 +21,17 @@ public void BytesTest()
Assert.Equal(bs, key.ByteArray);
}

[Fact]
public void BytesSanityCheckTest()
{
var bs = new byte[]
{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
};
Assert.Throws<InvalidOperationException>(() => new PrivateKey(bs));
}

[Fact]
public void PublicKeyTest()
{
Expand Down
29 changes: 24 additions & 5 deletions Libplanet/Crypto/PrivateKey.cs
Expand Up @@ -69,11 +69,7 @@ public PrivateKey()
/// <seealso cref="ByteArray"/>
public PrivateKey(byte[] privateKey)
: this(
new ECPrivateKeyParameters(
"ECDSA",
new BigInteger(1, privateKey),
GetECParameters()
)
GenerateKeyFromBytes(privateKey)
)
{
}
Expand Down Expand Up @@ -269,6 +265,29 @@ private static ECPrivateKeyParameters GenerateKeyParam()
return gen.GenerateKeyPair().Private as ECPrivateKeyParameters;
}

private static ECPrivateKeyParameters GenerateKeyFromBytes(byte[] privateKey)
{
var param = new ECPrivateKeyParameters(
"ECDSA",
new BigInteger(1, privateKey),
GetECParameters()
);

var key = new PrivateKey(param);
try
{
var publicKey = key.PublicKey;
}
catch (ArgumentException)
{
throw new InvalidOperationException(
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe ArgumentException would be more suitable here.

Suggested change
throw new InvalidOperationException(
throw new ArgumentException(

"Infinity is not a valid public key for ECDH"
);
}

return param;
}

private ECPoint CalculatePoint(ECPublicKeyParameters pubKeyParams)
{
ECDomainParameters dp = keyParam.Parameters;
Expand Down