Line data Source code
1 : // Copyright (c) 2018-2024 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 :
11 : /**
12 : * All objects in this module working from assumption that basic scheme is
13 : * available on all masternodes. Serialization of public key for Encrypt and
14 : * Decrypt by bls_ies.h done using Basic Scheme.
15 : */
16 0 : class CBLSIESEncryptedBlob
17 : {
18 : public:
19 : CBLSPublicKey ephemeralPubKey;
20 : uint256 ivSeed;
21 : std::vector<unsigned char> data;
22 :
23 : uint256 GetIV(size_t idx) const;
24 :
25 0 : SERIALIZE_METHODS(CBLSIESEncryptedBlob, obj)
26 : {
27 0 : READWRITE(obj.ephemeralPubKey, obj.ivSeed, obj.data);
28 0 : }
29 :
30 : bool Decrypt(size_t idx, const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const;
31 : bool IsValid() const;
32 : };
33 :
34 : template <typename Object>
35 : class CBLSIESEncryptedObject : public CBLSIESEncryptedBlob
36 : {
37 : public:
38 0 : CBLSIESEncryptedObject() = default;
39 :
40 0 : CBLSIESEncryptedObject(const CBLSPublicKey& ephemeralPubKeyIn, const uint256& ivSeedIn, const std::vector<unsigned char>& dataIn)
41 0 : {
42 0 : ephemeralPubKey = ephemeralPubKeyIn;
43 0 : ivSeed = ivSeedIn;
44 0 : data = dataIn;
45 0 : }
46 :
47 0 : bool Decrypt(size_t idx, const CBLSSecretKey& secretKey, Object& objRet, int nVersion) const
48 : {
49 0 : CDataStream ds(SER_NETWORK, nVersion);
50 0 : if (!CBLSIESEncryptedBlob::Decrypt(idx, secretKey, ds)) {
51 0 : return false;
52 : }
53 : try {
54 0 : ds >> objRet;
55 0 : } catch (const std::exception&) {
56 0 : return false;
57 0 : }
58 0 : return true;
59 0 : }
60 : };
61 :
62 0 : class CBLSIESMultiRecipientBlobs
63 : {
64 : public:
65 : using Blob = std::vector<unsigned char>;
66 : using BlobVector = std::vector<Blob>;
67 :
68 : CBLSPublicKey ephemeralPubKey;
69 : uint256 ivSeed;
70 : BlobVector blobs;
71 :
72 : // Used while encrypting. Temporary and only in-memory
73 : CBLSSecretKey ephemeralSecretKey;
74 : std::vector<uint256> ivVector;
75 :
76 : void InitEncrypt(size_t count);
77 : bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob);
78 : bool Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const;
79 :
80 0 : SERIALIZE_METHODS(CBLSIESMultiRecipientBlobs, obj)
81 : {
82 0 : READWRITE(obj.ephemeralPubKey, obj.ivSeed, obj.blobs);
83 0 : }
84 : };
85 :
86 : template <typename Object>
87 : class CBLSIESMultiRecipientObjects : public CBLSIESMultiRecipientBlobs
88 : {
89 : public:
90 0 : bool Encrypt(size_t idx, const CBLSPublicKey& recipient, const Object& obj, int nVersion)
91 : {
92 0 : CDataStream ds(SER_NETWORK, nVersion);
93 0 : ds << obj;
94 0 : Blob blob(UCharCast(ds.data()), UCharCast(ds.data() + ds.size()));
95 0 : return CBLSIESMultiRecipientBlobs::Encrypt(idx, recipient, blob);
96 0 : }
97 :
98 0 : bool Decrypt(size_t idx, const CBLSSecretKey& sk, Object& objectRet, int nVersion) const
99 : {
100 0 : Blob blob;
101 0 : if (!CBLSIESMultiRecipientBlobs::Decrypt(idx, sk, blob)) {
102 0 : return false;
103 : }
104 :
105 : try {
106 0 : CDataStream ds(blob, SER_NETWORK, nVersion);
107 0 : ds >> objectRet;
108 0 : return true;
109 0 : } catch (const std::exception&) {
110 0 : return false;
111 0 : }
112 0 : }
113 :
114 0 : CBLSIESEncryptedObject<Object> Get(const size_t idx)
115 : {
116 0 : return {ephemeralPubKey, ivSeed, blobs[idx]};
117 : }
118 : };
119 :
120 : #endif // DASH_CRYPTO_BLS_IES_H
|