Quellcode durchsuchen

Fix CTR mode nonce generation + update CTR tests.

Marcin Krzyżanowski vor 9 Jahren
Ursprung
Commit
b2b36d879b
2 geänderte Dateien mit 8 neuen und 10 gelöschten Zeilen
  1. 6 8
      CryptoSwift/CipherBlockMode.swift
  2. 2 2
      CryptoSwiftTests/AESTests.swift

+ 6 - 8
CryptoSwift/CipherBlockMode.swift

@@ -196,11 +196,9 @@ private struct CTRMode: BlockMode {
     private func buildNonce(iv: [UInt8], counter: UInt64) -> [UInt8] {
         let noncePartLen = AES.blockSize / 2
         let noncePrefix = Array(iv[0..<noncePartLen])
-        let nonceSuffix = arrayOfBytes(counter, length: noncePartLen)
-        
-        var nonce = noncePrefix
-        nonce += nonceSuffix
-        return nonce
+        let nonceSuffix = Array(iv[noncePartLen..<iv.count])
+        let c = UInt64.withBytes(nonceSuffix) + counter
+        return noncePrefix + arrayOfBytes(c)
     }
     
     func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) throws -> [UInt8] {
@@ -230,10 +228,10 @@ private struct CTRMode: BlockMode {
         var counter:UInt = 0
         var out = [UInt8]()
         out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
-        for plaintext in blocks {
+        for ciphertext in blocks {
             let nonce = buildNonce(iv, counter: UInt64(counter++))
-            if let encrypted = cipherOperation(block: nonce) {
-                out.appendContentsOf(xor(encrypted, plaintext))
+            if let decrypted = cipherOperation(block: nonce) {
+                out.appendContentsOf(xor(decrypted, ciphertext))
             }
         }
         return out

+ 2 - 2
CryptoSwiftTests/AESTests.swift

@@ -100,7 +100,7 @@ final class AESTests: XCTestCase {
         let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:[UInt8] = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
         let plaintext:[UInt8] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
-        let expected:[UInt8] = [103, 238, 5, 84, 116, 153, 248, 188, 240, 195, 131, 36, 232, 96, 92, 40]
+        let expected:[UInt8] = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce]
         
         let aes = try! AES(key: key, iv:iv, blockMode: .CTR)
         XCTAssertTrue(aes.blockMode == .CTR, "Invalid block mode")
@@ -114,7 +114,7 @@ final class AESTests: XCTestCase {
         let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:[UInt8] = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
         let plaintext:[UInt8] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01]
-        let expected:[UInt8] = [103, 238, 5, 84, 116, 153, 248, 188, 240, 195, 131, 36, 232, 96, 92, 40, 174]
+        let expected:[UInt8] = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,0x37]
 
         let aes = try! AES(key: key, iv:iv, blockMode: .CTR)
         XCTAssertTrue(aes.blockMode == .CTR, "Invalid block mode")