@@ -26,16 +26,21 @@ extension AES._CBC {
26
26
/// that nonces are unique per call to encryption APIs in order to protect the
27
27
/// integrity of the encryption.
28
28
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
30
35
31
36
/// Creates a new random nonce.
32
37
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 )
37
42
}
38
- self . bytes = data
43
+ self . bytes = bytes
39
44
}
40
45
41
46
/// Creates a nonce from the given collection.
@@ -47,14 +52,15 @@ extension AES._CBC {
47
52
/// - ivBytes: A collection of bytes representation of the nonce.
48
53
/// The initializer throws an error if the data has the incorrect length.
49
54
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 {
51
56
throw CryptoKitError . incorrectKeySize
52
57
}
53
58
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
56
61
bytesPtr. copyBytes ( from: ivBytes)
57
62
}
63
+ self . bytes = bytes
58
64
}
59
65
60
66
/// Creates a nonce from the given data.
@@ -66,11 +72,15 @@ extension AES._CBC {
66
72
/// - data: A data representation of the nonce. The initializer throws an
67
73
/// error if the data has the incorrect length.
68
74
public init < D: DataProtocol > ( data: D ) throws {
69
- if data. count != AES . _CBC . nonceByteCount {
75
+ if data. count != MemoryLayout < IVTuple > . size {
70
76
throw CryptoKitError . incorrectParameterSize
71
77
}
72
78
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
74
84
}
75
85
76
86
/// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -85,11 +95,13 @@ extension AES._CBC {
85
95
///
86
96
/// - Returns: The return value, if any, of the body closure parameter.
87
97
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)
89
100
}
90
101
91
102
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)
93
105
}
94
106
95
107
/// Returns an iterator over the elements of the nonce.
@@ -109,16 +121,18 @@ extension AES._CFB {
109
121
/// that nonces are unique per call to encryption APIs in order to protect the
110
122
/// integrity of the encryption.
111
123
public struct IV : Sendable , ContiguousBytes , Sequence {
112
- private var bytes : Data
124
+ typealias IVTuple = ( UInt64 , UInt64 )
125
+
126
+ private var bytes : IVTuple
113
127
114
128
/// Creates a new random nonce.
115
129
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 )
120
134
}
121
- self . bytes = data
135
+ self . bytes = bytes
122
136
}
123
137
124
138
/// Creates a nonce from the given collection.
@@ -130,14 +144,15 @@ extension AES._CFB {
130
144
/// - ivBytes: A collection of bytes representation of the nonce.
131
145
/// The initializer throws an error if the data has the incorrect length.
132
146
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 {
134
148
throw CryptoKitError . incorrectKeySize
135
149
}
136
150
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
139
153
bytesPtr. copyBytes ( from: ivBytes)
140
154
}
155
+ self . bytes = bytes
141
156
}
142
157
143
158
/// Creates a nonce from the given data.
@@ -149,11 +164,15 @@ extension AES._CFB {
149
164
/// - data: A data representation of the nonce. The initializer throws an
150
165
/// error if the data has the incorrect length.
151
166
public init < D: DataProtocol > ( data: D ) throws {
152
- if data. count != AES . _CFB . nonceByteCount {
167
+ if data. count != MemoryLayout < IVTuple > . size {
153
168
throw CryptoKitError . incorrectParameterSize
154
169
}
155
170
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
157
176
}
158
177
159
178
/// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -168,11 +187,13 @@ extension AES._CFB {
168
187
///
169
188
/// - Returns: The return value, if any, of the body closure parameter.
170
189
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)
172
192
}
173
193
174
194
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)
176
197
}
177
198
178
199
/// Returns an iterator over the elements of the nonce.
@@ -192,16 +213,18 @@ extension AES._CTR {
192
213
/// that nonces are unique per call to encryption APIs in order to protect the
193
214
/// integrity of the encryption.
194
215
public struct Nonce : Sendable , ContiguousBytes , Sequence {
195
- private var bytes : Data
216
+ typealias NonceTuple = ( UInt64 , UInt32 , UInt32 )
217
+
218
+ private var bytes : NonceTuple
196
219
197
220
/// Creates a new random nonce.
198
221
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 )
203
226
}
204
- self . bytes = data
227
+ self . bytes = bytes
205
228
}
206
229
207
230
/// Creates a nonce from the given collection.
@@ -213,14 +236,15 @@ extension AES._CTR {
213
236
/// - nonceBytes: A collection of bytes representation of the nonce.
214
237
/// The initializer throws an error if the data has the incorrect length.
215
238
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 {
217
240
throw CryptoKitError . incorrectKeySize
218
241
}
219
242
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
222
245
bytesPtr. copyBytes ( from: nonceBytes)
223
246
}
247
+ self . bytes = bytes
224
248
}
225
249
226
250
/// Creates a nonce from the given data.
@@ -232,11 +256,15 @@ extension AES._CTR {
232
256
/// - data: A data representation of the nonce. The initializer throws an
233
257
/// error if the data has the incorrect length.
234
258
public init < D: DataProtocol > ( data: D ) throws {
235
- if data. count != AES . _CBC . nonceByteCount {
259
+ if data. count != MemoryLayout < NonceTuple > . size {
236
260
throw CryptoKitError . incorrectParameterSize
237
261
}
238
262
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
240
268
}
241
269
242
270
/// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -251,11 +279,13 @@ extension AES._CTR {
251
279
///
252
280
/// - Returns: The return value, if any, of the body closure parameter.
253
281
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)
255
284
}
256
285
257
286
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)
259
289
}
260
290
261
291
/// Returns an iterator over the elements of the nonce.
0 commit comments