Selaa lähdekoodia

Shuffle CipherMode protocols

Marcin Krzyzanowski 7 vuotta sitten
vanhempi
commit
2cc2d3e7db

+ 2 - 2
Sources/CryptoSwift/BlockDecryptor.swift

@@ -47,7 +47,7 @@ public class BlockDecryptor: Cryptor, Updatable {
         for var chunk in accumulatedWithoutSuffix.batched(by: blockSize) {
             if isLast || (accumulatedWithoutSuffix.count - processedBytesCount) >= blockSize {
 
-                if isLast, var finalizingWorker = worker as? BlockModeWorkerFinalizing {
+                if isLast, var finalizingWorker = worker as? FinalizingModeWorker {
                     chunk = try finalizingWorker.willDecryptLast(block: chunk + accumulated.suffix(worker.additionalBufferSize)) // tag size
                 }
 
@@ -55,7 +55,7 @@ public class BlockDecryptor: Cryptor, Updatable {
                     plaintext += worker.decrypt(block: chunk)
                 }
 
-                if var finalizingWorker = worker as? BlockModeWorkerFinalizing, isLast == true {
+                if var finalizingWorker = worker as? FinalizingModeWorker, isLast == true {
                     plaintext = try finalizingWorker.didDecryptLast(block: plaintext.slice)
                 }
 

+ 1 - 1
Sources/CryptoSwift/BlockEncryptor.swift

@@ -44,7 +44,7 @@ final class BlockEncryptor: Cryptor, Updatable {
         // Stream encrypts all, so it removes all elements
         accumulated.removeFirst(encrypted.count)
 
-        if var finalizingWorker = worker as? BlockModeWorkerFinalizing, isLast == true {
+        if var finalizingWorker = worker as? FinalizingModeWorker, isLast == true {
             encrypted = try finalizingWorker.finalize(encrypt: encrypted.slice)
         }
 

+ 16 - 9
Sources/CryptoSwift/BlockMode/CCM.swift

@@ -17,7 +17,7 @@
 // https://csrc.nist.gov/publications/detail/sp/800-38c/final
 
 
-public struct CCM: BlockMode {
+public struct CCM: StreamMode {
     public enum Error: Swift.Error {
         /// Invalid IV
         case invalidInitializationVector
@@ -30,9 +30,9 @@ public struct CCM: BlockMode {
     private let tagLength: Int
     private let messageLength: Int // total message length. need to know in advance
 
-    public init(nonce: Array<UInt8>, tagSize: Int, messageLength: Int, additionalAuthenticatedData: Array<UInt8>? = nil) {
+    public init(nonce: Array<UInt8>, tagLength: Int, messageLength: Int, additionalAuthenticatedData: Array<UInt8>? = nil) {
         self.nonce = nonce
-        self.tagLength = tagSize
+        self.tagLength = tagLength
         self.additionalAuthenticatedData = additionalAuthenticatedData
         self.messageLength = messageLength
     }
@@ -42,19 +42,21 @@ public struct CCM: BlockMode {
             throw Error.invalidInitializationVector
         }
 
-        return CCMModeWorker(blockSize: blockSize, nonce: nonce.slice, messageLength: messageLength, additionalAuthenticatedData: additionalAuthenticatedData, tagSize: tagLength, cipherOperation: cipherOperation)
+        return CCMModeWorker(blockSize: blockSize, nonce: nonce.slice, messageLength: messageLength, additionalAuthenticatedData: additionalAuthenticatedData, tagLength: tagLength, cipherOperation: cipherOperation)
     }
 }
 
-class CCMModeWorker: BlockModeWorkerFinalizing {
+class CCMModeWorker: StreamModeWorker, CounterModeWorker, FinalizingModeWorker {
+    typealias Counter = Int
+    var counter = 0
+
     let cipherOperation: CipherOperationOnBlock
     let blockSize: Int
     private let tagLength: Int
     private let messageLength: Int // total message length. need to know in advance
-    private var counter = 0
     private let q: UInt8
 
-    let additionalBufferSize: Int = 0
+    let additionalBufferSize: Int
     private let nonce: Array<UInt8>
     private var prev: ArraySlice<UInt8> = []
 
@@ -62,9 +64,10 @@ class CCMModeWorker: BlockModeWorkerFinalizing {
         case invalidParameter
     }
 
-    init(blockSize: Int, nonce: ArraySlice<UInt8>, messageLength: Int,  additionalAuthenticatedData: [UInt8]?, tagSize: Int, cipherOperation: @escaping CipherOperationOnBlock) {
+    init(blockSize: Int, nonce: ArraySlice<UInt8>, messageLength: Int,  additionalAuthenticatedData: [UInt8]?, tagLength: Int, cipherOperation: @escaping CipherOperationOnBlock) {
         self.blockSize = blockSize
-        self.tagLength = tagSize
+        self.tagLength = tagLength
+        self.additionalBufferSize = tagLength
         self.messageLength = messageLength
         self.cipherOperation = cipherOperation
         self.nonce = Array(nonce)
@@ -100,6 +103,10 @@ class CCMModeWorker: BlockModeWorkerFinalizing {
         return cipherOperation(ctr.slice)!
     }
 
+    func seek(to position: Int) throws {
+        self.counter = position
+    }
+
     func encrypt(block plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
         // y[i], where i is the counter
         guard let y = cipherOperation(xor(prev, plaintext)),

+ 1 - 2
Sources/CryptoSwift/BlockMode/CipherModeWorker.swift

@@ -37,8 +37,7 @@ public protocol StreamModeWorker: CipherModeWorker {
     mutating func seek(to position: Int) throws
 }
 
-// TODO: remove and merge with BlockModeWorker
-public protocol BlockModeWorkerFinalizing: BlockModeWorker {
+public protocol FinalizingModeWorker: CipherModeWorker {
     // Any final calculations, eg. calculate tag
     // Called after the last block is encrypted
     mutating func finalize(encrypt ciphertext: ArraySlice<UInt8>) throws -> Array<UInt8>

+ 1 - 1
Sources/CryptoSwift/BlockMode/GCM.swift

@@ -81,7 +81,7 @@ public final class GCM: BlockMode {
 
 // MARK: - Worker
 
-final class GCMModeWorker: BlockModeWorkerFinalizing {
+final class GCMModeWorker: BlockModeWorker, FinalizingModeWorker {
     let cipherOperation: CipherOperationOnBlock
 
     // Callback called when authenticationTag is ready