Selaa lähdekoodia

Add OFB AES block mode

Marcin Krzyżanowski 9 vuotta sitten
vanhempi
commit
ac29d752d8

+ 30 - 2
CryptoSwiftTests/AESTests.swift

@@ -85,7 +85,7 @@ final class AESTests: XCTestCase {
         let decrypted = try! aes.decrypt(encrypted, padding: nil)
         XCTAssertEqual(decrypted, plaintext, "decryption failed")
     }
-    
+
     // https://github.com/krzyzanowskim/CryptoSwift/issues/142
     func testAES_encrypt_cfb_long() {
         let key: [UInt8] = [56, 118, 37, 51, 125, 78, 103, 107, 119, 40, 74, 88, 117, 112, 123, 75, 122, 89, 72, 36, 46, 91, 106, 60, 54, 110, 34, 126, 69, 126, 61, 87]
@@ -95,7 +95,35 @@ final class AESTests: XCTestCase {
         let decrypted: [UInt8] = try! AES(key: key, iv: iv, blockMode: .CFB).decrypt(encrypted)
         XCTAssert(decrypted == plaintext, "decryption failed")
     }
-    
+
+    func testAES_encrypt_ofb128() {
+        let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
+        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] = [0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a];
+
+        let aes = try! AES(key: key, iv:iv, blockMode: .OFB)
+        XCTAssertTrue(aes.blockMode == .OFB, "Invalid block mode")
+        let encrypted = try! aes.encrypt(plaintext, padding: nil)
+        XCTAssertEqual(encrypted, expected, "encryption failed")
+        let decrypted = try! aes.decrypt(encrypted, padding: nil)
+        XCTAssertEqual(decrypted, plaintext, "decryption failed")
+    }
+
+    func testAES_encrypt_ofb256() {
+        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] = [0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60];
+
+        let aes = try! AES(key: key, iv:iv, blockMode: .OFB)
+        XCTAssertTrue(aes.blockMode == .OFB, "Invalid block mode")
+        let encrypted = try! aes.encrypt(plaintext, padding: nil)
+        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

@@ -47,6 +47,7 @@ Good mood
 - 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))
 - 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))
 
 #####Data padding

+ 2 - 2
Sources/CryptoSwift/AES.swift

@@ -198,8 +198,8 @@ final public class AES {
         let blocks = bytes.chunks(AES.blockSize)
         let out:[UInt8]
         switch (blockMode) {
-        case .CFB, .CTR:
-            // CFB, CTR uses encryptBlock to decrypt
+        case .CFB, .OFB, .CTR:
+            // CFB, OFB, CTR uses encryptBlock to decrypt
             out = try blockMode.decryptBlocks(blocks, iv: self.iv, cipherOperation: encryptBlock)
         default:
             out = try blockMode.decryptBlocks(blocks, iv: self.iv, cipherOperation: decryptBlock)

+ 55 - 2
Sources/CryptoSwift/CipherBlockMode.swift

@@ -28,7 +28,7 @@ private protocol BlockMode {
 }
 
 public enum CipherBlockMode {
-    case ECB, CBC, CFB, CTR
+    case ECB, CBC, CFB, OFB, CTR
     
     private var mode:BlockMode {
         switch (self) {
@@ -36,6 +36,8 @@ public enum CipherBlockMode {
             return CBCMode()
         case CFB:
             return CFBMode()
+        case OFB:
+            return OFBMode()
         case ECB:
             return ECBMode()
         case CTR:
@@ -111,7 +113,7 @@ private struct CBCMode: BlockMode {
         var prevCiphertext = iv // for the first time prevCiphertext = iv
         for ciphertext in blocks {
             if let decrypted = cipherOperation(block: ciphertext) { // decrypt
-                out.appendContentsOf(xor(prevCiphertext, decrypted)) //FIXME: b:
+                out.appendContentsOf(xor(prevCiphertext, decrypted))
             }
             prevCiphertext = ciphertext
         }
@@ -164,6 +166,57 @@ private struct CFBMode: BlockMode {
     }
 }
 
+/**
+ Output Feedback (OFB)
+ */
+private struct OFBMode: BlockMode {
+    let options: BlockModeOptions = [.InitializationVectorRequired]
+
+    func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) throws -> [UInt8] {
+        guard let iv = iv else {
+            throw BlockError.MissingInitializationVector
+        }
+
+        var out:[UInt8] = [UInt8]()
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
+
+        var lastEncryptedBlock = iv
+        for plaintext in blocks {
+            guard let ciphertext = cipherOperation(block: lastEncryptedBlock) else {
+                out.appendContentsOf(plaintext)
+                continue
+            }
+
+            lastEncryptedBlock = ciphertext
+            out.appendContentsOf(xor(plaintext, ciphertext))
+        }
+        return out
+    }
+
+    func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) throws -> [UInt8] {
+        guard let iv = iv else {
+            throw BlockError.MissingInitializationVector
+        }
+
+        var out:[UInt8] = [UInt8]()
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
+
+        var lastDecryptedBlock = iv
+        for ciphertext in blocks {
+            guard let decrypted = cipherOperation(block: lastDecryptedBlock) else {
+                out.appendContentsOf(ciphertext)
+                continue
+            }
+
+            lastDecryptedBlock = decrypted
+
+            let plaintext = xor(decrypted, ciphertext)
+            out.appendContentsOf(plaintext)
+        }
+
+        return out
+    }
+}
 
 /**
 Electronic codebook (ECB)