瀏覽代碼

Fix ChaCha20 counter

Marcin Krzyżanowski 8 年之前
父節點
當前提交
8a2e26873b
共有 2 個文件被更改,包括 11 次插入13 次删除
  1. 3 6
      Sources/CryptoSwift/ChaCha20.swift
  2. 8 7
      Tests/CryptoSwiftTests/ChaCha20Tests.swift

+ 3 - 6
Sources/CryptoSwift/ChaCha20.swift

@@ -200,12 +200,11 @@ public final class ChaCha20: BlockCipher {
     }
 
     // XORKeyStream
-    func process(bytes: Array<UInt8>, counter: Array<UInt8>, key: Array<UInt8>) -> (Array<UInt8>, Array<UInt8>) {
+    func process(bytes: Array<UInt8>, counter: inout Array<UInt8>, key: Array<UInt8>) -> Array<UInt8> {
         precondition(counter.count == 16)
         precondition(key.count == 32)
 
         var block = Array<UInt8>(repeating: 0, count: ChaCha20.blockSize)
-        var counter = counter
         var bytes = bytes //TODO: check bytes[bytes.indices]
         var out = Array<UInt8>.init(reserveCapacity: bytes.count)
 
@@ -229,7 +228,7 @@ public final class ChaCha20: BlockCipher {
                 out.append(v ^ block[i])
             }
         }
-        return (out, counter)
+        return out
     }
 }
 
@@ -237,9 +236,7 @@ public final class ChaCha20: BlockCipher {
 extension ChaCha20: Cipher {
 
     public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
-        let (result, newCounter) = process(bytes: Array(bytes), counter: self.counter, key: Array(self.key))
-        self.counter = newCounter
-        return result
+        return process(bytes: Array(bytes), counter: &self.counter, key: Array(self.key))
     }
 
     public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {

+ 8 - 7
Tests/CryptoSwiftTests/ChaCha20Tests.swift

@@ -57,11 +57,11 @@ final class ChaCha20Tests: XCTestCase {
 
     func testCore() {
         let key: Array<UInt8> = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
-        let counter: Array<UInt8> = [1,0,0,0,0,0,0,9,0,0,0,74,0,0,0,0]
+        var counter: Array<UInt8> = [1,0,0,0,0,0,0,9,0,0,0,74,0,0,0,0]
         let input = Array<UInt8>.init(repeating: 0, count: 129)
         let chacha = try! ChaCha20(key: key, iv: Array(key[4..<16]))
-        let result = chacha.process(bytes: input, counter: counter, key: key)
-        XCTAssertEqual(result.0.toHexString(), "10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e0a88837739d7bf4ef8ccacb0ea2bb9d69d56c394aa351dfda5bf459f0a2e9fe8e721f89255f9c486bf21679c683d4f9c5cf2fa27865526005b06ca374c86af3bdc")
+        let result = chacha.process(bytes: input, counter: &counter, key: key)
+        XCTAssertEqual(result.toHexString(), "10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e0a88837739d7bf4ef8ccacb0ea2bb9d69d56c394aa351dfda5bf459f0a2e9fe8e721f89255f9c486bf21679c683d4f9c5cf2fa27865526005b06ca374c86af3bdc")
     }
 
     func testVector1Py() {
@@ -106,9 +106,9 @@ final class ChaCha20Tests: XCTestCase {
     extension ChaCha20Tests {
 
         func testChaCha20Performance() {
-            let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
-            let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
-            let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
+            let key: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F]
+            let iv: Array<UInt8> = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
+            let message = Array<UInt8>(repeating: 7, count: 1024)
             measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, for: { () -> Void in
                 do {
                     let _ = try ChaCha20(key: key, iv: iv).encrypt(message)
@@ -126,8 +126,9 @@ extension ChaCha20Tests {
     static func allTests() -> [(String, (ChaCha20Tests) -> () -> Void)] {
         var tests = [
             ("testChaCha20", testChaCha20),
+            ("testCore", testCore),
             ("testVector1Py", testVector1Py),
-//            ("testChaCha20EncryptPartial", testChaCha20EncryptPartial),
+            ("testChaCha20EncryptPartial", testChaCha20EncryptPartial),
         ]
 
         #if !CI