react-native-fs-turbo 0.3.7 → 0.4.0

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.
Files changed (66) hide show
  1. package/README.md +119 -27
  2. package/RNFSTurbo.podspec +2 -2
  3. package/android/CMakeLists.txt +62 -9
  4. package/android/build.gradle +80 -3
  5. package/android/gradle.properties +5 -5
  6. package/android/src/main/codegen/RNFSTurboSpec.java +17 -0
  7. package/android/src/main/cpp/JNIOnLoad.cpp +18 -0
  8. package/android/src/main/cpp/RNFSTurboLogger.cpp +4 -0
  9. package/android/src/main/cpp/RNFSTurboModule.cpp +44 -0
  10. package/android/src/main/cpp/RNFSTurboModule.h +40 -0
  11. package/android/src/main/cpp/RNFSTurboPlatformHelper.cpp +5 -1
  12. package/android/src/main/java/com/cmpayc/rnfsturbo/JNIOnLoad.java +27 -0
  13. package/android/src/main/java/com/cmpayc/rnfsturbo/{RNFSTurboPlatformContextModule.java → RNFSTurboModule.java} +62 -11
  14. package/android/src/main/java/com/cmpayc/rnfsturbo/RNFSTurboPackage.java +11 -9
  15. package/cpp/RNFSTurboHostObject.cpp +138 -15
  16. package/cpp/RNFSTurboHostObject.h +12 -4
  17. package/cpp/RNFSTurboInstall.cpp +27 -0
  18. package/cpp/RNFSTurboInstall.h +20 -0
  19. package/cpp/RNFSTurboLogger.h +4 -0
  20. package/cpp/RNFSTurboPlatformHelper.h +4 -0
  21. package/cpp/algorithms/Krypt/AES.cpp +502 -0
  22. package/cpp/algorithms/Krypt/bytearray.cpp +158 -0
  23. package/cpp/algorithms/Krypt/functions.cpp +137 -0
  24. package/cpp/algorithms/Krypt/mode.cpp +203 -0
  25. package/cpp/algorithms/Krypt/padding.cpp +219 -0
  26. package/cpp/encryption/encryption-utils.cpp +162 -0
  27. package/cpp/encryption/encryption-utils.h +43 -0
  28. package/cpp/filesystem/{helpers.cpp → filesystem-utils.cpp} +5 -1
  29. package/cpp/filesystem/{helpers.h → filesystem-utils.h} +4 -0
  30. package/ios/RNFSTurboLogger.mm +4 -0
  31. package/ios/{RNFSTurboPlatformContextModule.h → RNFSTurboModule.h} +3 -3
  32. package/ios/{RNFSTurboPlatformContextModule.mm → RNFSTurboModule.mm} +22 -5
  33. package/ios/RNFSTurboPlatformHelper.mm +4 -0
  34. package/lib/commonjs/NativeRNFSTurboModule.js +14 -16
  35. package/lib/commonjs/NativeRNFSTurboModule.js.map +1 -1
  36. package/lib/commonjs/createRNFSTurbo.js +3 -2
  37. package/lib/commonjs/createRNFSTurbo.js.map +1 -1
  38. package/lib/commonjs/globals.d.js +6 -0
  39. package/lib/commonjs/globals.d.js.map +1 -0
  40. package/lib/module/NativeRNFSTurboModule.js +14 -16
  41. package/lib/module/NativeRNFSTurboModule.js.map +1 -1
  42. package/lib/module/createRNFSTurbo.js +3 -2
  43. package/lib/module/createRNFSTurbo.js.map +1 -1
  44. package/lib/module/globals.d.js +4 -0
  45. package/lib/module/globals.d.js.map +1 -0
  46. package/lib/typescript/NativeRNFSTurboModule.d.ts +12 -3
  47. package/lib/typescript/NativeRNFSTurboModule.d.ts.map +1 -1
  48. package/lib/typescript/Types.d.ts +11 -0
  49. package/lib/typescript/Types.d.ts.map +1 -1
  50. package/lib/typescript/createRNFSTurbo.d.ts.map +1 -1
  51. package/package.json +6 -3
  52. package/react-native.config.js +1 -5
  53. package/src/NativeRNFSTurboModule.ts +25 -21
  54. package/src/Types.ts +11 -0
  55. package/src/createRNFSTurbo.ts +4 -2
  56. package/src/globals.d.ts +9 -0
  57. package/cpp/NativeRNFSTurboModule.cpp +0 -24
  58. package/cpp/NativeRNFSTurboModule.h +0 -31
  59. package/ios/RNFSTurboOnLoad.mm +0 -25
  60. package/lib/commonjs/NativeRNFSTurboPlatformContextModule.js +0 -22
  61. package/lib/commonjs/NativeRNFSTurboPlatformContextModule.js.map +0 -1
  62. package/lib/module/NativeRNFSTurboPlatformContextModule.js +0 -18
  63. package/lib/module/NativeRNFSTurboPlatformContextModule.js.map +0 -1
  64. package/lib/typescript/NativeRNFSTurboPlatformContextModule.d.ts +0 -16
  65. package/lib/typescript/NativeRNFSTurboPlatformContextModule.d.ts.map +0 -1
  66. 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,158 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2019 SergeyBel
