From 55eaa0f870fd0cc954d1c5c33e90cccfc840a841 Mon Sep 17 00:00:00 2001 From: Mark Schall Date: Tue, 1 Oct 2024 10:09:29 -0400 Subject: [PATCH] Formatting and restoring Block constructor --- Sources/_CryptoExtras/AES/AES_CBC.swift | 4 +-- .../_CryptoExtras/AES/Block Function.swift | 4 +++ Sources/_CryptoExtras/AES/Nonces.swift | 30 +++++++++++-------- Sources/_CryptoExtras/AES/Nonces.swift.gyb | 18 ++++++----- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Sources/_CryptoExtras/AES/AES_CBC.swift b/Sources/_CryptoExtras/AES/AES_CBC.swift index 2bf4ea3e..85c449d7 100644 --- a/Sources/_CryptoExtras/AES/AES_CBC.swift +++ b/Sources/_CryptoExtras/AES/AES_CBC.swift @@ -67,7 +67,7 @@ extension AES { var ciphertext = Data() ciphertext.reserveCapacity(plaintext.count + Self.blockSize) // Room for padding. - var previousBlock = Block(blockBytes: iv) + var previousBlock = Block(iv) var plaintext = plaintext[...] while plaintext.count > 0 { @@ -121,7 +121,7 @@ extension AES { var plaintext = Data() plaintext.reserveCapacity(ciphertext.count) - var previousBlock = Block(blockBytes: iv) + var previousBlock = Block(iv) var ciphertext = ciphertext[...] while ciphertext.count > 0 { diff --git a/Sources/_CryptoExtras/AES/Block Function.swift b/Sources/_CryptoExtras/AES/Block Function.swift index aef1f6f1..71546db6 100644 --- a/Sources/_CryptoExtras/AES/Block Function.swift +++ b/Sources/_CryptoExtras/AES/Block Function.swift @@ -132,6 +132,10 @@ extension AES { self.blockBytes = blockBytes } + init(_ iv: AES._CBC.IV) { + self.blockBytes = iv.bytes + } + init(blockBytes: BlockBytes) where BlockBytes.Element == UInt8 { let blockBytes: [UInt8] = Array(blockBytes) self.init(blockBytes: blockBytes) diff --git a/Sources/_CryptoExtras/AES/Nonces.swift b/Sources/_CryptoExtras/AES/Nonces.swift index 1d022ae8..4a859738 100644 --- a/Sources/_CryptoExtras/AES/Nonces.swift +++ b/Sources/_CryptoExtras/AES/Nonces.swift @@ -31,11 +31,15 @@ extension AES._CBC { UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8 ) - private var bytes: IVTuple + var bytes: IVTuple + static var emptyBytes: IVTuple = ( + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + ) /// Creates a new random nonce. public init() { - var bytes = IVTuple(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { let count = MemoryLayout.size $0.initializeWithRandomBytes(count: count) @@ -56,7 +60,7 @@ extension AES._CBC { throw CryptoKitError.incorrectKeySize } - var bytes = IVTuple(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in bytesPtr.copyBytes(from: ivBytes) } @@ -76,7 +80,7 @@ extension AES._CBC { throw CryptoKitError.incorrectParameterSize } - var bytes = IVTuple(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in data.copyBytes(to: bytesPtr) } @@ -123,11 +127,12 @@ extension AES._CFB { public struct IV: Sendable, ContiguousBytes, Sequence { typealias IVTuple = (UInt64, UInt64) - private var bytes: IVTuple + var bytes: IVTuple + static var emptyBytes: IVTuple = (0, 0) /// Creates a new random nonce. public init() { - var bytes = IVTuple(0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { let count = MemoryLayout.size $0.initializeWithRandomBytes(count: count) @@ -148,7 +153,7 @@ extension AES._CFB { throw CryptoKitError.incorrectKeySize } - var bytes = IVTuple(0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in bytesPtr.copyBytes(from: ivBytes) } @@ -168,7 +173,7 @@ extension AES._CFB { throw CryptoKitError.incorrectParameterSize } - var bytes = IVTuple(0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in data.copyBytes(to: bytesPtr) } @@ -215,11 +220,12 @@ extension AES._CTR { public struct Nonce: Sendable, ContiguousBytes, Sequence { typealias NonceTuple = (UInt64, UInt32, UInt32) - private var bytes: NonceTuple + var bytes: NonceTuple + static var emptyBytes: NonceTuple = (0, 0, 0) /// Creates a new random nonce. public init() { - var bytes = NonceTuple(0,0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { let count = MemoryLayout.size $0.initializeWithRandomBytes(count: count) @@ -240,7 +246,7 @@ extension AES._CTR { throw CryptoKitError.incorrectKeySize } - var bytes = NonceTuple(0,0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in bytesPtr.copyBytes(from: nonceBytes) } @@ -260,7 +266,7 @@ extension AES._CTR { throw CryptoKitError.incorrectParameterSize } - var bytes = NonceTuple(0,0,0) + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in data.copyBytes(to: bytesPtr) } diff --git a/Sources/_CryptoExtras/AES/Nonces.swift.gyb b/Sources/_CryptoExtras/AES/Nonces.swift.gyb index fb434c77..e622cb02 100644 --- a/Sources/_CryptoExtras/AES/Nonces.swift.gyb +++ b/Sources/_CryptoExtras/AES/Nonces.swift.gyb @@ -22,9 +22,12 @@ ciphers = [ {"name": "AES._CBC", "nonceName": "IV", "tupleDefinition": """( UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8 - )""", "tupleBlank": "(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)"}, - {"name": "AES._CFB", "nonceName": "IV", "tupleDefinition": "(UInt64, UInt64)", "tupleBlank": "(0,0)"}, - {"name": "AES._CTR", "nonceName": "Nonce", "tupleDefinition": "(UInt64, UInt32, UInt32)", "tupleBlank": "(0,0,0)"}] + )""", "tupleBlank": """( + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + )"""}, + {"name": "AES._CFB", "nonceName": "IV", "tupleDefinition": "(UInt64, UInt64)", "tupleBlank": "(0, 0)"}, + {"name": "AES._CTR", "nonceName": "Nonce", "tupleDefinition": "(UInt64, UInt32, UInt32)", "tupleBlank": "(0, 0, 0)"}] }% % for cipher in ciphers: %{ @@ -44,11 +47,12 @@ extension ${name} { public struct ${nonceName}: Sendable, ContiguousBytes, Sequence { typealias ${nonceName}Tuple = ${tupleDefinition} - private var bytes: ${nonceName}Tuple + var bytes: ${nonceName}Tuple + static var emptyBytes: ${nonceName}Tuple = ${tupleBlank} /// Creates a new random nonce. public init() { - var bytes = ${nonceName}Tuple${tupleBlank} + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { let count = MemoryLayout<${nonceName}Tuple>.size $0.initializeWithRandomBytes(count: count) @@ -69,7 +73,7 @@ extension ${name} { throw CryptoKitError.incorrectKeySize } - var bytes = ${nonceName}Tuple${tupleBlank} + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in bytesPtr.copyBytes(from: ${nonceName.lower()}Bytes) } @@ -89,7 +93,7 @@ extension ${name} { throw CryptoKitError.incorrectParameterSize } - var bytes = ${nonceName}Tuple${tupleBlank} + var bytes = Self.emptyBytes Swift.withUnsafeMutableBytes(of: &bytes) { bytesPtr in data.copyBytes(to: bytesPtr) }