浏览代码

Add PCBC AES block mode

Marcin Krzyżanowski 9 年之前
父节点
当前提交
a2a9a2a29d
共有 3 个文件被更改,包括 72 次插入1 次删除
  1. 15 0
      CryptoSwiftTests/AESTests.swift
  2. 1 0
      README.md
  3. 56 1
      Sources/CryptoSwift/CipherBlockMode.swift

+ 15 - 0
CryptoSwiftTests/AESTests.swift

@@ -124,6 +124,21 @@ final class AESTests: XCTestCase {
         XCTAssertEqual(decrypted, plaintext, "decryption failed")
     }
 
+    func testAES_encrypt_pcbc256() {
+        let key: [UInt8] = [0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4]
+        let iv: [UInt8] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
+        let plaintext: [UInt8] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
+        let expected:[UInt8] = [0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6];
+
+        let aes = try! AES(key: key, iv:iv, blockMode: .PCBC)
+        XCTAssertTrue(aes.blockMode == .PCBC, "Invalid block mode")
+        let encrypted = try! aes.encrypt(plaintext, padding: nil)
+        print(encrypted.toHexString())
+        XCTAssertEqual(encrypted, expected, "encryption failed")
+        let decrypted = try! aes.decrypt(encrypted, padding: nil)
+        XCTAssertEqual(decrypted, plaintext, "decryption failed")
+    }
+
     func testAES_encrypt_ctr() {
         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]

+ 1 - 0
README.md

@@ -46,6 +46,7 @@ Good mood
 #####Cipher block mode
 - Electronic codebook ([ECB](http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_.28ECB.29))
 - Cipher-block chaining ([CBC](http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29))
+- Propagating Cipher Block Chaining ([PCBC](http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Propagating_Cipher_Block_Chaining_.28PCBC.29))
 - Cipher feedback ([CFB](http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_feedback_.28CFB.29))
 - Output Feedback ([OFB](http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Output_Feedback_.28OFB.29))
 - Counter ([CTR](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29))

+ 56 - 1
Sources/CryptoSwift/CipherBlockMode.swift

@@ -28,12 +28,14 @@ private protocol BlockMode {
 }
 
 public enum CipherBlockMode {
-    case ECB, CBC, CFB, OFB, CTR
+    case ECB, CBC, PCBC, CFB, OFB, CTR
     
     private var mode:BlockMode {
         switch (self) {
         case CBC:
             return CBCMode()
+        case PCBC:
+            return PCBCMode()
         case CFB:
             return CFBMode()
         case OFB:
@@ -122,6 +124,59 @@ private struct CBCMode: BlockMode {
     }
 }
 
+/**
+ Propagating Cipher Block Chaining (PCBC)
+ */
+private struct PCBCMode: BlockMode {
+    let options: BlockModeOptions = [.InitializationVectorRequired, .PaddingRequired]
+
+    func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) throws -> [UInt8] {
+        precondition(blocks.count > 0)
+        guard let iv = iv else {
+            throw BlockError.MissingInitializationVector
+        }
+
+        var out:[UInt8] = [UInt8]()
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
+        var prevCiphertext = iv // for the first time prevCiphertext = iv
+        for plaintext in blocks {
+            guard let encrypted = cipherOperation(block: xor(prevCiphertext, plaintext)) else {
+                out.appendContentsOf(plaintext)
+                continue
+            }
+
+            let ciphertext = encrypted
+            out.appendContentsOf(ciphertext)
+
+            prevCiphertext = xor(plaintext, ciphertext)
+        }
+        return out
+    }
+
+    func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) throws -> [UInt8] {
+        precondition(blocks.count > 0)
+        guard let iv = iv else {
+            throw BlockError.MissingInitializationVector
+        }
+
+        var out:[UInt8] = [UInt8]()
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
+        var prevCiphertext = iv // for the first time prevCiphertext = iv
+        for ciphertext in blocks {
+            guard let decrypted = cipherOperation(block: ciphertext) else {
+                out.appendContentsOf(ciphertext)
+                continue
+            }
+
+            let plaintext = xor(prevCiphertext, decrypted)
+            out.appendContentsOf(plaintext)
+            prevCiphertext = xor(plaintext, ciphertext)
+        }
+
+        return out
+    }
+}
+
 /**
 Cipher feedback (CFB)
 */