react-native-fs-turbo 0.3.7 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -27
- package/RNFSTurbo.podspec +2 -2
- package/android/CMakeLists.txt +62 -9
- package/android/build.gradle +80 -3
- package/android/gradle.properties +5 -5
- package/android/src/main/codegen/RNFSTurboSpec.java +17 -0
- package/android/src/main/cpp/JNIOnLoad.cpp +18 -0
- package/android/src/main/cpp/RNFSTurboLogger.cpp +4 -0
- package/android/src/main/cpp/RNFSTurboModule.cpp +44 -0
- package/android/src/main/cpp/RNFSTurboModule.h +40 -0
- package/android/src/main/cpp/RNFSTurboPlatformHelper.cpp +5 -1
- package/android/src/main/java/com/cmpayc/rnfsturbo/JNIOnLoad.java +27 -0
- package/android/src/main/java/com/cmpayc/rnfsturbo/{RNFSTurboPlatformContextModule.java → RNFSTurboModule.java} +62 -11
- package/android/src/main/java/com/cmpayc/rnfsturbo/RNFSTurboPackage.java +11 -9
- package/cpp/RNFSTurboHostObject.cpp +138 -15
- package/cpp/RNFSTurboHostObject.h +12 -4
- package/cpp/RNFSTurboInstall.cpp +27 -0
- package/cpp/RNFSTurboInstall.h +20 -0
- package/cpp/RNFSTurboLogger.h +4 -0
- package/cpp/RNFSTurboPlatformHelper.h +4 -0
- package/cpp/algorithms/Krypt/AES.cpp +502 -0
- package/cpp/algorithms/Krypt/AES.hpp +150 -0
- package/cpp/algorithms/Krypt/aes-config.hpp +71 -0
- package/cpp/algorithms/Krypt/blockcipher.hpp +69 -0
- package/cpp/algorithms/Krypt/bytearray.cpp +158 -0
- package/cpp/algorithms/Krypt/bytearray.hpp +80 -0
- package/cpp/algorithms/Krypt/functions.cpp +137 -0
- package/cpp/algorithms/Krypt/functions.hpp +54 -0
- package/cpp/algorithms/Krypt/mode.cpp +203 -0
- package/cpp/algorithms/Krypt/mode.hpp +131 -0
- package/cpp/algorithms/Krypt/padding.cpp +219 -0
- package/cpp/algorithms/Krypt/padding.hpp +148 -0
- package/cpp/algorithms/Krypt/types.hpp +208 -0
- package/cpp/encryption/encryption-utils.cpp +162 -0
- package/cpp/encryption/encryption-utils.h +43 -0
- package/cpp/filesystem/{helpers.cpp → filesystem-utils.cpp} +5 -1
- package/cpp/filesystem/{helpers.h → filesystem-utils.h} +4 -0
- package/ios/RNFSTurboLogger.mm +4 -0
- package/ios/{RNFSTurboPlatformContextModule.h → RNFSTurboModule.h} +3 -3
- package/ios/{RNFSTurboPlatformContextModule.mm → RNFSTurboModule.mm} +22 -5
- package/ios/RNFSTurboPlatformHelper.mm +4 -0
- package/lib/commonjs/NativeRNFSTurboModule.js +14 -16
- package/lib/commonjs/NativeRNFSTurboModule.js.map +1 -1
- package/lib/commonjs/createRNFSTurbo.js +3 -2
- package/lib/commonjs/createRNFSTurbo.js.map +1 -1
- package/lib/commonjs/globals.d.js +6 -0
- package/lib/commonjs/globals.d.js.map +1 -0
- package/lib/module/NativeRNFSTurboModule.js +14 -16
- package/lib/module/NativeRNFSTurboModule.js.map +1 -1
- package/lib/module/createRNFSTurbo.js +3 -2
- package/lib/module/createRNFSTurbo.js.map +1 -1
- package/lib/module/globals.d.js +4 -0
- package/lib/module/globals.d.js.map +1 -0
- package/lib/typescript/NativeRNFSTurboModule.d.ts +12 -3
- package/lib/typescript/NativeRNFSTurboModule.d.ts.map +1 -1
- package/lib/typescript/Types.d.ts +11 -0
- package/lib/typescript/Types.d.ts.map +1 -1
- package/lib/typescript/createRNFSTurbo.d.ts.map +1 -1
- package/package.json +7 -3
- package/react-native.config.js +1 -5
- package/src/NativeRNFSTurboModule.ts +25 -21
- package/src/Types.ts +11 -0
- package/src/createRNFSTurbo.ts +4 -2
- package/src/globals.d.ts +9 -0
- package/cpp/NativeRNFSTurboModule.cpp +0 -24
- package/cpp/NativeRNFSTurboModule.h +0 -31
- package/ios/RNFSTurboOnLoad.mm +0 -25
- package/lib/commonjs/NativeRNFSTurboPlatformContextModule.js +0 -22
- package/lib/commonjs/NativeRNFSTurboPlatformContextModule.js.map +0 -1
- package/lib/module/NativeRNFSTurboPlatformContextModule.js +0 -18
- package/lib/module/NativeRNFSTurboPlatformContextModule.js.map +0 -1
- package/lib/typescript/NativeRNFSTurboPlatformContextModule.d.ts +0 -16
- package/lib/typescript/NativeRNFSTurboPlatformContextModule.d.ts.map +0 -1
- package/src/NativeRNFSTurboPlatformContextModule.ts +0 -34
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2019 SergeyBel
|
|
5
|
+
* Copyright (c) 2023 Jubal Mordecai Velasco
|
|
6
|
+
* Copyright (c) 2025 Sergei Kazakov
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
#ifdef RNFSTURBO_USE_ENCRYPTION
|
|
28
|
+
|
|
29
|
+
#ifndef KRYPT_AES_CPP
|
|
30
|
+
#define KRYPT_AES_CPP
|
|
31
|
+
|
|
32
|
+
#include "AES.hpp"
|
|
33
|
+
|
|
34
|
+
namespace Krypt {
|
|
35
|
+
namespace BlockCipher {
|
|
36
|
+
void AES::AddRoundKey(unsigned char state[16], unsigned char *key) {
|
|
37
|
+
state[0 * 4 + 0] = state[0 * 4 + 0] ^ key[0];
|
|
38
|
+
state[0 * 4 + 1] = state[0 * 4 + 1] ^ key[4];
|
|
39
|
+
state[0 * 4 + 2] = state[0 * 4 + 2] ^ key[8];
|
|
40
|
+
state[0 * 4 + 3] = state[0 * 4 + 3] ^ key[12];
|
|
41
|
+
|
|
42
|
+
state[1 * 4 + 0] = state[1 * 4 + 0] ^ key[1];
|
|
43
|
+
state[1 * 4 + 1] = state[1 * 4 + 1] ^ key[5];
|
|
44
|
+
state[1 * 4 + 2] = state[1 * 4 + 2] ^ key[9];
|
|
45
|
+
state[1 * 4 + 3] = state[1 * 4 + 3] ^ key[13];
|
|
46
|
+
|
|
47
|
+
state[2 * 4 + 0] = state[2 * 4 + 0] ^ key[2];
|
|
48
|
+
state[2 * 4 + 1] = state[2 * 4 + 1] ^ key[6];
|
|
49
|
+
state[2 * 4 + 2] = state[2 * 4 + 2] ^ key[10];
|
|
50
|
+
state[2 * 4 + 3] = state[2 * 4 + 3] ^ key[14];
|
|
51
|
+
|
|
52
|
+
state[3 * 4 + 0] = state[3 * 4 + 0] ^ key[3];
|
|
53
|
+
state[3 * 4 + 1] = state[3 * 4 + 1] ^ key[7];
|
|
54
|
+
state[3 * 4 + 2] = state[3 * 4 + 2] ^ key[11];
|
|
55
|
+
state[3 * 4 + 3] = state[3 * 4 + 3] ^ key[15];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void AES::EncryptBlock(Bytes *plain, Bytes *cipher) {
|
|
59
|
+
#ifdef USE_AESNI
|
|
60
|
+
// load the current block & current round key into the registers
|
|
61
|
+
__m128i state = _mm_loadu_si128((__m128i *) &plain[0]);
|
|
62
|
+
|
|
63
|
+
// original key
|
|
64
|
+
state = _mm_xor_si128(state, RoundedKeys[0]);
|
|
65
|
+
|
|
66
|
+
// perform usual rounds
|
|
67
|
+
for (size_t i = 1; i < Nr - 1; i += 2) {
|
|
68
|
+
state = _mm_aesenc_si128(state, RoundedKeys[i]);
|
|
69
|
+
state = _mm_aesenc_si128(state, RoundedKeys[i + 1]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// last round
|
|
73
|
+
state = _mm_aesenc_si128(state, RoundedKeys[Nr - 1]);
|
|
74
|
+
state = _mm_aesenclast_si128(state, RoundedKeys[Nr]);
|
|
75
|
+
|
|
76
|
+
// store from register to array
|
|
77
|
+
_mm_storeu_si128((__m128i *) (cipher), state);
|
|
78
|
+
#elif defined(USE_ARM_AES) && defined(__aarch64__)
|
|
79
|
+
uint8x16_t state = vld1q_u8(plain);
|
|
80
|
+
|
|
81
|
+
// Initial round
|
|
82
|
+
state = vaesmcq_u8(vaeseq_u8(state, RoundedKeys[0]));
|
|
83
|
+
|
|
84
|
+
// 8 main rounds
|
|
85
|
+
for (size_t i = 1; i < Nr - 1; i += 2) {
|
|
86
|
+
state = vaesmcq_u8(vaeseq_u8(state, RoundedKeys[i]));
|
|
87
|
+
state = vaesmcq_u8(vaeseq_u8(state, RoundedKeys[i + 1]));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// last 2 final round
|
|
91
|
+
state = vaeseq_u8(state, RoundedKeys[Nr - 1]);
|
|
92
|
+
state = veorq_u8(state, RoundedKeys[Nr]);
|
|
93
|
+
|
|
94
|
+
// store the result to cipher
|
|
95
|
+
vst1q_u8(cipher, state);
|
|
96
|
+
#else
|
|
97
|
+
Bytes state[16];
|
|
98
|
+
|
|
99
|
+
state[0 * 4 + 0] = plain[0];
|
|
100
|
+
state[0 * 4 + 1] = plain[4];
|
|
101
|
+
state[0 * 4 + 2] = plain[8];
|
|
102
|
+
state[0 * 4 + 3] = plain[12];
|
|
103
|
+
|
|
104
|
+
state[1 * 4 + 0] = plain[1];
|
|
105
|
+
state[1 * 4 + 1] = plain[5];
|
|
106
|
+
state[1 * 4 + 2] = plain[9];
|
|
107
|
+
state[1 * 4 + 3] = plain[13];
|
|
108
|
+
|
|
109
|
+
state[2 * 4 + 0] = plain[2];
|
|
110
|
+
state[2 * 4 + 1] = plain[6];
|
|
111
|
+
state[2 * 4 + 2] = plain[10];
|
|
112
|
+
state[2 * 4 + 3] = plain[14];
|
|
113
|
+
|
|
114
|
+
state[3 * 4 + 0] = plain[3];
|
|
115
|
+
state[3 * 4 + 1] = plain[7];
|
|
116
|
+
state[3 * 4 + 2] = plain[11];
|
|
117
|
+
state[3 * 4 + 3] = plain[15];
|
|
118
|
+
|
|
119
|
+
AddRoundKey(state, RoundedKeys);
|
|
120
|
+
|
|
121
|
+
for (uint8_t round = 1; round <= Nr - 1; round++) {
|
|
122
|
+
SubBytes(state);
|
|
123
|
+
ShiftRows(state);
|
|
124
|
+
MixColumns(state);
|
|
125
|
+
AddRoundKey(state, RoundedKeys + round * 4 * Nb);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
SubBytes(state);
|
|
129
|
+
ShiftRows(state);
|
|
130
|
+
AddRoundKey(state, RoundedKeys + Nr * 4 * Nb);
|
|
131
|
+
|
|
132
|
+
cipher[0] = state[0 * 4 + 0];
|
|
133
|
+
cipher[4] = state[0 * 4 + 1];
|
|
134
|
+
cipher[8] = state[0 * 4 + 2];
|
|
135
|
+
cipher[12] = state[0 * 4 + 3];
|
|
136
|
+
|
|
137
|
+
cipher[1] = state[1 * 4 + 0];
|
|
138
|
+
cipher[5] = state[1 * 4 + 1];
|
|
139
|
+
cipher[9] = state[1 * 4 + 2];
|
|
140
|
+
cipher[13] = state[1 * 4 + 3];
|
|
141
|
+
|
|
142
|
+
cipher[2] = state[2 * 4 + 0];
|
|
143
|
+
cipher[6] = state[2 * 4 + 1];
|
|
144
|
+
cipher[10] = state[2 * 4 + 2];
|
|
145
|
+
cipher[14] = state[2 * 4 + 3];
|
|
146
|
+
|
|
147
|
+
cipher[3] = state[3 * 4 + 0];
|
|
148
|
+
cipher[7] = state[3 * 4 + 1];
|
|
149
|
+
cipher[11] = state[3 * 4 + 2];
|
|
150
|
+
cipher[15] = state[3 * 4 + 3];
|
|
151
|
+
#endif
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
void AES::DecryptBlock(Bytes *cipher, Bytes *recover) {
|
|
155
|
+
#ifdef USE_AESNI
|
|
156
|
+
// load the current block & current round key into the registers
|
|
157
|
+
__m128i state = _mm_loadu_si128((__m128i *) &cipher[0]);
|
|
158
|
+
|
|
159
|
+
// first round
|
|
160
|
+
state = _mm_xor_si128(state, DecryptionRoundedKeys[Nr]);
|
|
161
|
+
|
|
162
|
+
// usual rounds
|
|
163
|
+
for (size_t i = Nr - 1; i > 1; i -= 2) {
|
|
164
|
+
state = _mm_aesdec_si128(state, DecryptionRoundedKeys[i]);
|
|
165
|
+
state = _mm_aesdec_si128(state, DecryptionRoundedKeys[i - 1]);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// last round
|
|
169
|
+
state = _mm_aesdec_si128(state, DecryptionRoundedKeys[1]);
|
|
170
|
+
state = _mm_aesdeclast_si128(state, DecryptionRoundedKeys[0]);
|
|
171
|
+
|
|
172
|
+
// store from register to array
|
|
173
|
+
_mm_storeu_si128((__m128i *) recover, state);
|
|
174
|
+
#elif defined(USE_ARM_AES) && defined(__aarch64__)
|
|
175
|
+
uint8x16_t state = vld1q_u8(cipher);
|
|
176
|
+
|
|
177
|
+
// Initial round
|
|
178
|
+
state = vaesimcq_u8(vaesdq_u8(state, DecryptionRoundedKeys[Nr]));
|
|
179
|
+
|
|
180
|
+
// 8 main rounds
|
|
181
|
+
for (size_t i = Nr - 1; i > 1; i -= 2) {
|
|
182
|
+
state = vaesimcq_u8(vaesdq_u8(state, DecryptionRoundedKeys[i]));
|
|
183
|
+
state = vaesimcq_u8(vaesdq_u8(state, DecryptionRoundedKeys[i - 1]));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// final 2 rounds
|
|
187
|
+
state = vaesdq_u8(state, DecryptionRoundedKeys[1]);
|
|
188
|
+
state = veorq_u8(state, DecryptionRoundedKeys[0]);
|
|
189
|
+
|
|
190
|
+
// store the result to recover
|
|
191
|
+
vst1q_u8(recover, state);
|
|
192
|
+
#else
|
|
193
|
+
Bytes state[16];
|
|
194
|
+
|
|
195
|
+
state[0 * 4 + 0] = cipher[0];
|
|
196
|
+
state[0 * 4 + 1] = cipher[4];
|
|
197
|
+
state[0 * 4 + 2] = cipher[8];
|
|
198
|
+
state[0 * 4 + 3] = cipher[12];
|
|
199
|
+
state[1 * 4 + 0] = cipher[1];
|
|
200
|
+
state[1 * 4 + 1] = cipher[5];
|
|
201
|
+
state[1 * 4 + 2] = cipher[9];
|
|
202
|
+
state[1 * 4 + 3] = cipher[13];
|
|
203
|
+
state[2 * 4 + 0] = cipher[2];
|
|
204
|
+
state[2 * 4 + 1] = cipher[6];
|
|
205
|
+
state[2 * 4 + 2] = cipher[10];
|
|
206
|
+
state[2 * 4 + 3] = cipher[14];
|
|
207
|
+
state[3 * 4 + 0] = cipher[3];
|
|
208
|
+
state[3 * 4 + 1] = cipher[7];
|
|
209
|
+
state[3 * 4 + 2] = cipher[11];
|
|
210
|
+
state[3 * 4 + 3] = cipher[15];
|
|
211
|
+
|
|
212
|
+
AddRoundKey(state, RoundedKeys + Nr * 4 * Nb);
|
|
213
|
+
|
|
214
|
+
for (uint8_t round = Nr - 1; round >= 1; round--) {
|
|
215
|
+
InvSubBytes(state);
|
|
216
|
+
InvShiftRows(state);
|
|
217
|
+
AddRoundKey(state, RoundedKeys + round * 4 * Nb);
|
|
218
|
+
InvMixColumns(state);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
InvSubBytes(state);
|
|
222
|
+
InvShiftRows(state);
|
|
223
|
+
AddRoundKey(state, RoundedKeys);
|
|
224
|
+
|
|
225
|
+
recover[0] = state[0 * 4 + 0];
|
|
226
|
+
recover[4] = state[0 * 4 + 1];
|
|
227
|
+
recover[8] = state[0 * 4 + 2];
|
|
228
|
+
recover[12] = state[0 * 4 + 3];
|
|
229
|
+
recover[1] = state[1 * 4 + 0];
|
|
230
|
+
recover[5] = state[1 * 4 + 1];
|
|
231
|
+
recover[9] = state[1 * 4 + 2];
|
|
232
|
+
recover[13] = state[1 * 4 + 3];
|
|
233
|
+
recover[2] = state[2 * 4 + 0];
|
|
234
|
+
recover[6] = state[2 * 4 + 1];
|
|
235
|
+
recover[10] = state[2 * 4 + 2];
|
|
236
|
+
recover[14] = state[2 * 4 + 3];
|
|
237
|
+
recover[3] = state[3 * 4 + 0];
|
|
238
|
+
recover[7] = state[3 * 4 + 1];
|
|
239
|
+
recover[11] = state[3 * 4 + 2];
|
|
240
|
+
recover[15] = state[3 * 4 + 3];
|
|
241
|
+
|
|
242
|
+
#endif
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void AES::KeyExpansion(const Bytes *key) {
|
|
246
|
+
Bytes *w = new Bytes[4 * Nb * (Nr + 1)];
|
|
247
|
+
Bytes *temp = new Bytes[4];
|
|
248
|
+
Bytes *rcon = new Bytes[4];
|
|
249
|
+
|
|
250
|
+
size_t i = 0;
|
|
251
|
+
while (i < 4 * Nk) {
|
|
252
|
+
w[i] = key[i];
|
|
253
|
+
i++;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
i = 4 * Nk;
|
|
257
|
+
while (i < 4 * Nb * (Nr + 1)) {
|
|
258
|
+
temp[0] = w[i - 4 + 0];
|
|
259
|
+
temp[1] = w[i - 4 + 1];
|
|
260
|
+
temp[2] = w[i - 4 + 2];
|
|
261
|
+
temp[3] = w[i - 4 + 3];
|
|
262
|
+
|
|
263
|
+
if (i / 4 % Nk == 0) {
|
|
264
|
+
RotDWord(temp);
|
|
265
|
+
SubDWordBytes(temp);
|
|
266
|
+
Rcon(rcon, i / (Nk * 4));
|
|
267
|
+
XorDWords(temp, rcon, temp);
|
|
268
|
+
} else if (Nk > 6 && i / 4 % Nk == 4) {
|
|
269
|
+
SubDWordBytes(temp);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
w[i + 0] = w[i - 4 * Nk] ^ temp[0];
|
|
273
|
+
w[i + 1] = w[i + 1 - 4 * Nk] ^ temp[1];
|
|
274
|
+
w[i + 2] = w[i + 2 - 4 * Nk] ^ temp[2];
|
|
275
|
+
w[i + 3] = w[i + 3 - 4 * Nk] ^ temp[3];
|
|
276
|
+
i += 4;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
delete[] rcon;
|
|
280
|
+
delete[] temp;
|
|
281
|
+
|
|
282
|
+
#ifdef USE_AESNI
|
|
283
|
+
size_t RoundKeySize = Nr + 1;
|
|
284
|
+
RoundedKeys = new __m128i[RoundKeySize];
|
|
285
|
+
DecryptionRoundedKeys = new __m128i[RoundKeySize];
|
|
286
|
+
|
|
287
|
+
RoundedKeys[0] = _mm_loadu_si128((__m128i *) &w[0]);
|
|
288
|
+
DecryptionRoundedKeys[0] = RoundedKeys[0];
|
|
289
|
+
|
|
290
|
+
for (size_t i = 1; i < RoundKeySize; ++i) {
|
|
291
|
+
RoundedKeys[i] = _mm_loadu_si128((__m128i *) &w[i * 4 * Nb]);
|
|
292
|
+
DecryptionRoundedKeys[i] = _mm_aesimc_si128(RoundedKeys[i]);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
RoundedKeys[Nr] = _mm_loadu_si128((__m128i *) &w[Nr * 4 * Nb]);
|
|
296
|
+
DecryptionRoundedKeys[Nr] = RoundedKeys[Nr];
|
|
297
|
+
|
|
298
|
+
delete[] w;
|
|
299
|
+
#elif defined(USE_ARM_AES) && defined(__aarch64__)
|
|
300
|
+
size_t RoundKeySize = Nr + 1;
|
|
301
|
+
RoundedKeys = new uint8x16_t[RoundKeySize];
|
|
302
|
+
DecryptionRoundedKeys = new uint8x16_t[RoundKeySize];
|
|
303
|
+
|
|
304
|
+
RoundedKeys[0] = vld1q_u8(&w[0]);
|
|
305
|
+
DecryptionRoundedKeys[0] = vld1q_u8(&w[0]);
|
|
306
|
+
|
|
307
|
+
for (size_t i = 1; i < RoundKeySize; ++i) {
|
|
308
|
+
RoundedKeys[i] = vld1q_u8(&w[i * 4 * Nb]);
|
|
309
|
+
DecryptionRoundedKeys[i] = vaesimcq_u8(RoundedKeys[i]);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
RoundedKeys[Nr] = vld1q_u8(&w[Nr * 4 * Nb]);
|
|
313
|
+
DecryptionRoundedKeys[Nr] = RoundedKeys[Nr];
|
|
314
|
+
|
|
315
|
+
delete[] w;
|
|
316
|
+
#else
|
|
317
|
+
RoundedKeys = w;
|
|
318
|
+
#endif
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
void AES::MixColumns(unsigned char state[16]) {
|
|
322
|
+
unsigned char temp_state[4 * 4 + 4];
|
|
323
|
+
|
|
324
|
+
memset(temp_state, 0x00, 4 * 4);
|
|
325
|
+
|
|
326
|
+
for (size_t i = 0; i < 4; ++i) {
|
|
327
|
+
for (size_t k = 0; k < 4; ++k) {
|
|
328
|
+
for (size_t j = 0; j < 4; ++j) {
|
|
329
|
+
if (CMDS[i][k] == 1) {
|
|
330
|
+
temp_state[i * 4 + j] ^= state[k * 4 + j];
|
|
331
|
+
} else {
|
|
332
|
+
temp_state[i * 4 + j] ^= GF_MUL_TABLE[CMDS[i][k]][state[k * 4 + j]];
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
memcpy(state, temp_state, 4 * 4);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
void AES::InvMixColumns(unsigned char state[16]) {
|
|
342
|
+
unsigned char temp_state[4 * 4 + 4];
|
|
343
|
+
|
|
344
|
+
memset(temp_state, 0x00, 4 * 4);
|
|
345
|
+
|
|
346
|
+
for (size_t i = 0; i < 4; ++i) {
|
|
347
|
+
for (size_t k = 0; k < 4; ++k) {
|
|
348
|
+
for (size_t j = 0; j < 4; ++j) {
|
|
349
|
+
temp_state[i * 4 + j] ^= GF_MUL_TABLE[INV_CMDS[i][k]][state[k * 4 + j]];
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
memcpy(state, temp_state, 4 * 4);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
void AES::setKey(const Bytes *ByteArray, size_t keyLen) {
|
|
358
|
+
switch (keyLen) {
|
|
359
|
+
case 16: // AES128
|
|
360
|
+
this->Nk = 4;
|
|
361
|
+
this->Nr = 10;
|
|
362
|
+
break;
|
|
363
|
+
case 24: // AES192
|
|
364
|
+
this->Nk = 6;
|
|
365
|
+
this->Nr = 12;
|
|
366
|
+
break;
|
|
367
|
+
case 32: // AES256
|
|
368
|
+
this->Nk = 8;
|
|
369
|
+
this->Nr = 14;
|
|
370
|
+
break;
|
|
371
|
+
default:
|
|
372
|
+
throw std::invalid_argument("Incorrect key length");
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
KeyExpansion(ByteArray);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
AES::AES(const Bytes *ByteArray, size_t keyLen) : BASE_BLOCKCIPHER(16) {
|
|
379
|
+
setKey(ByteArray, keyLen);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
AES::~AES() {
|
|
383
|
+
if (RoundedKeys != NULL) {
|
|
384
|
+
delete[] RoundedKeys;
|
|
385
|
+
|
|
386
|
+
#if defined(USE_AESNI) || (defined(USE_ARM_AES) && defined(__aarch64__))
|
|
387
|
+
delete[] DecryptionRoundedKeys;
|
|
388
|
+
#endif
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
void AES::ShiftRows(unsigned char state[16]) {
|
|
393
|
+
// row 2
|
|
394
|
+
unsigned char buffer = state[1 * 4 + 0];
|
|
395
|
+
memmove(&state[4], &state[4] + 1, sizeof(unsigned char) * 3);
|
|
396
|
+
state[1 * 4 + 3] = buffer;
|
|
397
|
+
|
|
398
|
+
// row 3
|
|
399
|
+
unsigned char thrid[2];
|
|
400
|
+
memcpy(thrid, &state[8], sizeof(unsigned char) * 2);
|
|
401
|
+
memcpy(&state[8], &state[8] + 2, sizeof(unsigned char) * 2);
|
|
402
|
+
memcpy(&state[8] + 2, thrid, sizeof(unsigned char) * 2);
|
|
403
|
+
|
|
404
|
+
// row 4
|
|
405
|
+
buffer = state[3 * 4 + 3];
|
|
406
|
+
memmove(&state[12] + 1, &state[12], sizeof(unsigned char) * 3);
|
|
407
|
+
state[3 * 4 + 0] = buffer;
|
|
408
|
+
|
|
409
|
+
/*
|
|
410
|
+
unsigned char temp;
|
|
411
|
+
|
|
412
|
+
//Row 2
|
|
413
|
+
temp = state[1 * 4 + 0];
|
|
414
|
+
state[1 * 4 + 0] = state[1 * 4 + 1];
|
|
415
|
+
state[1 * 4 + 1] = state[1 * 4 + 2];
|
|
416
|
+
state[1 * 4 + 2] = state[1 * 4 + 3];
|
|
417
|
+
state[1 * 4 + 3] = temp;
|
|
418
|
+
|
|
419
|
+
//Row 3
|
|
420
|
+
temp = state[2 * 4 + 0];
|
|
421
|
+
state[ * 4 + [0] = state[2 * 4 + 2];
|
|
422
|
+
state[2 * 4 + 2] = temp;
|
|
423
|
+
temp = state[2 * 4 + 1];
|
|
424
|
+
state[2 * 4 + 1] = state[2 * 4 + 3];
|
|
425
|
+
state[2 * 4 + 3] = temp;
|
|
426
|
+
|
|
427
|
+
//Row 4
|
|
428
|
+
temp = state[3 * 4 + 0];
|
|
429
|
+
state[3 * 4 + 0] = state[3 * 4 + 3];
|
|
430
|
+
state[3 * 4 + 3] = state[3 * 4 + 2];
|
|
431
|
+
state[3 * 4 + 2] = state[3 * 4 + 1];
|
|
432
|
+
state[3 * 4 + 1] = temp;
|
|
433
|
+
*/
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
void AES::InvShiftRows(unsigned char state[16]) {
|
|
437
|
+
// row 2
|
|
438
|
+
unsigned char buffer = state[1 * 4 + 3];
|
|
439
|
+
memmove(&state[4] + 1, &state[4], sizeof(unsigned char) * 3);
|
|
440
|
+
state[1 * 4 + 0] = buffer;
|
|
441
|
+
|
|
442
|
+
// row 3
|
|
443
|
+
unsigned char thrid[2];
|
|
444
|
+
memcpy(thrid, &state[8], sizeof(unsigned char) * 2);
|
|
445
|
+
memcpy(&state[8], &state[8] + 2, sizeof(unsigned char) * 2);
|
|
446
|
+
memcpy(&state[8] + 2, thrid, sizeof(unsigned char) * 2);
|
|
447
|
+
|
|
448
|
+
// row 4
|
|
449
|
+
buffer = state[3 * 4 + 0];
|
|
450
|
+
memmove(&state[12], &state[12] + 1, sizeof(unsigned char) * 3);
|
|
451
|
+
state[3 * 4 + 3] = buffer;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
void AES::SubBytes(unsigned char state[16]) {
|
|
455
|
+
state[0 * 4 + 0] = sbox[state[0 * 4 + 0]];
|
|
456
|
+
state[0 * 4 + 1] = sbox[state[0 * 4 + 1]];
|
|
457
|
+
state[0 * 4 + 2] = sbox[state[0 * 4 + 2]];
|
|
458
|
+
state[0 * 4 + 3] = sbox[state[0 * 4 + 3]];
|
|
459
|
+
|
|
460
|
+
state[1 * 4 + 0] = sbox[state[1 * 4 + 0]];
|
|
461
|
+
state[1 * 4 + 1] = sbox[state[1 * 4 + 1]];
|
|
462
|
+
state[1 * 4 + 2] = sbox[state[1 * 4 + 2]];
|
|
463
|
+
state[1 * 4 + 3] = sbox[state[1 * 4 + 3]];
|
|
464
|
+
|
|
465
|
+
state[2 * 4 + 0] = sbox[state[2 * 4 + 0]];
|
|
466
|
+
state[2 * 4 + 1] = sbox[state[2 * 4 + 1]];
|
|
467
|
+
state[2 * 4 + 2] = sbox[state[2 * 4 + 2]];
|
|
468
|
+
state[2 * 4 + 3] = sbox[state[2 * 4 + 3]];
|
|
469
|
+
|
|
470
|
+
state[3 * 4 + 0] = sbox[state[3 * 4 + 0]];
|
|
471
|
+
state[3 * 4 + 1] = sbox[state[3 * 4 + 1]];
|
|
472
|
+
state[3 * 4 + 2] = sbox[state[3 * 4 + 2]];
|
|
473
|
+
state[3 * 4 + 3] = sbox[state[3 * 4 + 3]];
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
void AES::InvSubBytes(unsigned char state[16]) {
|
|
477
|
+
state[0 * 4 + 0] = inv_sbox[state[0 * 4 + 0]];
|
|
478
|
+
state[0 * 4 + 1] = inv_sbox[state[0 * 4 + 1]];
|
|
479
|
+
state[0 * 4 + 2] = inv_sbox[state[0 * 4 + 2]];
|
|
480
|
+
state[0 * 4 + 3] = inv_sbox[state[0 * 4 + 3]];
|
|
481
|
+
|
|
482
|
+
state[1 * 4 + 0] = inv_sbox[state[1 * 4 + 0]];
|
|
483
|
+
state[1 * 4 + 1] = inv_sbox[state[1 * 4 + 1]];
|
|
484
|
+
state[1 * 4 + 2] = inv_sbox[state[1 * 4 + 2]];
|
|
485
|
+
state[1 * 4 + 3] = inv_sbox[state[1 * 4 + 3]];
|
|
486
|
+
|
|
487
|
+
state[2 * 4 + 0] = inv_sbox[state[2 * 4 + 0]];
|
|
488
|
+
state[2 * 4 + 1] = inv_sbox[state[2 * 4 + 1]];
|
|
489
|
+
state[2 * 4 + 2] = inv_sbox[state[2 * 4 + 2]];
|
|
490
|
+
state[2 * 4 + 3] = inv_sbox[state[2 * 4 + 3]];
|
|
491
|
+
|
|
492
|
+
state[3 * 4 + 0] = inv_sbox[state[3 * 4 + 0]];
|
|
493
|
+
state[3 * 4 + 1] = inv_sbox[state[3 * 4 + 1]];
|
|
494
|
+
state[3 * 4 + 2] = inv_sbox[state[3 * 4 + 2]];
|
|
495
|
+
state[3 * 4 + 3] = inv_sbox[state[3 * 4 + 3]];
|
|
496
|
+
}
|
|
497
|
+
} // namespace BlockCipher
|
|
498
|
+
} // namespace Krypt
|
|
499
|
+
|
|
500
|
+
#endif
|
|
501
|
+
|
|
502
|
+
#endif
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2019 SergeyBel
|
|
5
|
+
* Copyright (c) 2023 Jubal Mordecai Velasco
|
|
6
|
+
* Copyright (c) 2025 Sergei Kazakov
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
#pragma once
|
|
28
|
+
|
|
29
|
+
#ifdef RNFSTURBO_USE_ENCRYPTION
|
|
30
|
+
|
|
31
|
+
#ifndef KRYPT_AES_HPP
|
|
32
|
+
#define KRYPT_AES_HPP
|
|
33
|
+
|
|
34
|
+
#include <cstring>
|
|
35
|
+
#include <exception>
|
|
36
|
+
#include <iostream>
|
|
37
|
+
#include "blockcipher.hpp"
|
|
38
|
+
#include "functions.hpp"
|
|
39
|
+
|
|
40
|
+
#ifdef USE_AESNI
|
|
41
|
+
#include <immintrin.h>
|
|
42
|
+
#include <stdio.h>
|
|
43
|
+
#elif defined(USE_ARM_AES)
|
|
44
|
+
#if defined(__arm__) || defined(__aarch32__) || defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM)
|
|
45
|
+
#if defined(__GNUC__)
|
|
46
|
+
#include <stdint.h>
|
|
47
|
+
#endif
|
|
48
|
+
|
|
49
|
+
#if defined(__ARM_NEON) || defined(_MSC_VER)
|
|
50
|
+
#include <arm_neon.h>
|
|
51
|
+
#endif
|
|
52
|
+
|
|
53
|
+
/* GCC and LLVM Clang, but not Apple Clang */
|
|
54
|
+
#if defined(__GNUC__) && !defined(__apple_build_version__)
|
|
55
|
+
#if defined(__ARM_ACLE) || defined(__ARM_FEATURE_CRYPTO)
|
|
56
|
+
#include <arm_acle.h>
|
|
57
|
+
#endif
|
|
58
|
+
#endif
|
|
59
|
+
#endif
|
|
60
|
+
#endif
|
|
61
|
+
|
|
62
|
+
namespace Krypt {
|
|
63
|
+
namespace BlockCipher {
|
|
64
|
+
/// Advance Encryption Standard.
|
|
65
|
+
///
|
|
66
|
+
/// provides method of encryption and decryption.
|
|
67
|
+
/// @warning the method of this class only operates on a 16 byte array of unsigned char* or Bytes*
|
|
68
|
+
/// if you pass an array of unsigned char* in the method with less than 16 elements in it that will cause an
|
|
69
|
+
/// access overflow.
|
|
70
|
+
class AES : public BASE_BLOCKCIPHER {
|
|
71
|
+
private:
|
|
72
|
+
|
|
73
|
+
const static size_t Nb = 4;
|
|
74
|
+
size_t Nk;
|
|
75
|
+
size_t Nr;
|
|
76
|
+
#ifdef USE_AESNI
|
|
77
|
+
__m128i *RoundedKeys;
|
|
78
|
+
__m128i *DecryptionRoundedKeys;
|
|
79
|
+
#elif defined(USE_ARM_AES) && defined(__aarch64__)
|
|
80
|
+
uint8x16_t *RoundedKeys;
|
|
81
|
+
uint8x16_t *DecryptionRoundedKeys;
|
|
82
|
+
#else
|
|
83
|
+
Bytes *RoundedKeys;
|
|
84
|
+
#endif
|
|
85
|
+
#if defined(USE_ARM_AES) && defined(__aarch64__)
|
|
86
|
+
__attribute__((__target__("+crypto+aes")))
|
|
87
|
+
#endif
|
|
88
|
+
void KeyExpansion(const Bytes *key);
|
|
89
|
+
|
|
90
|
+
void SubBytes(unsigned char state[16]);
|
|
91
|
+
void InvSubBytes(unsigned char state[16]);
|
|
92
|
+
|
|
93
|
+
void ShiftRows(unsigned char state[16]);
|
|
94
|
+
void InvShiftRows(unsigned char state[16]);
|
|
95
|
+
|
|
96
|
+
void MixColumns(unsigned char state[16]);
|
|
97
|
+
void InvMixColumns(unsigned char state[16]);
|
|
98
|
+
|
|
99
|
+
void AddRoundKey(unsigned char state[16], unsigned char *key);
|
|
100
|
+
|
|
101
|
+
public:
|
|
102
|
+
|
|
103
|
+
/// encrypts a fixed 16 byte block from `plain` into `cipher`.
|
|
104
|
+
/// @param plain a pointer to an array of unsigned char*/Bytes* in heap this is
|
|
105
|
+
/// the array you want to encrypt.
|
|
106
|
+
/// @param cipher another pointer to an array of unsigned char*/Bytes* in heap
|
|
107
|
+
/// this is the array where the encrypted block will be stored.
|
|
108
|
+
#if defined(USE_ARM_AES) && defined(__aarch64__)
|
|
109
|
+
__attribute__((__target__("+crypto+aes")))
|
|
110
|
+
#endif
|
|
111
|
+
void EncryptBlock(Bytes *plain, Bytes *cipher) override;
|
|
112
|
+
|
|
113
|
+
/// decrypts a fixed 16 byte block from `cipher` into `recover`.
|
|
114
|
+
/// @param cipher a pointer to an array of unsigned char*/Bytes* in heap this is
|
|
115
|
+
/// the array you want to decrypt.
|
|
116
|
+
/// @param recover another pointer to an array of unsigned char*/Bytes* in heap
|
|
117
|
+
/// this is the array where the decrypted block will be stored.
|
|
118
|
+
#if defined(USE_ARM_AES) && defined(__aarch64__)
|
|
119
|
+
__attribute__((__target__("+crypto+aes")))
|
|
120
|
+
#endif
|
|
121
|
+
void DecryptBlock(Bytes *cipher, Bytes *recover) override;
|
|
122
|
+
|
|
123
|
+
/// initialize the round key from a key.
|
|
124
|
+
///
|
|
125
|
+
/// @note this function is automatically called when you initialize and object of AES,
|
|
126
|
+
/// the only time you want to use or call this member method is when you want to change the
|
|
127
|
+
/// key and the roundKeys of an instance of an AES class.
|
|
128
|
+
void setKey(const Bytes *key, size_t keyLen);
|
|
129
|
+
|
|
130
|
+
/// @return 0 - portable, 1 - neon (aarch64, armv8), 2 - AES-NI (x86-64)
|
|
131
|
+
static constexpr int aes_implementation() {
|
|
132
|
+
#ifdef USE_AESNI
|
|
133
|
+
return 2;
|
|
134
|
+
#elif defined(USE_ARM_AES) && defined(__arm__) && defined(__aarch64__)
|
|
135
|
+
return 1;
|
|
136
|
+
#else
|
|
137
|
+
return 0;
|
|
138
|
+
#endif
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// constructor for AES.
|
|
142
|
+
AES(const Bytes *ByteArray, size_t keyLen);
|
|
143
|
+
~AES();
|
|
144
|
+
};
|
|
145
|
+
} // namespace BlockCipher
|
|
146
|
+
} // namespace Krypt
|
|
147
|
+
|
|
148
|
+
#endif
|
|
149
|
+
|
|
150
|
+
#endif
|