浏览代码

Fix decrypt for CBC in case IV is longer than block size. #32

Marcin Krzyżanowski 10 年之前
父节点
当前提交
a74fde67a5
共有 1 个文件被更改,包括 6 次插入8 次删除
  1. 6 8
      CryptoSwift/CipherBlockMode.swift

+ 6 - 8
CryptoSwift/CipherBlockMode.swift

@@ -73,10 +73,9 @@ public enum CipherBlockMode {
 */
 private struct CBCMode {
     static func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
-        
+        assert(iv != nil, "CFB require IV")
         if (iv == nil) {
-            assertionFailure("CBC require IV")
-            return nil
+            return nil;
         }
         
         var out:[UInt8]?
@@ -104,8 +103,8 @@ private struct CBCMode {
     }
     
     static func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
+        assert(iv != nil, "CFB require IV")
         if (iv == nil) {
-            assertionFailure("CBC require IV")
             return nil
         }
 
@@ -114,7 +113,7 @@ private struct CBCMode {
         for (idx,ciphertext) in enumerate(blocks) {
             if let decrypted = cipher(block: ciphertext) { // decrypt
                 
-                var xored:[UInt8] = [UInt8](count: lastCiphertext.count, repeatedValue: 0)
+                var xored:[UInt8] = [UInt8](count: ciphertext.count, repeatedValue: 0)
                 for i in 0..<ciphertext.count {
                     xored[i] = lastCiphertext[i] ^ decrypted[i]
                 }
@@ -136,9 +135,8 @@ private struct CBCMode {
 */
 private struct CFBMode {
     static func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
-        
+        assert(iv != nil, "CFB require IV")
         if (iv == nil) {
-            assertionFailure("CFB require IV")
             return nil
         }
         
@@ -164,8 +162,8 @@ private struct CFBMode {
     }
     
     static func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipher:CipherWorker) -> [UInt8]? {
+        assert(iv != nil, "CFB require IV")
         if (iv == nil) {
-            assertionFailure("CFB require IV")
             return nil
         }