react-native-quick-crypto

repository·main·Indexed 21 days ago

https://github.com/margelo/react-native-quick-crypto

A high-performance implementation of Node's crypto module for React Native, written in C/C++ using JSI and Nitro Modules. It serves as a fast, native replacement for JS-based polyfills like crypto-browserify. Version 1.1.6 supports React Native 0.75+. Features include a drop-in replacement for Node's crypto API, Argon2 password hashing (including WebCrypto API support), and high-performance BLAKE3 hashing with MAC and KDF modes.

Tokens
81.3K
Snippets
213
Records
305
Agent score
72%

What's inside react-native-quick-crypto

  1. Overview of react-native-quick-crypto (RNQC) modules

    main

    RNQC provides a high-performance cryptographic suite for React Native that mirrors the Node.js crypto API. It is organized into several functional categories:

    Core Modules

    • Install Polyfills: Global polyfill injection for drop-in compatibility with Node.js-style code.
    • Cipher: Symmetric encryption including AES, ChaCha20, and XChaCha20.
    • Hash: Message digests such as SHA-2, SHA-3, and MD5.
    • HMAC: Keyed-hash message authentication.
    • Random: CSPRNG, UUIDs, and random integers.
    • Keys: Key generation (RSA, EC) and KeyObject management.
    • Signing: Digital signatures (RSA, ECDSA, Ed25519).
    • Public Cipher: RSA asymmetric encryption/decryption.
    • Utilities: One-shot hashing, timing-safe comparison, primes, and introspection.

    Key Exchange

    • DiffieHellman: Classic DH key exchange with standard groups.
    • ECDH: Elliptic Curve Diffie-Hellman (P-256, secp256k1).
    • Edwards & Montgomery: Ed25519, Ed448, X25519, and X448 signatures and key exchange.

    Key Derivation

    • PBKDF2: Password-based key derivation (RFC 2898).
    • Scrypt: Memory-hard key derivation.
    • HKDF: Extract-and-Expand KDF (RFC 5869).
    • Argon2: Memory-hard password hashing (PHC winner).
    • BLAKE3: Next-gen high-performance hashing and KDF.

    Advanced

    • Post-Quantum (PQC): ML-DSA / SLH-DSA signatures and ML-KEM key encapsulation.
    • KMAC: Keccak Message Authentication Code (KMAC128/KMAC256).
    • Certificate: SPKAC certificate request processing.
    • X.509: X.509 certificate parsing and validation.
    • Subtle (WebCrypto): W3C Web Cryptography API implementation.
  2. Overview of Post-Quantum Cryptography (PQC) Algorithms

    main

    RNQC implements NIST-standardized quantum-resistant algorithms via OpenSSL 3.6+. These algorithms are designed to be secure against both classical and quantum computer attacks.

    Supported Algorithms

    ML-DSA (FIPS 204)

    Module Lattice Digital Signature Algorithm. A replacement for RSA and ECDSA signatures.

    • ML-DSA-44: NIST Level 2 (General purpose)
    • ML-DSA-65: NIST Level 3 (Recommended)
    • ML-DSA-87: NIST Level 5 (Maximum security)

    ML-KEM (FIPS 203)

    Module Lattice Key Encapsulation Mechanism. A replacement for ECDH key exchange.

    • ML-KEM-512: NIST Level 1
    • ML-KEM-768: NIST Level 3
    • ML-KEM-1024: NIST Level 5

    SLH-DSA (FIPS 205)

    Stateless Hash-Based Digital Signature Algorithm. A conservative alternative to ML-DSA that relies only on hash function security. It offers two variants:

    • s (small signature): Smaller signatures, but significantly slower signing.
    • f (fast): Faster signing, but signatures are 4–6× larger.

    Available parameter sets follow the pattern: slh-dsa-{sha2,shake}-{128,192,256}{s,f}.

  3. What is React Native Quick Crypto (RNQC)?

    main

    RNQC (React Native Quick Crypto) is a high-performance cryptography library for React Native designed to be compatible with Node's crypto API. Unlike many other libraries that act as wrappers, RNQC is a complete re-implementation built on C++ JSI bindings. It provides direct, synchronous access to native cryptographic primitives, making it significantly faster (up to hundreds of times) than standard JavaScript-only solutions.

    Key characteristics:

    • Node.js Compatibility: Implements the exact Node.js crypto API surface.
    • Performance: Uses Nitro Modules to bypass the React Native Bridge, allowing for zero-serialization and direct memory access.
    • Security: Maps primitives directly to the platform-native OpenSSL library (bundled or system on Android, via CocoaPods on iOS).
    • Non-blocking: Heavy cryptographic tasks are offloaded to a dedicated native thread pool to prevent UI freezes.
  4. Compare RNQC with other React Native crypto libraries

    main

    RNQC (react-native-quick-crypto) is designed as a high-performance alternative to existing React Native crypto solutions. It is distinguished by its use of the Nitro (Native C++) architecture, which provides 1:1 Node.js API compatibility, full support for synchronous methods, and off-main-thread execution for thread safety.

    Key differences compared to other libraries:

    • Performance: Uses Nitro (Native C++) vs. Bridge-based (slow) or partial JSI.
    • API: 1:1 Node.js compatibility vs. custom APIs.
    • Sync Methods: Fully supported in RNQC.
    • Thread Safety: Executes off-main-thread to prevent UI blocking, whereas bridge-based solutions often block the JS thread.
  5. Check Node.js and WebCrypto API implementation coverage

    main

    The react-native-quick-crypto library aims to provide compatibility with the standard Node.js crypto API and the W3C WebCrypto API. You can verify the implementation status of specific methods and algorithms using the coverage documentation.

    Status indicators used in the coverage tracking:

    • Implemented: Full support for the API/algorithm.
    • Partial: The API is present but not fully implemented.
    • Missing: The API/algorithm is not implemented.
  6. Security Audit: Runtime Dependencies

    main

    The runtime dependencies of react-native-quick-crypto are verified as clean and free of known vulnerabilities. The following 6 runtime dependencies are used in the published tree:

    • @craftzdog/react-native-buffer 6.1.0
    • events 3.3.0
    • readable-stream 4.5.2
    • safe-buffer ^5.2.1
    • string_decoder ^1.3.0
    • util 0.12.5

    Note: Vulnerabilities reported by tools like bun audit often trace to developer-only tooling (e.g., expo, eslint, release-it) or the example app's dev tree, which do not affect the consumer's production bundle.

  7. What is KMAC and how does it differ from HMAC?

    main
    KMAC (Keccak Message Authentication Code) is a MAC function based on the Keccak (SHA-3) permutation. Unlike HMAC, which uses a nested hash construction, KMAC uses the Keccak sponge directly. This makes KMAC simpler, more efficient, and inherently resistant to length-extension attacks. KMAC also supports customization strings for domain separation.
  8. Security risks in RSA implementation

    main

    The RSA implementation has specific security considerations regarding error handling and key management:

    • Padding Oracles: RSA error messages may propagate internal OpenSSL strings. Specifically, publicDecrypt may have distinguishable error paths (e.g., distinguishing between an empty buffer and an exception), which can facilitate Bleichenbacher-style padding oracle attacks.
    • Prototype Pollution: The library uses 'key' in key checks during key preparation. If Object.prototype.key is polluted, it can trigger incorrect code paths in preparePublicCipherKey or preparePrivateCipherKey.
    • Key Size: While initWithSize enforces a 2048-bit minimum, other initialization methods like init or DhKeyPair may have different or inconsistent enforcement levels.
  9. Security considerations for PBKDF2

    main

    When using PBKDF2, be aware of the following implementation details and potential risks identified during security audits:

    • Return Value Validation: Ensure you check the return values of PKCS5_PBKDF2_HMAC and fastpbkdf2_hmac_* calls. In some scenarios, failures might return uninitialized buffer data rather than throwing an error.
    • Parameter Types: The C++ layer may not reject keylen=0, which can lead to implementation-defined behavior. Additionally, iteration counts are subject to double to uint32_t truncation at the C++ layer.
    • Memory Safety: Derived key material is not automatically zeroed out in memory after use. Developers should be cautious when handling sensitive keys in memory.
    • Input Validation: Passing a null ArrayBuffer as a password may lead to undefined behavior due to lack of null checks in the underlying C++ layer.
  10. Stream API for Hash

    main

    Because Hash implements stream.Transform, you can use standard Node.js stream methods to process data.

    • hash.write(chunk[, encoding][, callback]): Writes data to the stream.
    • hash.pipe(destination): Pipes the output digest to a destination stream.
    import fs from 'fs';
    const hash = createHash('sha256');
    const input = fs.createReadStream('file.txt');
    input.pipe(hash).pipe(process.stdout);