Bläddra i källkod

Block mode using input ArraySlice output Array

Marcin Krzyżanowski 8 år sedan
förälder
incheckning
1891a6613d

+ 3 - 3
Sources/CryptoSwift/AES.swift

@@ -433,8 +433,8 @@ extension AES {
             var processedBytes = 0
             var encrypted = Array<UInt8>()
             encrypted.reserveCapacity(self.accumulated.count)
-            for chunk in self.accumulated.chunks(size: AES.blockSize) {
-                if (isLast || (self.accumulated.count - processedBytes) >= AES.blockSize) {
+            for chunk in self.accumulated.batched(by: AES.blockSize) {
+                if isLast || (self.accumulated.count - processedBytes) >= AES.blockSize {
                     encrypted += worker.encrypt(chunk)
                     processedBytes += chunk.count
                 }
@@ -488,7 +488,7 @@ extension AES {
             plaintext.reserveCapacity(self.accumulated.count)
             for chunk in self.accumulated.batched(by: AES.blockSize) {
                 if (isLast || (self.accumulated.count - processedBytes) >= AES.blockSize) {
-                    plaintext += self.worker.decrypt(Array(chunk))
+                    plaintext += self.worker.decrypt(chunk)
 
                     // remove "offset" from the beginning of first chunk
                     if self.offsetToRemove > 0 {

+ 2 - 2
Sources/CryptoSwift/BlockMode/BlockModeWorker.swift

@@ -8,6 +8,6 @@
 
 protocol BlockModeWorker {
     var cipherOperation: CipherOperationOnBlock { get }
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8>
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8>
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8>
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8>
 }

+ 6 - 6
Sources/CryptoSwift/BlockMode/CBC.swift

@@ -20,20 +20,20 @@ struct CBCModeWorker: BlockModeWorker {
         self.cipherOperation = cipherOperation
     }
 
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
         guard let ciphertext = cipherOperation(xor(prev ?? iv, plaintext)) else {
-            return plaintext
+            return Array(plaintext)
         }
         prev = ciphertext
         return ciphertext
     }
 
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
-        guard let plaintext = cipherOperation(ciphertext) else {
-            return ciphertext
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
+        guard let plaintext = cipherOperation(Array(ciphertext)) else {
+            return Array(ciphertext)
         }
         let result = xor(prev ?? iv, plaintext)
-        self.prev = ciphertext
+        self.prev = Array(ciphertext)
         return result
     }
 }

+ 5 - 5
Sources/CryptoSwift/BlockMode/CFB.swift

@@ -20,20 +20,20 @@ struct CFBModeWorker: BlockModeWorker {
         self.cipherOperation = cipherOperation
     }
 
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
         guard let ciphertext = cipherOperation(prev ?? iv) else {
-            return plaintext
+            return Array(plaintext)
         }
         prev = xor(plaintext, ciphertext)
         return prev ?? []
     }
 
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
         guard let plaintext = cipherOperation(prev ?? iv) else {
-            return ciphertext
+            return Array(ciphertext)
         }
         let result = xor(plaintext, ciphertext)
-        self.prev = ciphertext
+        self.prev = Array(ciphertext)
         return result
     }
 }

+ 3 - 3
Sources/CryptoSwift/BlockMode/CTR.swift

@@ -20,18 +20,18 @@ struct CTRModeWorker: RandomAccessBlockModeWorker {
         self.cipherOperation = cipherOperation
     }
 
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
         let nonce = buildNonce(iv, counter: UInt64(counter))
         counter = counter + 1
 
         guard let ciphertext = cipherOperation(nonce) else {
-            return plaintext
+            return Array(plaintext)
         }
 
         return xor(plaintext, ciphertext)
     }
 
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
         return encrypt(ciphertext)
     }
 }

+ 4 - 4
Sources/CryptoSwift/BlockMode/ECB.swift

@@ -16,14 +16,14 @@ struct ECBModeWorker: BlockModeWorker {
         self.cipherOperation = cipherOperation
     }
 
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
-        guard let ciphertext = cipherOperation(plaintext) else {
-            return plaintext
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
+        guard let ciphertext = cipherOperation(Array(plaintext)) else {
+            return Array(plaintext)
         }
         return ciphertext
     }
 
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
         return encrypt(ciphertext)
     }
 }

+ 4 - 4
Sources/CryptoSwift/BlockMode/OFB.swift

@@ -20,17 +20,17 @@ struct OFBModeWorker: BlockModeWorker {
         self.cipherOperation = cipherOperation
     }
 
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
         guard let ciphertext = cipherOperation(prev ?? iv) else {
-            return plaintext
+            return Array(plaintext)
         }
         prev = ciphertext
         return xor(plaintext, ciphertext)
     }
 
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
         guard let decrypted = cipherOperation(prev ?? iv) else {
-            return ciphertext
+            return Array(ciphertext)
         }
         let plaintext = xor(decrypted, ciphertext)
         self.prev = decrypted

+ 5 - 5
Sources/CryptoSwift/BlockMode/PCBC.swift

@@ -20,17 +20,17 @@ struct PCBCModeWorker: BlockModeWorker {
         self.cipherOperation = cipherOperation
     }
 
