react-native-quick-crypto icon indicating copy to clipboard operation
react-native-quick-crypto copied to clipboard

🐛 aes-128-cbc not in CipherCCMTypes in createCipheriv function

Open Miigaarino opened this issue 1 year ago • 1 comments

What's happening?

After upgrading from 0.6.1 to 0.7.1 function createCipheriv shows type error saying Argument of type '"aes-128-cbc"' is not assignable to parameter of type 'CipherGCMTypes' image

Reproducible Code

import { Buffer } from "@craftzdog/react-native-buffer";
import crypto from "react-native-quick-crypto";

const KEY = ...
const IV = ...

const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(KEY, "utf8"), Buffer.from(IV, "utf8"));

Relevant log output

Argument of type '"aes-128-cbc"' is not assignable to parameter of type 'CipherGCMTypes'.

Device

Iphone 11

QuickCrypto Version

0.7.1

Can you reproduce this issue in the QuickCrypto Example app?

Yes, I can reproduce the same issue in the Example app here

Additional information

Miigaarino avatar Jul 24 '24 09:07 Miigaarino

@Miigaarino,

Are you sure that algorithm is allowed in Node crypto? I don't see it in @node/types here.

@shamilovtim made the types a bit stronger than any in #247 by using @node/types.

See if you can find somewhere online where createCipheriv() should support aes-128-cbc 🙏

boorad avatar Aug 14 '24 01:08 boorad

In the implementation file (src/Cipher.ts), the createCipheriv function accepts algorithm strings: https://github.com/margelo/react-native-quick-crypto/blob/v0.7.3/src/Cipher.ts#L345-L356

However, in the type definition file (node_modules/react-native-quick-crypto/lib/typescript/Cipher.d.ts), the algorithm parameter is restricted to CipherCCM, CipherOCB, CipherGCM.

export declare function createCipheriv(algorithm: CipherCCMTypes, key: BinaryLikeNode, iv: BinaryLike, options: CipherCCMOptions): CipherCCM;
export declare function createCipheriv(algorithm: CipherOCBTypes, key: BinaryLikeNode, iv: BinaryLike, options: CipherOCBOptions): CipherOCB;
export declare function createCipheriv(algorithm: CipherGCMTypes, key: BinaryLikeNode, iv: BinaryLike, options?: CipherGCMOptions): CipherGCM;
image

blluv avatar Aug 31 '24 07:08 blluv

In the implementation file (src/Cipher.ts), the createCipheriv function accepts algorithm strings: https://github.com/margelo/react-native-quick-crypto/blob/v0.7.3/src/Cipher.ts#L345-L356

However, in the type definition file (node_modules/react-native-quick-crypto/lib/typescript/Cipher.d.ts), the algorithm parameter is restricted to CipherCCM, CipherOCB, CipherGCM.

export declare function createCipheriv(algorithm: CipherCCMTypes, key: BinaryLikeNode, iv: BinaryLike, options: CipherCCMOptions): CipherCCM;
export declare function createCipheriv(algorithm: CipherOCBTypes, key: BinaryLikeNode, iv: BinaryLike, options: CipherOCBOptions): CipherOCB;
export declare function createCipheriv(algorithm: CipherGCMTypes, key: BinaryLikeNode, iv: BinaryLike, options?: CipherGCMOptions): CipherGCM;
image

That's just one of the overloads. Above it you'll see the other overloads that narrow the algorithm based on the combination of params passed.

shamilovtim avatar Aug 31 '24 10:08 shamilovtim

@Miigaarino @blluv if this is a bug in this case the bug is in @node/types. Consider opening this issue or a PR in https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/crypto.d.ts#L677-L679 and following up there with either an issue or a PR.

shamilovtim avatar Aug 31 '24 10:08 shamilovtim

I think I've handled this a bit better in #419

boorad avatar Sep 01 '24 01:09 boorad

My ask above was for someone to validate if Node supports this. Actually I did handle it in #419 but might not have gotten aes-128-cbc. I'll add a test for all of them soon.

boorad avatar Sep 01 '24 01:09 boorad