Skip to content

Commit 895cfb8

Browse files
authored
🤖 Merge PR DefinitelyTyped#71834 feat(node/crypto): separate chacha20-poly1305 from CipherCCMTypes by @hkleungai
1 parent 6a2c9e7 commit 895cfb8

File tree

6 files changed

+960
-3
lines changed

6 files changed

+960
-3
lines changed

‎types/node/crypto.d.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,10 @@ declare module "crypto" {
682682
*/
683683
type: KeyObjectType;
684684
}
685-
type CipherCCMTypes = "aes-128-ccm" | "aes-192-ccm" | "aes-256-ccm" | "chacha20-poly1305";
685+
type CipherCCMTypes = "aes-128-ccm" | "aes-192-ccm" | "aes-256-ccm";
686686
type CipherGCMTypes = "aes-128-gcm" | "aes-192-gcm" | "aes-256-gcm";
687687
type CipherOCBTypes = "aes-128-ocb" | "aes-192-ocb" | "aes-256-ocb";
688+
type CipherChaCha20Poly1305Types = "chacha20-poly1305";
688689
type BinaryLike = string | NodeJS.ArrayBufferView;
689690
type CipherKey = BinaryLike | KeyObject;
690691
interface CipherCCMOptions extends stream.TransformOptions {
@@ -696,6 +697,10 @@ declare module "crypto" {
696697
interface CipherOCBOptions extends stream.TransformOptions {
697698
authTagLength: number;
698699
}
700+
interface CipherChaCha20Poly1305Options extends stream.TransformOptions {
701+
/** @default 16 */
702+
authTagLength?: number | undefined;
703+
}
699704
/**
700705
* Creates and returns a `Cipher` object, with the given `algorithm`, `key` and
701706
* initialization vector (`iv`).
@@ -744,6 +749,12 @@ declare module "crypto" {
744749
iv: BinaryLike,
745750
options?: CipherGCMOptions,
746751
): CipherGCM;
752+
function createCipheriv(
753+
algorithm: CipherChaCha20Poly1305Types,
754+
key: CipherKey,
755+
iv: BinaryLike,
756+
options?: CipherChaCha20Poly1305Options,
757+
): CipherChaCha20Poly1305;
747758
function createCipheriv(
748759
algorithm: string,
749760
key: CipherKey,
@@ -943,6 +954,15 @@ declare module "crypto" {
943954
): this;
944955
getAuthTag(): Buffer;
945956
}
957+
interface CipherChaCha20Poly1305 extends Cipher {
958+
setAAD(
959+
buffer: NodeJS.ArrayBufferView,
960+
options: {
961+
plaintextLength: number;
962+
},
963+
): this;
964+
getAuthTag(): Buffer;
965+
}
946966
/**
947967
* Creates and returns a `Decipher` object that uses the given `algorithm`, `key` and initialization vector (`iv`).
948968
*
@@ -990,6 +1010,12 @@ declare module "crypto" {
9901010
iv: BinaryLike,
9911011
options?: CipherGCMOptions,
9921012
): DecipherGCM;
1013+
function createDecipheriv(
1014+
algorithm: CipherChaCha20Poly1305Types,
1015+
key: CipherKey,
1016+
iv: BinaryLike,
1017+
options?: CipherChaCha20Poly1305Options,
1018+
): DecipherChaCha20Poly1305;
9931019
function createDecipheriv(
9941020
algorithm: string,
9951021
key: CipherKey,
@@ -1175,6 +1201,15 @@ declare module "crypto" {
11751201
},
11761202
): this;
11771203
}
1204+
interface DecipherChaCha20Poly1305 extends Decipher {
1205+
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
1206+
setAAD(
1207+
buffer: NodeJS.ArrayBufferView,
1208+
options: {
1209+
plaintextLength: number;
1210+
},
1211+
): this;
1212+
}
11781213
interface PrivateKeyInput {
11791214
key: string | Buffer;
11801215
format?: KeyFormat | undefined;

‎types/node/test/crypto.ts‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,170 @@ import { promisify } from "node:util";
216216
decipher.final();
217217
}
218218

219+
{
220+
let key: crypto.CipherKey = "" as crypto.CipherKey;
221+
let iv: crypto.BinaryLike = "" as crypto.BinaryLike;
222+
{
223+
let cipher = crypto.createCipheriv("aes-128-ccm", key, iv, { authTagLength: 16 });
224+
cipher = crypto.createCipheriv("aes-128-ccm", key, iv, {
225+
authTagLength: 16,
226+
read(size) {
227+
// $ExpectType Transform
228+
this;
229+
// $ExpectType number
230+
size;
231+
},
232+
});
233+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
234+
// $ExpectType Buffer<ArrayBufferLike>
235+
cipher.getAuthTag();
236+
}
237+
238+
{
239+
let cipher = crypto.createCipheriv("aes-128-gcm", key, iv);
240+
cipher = crypto.createCipheriv("aes-128-gcm", key, iv, {});
241+
cipher = crypto.createCipheriv("aes-128-gcm", key, iv, { authTagLength: 16 });
242+
cipher = crypto.createCipheriv("aes-128-gcm", key, iv, {
243+
read(size) {
244+
// $ExpectType Transform
245+
this;
246+
// $ExpectType number
247+
size;
248+
},
249+
});
250+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
251+
// $ExpectType Buffer<ArrayBufferLike>
252+
cipher.getAuthTag();
253+
}
254+
255+
{
256+
let cipher = crypto.createCipheriv("aes-128-ocb", key, iv, { authTagLength: 16 });
257+
cipher = crypto.createCipheriv("aes-128-ocb", key, iv, {
258+
authTagLength: 16,
259+
read(size) {
260+
// $ExpectType Transform
261+
this;
262+
// $ExpectType number
263+
size;
264+
},
265+
});
266+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
267+
// $ExpectType Buffer<ArrayBufferLike>
268+
cipher.getAuthTag();
269+
}
270+
271+
{
272+
let cipher = crypto.createCipheriv("chacha20-poly1305", key, iv);
273+
cipher = crypto.createCipheriv("chacha20-poly1305", key, iv, {});
274+
cipher = crypto.createCipheriv("chacha20-poly1305", key, iv, { authTagLength: 16 });
275+
cipher = crypto.createCipheriv("chacha20-poly1305", key, iv, {
276+
read(size) {
277+
// $ExpectType Transform
278+
this;
279+
// $ExpectType number
280+
size;
281+
},
282+
});
283+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
284+
// $ExpectType Buffer<ArrayBufferLike>
285+
cipher.getAuthTag();
286+
}
287+
288+
let cipher = crypto.createCipheriv("aes-128-ecb", key, iv);
289+
cipher = crypto.createCipheriv("aes-128-ecb", key, iv, {
290+
read(size) {
291+
// $ExpectType Transform
292+
this;
293+
// $ExpectType number
294+
size;
295+
},
296+
});
297+
// @ts-expect-error - .setAAD() does not exist
298+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
299+
// @ts-expect-error - .getAuthTag() does not exist
300+
cipher.getAuthTag();
301+
}
302+
303+
{
304+
let key: crypto.CipherKey = "" as crypto.CipherKey;
305+
let iv: crypto.BinaryLike = "" as crypto.BinaryLike;
306+
{
307+
let cipher = crypto.createDecipheriv("aes-128-ccm", key, iv, { authTagLength: 16 });
308+
cipher = crypto.createDecipheriv("aes-128-ccm", key, iv, {
309+
authTagLength: 16,
310+
read(size) {
311+
// $ExpectType Transform
312+
this;
313+
// $ExpectType number
314+
size;
315+
},
316+
});
317+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
318+
cipher.setAuthTag(Buffer.from([]));
319+
}
320+
321+
{
322+
let cipher = crypto.createDecipheriv("aes-128-gcm", key, iv);
323+
cipher = crypto.createDecipheriv("aes-128-gcm", key, iv, {});
324+
cipher = crypto.createDecipheriv("aes-128-gcm", key, iv, { authTagLength: 16 });
325+
cipher = crypto.createDecipheriv("aes-128-gcm", key, iv, {
326+
read(size) {
327+
// $ExpectType Transform
328+
this;
329+
// $ExpectType number
330+
size;
331+
},
332+
});
333+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
334+
cipher.setAuthTag(Buffer.from([]));
335+
}
336+
337+
{
338+
let cipher = crypto.createDecipheriv("aes-128-ocb", key, iv, { authTagLength: 16 });
339+
cipher = crypto.createDecipheriv("aes-128-ocb", key, iv, {
340+
authTagLength: 16,
341+
read(size) {
342+
// $ExpectType Transform
343+
this;
344+
// $ExpectType number
345+
size;
346+
},
347+
});
348+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
349+
cipher.setAuthTag(Buffer.from([]));
350+
}
351+
352+
{
353+
let cipher = crypto.createDecipheriv("chacha20-poly1305", key, iv);
354+
cipher = crypto.createDecipheriv("chacha20-poly1305", key, iv, {});
355+
cipher = crypto.createDecipheriv("chacha20-poly1305", key, iv, { authTagLength: 16 });
356+
cipher = crypto.createDecipheriv("chacha20-poly1305", key, iv, {
357+
read(size) {
358+
// $ExpectType Transform
359+
this;
360+
// $ExpectType number
361+
size;
362+
},
363+
});
364+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
365+
cipher.setAuthTag(Buffer.from([]));
366+
}
367+
368+
let cipher = crypto.createDecipheriv("aes-128-ecb", key, iv);
369+
cipher = crypto.createDecipheriv("aes-128-ecb", key, iv, {
370+
read(size) {
371+
// $ExpectType Transform
372+
this;
373+
// $ExpectType number
374+
size;
375+
},
376+
});
377+
// @ts-expect-error - .setAAD() does not exist
378+
cipher = cipher.setAAD(Buffer.from([]), { plaintextLength: 0 });
379+
// @ts-expect-error - .setAuthTag() does not exist
380+
cipher.setAuthTag(Buffer.from([]));
381+
}
382+
219383
{
220384
// crypto_timingsafeequal_buffer_test
221385
const buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);

‎types/node/v18/crypto.d.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,10 @@ declare module "crypto" {
669669
*/
670670
type: KeyObjectType;
671671
}
672-
type CipherCCMTypes = "aes-128-ccm" | "aes-192-ccm" | "aes-256-ccm" | "chacha20-poly1305";
672+
type CipherCCMTypes = "aes-128-ccm" | "aes-192-ccm" | "aes-256-ccm";
673673
type CipherGCMTypes = "aes-128-gcm" | "aes-192-gcm" | "aes-256-gcm";
674674
type CipherOCBTypes = "aes-128-ocb" | "aes-192-ocb" | "aes-256-ocb";
675+
type CipherChaCha20Poly1305Types = "chacha20-poly1305";
675676
type BinaryLike = string | NodeJS.ArrayBufferView;
676677
type CipherKey = BinaryLike | KeyObject;
677678
interface CipherCCMOptions extends stream.TransformOptions {
@@ -683,6 +684,10 @@ declare module "crypto" {
683684
interface CipherOCBOptions extends stream.TransformOptions {
684685
authTagLength: number;
685686
}
687+
interface CipherChaCha20Poly1305Options extends stream.TransformOptions {
688+
/** @default 16 */
689+
authTagLength?: number | undefined;
690+
}
686691
/**
687692
* Creates and returns a `Cipher` object that uses the given `algorithm` and `password`.
688693
*
@@ -720,6 +725,14 @@ declare module "crypto" {
720725
/** @deprecated since v10.0.0 use `createCipheriv()` */
721726
function createCipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): CipherGCM;
722727
/** @deprecated since v10.0.0 use `createCipheriv()` */
728+
function createCipher(algorithm: CipherOCBTypes, password: BinaryLike, options: CipherOCBOptions): CipherOCB;
729+
/** @deprecated since v10.0.0 use `createCipheriv()` */
730+
function createCipher(
731+
algorithm: CipherChaCha20Poly1305Types,
732+
password: BinaryLike,
733+
options?: CipherChaCha20Poly1305Options,
734+
): CipherChaCha20Poly1305;
735+
/** @deprecated since v10.0.0 use `createCipheriv()` */
723736
function createCipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Cipher;
724737
/**
725738
* Creates and returns a `Cipher` object, with the given `algorithm`, `key` and
@@ -769,6 +782,12 @@ declare module "crypto" {
769782
iv: BinaryLike,
770783
options?: CipherGCMOptions,
771784
): CipherGCM;
785+
function createCipheriv(
786+
algorithm: CipherChaCha20Poly1305Types,
787+
key: CipherKey,
788+
iv: BinaryLike,
789+
options?: CipherChaCha20Poly1305Options,
790+
): CipherChaCha20Poly1305;
772791
function createCipheriv(
773792
algorithm: string,
774793
key: CipherKey,
@@ -968,6 +987,15 @@ declare module "crypto" {
968987
): this;
969988
getAuthTag(): Buffer;
970989
}
990+
interface CipherChaCha20Poly1305 extends Cipher {
991+
setAAD(
992+
buffer: NodeJS.ArrayBufferView,
993+
options: {
994+
plaintextLength: number;
995+
},
996+
): this;
997+
getAuthTag(): Buffer;
998+
}
971999
/**
9721000
* Creates and returns a `Decipher` object that uses the given `algorithm` and `password` (key).
9731001
*
@@ -994,6 +1022,14 @@ declare module "crypto" {
9941022
/** @deprecated since v10.0.0 use `createDecipheriv()` */
9951023
function createDecipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
9961024
/** @deprecated since v10.0.0 use `createDecipheriv()` */
1025+
function createDecipher(algorithm: CipherOCBTypes, password: BinaryLike, options: CipherOCBOptions): DecipherOCB;
1026+
/** @deprecated since v10.0.0 use `createDecipheriv()` */
1027+
function createDecipher(
1028+
algorithm: CipherChaCha20Poly1305Types,
1029+
password: BinaryLike,
1030+
options?: CipherChaCha20Poly1305Options,
1031+
): DecipherChaCha20Poly1305;
1032+
/** @deprecated since v10.0.0 use `createDecipheriv()` */
9971033
function createDecipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Decipher;
9981034
/**
9991035
* Creates and returns a `Decipher` object that uses the given `algorithm`, `key` and initialization vector (`iv`).
@@ -1042,6 +1078,12 @@ declare module "crypto" {
10421078
iv: BinaryLike,
10431079
options?: CipherGCMOptions,
10441080
): DecipherGCM;
1081+
function createDecipheriv(
1082+
algorithm: CipherChaCha20Poly1305Types,
1083+
key: CipherKey,
1084+
iv: BinaryLike,
1085+
options?: CipherChaCha20Poly1305Options,
1086+
): DecipherChaCha20Poly1305;
10451087
function createDecipheriv(
10461088
algorithm: string,
10471089
key: CipherKey,
@@ -1226,6 +1268,15 @@ declare module "crypto" {
12261268
},
12271269
): this;
12281270
}
1271+
interface DecipherChaCha20Poly1305 extends Decipher {
1272+
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
1273+
setAAD(
1274+
buffer: NodeJS.ArrayBufferView,
1275+
options: {
1276+
plaintextLength: number;
1277+
},
1278+
): this;
1279+
}
12291280
interface PrivateKeyInput {
12301281
key: string | Buffer;
12311282
format?: KeyFormat | undefined;

0 commit comments

Comments
 (0)