From 9e9127d49e321e42e478933ee00f86232e4ea010 Mon Sep 17 00:00:00 2001 From: Rich Logan Date: Fri, 30 May 2025 08:13:26 +0100 Subject: [PATCH] Add error descriptions --- Sources/SFrame/MLS.swift | 16 ++++++++++++++++ Sources/SFrame/Provider.swift | 7 +++++++ Sources/SFrame/SFrame.swift | 17 ++++++++++++++++- Sources/SFrame/SyntheticAEAD.swift | 13 +++++++++++++ Sources/SFrame/Utilities.swift | 15 +++++++++++---- Tests/SFrameTests/Utilities.swift | 10 ++++++++++ 6 files changed, 73 insertions(+), 5 deletions(-) diff --git a/Sources/SFrame/MLS.swift b/Sources/SFrame/MLS.swift index de95880..607bc81 100644 --- a/Sources/SFrame/MLS.swift +++ b/Sources/SFrame/MLS.swift @@ -15,6 +15,22 @@ public enum MLSError: Error { case contextOverflow /// Epoch bits must be 0 < x < 64. case badEpochBits + + public var localizedDescription: String { + switch self { + case .unknownEpoch: + return "Unknown epoch." + + case .senderOverflow: + return "Sender ID exceeds maximum allowed." + + case .contextOverflow: + return "Context ID exceeds maximum allowed." + + case .badEpochBits: + return "Epoch bits must be between 1 and 63." + } + } } /// Provides an interface to utilize SFrame with MLS keying. diff --git a/Sources/SFrame/Provider.swift b/Sources/SFrame/Provider.swift index 51640f3..065a4f6 100644 --- a/Sources/SFrame/Provider.swift +++ b/Sources/SFrame/Provider.swift @@ -76,4 +76,11 @@ public protocol CryptoProvider { public enum CryptoProviderError: Error { /// This provider does not support the requested cipher suite. case unsupportedCipherSuite + + public var localizedDescription: String { + switch self { + case .unsupportedCipherSuite: + return "This provider does not support the requested cipher suite." + } + } } diff --git a/Sources/SFrame/SFrame.swift b/Sources/SFrame/SFrame.swift index 51b0855..713e2c9 100644 --- a/Sources/SFrame/SFrame.swift +++ b/Sources/SFrame/SFrame.swift @@ -17,7 +17,22 @@ public enum SFrameError: Error { case existingKey case badParameter case malformedCipherText - case keyMisuse + + public var localizedDescription: String { + switch self { + case .missingKey: + return "The required key is missing." + + case .existingKey: + return "This key already exists." + + case .badParameter: + return "A bit size parameter was invalid." + + case .malformedCipherText: + return "The ciphertext is malformed." + } + } } /// The operation this key is to be used for. diff --git a/Sources/SFrame/SyntheticAEAD.swift b/Sources/SFrame/SyntheticAEAD.swift index 9045f72..c4de2ee 100644 --- a/Sources/SFrame/SyntheticAEAD.swift +++ b/Sources/SFrame/SyntheticAEAD.swift @@ -9,6 +9,19 @@ public enum SyntheticAEADError: Error { case missingKeySize case authenticationFailure case badKeySize + + public var localizedDescription: String { + switch self { + case .missingKeySize: + return "The cipher suite does not specify a key size." + + case .authenticationFailure: + return "Authentication failed." + + case .badKeySize: + return "The provided key size does not match the cipher suite requirements." + } + } } /// SFrame AES-CTR with SHA2 implementation. diff --git a/Sources/SFrame/Utilities.swift b/Sources/SFrame/Utilities.swift index ce37ebe..4406d07 100644 --- a/Sources/SFrame/Utilities.swift +++ b/Sources/SFrame/Utilities.swift @@ -4,10 +4,6 @@ import Foundation -public enum DataError: Error { - case lengthMismatch -} - // SealedBox implementation. internal struct SealedDataBox: SealedBox { internal let authTag: Data @@ -15,6 +11,17 @@ internal struct SealedDataBox: SealedBox { internal let nonceBytes: Data } +public enum DataError: Error { + case lengthMismatch + + public var localizedDescription: String { + switch self { + case .lengthMismatch: + return "XOR requires buffers of equal length." + } + } +} + extension Data { /// Initialize a view into a contiguous byte buffer. /// - Parameter bytes: The contiguous byte buffer to expose a view into. diff --git a/Tests/SFrameTests/Utilities.swift b/Tests/SFrameTests/Utilities.swift index 56d4005..dfb61ba 100644 --- a/Tests/SFrameTests/Utilities.swift +++ b/Tests/SFrameTests/Utilities.swift @@ -176,6 +176,16 @@ internal func hexToData(_ hex: String) -> Data { internal enum TestError: Error { case missingVectors case unsupportedCipherSuite + + internal var localizedDescription: String { + switch self { + case .missingVectors: + return "Test vectors are missing." + + case .unsupportedCipherSuite: + return "The cipher suite is not supported." + } + } } internal func loadTestVectors() throws -> TestVectors {