-
Notifications
You must be signed in to change notification settings - Fork 7
/
F00DFileKeyEncryptor.cpp
149 lines (119 loc) · 3.68 KB
/
F00DFileKeyEncryptor.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "F00DFileKeyEncryptor.h"
#include "Utils.h"
#include <fstream>
#include <regex>
F00DFileKeyEncryptor::F00DFileKeyEncryptor(const psvpfs::path& filePath)
: m_filePath(filePath), m_isCacheLoaded(false)
{
}
int F00DFileKeyEncryptor::load_cache_flat_file()
{
if(!psvpfs::exists(m_filePath))
return -1;
std::ifstream input(m_filePath.generic_string().c_str());
if(!input.is_open())
return -1;
std::string line;
std::vector<std::string> tokens;
while(std::getline(input, line))
{
//parse string - allow multiple split tokens
tokens.clear();
std::regex re(" \t,");
std::sregex_token_iterator first(line.begin(), line.end(), re, -1), last;
std::vector<std::string> tokens(first, last);
//there should be exactly three values - titleid, key, value
if(tokens.size() != 3)
return -1;
//extract tokens
std::string key = tokens.at(1);
std::string value = tokens.at(2);
// check key length to be 128 or 256 bit
if(key.length() != 32 && key.length() != 64)
return -1;
//key size must equal value size
if(key.length() != value.length())
return -1;
// do not allow duplicates
auto kit = m_keyCache.find(key);
if(kit != m_keyCache.end())
return -1;
m_keyCache.emplace(key, value);
}
return 0;
}
/*
int F00DFileKeyEncryptor::load_cache_json_file()
{
if(!psvpfs::exists(m_filePath))
return -1;
try
{
boost::property_tree::ptree pt;
boost::property_tree::read_json(m_filePath.generic_string(), pt);
for(auto& item : pt)
{
//parse entry
std::string titleid = item.first;
auto keych = item.second.get_child("key");
auto valuech = item.second.get_child("value");
std::string key = keych.get_value<std::string>();
std::string value = valuech.get_value<std::string>();
//check that values are not empty
if(titleid.empty() || key.empty() || value.empty())
return -1;
// check key length to be 128 or 256 bit
if(key.length() != 32 && key.length() != 64)
return -1;
//key size must equal value size
if(key.length() != value.length())
return -1;
// do not allow duplicates
auto kit = m_keyCache.find(key);
if(kit != m_keyCache.end())
return -1;
m_keyCache.emplace(key, value);
}
}
catch(std::exception e)
{
return -1;
}
return 0;
}
int F00DFileKeyEncryptor::load_cache_file()
{
if(m_filePath.extension() == ".txt")
return load_cache_flat_file();
else if(m_filePath.extension() == ".json")
return load_cache_json_file();
else
return -1;
}
*/
int F00DFileKeyEncryptor::encrypt_key(const unsigned char* key, int key_size, unsigned char* drv_key)
{
if(key_size != 0x80 &&
// key_size != 0xC0 && //TODO: need to implement padding
key_size != 0x100)
return -1;
std::string keyStr = byte_array_to_string(key, key_size / 8);
auto kit = m_keyCache.find(keyStr);
if(kit == m_keyCache.end())
return -1;
std::uint32_t nbytes = key_size / 8;
string_to_byte_array(kit->second, nbytes, drv_key);
return 0;
}
void F00DFileKeyEncryptor::print_cache(std::ostream& os, std::string sep) const
{
os << "Number of items in cache: " << m_keyCache.size() << std::endl;
//its not ok to print whole cache because it can be very long
int i = 0;
for (std::map<std::string, std::string>::const_iterator it = m_keyCache.begin(); it != m_keyCache.end(); ++it, i++)
{
if(i >= 10)
break;
os << it->first << sep << it->second << std::endl;
}
}