Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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 <hash.h>
8 #include <random.h>
9 #include <streams.h>
10 
11 #include <crypto/aes.h>
12 
13 template <typename Out>
14 static bool EncryptBlob(const void* in, size_t inSize, Out& out, const void* symKey, const void* iv)
15 {
16  out.resize(inSize);
17 
18  AES256CBCEncrypt enc((const unsigned char*)symKey, (const unsigned char*)iv, false);
19  int w = enc.Encrypt((const unsigned char*)in, (int)inSize, (unsigned char*)out.data());
20  return w == (int)inSize;
21 }
22 
23 template <typename Out>
24 static bool DecryptBlob(const void* in, size_t inSize, Out& out, const void* symKey, const void* iv)
25 {
26  out.resize(inSize);
27 
28  AES256CBCDecrypt enc((const unsigned char*)symKey, (const unsigned char*)iv, false);
29  int w = enc.Decrypt((const unsigned char*)in, (int)inSize, (unsigned char*)out.data());
30  return w == (int)inSize;
31 }
32 
33 bool CBLSIESEncryptedBlob::Encrypt(const CBLSPublicKey& peerPubKey, const void* plainTextData, size_t dataSize)
34 {
35  CBLSSecretKey ephemeralSecretKey;
36  ephemeralSecretKey.MakeNewKey();
37  ephemeralPubKey = ephemeralSecretKey.GetPublicKey();
38  GetStrongRandBytes(iv, sizeof(iv));
39 
40  CBLSPublicKey pk;
41  if (!pk.DHKeyExchange(ephemeralSecretKey, peerPubKey)) {
42  return false;
43  }
44 
45  std::vector<unsigned char> symKey;
46  pk.GetBuf(symKey);
47  symKey.resize(32);
48 
49  return EncryptBlob(plainTextData, dataSize, data, symKey.data(), iv);
50 }
51 
52 bool CBLSIESEncryptedBlob::Decrypt(const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const
53 {
54  CBLSPublicKey pk;
55  if (!pk.DHKeyExchange(secretKey, ephemeralPubKey)) {
56  return false;
57  }
58 
59  std::vector<unsigned char> symKey;
60  pk.GetBuf(symKey);
61  symKey.resize(32);
62 
63  return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv);
64 }
65 
66 
67 bool CBLSIESMultiRecipientBlobs::Encrypt(const std::vector<CBLSPublicKey>& recipients, const BlobVector& _blobs)
68 {
69  if (recipients.size() != _blobs.size()) {
70  return false;
71  }
72 
73  InitEncrypt(_blobs.size());
74 
75  for (size_t i = 0; i < _blobs.size(); i++) {
76  if (!Encrypt(i, recipients[i], _blobs[i])) {
77  return false;
78  }
79  }
80 
81  return true;
82 }
83 
85 {
89 
90  uint256 iv = ivSeed;
91  ivVector.resize(count);
92  blobs.resize(count);
93  for (size_t i = 0; i < count; i++) {
94  ivVector[i] = iv;
95  iv = ::SerializeHash(iv);
96  }
97 }
98 
99 bool CBLSIESMultiRecipientBlobs::Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob)
100 {
101  assert(idx < blobs.size());
102 
103  CBLSPublicKey pk;
104  if (!pk.DHKeyExchange(ephemeralSecretKey, recipient)) {
105  return false;
106  }
107 
108  std::vector<unsigned char> symKey;
109  pk.GetBuf(symKey);
110  symKey.resize(32);
111 
112  return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin());
113 }
114 
115 bool CBLSIESMultiRecipientBlobs::Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const
116 {
117  if (idx >= blobs.size()) {
118  return false;
119  }
120 
121  CBLSPublicKey pk;
122  if (!pk.DHKeyExchange(sk, ephemeralPubKey)) {
123  return false;
124  }
125 
126  std::vector<unsigned char> symKey;
127  pk.GetBuf(symKey);
128  symKey.resize(32);
129 
130  uint256 iv = ivSeed;
131  for (size_t i = 0; i < idx; i++) {
132  iv = ::SerializeHash(iv);
133  }
134 
135  return DecryptBlob(blobs[idx].data(), blobs[idx].size(), blobRet, symKey.data(), iv.begin());
136 }
CBLSPublicKey ephemeralPubKey
Definition: bls_ies.h:14
bool Decrypt(const CBLSSecretKey &secretKey, CDataStream &decryptedDataRet) const
Definition: bls_ies.cpp:52
bool Encrypt(const CBLSPublicKey &peerPubKey, const void *data, size_t dataSize)
Definition: bls_ies.cpp:33
std::vector< uint256 > ivVector
Definition: bls_ies.h:91
void InitEncrypt(size_t count)
Definition: bls_ies.cpp:84
value_type * data()
Definition: streams.h:203
void GetStrongRandBytes(unsigned char *out, int num)
Function to gather random data from multiple sources, failing whenever any of those source fail to pr...
Definition: random.cpp:317
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:176
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
bool Encrypt(const std::vector< CBLSPublicKey > &recipients, const BlobVector &_blobs)
Definition: bls_ies.cpp:67
unsigned char * begin()
Definition: uint256.h:57
void MakeNewKey()
Definition: bls.cpp:102
void GetBuf(void *buf, size_t size) const
Definition: bls.h:122
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:254
std::vector< Blob > BlobVector
Definition: bls_ies.h:82
CBLSPublicKey ephemeralPubKey
Definition: bls_ies.h:85
unsigned char iv[16]
Definition: bls_ies.h:15
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
Definition: aes.cpp:159
std::vector< unsigned char > Blob
Definition: bls_ies.h:81
unsigned int size() const
Definition: uint256.h:77
CBLSSecretKey ephemeralSecretKey
Definition: bls_ies.h:90
256-bit opaque blob.
Definition: uint256.h:123
static bool DecryptBlob(const void *in, size_t inSize, Out &out, const void *symKey, const void *iv)
Definition: bls_ies.cpp:24
bool DHKeyExchange(const CBLSSecretKey &sk, const CBLSPublicKey &pk)
Definition: bls.cpp:247
CBLSPublicKey GetPublicKey() const
Definition: bls.cpp:147
static int count
Definition: tests.c:45
std::vector< unsigned char > data
Definition: bls_ies.h:16
bool Decrypt(size_t idx, const CBLSSecretKey &sk, Blob &blobRet) const
Definition: bls_ies.cpp:115
static bool EncryptBlob(const void *in, size_t inSize, Out &out, const void *symKey, const void *iv)
Definition: bls_ies.cpp:14
Released under the MIT license