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

Protected private key & Web3 Secret Storage Definition #614

Merged
merged 13 commits into from
Oct 31, 2019
13 changes: 13 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ To be released.

### Added interfaces

- Added `ProtectedPrivateKey` class. [[#577], [#614]]
- Added `IncorrectPassphraseException` class. [[#577], [#614]]
- Added `MismatchedAddressException` class. [[#577], [#614]]
- Added `KeyJsonException` abstract class. [[#577], [#614]]
- Added `InvalidKeyJsonException` class. [[#577], [#614]]
- Added `UnsupportedKeyJsonException` class. [[#577], [#614]]
- Added `ICipher` interface. [[#577], [#614]]
- Added `Aes128Ctr` class. [[#577], [#614]]
- Added `IKdf` interface. [[#577], [#614]]
- Added `Pbkdf2` class. [[#577], [#614]]
- Added `BlockChain<T>.LongCount()` method. [[#575]]
- Added `BlockChain<T>[HashDigest<T>]` indexer. [[#409], [#583]]
- Added `BlockChain<T>.Contains(HashDigest<T>)` method. [[#409], [#583]]
- Added `BlockChain<T>.GetTransaction(TxId)` method. [[#409], [#583]]
- Added `BlockChain<T>.Contains(TxId)` method. [[#409], [#583]]
- Added `ByteUtil.Hex(ImmutableArray<byte>)` overloaded method. [[#614]]

### Behavioral changes

Expand Down Expand Up @@ -112,6 +123,7 @@ To be released.
[#568]: https://github.com/planetarium/libplanet/issues/568
[#575]: https://github.com/planetarium/libplanet/pull/575
[#576]: https://github.com/planetarium/libplanet/pull/576
[#577]: https://github.com/planetarium/libplanet/issues/577
[#579]: https://github.com/planetarium/libplanet/pull/579
[#581]: https://github.com/planetarium/libplanet/pull/581
[#583]: https://github.com/planetarium/libplanet/pull/583
Expand All @@ -124,6 +136,7 @@ To be released.
[#608]: https://github.com/planetarium/libplanet/pull/608
[#609]: https://github.com/planetarium/libplanet/pull/609
[#610]: https://github.com/planetarium/libplanet/pull/610
[#614]: https://github.com/planetarium/libplanet/pull/614
[#622]: https://github.com/planetarium/libplanet/pull/622


Expand Down
9 changes: 5 additions & 4 deletions Libplanet.Tests/ByteUtilTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Immutable;
using Xunit;

namespace Libplanet.Tests
Expand All @@ -8,14 +9,14 @@ public class ByteUtilTest
[Fact]
public void HexTest()
{
var bs = new byte[20]
var bs = new byte[]
{
0x45, 0xa2, 0x21, 0x87, 0xe2, 0xd8, 0x85, 0x0b, 0xb3, 0x57,
0x88, 0x69, 0x58, 0xbc, 0x3e, 0x85, 0x60, 0x92, 0x9c, 0xcc,
};
Assert.Equal(
"45a22187e2d8850bb357886958bc3e8560929ccc",
ByteUtil.Hex(bs));
const string expectedHex = "45a22187e2d8850bb357886958bc3e8560929ccc";
Assert.Equal(expectedHex, ByteUtil.Hex(bs));
Assert.Equal(expectedHex, ByteUtil.Hex(ImmutableArray.Create(bs)));
}

[Fact]
Expand Down
97 changes: 97 additions & 0 deletions Libplanet.Tests/KeyStore/Ciphers/Aes128CtrTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Immutable;
using System.Text.Json;
using Libplanet.KeyStore;
using Libplanet.KeyStore.Ciphers;
using Xunit;

namespace Libplanet.Tests.KeyStore.Ciphers
{
public class Aes128CtrTest : CipherTest<Aes128Ctr>
{
public Aes128CtrTest()
{
var random = new Random();
var buffer = new byte[16];
random.NextBytes(buffer);
Cipher = new Aes128Ctr(buffer.ToImmutableArray());
}

public override Aes128Ctr Cipher { get; }

[Fact]
public void Constructor()
{
Assert.Throws<ArgumentException>(() =>
new Aes128Ctr(new byte[0].ToImmutableArray())
);
var random = new Random();
var buffer = new byte[17];
random.NextBytes(buffer);
Assert.Throws<ArgumentOutOfRangeException>(() =>
new Aes128Ctr(buffer.ToImmutableArray())
);
}

[Fact]
public void FromJson()
{
var options = new JsonDocumentOptions
{
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip,
};

Aes128Ctr Load(string json)
{
using (JsonDocument doc = JsonDocument.Parse(json, options))
{
return (Aes128Ctr)Aes128Ctr.FromJson(doc.RootElement);
}
}

Aes128Ctr cipher = Load(@"{
""iv"": ""bc7f2ca23bfee0dd9725228ab2b0d98a"",
}");
TestUtils.AssertBytesEqual(
new byte[]
{
0xbc, 0x7f, 0x2c, 0xa2, 0x3b, 0xfe, 0xe0, 0xdd,
0x97, 0x25, 0x22, 0x8a, 0xb2, 0xb0, 0xd9, 0x8a,
}.ToImmutableArray(),
cipher.Iv
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"{
// ""iv"": ""..."", // lacks
}")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"{
""iv"": true, // not a string
}")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"{
""iv"": null, // not a string, but null
}")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"{
""iv"": ""not a hexadecimal string"",
}")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"{
""iv"": ""bc7f2ca23bfee0dd9725228ab2b0d98"",
// iv: invalid length
}")
);
}
}
}
28 changes: 28 additions & 0 deletions Libplanet.Tests/KeyStore/Ciphers/CipherTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Immutable;
using Libplanet.KeyStore.Ciphers;
using Xunit;
using Random = System.Random;

namespace Libplanet.Tests.KeyStore.Ciphers
{
public abstract class CipherTest<T>
where T : ICipher
{
public abstract T Cipher { get; }

[Fact]
public void EncryptDecrypt()
{
var random = new Random();
var buffer = new byte[4096];
random.NextBytes(buffer);
ImmutableArray<byte> key = ImmutableArray.Create(buffer, 0, 16);
random.NextBytes(buffer);
ImmutableArray<byte> value = buffer.ToImmutableArray();
T c = Cipher;
ImmutableArray<byte> encrypted = c.Encrypt(key, value);
ImmutableArray<byte> decrypted = c.Decrypt(key, encrypted);
TestUtils.AssertBytesEqual(value, decrypted);
}
}
}
31 changes: 31 additions & 0 deletions Libplanet.Tests/KeyStore/IncorrectPassphraseExceptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Immutable;
using Libplanet.KeyStore;
using Xunit;
using static Libplanet.Tests.TestUtils;

namespace Libplanet.Tests.KeyStore
{
public class IncorrectPassphraseExceptionTest
{
[Fact]
public void Constructor()
{
ImmutableArray<byte>
expectedMac = new byte[] { 0x00, 0x01, 0x02, 0x03 }.ToImmutableArray(),
inputMac = new byte[] { 0x04, 0x05, 0x06, 0x07 }.ToImmutableArray();
var e = new IncorrectPassphraseException(
"Some message.",
"paramName",
expectedMac,
inputMac
);
Assert.StartsWith(
"Some message.\nExpected MAC: 00010203\nInput MAC: 04050607",
e.Message
);
Assert.Equal("paramName", e.ParamName);
AssertBytesEqual(expectedMac, e.ExpectedMac);
AssertBytesEqual(inputMac, e.InputMac);
}
}
}
32 changes: 32 additions & 0 deletions Libplanet.Tests/KeyStore/Kdfs/KdfTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Immutable;
using System.Security.Cryptography;
using Libplanet.KeyStore.Kdfs;
using Xunit;

namespace Libplanet.Tests.KeyStore.Kdfs
{
public abstract class KdfTest<T>
where T : IKdf
{
public abstract T MakeInstance(byte[] randomBytes);

[InlineData(16)]
[InlineData(32)]
[Theory]
public void Derive(int size)
{
var randomBytes = new byte[size];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(randomBytes);
}

T kdf = MakeInstance(randomBytes);
ImmutableArray<byte> dFoo = kdf.Derive("foo");
Assert.Equal(size, dFoo.Length);
ImmutableArray<byte> dBar = kdf.Derive("bar");
Assert.NotEqual(dFoo, dBar);
TestUtils.AssertBytesEqual(dFoo, kdf.Derive("foo"));
}
}
}
Loading