PIVX Core  5.6.99
P2P Digital Currency
bls_ies.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "bls/bls_ies.h"
6 
7 #include "crypto/aes.h"
8 #include "hash.h"
9 #include "random.h"
10 #include "streams.h"
11 
12 template <typename Out>
13 static bool EncryptBlob(const void* in, size_t inSize, Out& out, const void* symKey, const void* iv)
14 {
15  out.resize(inSize);
16 
17  AES256CBCEncrypt enc((const unsigned char*)symKey, (const unsigned char*)iv, false);
18  int w = enc.Encrypt((const unsigned char*)in, (int)inSize, (unsigned char*)out.data());
19  return w == (int)inSize;
20 }
21 
22 template <typename Out>
23 static bool DecryptBlob(const void* in, size_t inSize, Out& out, const void* symKey, const void* iv)
24 {
25  out.resize(inSize);
26 
27  AES256CBCDecrypt enc((const unsigned char*)symKey, (const unsigned char*)iv, false);
28  int w = enc.Decrypt((const unsigned char*)in, (int)inSize, (unsigned char*)out.data());
29  return w == (int)inSize;
30 }
31 
32 bool CBLSIESEncryptedBlob::Encrypt(const CBLSPublicKey& peerPubKey, const void* plainTextData, size_t dataSize)
33 {
34  CBLSSecretKey ephemeralSecretKey;
35  ephemeralSecretKey.MakeNewKey();
36  ephemeralPubKey = ephemeralSecretKey.GetPublicKey();
37  GetStrongRandBytes(iv, sizeof(iv));
38 
39  CBLSPublicKey pk;
40  if (!pk.DHKeyExchange(ephemeralSecretKey, peerPubKey)) {
41  return false;
42  }
43 
44  std::vector<unsigned char> symKey = pk.ToByteVector();
45  symKey.resize(32);
46 
47  return EncryptBlob(plainTextData, dataSize, data, symKey.data(), iv);
48 }
49 
50 bool CBLSIESEncryptedBlob::Decrypt(const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const
51 {
52  CBLSPublicKey pk;
53  if (!pk.DHKeyExchange(secretKey, ephemeralPubKey)) {
54  return false;
55  }
56 
57  std::vector<unsigned char> symKey = pk.ToByteVector();
58  symKey.resize(32);
59 
60  return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv);
61 }
62 
63 
64 bool CBLSIESMultiRecipientBlobs::Encrypt(const std::vector<CBLSPublicKey>& recipients, const BlobVector& _blobs)
65 {
66  if (recipients.size() != _blobs.size()) {
67  return false;
68  }
69 
70  InitEncrypt(_blobs.size());
71 
72  for (size_t i = 0; i < _blobs.size(); i++) {
73  if (!Encrypt(i, recipients[i], _blobs[i])) {
74  return false;
75  }
76  }
77 
78  return true;
79 }
80 
82 {
86 
87  ivVector.resize(count);
88  blobs.resize(count);
89  for (size_t i = 0; i < count; i++) {
90  ivVector[i] = (i == 0 ? ivSeed : ::SerializeHash(ivVector[i-1]));
91  }
92 }
93 
94 bool CBLSIESMultiRecipientBlobs::Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob)
95 {
96  assert(idx < blobs.size());
97 
98  CBLSPublicKey pk;
99  if (!pk.DHKeyExchange(ephemeralSecretKey, recipient)) {
100  return false;
101  }
102 
103  std::vector<unsigned char> symKey = pk.ToByteVector();
104  symKey.resize(32);
105 
106  return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin());
107 }
108 
109 bool CBLSIESMultiRecipientBlobs::Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const
110 {
111  if (idx >= blobs.size()) {
112  return false;
113  }
114 
115  CBLSPublicKey pk;
116  if (!pk.DHKeyExchange(sk, ephemeralPubKey)) {
117  return false;
118  }
119 
120  std::vector<unsigned char> symKey = pk.ToByteVector();
121  symKey.resize(32);
122 
123  uint256 iv = ivSeed;
124  for (size_t i = 0; i < idx; i++) {
125  iv = ::SerializeHash(iv);
126  }
127 
128  return DecryptBlob(blobs[idx].data(), blobs[idx].size(), blobRet, symKey.data(), iv.begin());
129 }
std::vector< unsigned char > data
Definition: bls_ies.h:19
CBLSPublicKey ephemeralPubKey
Definition: bls_ies.h:17
bool Decrypt(const CBLSSecretKey &secretKey, CDataStream &decryptedDataRet) const
Definition: bls_ies.cpp:50
bool Encrypt(const CBLSPublicKey &peerPubKey, const void *data, size_t dataSize)
Definition: bls_ies.cpp:32
unsigned char iv[16]
Definition: bls_ies.h:18
void InitEncrypt(size_t count)
Definition: bls_ies.cpp:81
CBLSSecretKey ephemeralSecretKey
Definition: bls_ies.h:87
bool Encrypt(const std::vector< CBLSPublicKey > &recipients, const BlobVector &_blobs)
Definition: bls_ies.cpp:64
CBLSPublicKey ephemeralPubKey
Definition: bls_ies.h:82
bool Decrypt(size_t idx, const CBLSSecretKey &sk, Blob &blobRet) const
Definition: bls_ies.cpp:109
std::vector< uint256 > ivVector
Definition: bls_ies.h:88
std::vector< Blob > BlobVector
Definition: bls_ies.h:79
std::vector< unsigned char > Blob
Definition: bls_ies.h:78
bool DHKeyExchange(const CBLSSecretKey &sk, const CBLSPublicKey &pk)
void MakeNewKey()
Definition: bls_wrapper.cpp:54
CBLSPublicKey GetPublicKey() const
Definition: bls_wrapper.cpp:99
std::vector< uint8_t > ToByteVector() const
Definition: bls_wrapper.h:116
value_type * data()
Definition: streams.h:178
unsigned int size() const
Definition: uint256.h:83
unsigned char * begin()
Definition: uint256.h:63
256-bit opaque blob.
Definition: uint256.h:138
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:289
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
Definition: random.cpp:580