Skip to content

Commit 562d43c

Browse files
committed
stack -> heap buffer for final()
1 parent ef3e85c commit 562d43c

4 files changed

Lines changed: 20 additions & 25 deletions

File tree

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ SPEC CHECKSUMS:
19401940
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
19411941
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
19421942
hermes-engine: 46f1ffbf0297f4298862068dd4c274d4ac17a1fd
1943-
NitroModules: 3a58d9bc70815a0d5de4476ed6a36eff05a6a0ae
1943+
NitroModules: c36d6f656038a56beb1b1bcab2d0252d71744013
19441944
OpenSSL-Universal: b60a3702c9fea8b3145549d421fdb018e53ab7b4
19451945
QuickCrypto: 11878b44cfc77fad2ea8f387a16e315841651305
19461946
RCT-Folly: 84578c8756030547307e4572ab1947de1685c599

example/src/tests/cipher/cipher_tests.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ test(SUITE, 'cipher - buffers', async () => {
5454
);
5555
});
5656

57-
// // update/final
58-
// ciphers.forEach(cipherName => {
59-
// test(SUITE, `cipher - non-stream - ${cipherName}`, async () => {
60-
// roundtrip(cipherName as CipherType, key, iv, plaintext);
61-
// });
62-
// });
57+
// update/final
58+
ciphers.forEach(cipherName => {
59+
test(SUITE, `cipher - non-stream - ${cipherName}`, async () => {
60+
roundtrip(cipherName as CipherType, key, iv, plaintext);
61+
});
62+
});
6363

6464
function roundtrip(
6565
cipherName: CipherType,
@@ -68,8 +68,8 @@ function roundtrip(
6868
payload: string,
6969
) {
7070
const cipher: Cipher = createCipheriv(cipherName, lKey, lIv, {});
71-
let ciph = cipher.update(payload, 'utf8', 'buffer');
72-
ciph = Buffer.concat([ciph, cipher.final('buffer')]);
71+
let ciph = cipher.update(payload, 'utf8', 'buffer') as Buffer;
72+
ciph = Buffer.concat([ciph, cipher.final()]);
7373

7474
const decipher: Decipher = createDecipheriv(cipherName, lKey, lIv, {});
7575
let deciph = decipher.update(ciph, 'buffer', 'utf8');

packages/react-native-quick-crypto/cpp/cipher/HybridCipher.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ HybridCipher::final() {
104104
}
105105

106106
int finalLen = 0;
107-
unsigned char tempBuf[EVP_MAX_BLOCK_LENGTH];
107+
uint8_t* tempBuf = new uint8_t[EVP_MAX_BLOCK_LENGTH];
108108

109109
// Finalize the encryption/decryption
110110
if (EVP_CipherFinal_ex(
111111
ctx,
112112
tempBuf,
113113
&finalLen) != 1) {
114+
delete[] tempBuf;
114115
throw std::runtime_error("Failed to finalize cipher: " +
115116
std::to_string(ERR_get_error()));
116117
}
@@ -119,7 +120,7 @@ HybridCipher::final() {
119120
return std::make_shared<NativeArrayBuffer>(
120121
tempBuf,
121122
finalLen,
122-
nullptr
123+
[=]() { delete[] tempBuf; }
123124
);
124125
}
125126

packages/react-native-quick-crypto/src/cipher.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import type {
1010
CipherGCMOptions,
1111
CipherOCBOptions,
1212
CipherOCBTypes,
13-
DecipherGCM,
14-
DecipherOCB,
15-
DecipherCCM,
16-
CipherCCM,
17-
CipherOCB,
18-
CipherGCM,
1913
} from 'crypto'; // @types/node
2014
import type { Cipher as NativeCipher } from './specs/cipher.nitro';
2115
import { binaryLikeToArrayBuffer } from './utils';
@@ -184,7 +178,7 @@ class Cipheriv extends CipherCommon {
184178
}
185179
}
186180

187-
type Cipher = CipherCCM | CipherOCB | CipherGCM | Cipheriv;
181+
type Cipher = Cipheriv;
188182

189183
class Decipheriv extends CipherCommon {
190184
constructor(
@@ -203,26 +197,26 @@ class Decipheriv extends CipherCommon {
203197
}
204198
}
205199

206-
type Decipher = DecipherCCM | DecipherOCB | DecipherGCM | Decipheriv;
200+
type Decipher = Decipheriv;
207201

208202
export function createDecipheriv(
209203
algorithm: CipherCCMTypes,
210204
key: BinaryLikeNode,
211205
iv: BinaryLike,
212206
options: CipherCCMOptions,
213-
): DecipherCCM;
207+
): Decipher;
214208
export function createDecipheriv(
215209
algorithm: CipherOCBTypes,
216210
key: BinaryLikeNode,
217211
iv: BinaryLike,
218212
options: CipherOCBOptions,
219-
): DecipherOCB;
213+
): Decipher;
220214
export function createDecipheriv(
221215
algorithm: CipherGCMTypes,
222216
key: BinaryLikeNode,
223217
iv: BinaryLike,
224218
options?: CipherGCMOptions,
225-
): DecipherGCM;
219+
): Decipher;
226220
export function createDecipheriv(
227221
algorithm: CipherType,
228222
key: BinaryLikeNode,
@@ -252,19 +246,19 @@ export function createCipheriv(
252246
key: BinaryLikeNode,
253247
iv: BinaryLike,
254248
options: CipherCCMOptions,
255-
): CipherCCM;
249+
): Cipher;
256250
export function createCipheriv(
257251
algorithm: CipherOCBTypes,
258252
key: BinaryLikeNode,
259253
iv: BinaryLike,
260254
options: CipherOCBOptions,
261-
): CipherOCB;
255+
): Cipher;
262256
export function createCipheriv(
263257
algorithm: CipherGCMTypes,
264258
key: BinaryLikeNode,
265259
iv: BinaryLike,
266260
options?: CipherGCMOptions,
267-
): CipherGCM;
261+
): Cipher;
268262
export function createCipheriv(
269263
algorithm: CipherType,
270264
key: BinaryLikeNode,

0 commit comments

Comments
 (0)