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

Add Salt and NEP2 to db3 wallet #1444

Merged
merged 9 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/neo/Wallets/SQLite/Account.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using Neo.Wallets.NEP6;

namespace Neo.Wallets.SQLite
{
internal class Account
{
public byte[] PrivateKeyEncrypted { get; set; }
public byte[] PublicKeyHash { get; set; }
public string Nep2key { get; set; }

public int ScryptN { get; set; } = ScryptParameters.Default.N;
public int ScryptR { get; set; } = ScryptParameters.Default.R;
public int ScryptP { get; set; } = ScryptParameters.Default.P;
shargon marked this conversation as resolved.
Show resolved Hide resolved
}
}
50 changes: 24 additions & 26 deletions src/neo/Wallets/SQLite/UserWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.Cryptography;
using Neo.IO;
using Neo.SmartContract;
using Neo.Wallets.NEP6;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
Expand All @@ -10,11 +11,14 @@
using System.Reflection;
using System.Security;
using System.Security.Cryptography;
using System.Text;

namespace Neo.Wallets.SQLite
{
public class UserWallet : Wallet
{
private static readonly byte[] Salt = Encoding.ASCII.GetBytes("NEO");
shargon marked this conversation as resolved.
Show resolved Hide resolved

private readonly object db_lock = new object();
private readonly string path;
private readonly byte[] iv;
Expand Down Expand Up @@ -52,24 +56,26 @@ private UserWallet(string path, byte[] passwordKey, bool create)
}
Version version = Assembly.GetExecutingAssembly().GetName().Version;
BuildDatabase();
SaveStoredData("PasswordHash", passwordKey.Sha256());
SaveStoredData("PasswordHash", passwordKey.Concat(Salt).ToArray().Sha256());
SaveStoredData("IV", iv);
SaveStoredData("MasterKey", masterKey.AesEncrypt(passwordKey, iv));
SaveStoredData("Version", new[] { version.Major, version.Minor, version.Build, version.Revision }.Select(p => BitConverter.GetBytes(p)).SelectMany(p => p).ToArray());
}
else
{
byte[] passwordHash = LoadStoredData("PasswordHash");
if (passwordHash != null && !passwordHash.SequenceEqual(passwordKey.Sha256()))
if (passwordHash != null && !passwordHash.SequenceEqual(passwordKey.Concat(Salt).ToArray().Sha256()))
throw new CryptographicException();
this.iv = LoadStoredData("IV");
this.masterKey = LoadStoredData("MasterKey").AesDecrypt(passwordKey, iv);
shargon marked this conversation as resolved.
Show resolved Hide resolved
this.accounts = LoadAccounts();
}
}

private void AddAccount(UserWalletAccount account, bool is_import)
private void AddAccount(UserWalletAccount account, ScryptParameters scrypt)
{
if (scrypt == null) scrypt = ScryptParameters.Default;

lock (accounts)
{
if (accounts.TryGetValue(account.ScriptHash, out UserWalletAccount account_old))
Expand All @@ -86,23 +92,25 @@ private void AddAccount(UserWalletAccount account, bool is_import)
{
if (account.HasKey)
{
byte[] decryptedPrivateKey = new byte[96];
Buffer.BlockCopy(account.Key.PublicKey.EncodePoint(false), 1, decryptedPrivateKey, 0, 64);
Buffer.BlockCopy(account.Key.PrivateKey, 0, decryptedPrivateKey, 64, 32);
byte[] encryptedPrivateKey = EncryptPrivateKey(decryptedPrivateKey);
Array.Clear(decryptedPrivateKey, 0, decryptedPrivateKey.Length);
string passphrase = Encoding.UTF8.GetString(masterKey);
Account db_account = ctx.Accounts.FirstOrDefault(p => p.PublicKeyHash == account.Key.PublicKeyHash.ToArray());
if (db_account == null)
{
db_account = ctx.Accounts.Add(new Account
{
PrivateKeyEncrypted = encryptedPrivateKey,
ScryptN = scrypt.N,
ScryptR = scrypt.R,
ScryptP = scrypt.P,
Nep2key = account.Key.Export(passphrase, scrypt.N, scrypt.R, scrypt.P),
PublicKeyHash = account.Key.PublicKeyHash.ToArray()
}).Entity;
}
else
{
db_account.PrivateKeyEncrypted = encryptedPrivateKey;
db_account.ScryptN = scrypt.N;
db_account.ScryptR = scrypt.R;
db_account.ScryptP = scrypt.P;
db_account.Nep2key = account.Key.Export(passphrase, scrypt.N, scrypt.R, scrypt.P);
}
}
if (account.Contract != null)
Expand Down Expand Up @@ -193,7 +201,7 @@ public override WalletAccount CreateAccount(byte[] privateKey)
Key = key,
Contract = contract
};
AddAccount(account, false);
AddAccount(account, ScryptParameters.Default);
return account;
}

Expand All @@ -213,24 +221,17 @@ public override WalletAccount CreateAccount(SmartContract.Contract contract, Key
Key = key,
Contract = verification_contract
};
AddAccount(account, false);
AddAccount(account, ScryptParameters.Default);
return account;
}

