Skip to content

Conversation

@rejectedpromise
Copy link

@rejectedpromise rejectedpromise commented Nov 26, 2017

Currently, zPIV mints are stored, unencrypted in wallet. Functionality has been rerouted through CWallet -> CCryptoKeyStore to enable access to the master wallet passphrase when wallet unlocked. zPIV mints are then encrypted the same way that wallets private keys are before saving to wallet.

Currently zPIV is stored as plaintext. Created new class CZerocoinCrypter which implements the AES256CBC routine same as CCrypter, but operated over a CBigNum inplace.

This allows for CZerocoinMint to initialize and set key data for CZerocoinCrypter on construction. The cipher is derived from a pub/priv keypair that the wallet owns.
@rejectedpromise
Copy link
Author

rejectedpromise commented Nov 26, 2017

Currently hitting Segfault on -reindex. Currently working toward a solution

Copy link
Author

@rejectedpromise rejectedpromise left a comment

Choose a reason for hiding this comment

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

currently segfaulting and putting testnet DB into errorstate

@ghost ghost removed the review label Nov 26, 2017
@rejectedpromise
Copy link
Author

progress update: I forgot to check for InitialChainSync ;)

Will reopen if full sync and no error minting/spending

Copy link

@presstab presstab left a comment

Choose a reason for hiding this comment

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

Havent run it yet, looks like some cool code. Hoping to run it soon.

}

//inialize IV for 32 bytes
vector<unsigned char> chIV(crypter.KEY_SIZE);

Choose a reason for hiding this comment

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

You should follow our current naming conventions. A vector will start with v and camel case after. A vector of unsigned chars should start with vch. Descriptive names are also a good idea.

Choose a reason for hiding this comment

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

This function appears to use no member variables of CZeromint at all (plus it uses a global pointer) so I wonder why it is not a free function or part of the CWallet class. Personally I would have added this, Encrypt and Decrypt as separate functions so there would be separate plain CZeromint objects and encrypted objects. I don't see the benefit of encrypting an object if the un-encrypted member variables are still accessible. However, with separate objects you can dispose of the un-encrypted ones in favor of the encrypted (once they've been encrypted) or vice-versa. This may just be a lack of understanding of the bigger picture here but it just doesn't seem like a good separation of concerns. Hope this is clear. So essentially a separate layer or separate encrypted objects that is all done outside of this class

