Bläddra i källkod

Merge pull request #897 from jimmiejohansson/pcbc_decrypt_issue_896

Use result for iv-propagation in PCBC. Fixes #896
Marcin Krzyzanowski 3 år sedan
förälder
incheckning
19d021f49b
2 ändrade filer med 14 tillägg och 1 borttagningar
  1. 1 1
      Sources/CryptoSwift/BlockMode/PCBC.swift
  2. 13 0
      Tests/CryptoSwiftTests/AESTests.swift

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

@@ -68,7 +68,7 @@ struct PCBCModeWorker: BlockModeWorker {
       return Array(ciphertext)
     }
     let result: Array<UInt8> = xor(prev ?? self.iv, plaintext)
-    self.prev = xor(plaintext.slice, ciphertext)
+    self.prev = xor(result, ciphertext)
     return result
   }
 }

+ 13 - 0
Tests/CryptoSwiftTests/AESTests.swift

@@ -216,6 +216,19 @@ final class AESTests: XCTestCase {
     XCTAssertEqual(decrypted, plaintext, "decryption failed")
   }
 
+  func testAESEncryptBigPCBC128() {
+    let key = "0123456789abcdef".bytes
+    let iv = "fedcba9876543210".bytes
+    let plaintext = "64 byte plaintext that will split into 4 chunks of 16 bytes each".bytes
+    let ciphertext: Array<UInt8> = [0xd6, 0x83, 0x7b, 0xb8, 0xfe, 0x1d, 0x62, 0xf7, 0x04, 0x69, 0xd1, 0xfd, 0x47, 0x06, 0x9c, 0x3d, 0xc0, 0x7c, 0xfe, 0xc9, 0x3d, 0xba, 0x35, 0x61, 0x40, 0xef, 0xe2, 0xac, 0xc6, 0x4c, 0x3d, 0x04, 0xbf, 0x4c, 0xa4, 0xf6, 0xfc, 0x09, 0xfc, 0x8c, 0x2e, 0x09, 0xd0, 0x74, 0x66, 0x2b, 0x8f, 0x02, 0x54, 0x01, 0x25, 0x76, 0x20, 0x88, 0x5e, 0x19, 0x3f, 0x74, 0xcd, 0x48, 0x29, 0xc7, 0xe1, 0xc6, 0xfb, 0xc9, 0xb9, 0xcf, 0xcd, 0xf8, 0xeb, 0x42, 0xbc, 0x0f, 0xc5, 0x73, 0x96, 0xe4, 0xf8, 0x0f]
+
+    let aes = try! AES(key: key, blockMode: PCBC(iv: iv), padding: .pkcs7)
+    let encrypted = try! aes.encrypt(plaintext)
+    XCTAssertEqual(encrypted, ciphertext, "encryption failed")
+    let decrypted = try! aes.decrypt(ciphertext)
+    XCTAssertEqual(decrypted, plaintext, "decryption failed")
+  }
+
   func testAESEncryptCTR() {
     let key: Array<UInt8> = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c]
     let iv: Array<UInt8> = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]