From cdb8cf344cf9e70fa615069047ff1d6bcf9ee619 Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich Date: Fri, 17 Aug 2018 21:59:21 +0200 Subject: [PATCH] [node] fix crypto cipher/decipher overloads and typos --- types/node/index.d.ts | 32 +++++++++++++----------- types/node/node-tests.ts | 53 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index d6bc8695ef..c8ef4fc79a 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -5903,13 +5903,15 @@ declare module "crypto" { authTagLength?: number; } /** @deprecated since v10.0.0 use createCipheriv() */ - export function createCipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher; export function createCipher(algorithm: CipherCCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM; - export function createCipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): CipherGCM; + /** @deprecated since v10.0.0 use createCipheriv() */ + export function createCipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): CipherGCM; + /** @deprecated since v10.0.0 use createCipheriv() */ + export function createCipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher; + export function createCipheriv(algorithm: CipherCCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM; + export function createCipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): CipherGCM; export function createCipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Cipher; - export function createCipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): CipherCCM; - export function createCipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): CipherGCM; export interface Cipher extends NodeJS.ReadWriteStream { update(data: string | Buffer | NodeJS.TypedArray | DataView): Buffer; @@ -5925,21 +5927,23 @@ declare module "crypto" { // setAAD(buffer: Buffer): this; // docs only say buffer } export interface CipherCCM extends Cipher { - setAAD(buffer: Buffer, options: { plainTextLength: number }): this; + setAAD(buffer: Buffer, options: { plaintextLength: number }): this; getAuthTag(): Buffer; } export interface CipherGCM extends Cipher { - setAAD(buffer: Buffer, options?: { plainTextLength: number }): this; + setAAD(buffer: Buffer, options?: { plaintextLength: number }): this; getAuthTag(): Buffer; } /** @deprecated since v10.0.0 use createCipheriv() */ - export function createDecipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher; export function createDecipher(algorithm: CipherCCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): DecipherCCM; - export function createDecipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): DecipherGCM; + /** @deprecated since v10.0.0 use createCipheriv() */ + export function createDecipher(algorithm: CipherGCMTypes, password: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): DecipherGCM; + /** @deprecated since v10.0.0 use createCipheriv() */ + export function createDecipher(algorithm: string, password: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher; - export function createDecipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher; export function createDecipheriv(algorithm: CipherCCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherCCMOptions): DecipherCCM; - export function createDecipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options: CipherGCMOptions): DecipherGCM; + export function createDecipheriv(algorithm: CipherGCMTypes, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: CipherGCMOptions): DecipherGCM; + export function createDecipheriv(algorithm: string, key: string | Buffer | NodeJS.TypedArray | DataView, iv: string | Buffer | NodeJS.TypedArray | DataView, options?: stream.TransformOptions): Decipher; export interface Decipher extends NodeJS.ReadWriteStream { update(data: Buffer | NodeJS.TypedArray | DataView): Buffer; @@ -5954,12 +5958,12 @@ declare module "crypto" { // setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this; } export interface DecipherCCM extends Decipher { - setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView, options: { plainTextLength: number }): this; - setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this; + setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView): this; + setAAD(buffer: Buffer | NodeJS.TypedArray | DataView, options: { plaintextLength: number }): this; } export interface DecipherGCM extends Decipher { - setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView, options?: { plainTextLength: number }): this; - setAAD(buffer: Buffer | NodeJS.TypedArray | DataView): this; + setAuthTag(buffer: Buffer | NodeJS.TypedArray | DataView): this; + setAAD(buffer: Buffer | NodeJS.TypedArray | DataView, options?: { plaintextLength: number }): this; } export function createSign(algorithm: string, options?: stream.WritableOptions): Signer; diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 266dc5d97e..ba654da264 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -1205,8 +1205,7 @@ namespace crypto_tests { { // crypto_cipher_decipher_dataview_test let key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]); - let clearText: DataView = new DataView( - new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer); + let clearText: DataView = new DataView(new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer); let cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key); let cipherBuffers: Buffer[] = []; cipherBuffers.push(cipher.update(clearText)); @@ -1224,6 +1223,56 @@ namespace crypto_tests { assert.deepEqual(clearText2, clearText); } + { + const key = 'keykeykeykeykeykeykeykey'; + const nonce = crypto.randomBytes(12); + const aad = Buffer.from('0123456789', 'hex'); + + const cipher = crypto.createCipheriv('aes-192-ccm', key, nonce, { + authTagLength: 16 + }); + const plaintext = 'Hello world'; + cipher.setAAD(aad, { + plaintextLength: Buffer.byteLength(plaintext) + }); + const ciphertext = cipher.update(plaintext, 'utf8'); + cipher.final(); + const tag = cipher.getAuthTag(); + + const decipher = crypto.createDecipheriv('aes-192-ccm', key, nonce, { + authTagLength: 16 + }); + decipher.setAuthTag(tag); + decipher.setAAD(aad, { + plaintextLength: ciphertext.length + }); + const receivedPlaintext: string = decipher.update(ciphertext, null, 'utf8'); + decipher.final(); + } + + { + const key = 'keykeykeykeykeykeykeykey'; + const nonce = crypto.randomBytes(12); + const aad = Buffer.from('0123456789', 'hex'); + + const cipher = crypto.createCipheriv('aes-192-gcm', key, nonce); + const plaintext = 'Hello world'; + cipher.setAAD(aad, { + plaintextLength: Buffer.byteLength(plaintext) + }); + const ciphertext = cipher.update(plaintext, 'utf8'); + cipher.final(); + const tag = cipher.getAuthTag(); + + const decipher = crypto.createDecipheriv('aes-192-gcm', key, nonce); + decipher.setAuthTag(tag); + decipher.setAAD(aad, { + plaintextLength: ciphertext.length + }); + const receivedPlaintext: string = decipher.update(ciphertext, null, 'utf8'); + decipher.final(); + } + { // crypto_timingsafeequal_buffer_test let buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);