Skip to content

Silent Error When Decrypting >=148Kb base64 strings in React Native #1056

@adamblvck

Description

@adamblvck

Describe the bug

Decryption in React Native, specifically tested on iOS, has trouble decrypting files larger than 148Kb.
I'm using Expo. To have Themis React Native work in this environment, a dev build is needed. It worked on Expo SDK 46 (2 years ago), on SDK 49, SDK 50 and now SDK 51 it seems to be able to encrypt rather large files, but not able to decrypt them.

In #994 @djaffer mentions a similar issue. React Native Encryption functions seem to work for files even 120Mb in size, but decryption fails for files >=148Kb in size. For files >=10Mb I get regex stack errors, which might be a React Native limitation.

To Reproduce

Start a fresh SDK 51 installation, install themis, create an EAS build, and launch the app. For your information, Expo SDK 51 runs:

  • "react": "18.2.0"
  • "react-native": "0.74.5"

Next define a dummy generator in App.js:

function getdummy(mbytes) {
    // Calculate the number of bytes
    const bytes = mbytes * 1000000;
    
    // Calculate the length of the base64 string
    // (4 base64 characters represent 3 bytes)
    const base64Length = Math.ceil(bytes / 3) * 4;
    
    // Generate a random string of the calculated length
    let result = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (let i = 0; i < base64Length; i++) {
		    result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    
    return result;
}

Then have a simple function test the encryption as follows, invoked after a button click, or on load of the app:

function testDecrypt = () => {
    try {
        const dummy = getdummy(0.145);
        console.log("DUMMY", dummy.length, dummy.slice(0,20));
        
        const encrypted64 = await secureCellSealWithSymmetricKeyEncrypt64(userPair.priv, dummy);
        console.log("ENCRYPTED", encrypted64.length, encrypted64.slice(0,20));

        const decrypted64 = await secureCellSealWithSymmetricKeyDecrypt64(userPair.priv, encrypted64);
        console.log("DECRYPTED", decrypted64.length, decrypted64.slice(0,20));

        console.log("THEY'RE THE SAME", dummy == decrypted64)
    } catch (error) {
        console.log("MASSIVE ERROR", error);
    }
}

I'm getting the following output for a 140 Kb size:

 LOG  DUMMY 186668 lASDSbaVW2t1qlkwenxg
 LOG  ENCRYPTED 248892 qCKtk0zbO30++uXklq/X
 LOG  DECRYPTED 186668 lASDSbaVW2t1qlkwenxg
 LOG  THEY'RE THE SAME true

For a 145 Kb size, I get a silence error in the bridge:

 LOG  DUMMY 266668 B3TmzBzd20Thk8QDKEHr
 LOG  ENCRYPTED 355560 bdfTmNvOufF1JEDv7FOQ
 LOG  MASSIVE ERROR [Error: Exception in HostFunction: Malformed calls from JS: field sizes are different.

[[121,80],[10,0],[[1759,100,1725019047524,false]],6350]]

We can debug which host functions bugged out by defining a findModuleByModuleAndMethodIds function, then looking them up for processes 121.80 and 80.0:

// bridge debugging
if (global.__fbBatchedBridge) {
	const origMessageQueue = global.__fbBatchedBridge;
	const modules = origMessageQueue._remoteModuleTable;
	const methods = origMessageQueue._remoteMethodTable;
	global.findModuleByModuleAndMethodIds = (moduleId, methodId) => {
	  console.log(`The problematic line code is in: ${modules[moduleId]}.${methods[moduleId][methodId]}`)
	}
}

global.findModuleByModuleAndMethodIds(121, 10);
global.findModuleByModuleAndMethodIds(80, 0);

Which returns the following two possible functions having no parameters passed over the bridge:

 LOG  The problematic line code is in: Themis.secureCellSealWithSymmetricKeyEncrypt64
 LOG  The problematic line code is in: Timing.createTimer

I've found that this type of silent error happens with any decryption, whereas it doesn't happen at the encryption stage. For a 10Mbyte string, encryption works, decryption fails with a max regex stack depth error:

 LOG  DUMMY 13333336 gpPnBQloMKtC4uiFQmym
 LOG  ENCRYPTED 17777784 fG3//XnUadHaNx5qrp20
 LOG  MASSIVE ERROR [RangeError: Maximum regex stack depth reached]

Have less than 10Mbyte chunks is something that I can easily live with, but to encrypt and decrypt files into chunks of less than 150Kb feels like a bridge overhead.

Expected behavior
Two years ago, I was able to decrypt files up to 10Mbyte in size in React Native. Themis Nodejs implementation doesn't suffer from this limitation.

Environment (please complete the following information):

  • OS: iOS 17.5
  • Hardware: iOS Simulator / iPhone 14 Pro
  • Themis version: "react-native-themis": "^0.15.2",
  • Installation way:
    • via package manager
    • Requires EAS Build to make native library work.

Additional context

If any additional context is required, please share away. Generally I would like to know if this perhaps has to do with the new architecture of React Native 18?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions