Skip to content

Commit

Permalink
ProtectedPrivateKey.FromJson() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Oct 30, 2019
1 parent 1626be8 commit 6d8a13d
Show file tree
Hide file tree
Showing 11 changed files with 1,252 additions and 36 deletions.
101 changes: 101 additions & 0 deletions Libplanet.Tests/KeyStore/Ciphers/Aes128CtrTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Immutable;
using System.Text.Json;
using Libplanet.KeyStore;
using Libplanet.KeyStore.Ciphers;
using Libplanet.KeyStore.Kdfs;
using Org.BouncyCastle.Crypto.Digests;
using Xunit;

namespace Libplanet.Tests.KeyStore.Ciphers
{
public class Aes128CtrTest : CipherTest<Aes128Ctr>
{
private Aes128Ctr _cipher;

public Aes128CtrTest()
{
var random = new Random();
var buffer = new byte[16];
random.NextBytes(buffer);
_cipher = new Aes128Ctr(buffer.ToImmutableArray());
}

public override Aes128Ctr Cipher => _cipher;

[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<ArgumentException>(() =>
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
}")
);
}
}
}
36 changes: 0 additions & 36 deletions Libplanet.Tests/KeyStore/Ciphers/AesCtrTest.cs

This file was deleted.

185 changes: 185 additions & 0 deletions Libplanet.Tests/KeyStore/Kdfs/Pbkdf2Test.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.Immutable;
using System.Text.Json;
using Libplanet.KeyStore;
using Libplanet.KeyStore.Kdfs;
using Org.BouncyCastle.Crypto.Digests;
using Xunit;

namespace Libplanet.Tests.KeyStore.Kdfs
{
Expand All @@ -10,5 +13,187 @@ public override Pbkdf2<Sha256Digest> MakeInstance(byte[] randomBytes)
{
return new Pbkdf2<Sha256Digest>(10, randomBytes, randomBytes.Length);
}

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

Pbkdf2<Sha256Digest> Load(string json)
{
using (JsonDocument doc = JsonDocument.Parse(json, options))
{
return (Pbkdf2<Sha256Digest>)Pbkdf2.FromJson(doc.RootElement);
}
}

var kdf = Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
");
Assert.Equal(10240, kdf.Iterations);
Assert.Equal(32, kdf.KeyLength);
TestUtils.AssertBytesEqual(
new byte[]
{
0x3e, 0xea, 0xaf, 0x35, 0xda, 0x70, 0x92, 0x83, 0x87, 0xca, 0xe1,
0xea, 0xd3, 0x1e, 0xd7, 0x82, 0xb1, 0x13, 0x5d, 0x75, 0x78, 0xa8,
0x9d, 0x95, 0xe3, 0x0c, 0xc9, 0x14, 0x01, 0x0b, 0xa2, 0xed,
}.ToImmutableArray(),
kdf.Salt
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
// ""c"": 10240, // lacks
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": true, // not a number
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": null, // not a number, but null
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
// ""dklen"": 32, // lacks
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": false, // not a number
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": null, // not a number, but null
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
// ""prf"": ""hmac-sha256"", // lacks
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": 123, // not a string, but a number
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<UnsupportedKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": ""hmac-sha512"", // unsupported prf
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2ed"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": ""hmac-sha256"",
// ""salt"": ""..."", // lacks
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": 1234, // not a string, but a number
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": ""not a hexadecimal string"",
}
")
);

Assert.Throws<InvalidKeyJsonException>(() =>
Load(@"
{
""c"": 10240,
""dklen"": 32,
""prf"": ""hmac-sha256"",
""salt"": ""3eeaaf35da70928387cae1ead31ed782b1135d7578a89d95e30cc914010ba2e"",
// salt: invalid length
}
")
);
}
}
}
Loading

0 comments on commit 6d8a13d

Please sign in to comment.