Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraIdentity/Transfer.swift8// Module : Qyvora Identity9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import QyvoraMath42import QyvoraVault4344/// Signs backups (and any other container using `DetachedSigner`) with an identity.45/// The signature bytes are the binary `IdentitySignature`; the signer encoding is the binary `PublicIdentity`.46public struct IdentityDetachedSigner: DetachedSigner {47 private let identity: Identity48 private let dual: Bool4950 /// With `dual`, the SLH-DSA signature is added to the ML-DSA one.51 public init(_ identity: Identity, dual: Bool = false) {52 self.identity = identity53 self.dual = dual54 }5556 public var signerEncoding: Bytes { identity.publicIdentity.encoded() }5758 public func detachedSignature(over message: Bytes, purpose: String) throws -> Bytes {59 guard let purpose = SignaturePurpose(purpose) else { throw IdentityError.invalidPurpose }60 return try identity.sign(message, purpose: purpose, dual: dual).encoded()61 }62}6364/// Verifies signatures made by `IdentityDetachedSigner`.65public struct IdentityDetachedVerifier: DetachedVerifier {66 private let requireDual: Bool6768 public init(requireDual: Bool = false) {69 self.requireDual = requireDual70 }7172 public func isValid(signature: Bytes, over message: Bytes, signerEncoding: Bytes, purpose: String) -> Bool {73 guard let purpose = SignaturePurpose(purpose), let signer = try? PublicIdentity(encoded: signerEncoding),74 let parsed = try? IdentitySignature(encoded: signature)75 else { return false }76 return parsed.verify(message: message, signer: signer, purpose: purpose, requireDual: requireDual)77 }7879 public func signerFingerprint(of signerEncoding: Bytes) -> Bytes? {80 (try? PublicIdentity(encoded: signerEncoding))?.fingerprint81 }82}8384/// Device-to-device transfer: an encrypted backup file (QYVORA Backup v1, still protected by its own password)85/// wrapped for the receiving device.86///87/// 1. The new iPhone generates an identity and shows its `PublicIdentity` (text form, as QR frames).88/// 2. The old iPhone scans it, calls `create` and shows the package (binary `HybridEnvelope`, as QR frames or AirDrop).89/// 3. The new iPhone calls `open`, then restores the backup with the backup password.90///91/// The package is a `HybridEnvelope` with content type "qyvora/transfer-v1": X25519 + ML-KEM-1024 to the receiver,92/// signed by the sender.93public enum TransferPackage {94 public static let contentType = "qyvora/transfer-v1"9596 public static func create(97 backup: Bytes, to receiver: PublicIdentity, from sender: Identity, dualSignature: Bool = false98 ) throws(IdentityError) -> Bytes {99 try HybridEnvelope.seal(backup, contentType: contentType, to: receiver, from: sender, dualSignature: dualSignature).encoded()100 }101102 /// Returns the backup file and the identity that sent it. Pass `expectedSender` when the sender's identity was103 /// verified beforehand (scanned or compared by fingerprint); otherwise show `sender.fingerprintText` to the user.104 public static func open(105 _ package: Bytes, receiver: Identity, expectedSender: PublicIdentity? = nil106 ) throws(IdentityError) -> (backup: Bytes, sender: PublicIdentity) {107 let envelope = try HybridEnvelope(encoded: package)108 let backup = try envelope.open(with: receiver, expectedSender: expectedSender, expectedContentType: contentType)109 return (backup, envelope.sender)110 }111}112