Skip to content

Commit 773f208

Browse files
committed
Formatting and restoring Block constructor
1 parent 519f411 commit 773f208

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

Sources/_CryptoExtras/AES/AES_CBC.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension AES {
6767
var ciphertext = Data()
6868
ciphertext.reserveCapacity(plaintext.count + Self.blockSize) // Room for padding.
6969

70-
var previousBlock = Block(blockBytes: iv)
70+
var previousBlock = Block(iv)
7171
var plaintext = plaintext[...]
7272

7373
while plaintext.count > 0 {
@@ -121,7 +121,7 @@ extension AES {
121121
var plaintext = Data()
122122
plaintext.reserveCapacity(ciphertext.count)
123123

124-
var previousBlock = Block(blockBytes: iv)
124+
var previousBlock = Block(iv)
125125
var ciphertext = ciphertext[...]
126126

127127
while ciphertext.count > 0 {

Sources/_CryptoExtras/AES/Block Function.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ extension AES {
132132
self.blockBytes = blockBytes
133133
}
134134

135+
init(_ iv: AES._CBC.IV) {
136+
self.blockBytes = iv.bytes
137+
}
138+
135139
init<BlockBytes: Sequence>(blockBytes: BlockBytes) where BlockBytes.Element == UInt8 {
136140
let blockBytes: [UInt8] = Array(blockBytes)
137141
self.init(blockBytes: blockBytes)

Sources/_CryptoExtras/AES/Nonces.swift

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ extension AES._CBC {
3131
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8
3232
)
3333

34-
private var bytes: IVTuple
34+
var bytes: IVTuple
35+
static var emptyBytes: IVTuple = (
36+
0, 0, 0, 0, 0, 0, 0, 0,
37+
0, 0, 0, 0, 0, 0, 0, 0
38+
)
3539

3640
/// Creates a new random nonce.
3741
public init() {
38-
var bytes = IVTuple(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
42+
var bytes = Self.emptyBytes
3943
Swift.withUnsafeMutableBytes(of: &bytes) {
4044
let count = MemoryLayout<IVTuple>.size
4145
$0.initializeWithRandomBytes(count: count)
@@ -52,11 +56,11 @@ extension AES._CBC {
5256
/// - ivBytes: A collection of bytes representation of the nonce.
5357
/// The initializer throws an error if the data has the incorrect length.
5458
public init<IVBytes: Collection>(ivBytes: IVBytes) throws where IVBytes.Element == UInt8 {
55-
guard ivBytes.count == MemoryLayout<IVTuple>.size else {
59+
guard [16].contains(ivBytes.count) else {
5660
throw CryptoKitError.incorrectKeySize
5761
}
5862

59-
var bytes = IVTuple(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
63+
var bytes = Self.emptyBytes
6064
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
6165
bytesPtr.copyBytes(from: ivBytes)
6266
}
@@ -72,11 +76,11 @@ extension AES._CBC {
7276
/// - data: A data representation of the nonce. The initializer throws an
7377
/// error if the data has the incorrect length.
7478
public init<D: DataProtocol>(data: D) throws {
75-
if data.count != MemoryLayout<IVTuple>.size {
76-
throw CryptoKitError.incorrectParameterSize
79+
guard [16].contains(data.count) else {
80+
throw CryptoKitError.incorrectKeySize
7781
}
7882

79-
var bytes = IVTuple(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
83+
var bytes = Self.emptyBytes
8084
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
8185
data.copyBytes(to: bytesPtr)
8286
}
@@ -123,11 +127,12 @@ extension AES._CFB {
123127
public struct IV: Sendable, ContiguousBytes, Sequence {
124128
typealias IVTuple = (UInt64, UInt64)
125129

126-
private var bytes: IVTuple
130+
var bytes: IVTuple
131+
static var emptyBytes: IVTuple = (0, 0)
127132

128133
/// Creates a new random nonce.
129134
public init() {
130-
var bytes = IVTuple(0,0)
135+
var bytes = Self.emptyBytes
131136
Swift.withUnsafeMutableBytes(of: &bytes) {
132137
let count = MemoryLayout<IVTuple>.size
133138
$0.initializeWithRandomBytes(count: count)
@@ -144,11 +149,11 @@ extension AES._CFB {
144149
/// - ivBytes: A collection of bytes representation of the nonce.
145150
/// The initializer throws an error if the data has the incorrect length.
146151
public init<IVBytes: Collection>(ivBytes: IVBytes) throws where IVBytes.Element == UInt8 {
147-
guard ivBytes.count == MemoryLayout<IVTuple>.size else {
152+
guard [16].contains(ivBytes.count) else {
148153
throw CryptoKitError.incorrectKeySize
149154
}
150155

151-
var bytes = IVTuple(0,0)
156+
var bytes = Self.emptyBytes
152157
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
153158
bytesPtr.copyBytes(from: ivBytes)
154159
}
@@ -164,11 +169,11 @@ extension AES._CFB {
164169
/// - data: A data representation of the nonce. The initializer throws an
165170
/// error if the data has the incorrect length.
166171
public init<D: DataProtocol>(data: D) throws {
167-
if data.count != MemoryLayout<IVTuple>.size {
168-
throw CryptoKitError.incorrectParameterSize
172+
guard [16].contains(data.count) else {
173+
throw CryptoKitError.incorrectKeySize
169174
}
170175

171-
var bytes = IVTuple(0,0)
176+
var bytes = Self.emptyBytes
172177
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
173178
data.copyBytes(to: bytesPtr)
174179
}
@@ -215,11 +220,12 @@ extension AES._CTR {
215220
public struct Nonce: Sendable, ContiguousBytes, Sequence {
216221
typealias NonceTuple = (UInt64, UInt32, UInt32)
217222

218-
private var bytes: NonceTuple
223+
var bytes: NonceTuple
224+
static var emptyBytes: NonceTuple = (0, 0, 0)
219225

220226
/// Creates a new random nonce.
221227
public init() {
222-
var bytes = NonceTuple(0,0,0)
228+
var bytes = Self.emptyBytes
223229
Swift.withUnsafeMutableBytes(of: &bytes) {
224230
let count = MemoryLayout<NonceTuple>.size
225231
$0.initializeWithRandomBytes(count: count)
@@ -236,11 +242,11 @@ extension AES._CTR {
236242
/// - nonceBytes: A collection of bytes representation of the nonce.
237243
/// The initializer throws an error if the data has the incorrect length.
238244
public init<NonceBytes: Collection>(nonceBytes: NonceBytes) throws where NonceBytes.Element == UInt8 {
239-
guard nonceBytes.count == MemoryLayout<NonceTuple>.size else {
245+
guard [12, 16].contains(nonceBytes.count) else {
240246
throw CryptoKitError.incorrectKeySize
241247
}
242248

243-
var bytes = NonceTuple(0,0,0)
249+
var bytes = Self.emptyBytes
244250
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
245251
bytesPtr.copyBytes(from: nonceBytes)
246252
}
@@ -256,11 +262,11 @@ extension AES._CTR {
256262
/// - data: A data representation of the nonce. The initializer throws an
257263
/// error if the data has the incorrect length.
258264
public init<D: DataProtocol>(data: D) throws {
259-
if data.count != MemoryLayout<NonceTuple>.size {
260-
throw CryptoKitError.incorrectParameterSize
265+
guard [12, 16].contains(data.count) else {
266+
throw CryptoKitError.incorrectKeySize
261267
}
262268

263-
var bytes = NonceTuple(0,0,0)
269+
var bytes = Self.emptyBytes
264270
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
265271
data.copyBytes(to: bytesPtr)
266272
}

Sources/_CryptoExtras/AES/Nonces.swift.gyb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ ciphers = [
2222
{"name": "AES._CBC", "nonceName": "IV", "tupleDefinition": """(
2323
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
2424
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)"}]
25+
)""", "tupleBlank": """(
26+
0, 0, 0, 0, 0, 0, 0, 0,
27+
0, 0, 0, 0, 0, 0, 0, 0
28+
)""", "validSizes": "[16]"},
29+
{"name": "AES._CFB", "nonceName": "IV", "tupleDefinition": "(UInt64, UInt64)", "tupleBlank": "(0, 0)", "validSizes": "[16]"},
30+
{"name": "AES._CTR", "nonceName": "Nonce", "tupleDefinition": "(UInt64, UInt32, UInt32)", "tupleBlank": "(0, 0, 0)", "validSizes": "[12, 16]"}]
2831
}%
2932
% for cipher in ciphers:
3033
%{
3134
name = cipher["name"]
3235
nonceName = cipher["nonceName"]
3336
tupleDefinition = cipher["tupleDefinition"]
3437
tupleBlank = cipher["tupleBlank"]
38+
validSizes = cipher["validSizes"]
3539
}%
3640

3741
// MARK: - ${name} + ${nonceName}
@@ -44,11 +48,12 @@ extension ${name} {
4448
public struct ${nonceName}: Sendable, ContiguousBytes, Sequence {
4549
typealias ${nonceName}Tuple = ${tupleDefinition}
4650

47-
private var bytes: ${nonceName}Tuple
51+
var bytes: ${nonceName}Tuple
52+
static var emptyBytes: ${nonceName}Tuple = ${tupleBlank}
4853

4954
/// Creates a new random nonce.
5055
public init() {
51-
var bytes = ${nonceName}Tuple${tupleBlank}
56+
var bytes = Self.emptyBytes
5257
Swift.withUnsafeMutableBytes(of: &bytes) {
5358
let count = MemoryLayout<${nonceName}Tuple>.size
5459
$0.initializeWithRandomBytes(count: count)
@@ -65,11 +70,11 @@ extension ${name} {
6570
/// - ${nonceName.lower()}Bytes: A collection of bytes representation of the nonce.
6671
/// The initializer throws an error if the data has the incorrect length.
6772
public init<${nonceName}Bytes: Collection>(${nonceName.lower()}Bytes: ${nonceName}Bytes) throws where ${nonceName}Bytes.Element == UInt8 {
68-
guard ${nonceName.lower()}Bytes.count == MemoryLayout<${nonceName}Tuple>.size else {
73+
guard ${validSizes}.contains(${nonceName.lower()}Bytes.count) else {
6974
throw CryptoKitError.incorrectKeySize
7075
}
7176

72-
var bytes = ${nonceName}Tuple${tupleBlank}
77+
var bytes = Self.emptyBytes
7378
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
7479
bytesPtr.copyBytes(from: ${nonceName.lower()}Bytes)
7580
}
@@ -85,11 +90,11 @@ extension ${name} {
8590
/// - data: A data representation of the nonce. The initializer throws an
8691
/// error if the data has the incorrect length.
8792
public init<D: DataProtocol>(data: D) throws {
88-
if data.count != MemoryLayout<${nonceName}Tuple>.size {
89-
throw CryptoKitError.incorrectParameterSize
93+
guard ${validSizes}.contains(data.count) else {
94+
throw CryptoKitError.incorrectKeySize
9095
}
9196

92-
var bytes = ${nonceName}Tuple${tupleBlank}
97+
var bytes = Self.emptyBytes
9398
Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in
9499
data.copyBytes(to: bytesPtr)
95100
}

0 commit comments

Comments
 (0)