Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Vault9// File : apple/SharedVaultKit/Tests/SharedVaultKitTests/SecureEnclaveTests.swift10// Module : SharedVaultKitTests11// Repository : https://git.spboucher.ai/sp-vault12// Project Website : https://vault.spboucher.ai13// Created : 2026-11-1914// Last Modified : 2026-11-1915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author : Simon-Pierre Boucher21// Position : Professor of Finance22// Institution : Université du Québec en Outaouais (UQO)23// Department : Département des sciences administratives24// Organization : SP Boucher25//26// Website : https://spboucher.ai27// Git : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General : info@spboucher.ai34// Professional : contact@spboucher.ai35// Direct : simon@spboucher.ai36// Research : research@spboucher.ai37// Development : dev@spboucher.ai38// Support : support@spboucher.ai39// Security/Admin : admin@spboucher.ai40// Notifications : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification : Public67// Distribution : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License : None specified (no license file in this repository)81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import CryptoKit89import XCTest90@testable import SharedVaultKit9192/// Proves that CryptoKit's HPKE.Recipient accepts a Secure Enclave key-agreement key through `DeviceKEMKey`.93/// The keys are created WITHOUT user presence (`.privateKeyUsage` only) so that no prompt appears.94final class SecureEnclaveTests: XCTestCase {95 private func makeKeys() throws -> (SecureEnclaveKEMKey, SecureEnclaveSigningKey) {96 guard SecureEnclaveSupport.isAvailable else { throw XCTSkip("Secure Enclave not available on this machine") }97 let ac = try SecureEnclaveSupport.accessControl(userPresence: false)98 do { return (try SecureEnclaveKEMKey(accessControl: ac), try SecureEnclaveSigningKey(accessControl: ac)) }99 catch { throw XCTSkip("Secure Enclave key creation is not possible in this process (unsigned test runner?): \(error)") }100 }101102 func testSecureEnclaveHpkeUnwrapsArk() throws {103 let (enc, sig) = try makeKeys()104 XCTAssertTrue(enc.isHardwareBound)105 XCTAssertEqual(enc.publicKey.count, 65)106 XCTAssertEqual(enc.publicKey.first, 4)107 let dev = DeviceKeys(enc: enc, sig: sig)108 XCTAssertTrue(dev.isHardwareBound)109 let ark = SPV1.random(32), accountId = SPV1.uuid(), deviceId = SPV1.uuid()110 let envelope = try SPV1.wrapArkForDevice(ark: ark, accountId: accountId, deviceId: deviceId, devEncPub: enc.publicKey)111 XCTAssertEqual(try SPV1.unwrapArk(device: dev, accountId: accountId, deviceId: deviceId, envelope: envelope), ark)112 assertThrows(.authenticationFailed, try SPV1.unwrapArk(device: dev, accountId: accountId, deviceId: SPV1.uuid(), envelope: envelope))113 assertThrows(.authenticationFailed, try SPV1.unwrapArk(device: SPV1.generateDeviceKeys(), accountId: accountId, deviceId: deviceId, envelope: envelope))114115 // restoring from the stored representation gives the same key116 let restored = try SecureEnclaveKEMKey(dataRepresentation: enc.dataRepresentation)117 XCTAssertEqual(restored.publicKey, enc.publicKey)118 XCTAssertEqual(try SPV1.unwrapArk(device: restored, accountId: accountId, deviceId: deviceId, envelope: envelope), ark)119 XCTAssertThrowsError(try SecureEnclaveKEMKey(dataRepresentation: Data("garbage".utf8)))120 }121122 func testSecureEnclaveSigningKeyProducesRawSignatures() throws {123 let (enc, sig) = try makeKeys()124 let msg = Data("device challenge".utf8)125 let s = try sig.sign(msg)126 XCTAssertEqual(s.count, 64)127 XCTAssertTrue(try SPV1.verify(publicKey: sig.publicKey, message: msg, signature: s))128 XCTAssertFalse(try SPV1.verify(publicKey: sig.publicKey, message: Data("other".utf8), signature: s))129 let restored = try SecureEnclaveSigningKey(dataRepresentation: sig.dataRepresentation)130 XCTAssertEqual(restored.publicKey, sig.publicKey)131 // a full device certificate + pairing flow with hardware keys132 let account = SPV1.generateAccountKeys(), accountId = SPV1.uuid(), deviceId = SPV1.uuid()133 let dev = DeviceKeys(enc: enc, sig: sig)134 let cert = try SPV1.signDeviceCert(account: account, accountId: accountId, deviceId: deviceId, encPub: dev.encPub, sigPub: dev.sigPub, createdAt: "2026-11-19T00:00:00.000Z")135 XCTAssertTrue(SPV1.verifyDeviceCert(accountSigPub: account.sigPub, accountId: accountId,136 device: DevicePublic(deviceId: deviceId, encPub: dev.encPub, sigPub: dev.sigPub, createdAt: "2026-11-19T00:00:00.000Z", cert: cert)))137 }138139 func testGenerateSecureEnclaveDeviceKeysHelper() throws {140 guard SecureEnclaveSupport.isAvailable else { throw XCTSkip("Secure Enclave not available on this machine") }141 let made: (keys: DeviceKeys, encData: Data, sigData: Data)142 do { made = try SPV1.generateSecureEnclaveDeviceKeys(userPresence: false) } catch { throw XCTSkip("Secure Enclave key creation is not possible in this process: \(error)") }143 XCTAssertTrue(made.keys.isHardwareBound)144 XCTAssertEqual(try SecureEnclaveKEMKey(dataRepresentation: made.encData).publicKey, made.keys.enc.publicKey)145 XCTAssertEqual(try SecureEnclaveSigningKey(dataRepresentation: made.sigData).publicKey, made.keys.sig.publicKey)146 }147}148