Skip to content

Commit 519f411

Browse files
committed
swiftching back to tuples
1 parent cc3709d commit 519f411

File tree

3 files changed

+100
-56
lines changed

3 files changed

+100
-56
lines changed

Sources/_CryptoExtras/AES/AES_CBC.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extension AES {
2020
/// suite.
2121
public enum _CBC {
2222
private static var blockSize: Int { 16 }
23-
static let nonceByteCount = 16
2423

2524
private static func encryptBlockInPlace(
2625
_ plaintext: inout Block,

Sources/_CryptoExtras/AES/Nonces.swift

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

Sources/_CryptoExtras/AES/Nonces.swift.gyb

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ import Foundation
1818
// any edits of this file WILL be overwritten and thus discarded
1919
// see section `gyb` in `README` for details.
2020
%{
21-
ciphers = [{"name": "AES._CBC", "nonceName": "IV", "recommendedNonceSize": "AES._CBC.nonceByteCount", "nonceValidation": "!= AES._CBC.nonceByteCount"},{"name": "AES._CFB", "nonceName": "IV", "recommendedNonceSize": "AES._CFB.nonceByteCount", "nonceValidation": "!= AES._CFB.nonceByteCount"},{"name": "AES._CTR", "nonceName": "Nonce", "recommendedNonceSize": "AES._CTR.nonceByteCount", "nonceValidation": "!= AES._CBC.nonceByteCount"}]
21+
ciphers = [
22+
{"name": "AES._CBC", "nonceName": "IV", "tupleDefinition": """(
23+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
24+
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8
25+
)""", "tupleBlank": "(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)"},
26+
{"name": "AES._CFB", "nonceName": "IV", "tupleDefinition": "(UInt64, UInt64)", "tupleBlank": "(0,0)"},
27+
{"name": "AES._CTR", "nonceName": "Nonce", "tupleDefinition": "(UInt64, UInt32, UInt32)", "tupleBlank": "(0,0,0)"}]
2228
}%
2329
% for cipher in ciphers:
2430
%{
2531
name = cipher["name"]
2632
nonceName = cipher["nonceName"]
27-
nonceSize = cipher["recommendedNonceSize"]
28-
nonceValidation = cipher["nonceValidation"]
33+
tupleDefinition = cipher["tupleDefinition"]
34+
tupleBlank = cipher["tupleBlank"]
2935
}%
3036

3137
// MARK: - ${name} + ${nonceName}
@@ -36,16 +42,18 @@ extension ${name} {
3642
/// that nonces are unique per call to encryption APIs in order to protect the
3743
/// integrity of the encryption.
3844
public struct ${nonceName}: Sendable, ContiguousBytes, Sequence {
39-
private var bytes: Data
45+
typealias ${nonceName}Tuple = ${tupleDefinition}
46+
47+
private var bytes: ${nonceName}Tuple
4048

4149
/// Creates a new random nonce.
4250
public init() {
43-
var data = Data(repeating: 0, count: ${nonceSize})
44-
data.withUnsafeMutableBytes {
45-
assert($0.count == ${nonceSize})
46-
$0.initializeWithRandomBytes(count: ${nonceSize})
51+
var bytes = ${nonceName}Tuple${tupleBlank}
52+
Swift.withUnsafeMutableBytes(of: &bytes) {
53+
let count = MemoryLayout<${nonceName}Tuple>.size
54+
$0.initializeWithRandomBytes(count: count)
4755
}
48-
self.bytes = data
56+
self.bytes = bytes
4957
}
5058

5159
/// Creates a nonce from the given collection.
@@ -57,14 +65,15 @@ extension ${name} {
5765
/// - ${nonceName.lower()}Bytes: A collection of bytes representation of the nonce.
5866
/// The initializer throws an error if the data has the incorrect length.
5967
public init<${nonceName}Bytes: Collection>(${nonceName.lower()}Bytes: ${nonceName}Bytes) throws where ${nonceName}Bytes.Element == UInt8 {
60-
guard ${nonceName.lower()}Bytes.count == ${nonceSize} else {
68+
guard ${nonceName.lower()}Bytes.count == MemoryLayout<${nonceName}Tuple>.size else {
6169
throw CryptoKitError.incorrectKeySize
6270
}
6371

64-
self.bytes = Data(repeating: 0, count: ${nonceSize})
65-
Swift.withUnsafeMutableBytes(of: &self.bytes) { bytesPtr in
72+
var bytes = ${nonceName}Tuple${tupleBlank}
73+
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
6674
bytesPtr.copyBytes(from: ${nonceName.lower()}Bytes)
6775
}
76+
self.bytes = bytes
6877
}
6978

7079
/// Creates a nonce from the given data.
@@ -76,11 +85,15 @@ extension ${name} {
7685
/// - data: A data representation of the nonce. The initializer throws an
7786
/// error if the data has the incorrect length.
7887
public init<D: DataProtocol>(data: D) throws {
79-
if data.count ${nonceValidation} {
88+
if data.count != MemoryLayout<${nonceName}Tuple>.size {
8089
throw CryptoKitError.incorrectParameterSize
8190
}
8291

83-
self.bytes = Data(data)
92+
var bytes = ${nonceName}Tuple${tupleBlank}
93+
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
94+
data.copyBytes(to: bytesPtr)
95+
}
96+
self.bytes = bytes
8497
}
8598

8699
/// Calls the given closure with a pointer to the underlying bytes of the array’s
@@ -95,11 +108,13 @@ extension ${name} {
95108
///
96109
/// - Returns: The return value, if any, of the body closure parameter.
97110
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
98-
return try self.bytes.withUnsafeBytes(body)
111+
var bytes = self.bytes
112+
return try Swift.withUnsafeBytes(of: &bytes, body)
99113
}
100114

101115
mutating func withUnsafeMutableBytes<ReturnType>(_ body: (UnsafeMutableRawBufferPointer) throws -> ReturnType) rethrows -> ReturnType {
102-
return try Swift.withUnsafeMutableBytes(of: &self.bytes, body)
116+
var bytes = self.bytes
117+
return try Swift.withUnsafeMutableBytes(of: &bytes, body)
103118
}
104119

105120
/// Returns an iterator over the elements of the nonce.

0 commit comments

Comments
 (0)