Debian Codegraph @

src/librssguard/3rd-party/sc/simplecrypt.h

raw symbols SCIP
1 /*
2 Copyright (c) 2011, Andre Somers
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Rathenau Instituut, Andre Somers nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR #######; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef SIMPLECRYPT_H
29 #define SIMPLECRYPT_H
30
31 #include <QFlags>
32 #include <QString>
33 #include <QVector>
34
35 /**
36 @short Simple encryption and decryption of strings and byte arrays
37
38 This class provides a simple implementation of encryption and decryption
39 of strings and byte arrays.
40
41 @warning The encryption provided by this class is NOT strong encryption. It may
42 help to shield things from curious eyes, but it will NOT stand up to someone
43 determined to break the encryption. Don't say you were not warned.
44
45 The class uses a 64 bit key. Simply create an instance of the class, set the key,
46 and use the encryptToString() method to calculate an encrypted version of the input string.
47 To decrypt that string again, use an instance of SimpleCrypt initialized with
48 the same key, and call the decryptToString() method with the encrypted string. If the key
49 matches, the decrypted version of the string will be returned again.
50
51 If you do not provide a key, or if something else is wrong, the encryption and
52 decryption function will return an empty string or will return a string containing nonsense.
53 lastError() will return a value indicating if the method was succesful, and if not, why not.
54
55 SimpleCrypt is prepared for the case that the encryption and decryption
56 algorithm is changed in a later version, by prepending a version identifier to the cypertext.
57 */
58
59 class SimpleCrypt {
60 public:
61 /**
62 CompressionMode describes if compression will be applied to the data to be
63 encrypted.
64 */
65 enum CompressionMode {
66 CompressionAuto, /*!< Only apply compression if that results in a shorter plaintext. */
67 CompressionAlways, /*!< Always apply compression. Note that for short inputs, a compression may result in longer
68 data */
69 CompressionNever /*!< Never apply compression. */
70 };
71
72 /**
73 IntegrityProtectionMode describes measures taken to make it possible to detect problems with the data
74 or wrong decryption keys.
75
76 Measures involve adding a checksum or a cryptograhpic hash to the data to be encrypted. This
77 increases the length of the resulting cypertext, but makes it possible to check if the plaintext
78 appears to be valid after decryption.
79 */
80 enum IntegrityProtectionMode {
81 ProtectionNone, /*!< The integerity of the encrypted data is not protected. It is not really possible to detect a
82 wrong key, for instance. */
83 ProtectionChecksum, /*!< A simple checksum is used to verify that the data is in order. If not, an empty string is
84 returned. */
85 ProtectionHash /*!< A cryptographic hash is used to verify the integrity of the data. This method produces a much
86 stronger, but longer check */
87 };
88
89 /**
90 Error describes the type of error that occured.
91 */
92 enum Error {
93 ErrorNoError, /*!< No error occurred. */
94 ErrorNoKeySet, /*!< No key was set. You can not encrypt or decrypt without a valid key. */
95 ErrorUnknownVersion, /*!< The version of this data is unknown, or the data is otherwise not valid. */
96 ErrorIntegrityFailed /*!< The integrity check of the data failed. Perhaps the wrong key was used. */
97 };
98
99 /**
100 Constructor.
101
102 Constructs a SimpleCrypt instance without a valid key set on it.
103 */
104 SimpleCrypt();
105
106 /**
107 Constructor.
108
109 Constructs a SimpleCrypt instance and initializes it with the given @arg key.
110 */
111 explicit SimpleCrypt(quint64 key);
112
113 /**
114 (Re-) initializes the key with the given @arg key.
115 */
116 void setKey(quint64 key);
117
118 /**
119 Returns true if SimpleCrypt has been initialized with a key.
120 */
121 bool hasKey() const {
122 return !m_keyParts.isEmpty();
123 }
124
125 /**
126 Sets the compression mode to use when encrypting data. The default mode is Auto.
127
128 Note that decryption is not influenced by this mode, as the decryption recognizes
129 what mode was used when encrypting.
130 */
131 void setCompressionMode(CompressionMode mode) {
132 m_compressionMode = mode;
133 }
134
135 /**
136 Returns the CompressionMode that is currently in use.
137 */
138 CompressionMode compressionMode() const {
139 return m_compressionMode;
140 }
141
142 /**
143 Sets the integrity mode to use when encrypting data. The default mode is Checksum.
144
145 Note that decryption is not influenced by this mode, as the decryption recognizes
146 what mode was used when encrypting.
147 */
148 void setIntegrityProtectionMode(IntegrityProtectionMode mode) {
149 m_protectionMode = mode;
150 }
151
152 /**
153 Returns the IntegrityProtectionMode that is currently in use.
154 */
155 IntegrityProtectionMode integrityProtectionMode() const {
156 return m_protectionMode;
157 }
158
159 /**
160 Returns the last error that occurred.
161 */
162 Error lastError() const {
163 return m_lastError;
164 }
165
166 /**
167 Encrypts the @arg plaintext string with the key the class was initialized with, and returns
168 a cyphertext the result. The result is a base64 encoded version of the binary array that is the
169 actual result of the string, so it can be stored easily in a text format.
170 */
171 QString encryptToString(const QString& plaintext);
172
173 /**
174 Encrypts the @arg plaintext QByteArray with the key the class was initialized with, and returns
175 a cyphertext the result. The result is a base64 encoded version of the binary array that is the
176 actual result of the encryption, so it can be stored easily in a text format.
177 */
178 QString encryptToString(QByteArray plaintext);
179
180 /**
181 Encrypts the @arg plaintext string with the key the class was initialized with, and returns
182 a binary cyphertext in a QByteArray the result.
183
184 This method returns a byte array, that is useable for storing a binary format. If you need
185 a string you can store in a text file, use encryptToString() instead.
186 */
187 QByteArray encryptToByteArray(const QString& plaintext);
188
189 /**
190 Encrypts the @arg plaintext QByteArray with the key the class was initialized with, and returns
191 a binary cyphertext in a QByteArray the result.
192
193 This method returns a byte array, that is useable for storing a binary format. If you need
194 a string you can store in a text file, use encryptToString() instead.
195 */
196 QByteArray encryptToByteArray(QByteArray plaintext);
197
198 /**
199 Decrypts a cyphertext string encrypted with this class with the set key back to the
200 plain text version.
201
202 If an error occured, such as non-matching keys between encryption and decryption,
203 an empty string or a string containing nonsense may be returned.
204 */
205 QString decryptToString(const QString& cyphertext);
206
207 /**
208 Decrypts a cyphertext string encrypted with this class with the set key back to the
209 plain text version.
210
211 If an error occured, such as non-matching keys between encryption and decryption,
212 an empty string or a string containing nonsense may be returned.
213 */
214 QByteArray decryptToByteArray(const QString& cyphertext);
215
216 /**
217 Decrypts a cyphertext binary encrypted with this class with the set key back to the
218 plain text version.
219
220 If an error occured, such as non-matching keys between encryption and decryption,
221 an empty string or a string containing nonsense may be returned.
222 */
223 QString decryptToString(QByteArray cypher);
224
225 /**
226 Decrypts a cyphertext binary encrypted with this class with the set key back to the
227 plain text version.
228
229 If an error occured, such as non-matching keys between encryption and decryption,
230 an empty string or a string containing nonsense may be returned.
231 */
232 QByteArray decryptToByteArray(QByteArray cypher);
233
234 // enum to describe options that have been used for the encryption. Currently only one, but
235 // that only leaves room for future extensions like adding a cryptographic hash...
236 enum CryptoFlag {
237 CryptoFlagNone = 0,
238 CryptoFlagCompression = 0x01,
239 CryptoFlagChecksum = 0x02,
240 CryptoFlagHash = 0x04
241 };
242
243 Q_DECLARE_FLAGS(CryptoFlags, CryptoFlag)
244
245 private:
246 void splitKey();
247
248 quint64 m_key;
249
250 QVector<char> m_keyParts;
251 CompressionMode m_compressionMode;
252 IntegrityProtectionMode m_protectionMode;
253 Error m_lastError;
254 };
255
256 Q_DECLARE_OPERATORS_FOR_FLAGS(SimpleCrypt::CryptoFlags)
257
258 #endif // SimpleCrypt_H