Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraVault/SecretBytes.swift8// Module : Qyvora Vault9// 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 Foundation42import QyvoraMath4344/// Secret material held outside Swift's copy-on-write arrays: a manually allocated buffer, locked in memory45/// when the system allows it (`mlock`, best effort), and overwritten with zeros when the last reference goes away.46///47/// The content is immutable after creation, which makes sharing across tasks safe. It never appears in48/// `print`, string interpolation, `dump` or a debugger description: all of them show `<redacted>`.49public final class SecretBytes: @unchecked Sendable, CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable {50 private let pointer: UnsafeMutableRawPointer51 private let memoryLocked: Bool52 public let count: Int5354 /// Copies `bytes`. The caller remains responsible for wiping its own array (`SecureMemory.zero`).55 public init(_ bytes: Bytes) {56 count = bytes.count57 pointer = UnsafeMutableRawPointer.allocate(byteCount: Swift.max(bytes.count, 1), alignment: 16)58 memoryLocked = bytes.isEmpty ? false : mlock(pointer, bytes.count) == 059 bytes.withUnsafeBytes { src in60 if let base = src.baseAddress, !bytes.isEmpty { pointer.copyMemory(from: base, byteCount: bytes.count) }61 }62 }6364 /// `count` fresh random bytes from the system generator.65 public static func random(_ count: Int) -> SecretBytes {66 var b = SystemRandom.bytes(count)67 defer { SecureMemory.zero(&b) }68 return SecretBytes(b)69 }7071 deinit {72 if count > 0 { _ = memset_s(pointer, count, 0, count) }73 if memoryLocked { munlock(pointer, count) }74 pointer.deallocate()75 }7677 /// Gives temporary read access to the secret. The buffer must not escape the closure.78 public func withBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {79 try body(UnsafeRawBufferPointer(start: pointer, count: count))80 }8182 /// An ordinary array copy for APIs that need one. The caller must wipe it with `SecureMemory.zero`.83 public func copyBytes() -> Bytes {84 withBytes { Bytes($0) }85 }8687 /// Constant-time comparison (for equal lengths).88 public func constantTimeEquals(_ other: SecretBytes) -> Bool {89 guard count == other.count else { return false }90 var diff: UInt8 = 091 withBytes { a in92 other.withBytes { b in93 for i in 0..<count { diff |= a[i] ^ b[i] }94 }95 }96 return diff == 097 }9899 public var description: String { "<redacted>" }100 public var debugDescription: String { "<redacted>" }101 public var customMirror: Mirror { Mirror(self, children: [], displayStyle: .none) }102}103