public override WalletAccount CreateAccount(UInt160 scriptHash)
{
UserWalletAccount account = new UserWalletAccount(scriptHash);
AddAccount(account, true);
AddAccount(account, ScryptParameters.Default);
return account;
}

private byte[] DecryptPrivateKey(byte[] encryptedPrivateKey)
{
if (encryptedPrivateKey == null) throw new ArgumentNullException(nameof(encryptedPrivateKey));
if (encryptedPrivateKey.Length != 96) throw new ArgumentException();
return encryptedPrivateKey.AesDecrypt(masterKey, iv);
}

public override bool DeleteAccount(UInt160 scriptHash)
{
UserWalletAccount account;
Expand Down Expand Up @@ -266,11 +267,6 @@ public override bool DeleteAccount(UInt160 scriptHash)
return false;
}

private byte[] EncryptPrivateKey(byte[] decryptedPrivateKey)
{
return decryptedPrivateKey.AesEncrypt(masterKey, iv);
}

public override WalletAccount GetAccount(UInt160 scriptHash)
{
lock (accounts)
Expand All @@ -293,13 +289,15 @@ public override IEnumerable<WalletAccount> GetAccounts()
{
using (WalletDataContext ctx = new WalletDataContext(path))
{
string passphrase = Encoding.UTF8.GetString(masterKey);
Dictionary<UInt160, UserWalletAccount> accounts = ctx.Addresses.Select(p => p.ScriptHash).AsEnumerable().Select(p => new UserWalletAccount(new UInt160(p))).ToDictionary(p => p.ScriptHash);
foreach (Contract db_contract in ctx.Contracts.Include(p => p.Account))
{
VerificationContract contract = db_contract.RawData.AsSerializable<VerificationContract>();
UserWalletAccount account = accounts[contract.ScriptHash];
account.Contract = contract;
account.Key = new KeyPair(DecryptPrivateKey(db_contract.Account.PrivateKeyEncrypted));
account.Key = new KeyPair(GetPrivateKeyFromNEP2(db_contract.Account.Nep2key, passphrase,
db_contract.Account.ScryptN, db_contract.Account.ScryptR, db_contract.Account.ScryptP));
}
return accounts;
}
Expand Down
1 change: 0 additions & 1 deletion src/neo/Wallets/SQLite/VerificationContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Neo.IO;
using Neo.SmartContract;
using Neo.VM;
using System;
using System.IO;
using System.Linq;
Expand Down
5 changes: 4 additions & 1 deletion src/neo/Wallets/SQLite/WalletDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Account>().ToTable(nameof(Account));
modelBuilder.Entity<Account>().HasKey(p => p.PublicKeyHash);
modelBuilder.Entity<Account>().Property(p => p.PrivateKeyEncrypted).HasColumnType("VarBinary").HasMaxLength(96).IsRequired();
modelBuilder.Entity<Account>().Property(p => p.Nep2key).HasColumnType("VarChar").HasMaxLength(byte.MaxValue).IsRequired();
modelBuilder.Entity<Account>().Property(p => p.ScryptN).HasColumnType("Integer").IsRequired();
modelBuilder.Entity<Account>().Property(p => p.ScryptR).HasColumnType("Integer").IsRequired();
modelBuilder.Entity<Account>().Property(p => p.ScryptP).HasColumnType("Integer").IsRequired();
modelBuilder.Entity<Account>().Property(p => p.PublicKeyHash).HasColumnType("Binary").HasMaxLength(20).IsRequired();
modelBuilder.Entity<Address>().ToTable(nameof(Address));
modelBuilder.Entity<Address>().HasKey(p => p.ScriptHash);
Expand Down
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/Wallets/SQLite/UT_Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public void TestGenerator()
}

[TestMethod]
public void TestSetAndGetPrivateKeyEncrypted()
public void TestSetAndGetNep2key()
{
Account account = new Account
{
PrivateKeyEncrypted = new byte[] { 0x01 }
Nep2key = "123"
};
Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0x01 }), Encoding.Default.GetString(account.PrivateKeyEncrypted));
Assert.AreEqual("123", account.Nep2key);
}

[TestMethod]
Expand Down