-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect.cpp
108 lines (88 loc) · 2.54 KB
/
detect.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
/* Copyright (c) 2009, Markus Peloquin <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <cstdint>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include "detect.hpp"
#include "hash.hpp"
namespace fluks {
namespace {
struct cipher_stats {
uint16_t blocksize;
uint16_t key_min;
uint16_t key_max;
uint16_t key_step;
};
/** Singleton to parse /proc/crypto for supported ciphers and hashes */
class Crypto_detect {
public:
static Crypto_detect *instance()
{ return &_instance; }
// detected from /proc/crypto
std::set<std::string> ciphers;
std::set<std::string> hashes;
private:
Crypto_detect();
~Crypto_detect() {}
Crypto_detect(const Crypto_detect &) {}
void operator=(const Crypto_detect &) {}
static Crypto_detect _instance;
};
Crypto_detect::Crypto_detect()
{
std::ifstream file_in("/proc/crypto");
// can't throw an exception
if (!file_in) return;
std::regex expr("(.+?)\\s*:\\s(.+)");
std::string line;
std::string name;
std::string type;
while (file_in) {
if (!std::getline(file_in, line)) break;
if (line.empty()) {
// empty line signifies the end of a crypto
// description
if (type == "cipher")
ciphers.insert(name);
else if (type == "digest" ||
type == "shash" ||
type == "ahash")
hashes.insert(name);
name = "";
type = "";
} else {
std::smatch matches;
if (!std::regex_match(line, matches, expr))
std::cerr << "/proc/crypto match failed: "
<< line << '\n';
else if (matches[1] == "name") name = matches[2];
else if (matches[1] == "type") type = matches[2];
}
}
}
Crypto_detect Crypto_detect::_instance;
} // end anon namespace
}
const std::set<std::string> &
fluks::system_ciphers()
{
return Crypto_detect::instance()->ciphers;
}
const std::set<std::string> &
fluks::system_hashes()
{
return Crypto_detect::instance()->hashes;
}