forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbls_ies.cpp
More file actions
109 lines (86 loc) · 3.2 KB
/
bls_ies.cpp
File metadata and controls
109 lines (86 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2018-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bls/bls_ies.h>
#include <hash.h>
#include <random.h>
#include <crypto/aes.h>
static bool EncryptBlob(const void* in, size_t inSize, std::vector<unsigned char>& out, const void* symKey, const void* iv)
{
out.resize(inSize);
AES256CBCEncrypt enc(reinterpret_cast<const unsigned char*>(symKey), reinterpret_cast<const unsigned char*>(iv), false);
int w = enc.Encrypt(reinterpret_cast<const unsigned char*>(in), int(inSize), reinterpret_cast<unsigned char*>(out.data()));
return w == int(inSize);
}
template <typename Out>
static bool DecryptBlob(const void* in, size_t inSize, Out& out, const void* symKey, const void* iv)
{
out.resize(inSize);
AES256CBCDecrypt enc(reinterpret_cast<const unsigned char*>(symKey), reinterpret_cast<const unsigned char*>(iv), false);
int w = enc.Decrypt(reinterpret_cast<const unsigned char*>(in), int(inSize), reinterpret_cast<unsigned char*>(out.data()));
return w == (int)inSize;
}
uint256 CBLSIESEncryptedBlob::GetIV(size_t idx) const
{
uint256 iv = ivSeed;
for (size_t i = 0; i < idx; i++) {
iv = ::SerializeHash(iv);
}
return iv;
}
bool CBLSIESEncryptedBlob::Decrypt(size_t idx, const CBLSSecretKey& secretKey, CDataStream& decryptedDataRet) const
{
CBLSPublicKey pk;
if (!pk.DHKeyExchange(secretKey, ephemeralPubKey)) {
return false;
}
std::vector<unsigned char> symKey = pk.ToByteVector(false);
symKey.resize(32);
uint256 iv = GetIV(idx);
return DecryptBlob(data.data(), data.size(), decryptedDataRet, symKey.data(), iv.begin());
}
bool CBLSIESEncryptedBlob::IsValid() const
{
return ephemeralPubKey.IsValid() && !data.empty() && !ivSeed.IsNull();
}
void CBLSIESMultiRecipientBlobs::InitEncrypt(size_t count)
{
ephemeralSecretKey.MakeNewKey();
ephemeralPubKey = ephemeralSecretKey.GetPublicKey();
GetStrongRandBytes({ivSeed.begin(), ivSeed.size()});
uint256 iv = ivSeed;
ivVector.resize(count);
blobs.resize(count);
for (size_t i = 0; i < count; i++) {
ivVector[i] = iv;
iv = ::SerializeHash(iv);
}
}
bool CBLSIESMultiRecipientBlobs::Encrypt(size_t idx, const CBLSPublicKey& recipient, const Blob& blob)
{
assert(idx < blobs.size());
CBLSPublicKey pk;
if (!pk.DHKeyExchange(ephemeralSecretKey, recipient)) {
return false;
}
std::vector<uint8_t> symKey = pk.ToByteVector(false);
symKey.resize(32);
return EncryptBlob(blob.data(), blob.size(), blobs[idx], symKey.data(), ivVector[idx].begin());
}
bool CBLSIESMultiRecipientBlobs::Decrypt(size_t idx, const CBLSSecretKey& sk, Blob& blobRet) const
{
if (idx >= blobs.size()) {
return false;
}
CBLSPublicKey pk;
if (!pk.DHKeyExchange(sk, ephemeralPubKey)) {
return false;
}
std::vector<uint8_t> symKey = pk.ToByteVector(false);
symKey.resize(32);
uint256 iv = ivSeed;
for (size_t i = 0; i < idx; i++) {
iv = ::SerializeHash(iv);
}
return DecryptBlob(blobs[idx].data(), blobs[idx].size(), blobRet, symKey.data(), iv.begin());
}