-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcipher.cpp
More file actions
118 lines (100 loc) · 3.31 KB
/
cipher.cpp
File metadata and controls
118 lines (100 loc) · 3.31 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
110
111
112
113
114
115
116
117
118
#include <map>
#include "cipher.hpp"
namespace fluks {
namespace {
class Lookup {
public:
static const Cipher_traits *traits(cipher_type type);
static cipher_type type(std::string_view name);
static const std::vector<cipher_type> &types() {
return inst._types;
}
private:
Lookup();
Lookup(const Lookup &) {}
void operator=(const Lookup &) {}
static Lookup inst;
std::map<cipher_type, Cipher_traits, std::less<>> _map_traits;
std::map<std::string, cipher_type, std::less<>> _map_type;
std::vector<cipher_type> _types;
};
const Cipher_traits *
Lookup::traits(cipher_type type) {
auto it = inst._map_traits.find(type);
if (it == inst._map_traits.end()) return nullptr;
return &it->second;
}
cipher_type
Lookup::type(std::string_view name) {
auto it = inst._map_type.find(name);
if (it == inst._map_type.end()) return cipher_type::UNDEFINED;
return it->second;
}
Lookup::Lookup() {
// name, min_key, max_key, key_step, blocksize, version
_map_traits[cipher_type::AES] = Cipher_traits("aes", 16, 32, 8, 16, 1);
_map_traits[cipher_type::BLOWFISH] = Cipher_traits("blowfish", 4, 56, 1, 8, 0);
_map_traits[cipher_type::CAMELLIA] = Cipher_traits("camellia", 16, 32, 8, 16, 0);
_map_traits[cipher_type::CAST5] = Cipher_traits("cast5", 5, 16, 1, 8, 1);
_map_traits[cipher_type::CAST6] = Cipher_traits("cast6", 16, 32, 4, 16, 1);
_map_traits[cipher_type::SERPENT] = Cipher_traits("serpent", 16, 32, 8, 16, 1);
_map_traits[cipher_type::TWOFISH] = Cipher_traits("twofish", 16, 32, 8, 16, 1);
_map_type["aes"] = cipher_type::AES;
_map_type["blowfish"] = cipher_type::BLOWFISH;
_map_type["camellia"] = cipher_type::CAMELLIA;
_map_type["cast5"] = cipher_type::CAST5;
_map_type["cast6"] = cipher_type::CAST6;
_map_type["serpent"] = cipher_type::SERPENT;
_map_type["twofish"] = cipher_type::TWOFISH;
for (auto &[cipher_type, _] : _map_traits)
_types.push_back(cipher_type);
}
Lookup Lookup::inst;
} // end anon namespace
}
fluks::Cipher_traits::Cipher_traits(std::string_view name,
uint16_t min_key, uint16_t max_key, uint16_t key_step,
uint16_t sz_blk, uint16_t version) :
name(name),
key_sizes((max_key - min_key + key_step) / key_step),
block_size(sz_blk),
luks_version(version)
{
uint16_t i = 0;
for (uint16_t n = min_key; n <= max_key; n += key_step)
key_sizes[i++] = n;
}
const fluks::Cipher_traits *
fluks::Cipher_traits::traits(cipher_type type) {
return Lookup::traits(type);
}
fluks::cipher_type
fluks::Cipher_traits::type(std::string_view name) {
return Lookup::type(name);
}
const std::vector<fluks::cipher_type> &
fluks::Cipher_traits::types() {
return Lookup::types();
}
std::shared_ptr<fluks::Cipher>
fluks::Cipher::create(cipher_type type) {
switch (type) {
case cipher_type::AES:
return std::shared_ptr<Cipher>(new Cipher_aes);
case cipher_type::BLOWFISH:
return std::shared_ptr<Cipher>(new Cipher_blowfish);
case cipher_type::CAMELLIA:
return std::shared_ptr<Cipher>(new Cipher_camellia);
case cipher_type::CAST5:
return std::shared_ptr<Cipher>(new Cipher_cast5);
case cipher_type::CAST6:
return std::shared_ptr<Cipher>(new Cipher_cast6);
case cipher_type::SERPENT:
return std::shared_ptr<Cipher>(new Cipher_serpent);
case cipher_type::TWOFISH:
return std::shared_ptr<Cipher>(new Cipher_twofish);
default:
Assert(0, "Cipher::create() bad cipher type");
return std::shared_ptr<Cipher>();
}
}