-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotected_example.cpp
More file actions
73 lines (60 loc) · 1.53 KB
/
Copy pathprotected_example.cpp
File metadata and controls
73 lines (60 loc) · 1.53 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
#ifdef _WIN32
#define NOMINMAX
#endif
#include <mexce_protected.h>
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <utility>
#include <vector>
std::vector<uint8_t> read_file(const char* path)
{
std::ifstream input(path, std::ios::binary);
if (!input) {
throw std::runtime_error("Cannot open a protected-expression input file.");
}
return std::vector<uint8_t>(
std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
}
class Raw_key_wipe_guard
{
public:
explicit Raw_key_wipe_guard(std::vector<uint8_t>& key)
:
m_key(key)
{}
~Raw_key_wipe_guard()
{
if (!m_key.empty()) {
sodium_memzero(m_key.data(), m_key.size());
}
}
private:
std::vector<uint8_t>& m_key;
};
int run_protected_example(char* argv[])
{
const std::vector<uint8_t> program = read_file(argv[1]);
std::vector<uint8_t> key = read_file(argv[2]);
Raw_key_wipe_guard key_wipe(key);
auto protected_key = mexce::Protected_expression_key::from_bytes(
key.data(), key.size());
double value = 2.0;
mexce::evaluator evaluator;
evaluator.bind_protected(value, 0);
evaluator.set_protected_expression(
program.data(), program.size(), std::move(protected_key));
return evaluator.evaluate() == 3.0 ? 0 : 1;
}
int main(int argc, char* argv[])
{
if (argc != 3) {
return 2;
}
try {
return run_protected_example(argv);
}
catch (const std::exception&) {
return 1;
}
}