Fabric - Data encryption - hyperledger-fabric

Is it possible to encrypt the data directly inside the chaincode?
What I'm trying to do is to hide data between participants without using the channels.
For example:
The network have three participants A,B,C
The chaincode holds the public key of B which is used to encrypt the data.
A and C send data to the chaincode which store the data encrypted with the public key of B.
B with his private key can decrypt the data retrieved.
Is this way a secure way to encrypt the data? Could work?
Thanks.

There is a Fabric example for symmetric key encryption, see the doc and an example.
A similar mechanism could also be used for asymmetric encryption as you propose. However you may want to encrypt the data on the client side, and then pass the encrypted data into the chaincode.
Alternatively, you could use the private data feature to pass the private data to peers of authorized organizations. The other organizations would only receive a hash of the private data in the block transaction. For more details see the private data documentation.

Related

Unable to understand PKI concept in Hyperledger Fabric?

I am just starting Hyperledger Fabric. I was reading about PKI concept in Hyperledger fabric.I am reading form this link
Below is the diagram they explain Marry uses her private key to sign the document. But in general its is explain in this link
that public key is used for encryption.
So which key is used for encryption ?
There are various aspects to Asymmetric Cryptography. A confidential message can be encrypted and disseminated over the network in various ways. Let's assume there are two participants in the network - A and B, each of them have registered themselves with a certificate authority and have obtained their crypto pair - a public key and a private key. Now, if A wants to send a message to B, he has two options
encrypt the message M using his private key and send. Now, if B has
access to A's public key over the network, he will be able to
decrypt the message. Now, assuming A's public key is generally
available over the consortium network, people other than B also will
have access to A's public key. Now, does this compromise the message
integrity? Well, no. This is because the encryption is a function of
the private key as well as the message, so people having the right
access can decrypt and see the message but will not be able to
tamper it unless A's private key is compromised.
Another way in which A can send message to B, is by encrypting it
with B's public key. Now, this message can only be decrypted only by
B and whoever B chooses to share his private key with in a secure
manner. Here, again the encryption will be a function of B's public
key and the message - that said the integrity of message can be
assured.
Hyperledger Fabric follows the first method - the messages are encrypted by sender's private key and recipient's access to the sender's public key or to the message per say is managed by by consortium/organization's membership, access control rules and channel policies.

Hyperledger Fabric Data Confidentiality

I did not find a good tutorial or article answering this question so far, maybe you can help. What I want to do is the following:
Peer A in Org 1 calls chaincode, this chaincode has access to data only available in Org 2 and Peer A never gets access to the full information.
I know that Hyperledger Fabric supports private channels and private data, is it possible to achieve this requirement? If yes can you point me in the right direction to do this?
Yes, it is possible. below are the steps to accomplish.
By using private data, you can accomplish your requirements, but little work needed. you an also follow the fabric documentation, i have provided the links here.
The client application submits a proposal request to invoke a chaincode function (reading or writing private data) to endorsing peers which are part of authorized organizations of the collection. The private data, or data used to generate private data in chaincode, is sent in a transient field of the proposal.
The endorsing peers simulate the transaction and store the private data in a transient data store (a temporary storage local to the peer). They distribute the private data, based on the collection policy, to authorized peers via gossip.
The endorsing peer sends the proposal response back to the client with public data, including a hash of the private data key and value. No private data is sent back to the client.
Check this for endorsement: https://hyperledger-fabric.readthedocs.io/en/release-1.4/private-data-arch.html#endorsement
The client application submits the transaction to the ordering service (with hashes of the private data) which gets distributed into blocks as normal. The block with the hashed values is distributed to all the peers. In this way, all peers on the channel can validate transactions with the hashes of the private data in a consistent way, without knowing the actual private data(This is what you need to accomplish).
At block-committal time, authorized peers use the collection policy to determine if they are authorized to have access to the private data(For reading the block data). If they do, they will first check their local transient data store to determine if they have already received the private data at chaincode endorsement time. If not, they will attempt to pull the private data from another peer. Then they will validate the private data against the hashes in the public block and commit the transaction and the block. Upon validation/commit, the private data is moved to their copy of the private state database and private writeset storage. The private data is then deleted from the transient data store.
Resources: https://hyperledger-fabric.readthedocs.io/en/release-1.4/private-data/private-data.html

Hyperledger Fabric - Copy private data from one's collection to another organization's collection

Is there any way to copy private data from one collection to another? For e.g. consider a case when you have private data and you are selling it to someone, so it goes into their collection.
The documentation says:
"Collection members may decide to share the private data with other > parties if they get into a dispute or if they want to transfer the asset to a third party. The third party can then compute the hash of the private data and see if it matches the state on the channel ledger, proving that the state existed between the collection members at a certain point in time."
However, I can't seem to find an API to do it.
I think the answer is in this thread link.
The private data collection can be "copied" by updating the collection policy on the chaincode. Which then allow the previously unauthorized member to access it.
After chaincode updating, the private data from your peer can now be distributed to the buyer peer via gossip protocol. Since private data are stored in the private statedb of peers rather than in the blockchain. Only the hash of that data is stored in the blockchain. Then, your buyer need to "compute the hash of the private data and see if it matches the state on the channel ledger" to make sure that you sold him a valid private data.
I hope that I understand it correctly. More detail could be found here link

When creating a block through a transaction, is it proper to write the device's private key as a signature on the block?

I want to create a block-chain environment for devices.
When creating a block through a transaction, is it proper to write the device's private key as a signature on the block?
Never (ever) reveal the private key for anything - person, phone or device. The public key is what is shared with other parties.
WRT blockchain and especially Hyperledger Fabric, transactions submitted are signed with the private key and the signature is actually around a transaction containing the public key.
That's a huge security flaw and defeats one of the key benefits of BC. Look at your private key as your private password to your bank account, laptop, phone among others. You definitely don't want anyone know about it.
Short answer, No!
Happy coding.

In Fabric, how the key used for data encryption is supposed to be shared?

With FAB-830 implemented in Fabric v1.1, it is now possible for the chaincode to encrypt the data stored in the state.
The idea is: the symmetric encryption key is passed as a transient parameter and is therefore only known to the endorsers.
This allows to run business logic on clear data and upsert encrypted data (such on-chain encryption is impossible in Ethereum and most other blockchains AFAIK).
The part I still miss is: how the organizations which are supposed to share the secret get to know the symmetric key?
Also, even if the endorser get the transient key from the sender as part of the transaction proposal, is there any out-of-the-box way to store it?
Many thanks in advance.
The peers/endorsers should not actually store the encryption key. The idea is that the encryption key(s) are managed at the application level and only passed to the peers as required (generally when executing contract logic and not necessarily needed for querying data as the decryption can be done in the application rather than on the peer(s)).

Resources