-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpp_start.cpp
135 lines (130 loc) · 4.7 KB
/
pp_start.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <bitcoin/bitcoin.hpp>
#include "aes256.h"
using namespace bc;
namespace fs = boost::filesystem;
payment_address bidding_address(const ec_point& pubkey)
{
data_chunk data(pubkey.begin(), pubkey.end());
payment_address payaddr;
set_public_key(payaddr, data);
return payaddr;
}
hash_digest derive_seed(const ec_point& pubkey)
{
data_chunk data(pubkey.begin(), pubkey.end());
return bitcoin_hash(data);
}
// Encrypt for first hash.
data_chunk pp_encrypt(data_chunk buffer, hash_digest seed)
{
aes256_context ctx;
BITCOIN_ASSERT(seed.size() == 32);
aes256_init(&ctx, seed.data());
BITCOIN_ASSERT(buffer.size() % 16 == 0);
for (size_t i = 0; i < buffer.size(); i += 16)
aes256_encrypt_ecb(&ctx, buffer.data() + i);
aes256_done(&ctx);
return buffer;
}
void test_decryption(const data_chunk& buffer,
data_chunk cipher, const ec_point& addr_pubkey)
{
hash_digest seed = derive_seed(addr_pubkey);
aes256_context ctx;
BITCOIN_ASSERT(seed.size() == 32);
aes256_init(&ctx, seed.data());
BITCOIN_ASSERT(cipher.size() % 16 == 0);
for (size_t i = 0; i < cipher.size(); i += 16)
aes256_decrypt_ecb(&ctx, cipher.data() + i);
aes256_done(&ctx);
BITCOIN_ASSERT(buffer == cipher);
}
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cerr << "Usage: pp_start DOCUMENT CHUNKS" << std::endl;
std::cerr << "Good default for CHUNKS is 100" << std::endl;
return -1;
}
const fs::path document_full_path = argv[1];
const fs::path doc_path = document_full_path.parent_path();
const fs::path doc_filename = document_full_path.filename();
const std::string chunks_str = argv[2];
size_t chunks = 0;
try
{
chunks = boost::lexical_cast<size_t>(chunks_str);
}
catch (const boost::bad_lexical_cast&)
{
std::cerr << "pp_start: bad CHUNKS provided." << std::endl;
return -1;
}
const fs::path public_chunks_path =
doc_path / (doc_filename.native() + "_public_chunks");
#if 0
if (!fs::create_directory(public_chunks_path))
{
std::cerr << "pp_start: error creating path '"
<< public_chunks_path.native() << "'" << std::endl;
return -1;
}
#endif
std::ifstream infile(document_full_path.native(), std::ifstream::binary);
infile.seekg(0, std::ifstream::end);
size_t file_size = infile.tellg();
infile.seekg(0, std::ifstream::beg);
size_t chunk_size = file_size / chunks;
// AES works on blocks of 16 bytes. Round up to nearest multiple.
chunk_size += 16 - (chunk_size % 16);
BITCOIN_ASSERT(chunk_size % 16 == 0);
//std::cout << "Creating chunks of "
// << chunk_size << " bytes each." << std::endl;
// Write the bidding address and chunks
const fs::path bid_filename = public_chunks_path / "ADDRS";
std::ofstream bidfile(bid_filename.native());
size_t i = 0;
while (infile)
{
data_chunk buffer(chunk_size);
// Copy chunk to public chunk file.
char* data = reinterpret_cast<char*>(buffer.data());
infile.read(data, chunk_size);
++i;
const std::string i_str = boost::lexical_cast<std::string>(i);
const fs::path chunk_filename =
public_chunks_path / (std::string("CHUNK.") + i_str);
std::ofstream outfile(chunk_filename.native(), std::ifstream::binary);
// Create a seed.
BITCOIN_ASSERT(ec_secret_size == hash_size);
ec_secret secret = bitcoin_hash(buffer);
ec_point pubkey = secret_to_public_key(secret);
// Once we spend funds, we reveal the decryption pubkey.
payment_address bid_addr = bidding_address(pubkey);
hash_digest seed = derive_seed(pubkey);
// Should be encrypted!!
// Use hash of pubkey as encryption key.
if (buffer.size() < 16)
{
BITCOIN_ASSERT(infile.gcount() < 16);
extend_data(buffer, data_chunk(0, 16 - buffer.size()));
}
BITCOIN_ASSERT(buffer.size() >= 16);
data_chunk encrypted = pp_encrypt(buffer, seed);
char* enc_data = reinterpret_cast<char*>(encrypted.data());
outfile.write(enc_data, encrypted.size());
test_decryption(buffer, encrypted, pubkey);
// Write bidding address also.
const std::string line = i_str + " " + bid_addr.encoded() + "\n";
bidfile.write(line.c_str(), line.size());
}
std::cout << i << " chunks created." << std::endl;
std::cout << "Choose a future block height and "
"a number of chunks to release." << std::endl;
std::cout << "Announce them to the world." << std::endl;
return 0;
}