-    mutating func encrypt(_ plaintext: Array<UInt8>) -> Array<UInt8> {
+    mutating func encrypt(_ plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
         guard let ciphertext = cipherOperation(xor(prev ?? iv, plaintext)) else {
-            return plaintext
+            return Array(plaintext)
         }
         prev = xor(plaintext, ciphertext)
         return ciphertext
     }
 
-    mutating func decrypt(_ ciphertext: Array<UInt8>) -> Array<UInt8> {
-        guard let plaintext = cipherOperation(ciphertext) else {
-            return ciphertext
+    mutating func decrypt(_ ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
+        guard let plaintext = cipherOperation(Array(ciphertext)) else {
+            return Array(ciphertext)
         }
         let result = xor(prev ?? iv, plaintext)
         self.prev = xor(plaintext, ciphertext)

+ 2 - 2
Sources/CryptoSwift/Blowfish.swift

@@ -468,7 +468,7 @@ extension Blowfish: Cipher {
         out.reserveCapacity(bytes.count)
 
         for chunk in bytes.batched(by: Blowfish.blockSize) {
-            out += self.encryptWorker.encrypt(Array(chunk))
+            out += self.encryptWorker.encrypt(chunk)
         }
 
         if blockMode.options.contains(.PaddingRequired) && (out.count % Blowfish.blockSize != 0) {
@@ -493,7 +493,7 @@ extension Blowfish: Cipher {
         out.reserveCapacity(bytes.count)
 
         for chunk in Array(bytes).batched(by: Blowfish.blockSize) {
-            out += self.decryptWorker.decrypt(Array(chunk)) //FIXME: copying here is innefective
+            out += self.decryptWorker.decrypt(chunk) //FIXME: copying here is innefective
         }
 
         out = padding.remove(from: out, blockSize: Blowfish.blockSize)

+ 14 - 12
Sources/CryptoSwift/Collection+Extension.swift

@@ -9,14 +9,15 @@
 extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
 
     func toUInt32Array() -> Array<UInt32> {
+        let count = self.count
         var result = Array<UInt32>()
         result.reserveCapacity(16)
         for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt32>.size) {
             var val: UInt32 = 0
-            val |= self.count > 3 ? UInt32(self[idx.advanced(by: 3)]) << 24 : 0
-            val |= self.count > 2 ? UInt32(self[idx.advanced(by: 2)]) << 16 : 0
-            val |= self.count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8 : 0
-            val |= self.count > 0 ? UInt32(self[idx]) : 0
+            val |= count > 3 ? UInt32(self[idx.advanced(by: 3)]) << 24 : 0
+            val |= count > 2 ? UInt32(self[idx.advanced(by: 2)]) << 16 : 0
+            val |= count > 1 ? UInt32(self[idx.advanced(by: 1)]) << 8 : 0
+            val |= count > 0 ? UInt32(self[idx]) : 0
             result.append(val)
         }
 
@@ -24,18 +25,19 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
     }
 
     func toUInt64Array() -> Array<UInt64> {
+        let count = self.count
         var result = Array<UInt64>()
         result.reserveCapacity(32)
         for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt64>.size) {
             var val: UInt64 = 0
-            val |= self.count > 7 ? UInt64(self[idx.advanced(by: 7)]) << 56 : 0
-            val |= self.count > 6 ? UInt64(self[idx.advanced(by: 6)]) << 48 : 0
-            val |= self.count > 5 ? UInt64(self[idx.advanced(by: 5)]) << 40 : 0
-            val |= self.count > 4 ? UInt64(self[idx.advanced(by: 4)]) << 32 : 0
-            val |= self.count > 3 ? UInt64(self[idx.advanced(by: 3)]) << 24 : 0
-            val |= self.count > 2 ? UInt64(self[idx.advanced(by: 2)]) << 16 : 0
-            val |= self.count > 1 ? UInt64(self[idx.advanced(by: 1)]) << 8 : 0
-            val |= self.count > 0 ? UInt64(self[idx.advanced(by: 0)]) << 0 : 0
+            val |= count > 7 ? UInt64(self[idx.advanced(by: 7)]) << 56 : 0
+            val |= count > 6 ? UInt64(self[idx.advanced(by: 6)]) << 48 : 0
+            val |= count > 5 ? UInt64(self[idx.advanced(by: 5)]) << 40 : 0
+            val |= count > 4 ? UInt64(self[idx.advanced(by: 4)]) << 32 : 0
+            val |= count > 3 ? UInt64(self[idx.advanced(by: 3)]) << 24 : 0
+            val |= count > 2 ? UInt64(self[idx.advanced(by: 2)]) << 16 : 0
+            val |= count > 1 ? UInt64(self[idx.advanced(by: 1)]) << 8 : 0
+            val |= count > 0 ? UInt64(self[idx.advanced(by: 0)]) << 0 : 0
             result.append(val)
         }
 

+ 14 - 1
Sources/CryptoSwift/Utils.swift

@@ -53,13 +53,26 @@ func reversed(_ uint32: UInt32) -> UInt32 {
 }
 
 func xor(_ a: Array<UInt8>, _ b: Array<UInt8>) -> Array<UInt8> {
+    return xor(a.suffix(from: a.startIndex), b.suffix(from: b.startIndex))
+}
+
+func xor(_ a: Array<UInt8>, _ b: ArraySlice<UInt8>) -> Array<UInt8> {
+    return xor(a.suffix(from: a.startIndex), b.suffix(from: b.startIndex))
+}
+
+func xor(_ a: ArraySlice<UInt8>, _ b: Array<UInt8>) -> Array<UInt8> {
+    return xor(a.suffix(from: a.startIndex), b.suffix(from: b.startIndex))
+}
+
+func xor(_ a: ArraySlice<UInt8>, _ b: ArraySlice<UInt8>) -> Array<UInt8> {
     var xored = Array<UInt8>(repeating: 0, count: min(a.count, b.count))
     for i in 0 ..< xored.count {
-        xored[i] = a[i] ^ b[i]
+        xored[xored.startIndex.advanced(by: i)] = a[a.startIndex.advanced(by: i)] ^ b[b.startIndex.advanced(by: i)]
     }
     return xored
 }
 
+
 /**
  ISO/IEC 9797-1 Padding method 2.
  Add a single bit with value 1 to the end of the data.