if(!crypter.Decrypt(serialNumber) ||
!crypter.Decrypt(randomness)) {

LogPrintf("%s : failed to encrypt zerocoin mint\n", __func__);

Choose a reason for hiding this comment

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

typo in log print

for (CZerocoinMint mint : vMintsToUpdate) {

if(pwalletMain->IsCrypted() && !mint.Encrypt()){
LogPrintf("Error: encryption of mint failed");

Choose a reason for hiding this comment

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

It would be beneficial to add context to these logs so that if there is a failure to mint we know where exactly in the code it is happening. Also you need to terminate the string with \n.

denomination = libzerocoin::IntToZerocoinDenomination(params[1].get_int());
list<CZerocoinMint> listMints = walletdb.ListMintedCoins(!fIncludeSpent, false, false);

for(CZerocoinMint mint : listMints) {

Choose a reason for hiding this comment

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

Seems like bad form to have two identical for loops right next to each other. Any particular reason not to decrypt the mint within the loop a few lines below?

Copy link
Author

Choose a reason for hiding this comment

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

The loop below was const objects. Wasn't sure if there is reasoning around when to treat the objects as constants and when to treat them as mutable. Is there any particular reason in the loop below it is marked const?

Choose a reason for hiding this comment

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

It's marked const since there is no need to change the object itself. Thus code is clearer as to what is going on. Now you are decrypting, so you could either remove the 'const' restriction or have Decrypt make new object. BTW, shouldn't the error message be "Decryption of mint failed?"

src/walletdb.cpp Outdated
CZerocoinMint mint;
ssValue >> mint;

if(pwalletMain->IsCrypted() && !mint.Decrypt())

Choose a reason for hiding this comment

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

Why do we want to decrypt the coins everytime we get the list of mints?

Copy link
Author

Choose a reason for hiding this comment

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

does pwalletMain->IsCrypted() returning false not terminate the statement?

@@ -0,0 +1,79 @@
//
// Created by rejectedpromise on 11/25/17.

Choose a reason for hiding this comment

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

Replace this with the PIVX Devlopers copyright heading.

@@ -0,0 +1,61 @@
//
// Created by rejectedpromise on 11/25/17.

Choose a reason for hiding this comment

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

replace

@rejectedpromise rejectedpromise changed the title [Security] zPIV Encryption [WIP] zPIV Encryption Nov 26, 2017
@ghost ghost added the review label Nov 26, 2017
@rejectedpromise
Copy link
Author

rejectedpromise commented Nov 26, 2017

Reopen. Still may have problems with DB on -reindex causing runaway exception. BE WARNED.

@rejectedpromise
Copy link
Author

May also be problems in logic for syncing after encryptions have happened

@Fuzzbawls
Copy link
Collaborator

Just off the top of my head, I think this should bump the wallet version no?
Does this "play nice" with existing wallets or does it only apply to NEW wallets? (ie, are existing wallets "converted" to a full encryption state or does the encryption of the zPIV space only apply to newly encrypted wallets?)

@Fuzzbawls Fuzzbawls added this to the 3.1.0 milestone Nov 30, 2017
Move cryption functionality into crypter, and crypting same time as is currently implemented in wallet.

Discarding plain mints and returning ciphered copies.

Crashing on open of wallet.
CMakeLists.txt
cmake-build-debug
db4/
db-4*
Copy link
Author

Choose a reason for hiding this comment

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

revert

@PIVX-Project
Copy link
Collaborator

Where are we on this??

failure on GetCursor() from listzerocoinmints called on unlock
@rejectedpromise
Copy link
Author

Current status of ezPIV:

  • CCrypter methods for EncryptZerocoinMint and DecryptZerocoinMint
  • wallet->EncryptWallet encrypts zerocoinmints same way as it encrypts keys
  • wallet->Unlock decrypts zerocoinmints same way as it encrypts keys

Still debugging issues with reading and writing to disk
Seems to be having trouble with walletdb->ListZerocoinMints(true, false, false)
rooting from the GetCursor() call

… to vMasterKey for encryption

Log statements in new methods for debugging

no guarantees on no segfaults during zerocoin minting on this commit
@presstab
Copy link

What is the current status of this. Still waiting for this so that I can finish deterministic zpiv.

…rocoin_encryption_tests.cpp

Haven't tested live on testnet yet, but think it should be working properly through wallet::CCryptoKeyStore

Crypto actions should happen automatically, as with the wallet priv keys, and while unlocked (walletpassphrase kept in memory) decrypts crypted mints that are stored in a private map in CCryptoKeyStore

Hoping to get some good testnet runs tomorrow
src/crypter.cpp Outdated
return false;

// vector<unsigned char> x(vchPlaintext.begin(), vchPlaintext.end());
// CBigNum t(x);
Copy link
Author

Choose a reason for hiding this comment

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

revert

src/crypter.cpp Outdated

vchCiphertext.resize(nCLen + nFLen);

//CBigNum y(vchCiphertext);
Copy link
Author

Choose a reason for hiding this comment

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

revert

vector<unsigned char> key(vMasterKey.begin(), vMasterKey.end());
vector<unsigned char> plain(vchPlaintext.begin(), vchPlaintext.end());


Copy link
Author

Choose a reason for hiding this comment

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

remove spaces

src/crypter.cpp Outdated
// if (!fKeySet)
// return false;
//
// //already encrypted
Copy link
Author

Choose a reason for hiding this comment

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

delete

src/crypter.cpp Outdated

fUseCrypto = true;
BOOST_FOREACH (KeyMap::value_type& mKey, mapKeys) {

Copy link
Author

Choose a reason for hiding this comment

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

remove space


public:
enum CryptionMethod {
ENC,
Copy link
Author

Choose a reason for hiding this comment

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

revert

enum ZerocoinSecrets {
SERIAL,
RANDOM
};
Copy link
Author

Choose a reason for hiding this comment

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

revert

mint.SetHeight(nHeight);

// if(pwalletMain->IsCrypted() && !mint.Encrypt()){
// LogPrintf("Error: encryption of mint failed");
Copy link
Author

Choose a reason for hiding this comment

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

revert

@rejectedpromise
Copy link
Author

@presstab
see recent commit 25df0827647f92d98317e45e15cbab80640d6ec6

hoping to get it up on testnet tomorrow to do some live runs

@rejectedpromise
Copy link
Author

Status update:
GUI has been tested fairly well in minting and spending across a couple different wallets on testnet.
While testing RPC calls (specifically exportzerocoins) I have run into a couple of bugs that I am hoping to resolve soon before sending to QA.

@Fuzzbawls I do not think this would require a version bump, as it should be backwards compatible, as it hooks up to the current encryption scheme and uses the walletpassphrase same as the private keys, therefore not requiring bump AFAIK

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

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

A few changes needed from what I can see at a glance, otherwise great progress!

#include <boost/assign/list_of.hpp>

#include <univalue.h>
//#include <univalue.h>
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm, think your IDE was giving you some unique errors if was needed.

#include <univalue.h> should be enough.

#include "../sync.h"
#include "../main.h"
#include "../crypter.h"
#include "../wallet.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

not digging the relative includes here

Copy link
Author

Choose a reason for hiding this comment

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

Ahhh, nice catch. I have just recently configured CMakeLists.txt in CLion to work off of absolute paths. Forgot to actually crawl through and change all the relatives 👍

ss << zerocoinMint.GetValue();
uint256 hash = Hash(ss.begin(), ss.end());

LogPrintf("pub: %s\nserial: %s\nrand: %s\ncrypted: %b\n", zerocoinMint.GetValue().ToString(16), zerocoinMint
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggest making this a LogPrint("zero", ...); instead of a LogPrintf() to reduce log spam.

@rejectedpromise
Copy link
Author

Status update

Failing on block indexing since trying to read serials from walletDB, checking for spent/owned/etc., while they are still encrypted (main.h so not yet unlocked).

Initial approach

Keep a list of serial hashes in pwallet to enable checking if serial is owned by this wallet, without having to expose private data.

@Fuzzbawls Fuzzbawls added the Needs Release Notes Placeholder tag for anything needing mention in the "Notable Changes" section of release notes label Feb 24, 2018
@rejectedpromise
Copy link
Author

pulled into 3.1.0

@ghost ghost removed the in progress label Apr 22, 2018
@rejectedpromise rejectedpromise deleted the zPIVEncryption branch June 9, 2018 22:20
@Fuzzbawls Fuzzbawls removed the Needs Release Notes Placeholder tag for anything needing mention in the "Notable Changes" section of release notes label Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants