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.
Files changed (74) 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/AES.hpp +150 -0
  23. package/cpp/algorithms/Krypt/aes-config.hpp +71 -0
  24. package/cpp/algorithms/Krypt/blockcipher.hpp +69 -0
  25. package/cpp/algorithms/Krypt/bytearray.cpp +158 -0
  26. package/cpp/algorithms/Krypt/bytearray.hpp +80 -0
  27. package/cpp/algorithms/Krypt/functions.cpp +137 -0
  28. package/cpp/algorithms/Krypt/functions.hpp +54 -0
  29. package/cpp/algorithms/Krypt/mode.cpp +203 -0
  30. package/cpp/algorithms/Krypt/mode.hpp +131 -0
  31. package/cpp/algorithms/Krypt/padding.cpp +219 -0
  32. package/cpp/algorithms/Krypt/padding.hpp +148 -0
  33. package/cpp/algorithms/Krypt/types.hpp +208 -0
  34. package/cpp/encryption/encryption-utils.cpp +162 -0
  35. package/cpp/encryption/encryption-utils.h +43 -0
  36. package/cpp/filesystem/{helpers.cpp → filesystem-utils.cpp} +5 -1
  37. package/cpp/filesystem/{helpers.h → filesystem-utils.h} +4 -0
  38. package/ios/RNFSTurboLogger.mm +4 -0
  39. package/ios/{RNFSTurboPlatformContextModule.h → RNFSTurboModule.h} +3 -3
  40. package/ios/{RNFSTurboPlatformContextModule.mm → RNFSTurboModule.mm} +22 -5
  41. package/ios/RNFSTurboPlatformHelper.mm +4 -0
  42. package/lib/commonjs/NativeRNFSTurboModule.js +14 -16
  43. package/lib/commonjs/NativeRNFSTurboModule.js.map +1 -1
  44. package/lib/commonjs/createRNFSTurbo.js +3 -2
  45. package/lib/commonjs/createRNFSTurbo.js.map +1 -1
  46. package/lib/commonjs/globals.d.js +6 -0
  47. package/lib/commonjs/globals.d.js.map +1 -0
  48. package/lib/module/NativeRNFSTurboModule.js +14 -16
  49. package/lib/module/NativeRNFSTurboModule.js.map +1 -1
  50. package/lib/module/createRNFSTurbo.js +3 -2
  51. package/lib/module/createRNFSTurbo.js.map +1 -1
  52. package/lib/module/globals.d.js +4 -0
  53. package/lib/module/globals.d.js.map +1 -0
  54. package/lib/typescript/NativeRNFSTurboModule.d.ts +12 -3
  55. package/lib/typescript/NativeRNFSTurboModule.d.ts.map +1 -1
  56. package/lib/typescript/Types.d.ts +11 -0
  57. package/lib/typescript/Types.d.ts.map +1 -1
  58. package/lib/typescript/createRNFSTurbo.d.ts.map +1 -1
  59. package/package.json +7 -3
  60. package/react-native.config.js +1 -5
  61. package/src/NativeRNFSTurboModule.ts +25 -21
  62. package/src/Types.ts +11 -0
  63. package/src/createRNFSTurbo.ts +4 -2
  64. package/src/globals.d.ts +9 -0
  65. package/cpp/NativeRNFSTurboModule.cpp +0 -24
  66. package/cpp/NativeRNFSTurboModule.h +0 -31
  67. package/ios/RNFSTurboOnLoad.mm +0 -25
  68. package/lib/commonjs/NativeRNFSTurboPlatformContextModule.js +0 -22
  69. package/lib/commonjs/NativeRNFSTurboPlatformContextModule.js.map +0 -1
  70. package/lib/module/NativeRNFSTurboPlatformContextModule.js +0 -18
  71. package/lib/module/NativeRNFSTurboPlatformContextModule.js.map +0 -1
  72. package/lib/typescript/NativeRNFSTurboPlatformContextModule.d.ts +0 -16
  73. package/lib/typescript/NativeRNFSTurboPlatformContextModule.d.ts.map +0 -1
  74. package/src/NativeRNFSTurboPlatformContextModule.ts +0 -34
