mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
334e682869 | ||
|
|
c68a7a7c7f | ||
|
|
8c9d27a339 | ||
|
|
176abc28e0 | ||
|
|
64a4de00fb | ||
|
|
84b1d49411 | ||
|
|
88b9c9b3b7 | ||
|
|
993467897d |
No files matched your search
@@ -25,6 +25,15 @@ jobs:
|
||||
windows_nightly_next_arguments_override: "--explicit-target-dependency-import-check error"
|
||||
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"
|
||||
|
||||
release-builds:
|
||||
name: Release builds
|
||||
uses: apple/swift-nio/.github/workflows/release_builds.yml@main
|
||||
with:
|
||||
windows_6_0_enabled: true
|
||||
windows_6_1_enabled: true
|
||||
windows_nightly_next_enabled: true
|
||||
windows_nightly_main_enabled: true
|
||||
|
||||
cxx-interop:
|
||||
name: Cxx interop
|
||||
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
|
||||
|
||||
@@ -30,6 +30,15 @@ jobs:
|
||||
windows_nightly_next_arguments_override: "--explicit-target-dependency-import-check error"
|
||||
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"
|
||||
|
||||
release-builds:
|
||||
name: Release builds
|
||||
uses: apple/swift-nio/.github/workflows/release_builds.yml@main
|
||||
with:
|
||||
windows_6_0_enabled: true
|
||||
windows_6_1_enabled: true
|
||||
windows_nightly_next_enabled: true
|
||||
windows_nightly_main_enabled: true
|
||||
|
||||
cxx-interop:
|
||||
name: Cxx interop
|
||||
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
|
||||
|
||||
@@ -158,4 +158,49 @@ let benchmarks = {
|
||||
blackHole(try privateKey.evaluate(blindedElement))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"key-exchange-p256",
|
||||
configuration: Benchmark.Configuration(
|
||||
metrics: defaultMetrics,
|
||||
scalingFactor: .kilo,
|
||||
maxDuration: .seconds(10_000_000),
|
||||
maxIterations: 10
|
||||
)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
let (key1, key2) = (P256.KeyAgreement.PrivateKey(), P256.KeyAgreement.PrivateKey())
|
||||
blackHole(try key1.sharedSecretFromKeyAgreement(with: key2.publicKey))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"key-exchange-p384",
|
||||
configuration: Benchmark.Configuration(
|
||||
metrics: defaultMetrics,
|
||||
scalingFactor: .kilo,
|
||||
maxDuration: .seconds(10_000_000),
|
||||
maxIterations: 10
|
||||
)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
let (key1, key2) = (P384.KeyAgreement.PrivateKey(), P384.KeyAgreement.PrivateKey())
|
||||
blackHole(try key1.sharedSecretFromKeyAgreement(with: key2.publicKey))
|
||||
}
|
||||
}
|
||||
|
||||
Benchmark(
|
||||
"key-exchange-p521",
|
||||
configuration: Benchmark.Configuration(
|
||||
metrics: defaultMetrics,
|
||||
scalingFactor: .kilo,
|
||||
maxDuration: .seconds(10_000_000),
|
||||
maxIterations: 10
|
||||
)
|
||||
) { benchmark in
|
||||
for _ in benchmark.scaledIterations {
|
||||
let (key1, key2) = (P521.KeyAgreement.PrivateKey(), P521.KeyAgreement.PrivateKey())
|
||||
blackHole(try key1.sharedSecretFromKeyAgreement(with: key2.publicKey))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,10 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.15.1)
|
||||
|
||||
if(POLICY CMP0157)
|
||||
cmake_policy(SET CMP0157 NEW)
|
||||
endif()
|
||||
|
||||
project(SwiftCrypto
|
||||
LANGUAGES ASM C CXX Swift)
|
||||
|
||||
@@ -47,6 +51,18 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND NOT CMAKE_CROSSCOMPILING)
|
||||
set(CMAKE_RANLIB "/usr/bin/ranlib")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
|
||||
# We need to ensure that we don't include the min/max macros from the Windows SDK.
|
||||
add_compile_definitions(NOMINMAX)
|
||||
# We can only link against the DLL version of the MSVC runtime library for now.
|
||||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
|
||||
if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2)
|
||||
# We need to set the static library prefix to "lib" so that we can link against the static libraries.
|
||||
set(CMAKE_STATIC_LIBRARY_PREFIX_Swift "lib")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
find_package(dispatch CONFIG)
|
||||
find_package(Foundation CONFIG)
|
||||
|
||||
+20
-1
@@ -342,8 +342,27 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|FreeBSD|OpenBSD" AND CMAKE_SYSTE
|
||||
gen/bcm/vpaes-armv8-linux.S
|
||||
gen/crypto/chacha-armv8-linux.S
|
||||
gen/crypto/chacha20_poly1305_armv8-linux.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows" AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows" AND CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64|arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
gen/bcm/aesv8-armv8-win.S
|
||||
gen/bcm/aesv8-gcm-armv8-win.S
|
||||
gen/bcm/armv8-mont-win.S
|
||||
gen/bcm/bn-armv8-win.S
|
||||
gen/bcm/ghash-neon-armv8-win.S
|
||||
gen/bcm/ghashv8-armv8-win.S
|
||||
gen/bcm/p256-armv8-asm-win.S
|
||||
gen/bcm/p256_beeu-armv8-asm-win.S
|
||||
gen/bcm/sha1-armv8-win.S
|
||||
gen/bcm/sha256-armv8-win.S
|
||||
gen/bcm/sha512-armv8-win.S
|
||||
gen/bcm/vpaes-armv8-win.S
|
||||
gen/crypto/chacha-armv8-win.S
|
||||
gen/crypto/chacha20_poly1305_armv8-win.S)
|
||||
else()
|
||||
message(FATAL_ERROR "platform sources are not defined here")
|
||||
message(FATAL_ERROR "platform sources are not defined here for ${CMAKE_SYSTEM_NAME} on ${CMAKE_SYSTEM_PROCESSOR}")
|
||||
endif()
|
||||
|
||||
target_include_directories(CCryptoBoringSSL PUBLIC
|
||||
|
||||
@@ -36,26 +36,20 @@ extension OpenSSLSupportedNISTCurve {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P256: OpenSSLSupportedNISTCurve {
|
||||
@inlinable
|
||||
static var group: BoringSSLEllipticCurveGroup {
|
||||
try! BoringSSLEllipticCurveGroup(.p256)
|
||||
}
|
||||
@usableFromInline
|
||||
static let group = try! BoringSSLEllipticCurveGroup(.p256)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P384: OpenSSLSupportedNISTCurve {
|
||||
@inlinable
|
||||
static var group: BoringSSLEllipticCurveGroup {
|
||||
try! BoringSSLEllipticCurveGroup(.p384)
|
||||
}
|
||||
@usableFromInline
|
||||
static let group = try! BoringSSLEllipticCurveGroup(.p384)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P521: OpenSSLSupportedNISTCurve {
|
||||
@inlinable
|
||||
static var group: BoringSSLEllipticCurveGroup {
|
||||
try! BoringSSLEllipticCurveGroup(.p521)
|
||||
}
|
||||
@usableFromInline
|
||||
static let group = try! BoringSSLEllipticCurveGroup(.p521)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
|
||||
@@ -21,15 +21,12 @@ private let emptyStorage:SecureBytes.Backing = SecureBytes.Backing.createEmpty()
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct SecureBytes {
|
||||
@usableFromInline
|
||||
var backing: Backing
|
||||
|
||||
@inlinable
|
||||
init() {
|
||||
self = .init(count: 0)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
init(count: Int) {
|
||||
if count == 0 {
|
||||
self.backing = emptyStorage
|
||||
@@ -43,7 +40,6 @@ struct SecureBytes {
|
||||
}
|
||||
|
||||
/// Allows initializing a SecureBytes object with a closure that will initialize the memory.
|
||||
@usableFromInline
|
||||
init(unsafeUninitializedCapacity: Int, initializingWith callback: (inout UnsafeMutableRawBufferPointer, inout Int) throws -> Void) rethrows {
|
||||
self.backing = Backing.create(capacity: unsafeUninitializedCapacity)
|
||||
try self.backing._withVeryUnsafeMutableBytes { veryUnsafePointer in
|
||||
@@ -59,7 +55,6 @@ struct SecureBytes {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes {
|
||||
@inlinable
|
||||
mutating func append<C: Collection>(_ data: C) where C.Element == UInt8 {
|
||||
let requiredCapacity = self.count + data.count
|
||||
let backingCapacity = self.backing.allocatedCapacity
|
||||
@@ -71,7 +66,6 @@ extension SecureBytes {
|
||||
self.backing._appendBytes(data)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
mutating func reserveCapacity(_ n: Int) {
|
||||
let backingCapacity = self.backing.allocatedCapacity
|
||||
if backingCapacity >= n {
|
||||
@@ -95,32 +89,27 @@ extension SecureBytes: Equatable {
|
||||
// MARK: - Collection conformance
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: Collection {
|
||||
@usableFromInline
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct Index {
|
||||
/* fileprivate but usableFromInline */ @usableFromInline var offset: Int
|
||||
/* fileprivate but usableFromInline */ var offset: Int
|
||||
|
||||
/*@inlinable*/ @usableFromInline internal init(offset: Int) {
|
||||
/*@inlinable*/ internal init(offset: Int) {
|
||||
self.offset = offset
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
var startIndex: Index {
|
||||
return Index(offset: 0)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
var endIndex: Index {
|
||||
return Index(offset: self.count)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
var count: Int {
|
||||
return self.backing.count
|
||||
}
|
||||
|
||||
@inlinable
|
||||
subscript(_ index: Index) -> UInt8 {
|
||||
get {
|
||||
return self.backing[offset: index.offset]
|
||||
@@ -130,7 +119,6 @@ extension SecureBytes: Collection {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func index(after index: Index) -> Index {
|
||||
return index.advanced(by: 1)
|
||||
}
|
||||
@@ -139,7 +127,6 @@ extension SecureBytes: Collection {
|
||||
// MARK: - BidirectionalCollection conformance
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: BidirectionalCollection {
|
||||
@inlinable
|
||||
func index(before index: Index) -> Index {
|
||||
return index.advanced(by: -1)
|
||||
}
|
||||
@@ -156,7 +143,6 @@ extension SecureBytes: MutableCollection { }
|
||||
// MARK: - RangeReplaceableCollection conformance
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: RangeReplaceableCollection {
|
||||
@inlinable
|
||||
mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C) where C.Element == UInt8 {
|
||||
let requiredCapacity = self.backing.count - subrange.count + newElements.count
|
||||
let backingCapacity = self.backing.allocatedCapacity
|
||||
@@ -182,7 +168,6 @@ extension SecureBytes: RangeReplaceableCollection {
|
||||
}
|
||||
|
||||
// The default implementation of this from RangeReplaceableCollection can't take advantage of `ContiguousBytes`, so we override it here
|
||||
@inlinable
|
||||
public mutating func append<Elements: Sequence>(contentsOf newElements: Elements) where Elements.Element == UInt8 {
|
||||
let done:Void? = newElements.withContiguousStorageIfAvailable {
|
||||
replaceSubrange(endIndex..<endIndex, with: $0)
|
||||
@@ -199,12 +184,10 @@ extension SecureBytes: RangeReplaceableCollection {
|
||||
// MARK: - ContiguousBytes conformance
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: ContiguousBytes {
|
||||
@inlinable
|
||||
func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
|
||||
return try self.backing.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
mutating func withUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
|
||||
if !isKnownUniquelyReferenced(&self.backing) {
|
||||
self.backing = Backing.create(copying: self.backing)
|
||||
@@ -213,7 +196,6 @@ extension SecureBytes: ContiguousBytes {
|
||||
return try self.backing.withUnsafeMutableBytes(body)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
|
||||
return try self.backing.withContiguousStorageIfAvailable(body)
|
||||
}
|
||||
@@ -222,7 +204,6 @@ extension SecureBytes: ContiguousBytes {
|
||||
// MARK: - DataProtocol conformance
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: DataProtocol {
|
||||
@inlinable
|
||||
var regions: CollectionOfOne<SecureBytes> {
|
||||
return CollectionOfOne(self)
|
||||
}
|
||||
@@ -257,37 +238,29 @@ extension SecureBytes.Index: Strideable {
|
||||
// MARK: - Heap allocated backing storage.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes {
|
||||
@usableFromInline
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal struct BackingHeader {
|
||||
@usableFromInline
|
||||
internal var count: Int
|
||||
|
||||
@usableFromInline
|
||||
internal var capacity: Int
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal class Backing: ManagedBuffer<BackingHeader, UInt8> {
|
||||
|
||||
@usableFromInline
|
||||
class func createEmpty() -> Backing {
|
||||
return Backing.create(minimumCapacity: 0, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: 0) }) as! Backing
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
class func create(capacity: Int) -> Backing {
|
||||
let capacity = Int(UInt32(capacity).nextPowerOf2ClampedToMax())
|
||||
return Backing.create(minimumCapacity: capacity, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: capacity) }) as! Backing
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
class func create(copying original: Backing) -> Backing {
|
||||
return Backing.create(bytes: original)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
class func create<D: ContiguousBytes>(bytes: D) -> Backing {
|
||||
return bytes.withUnsafeBytes { bytesPtr in
|
||||
let backing = Backing.create(capacity: bytesPtr.count)
|
||||
@@ -300,7 +273,6 @@ extension SecureBytes {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
class func create(randomBytes: Int) -> Backing {
|
||||
let backing = Backing.create(capacity: randomBytes)
|
||||
backing._withVeryUnsafeMutableBytes { targetPtr in
|
||||
@@ -320,7 +292,6 @@ extension SecureBytes {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
var count: Int {
|
||||
get {
|
||||
return self.header.count
|
||||
@@ -330,7 +301,6 @@ extension SecureBytes {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
subscript(offset offset: Int) -> UInt8 {
|
||||
get {
|
||||
// precondition(offset >= 0 && offset < self.count)
|
||||
@@ -346,7 +316,6 @@ extension SecureBytes {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes.Backing {
|
||||
@usableFromInline
|
||||
var allocatedCapacity: Int {
|
||||
#if os(OpenBSD)
|
||||
return self.header.capacity
|
||||
@@ -419,7 +388,6 @@ extension SecureBytes.Backing {
|
||||
/// Moves the range of bytes identified by the slice by the delta, crashing if the move would
|
||||
/// place the bytes out of the storage. Note that this does not update the count: external code
|
||||
/// must ensure that that happens.
|
||||
@usableFromInline
|
||||
/* private but usableFromInline */ func _moveBytes(range: Range<Int>, by delta: Int) {
|
||||
// We have to check that the range is within the delta, as is the new location.
|
||||
precondition(range.lowerBound >= 0)
|
||||
@@ -437,7 +405,6 @@ extension SecureBytes.Backing {
|
||||
}
|
||||
|
||||
// Copies some bytes into the buffer at the appropriate place. Does not update count: external code must do so.
|
||||
@inlinable
|
||||
/* private but inlinable */ func _copyBytes<C: Collection>(_ bytes: C, at offset: Int) where C.Element == UInt8 {
|
||||
precondition(offset >= 0)
|
||||
precondition(offset + bytes.count <= self.allocatedCapacity)
|
||||
@@ -470,7 +437,6 @@ extension SecureBytes.Backing: ContiguousBytes {
|
||||
}
|
||||
|
||||
/// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file.
|
||||
@inlinable
|
||||
/* private but inlinable */ func _withVeryUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
|
||||
let capacity = self.allocatedCapacity
|
||||
|
||||
|
||||
@@ -69,6 +69,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.multiply(by: rhs, on: group, context: context)
|
||||
}
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package consuming func multiplying(
|
||||
by rhs: ArbitraryPrecisionInteger,
|
||||
@@ -78,6 +82,18 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.multiply(by: rhs, on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package func multiplying(
|
||||
by rhs: ArbitraryPrecisionInteger,
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var `self` = self
|
||||
try self.multiply(by: rhs, on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#endif
|
||||
|
||||
@usableFromInline
|
||||
package static func multiplying(
|
||||
@@ -110,6 +126,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.add(rhs, on: group, context: context)
|
||||
}
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package consuming func adding(
|
||||
_ rhs: consuming EllipticCurvePoint,
|
||||
@@ -119,7 +139,23 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.add(rhs, on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package func adding(
|
||||
_ rhs: consuming EllipticCurvePoint,
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var `self` = self
|
||||
try self.add(rhs, on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#endif
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package static func adding(
|
||||
_ lhs: consuming EllipticCurvePoint,
|
||||
@@ -130,6 +166,19 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try lhs.add(rhs, on: group, context: context)
|
||||
return lhs
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package static func adding(
|
||||
_ lhs: EllipticCurvePoint,
|
||||
_ rhs: EllipticCurvePoint,
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var lhs = lhs
|
||||
try lhs.add(rhs, on: group, context: context)
|
||||
return lhs
|
||||
}
|
||||
#endif
|
||||
|
||||
@usableFromInline
|
||||
package mutating func invert(
|
||||
@@ -150,6 +199,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.invert(on: group, context: context)
|
||||
}
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package consuming func inverting(
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
@@ -158,7 +211,22 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.invert(on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package func inverting(
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var `self` = self
|
||||
try self.invert(on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#endif
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package static func inverting(
|
||||
_ point: consuming EllipticCurvePoint,
|
||||
@@ -168,6 +236,18 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try point.invert(on: group, context: context)
|
||||
return point
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package static func inverting(
|
||||
_ point: EllipticCurvePoint,
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var point = point
|
||||
try point.invert(on: group, context: context)
|
||||
return point
|
||||
}
|
||||
#endif
|
||||
|
||||
@usableFromInline
|
||||
package mutating func subtract(
|
||||
@@ -190,6 +270,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.subtract(rhs, on: group, context: context)
|
||||
}
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package consuming func subtracting(
|
||||
_ rhs: consuming EllipticCurvePoint,
|
||||
@@ -199,7 +283,23 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try self.subtract(rhs, on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package func subtracting(
|
||||
_ rhs: EllipticCurvePoint,
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var `self` = self
|
||||
try self.subtract(rhs, on: group, context: context)
|
||||
return self
|
||||
}
|
||||
#endif
|
||||
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
@usableFromInline
|
||||
package static func subtracting(
|
||||
_ rhs: consuming EllipticCurvePoint,
|
||||
@@ -210,6 +310,19 @@ package struct EllipticCurvePoint: @unchecked Sendable {
|
||||
try lhs.subtract(rhs, on: group, context: context)
|
||||
return lhs
|
||||
}
|
||||
#else
|
||||
@usableFromInline
|
||||
package static func subtracting(
|
||||
_ rhs: EllipticCurvePoint,
|
||||
from lhs: EllipticCurvePoint,
|
||||
on group: BoringSSLEllipticCurveGroup,
|
||||
context: FiniteFieldArithmeticContext? = nil
|
||||
) throws -> EllipticCurvePoint {
|
||||
var lhs = lhs
|
||||
try lhs.subtract(rhs, on: group, context: context)
|
||||
return lhs
|
||||
}
|
||||
#endif
|
||||
|
||||
@usableFromInline
|
||||
package init<MessageBytes: ContiguousBytes, DSTBytes: ContiguousBytes>(
|
||||
|
||||
@@ -147,8 +147,9 @@ extension AES {
|
||||
extension AES._CBC {
|
||||
/// An initialization vector.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct IV: Sendable {
|
||||
public struct IV: Sendable, Sequence {
|
||||
// AES CBC uses a 128-bit IV.
|
||||
@usableFromInline
|
||||
var ivBytes: (
|
||||
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
|
||||
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8
|
||||
@@ -193,6 +194,13 @@ extension AES._CBC {
|
||||
bytesPtr.copyBytes(from: ivBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func makeIterator() -> some IteratorProtocol<UInt8> {
|
||||
withUnsafeBytes(of: ivBytes) { unsafeRawBufferPointer in
|
||||
Array(unsafeRawBufferPointer).makeIterator()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
@_implementationOnly import CCryptoBoringSSL
|
||||
import Crypto
|
||||
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
#else
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
|
||||
extension AES {
|
||||
/// A cipher-based message authentication code.
|
||||
///
|
||||
/// CMAC uses AES to implement a MAC. CMAC is useful in contexts where access to
|
||||
/// a hash function is not guaranteed, but a block cipher will be available.
|
||||
public struct CMAC: @unchecked Sendable {
|
||||
// Unchecked sendable because this is CoW.
|
||||
fileprivate var backing: Backing
|
||||
|
||||
/// Creates a message authentication code generator.
|
||||
///
|
||||
/// Defaults the output size to 128 bits.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - key: The symmetric key used to secure the computation.
|
||||
public init(key: SymmetricKey) throws {
|
||||
try self.init(key: key, outputSize: 16)
|
||||
}
|
||||
|
||||
/// Creates a message authentication code generator.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - key: The symmetric key used to secure the computation.
|
||||
/// - outputSize: The number of bytes of MAC to generate. Must be in the range 0 to 16 inclusive.
|
||||
public init(key: SymmetricKey, outputSize: Int) throws {
|
||||
guard [128, 192, 256].contains(key.bitCount) else {
|
||||
throw CryptoError.incorrectKeySize
|
||||
}
|
||||
guard (0...16).contains(outputSize) else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
self.backing = Backing(key: key, outputSize: outputSize)
|
||||
}
|
||||
|
||||
/// Adds data to be authenticated by MAC function. This can be called one or more times to append additional data.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - bufferPointer: The data to be authenticated.
|
||||
public mutating func update(bufferPointer: UnsafeRawBufferPointer) {
|
||||
self.cowIfNeeded()
|
||||
self.backing.update(bufferPointer)
|
||||
}
|
||||
|
||||
/// Finalizes the message authentication computation and returns the
|
||||
/// computed code.
|
||||
///
|
||||
/// - Returns: The message authentication code.
|
||||
public consuming func finalize() -> AES.CMAC.MAC {
|
||||
// The combination of "consuming" and "cowifneeded" should
|
||||
// produce an environment where, if users may choose to
|
||||
// keep using the MAC, they can, but if they aren't we'll
|
||||
// avoid an unnecessary CoW.
|
||||
self.cowIfNeeded()
|
||||
return self.backing.finalize()
|
||||
}
|
||||
|
||||
/// Updates the MAC with data.
|
||||
///
|
||||
/// - Parameter data: The data to update the MAC
|
||||
public mutating func update<D: DataProtocol>(data: D) {
|
||||
for memoryRegion in data.regions {
|
||||
memoryRegion.withUnsafeBytes { bp in
|
||||
self.update(bufferPointer: bp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func cowIfNeeded() {
|
||||
if !isKnownUniquelyReferenced(&self.backing) {
|
||||
self.backing = Backing(copying: self.backing)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
|
||||
extension AES.CMAC {
|
||||
/// A cipher-based message authentication code.
|
||||
public struct MAC: MessageAuthenticationCode {
|
||||
fileprivate let underlyingData: Data
|
||||
|
||||
init(underlyingData: Data) {
|
||||
self.underlyingData = underlyingData
|
||||
}
|
||||
|
||||
/// The number of bytes in the message authentication code.
|
||||
public var byteCount: Int {
|
||||
self.underlyingData.count
|
||||
}
|
||||
|
||||
/// Invokes the given closure with a buffer pointer covering the raw bytes
|
||||
/// of the code.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - body: A closure that takes a raw buffer pointer to the bytes of the
|
||||
/// code.
|
||||
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
|
||||
try self.underlyingData.withUnsafeBytes(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
|
||||
extension AES.CMAC {
|
||||
fileprivate final class Backing {
|
||||
private let key: SymmetricKey
|
||||
private let context: OpaquePointer
|
||||
private let outputSize: Int
|
||||
|
||||
init(key: SymmetricKey, outputSize: Int) {
|
||||
self.key = key
|
||||
self.context = CCryptoBoringSSL_CMAC_CTX_new()
|
||||
self.outputSize = outputSize
|
||||
|
||||
let rc = self.key.withUnsafeBytes { keyPtr in
|
||||
CCryptoBoringSSL_CMAC_Init(
|
||||
self.context,
|
||||
keyPtr.baseAddress,
|
||||
keyPtr.count,
|
||||
key.aesEVP,
|
||||
nil
|
||||
)
|
||||
}
|
||||
precondition(rc == 1)
|
||||
}
|
||||
|
||||
init(copying other: Backing) {
|
||||
self.key = other.key
|
||||
self.context = CCryptoBoringSSL_CMAC_CTX_new()
|
||||
self.outputSize = other.outputSize
|
||||
let rc = CCryptoBoringSSL_CMAC_CTX_copy(self.context, other.context)
|
||||
precondition(rc == 1)
|
||||
|
||||
// Ensure we don't lose `other` at this time.
|
||||
withExtendedLifetime(other) {}
|
||||
}
|
||||
|
||||
deinit {
|
||||
CCryptoBoringSSL_CMAC_CTX_free(self.context)
|
||||
}
|
||||
|
||||
func update(_ bytes: UnsafeRawBufferPointer) {
|
||||
let rc = CCryptoBoringSSL_CMAC_Update(self.context, bytes.baseAddress, bytes.count)
|
||||
precondition(rc == 1)
|
||||
}
|
||||
|
||||
func finalize() -> AES.CMAC.MAC {
|
||||
let bytes = withUnsafeTemporaryAllocation(byteCount: 16, alignment: 1) { bytes in
|
||||
precondition(bytes.count >= 16)
|
||||
var count = 16
|
||||
let rc = CCryptoBoringSSL_CMAC_Final(self.context, bytes.baseAddress, &count)
|
||||
precondition(count == 16)
|
||||
precondition(rc == 1)
|
||||
|
||||
return Data(UnsafeRawBufferPointer(rebasing: bytes.prefix(self.outputSize)))
|
||||
}
|
||||
return AES.CMAC.MAC(underlyingData: bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
|
||||
extension SymmetricKey {
|
||||
fileprivate var aesEVP: OpaquePointer {
|
||||
switch self.bitCount {
|
||||
case 128:
|
||||
CCryptoBoringSSL_EVP_aes_128_cbc()
|
||||
case 192:
|
||||
CCryptoBoringSSL_EVP_aes_192_cbc()
|
||||
case 256:
|
||||
CCryptoBoringSSL_EVP_aes_256_cbc()
|
||||
default:
|
||||
fatalError("Should be unreachable")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ add_library(_CryptoExtras
|
||||
"AES/BoringSSL/AES_CFB_boring.swift"
|
||||
"AES/BoringSSL/AES_CTR_boring.swift"
|
||||
"AES/BoringSSL/AES_GCM_SIV_boring.swift"
|
||||
"AES/CMAC.swift"
|
||||
"ARC/ARC+API.swift"
|
||||
"ARC/ARC.swift"
|
||||
"ARC/ARCCredential.swift"
|
||||
|
||||
@@ -15,9 +15,14 @@
|
||||
import Foundation
|
||||
|
||||
extension Data {
|
||||
// This enhancement can only be present on 6.1 or later because of the
|
||||
// absence of https://github.com/swiftlang/swift/pull/76186 in older
|
||||
// compilers.
|
||||
#if compiler(>=6.1)
|
||||
// This overload reduces allocations when used in a chain of infix operations.
|
||||
static func + (lhs: consuming Data, rhs: consuming Data) -> Data {
|
||||
lhs.append(contentsOf: rhs)
|
||||
return lhs
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -158,6 +158,14 @@ final class CBCTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testToDataConversion() throws {
|
||||
let randomBytes = (0..<16).map { _ in UInt8.random(in: UInt8.min...UInt8.max) }
|
||||
let dataIn = Data(randomBytes)
|
||||
let iv = try AES._CBC.IV(ivBytes: dataIn)
|
||||
let dataOut = Data(iv)
|
||||
XCTAssertEqual(dataIn, dataOut)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import Crypto
|
||||
import Foundation
|
||||
import XCTest
|
||||
import _CryptoExtras
|
||||
|
||||
final class CMACTests: XCTestCase {
|
||||
// Borrowed from CryptoKit
|
||||
func testVector1() throws {
|
||||
let key = try Array(hexString: "a60269f095ad3c3bafae907c6f215de0")
|
||||
let mac = try Array(hexString: "172084c3fe99fde4af29aa8e6e5fe1")
|
||||
let msg = try Array(hexString: "cead1c5af16ca89bc0821775f8cba8c25620a03dfd27d6f1186f75f1c0bcfe4a20")
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 15)
|
||||
authenticator.update(data: msg)
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
// rfc4493 example vectors
|
||||
// https://datatracker.ietf.org/doc/html/rfc4493#page-11
|
||||
let exampleVector1 = ""
|
||||
|
||||
let exampleVector2 = """
|
||||
6bc1bee22e409f96e93d7e117393172a
|
||||
"""
|
||||
|
||||
let exampleVector3 = """
|
||||
6bc1bee22e409f96e93d7e117393172a\
|
||||
ae2d8a571e03ac9c9eb76fac45af8e51\
|
||||
30c81c46a35ce411
|
||||
"""
|
||||
|
||||
let exampleVector4 = """
|
||||
6bc1bee22e409f96e93d7e117393172a\
|
||||
ae2d8a571e03ac9c9eb76fac45af8e51\
|
||||
30c81c46a35ce411e5fbc1191a0a52ef\
|
||||
f69f2445df4f9b17ad2b417be66c3710
|
||||
"""
|
||||
|
||||
func testExampleVector1() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "bb1d6929e95937287fa37d129b756746")
|
||||
let msg = try Array(hexString: exampleVector1)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
authenticator.update(data: msg)
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
func testExampleVector2() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "070a16b46b4d4144f79bdd9dd04a287c")
|
||||
let msg = try Array(hexString: exampleVector2)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
authenticator.update(data: msg)
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
func testExampleVector2PerByte() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "070a16b46b4d4144f79bdd9dd04a287c")
|
||||
let msg = try Array(hexString: exampleVector2)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
for byte in msg {
|
||||
authenticator.update(data: [byte])
|
||||
}
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
func testExampleVector3() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "dfa66747de9ae63030ca32611497c827")
|
||||
let msg = try Array(hexString: exampleVector3)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
authenticator.update(data: msg)
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
func testExampleVector3PerByte() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "dfa66747de9ae63030ca32611497c827")
|
||||
let msg = try Array(hexString: exampleVector3)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
for byte in msg {
|
||||
authenticator.update(data: [byte])
|
||||
}
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
// rfc4493 example vector 4
|
||||
// https://datatracker.ietf.org/doc/html/rfc4493#page-11
|
||||
func testExampleVector4() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "51f0bebf7e3b9d92fc49741779363cfe")
|
||||
let msg = try Array(hexString: exampleVector4)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
authenticator.update(data: msg)
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
// rfc4493 example vector 4
|
||||
// https://datatracker.ietf.org/doc/html/rfc4493#page-11
|
||||
func testExampleVector4PerByte() throws {
|
||||
let key = try Array(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
|
||||
let mac = try Array(hexString: "51f0bebf7e3b9d92fc49741779363cfe")
|
||||
let msg = try Array(hexString: exampleVector4)
|
||||
|
||||
var authenticator = try AES.CMAC(key: SymmetricKey(data: key), outputSize: 16)
|
||||
for byte in msg {
|
||||
authenticator.update(data: [byte])
|
||||
}
|
||||
XCTAssert(authenticator.finalize() == mac)
|
||||
}
|
||||
|
||||
func testWycheproof() throws {
|
||||
try wycheproofTest(jsonName: "aes_cmac_test") { (group: TestGroup) in
|
||||
for test in group.tests {
|
||||
precondition(test.flags.isEmpty)
|
||||
|
||||
do {
|
||||
var authenticator = try AES.CMAC(key: test.computedKey, outputSize: test.computedTag.count)
|
||||
authenticator.update(data: test.computedMsg)
|
||||
let result = authenticator.finalize()
|
||||
|
||||
switch test.result {
|
||||
case "valid":
|
||||
XCTAssertTrue(
|
||||
result == test.computedTag,
|
||||
"Unexpected invalid test \(test.tcId) (\(test.comment))"
|
||||
)
|
||||
case "invalid":
|
||||
XCTAssertFalse(
|
||||
result == test.computedTag,
|
||||
"Unexpected valid test \(test.tcId) (\(test.comment))"
|
||||
)
|
||||
default:
|
||||
fatalError("Unexpected result type")
|
||||
}
|
||||
} catch {
|
||||
XCTAssertTrue(test.result == "invalid", "Unexpected invalid test \(test.tcId) (\(test.comment))")
|
||||
XCTAssertTrue(
|
||||
test.comment == "invalid key size",
|
||||
"Unexpected invalid test \(test.tcId) (\(test.comment))"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TestGroup: Codable {
|
||||
var keySize: Int
|
||||
var tagSize: Int
|
||||
var type: String
|
||||
var tests: [Test]
|
||||
}
|
||||
|
||||
extension TestGroup {
|
||||
struct Test: Codable {
|
||||
var tcId: Int
|
||||
var comment: String
|
||||
var key: String
|
||||
var msg: String
|
||||
var tag: String
|
||||
var result: String
|
||||
var flags: [String]
|
||||
|
||||
var computedKey: SymmetricKey {
|
||||
SymmetricKey(hexEncoded: self.key)
|
||||
}
|
||||
|
||||
var computedMsg: Data {
|
||||
try! Data(hexString: self.msg)
|
||||
}
|
||||
|
||||
var computedTag: Data {
|
||||
try! Data(hexString: self.tag)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,11 @@ extension Array where Element == UInt8 {
|
||||
init(hexString: String) throws {
|
||||
self.init()
|
||||
|
||||
guard hexString.count.isMultiple(of: 2), !hexString.isEmpty else {
|
||||
if hexString.count == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if hexString.count % 2 != 0 {
|
||||
throw ByteHexEncodingErrors.incorrectString
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user