Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

bls_ies.h
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 #ifndef DASH_CRYPTO_BLS_IES_H
6 #define DASH_CRYPTO_BLS_IES_H
7 
8 #include <bls/bls.h>
9 #include <streams.h>
10 
12 {
13 public:
15  unsigned char iv[16];
16  std::vector<unsigned char> data;
17 
18  bool valid{false};
19 
20 public:
22 
23  template <typename Stream, typename Operation>
24  inline void SerializationOp(Stream& s, Operation ser_action)
25  {
26  if (!ser_action.ForRead()) {
27  assert(valid);
28  } else {
29  valid = false;
30  }
33  READWRITE(data);
34  if (ser_action.ForRead()) {
35  valid = true;
36  }
37  };
38 
39 public:
40  bool Encrypt(const CBLSPublicKey& peerPubKey, const void* data, size_t dataSize);
41  bool Decrypt(const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const;
42 };
43 
44 template <typename Object>
46 {
47 public:
49  {
50  }
51 
52  bool Encrypt(const CBLSPublicKey& peerPubKey, const Object& obj, int nVersion)
53  {
54  try {
55  CDataStream ds(SER_NETWORK, nVersion);
56  ds << obj;
57  return CBLSIESEncryptedBlob::Encrypt(peerPubKey, ds.data(), ds.size());
58  } catch (std::exception&) {
59  return false;
60  }
61  }
62 
63  bool Decrypt(const CBLSSecretKey& secretKey, Object& objRet, int nVersion) const
64  {
65  CDataStream ds(SER_NETWORK, nVersion);
66  if (!CBLSIESEncryptedBlob::Decrypt(secretKey, ds)) {
67  return false;
68  }
69  try {
70  ds >> objRet;
71  } catch (std::exception& e) {
72  return false;
73  }
74  return true;
75  }
76 };
77 
79 {
80 public:
81  typedef std::vector<unsigned char> Blob;
82  typedef std::vector<Blob> BlobVector;
83 
84 public:
88 
89  // Used while encrypting. Temporary and only in-memory
91  std::vector<uint256> ivVector;
92 
93 public:
94  bool Encrypt(const std::vector<CBLSPublicKey>& recipients, const BlobVector& _blobs);
95 
96  void InitEncrypt(size_t count);
97  bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob);
98  bool Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const;
99 
100 public:
102 
103  template <typename Stream, typename Operation>
104  inline void SerializationOp(Stream& s, Operation ser_action)
105  {
107  READWRITE(ivSeed);
108  READWRITE(blobs);
109  }
110 };
111 
112 template <typename Object>
114 {
115 public:
116  typedef std::vector<Object> ObjectVector;
117 
118 public:
119  bool Encrypt(const std::vector<CBLSPublicKey>& recipients, const ObjectVector& _objects, int nVersion)
120  {
122  blobs.resize(_objects.size());
123 
124  try {
125  CDataStream ds(SER_NETWORK, nVersion);
126  for (size_t i = 0; i < _objects.size(); i++) {
127  ds.clear();
128 
129  ds << _objects[i];
130  blobs[i].assign(ds.begin(), ds.end());
131  }
132  } catch (std::exception&) {
133  return false;
134  }
135 
136  return CBLSIESMultiRecipientBlobs::Encrypt(recipients, blobs);
137  }
138 
139  bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Object& obj, int nVersion)
140  {
141  CDataStream ds(SER_NETWORK, nVersion);
142  ds << obj;
143  Blob blob(ds.begin(), ds.end());
144  return CBLSIESMultiRecipientBlobs::Encrypt(idx, recipient, blob);
145  }
146 
147  bool Decrypt(size_t idx, const CBLSSecretKey& sk, Object& objectRet, int nVersion) const
148  {
149  Blob blob;
150  if (!CBLSIESMultiRecipientBlobs::Decrypt(idx, sk, blob)) {
151  return false;
152  }
153 
154  try {
155  CDataStream ds(blob, SER_NETWORK, nVersion);
156  ds >> objectRet;
157  return true;
158  } catch (std::exception&) {
159  return false;
160  }
161  }
162 };
163 
164 #endif // DASH_CRYPTO_BLS_IES_H
CBLSPublicKey ephemeralPubKey
Definition: bls_ies.h:14
bool Decrypt(const CBLSSecretKey &secretKey, CDataStream &decryptedDataRet) const
Definition: bls_ies.cpp:52
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
Definition: bls_ies.h:24
bool Encrypt(const CBLSPublicKey &peerPubKey, const void *data, size_t dataSize)
Definition: bls_ies.cpp:33
#define READWRITE(obj)
Definition: serialize.h:165
std::vector< uint256 > ivVector
Definition: bls_ies.h:91
void InitEncrypt(size_t count)
Definition: bls_ies.cpp:84
bool Decrypt(const CBLSSecretKey &secretKey, Object &objRet, int nVersion) const
Definition: bls_ies.h:63
value_type * data()
Definition: streams.h:203
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
bool Encrypt(size_t idx, const CBLSPublicKey &recipient, const Object &obj, int nVersion)
Definition: bls_ies.h:139
#define FLATDATA(obj)
Definition: serialize.h:370
bool Encrypt(const CBLSPublicKey &peerPubKey, const Object &obj, int nVersion)
Definition: bls_ies.h:52
std::vector< Blob > BlobVector
Definition: bls_ies.h:82
CBLSPublicKey ephemeralPubKey
Definition: bls_ies.h:85
size_type size() const
Definition: streams.h:194
unsigned char iv[16]
Definition: bls_ies.h:15
#define ADD_SERIALIZE_METHODS
Implement three methods for serializable objects.
Definition: serialize.h:174
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
Definition: bls_ies.h:104
std::vector< Object > ObjectVector
Definition: bls_ies.h:116
std::vector< unsigned char > Blob
Definition: bls_ies.h:81
bool Encrypt(const std::vector< CBLSPublicKey > &recipients, const ObjectVector &_objects, int nVersion)
Definition: bls_ies.h:119
CBLSSecretKey ephemeralSecretKey
Definition: bls_ies.h:90
256-bit opaque blob.
Definition: uint256.h:123
const_iterator end() const
Definition: streams.h:192
const_iterator begin() const
Definition: streams.h:190
static int count
Definition: tests.c:45
void clear()
Definition: streams.h:200
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
bool Decrypt(size_t idx, const CBLSSecretKey &sk, Object &objectRet, int nVersion) const
Definition: bls_ies.h:147
Released under the MIT license