@@ -0,0 +1,203 @@
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_MODE_OF_ENCRYPTION_CPP
29
+ #define KRYPT_MODE_OF_ENCRYPTION_CPP
30
+
31
+ #include "functions.hpp"
32
+ #include "mode.hpp"
33
+ #include "padding.hpp"
34
+ #include "types.hpp"
35
+
36
+ namespace Krypt {
37
+ namespace Mode {
38
+ // CBC
39
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
40
+ CBC<CIPHER_TYPE, PADDING_TYPE>::CBC(const Bytes *key, size_t keyLen) : MODE() {
41
+ Encryption = new CIPHER_TYPE(key, keyLen);
42
+ PaddingScheme = new PADDING_TYPE();
43
+ }
44
+
45
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
46
+ ByteArray CBC<CIPHER_TYPE, PADDING_TYPE>::encrypt(Bytes *plain, size_t plainLen, Bytes *iv) {
47
+ ByteArray padded = PaddingScheme->AddPadding(plain, plainLen, Encryption->BLOCK_SIZE);
48
+ Bytes *tempIV = new Bytes[Encryption->BLOCK_SIZE];
49
+ Bytes *cipher = new Bytes[padded.length];
50
+
51
+ memcpy(tempIV, iv, Encryption->BLOCK_SIZE);
52
+
53
+ for (size_t i = 0; i < padded.length; i += Encryption->BLOCK_SIZE) {
54
+ this->blockEncrypt(padded.array + i, cipher + i, tempIV);
55
+ }
56
+
57
+ delete[] tempIV;
58
+ return ByteArray(cipher, padded.length);
59
+ }
60
+
61
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
62
+ ByteArray CBC<CIPHER_TYPE, PADDING_TYPE>::decrypt(Bytes *cipher, size_t cipherLen, Bytes *iv) {
63
+ Bytes *recover = new Bytes[cipherLen];
64
+ Bytes *tempIV = new Bytes[Encryption->BLOCK_SIZE];
65
+ memcpy(tempIV, iv, Encryption->BLOCK_SIZE);
66
+
67
+ for (size_t i = 0; i < cipherLen; i += Encryption->BLOCK_SIZE) {
68
+ this->blockDecrypt(cipher + i, recover + i, tempIV);
69
+ }
70
+
71
+ ByteArray recoverNoPadding = PaddingScheme->RemovePadding(recover, cipherLen, Encryption->BLOCK_SIZE);
72
+
73
+ delete[] tempIV;
74
+ delete[] recover;
75
+
76
+ return recoverNoPadding;
77
+ }
78
+
79
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
80
+ void CBC<CIPHER_TYPE, PADDING_TYPE>::blockEncrypt(Bytes *plain, Bytes *cipher, Bytes *iv) {
81
+ XorAesBlock(iv, plain, iv);
82
+ Encryption->EncryptBlock(iv, cipher);
83
+ memcpy(iv, cipher, 16);
84
+ }
85
+
86
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
87
+ void CBC<CIPHER_TYPE, PADDING_TYPE>::blockDecrypt(Bytes *cipher, Bytes *recover, Bytes *iv) {
88
+ Encryption->DecryptBlock(cipher, recover);
89
+ XorAesBlock(iv, recover, recover);
90
+ memcpy(iv, cipher, Encryption->BLOCK_SIZE);
91
+ }
92
+
93
+ // CFB
94
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
95
+ CFB<CIPHER_TYPE, PADDING_TYPE>::CFB(const Bytes *key, size_t keyLen) : MODE() {
96
+ Encryption = new CIPHER_TYPE(key, keyLen);
97
+ PaddingScheme = new PADDING_TYPE();
98
+ }
99
+
100
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
101
+ ByteArray CFB<CIPHER_TYPE, PADDING_TYPE>::encrypt(Bytes *plain, size_t plainLen, Bytes *iv) {
102
+ ByteArray padded = PaddingScheme->AddPadding(plain, plainLen, Encryption->BLOCK_SIZE);
103
+
104
+ Bytes *tempIV = new Bytes[Encryption->BLOCK_SIZE];
105
+ Bytes *encIV = new Bytes[Encryption->BLOCK_SIZE];
106
+ Bytes *cipher = new Bytes[padded.length];
107
+
108
+ memcpy(tempIV, iv, Encryption->BLOCK_SIZE);
109
+
110
+ for (size_t i = 0; i < padded.length; i += Encryption->BLOCK_SIZE) {
111
+ blockEncrypt(padded.array + i, cipher + i, tempIV);
112
+ }
113
+
114
+ delete[] tempIV;
115
+ delete[] encIV;
116
+
117
+ return ByteArray(cipher, padded.length);
118
+ }
119
+
120
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
121
+ ByteArray CFB<CIPHER_TYPE, PADDING_TYPE>::decrypt(Bytes *cipher, size_t cipherLen, Bytes *iv) {
122
+ Bytes *recover = new Bytes[cipherLen];
123
+ Bytes *tempIV = new Bytes[Encryption->BLOCK_SIZE];
124
+ Bytes *encIV = new Bytes[Encryption->BLOCK_SIZE];
125
+
126
+ memcpy(tempIV, iv, Encryption->BLOCK_SIZE);
127
+
128
+ for (size_t i = 0; i < cipherLen; i += Encryption->BLOCK_SIZE) {
129
+ Encryption->EncryptBlock(tempIV, encIV);
130
+ XorAesBlock(cipher + i, encIV, recover + i);
131
+ memcpy(tempIV, cipher + i, Encryption->BLOCK_SIZE);
132
+ }
133
+
134
+ ByteArray recoverNoPadding = PaddingScheme->RemovePadding(recover, cipherLen, Encryption->BLOCK_SIZE);
135
+
136
+ delete[] recover;
137
+ delete[] tempIV;
138
+ delete[] encIV;
139
+
140
+ return recoverNoPadding;
141
+ }
142
+
143
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
144
+ void CFB<CIPHER_TYPE, PADDING_TYPE>::blockEncrypt(Bytes *plain, Bytes *cipher, Bytes *iv) {
145
+ Encryption->EncryptBlock(iv, cipher);
146
+ XorAesBlock(plain, cipher, cipher);
147
+ memcpy(iv, cipher, Encryption->BLOCK_SIZE);
148
+ }
149
+
150
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
151
+ void CFB<CIPHER_TYPE, PADDING_TYPE>::blockDecrypt(Bytes *cipher, Bytes *recover, Bytes *iv) {
152
+ Encryption->EncryptBlock(iv, recover);
153
+ XorAesBlock(cipher, recover, recover);
154
+ memcpy(iv, cipher, Encryption->BLOCK_SIZE);
155
+ }
156
+
157
+ // ECB
158
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
159
+ ECB<CIPHER_TYPE, PADDING_TYPE>::ECB(const Bytes *key, size_t keyLen) : MODE() {
160
+ this->Encryption = new CIPHER_TYPE(key, keyLen);
161
+ this->PaddingScheme = new PADDING_TYPE();
162
+ }
163
+
164
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
165
+ ByteArray ECB<CIPHER_TYPE, PADDING_TYPE>::encrypt(Bytes *plain, size_t plainLen, Bytes *) {
166
+ ByteArray padded = this->PaddingScheme->AddPadding(plain, plainLen, this->Encryption->BLOCK_SIZE);
167
+
168
+ Bytes *cipher = new Bytes[padded.length];
169
+ for (size_t i = 0; i < padded.length; i += this->Encryption->BLOCK_SIZE) {
170
+ blockEncrypt(padded.array + i, cipher + i);
171
+ }
172
+
173
+ return ByteArray(cipher, padded.length);
174
+ }
175
+
176
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
177
+ ByteArray ECB<CIPHER_TYPE, PADDING_TYPE>::decrypt(Bytes *cipher, size_t cipherLen, Bytes *) {
178
+ ByteArray recovered;
179
+ recovered.array = new Bytes[cipherLen];
180
+ recovered.length = cipherLen;
181
+
182
+ for (size_t i = 0; i < cipherLen; i += this->Encryption->BLOCK_SIZE) {
183
+ blockDecrypt(cipher + i, recovered.array + i);
184
+ }
185
+
186
+ return this->PaddingScheme->RemovePadding(recovered.array, recovered.length, this->Encryption->BLOCK_SIZE);
187
+ }
188
+
189
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
190
+ void ECB<CIPHER_TYPE, PADDING_TYPE>::blockEncrypt(Bytes *plain, Bytes *cipher, Bytes *) {
191
+ this->Encryption->EncryptBlock(plain, cipher);
192
+ }
193
+
194
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
195
+ void ECB<CIPHER_TYPE, PADDING_TYPE>::blockDecrypt(Bytes *cipher, Bytes *recover, Bytes *) {
196
+ this->Encryption->DecryptBlock(cipher, recover);
197
+ }
198
+ } // namespace Mode
199
+ } // namespace Krypt
200
+
201
+ #endif
202
+
203
+ #endif
@@ -0,0 +1,131 @@
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
+ #pragma once
27
+
28
+ #ifdef RNFSTURBO_USE_ENCRYPTION
29
+
30
+ #ifndef KRYPT_MODE_OF_ENCRYPTION_HPP
31
+ #define KRYPT_MODE_OF_ENCRYPTION_HPP
32
+
33
+ #include <iostream>
34
+ #include "blockcipher.hpp"
35
+ #include "bytearray.hpp"
36
+ #include "padding.hpp"
37
+
38
+ namespace Krypt {
39
+ namespace Mode {
40
+ class MODE {
41
+ public:
42
+
43
+ BlockCipher::BASE_BLOCKCIPHER *Encryption;
44
+ Padding::NoPadding *PaddingScheme;
45
+
46
+ MODE() : Encryption(nullptr), PaddingScheme(nullptr) {}
47
+
48
+ virtual ByteArray encrypt(Bytes *, size_t, Bytes *) = 0;
49
+ virtual ByteArray decrypt(Bytes *, size_t, Bytes *) = 0;
50
+
51
+ virtual void blockEncrypt(Bytes *, Bytes *, Bytes *) = 0;
52
+ virtual void blockDecrypt(Bytes *, Bytes *, Bytes *) = 0;
53
+
54
+ virtual ~MODE() {
55
+ delete Encryption;
56
+ delete PaddingScheme;
57
+ }
58
+ };
59
+
60
+ /// Electronic Code Blocking.
61
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
62
+ class ECB : public MODE {
63
+ public:
64
+
65
+ ECB(const Bytes *key, size_t keyLen);
66
+ /// encrypts the whole plain array using ECB mode of encryption with a chosen BlockCipher and Padding.
67
+ /// @param plain pointer to a Bytes*/unsigned char* array, this is the array you want to encrypt.
68
+ /// @param plainLen this is the size of the *plain array.
69
+ /// @param iv for ECB passing an iv will not do anything so there is no need to provide this function an iv
70
+ /// array.
71
+ ByteArray encrypt(Bytes *plain, size_t plainLen, Bytes *iv = nullptr) override;
72
+ /// decrypts the whole cipher array using ECB mode of decryption with a chosen BlockCipher and Padding.
73
+ /// @param cipher pointer to a Bytes*/unsigned char* array, this is the array you want to decrypt.
74
+ /// @param cipherLen this is the size of the *cipher array.
75
+ /// @param iv for ECB passing an iv will not do anything so there is no need to provide this function an iv
76
+ /// array.
77
+ ByteArray decrypt(Bytes *cipher, size_t cipherLen, Bytes *iv = nullptr) override;
78
+
79
+ void blockEncrypt(Bytes *plain, Bytes *cipher, Bytes *iv = nullptr) override;
80
+ void blockDecrypt(Bytes *plain, Bytes *cipher, Bytes *iv = nullptr) override;
81
+ };
82
+
83
+ /// Cipher Block Chaining.
84
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
85
+ class CBC : public MODE {
86
+ public:
87
+
88
+ CBC(const Bytes *key, size_t keyLen);
89
+ /// encrypts the whole plain array using CBC mode of encryption with a chosen BlockCipher and Padding.
90
+ /// @param plain pointer to a Bytes*/unsigned char* array, this is the array you want to encrypt.
91
+ /// @param plainLen this is the size of the *plain array.
92
+ /// @param iv the initial array use for the XOR'ing operations during the CBC encryption.
93
+ ByteArray encrypt(Bytes *plain, size_t plainLen, Bytes *iv) override;
94
+ /// decrypts the whole cipher array using CBC mode of decryption with a chosen BlockCipher and Padding.
95
+ /// @param cipher pointer to a Bytes*/unsigned char* array, this is the array you want to decrypt.
96
+ /// @param cipherLen this is the size of the *cipher array.
97
+ /// @param iv the initial array use for the XOR'ing operations during the CBC decryption.
98
+ ByteArray decrypt(Bytes *cipher, size_t cipherLen, Bytes *iv) override;
99
+
100
+ void blockEncrypt(Bytes *plain, Bytes *cipher, Bytes *iv) override;
101
+ void blockDecrypt(Bytes *plain, Bytes *cipher, Bytes *iv) override;
102
+ };
103
+
104
+ /// Cipher Feedback.
105
+ template <typename CIPHER_TYPE, typename PADDING_TYPE>
106
+ class CFB : public MODE {
107
+ public:
108
+
109
+ CFB(const Bytes *key, size_t keyLen);
110
+ /// encrypts the whole plain array using CFB mode of encryption with a chosen BlockCipher and Padding.
111
+ /// @param plain pointer to a Bytes*/unsigned char* array, this is the array you want to encrypt.
112
+ /// @param plainLen this is the size of the *plain array.
113
+ /// @param iv the initial array use for the XOR'ing operations during the CFB encryption.
114
+ ByteArray encrypt(Bytes *plain, size_t plainLen, Bytes *iv) override;
115
+ /// decrypts the whole cipher array using CFB mode of decryption with a chosen BlockCipher and Padding.
116
+ /// @param cipher pointer to a Bytes*/unsigned char* array, this is the array you want to decrypt.
117
+ /// @param cipherLen this is the size of the *cipher array.
118
+ /// @param iv the initial array use for the XOR'ing operations during the CFB decryption.
119
+ ByteArray decrypt(Bytes *cipher, size_t cipherLen, Bytes *iv) override;
120
+
121
+ void blockEncrypt(Bytes *plain, Bytes *cipher, Bytes *iv) override;
122
+ void blockDecrypt(Bytes *plain, Bytes *cipher, Bytes *iv) override;
123
+ };
124
+ } // namespace Mode
125
+ } // namespace Krypt
126
+
127
+ #include "mode.cpp"
128
+
129
+ #endif
130
+
131
+ #endif
@@ -0,0 +1,219 @@
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_PADDING_CPP
29
+ #define KRYPT_PADDING_CPP
30
+
31
+ #import "padding.hpp"
32
+
33
+ namespace Krypt {
34
+ namespace Padding {
35
+ // ANSI_X9_23
36
+ ByteArray ANSI_X9_23::AddPadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
37
+ size_t paddings = BLOCKSIZE - (len % BLOCKSIZE);
38
+ size_t paddedLen = paddings + len;
39
+ Bytes *paddedBlock = new Bytes[paddedLen];
40
+
41
+ memcpy(paddedBlock, src, len);
42
+ memset(paddedBlock + len, 0x00, paddings);
43
+ paddedBlock[paddedLen - 1] = static_cast<Bytes>(paddings);
44
+
45
+ return ByteArray(paddedBlock, paddedLen);
46
+ }
47
+
48
+ ByteArray ANSI_X9_23::RemovePadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
49
+ #ifndef PADDING_CHECK_DISABLE
50
+ if (len < BLOCKSIZE || len % BLOCKSIZE != 0) {
51
+ std::cerr << "\nA padded `src` should have a `len` greater than and divisible by the `BLOCKSIZE`\n";
52
+ throw InvalidPaddedLength("ANSI_X9_23: src's `len` indicates that it was not padded or is corrupted");
53
+ }
54
+ #endif
55
+
56
+ size_t paddings = src[len - 1];
57
+ size_t noPaddingLength = len - paddings;
58
+
59
+ #ifndef PADDING_CHECK_DISABLE
60
+ for (size_t i = 1; i < paddings; ++i) {
61
+ if (src[len - 1 - i] != 0x00) {
62
+ throw InvalidPadding("ANSI_X9_23: does not match the padding scheme used in `src`");
63
+ }
64
+ }
65
+ #endif
66
+
67
+ Bytes *NoPadding = new Bytes[noPaddingLength];
68
+ memcpy(NoPadding, src, noPaddingLength);
69
+
70
+ return ByteArray(NoPadding, noPaddingLength);
71
+ }
72
+
73
+ // ISO_IEC_7816_4
74
+ ByteArray ISO_IEC_7816_4::AddPadding(Bytes *src, size_t originalSrcLen, size_t BLOCKSIZE) {
75
+ size_t paddings = BLOCKSIZE - (originalSrcLen % BLOCKSIZE);
76
+ size_t paddedLen = paddings + originalSrcLen;
77
+ Bytes *paddedBlock = new Bytes[paddedLen];
78
+
79
+ memcpy(paddedBlock, src, originalSrcLen);
80
+ memset(paddedBlock + originalSrcLen, 0x00, paddings);
81
+ paddedBlock[originalSrcLen] = 0x80;
82
+
83
+ return ByteArray(paddedBlock, paddedLen);
84
+ }
85
+
86
+ ByteArray ISO_IEC_7816_4::RemovePadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
87
+ #ifndef PADDING_CHECK_DISABLE
88
+ if (len < BLOCKSIZE || len % BLOCKSIZE != 0) {
89
+ std::cerr << "\nA padded `src` should have a `len` greater than and divisible by the `BLOCKSIZE`\n";
90
+ throw InvalidPaddedLength("ISO_IEC_7816_4: src's `len` indicates that it was not padded or is corrupted"
91
+ );
92
+ }
93
+ #endif
94
+
95
+ size_t i;
96
+
97
+ #ifndef PADDING_CHECK_DISABLE
98
+ for (i = 1; i < BLOCKSIZE; ++i) {
99
+ if (src[len - i] == 0x80) {
100
+ break;
101
+ }
102
+
103
+ if (src[len - i] != 0x00) {
104
+ throw InvalidPadding("ISO_IEC_7816_4: does not match the padding scheme used in `src`");
105
+ }
106
+ }
107
+ #endif
108
+
109
+ size_t noPaddingLength = len - i;
110
+ Bytes *NoPadding = new Bytes[noPaddingLength];
111
+ memcpy(NoPadding, src, noPaddingLength);
112
+
113
+ return ByteArray(NoPadding, noPaddingLength);
114
+ }
115
+
116
+ // NoPadding
117
+ const char *InvalidPadding::what() const throw() {
118
+ return msg;
119
+ }
120
+ const char *InvalidPaddedLength::what() const throw() {
121
+ return msg;
122
+ }
123
+
124
+ ByteArray NoPadding::AddPadding(Bytes *src, size_t len, size_t) {
125
+ Bytes *Copy = new Bytes[len];
126
+ memcpy(Copy, src, len);
127
+ return ByteArray(Copy, len);
128
+ }
129
+
130
+ ByteArray NoPadding::RemovePadding(Bytes *src, size_t len, size_t) {
131
+ Bytes *Copy = new Bytes[len];
132
+ memcpy(Copy, src, len);
133
+ return ByteArray(Copy, len);
134
+ }
135
+
136
+ // PKCS_5_7
137
+ ByteArray PKCS_5_7::AddPadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
138
+ size_t paddings = BLOCKSIZE - (len % BLOCKSIZE);
139
+ size_t paddedLen = paddings + len;
140
+ Bytes *paddedBlock = new Bytes[paddedLen];
141
+
142
+ memcpy(paddedBlock, src, len);
143
+ memset(paddedBlock + len, static_cast<Bytes>(paddings), paddings);
144
+
145
+ return ByteArray(paddedBlock, paddedLen);
146
+ }
147
+
148
+ ByteArray PKCS_5_7::RemovePadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
149
+ #ifndef PADDING_CHECK_DISABLE
150
+ if (len < BLOCKSIZE || len % BLOCKSIZE != 0) {
151
+ std::cerr << "\nA padded `src` should have a `len` greater than and divisible by the `BLOCKSIZE`\n";
152
+ throw InvalidPaddedLength("PKCS_5_7: src's `len` indicates that it was not padded or is corrupted");
153
+ }
154
+ #endif
155
+
156
+ size_t paddings = src[len - 1];
157
+ size_t noPaddingLength = len - paddings;
158
+
159
+ #ifndef PADDING_CHECK_DISABLE
160
+ Bytes checkchar = static_cast<Bytes>(paddings);
161
+ for (size_t i = 1; i < paddings; ++i) {
162
+ if (src[len - 1 - i] != checkchar) {
163
+ throw InvalidPadding("PKCS_5_7: does not match the padding scheme used in `src`");
164
+ }
165
+ }
166
+ #endif
167
+
168
+ Bytes *NoPadding = new Bytes[noPaddingLength];
169
+ memcpy(NoPadding, src, noPaddingLength);
170
+
171
+ return ByteArray(NoPadding, noPaddingLength);
172
+ }
173
+
174
+ // ZeroNulls
175
+ ByteArray ZeroNulls::AddPadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
176
+ size_t paddings = BLOCKSIZE - (len % BLOCKSIZE);
177
+ size_t paddedLen = paddings + len;
178
+ Bytes *paddedBlock = new Bytes[paddedLen];
179
+
180
+ memcpy(paddedBlock, src, len);
181
+ memset(paddedBlock + len, 0x00, paddings);
182
+
183
+ return ByteArray(paddedBlock, paddedLen);
184
+ }
185
+
186
+ ByteArray ZeroNulls::RemovePadding(Bytes *src, size_t len, size_t BLOCKSIZE) {
187
+ #ifndef PADDING_CHECK_DISABLE
188
+ if (len < BLOCKSIZE || len % BLOCKSIZE != 0) {
189
+ std::cerr << "\nA padded `src` should have a `len` greater than and divisible by the `BLOCKSIZE`\n";
190
+ throw InvalidPaddedLength("ZeroNulls: src's `len` indicates that it was not padded or is corrupted");
191
+ }
192
+ #endif
193
+
194
+ #ifndef PADDING_CHECK_DISABLE
195
+ if (src[len - 1] != 0x00) {
196
+ throw InvalidPadding("ZeroNulls: does not match the padding scheme used in `src`");
197
+ }
198
+ #endif
199
+
200
+ size_t paddings = 0, noPaddingLength = 0;
201
+ for (size_t i = 0; i < BLOCKSIZE; ++i)
202
+ if (src[len - 1 - i] == 0x00) {
203
+ paddings++;
204
+ } else {
205
+ break;
206
+ }
207
+
208
+ noPaddingLength = len - paddings;
209
+ Bytes *NoPadding = new Bytes[noPaddingLength];
210
+ memcpy(NoPadding, src, noPaddingLength);
211
+
212
+ return ByteArray(NoPadding, noPaddingLength);
213
+ }
214
+ } // namespace Padding
215
+ } // namespace Krypt
216
+
217
+ #endif
218
+
219
+ #endif