-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcipher_spec.cpp
More file actions
153 lines (135 loc) · 4.74 KB
/
cipher_spec.cpp
File metadata and controls
153 lines (135 loc) · 4.74 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
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
150
151
152
153
#include <format>
#include <regex>
#include <set>
#include "cipher.hpp"
#include "cipher_spec.hpp"
#include "detect.hpp"
#include "hash.hpp"
#include "support.hpp"
void
fluks::Cipher_spec::check_spec_support(const Cipher_traits *cipher_traits,
const Hash_traits *hash_traits) {
// is the cipher spec supported by the system?
const std::set<std::string> &sys_ciph = system_ciphers();
if (!sys_ciph.count(cipher_traits->name))
throw Bad_spec("cipher not supported by system: " +
_nm_cipher);
const std::set<std::string> &sys_hash = system_hashes();
if (_nm_iv_hash.size() && !sys_hash.count(hash_traits->name))
throw Bad_spec("IV hash not supported by system: " +
_nm_iv_hash);
}
void
fluks::Cipher_spec::check_spec(ptrdiff_t sz_key) {
if (_ty_cipher == cipher_type::UNDEFINED)
throw Bad_spec("unrecognized cipher: " + _nm_cipher);
if (_ty_block_mode == block_mode::UNDEFINED)
throw Bad_spec("unrecognized block mode: " +
_nm_block_mode);
if (!_nm_iv_mode.empty() && _ty_iv_mode == iv_mode::UNDEFINED)
throw Bad_spec("unrecognized IV mode: " + _nm_iv_mode);
if (!_nm_iv_hash.empty() && _ty_iv_hash == hash_type::UNDEFINED)
throw Bad_spec("unrecognized IV hash: " + _nm_iv_hash);
const Cipher_traits *cipher_traits =
Cipher_traits::traits(_ty_cipher);
const Hash_traits *ivhash_traits =
Hash_traits::traits(_ty_iv_hash);
check_spec_support(cipher_traits, ivhash_traits);
// XXX how to check for CBC, etc? They get added to /proc/crypto, but
// XXX only *after* dm-crypt attempts to use them.
const std::vector<uint16_t> &sizes = cipher_traits->key_sizes;
if (sz_key != -1 && !std::ranges::binary_search(sizes, sz_key)) {
// sz_key not compatible with the cipher
auto out = std::format("cipher `{}' only supports keys of sizes", _nm_cipher);
bool first = true;
for (uint16_t size : sizes) {
if (!first) out += ',';
first = false;
out += std::format(" {}", size * 8);
}
out += std::format(" (not {})", sz_key);
throw Bad_spec(out);
}
// are the specs compatible?
if (_ty_block_mode == block_mode::ECB &&
_ty_iv_mode != iv_mode::UNDEFINED)
throw Bad_spec("ECB cannot use an IV mode");
if (_ty_block_mode != block_mode::ECB &&
_ty_iv_mode == iv_mode::UNDEFINED)
throw Bad_spec(
"block modes other than ECB require an IV mode");
if (_ty_iv_mode == iv_mode::ESSIV &&
_ty_iv_hash == hash_type::UNDEFINED)
throw Bad_spec("IV mode `essiv' requires an IV hash");
if (_ty_iv_mode == iv_mode::PLAIN &&
_ty_iv_hash != hash_type::UNDEFINED)
throw Bad_spec("IV mode `plain' cannot use an IV hash");
if (_ty_iv_mode == iv_mode::ESSIV) {
// check that ESSIV hash size is a possible key size of the
// cipher
uint16_t size = ivhash_traits->digest_size;
if (!std::ranges::binary_search(sizes, size)) {
auto out = std::format(
"cipher `{}' only supports keys of sizes",
_nm_cipher);
bool first = true;
for (uint16_t size : sizes) {
if (!first) out += ',';
first = false;
out += std::format(" {}", size * 8);
}
out += std::format("; incompatible with hash `{}'", _nm_iv_hash);
throw Bad_spec(out);
}
}
}
void
fluks::Cipher_spec::reset(ptrdiff_t sz_key, std::string_view spec) {
// valid patterns:
// [^-]* - [^-*]
// [^-]* - [^-*] - [^:]*
// [^-]* - [^-*] - [^:]* : .*
std::regex expr(R"(([^-]+)-([^-]+)(?:-([^:]+))?(?::(.+))?)");
std::cmatch matches;
if (!std::regex_match(spec.begin(), spec.end(), matches, expr))
throw Bad_spec("cannot be parsed");
_nm_cipher = matches[1];
_nm_block_mode = matches[2];
_nm_iv_mode = matches[3];
_nm_iv_hash = matches[4];
_ty_cipher = Cipher_traits::type(_nm_cipher);
_ty_block_mode = block_mode_info::type(_nm_block_mode);
_ty_iv_mode = iv_mode_info::type(_nm_iv_mode);
_ty_iv_hash = Hash_traits::type(_nm_iv_hash);
check_spec(sz_key);
}
void
fluks::Cipher_spec::reset(ptrdiff_t sz_key, cipher_type cipher,
block_mode block_mode, iv_mode iv_mode, hash_type iv_hash) {
const Cipher_traits *ctraits = Cipher_traits::traits(_ty_cipher);
_nm_cipher = ctraits->name;
_nm_block_mode = block_mode_info::name(_ty_block_mode);
_nm_iv_mode = iv_mode_info::name(_ty_iv_mode);
const Hash_traits *htraits = Hash_traits::traits(_ty_iv_hash);
_nm_iv_hash = htraits->name;
check_spec(sz_key);
}
std::string
fluks::Cipher_spec::canon_cipher() const {
const Cipher_traits *traits = Cipher_traits::traits(_ty_cipher);
return traits->name;
}
std::string
fluks::Cipher_spec::canon_mode() const {
std::string result = block_mode_info::name(_ty_block_mode);
if (_ty_iv_mode != iv_mode::UNDEFINED) {
result += '-';
result += iv_mode_info::name(_ty_iv_mode);
}
if (_ty_iv_hash != hash_type::UNDEFINED) {
const Hash_traits *traits = Hash_traits::traits(_ty_iv_hash);
result += ':';
result += traits->name;
}
return result;
}