Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): add ocb modes to crypto, fix ccm and gcm iv #59761

Merged
merged 1 commit into from Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 28 additions & 4 deletions types/node/crypto.d.ts
Expand Up @@ -646,6 +646,7 @@ declare module 'crypto' {
}
type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm' | 'chacha20-poly1305';
type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
type CipherOCBTypes = 'aes-128-ocb' | 'aes-192-ocb' | 'aes-256-ocb';
type BinaryLike = string | NodeJS.ArrayBufferView;
type CipherKey = BinaryLike | KeyObject;
interface CipherCCMOptions extends stream.TransformOptions {
Expand All @@ -654,6 +655,9 @@ declare module 'crypto' {
interface CipherGCMOptions extends stream.TransformOptions {
authTagLength?: number | undefined;
}
interface CipherOCBOptions extends stream.TransformOptions {
authTagLength: number;
peterblazejewicz marked this conversation as resolved.
Show resolved Hide resolved
}
/**
* Creates and returns a `Cipher` object that uses the given `algorithm` and`password`.
*
Expand Down Expand Up @@ -720,8 +724,9 @@ declare module 'crypto' {
* @since v0.1.94
* @param options `stream.transform` options
*/
function createCipheriv(algorithm: CipherCCMTypes, key: CipherKey, iv: BinaryLike | null, options: CipherCCMOptions): CipherCCM;
function createCipheriv(algorithm: CipherGCMTypes, key: CipherKey, iv: BinaryLike | null, options?: CipherGCMOptions): CipherGCM;
function createCipheriv(algorithm: CipherCCMTypes, key: CipherKey, iv: BinaryLike, options: CipherCCMOptions): CipherCCM;
function createCipheriv(algorithm: CipherOCBTypes, key: CipherKey, iv: BinaryLike, options: CipherOCBOptions): CipherOCB;
function createCipheriv(algorithm: CipherGCMTypes, key: CipherKey, iv: BinaryLike, options?: CipherGCMOptions): CipherGCM;
function createCipheriv(algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions): Cipher;
/**
* Instances of the `Cipher` class are used to encrypt data. The class can be
Expand Down Expand Up @@ -907,6 +912,15 @@ declare module 'crypto' {
): this;
getAuthTag(): Buffer;
}
interface CipherOCB extends Cipher {
setAAD(
buffer: NodeJS.ArrayBufferView,
options?: {
plaintextLength: number;
}
): this;
getAuthTag(): Buffer;
}
/**
* Creates and returns a `Decipher` object that uses the given `algorithm` and`password` (key).
*
Expand Down Expand Up @@ -961,8 +975,9 @@ declare module 'crypto' {
* @since v0.1.94
* @param options `stream.transform` options
*/
function createDecipheriv(algorithm: CipherCCMTypes, key: CipherKey, iv: BinaryLike | null, options: CipherCCMOptions): DecipherCCM;
function createDecipheriv(algorithm: CipherGCMTypes, key: CipherKey, iv: BinaryLike | null, options?: CipherGCMOptions): DecipherGCM;
function createDecipheriv(algorithm: CipherCCMTypes, key: CipherKey, iv: BinaryLike, options: CipherCCMOptions): DecipherCCM;
function createDecipheriv(algorithm: CipherOCBTypes, key: CipherKey, iv: BinaryLike, options: CipherOCBOptions): DecipherOCB;
function createDecipheriv(algorithm: CipherGCMTypes, key: CipherKey, iv: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
function createDecipheriv(algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;
/**
* Instances of the `Decipher` class are used to decrypt data. The class can be
Expand Down Expand Up @@ -1133,6 +1148,15 @@ declare module 'crypto' {
}
): this;
}
interface DecipherOCB extends Decipher {
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
setAAD(
buffer: NodeJS.ArrayBufferView,
options?: {
plaintextLength: number;
}
): this;
}
interface PrivateKeyInput {
key: string | Buffer;
format?: KeyFormat | undefined;
Expand Down
28 changes: 28 additions & 0 deletions types/node/test/crypto.ts
Expand Up @@ -189,6 +189,34 @@ import { promisify } from 'node:util';
decipher.final();
}

{
// crypto_cipheriv_decipheriv_aad_ocb_test
const key = 'keykeykeykeykeykeykeykey';
const iv = crypto.randomBytes(12);
const aad = Buffer.from('0123456789', 'hex');

const cipher = crypto.createCipheriv('aes-192-ocb', key, iv, { authTagLength: 16 });
const plaintext = 'Hello world';
cipher.setAAD(aad, {
plaintextLength: Buffer.byteLength(plaintext),
});
const ciphertext = Buffer.concat([
cipher.update(plaintext, 'utf8'),
cipher.final(),
]);
const tag = cipher.getAuthTag();

const decipher = crypto.createDecipheriv('aes-192-ocb', key, iv, { authTagLength: 16 });
decipher.setAuthTag(tag);
decipher.setAAD(aad, {
plaintextLength: ciphertext.length,
});
const receivedPlaintext: Buffer = Buffer.concat([
decipher.update(ciphertext),
decipher.final(),
]);
}

{
// crypto_cipheriv_decipheriv_cbc_string_encoding_test
const key: string | null = 'keykeykeykeykeykeykeykey';
Expand Down
32 changes: 28 additions & 4 deletions types/node/v12/crypto.d.ts
Expand Up @@ -167,6 +167,7 @@ declare module 'crypto' {

type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm';
type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
type CipherOCBTypes = 'aes-128-ocb' | 'aes-192-ocb' | 'aes-256-ocb';

type BinaryLike = string | NodeJS.ArrayBufferView;

Expand All @@ -178,6 +179,9 @@ declare module 'crypto' {
interface CipherGCMOptions extends stream.TransformOptions {
authTagLength?: number | undefined;
}
interface CipherOCBOptions extends stream.TransformOptions {
authTagLength: number;
}
/** @deprecated since v10.0.0 use createCipheriv() */
function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
/** @deprecated since v10.0.0 use createCipheriv() */
Expand All @@ -188,13 +192,19 @@ declare module 'crypto' {
function createCipheriv(
algorithm: CipherCCMTypes,
key: CipherKey,
iv: BinaryLike | null,
iv: BinaryLike,
options: CipherCCMOptions,
): CipherCCM;
function createCipheriv(
algorithm: CipherOCBTypes,
key: CipherKey,
iv: BinaryLike,
options: CipherOCBOptions,
): CipherOCB;
function createCipheriv(
algorithm: CipherGCMTypes,
key: CipherKey,
iv: BinaryLike | null,
iv: BinaryLike,
options?: CipherGCMOptions,
): CipherGCM;
function createCipheriv(
Expand Down Expand Up @@ -232,6 +242,10 @@ declare module 'crypto' {
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
getAuthTag(): Buffer;
}
interface CipherOCB extends Cipher {
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
getAuthTag(): Buffer;
}
/** @deprecated since v10.0.0 use createDecipheriv() */
function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
/** @deprecated since v10.0.0 use createDecipheriv() */
Expand All @@ -242,13 +256,19 @@ declare module 'crypto' {
function createDecipheriv(
algorithm: CipherCCMTypes,
key: BinaryLike,
iv: BinaryLike | null,
iv: BinaryLike,
options: CipherCCMOptions,
): DecipherCCM;
function createDecipheriv(
algorithm: CipherOCBTypes,
key: BinaryLike,
iv: BinaryLike,
options: CipherOCBOptions,
): DecipherOCB;
function createDecipheriv(
algorithm: CipherGCMTypes,
key: BinaryLike,
iv: BinaryLike | null,
iv: BinaryLike,
options?: CipherGCMOptions,
): DecipherGCM;
function createDecipheriv(
Expand Down Expand Up @@ -286,6 +306,10 @@ declare module 'crypto' {
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
}
interface DecipherOCB extends Decipher {
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
}

interface PrivateKeyInput {
key: string | Buffer;
Expand Down
27 changes: 27 additions & 0 deletions types/node/v12/test/crypto.ts
Expand Up @@ -169,6 +169,33 @@ import { promisify } from 'util';
decipher.final();
}

{
const key = 'keykeykeykeykeykeykeykey';
const iv = crypto.randomBytes(12);
const aad = Buffer.from('0123456789', 'hex');

const cipher = crypto.createCipheriv('aes-192-ocb', key, iv, { authTagLength: 16 });
const plaintext = 'Hello world';
cipher.setAAD(aad, {
plaintextLength: Buffer.byteLength(plaintext),
});
const ciphertext = Buffer.concat([
cipher.update(plaintext, 'utf8'),
cipher.final(),
]);
const tag = cipher.getAuthTag();

const decipher = crypto.createDecipheriv('aes-192-ocb', key, iv, { authTagLength: 16 });
decipher.setAuthTag(tag);
decipher.setAAD(aad, {
plaintextLength: ciphertext.length,
});
const receivedPlaintext: Buffer = Buffer.concat([
decipher.update(ciphertext),
decipher.final(),
]);
}

{
const key: string | null = 'keykeykeykeykeykeykeykey';
const nonce = crypto.randomBytes(12);
Expand Down
32 changes: 28 additions & 4 deletions types/node/v14/crypto.d.ts
Expand Up @@ -189,6 +189,7 @@ declare module 'crypto' {

type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm' | 'chacha20-poly1305';
type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
type CipherOCBTypes = 'aes-128-ocb' | 'aes-192-ocb' | 'aes-256-ocb';

type BinaryLike = string | NodeJS.ArrayBufferView;

Expand All @@ -200,6 +201,9 @@ declare module 'crypto' {
interface CipherGCMOptions extends stream.TransformOptions {
authTagLength?: number | undefined;
}
interface CipherOCBOptions extends stream.TransformOptions {
authTagLength: number;
}
/** @deprecated since v10.0.0 use `createCipheriv()` */
function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
/** @deprecated since v10.0.0 use `createCipheriv()` */
Expand All @@ -210,13 +214,19 @@ declare module 'crypto' {
function createCipheriv(
algorithm: CipherCCMTypes,
key: CipherKey,
iv: BinaryLike | null,
iv: BinaryLike,
options: CipherCCMOptions,
): CipherCCM;
function createCipheriv(
algorithm: CipherOCBTypes,
key: CipherKey,
iv: BinaryLike,
options: CipherOCBOptions,
): CipherOCB;
function createCipheriv(
algorithm: CipherGCMTypes,
key: CipherKey,
iv: BinaryLike | null,
iv: BinaryLike,
options?: CipherGCMOptions,
): CipherGCM;
function createCipheriv(
Expand Down Expand Up @@ -246,6 +256,10 @@ declare module 'crypto' {
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
getAuthTag(): Buffer;
}
interface CipherOCB extends Cipher {
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
getAuthTag(): Buffer;
}
/** @deprecated since v10.0.0 use `createDecipheriv()` */
function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
/** @deprecated since v10.0.0 use `createDecipheriv()` */
Expand All @@ -256,13 +270,19 @@ declare module 'crypto' {
function createDecipheriv(
algorithm: CipherCCMTypes,
key: CipherKey,
iv: BinaryLike | null,
iv: BinaryLike,
options: CipherCCMOptions,
): DecipherCCM;
function createDecipheriv(
algorithm: CipherOCBTypes,
key: CipherKey,
iv: BinaryLike,
options: CipherOCBOptions,
): DecipherOCB;
function createDecipheriv(
algorithm: CipherGCMTypes,
key: CipherKey,
iv: BinaryLike | null,
iv: BinaryLike,
options?: CipherGCMOptions,
): DecipherGCM;
function createDecipheriv(
Expand Down Expand Up @@ -292,6 +312,10 @@ declare module 'crypto' {
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
}
interface DecipherOCB extends Decipher {
setAuthTag(buffer: NodeJS.ArrayBufferView): this;
setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
}

interface PrivateKeyInput {
key: string | Buffer;
Expand Down
28 changes: 28 additions & 0 deletions types/node/v14/test/crypto.ts
Expand Up @@ -187,6 +187,34 @@ import { promisify } from 'node:util';
decipher.final();
}

{
// crypto_cipheriv_decipheriv_aad_ocb_test
const key = 'keykeykeykeykeykeykeykey';
const iv = crypto.randomBytes(12);
const aad = Buffer.from('0123456789', 'hex');

const cipher = crypto.createCipheriv('aes-192-ocb', key, iv, { authTagLength: 16 });
const plaintext = 'Hello world';
cipher.setAAD(aad, {
plaintextLength: Buffer.byteLength(plaintext),
});
const ciphertext = Buffer.concat([
cipher.update(plaintext, 'utf8'),
cipher.final(),
]);
const tag = cipher.getAuthTag();

const decipher = crypto.createDecipheriv('aes-192-ocb', key, iv, { authTagLength: 16 });
decipher.setAuthTag(tag);
decipher.setAAD(aad, {
plaintextLength: ciphertext.length,
});
const receivedPlaintext: Buffer = Buffer.concat([
decipher.update(ciphertext),
decipher.final(),
]);
}

{
// crypto_cipheriv_decipheriv_cbc_string_encoding_test
const key: string | null = 'keykeykeykeykeykeykeykey';
Expand Down