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,137 @@
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_FUNCTIONS_CPP
29
+ #define KRYPT_FUNCTIONS_CPP
30
+
31
+ #include "functions.hpp"
32
+ #include "types.hpp"
33
+
34
+ // #define USE_AESNI
35
+
36
+ #if defined(USE_AESNI)
37
+ #include <immintrin.h>
38
+ #else
39
+
40
+ #endif
41
+
42
+ // here the DWORDS is formed from 4 continous elements from a starting index of an unsinged char* array or Bytes*.
43
+ // a DWORD or double-word occupies 32-bits in a memory.
44
+ // the term DWORD is usually used to represent bits inside the eax,edx,...r15d parts of an x86_64 cpu register which can
45
+ // contain 32 bits inside.
46
+
47
+ namespace Krypt {
48
+ Bytes xtime(Bytes b) // multiply on x
49
+ {
50
+ return (b << 1) ^ (((b >> 7) & 1) * 0x1b);
51
+ }
52
+
53
+ void SubDWordBytes(Bytes *a) {
54
+ a[0] = sbox[a[0]];
55
+ a[1] = sbox[a[1]];
56
+ a[2] = sbox[a[2]];
57
+ a[3] = sbox[a[3]];
58
+ }
59
+
60
+ void RotDWord(Bytes *a) {
61
+ Bytes c = a[0];
62
+ a[0] = a[1];
63
+ a[1] = a[2];
64
+ a[2] = a[3];
65
+ a[3] = c;
66
+ }
67
+
68
+ void XorDWords(Bytes *a, Bytes *b, Bytes *dest) {
69
+ dest[0] = a[0] ^ b[0];
70
+ dest[1] = a[1] ^ b[1];
71
+ dest[2] = a[2] ^ b[2];
72
+ dest[3] = a[3] ^ b[3];
73
+ }
74
+
75
+ void XorBlocks(unsigned char *a, unsigned char *b, unsigned char *dest, unsigned int len) {
76
+ for (unsigned int i = 0; i < len; i++) {
77
+ dest[i] = a[i] ^ b[i];
78
+ }
79
+ }
80
+
81
+ void XorAesBlock(unsigned char *a, unsigned char *b, unsigned char *result) {
82
+ #if defined(USE_AESNI)
83
+ __m128i vxor = _mm_xor_si128(
84
+ _mm_load_si128((__m128i *) a),
85
+ _mm_load_si128((__m128i *) b)
86
+ );
87
+
88
+ _mm_storeu_si128((__m128i *) (result), vxor);
89
+ #else
90
+ uint64_t
91
+ *A = reinterpret_cast<uint64_t *>(a),
92
+ *B = reinterpret_cast<uint64_t *>(b),
93
+ *C = reinterpret_cast<uint64_t *>(result);
94
+
95
+ C[0] = A[0] ^ B[0];
96
+ C[1] = A[1] ^ B[1];
97
+ #endif
98
+ }
99
+
100
+ void Rcon(Bytes *a, int n) {
101
+ int i;
102
+ Bytes c = 1;
103
+ for (i = 0; i < n - 1; i++) {
104
+ c = xtime(c);
105
+ }
106
+
107
+ a[0] = c;
108
+ a[1] = a[2] = a[3] = 0;
109
+ }
110
+
111
+ void printHexArray(unsigned char a[], size_t n) {
112
+ for (size_t i = 0; i < n; i++) {
113
+ printf("%02x ", a[i]);
114
+ }
115
+ std::cout << "\n";
116
+ }
117
+
118
+ void printHexVector(const std::vector<unsigned char> &a) {
119
+ for (size_t i = 0; i < a.size(); i++) {
120
+ printf("%02x ", a[i]);
121
+ }
122
+ }
123
+
124
+ std::vector<unsigned char> ArrayToVector(std::unique_ptr<unsigned char[]> a, unsigned char len) {
125
+ return std::vector<unsigned char>(a.get(), a.get() + len);
126
+ }
127
+
128
+ std::unique_ptr<unsigned char[]> VectorToArray(const std::vector<unsigned char>& a) {
129
+ auto cpy = std::make_unique<unsigned char[]>(a.size());
130
+ memcpy(cpy.get(), a.data(), a.size());
131
+ return cpy;
132
+ }
133
+ } // namespace Krypt
134
+
135
+ #endif
136
+
137
+ #endif
@@ -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,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