Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraIdentity/X25519.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 CryptoKit42import Foundation43import QyvoraMath4445/// X25519 (RFC 7748) from CryptoKit: the classical half of the hybrid key exchange.46/// This file imports no engine cryptography, so system and engine primitives cannot be confused.47public enum X25519 {48 public static let keySize = 324950 public enum Failure: Error, Equatable, Sendable {51 case invalidKey52 /// The peer key is a low-order point (all-zero shared secret).53 case weakSharedSecret54 }5556 /// A fresh key pair (raw 32-byte representations).57 public static func generateKeyPair() -> (privateKey: Bytes, publicKey: Bytes) {58 let key = Curve25519.KeyAgreement.PrivateKey()59 return (Bytes(key.rawRepresentation), Bytes(key.publicKey.rawRepresentation))60 }6162 public static func publicKey(privateKey: Bytes) throws(Failure) -> Bytes {63 guard privateKey.count == keySize, let key = try? Curve25519.KeyAgreement.PrivateKey(rawRepresentation: privateKey) else {64 throw .invalidKey65 }66 return Bytes(key.publicKey.rawRepresentation)67 }6869 /// The raw 32-byte shared secret. An all-zero result is rejected.70 public static func sharedSecret(privateKey: Bytes, publicKey: Bytes) throws(Failure) -> Bytes {71 guard privateKey.count == keySize, publicKey.count == keySize,72 let own = try? Curve25519.KeyAgreement.PrivateKey(rawRepresentation: privateKey),73 let peer = try? Curve25519.KeyAgreement.PublicKey(rawRepresentation: publicKey)74 else { throw .invalidKey }75 guard let shared = try? own.sharedSecretFromKeyAgreement(with: peer) else { throw .weakSharedSecret }76 let bytes = shared.withUnsafeBytes { Bytes($0) }77 guard bytes.contains(where: { $0 != 0 }) else { throw .weakSharedSecret }78 return bytes79 }80}81