-
Notifications
You must be signed in to change notification settings - Fork 1
/
cipher_spec.hpp
162 lines (136 loc) · 3.81 KB
/
cipher_spec.hpp
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
154
155
156
157
158
159
160
161
162
/* Copyright (c) 2009-2010, 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. */
#ifndef CIPHER_SPEC_HPP
#define CIPHER_SPEC_HPP
#include <string>
#include "errors.hpp"
namespace fluks {
/** Ciphers supported by <em>fluks</em> */
enum class cipher_type {
UNDEFINED = 0,
AES,
BLOWFISH,
#ifndef OPENSSL_NO_CAMELLIA
CAMELLIA,
#endif
CAST5,
CAST6,
TWOFISH,
SERPENT
};
/** Cipher block modes supported by <em>fluks</em> */
enum class block_mode {
UNDEFINED = 0,
CBC, /**< Cipher-block chaining */
CBC_CTS, /**< Cipher-block chaining with ciphertext stealing */
CFB, /**< Cipher feedback */
CTR, /**< Counter */
/** Cipher Text Stealing
*
* Described in RFC 2040, Section 8 */
ECB, /**< Electronic codebook */
OFB, /**< Output feedback */
PCBC /**< Propogating cipher-block chaining */
};
enum class iv_mode {
UNDEFINED = 0,
PLAIN,
ESSIV
};
/** Hash types supported by <em>fluks</em>
*
* Tiger is optimized for 64-bit architectures, designed by the same folks
* who brought you the Serpent cipher. Tiger/{128,160} are just truncated
* versions of Tiger/192.
*
* Along with SHA-{1,256,384,512} and RMD-{128,160}, WHIRLPOOL is included
* in ISO/IEC's list of recommended hash functions (10118-3), and is
* also recommended by NESSIE. WHIRLPOOL-{256,384} are just truncated
* versions.
*/
enum class hash_type {
UNDEFINED = 0,
MD5, /**< (you probably should not use this) */
RMD160, /**< Possibly better knows as RIPEMD-160 */
SHA1,
SHA224,
SHA256,
SHA384,
SHA512,
TIGER128,
TIGER160,
TIGER192,
WHIRLPOOL256,
WHIRLPOOL384,
WHIRLPOOL512
};
class Cipher_traits;
class Hash_traits;
class Cipher_spec {
public:
Cipher_spec(ssize_t sz_key, const std::string &spec) {
reset(sz_key, spec);
}
Cipher_spec(ssize_t sz_key, cipher_type cipher,
block_mode block_mode=block_mode::UNDEFINED,
iv_mode iv_mode=iv_mode::UNDEFINED,
hash_type iv_hash=hash_type::UNDEFINED) {
reset(sz_key, cipher, block_mode, iv_mode, iv_hash);
}
void reset(ssize_t sz_key, const std::string &spec);
void reset(ssize_t sz_key, cipher_type cipher,
block_mode block_mode=block_mode::UNDEFINED,
iv_mode iv_mode=iv_mode::UNDEFINED,
hash_type iv_hash=hash_type::UNDEFINED);
cipher_type type_cipher() const {
return _ty_cipher;
}
block_mode type_block_mode() const {
return _ty_block_mode;
}
iv_mode type_iv_mode() const {
return _ty_iv_mode;
}
hash_type type_iv_hash() const {
return _ty_iv_hash;
}
const std::string &name_cipher() const {
return _nm_cipher;
}
const std::string &name_block_mode() const {
return _nm_block_mode;
}
const std::string &name_iv_mode() const {
return _nm_iv_mode;
}
const std::string &name_iv_hash() const {
return _nm_iv_hash;
}
std::string canon_cipher() const;
std::string canon_mode() const;
private:
void check_spec_support(const Cipher_traits *cipher_traits,
const Hash_traits *hash_traits);
void check_spec(ssize_t sz_key);
std::string _nm_cipher;
std::string _nm_block_mode;
std::string _nm_iv_mode;
std::string _nm_iv_hash;
cipher_type _ty_cipher;
block_mode _ty_block_mode;
iv_mode _ty_iv_mode;
hash_type _ty_iv_hash;
};
}
#endif