Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/_CryptoExtras/AES/AES_CBC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/_CryptoExtrasTests/AES_CBCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}


Expand Down