Page MenuHomePhabricator

[native] Implement iOS encryption for Rust
ClosedPublic

Authored by michal on Nov 20 2023, 9:37 AM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, Dec 27, 9:45 AM
Unknown Object (File)
Fri, Dec 27, 9:45 AM
Unknown Object (File)
Fri, Dec 27, 9:45 AM
Unknown Object (File)
Fri, Dec 27, 9:43 AM
Unknown Object (File)
Fri, Dec 27, 9:33 AM
Unknown Object (File)
Thu, Dec 26, 3:50 PM
Unknown Object (File)
Mon, Dec 9, 10:09 AM
Unknown Object (File)
Nov 3 2024, 12:02 PM
Subscribers

Details

Summary

ENG-5343

iOS implementation of the Rust encryption methods. Uses already existing functions.

Depends on D9932

Test Plan

Run this code on a physical iOS device:

fn test(promise_id: u32) {
  RUNTIME.spawn(async move {
    let f = move || -> Result<String, cxx::Exception> {
      let key = &mut [0; constants::aes::KEY_SIZE];
      ffi::generate_key(key)?;
      let plaintext = &mut [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      let sealed_data =
        &mut [0; 10 + constants::aes::IV_LENGTH + constants::aes::TAG_LENGTH];
      ffi::encrypt(key, plaintext, sealed_data)?;
      let plaintext2 = &mut [0; 10];
      ffi::decrypt(key, sealed_data, plaintext2)?;

      Ok(format!(
        "
    Key: {key:?}
    Plaintext: {plaintext:?}
    Sealed Data: {sealed_data:?}
    Plaintext2: {plaintext2:?}"
      ))
    };
    handle_string_result_as_callback(f(), promise_id);
  });
}

Example output:

Key: [25, 165, 133, 47, 109, 170, 104, 88, 70, 47, 174, 67, 235, 86, 27, 48, 117, 195, 150, 178, 120, 34, 117, 120, 115, 73, 135, 50, 216, 152, 37, 40]
Plaintext: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sealed Data: [75, 154, 51, 64, 189, 35, 46, 50, 39, 181, 228, 130, 86, 208, 62, 48, 14, 103, 18, 191, 92, 117, 248, 186, 35, 8, 134, 109, 251, 54, 182, 192, 74, 244, 175, 46, 228, 120]
Plaintext2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Also I've run it a few times anc checked that the key was different each time. Also run the encryption with invalid length of sealed_data and decryption with invalid data and in both cases it failed gracefully with Result::Err.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable