π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move tvOS Cast app to the cast UniFFI crate
Manav Rathifdc0674ea64b
diff --git a/mobile/native/apple/apps/cast/Cast.xcodeproj/project.pbxproj b/mobile/native/apple/apps/cast/Cast.xcodeproj/project.pbxprojindex 710190b1b5..fb61ec3434 100644--- a/mobile/native/apple/apps/cast/Cast.xcodeproj/project.pbxproj+++ b/mobile/native/apple/apps/cast/Cast.xcodeproj/project.pbxproj@@ -113,7 +113,7 @@isa = PBXNativeTarget;buildConfigurationList = DA72BC872E5F87A100D128FF /* Build configuration list for PBXNativeTarget "Cast" */;buildPhases = (- DAEC00012F40000000CABA11 /* Build Rust (ente-crypto) */,+ DAEC00012F40000000CABA11 /* Build Rust (cast) */,DA72BC5D2E5F87A000D128FF /* Sources */,DA72BC5E2E5F87A000D128FF /* Frameworks */,DA72BC5F2E5F87A000D128FF /* Resources */,@@ -245,16 +245,16 @@/* End PBXResourcesBuildPhase section *//* Begin PBXShellScriptBuildPhase section */- DAEC00012F40000000CABA11 /* Build Rust (ente-crypto) */ = {+ DAEC00012F40000000CABA11 /* Build Rust (cast) */ = {isa = PBXShellScriptBuildPhase;buildActionMask = 2147483647;files = ();inputPaths = ();- name = "Build Rust (ente-crypto)";+ name = "Build Rust (cast)";outputPaths = (- "$(TARGET_TEMP_DIR)/ente_crypto_rust/libente_crypto_ffi.a",+ "$(TARGET_TEMP_DIR)/cast_rust/libcast.a",);runOnlyForDeploymentPostprocessing = 0;shellPath = /bin/sh;@@ -448,9 +448,10 @@MARKETING_VERSION = 1.1;OTHER_LDFLAGS = ("$(inherited)",- "-force_load",- "$(TARGET_TEMP_DIR)/ente_crypto_rust/libente_crypto_ffi.a",+ "$(TARGET_TEMP_DIR)/cast_rust/libcast.a",);+ OTHER_SWIFT_FLAGS = "$(inherited) -Xcc -fmodule-map-file=$(SRCROOT)/Cast/Generated/castFFI.modulemap";+ SWIFT_INCLUDE_PATHS = "$(inherited) $(SRCROOT)/Cast/Generated";PRODUCT_BUNDLE_IDENTIFIER = io.ente.photos.tv.cast;PRODUCT_NAME = "$(TARGET_NAME)";PROVISIONING_PROFILE_SPECIFIER = "";@@ -486,9 +487,10 @@MARKETING_VERSION = 1.1;OTHER_LDFLAGS = ("$(inherited)",- "-force_load",- "$(TARGET_TEMP_DIR)/ente_crypto_rust/libente_crypto_ffi.a",+ "$(TARGET_TEMP_DIR)/cast_rust/libcast.a",);+ OTHER_SWIFT_FLAGS = "$(inherited) -Xcc -fmodule-map-file=$(SRCROOT)/Cast/Generated/castFFI.modulemap";+ SWIFT_INCLUDE_PATHS = "$(inherited) $(SRCROOT)/Cast/Generated";PRODUCT_BUNDLE_IDENTIFIER = io.ente.frame.tv.cast;PRODUCT_NAME = "$(TARGET_NAME)";PROVISIONING_PROFILE_SPECIFIER = "";diff --git a/mobile/native/apple/apps/cast/Cast/Services/Crypto.swift b/mobile/native/apple/apps/cast/Cast/Services/Crypto.swiftindex 5f2539542c..7a231853c2 100644--- a/mobile/native/apple/apps/cast/Cast/Services/Crypto.swift+++ b/mobile/native/apple/apps/cast/Cast/Services/Crypto.swift@@ -1,180 +1,20 @@import Foundation-enum CryptoError: Error, LocalizedError {- case failed(String)- case invalidOutput(String)-- var errorDescription: String? {- switch self {- case .failed(let message), .invalidOutput(let message):- return message- }- }-}--@_silgen_name("ente_crypto_string_free")-private func rustStringFree(_ ptr: UnsafeMutablePointer<CChar>?)--@_silgen_name("ente_crypto_generate_keypair_b64")-private func rustGenerateKeyPair(- _ outPublicKeyB64: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- _ outSecretKeyB64: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- _ outError: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?-) -> Int32--@_silgen_name("ente_crypto_secretbox_open_b64")-private func rustSecretBoxOpen(- _ cipherTextB64: UnsafePointer<CChar>?,- _ nonceB64: UnsafePointer<CChar>?,- _ keyB64: UnsafePointer<CChar>?,- _ outPlainTextB64: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- _ outError: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?-) -> Int32--@_silgen_name("ente_crypto_sealed_box_open_b64")-private func rustSealedBoxOpen(- _ cipherTextB64: UnsafePointer<CChar>?,- _ publicKeyB64: UnsafePointer<CChar>?,- _ secretKeyB64: UnsafePointer<CChar>?,- _ outPlainTextB64: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- _ outError: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?-) -> Int32--@_silgen_name("ente_crypto_secretstream_decrypt_b64")-private func rustDecryptSecretStream(- _ encryptedDataB64: UnsafePointer<CChar>?,- _ headerB64: UnsafePointer<CChar>?,- _ keyB64: UnsafePointer<CChar>?,- _ outPlainTextB64: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- _ outError: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?-) -> Int32-enum Crypto {static func generateKeyPair() throws -> (publicKey: Data, privateKey: Data) {- let (publicKey, privateKey) = try callForTwoStrings { out1, out2, error in- rustGenerateKeyPair(out1, out2, error)- }- return (- publicKey: try decodeBase64(publicKey, "public key"),- privateKey: try decodeBase64(privateKey, "private key")- )+ let keyPair = Cast.generateKeyPair()+ return (publicKey: keyPair.publicKey, privateKey: keyPair.privateKey)}static func secretBoxOpen(cipherText: Data, nonce: Data, key: Data) throws -> Data {- guard nonce.count == 24, key.count == 32 else {- throw CryptoError.failed("invalid SecretBox input")- }-- let plainText = try callForString(- cipherText.base64EncodedString(),- nonce.base64EncodedString(),- key.base64EncodedString()- ) { cipherText, nonce, key, output, error in- rustSecretBoxOpen(cipherText, nonce, key, output, error)- }- return try decodeBase64(plainText, "SecretBox plaintext")+ try Cast.openSecretBox(ciphertext: cipherText, nonce: nonce, key: key)}static func sealedBoxOpen(cipherText: Data, publicKey: Data, secretKey: Data) throws -> Data {- guard publicKey.count == 32, secretKey.count == 32 else {- throw CryptoError.failed("invalid SealedBox input")- }-- let plainText = try callForString(- cipherText.base64EncodedString(),- publicKey.base64EncodedString(),- secretKey.base64EncodedString()- ) { cipherText, publicKey, secretKey, output, error in- rustSealedBoxOpen(cipherText, publicKey, secretKey, output, error)- }- return try decodeBase64(plainText, "SealedBox plaintext")+ try Cast.openSealedBox(ciphertext: cipherText, publicKey: publicKey, privateKey: secretKey)}static func decryptSecretStream(encryptedData: Data, key: Data, header: Data) throws -> Data {- guard key.count == 32, header.count == 24 else {- throw CryptoError.failed("invalid secretstream input")- }-- let plainText = try callForString(- encryptedData.base64EncodedString(),- header.base64EncodedString(),- key.base64EncodedString()- ) { encryptedData, header, key, output, error in- rustDecryptSecretStream(encryptedData, header, key, output, error)- }- return try decodeBase64(plainText, "secretstream plaintext")- }-- private static func decodeBase64(_ value: String, _ label: String) throws -> Data {- guard let data = Data(base64Encoded: value) else {- throw CryptoError.invalidOutput("invalid base64 \(label)")- }- return data- }-- private static func callForString(- _ input1: String,- _ input2: String,- _ input3: String,- _ call: (- UnsafePointer<CChar>?,- UnsafePointer<CChar>?,- UnsafePointer<CChar>?,- UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?- ) -> Int32- ) throws -> String {- var output: UnsafeMutablePointer<CChar>? = nil- var error: UnsafeMutablePointer<CChar>? = nil- let status = input1.withCString { cInput1 in- input2.withCString { cInput2 in- input3.withCString { cInput3 in- call(cInput1, cInput2, cInput3, &output, &error)- }- }- }-- defer {- rustStringFree(output)- rustStringFree(error)- }-- guard status == 0 else {- throw CryptoError.failed(error.map { String(cString: $0) } ?? "unknown Rust crypto error")- }- guard let output, let value = String(validatingUTF8: output) else {- throw CryptoError.invalidOutput("missing Rust crypto output")- }- return value- }-- private static func callForTwoStrings(- _ call: (- UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?,- UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?- ) -> Int32- ) throws -> (String, String) {- var output1: UnsafeMutablePointer<CChar>? = nil- var output2: UnsafeMutablePointer<CChar>? = nil- var error: UnsafeMutablePointer<CChar>? = nil- let status = call(&output1, &output2, &error)-- defer {- rustStringFree(output1)- rustStringFree(output2)- rustStringFree(error)- }-- guard status == 0 else {- throw CryptoError.failed(error.map { String(cString: $0) } ?? "unknown Rust crypto error")- }- guard let output1,- let output2,- let value1 = String(validatingUTF8: output1),- let value2 = String(validatingUTF8: output2) else {- throw CryptoError.invalidOutput("missing Rust crypto output")- }- return (value1, value2)+ try Cast.decryptSecretStream(encryptedData: encryptedData, header: header, key: key)}}diff --git a/mobile/native/apple/apps/cast/scripts/build-rust.sh b/mobile/native/apple/apps/cast/scripts/build-rust.shindex bf268428e5..0d4c27ea57 100644--- a/mobile/native/apple/apps/cast/scripts/build-rust.sh+++ b/mobile/native/apple/apps/cast/scripts/build-rust.sh@@ -2,14 +2,21 @@set -eu# This script is invoked from Xcode's "Run Script" build phase.-# It builds the Rust crypto static lib for the current tvOS target.+# It builds the cast UniFFI static lib for the current tvOS target.REPO_ROOT=$(cd "$SRCROOT/../../../../.." && pwd)-OUT_DIR="$TARGET_TEMP_DIR/ente_crypto_rust"-LIB=libente_crypto_ffi.a+GENERATED_DIR="$SRCROOT/Cast/Generated"+OUT_DIR="$TARGET_TEMP_DIR/cast_rust"+LIB=libcast.aexport PATH="${CARGO_HOME:-$HOME/.cargo}/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"+if [ ! -f "$GENERATED_DIR/cast.swift" ] || [ ! -f "$GENERATED_DIR/castFFI.h" ] || [ ! -f "$GENERATED_DIR/castFFI.modulemap" ]; then+ echo "error: missing generated cast UniFFI bindings in $GENERATED_DIR" >&2+ echo " run: (cd $REPO_ROOT/rust && cargo codegen native cast)" >&2+ exit 1+fi+mkdir -p "$OUT_DIR"# Rust supports tvOS targets, but rustup does not ship precompiled std for them.@@ -43,7 +50,7 @@ for arch in $ARCHS; doCC="$clang" \CFLAGS="-isysroot $sdk" \RUSTFLAGS="$rustflags" \- cargo +nightly build -Z build-std --locked -p ente_crypto_ffi --target "$target" $release_flag+ cargo +nightly build -Z build-std --locked -p ente-cast-uniffi --target "$target" $release_flag)built="$REPO_ROOT/rust/target/$target/$profile/$LIB"