PIVX Core  5.6.99
P2P Digital Currency
bls_ies.h
Go to the documentation of this file.
1 // Copyright (c) 2018 The Dash Core developers
2 // Copyright (c) 2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_BLS_BLS_IES_H
7 #define PIVX_BLS_BLS_IES_H
8 
9 #include "bls/bls_wrapper.h"
10 #include "streams.h"
11 
12 // Base class to handle encryption/decryption of a binary object.
13 // No padding: the size of the object must be a multiple of AES_BLOCKSIZE (16)
15 {
16 public:
18  unsigned char iv[16];
19  std::vector<unsigned char> data;
20  bool valid{false};
21 
22  bool Encrypt(const CBLSPublicKey& peerPubKey, const void* data, size_t dataSize);
23  bool Decrypt(const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const;
24 
26  {
27  SER_WRITE(obj, assert(obj.valid));
28 
29  READWRITE(obj.ephemeralPubKey);
30  READWRITE(obj.iv);
31  READWRITE(obj.data);
32 
33  SER_READ(obj, obj.valid = true);
34  }
35 };
36 
37 // Encryption/Decryption of an object of type Object
38 // (serialized size of Object must be multiple of AES_BLOCKSIZE)
39 template <typename Object>
41 {
42 public:
44  {
45  }
46 
47  bool Encrypt(const CBLSPublicKey& peerPubKey, const Object& obj, int nVersion)
48  {
49  try {
50  CDataStream ds(SER_NETWORK, nVersion);
51  ds << obj;
52  return CBLSIESEncryptedBlob::Encrypt(peerPubKey, ds.data(), ds.size());
53  } catch (std::exception&) {
54  return false;
55  }
56  }
57 
58  bool Decrypt(const CBLSSecretKey& secretKey, Object& objRet, int nVersion) const
59  {
60  CDataStream ds(SER_NETWORK, nVersion);
61  if (!CBLSIESEncryptedBlob::Decrypt(secretKey, ds)) {
62  return false;
63  }
64  try {
65  ds >> objRet;
66  } catch (std::exception& e) {
67  return false;
68  }
69  return true;
70  }
71 };
72 
73 // Base class to handle encryption/decryption of a vector of binary objects.
74 // No padding: the size of each object must be a multiple of AES_BLOCKSIZE (16)
76 {
77 public:
78  typedef std::vector<unsigned char> Blob;
79  typedef std::vector<Blob> BlobVector;
80 
81 public:
85 
86  // Used while encrypting. Temporary and only in-memory
88  std::vector<uint256> ivVector;
89 
90 public:
91  bool Encrypt(const std::vector<CBLSPublicKey>& recipients, const BlobVector& _blobs);
92 
93  void InitEncrypt(size_t count);
94  bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob);
95  bool Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const;
96 
98  {
99  READWRITE(obj.ephemeralPubKey);
100  READWRITE(obj.ivSeed);
101  READWRITE(obj.blobs);
102  }
103 };
104 
105 // Encryption/Decryption of a vector of objects of type Object
106 // (the serialized size of Object must be multiple of AES_BLOCKSIZE)
107 template <typename Object>
109 {
110 public:
111  typedef std::vector<Object> ObjectVector;
112 
113 public:
114  bool Encrypt(const std::vector<CBLSPublicKey>& recipients, const ObjectVector& _objects, int nVersion)
115  {
117  blobs.resize(_objects.size());
118 
119  try {
120  CDataStream ds(SER_NETWORK, nVersion);
121  for (size_t i = 0; i < _objects.size(); i++) {
122  ds.clear();
123 
124  ds << _objects[i];
125  blobs[i].assign(ds.begin(), ds.end());
126  }
127  } catch (std::exception&) {
128  return false;
129  }
130 
131  return CBLSIESMultiRecipientBlobs::Encrypt(recipients, blobs);
132  }
133 
134  bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Object& obj, int nVersion)
135  {
136  CDataStream ds(SER_NETWORK, nVersion);
137  ds << obj;
138  Blob blob(ds.begin(), ds.end());
139  return CBLSIESMultiRecipientBlobs::Encrypt(idx, recipient, blob);
140  }
141 
142  bool Decrypt(size_t idx, const CBLSSecretKey& sk, Object& objectRet, int nVersion) const
143  {
144  Blob blob;
145  if (!CBLSIESMultiRecipientBlobs::Decrypt(idx, sk, blob)) {
146  return false;
147  }
148 
149  try {
150  CDataStream ds(blob, SER_NETWORK, nVersion);
151  ds >> objectRet;
152  return true;
153  } catch (std::exception&) {
154  return false;
155  }
156  }
157 };
158 
159 #endif // PIVX_BLS_BLS_IES_H
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
SERIALIZE_METHODS(CBLSIESEncryptedBlob, obj)
Definition: bls_ies.h:25
unsigned char iv[16]
Definition: bls_ies.h:18
bool Decrypt(const CBLSSecretKey &secretKey, Object &objRet, int nVersion) const
Definition: bls_ies.h:58
bool Encrypt(const CBLSPublicKey &peerPubKey, const Object &obj, int nVersion)
Definition: bls_ies.h:47
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
SERIALIZE_METHODS(CBLSIESMultiRecipientBlobs, obj)
Definition: bls_ies.h:97
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 Encrypt(const std::vector< CBLSPublicKey > &recipients, const ObjectVector &_objects, int nVersion)
Definition: bls_ies.h:114
bool Encrypt(size_t idx, const CBLSPublicKey &recipient, const Object &obj, int nVersion)
Definition: bls_ies.h:134
std::vector< Object > ObjectVector
Definition: bls_ies.h:111
bool Decrypt(size_t idx, const CBLSSecretKey &sk, Object &objectRet, int nVersion) const
Definition: bls_ies.h:142
value_type * data()
Definition: streams.h:178
const_iterator end() const
Definition: streams.h:163
const_iterator begin() const
Definition: streams.h:161
size_type size() const
Definition: streams.h:165
void clear()
Definition: streams.h:171
256-bit opaque blob.
Definition: uint256.h:138
#define SER_WRITE(obj, code)
Definition: serialize.h:186
@ SER_NETWORK
Definition: serialize.h:174
#define SER_READ(obj, code)
Definition: serialize.h:185
#define READWRITE(...)
Definition: serialize.h:183