Skip to content

Commit

Permalink
fix: issue #1696 (#2091)
Browse files Browse the repository at this point in the history
* fix: issue #1696

Modify GenerateKeyParam to return a new value
when .D.ToByteArrayUnsigned().Length != 32 Repeat until == 32

* Add change log

* Update PrivateKeyTest.cs

remove unused test

Co-authored-by: Ohjin <lazist134407@gmail.com>
Co-authored-by: Swen Mun <swen@planetariumhq.com>
Co-authored-by: Chanhyuck Ko <limeelbee@gmail.com>
  • Loading branch information
4 people committed Jul 5, 2022
1 parent 7d7ac90 commit d0de3aa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -25,6 +25,8 @@ To be released.

### Bug fixes

- Fixed a bug where `PrivateKey()` constructor had returned an invalid key
less than 32 bytes. [[#1696], [#2091]]
- (Libplanet.Net) Invalid `Uri.UserInfo` with multiple colons is now
rejected by `IceServer(Uri url)` constructor and exception is thrown.
[[#2116]]
Expand All @@ -40,6 +42,8 @@ To be released.
- (Libplanet.Extensions.Cocona) Removed `DerivationCommand` class.
[[#2118]]

[#1696]: https://github.com/planetarium/libplanet/issues/1696
[#2091]: https://github.com/planetarium/libplanet/pull/2091
[#2101]: https://github.com/planetarium/libplanet/pull/2101
[#2108]: https://github.com/planetarium/libplanet/pull/2108
[#2118]: https://github.com/planetarium/libplanet/pull/2118
Expand Down
19 changes: 19 additions & 0 deletions Libplanet.Tests/Crypto/PrivateKeyTest.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -325,5 +326,23 @@ public void HexStringConstructor()
actual.ToByteArray()
);
}

[Fact]

public void PrivateKeyGenerateLongerThan31Bytes()
{
var faults = new List<int>();
for (int i = 0; i < 3000; i++)
{
var pk = new PrivateKey();

if (pk.ByteArray.Length < 32)
{
faults.Add(i);
}
}

Assert.Empty(faults);
}
}
}
30 changes: 30 additions & 0 deletions Libplanet/Crypto/GenerateKeyParamTriesExceedException.cs
@@ -0,0 +1,30 @@
using System;
using System.Runtime.Serialization;

namespace Libplanet.Crypto
{
[Serializable]
public class GenerateKeyParamTriesExceedException : Exception
{
public GenerateKeyParamTriesExceedException()
{
}

public GenerateKeyParamTriesExceedException(string message)
: base(message)
{
}

public GenerateKeyParamTriesExceedException(string message, Exception innerException)
: base(message, innerException)
{
}

protected GenerateKeyParamTriesExceedException(
SerializationInfo info, StreamingContext context
)
: base(info, context)
{
}
}
}
20 changes: 19 additions & 1 deletion Libplanet/Crypto/PrivateKey.cs
Expand Up @@ -375,7 +375,25 @@ private static ECPrivateKeyParameters GenerateKeyParam()
new ECKeyGenerationParameters(ecParams, secureRandom);
gen.Init(keyGenParam);

return (ECPrivateKeyParameters)gen.GenerateKeyPair().Private;
const int maxTries = 3000;
int tries = 0;
ECPrivateKeyParameters result;

while (tries < maxTries)
{
result = (ECPrivateKeyParameters)gen.GenerateKeyPair().Private;
if (result.D.ToByteArrayUnsigned().Length == KeyByteSize)
{
return result;
}

tries++;
}

throw new GenerateKeyParamTriesExceedException(
"Can't find appropriate parameter for private key" +
$"(maxTries: {maxTries})"
);
}

private static ECPrivateKeyParameters GenerateKeyFromBytes(byte[] privateKey)
Expand Down

0 comments on commit d0de3aa

Please sign in to comment.