Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / apple / Shared / EverysyncCoreCrypto.swift

EverysyncCoreCrypto.swift91 lines · 3.9 KiBRaw
1// EverySync Apple shells — CryptoKit AES-GCM bridge (fills the Rust AES gap).
2//
3// The shared Rust crate (`e2ee_pqc`) has NO AES entry points — AES-GCM lives
4// in Kotlin/Swift on every target (JCE on Android/JVM, CryptoKit here).
5// Do NOT add AES to Rust.
6//
7// Layout contract (byte-identical to the JCE `AesGcm` actual + `Crypto.kt`):
8// encrypt output = [12B random nonce] + [ciphertext] + [16B tag]
9// (CryptoKit `AES.GCM.SealedBox.combined` layout is
10// [nonce][ciphertext][tag] — the SAME bytes. Pass `combined` through
11// verbatim; no re-framing.)
12// Key size: 256-bit (32B), from `AesGcm.newContentKey` (Kotlin side, backed
13// by `SecRandomCopyBytes` per `IOS_ACTUALS_NOTES.md`).
14//
15// Wiring: the Kotlin `AesGcm` actual calls these C functions via cinterop
16// (see `../XCFramework/e2eePqc.def` — same module as the 10 PQC fns).
17// Swift never calls Rust directly — only via the shared XCFramework, and the
18// XCFramework never calls Swift except through this C bridge.
19//
20// MAC-CHECK (needs xcodebuild on a Mac):
21// - `CryptoKit` import + `AES.GCM` availability (iOS 13+; deployment target
22// must be >= 13 — set in the Xcode project on the Mac).
23// - `@_cdecl` symbol names match the cinterop header exactly.
24
25import CryptoKit
26import Foundation
27
28/// Encrypts `plaintext` under 256-bit `key`. Returns
29/// `[12B nonce][ciphertext][16B tag]`, or nil on any failure (caller treats
30/// nil as a crypto failure, never as empty plaintext).
31@_cdecl("everysync_aes_gcm_encrypt")
32public func everysyncAESGCMEncrypt(
33 keyPtr: UnsafePointer<UInt8>?, keyLen: Int,
34 ptPtr: UnsafePointer<UInt8>?, ptLen: Int,
35 outPtr: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?,
36 outLen: UnsafeMutablePointer<Int>?
37) -> Int32 {
38 guard let keyPtr, let ptPtr, let outPtr, let outLen,
39 keyLen == 32, ptLen >= 0 else { return 0 }
40 do {
41 let key = SymmetricKey(data: Data(bytes: keyPtr, count: keyLen))
42 let sealed = try AES.GCM.seal(Data(bytes: ptPtr, count: ptLen), using: key)
43 guard let combined = sealed.combined else { return 0 }
44 let bytes = [UInt8](combined)
45 let out = UnsafeMutablePointer<UInt8>.allocate(capacity: bytes.count)
46 out.initialize(from: bytes, count: bytes.count)
47 outPtr.pointee = out
48 outLen.pointee = bytes.count
49 return 1
50 } catch {
51 return 0
52 }
53}
54
55/// Decrypts `combined` ([nonce][ciphertext][tag]) under 256-bit `key`.
56/// Returns plaintext bytes, or nil on any failure (wrong key, tampered tag,
57/// malformed input). Callers (relay envelope pulls) count undecryptables and
58/// keep the cycle — they never fold a crypto failure as an empty log.
59@_cdecl("everysync_aes_gcm_decrypt")
60public func everysyncAESGCMDecrypt(
61 keyPtr: UnsafePointer<UInt8>?, keyLen: Int,
62 ctPtr: UnsafePointer<UInt8>?, ctLen: Int,
63 outPtr: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?,
64 outLen: UnsafeMutablePointer<Int>?
65) -> Int32 {
66 guard let keyPtr, let ctPtr, let outPtr, let outLen,
67 keyLen == 32, ctLen > 12 + 16 else { return 0 }
68 do {
69 let key = SymmetricKey(data: Data(bytes: keyPtr, count: keyLen))
70 let box = try AES.GCM.SealedBox(combined: Data(bytes: ctPtr, count: ctLen))
71 let plain = try AES.GCM.open(box, using: key)
72 let bytes = [UInt8](plain)
73 let out = UnsafeMutablePointer<UInt8>.allocate(capacity: bytes.count)
74 out.initialize(from: bytes, count: bytes.count)
75 outPtr.pointee = out
76 outLen.pointee = bytes.count
77 return 1
78 } catch {
79 return 0
80 }
81}
82
83/// Frees a buffer returned by the two functions above. The Kotlin cinterop
84/// actual MUST copy the bytes first, then call this (same discipline as
85/// Rust `pqc_free`: every `*mut u8 + len` out-param buffer is caller-freed).
86@_cdecl("everysync_crypto_free")
87public func everysyncCryptoFree(ptr: UnsafeMutablePointer<UInt8>?, len: Int) {
88 guard let ptr, len > 0 else { return }
89 ptr.deinitialize(count: len)
90 ptr.deallocate()
91}