|
@@ -137,9 +137,9 @@ final public class AES {
|
|
/**
|
|
/**
|
|
Encrypt message. If padding is necessary, then PKCS7 padding is added and needs to be removed after decryption.
|
|
Encrypt message. If padding is necessary, then PKCS7 padding is added and needs to be removed after decryption.
|
|
|
|
|
|
- :param: message Plaintext data
|
|
|
|
|
|
+ - parameter message: Plaintext data
|
|
|
|
|
|
- :returns: Encrypted data
|
|
|
|
|
|
+ - returns: Encrypted data
|
|
*/
|
|
*/
|
|
|
|
|
|
public func encrypt(bytes:[UInt8], padding:Padding? = PKCS7()) -> [UInt8]? {
|
|
public func encrypt(bytes:[UInt8], padding:Padding? = PKCS7()) -> [UInt8]? {
|
|
@@ -161,8 +161,8 @@ final public class AES {
|
|
|
|
|
|
autoreleasepool { () -> () in
|
|
autoreleasepool { () -> () in
|
|
var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
|
|
var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
|
|
- for (i, row) in enumerate(state) {
|
|
|
|
- for (j, val) in enumerate(row) {
|
|
|
|
|
|
+ for (i, row) in state.enumerate() {
|
|
|
|
+ for (j, _) in row.enumerate() {
|
|
state[j][i] = block[i * row.count + j]
|
|
state[j][i] = block[i * row.count + j]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -215,15 +215,15 @@ final public class AES {
|
|
|
|
|
|
private func decryptBlock(block:[UInt8]) -> [UInt8]? {
|
|
private func decryptBlock(block:[UInt8]) -> [UInt8]? {
|
|
var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
|
|
var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
|
|
- for (i, row) in enumerate(state) {
|
|
|
|
- for (j, val) in enumerate(row) {
|
|
|
|
|
|
+ for (i, row) in state.enumerate() {
|
|
|
|
+ for (j, _) in row.enumerate() {
|
|
state[j][i] = block[i * row.count + j]
|
|
state[j][i] = block[i * row.count + j]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
state = addRoundKey(state,expandedKey, variant.Nr)
|
|
state = addRoundKey(state,expandedKey, variant.Nr)
|
|
|
|
|
|
- for roundCount in reverse(1..<variant.Nr) {
|
|
|
|
|
|
+ for roundCount in Array((1..<variant.Nr).reverse()) { //FIXME: WAT?
|
|
state = invShiftRows(state)
|
|
state = invShiftRows(state)
|
|
state = invSubBytes(state)
|
|
state = invSubBytes(state)
|
|
state = addRoundKey(state, expandedKey, roundCount)
|
|
state = addRoundKey(state, expandedKey, roundCount)
|
|
@@ -274,7 +274,7 @@ final public class AES {
|
|
tmp[wordIdx] = w[4*(i-1)+wordIdx]
|
|
tmp[wordIdx] = w[4*(i-1)+wordIdx]
|
|
}
|
|
}
|
|
if ((i % variant.Nk) == 0) {
|
|
if ((i % variant.Nk) == 0) {
|
|
- let rotWord = rotateLeft(UInt32.withBytes(tmp), 8).bytes(sizeof(UInt32)) // RotWord
|
|
|
|
|
|
+ let rotWord = rotateLeft(UInt32.withBytes(tmp), n: 8).bytes(sizeof(UInt32)) // RotWord
|
|
tmp = subWord(rotWord)
|
|
tmp = subWord(rotWord)
|
|
tmp[0] = tmp[0] ^ Rcon[i/variant.Nk]
|
|
tmp[0] = tmp[0] ^ Rcon[i/variant.Nk]
|
|
} else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
|
|
} else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
|
|
@@ -294,8 +294,8 @@ extension AES {
|
|
|
|
|
|
// byte substitution with table (S-box)
|
|
// byte substitution with table (S-box)
|
|
public func subBytes(inout state:[[UInt8]]) {
|
|
public func subBytes(inout state:[[UInt8]]) {
|
|
- for (i,row) in enumerate(state) {
|
|
|
|
- for (j,value) in enumerate(row) {
|
|
|
|
|
|
+ for (i,row) in state.enumerate() {
|
|
|
|
+ for (j,value) in row.enumerate() {
|
|
state[i][j] = AES.sBox[Int(value)]
|
|
state[i][j] = AES.sBox[Int(value)]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -303,8 +303,8 @@ extension AES {
|
|
|
|
|
|
public func invSubBytes(state:[[UInt8]]) -> [[UInt8]] {
|
|
public func invSubBytes(state:[[UInt8]]) -> [[UInt8]] {
|
|
var result = state
|
|
var result = state
|
|
- for (i,row) in enumerate(state) {
|
|
|
|
- for (j,value) in enumerate(row) {
|
|
|
|
|
|
+ for (i,row) in state.enumerate() {
|
|
|
|
+ for (j,value) in row.enumerate() {
|
|
result[i][j] = AES.invSBox[Int(value)]
|
|
result[i][j] = AES.invSBox[Int(value)]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -337,7 +337,7 @@ extension AES {
|
|
var a = a, b = b
|
|
var a = a, b = b
|
|
var p:UInt8 = 0, hbs:UInt8 = 0
|
|
var p:UInt8 = 0, hbs:UInt8 = 0
|
|
|
|
|
|
- for i in 0..<8 {
|
|
|
|
|
|
+ for _ in 0..<8 {
|
|
if (b & 1 == 1) {
|
|
if (b & 1 == 1) {
|
|
p ^= a
|
|
p ^= a
|
|
}
|
|
}
|
|
@@ -353,8 +353,8 @@ extension AES {
|
|
|
|
|
|
public func matrixMultiplyPolys(matrix:[[UInt8]], _ array:[UInt8]) -> [UInt8] {
|
|
public func matrixMultiplyPolys(matrix:[[UInt8]], _ array:[UInt8]) -> [UInt8] {
|
|
var returnArray:[UInt8] = [UInt8](count: array.count, repeatedValue: 0)
|
|
var returnArray:[UInt8] = [UInt8](count: array.count, repeatedValue: 0)
|
|
- for (i, row) in enumerate(matrix) {
|
|
|
|
- for (j, boxVal) in enumerate(row) {
|
|
|
|
|
|
+ for (i, row) in matrix.enumerate() {
|
|
|
|
+ for (j, boxVal) in row.enumerate() {
|
|
returnArray[i] = multiplyPolys(boxVal, array[j]) ^ returnArray[i]
|
|
returnArray[i] = multiplyPolys(boxVal, array[j]) ^ returnArray[i]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -377,7 +377,7 @@ extension AES {
|
|
// mixes data (independently of one another)
|
|
// mixes data (independently of one another)
|
|
public func mixColumns(state:[[UInt8]]) -> [[UInt8]] {
|
|
public func mixColumns(state:[[UInt8]]) -> [[UInt8]] {
|
|
var state = state
|
|
var state = state
|
|
- var colBox:[[UInt8]] = [[2,3,1,1],[1,2,3,1],[1,1,2,3],[3,1,1,2]]
|
|
|
|
|
|
+ let colBox:[[UInt8]] = [[2,3,1,1],[1,2,3,1],[1,1,2,3],[3,1,1,2]]
|
|
|
|
|
|
var rowMajorState = [[UInt8]](count: state.count, repeatedValue: [UInt8](count: state.first!.count, repeatedValue: 0)) //state.map({ val -> [UInt8] in return val.map { _ in return 0 } }) // zeroing
|
|
var rowMajorState = [[UInt8]](count: state.count, repeatedValue: [UInt8](count: state.first!.count, repeatedValue: 0)) //state.map({ val -> [UInt8] in return val.map { _ in return 0 } }) // zeroing
|
|
var newRowMajorState = rowMajorState
|
|
var newRowMajorState = rowMajorState
|
|
@@ -388,7 +388,7 @@ extension AES {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- for (i, row) in enumerate(rowMajorState) {
|
|
|
|
|
|
+ for (i, row) in rowMajorState.enumerate() {
|
|
newRowMajorState[i] = matrixMultiplyPolys(colBox, row)
|
|
newRowMajorState[i] = matrixMultiplyPolys(colBox, row)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -403,7 +403,7 @@ extension AES {
|
|
|
|
|
|
public func invMixColumns(state:[[UInt8]]) -> [[UInt8]] {
|
|
public func invMixColumns(state:[[UInt8]]) -> [[UInt8]] {
|
|
var state = state
|
|
var state = state
|
|
- var invColBox:[[UInt8]] = [[14,11,13,9],[9,14,11,13],[13,9,14,11],[11,13,9,14]]
|
|
|
|
|
|
+ let invColBox:[[UInt8]] = [[14,11,13,9],[9,14,11,13],[13,9,14,11],[11,13,9,14]]
|
|
|
|
|
|
var colOrderState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } }) // zeroing
|
|
var colOrderState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } }) // zeroing
|
|
|
|
|
|
@@ -415,7 +415,7 @@ extension AES {
|
|
|
|
|
|
var newState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } })
|
|
var newState = state.map({ val -> [UInt8] in return val.map { _ in return 0 } })
|
|
|
|
|
|
- for (i, row) in enumerate(colOrderState) {
|
|
|
|
|
|
+ for (i, row) in colOrderState.enumerate() {
|
|
newState[i] = matrixMultiplyPolys(invColBox, row)
|
|
newState[i] = matrixMultiplyPolys(invColBox, row)
|
|
}
|
|
}
|
|
|
|
|