-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtea.hpp
More file actions
399 lines (369 loc) · 14.6 KB
/
tea.hpp
File metadata and controls
399 lines (369 loc) · 14.6 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#ifndef ATOM_ALGORITHM_TEA_HPP
#define ATOM_ALGORITHM_TEA_HPP
#include <array>
#include <concepts>
#include <span>
#include <stdexcept>
#include <vector>
#include <spdlog/spdlog.h>
#include "atom/algorithm/rust_numeric.hpp"
namespace atom::algorithm {
/**
* @brief Custom exception class for TEA-related errors.
*
* This class inherits from std::runtime_error and is used to throw exceptions
* specific to the TEA, XTEA, and XXTEA algorithms.
*/
class TEAException : public std::runtime_error {
public:
/**
* @brief Constructs a TEAException with a specified error message.
*
* @param message The error message associated with the exception.
*/
using std::runtime_error::runtime_error;
};
/**
* @brief Concept that checks if a type is a container of 32-bit unsigned
* integers.
*
* A type satisfies this concept if it is a contiguous range where each element
* is a 32-bit unsigned integer.
*
* @tparam T The type to check.
*/
template <typename T>
concept UInt32Container = std::ranges::contiguous_range<T> && requires(T t) {
{ std::data(t) } -> std::convertible_to<const u32 *>;
{ std::size(t) } -> std::convertible_to<usize>;
requires sizeof(std::ranges::range_value_t<T>) == sizeof(u32);
};
/**
* @brief Type alias for a 128-bit key used in the XTEA algorithm.
*
* Represents the key as an array of four 32-bit unsigned integers.
*/
using XTEAKey = std::array<u32, 4>;
/**
* @brief Encrypts two 32-bit values using the TEA (Tiny Encryption Algorithm).
*
* The TEA algorithm is a symmetric-key block cipher known for its simplicity.
* This function encrypts two 32-bit unsigned integers using a 128-bit key.
*
* @param value0 The first 32-bit value to be encrypted (modified in place).
* @param value1 The second 32-bit value to be encrypted (modified in place).
* @param key A reference to an array of four 32-bit unsigned integers
* representing the 128-bit key.
* @throws TEAException if the key is invalid.
*/
auto teaEncrypt(u32 &value0, u32 &value1,
const std::array<u32, 4> &key) noexcept(false) -> void;
/**
* @brief Decrypts two 32-bit values using the TEA (Tiny Encryption Algorithm).
*
* This function decrypts two 32-bit unsigned integers using a 128-bit key.
*
* @param value0 The first 32-bit value to be decrypted (modified in place).
* @param value1 The second 32-bit value to be decrypted (modified in place).
* @param key A reference to an array of four 32-bit unsigned integers
* representing the 128-bit key.
* @throws TEAException if the key is invalid.
*/
auto teaDecrypt(u32 &value0, u32 &value1,
const std::array<u32, 4> &key) noexcept(false) -> void;
/**
* @brief Encrypts a container of 32-bit values using the XXTEA algorithm.
*
* The XXTEA algorithm is an extension of TEA, designed to correct some of TEA's
* weaknesses.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be encrypted.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @return A vector of encrypted 32-bit values.
* @throws TEAException if the input data is too small or the key is invalid.
*/
template <UInt32Container Container>
auto xxteaEncrypt(const Container &inputData, std::span<const u32, 4> inputKey)
-> std::vector<u32>;
/**
* @brief Decrypts a container of 32-bit values using the XXTEA algorithm.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be decrypted.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @return A vector of decrypted 32-bit values.
* @throws TEAException if the input data is too small or the key is invalid.
*/
template <UInt32Container Container>
auto xxteaDecrypt(const Container &inputData, std::span<const u32, 4> inputKey)
-> std::vector<u32>;
/**
* @brief Encrypts two 32-bit values using the XTEA (Extended TEA) algorithm.
*
* XTEA is a block cipher that corrects some weaknesses of TEA.
*
* @param value0 The first 32-bit value to be encrypted (modified in place).
* @param value1 The second 32-bit value to be encrypted (modified in place).
* @param key A reference to an XTEAKey representing the 128-bit key.
* @throws TEAException if the key is invalid.
*/
auto xteaEncrypt(u32 &value0, u32 &value1, const XTEAKey &key) noexcept(false)
-> void;
/**
* @brief Decrypts two 32-bit values using the XTEA (Extended TEA) algorithm.
*
* @param value0 The first 32-bit value to be decrypted (modified in place).
* @param value1 The second 32-bit value to be decrypted (modified in place).
* @param key A reference to an XTEAKey representing the 128-bit key.
* @throws TEAException if the key is invalid.
*/
auto xteaDecrypt(u32 &value0, u32 &value1, const XTEAKey &key) noexcept(false)
-> void;
/**
* @brief Converts a byte array to a vector of 32-bit unsigned integers.
*
* This function is used to prepare byte data for encryption or decryption with
* the XXTEA algorithm.
*
* @tparam T A type that satisfies the requirements of a contiguous range of
* uint8_t.
* @param data The byte array to be converted.
* @return A vector of 32-bit unsigned integers.
*/
template <typename T>
requires std::ranges::contiguous_range<T> &&
std::same_as<std::ranges::range_value_t<T>, u8>
auto toUint32Vector(const T &data) -> std::vector<u32>;
/**
* @brief Converts a vector of 32-bit unsigned integers back to a byte array.
*
* This function is used to convert the result of XXTEA decryption back into a
* byte array.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param data The vector of 32-bit unsigned integers to be converted.
* @return A byte array.
*/
template <UInt32Container Container>
auto toByteArray(const Container &data) -> std::vector<u8>;
/**
* @brief Parallel version of XXTEA encryption for large data sets.
*
* This function uses multiple threads to encrypt the input data, which can
* significantly improve performance for large data sets.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be encrypted.
* @param inputKey The 128-bit key used for encryption.
* @param numThreads The number of threads to use. If 0, the function uses the
* number of hardware threads available.
* @return A vector of encrypted 32-bit values.
*/
template <UInt32Container Container>
auto xxteaEncryptParallel(const Container &inputData,
std::span<const u32, 4> inputKey,
usize numThreads = 0) -> std::vector<u32>;
/**
* @brief Parallel version of XXTEA decryption for large data sets.
*
* This function uses multiple threads to decrypt the input data, which can
* significantly improve performance for large data sets.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be decrypted.
* @param inputKey The 128-bit key used for decryption.
* @param numThreads The number of threads to use. If 0, the function uses the
* number of hardware threads available.
* @return A vector of decrypted 32-bit values.
*/
template <UInt32Container Container>
auto xxteaDecryptParallel(const Container &inputData,
std::span<const u32, 4> inputKey,
usize numThreads = 0) -> std::vector<u32>;
/**
* @brief Implementation detail for XXTEA encryption.
*
* This function performs the actual XXTEA encryption.
*
* @param inputData A span of 32-bit values to encrypt.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @return A vector of encrypted 32-bit values.
*/
auto xxteaEncryptImpl(std::span<const u32> inputData,
std::span<const u32, 4> inputKey) -> std::vector<u32>;
/**
* @brief Implementation detail for XXTEA decryption.
*
* This function performs the actual XXTEA decryption.
*
* @param inputData A span of 32-bit values to decrypt.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @return A vector of decrypted 32-bit values.
*/
auto xxteaDecryptImpl(std::span<const u32> inputData,
std::span<const u32, 4> inputKey) -> std::vector<u32>;
/**
* @brief Implementation detail for parallel XXTEA encryption.
*
* This function performs the actual parallel XXTEA encryption.
*
* @param inputData A span of 32-bit values to encrypt.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @param numThreads The number of threads to use for encryption.
* @return A vector of encrypted 32-bit values.
*/
auto xxteaEncryptParallelImpl(std::span<const u32> inputData,
std::span<const u32, 4> inputKey,
usize numThreads) -> std::vector<u32>;
/**
* @brief Implementation detail for parallel XXTEA decryption.
*
* This function performs the actual parallel XXTEA decryption.
*
* @param inputData A span of 32-bit values to decrypt.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @param numThreads The number of threads to use for decryption.
* @return A vector of decrypted 32-bit values.
*/
auto xxteaDecryptParallelImpl(std::span<const u32> inputData,
std::span<const u32, 4> inputKey,
usize numThreads) -> std::vector<u32>;
/**
* @brief Implementation detail for converting a byte array to a vector of
* u32.
*
* This function performs the actual conversion from a byte array to a vector of
* 32-bit unsigned integers.
*
* @param data A span of bytes to convert.
* @return A vector of 32-bit unsigned integers.
*/
auto toUint32VectorImpl(std::span<const u8> data) -> std::vector<u32>;
/**
* @brief Implementation detail for converting a vector of u32 to a byte
* array.
*
* This function performs the actual conversion from a vector of 32-bit unsigned
* integers to a byte array.
*
* @param data A span of 32-bit unsigned integers to convert.
* @return A vector of bytes.
*/
auto toByteArrayImpl(std::span<const u32> data) -> std::vector<u8>;
/**
* @brief Encrypts a container of 32-bit values using the XXTEA algorithm.
*
* The XXTEA algorithm is an extension of TEA, designed to correct some of TEA's
* weaknesses.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be encrypted.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @return A vector of encrypted 32-bit values.
* @throws TEAException if the input data is too small or the key is invalid.
*/
template <UInt32Container Container>
auto xxteaEncrypt(const Container &inputData, std::span<const u32, 4> inputKey)
-> std::vector<u32> {
return xxteaEncryptImpl(
std::span<const u32>{inputData.data(), inputData.size()}, inputKey);
}
/**
* @brief Decrypts a container of 32-bit values using the XXTEA algorithm.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be decrypted.
* @param inputKey A span of four 32-bit unsigned integers representing the
* 128-bit key.
* @return A vector of decrypted 32-bit values.
* @throws TEAException if the input data is too small or the key is invalid.
*/
template <UInt32Container Container>
auto xxteaDecrypt(const Container &inputData, std::span<const u32, 4> inputKey)
-> std::vector<u32> {
return xxteaDecryptImpl(
std::span<const u32>{inputData.data(), inputData.size()}, inputKey);
}
/**
* @brief Parallel version of XXTEA encryption for large data sets.
*
* This function uses multiple threads to encrypt the input data, which can
* significantly improve performance for large data sets.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be encrypted.
* @param inputKey The 128-bit key used for encryption.
* @param numThreads The number of threads to use. If 0, the function uses the
* number of hardware threads available.
* @return A vector of encrypted 32-bit values.
*/
template <UInt32Container Container>
auto xxteaEncryptParallel(const Container &inputData,
std::span<const u32, 4> inputKey, usize numThreads)
-> std::vector<u32> {
return xxteaEncryptParallelImpl(
std::span<const u32>{inputData.data(), inputData.size()}, inputKey,
numThreads);
}
/**
* @brief Parallel version of XXTEA decryption for large data sets.
*
* This function uses multiple threads to decrypt the input data, which can
* significantly improve performance for large data sets.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param inputData The container of 32-bit values to be decrypted.
* @param inputKey The 128-bit key used for decryption.
* @param numThreads The number of threads to use. If 0, the function uses the
* number of hardware threads available.
* @return A vector of decrypted 32-bit values.
*/
template <UInt32Container Container>
auto xxteaDecryptParallel(const Container &inputData,
std::span<const u32, 4> inputKey, usize numThreads)
-> std::vector<u32> {
return xxteaDecryptParallelImpl(
std::span<const u32>{inputData.data(), inputData.size()}, inputKey,
numThreads);
}
/**
* @brief Converts a byte array to a vector of 32-bit unsigned integers.
*
* This function is used to prepare byte data for encryption or decryption with
* the XXTEA algorithm.
*
* @tparam T A type that satisfies the requirements of a contiguous range of
* u8.
* @param data The byte array to be converted.
* @return A vector of 32-bit unsigned integers.
*/
template <typename T>
requires std::ranges::contiguous_range<T> &&
std::same_as<std::ranges::range_value_t<T>, u8>
auto toUint32Vector(const T &data) -> std::vector<u32> {
return toUint32VectorImpl(std::span<const u8>{data.data(), data.size()});
}
/**
* @brief Converts a vector of 32-bit unsigned integers back to a byte array.
*
* This function is used to convert the result of XXTEA decryption back into a
* byte array.
*
* @tparam Container A type that satisfies the UInt32Container concept.
* @param data The vector of 32-bit unsigned integers to be converted.
* @return A byte array.
*/
template <UInt32Container Container>
auto toByteArray(const Container &data) -> std::vector<u8> {
return toByteArrayImpl(std::span<const u32>{data.data(), data.size()});
}
} // namespace atom::algorithm
#endif