@@ -26,16 +26,21 @@ extension AES._CBC {
2626 /// that nonces are unique per call to encryption APIs in order to protect the
2727 /// integrity of the encryption.
2828 public struct IV : Sendable , ContiguousBytes , Sequence {
29- private var bytes : Data
29+ typealias IVTuple = (
30+ UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ,
31+ UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8
32+ )
33+
34+ private var bytes : IVTuple
3035
3136 /// Creates a new random nonce.
3237 public init ( ) {
33- var data = Data ( repeating : 0 , count : AES . _CBC . nonceByteCount )
34- data . withUnsafeMutableBytes {
35- assert ( $0 . count == AES . _CBC . nonceByteCount )
36- $0. initializeWithRandomBytes ( count: AES . _CBC . nonceByteCount )
38+ var bytes = IVTuple ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
39+ Swift . withUnsafeMutableBytes ( of : & bytes ) {
40+ let count = MemoryLayout < IVTuple > . size
41+ $0. initializeWithRandomBytes ( count: count )
3742 }
38- self . bytes = data
43+ self . bytes = bytes
3944 }
4045
4146 /// Creates a nonce from the given collection.
@@ -47,14 +52,15 @@ extension AES._CBC {
4752 /// - ivBytes: A collection of bytes representation of the nonce.
4853 /// The initializer throws an error if the data has the incorrect length.
4954 public init < IVBytes: Collection > ( ivBytes: IVBytes ) throws where IVBytes. Element == UInt8 {
50- guard ivBytes. count == AES . _CBC . nonceByteCount else {
55+ guard ivBytes. count == MemoryLayout < IVTuple > . size else {
5156 throw CryptoKitError . incorrectKeySize
5257 }
5358
54- self . bytes = Data ( repeating : 0 , count : AES . _CBC . nonceByteCount )
55- Swift . withUnsafeMutableBytes ( of: & self . bytes) { bytesPtr in
59+ var bytes = IVTuple ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
60+ Swift . withUnsafeMutableBytes ( of: & bytes) { bytesPtr in
5661 bytesPtr. copyBytes ( from: ivBytes)
5762 }
63+ self . bytes = bytes
5864 }
5965
6066 /// Creates a nonce from the given data.
@@ -66,11 +72,15 @@ extension AES._CBC {
6672 /// - data: A data representation of the nonce. The initializer throws an
6773 /// error if the data has the incorrect length.
6874 public init < D: DataProtocol > ( data: D ) throws {
69- if data. count != AES . _CBC . nonceByteCount {
75+ if data. count != MemoryLayout < IVTuple > . size {
7076 throw CryptoKitError . incorrectParameterSize
7177 }
7278
73- self . bytes = Data ( data)
79+ var bytes = IVTuple ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
80+ Swift . withUnsafeMutableBytes ( of: & bytes) { bytesPtr in
81+ data. copyBytes ( to: bytesPtr)
82+ }
83+ self . bytes = bytes
7484 }
7585
7686 /// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -85,11 +95,13 @@ extension AES._CBC {
8595 ///
8696 /// - Returns: The return value, if any, of the body closure parameter.
8797 public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
88- return try self . bytes. withUnsafeBytes ( body)
98+ var bytes = self . bytes
99+ return try Swift . withUnsafeBytes ( of: & bytes, body)
89100 }
90101
91102 mutating func withUnsafeMutableBytes< ReturnType> ( _ body: ( UnsafeMutableRawBufferPointer ) throws -> ReturnType ) rethrows -> ReturnType {
92- return try Swift . withUnsafeMutableBytes ( of: & self . bytes, body)
103+ var bytes = self . bytes
104+ return try Swift . withUnsafeMutableBytes ( of: & bytes, body)
93105 }
94106
95107 /// Returns an iterator over the elements of the nonce.
@@ -109,16 +121,18 @@ extension AES._CFB {
109121 /// that nonces are unique per call to encryption APIs in order to protect the
110122 /// integrity of the encryption.
111123 public struct IV : Sendable , ContiguousBytes , Sequence {
112- private var bytes : Data
124+ typealias IVTuple = ( UInt64 , UInt64 )
125+
126+ private var bytes : IVTuple
113127
114128 /// Creates a new random nonce.
115129 public init ( ) {
116- var data = Data ( repeating : 0 , count : AES . _CFB . nonceByteCount )
117- data . withUnsafeMutableBytes {
118- assert ( $0 . count == AES . _CFB . nonceByteCount )
119- $0. initializeWithRandomBytes ( count: AES . _CFB . nonceByteCount )
130+ var bytes = IVTuple ( 0 , 0 )
131+ Swift . withUnsafeMutableBytes ( of : & bytes ) {
132+ let count = MemoryLayout < IVTuple > . size
133+ $0. initializeWithRandomBytes ( count: count )
120134 }
121- self . bytes = data
135+ self . bytes = bytes
122136 }
123137
124138 /// Creates a nonce from the given collection.
@@ -130,14 +144,15 @@ extension AES._CFB {
130144 /// - ivBytes: A collection of bytes representation of the nonce.
131145 /// The initializer throws an error if the data has the incorrect length.
132146 public init < IVBytes: Collection > ( ivBytes: IVBytes ) throws where IVBytes. Element == UInt8 {
133- guard ivBytes. count == AES . _CFB . nonceByteCount else {
147+ guard ivBytes. count == MemoryLayout < IVTuple > . size else {
134148 throw CryptoKitError . incorrectKeySize
135149 }
136150
137- self . bytes = Data ( repeating : 0 , count : AES . _CFB . nonceByteCount )
138- Swift . withUnsafeMutableBytes ( of: & self . bytes) { bytesPtr in
151+ var bytes = IVTuple ( 0 , 0 )
152+ Swift . withUnsafeMutableBytes ( of: & bytes) { bytesPtr in
139153 bytesPtr. copyBytes ( from: ivBytes)
140154 }
155+ self . bytes = bytes
141156 }
142157
143158 /// Creates a nonce from the given data.
@@ -149,11 +164,15 @@ extension AES._CFB {
149164 /// - data: A data representation of the nonce. The initializer throws an
150165 /// error if the data has the incorrect length.
151166 public init < D: DataProtocol > ( data: D ) throws {
152- if data. count != AES . _CFB . nonceByteCount {
167+ if data. count != MemoryLayout < IVTuple > . size {
153168 throw CryptoKitError . incorrectParameterSize
154169 }
155170
156- self . bytes = Data ( data)
171+ var bytes = IVTuple ( 0 , 0 )
172+ Swift . withUnsafeMutableBytes ( of: & bytes) { bytesPtr in
173+ data. copyBytes ( to: bytesPtr)
174+ }
175+ self . bytes = bytes
157176 }
158177
159178 /// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -168,11 +187,13 @@ extension AES._CFB {
168187 ///
169188 /// - Returns: The return value, if any, of the body closure parameter.
170189 public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
171- return try self . bytes. withUnsafeBytes ( body)
190+ var bytes = self . bytes
191+ return try Swift . withUnsafeBytes ( of: & bytes, body)
172192 }
173193
174194 mutating func withUnsafeMutableBytes< ReturnType> ( _ body: ( UnsafeMutableRawBufferPointer ) throws -> ReturnType ) rethrows -> ReturnType {
175- return try Swift . withUnsafeMutableBytes ( of: & self . bytes, body)
195+ var bytes = self . bytes
196+ return try Swift . withUnsafeMutableBytes ( of: & bytes, body)
176197 }
177198
178199 /// Returns an iterator over the elements of the nonce.
@@ -192,16 +213,18 @@ extension AES._CTR {
192213 /// that nonces are unique per call to encryption APIs in order to protect the
193214 /// integrity of the encryption.
194215 public struct Nonce : Sendable , ContiguousBytes , Sequence {
195- private var bytes : Data
216+ typealias NonceTuple = ( UInt64 , UInt32 , UInt32 )
217+
218+ private var bytes : NonceTuple
196219
197220 /// Creates a new random nonce.
198221 public init ( ) {
199- var data = Data ( repeating : 0 , count : AES . _CTR . nonceByteCount )
200- data . withUnsafeMutableBytes {
201- assert ( $0 . count == AES . _CTR . nonceByteCount )
202- $0. initializeWithRandomBytes ( count: AES . _CTR . nonceByteCount )
222+ var bytes = NonceTuple ( 0 , 0 , 0 )
223+ Swift . withUnsafeMutableBytes ( of : & bytes ) {
224+ let count = MemoryLayout < NonceTuple > . size
225+ $0. initializeWithRandomBytes ( count: count )
203226 }
204- self . bytes = data
227+ self . bytes = bytes
205228 }
206229
207230 /// Creates a nonce from the given collection.
@@ -213,14 +236,15 @@ extension AES._CTR {
213236 /// - nonceBytes: A collection of bytes representation of the nonce.
214237 /// The initializer throws an error if the data has the incorrect length.
215238 public init < NonceBytes: Collection > ( nonceBytes: NonceBytes ) throws where NonceBytes. Element == UInt8 {
216- guard nonceBytes. count == AES . _CTR . nonceByteCount else {
239+ guard nonceBytes. count == MemoryLayout < NonceTuple > . size else {
217240 throw CryptoKitError . incorrectKeySize
218241 }
219242
220- self . bytes = Data ( repeating : 0 , count : AES . _CTR . nonceByteCount )
221- Swift . withUnsafeMutableBytes ( of: & self . bytes) { bytesPtr in
243+ var bytes = NonceTuple ( 0 , 0 , 0 )
244+ Swift . withUnsafeMutableBytes ( of: & bytes) { bytesPtr in
222245 bytesPtr. copyBytes ( from: nonceBytes)
223246 }
247+ self . bytes = bytes
224248 }
225249
226250 /// Creates a nonce from the given data.
@@ -232,11 +256,15 @@ extension AES._CTR {
232256 /// - data: A data representation of the nonce. The initializer throws an
233257 /// error if the data has the incorrect length.
234258 public init < D: DataProtocol > ( data: D ) throws {
235- if data. count != AES . _CBC . nonceByteCount {
259+ if data. count != MemoryLayout < NonceTuple > . size {
236260 throw CryptoKitError . incorrectParameterSize
237261 }
238262
239- self . bytes = Data ( data)
263+ var bytes = NonceTuple ( 0 , 0 , 0 )
264+ Swift . withUnsafeMutableBytes ( of: & bytes) { bytesPtr in
265+ data. copyBytes ( to: bytesPtr)
266+ }
267+ self . bytes = bytes
240268 }
241269
242270 /// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -251,11 +279,13 @@ extension AES._CTR {
251279 ///
252280 /// - Returns: The return value, if any, of the body closure parameter.
253281 public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
254- return try self . bytes. withUnsafeBytes ( body)
282+ var bytes = self . bytes
283+ return try Swift . withUnsafeBytes ( of: & bytes, body)
255284 }
256285
257286 mutating func withUnsafeMutableBytes< ReturnType> ( _ body: ( UnsafeMutableRawBufferPointer ) throws -> ReturnType ) rethrows -> ReturnType {
258- return try Swift . withUnsafeMutableBytes ( of: & self . bytes, body)
287+ var bytes = self . bytes
288+ return try Swift . withUnsafeMutableBytes ( of: & bytes, body)
259289 }
260290
261291 /// Returns an iterator over the elements of the nonce.
0 commit comments