5
+ * Copyright (c) 2023 Jubal Mordecai Velasco
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifdef RNFSTURBO_USE_ENCRYPTION
27
+
28
+ #ifndef KRYPT_BYTEARRAY_CPP
29
+ #define KRYPT_BYTEARRAY_CPP
30
+
31
+ #include "bytearray.hpp"
32
+
33
+ namespace Krypt {
34
+ // returns the pointer array, leaving the .array() equal to NULL, and .length() equal to zero
35
+ Bytes *ByteArray::detach() {
36
+ Bytes *ptr = array;
37
+ array = NULL;
38
+ length = 0;
39
+ return ptr;
40
+ }
41
+
42
+ const Bytes &ByteArray::operator[](size_t i) const {
43
+ #ifndef INDEX_CHECK_DISABLE
44
+ if (length == 0) {
45
+ throw std::underflow_error(
46
+ "Krypt::ByteArray[] :"
47
+ " accessing an empty byte array"
48
+ );
49
+ }
50
+
51
+ if (i >= length) {
52
+ throw std::overflow_error(
53
+ "Krypt::ByteArray[] :"
54
+ " index given for operator[] is greater-than ByteArray.length()-1"
55
+ );
56
+ }
57
+ #endif
58
+ return array[i];
59
+ }
60
+
61
+ Bytes &ByteArray::operator[](size_t i) {
62
+ #ifndef INDEX_CHECK_DISABLE
63
+ if (length == 0) {
64
+ throw std::underflow_error(
65
+ "Krypt::ByteArray[] :"
66
+ " accessing an empty byte array"
67
+ );
68
+ }
69
+
70
+ if (i >= length) {
71
+ throw std::overflow_error(
72
+ "Krypt::ByteArray[] :"
73
+ " index given for operator[] is greater-than ByteArray.length()-1"
74
+ );
75
+ }
76
+ #endif
77
+ return array[i];
78
+ }
79
+
80
+ ByteArray::ByteArray() {
81
+ array = NULL;
82
+ length = 0;
83
+ }
84
+
85
+ ByteArray::ByteArray(Bytes *heap_obj, size_t length) {
86
+ array = heap_obj;
87
+ this->length = length;
88
+ }
89
+
90
+ // copy constructor
91
+ ByteArray::ByteArray(const ByteArray &other) : array(new Bytes[other.length]), length(other.length) {
92
+ memcpy(array, other.array, other.length);
93
+ }
94
+
95
+ // move constructor
96
+ ByteArray::ByteArray(ByteArray &&other) noexcept : array(other.array), length(other.length) {
97
+ other.array = NULL;
98
+ other.length = 0;
99
+ }
100
+
101
+ // copy assignment
102
+ ByteArray &ByteArray::operator=(const ByteArray &other) {
103
+ if (this != &other) {
104
+ if (array != NULL) {
105
+ memset((Bytes *) array, 0x00, length);
106
+ delete[] array;
107
+ }
108
+
109
+ array = new Bytes[other.length];
110
+ length = other.length;
111
+ memcpy(array, other.array, other.length);
112
+ }
113
+ return *this;
114
+ }
115
+
116
+ // move assingment
117
+ ByteArray &ByteArray::operator=(ByteArray &&other) noexcept {
118
+ if (this != &other) {
119
+ if (array != NULL) {
120
+ memset((Bytes *) array, 0x00, length);
121
+ delete[] array;
122
+ }
123
+
124
+ array = other.array;
125
+ length = other.length;
126
+
127
+ other.array = NULL;
128
+ other.length = 0;
129
+ }
130
+ return *this;
131
+ }
132
+
133
+ ByteArray::~ByteArray() {
134
+ if (array != NULL) {
135
+ memset((Bytes *) array, 0x00, length);
136
+ delete[] array;
137
+ }
138
+ length = 0;
139
+ }
140
+
141
+ std::ostream &operator<<(std::ostream &outputStream, const ByteArray &instance) {
142
+ for (size_t i = 0; i < instance.length; ++i) {
143
+ outputStream << instance.array[i];
144
+ }
145
+ return outputStream;
146
+ }
147
+
148
+ std::istream &operator>>(std::istream &inputStream, ByteArray &instance) {
149
+ for (size_t i = 0; i < instance.length; ++i) {
150
+ inputStream >> instance.array[i];
151
+ }
152
+ return inputStream;
153
+ }
154
+ } // namespace Krypt
155
+
156
+ #endif
157
+
158
+ #endif