Skip to content

Commit

Permalink
Formatting and restoring Block constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
maschall committed Oct 1, 2024
1 parent 519f411 commit 55eaa0f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Sources/_CryptoExtras/AES/AES_CBC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions Sources/_CryptoExtras/AES/Block Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ extension AES {
self.blockBytes = blockBytes
}

init(_ iv: AES._CBC.IV) {
self.blockBytes = iv.bytes
}

init<BlockBytes: Sequence>(blockBytes: BlockBytes) where BlockBytes.Element == UInt8 {
let blockBytes: [UInt8] = Array(blockBytes)
self.init(blockBytes: blockBytes)
Expand Down
30 changes: 18 additions & 12 deletions Sources/_CryptoExtras/AES/Nonces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<IVTuple>.size
$0.initializeWithRandomBytes(count: count)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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<IVTuple>.size
$0.initializeWithRandomBytes(count: count)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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<NonceTuple>.size
$0.initializeWithRandomBytes(count: count)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
18 changes: 11 additions & 7 deletions Sources/_CryptoExtras/AES/Nonces.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
%{
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 55eaa0f

Please sign in to comment.