Browse Source

CBF cleanup

Marcin Krzyżanowski 11 years ago
parent
commit
6863b59b62

+ 14 - 42
CryptoSwift/CipherBlockMode.swift

@@ -72,10 +72,6 @@ public enum CipherBlockMode {
 private struct CBCMode: BlockMode {
     var needIV:Bool = true
     
-    init() {
-        
-    }
-    
     func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
         precondition(blocks.count > 0)
         assert(iv != nil, "CFB require IV")
@@ -85,9 +81,9 @@ private struct CBCMode: BlockMode {
         
         
         var out:[UInt8]?
-        var prevCiphertext:[UInt8]? // for the first time prevCiphertext = iv
+        var prevCiphertext = iv! // for the first time prevCiphertext = iv
         for plaintext in blocks {
-            if let encrypted = cipherOperation(block: xor(prevCiphertext ?? iv!, plaintext)) {
+            if let encrypted = cipherOperation(block: xor(prevCiphertext, plaintext)) {
                 out = (out ?? [UInt8]()) + encrypted
                 prevCiphertext = encrypted
             }
@@ -103,10 +99,10 @@ private struct CBCMode: BlockMode {
         }
 
         var out:[UInt8]?
-        var prevCiphertext:[UInt8]? // for the first time prevCiphertext = iv
+        var prevCiphertext = iv! // for the first time prevCiphertext = iv
         for ciphertext in blocks {
             if let decrypted = cipherOperation(block: ciphertext) { // decrypt
-                out = (out ?? [UInt8]()) + xor(prevCiphertext ?? iv!, decrypted)
+                out = (out ?? [UInt8]()) + xor(prevCiphertext, decrypted)
             }
             prevCiphertext = ciphertext
         }
@@ -120,6 +116,7 @@ private struct CBCMode: BlockMode {
 */
 private struct CFBMode: BlockMode {
     var needIV:Bool = true
+    
     func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
         assert(iv != nil, "CFB require IV")
         if (iv == nil) {
@@ -127,21 +124,11 @@ private struct CFBMode: BlockMode {
         }
         
         var out:[UInt8]?
-        var lastCiphertext:[UInt8] = iv!
-        for (idx,plaintext) in enumerate(blocks) {
+        var lastCiphertext = iv!
+        for plaintext in blocks {
             if let encrypted = cipherOperation(block: lastCiphertext) {
-                var xoredPlaintext:[UInt8] = [UInt8](count: plaintext.count, repeatedValue: 0)
-                for i in 0..<plaintext.count {
-                    xoredPlaintext[i] = plaintext[i] ^ encrypted[i]
-                }
-                lastCiphertext = xoredPlaintext
-
-                
-                if (out == nil) {
-                    out = [UInt8]()
-                }
-                
-                out = out! + xoredPlaintext
+                lastCiphertext = xor(plaintext,encrypted)
+                out = (out ?? [UInt8]()) + lastCiphertext
             }
         }
         return out;
@@ -155,20 +142,10 @@ private struct CFBMode: BlockMode {
         
         var out:[UInt8]?
         var lastCiphertext:[UInt8] = iv!
-        for (idx,ciphertext) in enumerate(blocks) {
+        for ciphertext in blocks {
             if let decrypted = cipherOperation(block: lastCiphertext) {
-                var xored:[UInt8] = [UInt8](count: ciphertext.count, repeatedValue: 0)
-                for i in 0..<ciphertext.count {
-                    xored[i] = ciphertext[i] ^ decrypted[i]
-                }
-                lastCiphertext = xored
-                
-                
-                if (out == nil) {
-                    out = [UInt8]()
-                }
-                
-                out = out! + xored
+                lastCiphertext = xor(ciphertext, decrypted)
+                out = (out ?? [UInt8]()) + lastCiphertext
             }
         }
         return out;
@@ -184,14 +161,9 @@ private struct ECBMode: BlockMode {
     var needIV:Bool = false
     func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
         var out:[UInt8]?
-        for (idx,plaintext) in enumerate(blocks) {
+        for plaintext in blocks {
             if let encrypted = cipherOperation(block: plaintext) {
-                
-                if (out == nil) {
-                    out = [UInt8]()
-                }
-
-                out = out! + encrypted
+                out = (out ?? [UInt8]()) + encrypted
             }
         }
         return out

+ 116 - 10
CryptoSwift/Playground/CryptoPlayground.playground/section-1.swift

@@ -1,17 +1,123 @@
 // Playground - noun: a place where people can play
 
 import Foundation
+//import CryptoSwift
 
-protocol Proto {
-    
-}
+//Cipher.AES(key: [0x01], iv: [0x01], blockMode: .CBC)
+var arr = [1,2,3,4,5]
 
-struct S1: Proto {
-    
-}
+//arr.reduce(0, combine: { a,b in
+//    println("a \(a), b \(b)")
+//    return a + 1
+//})
 
-func create<M: Proto>(p:M) -> M {
-    return p()
-}
+let blocks:[[UInt8]] = [[1,2],[3,4],[5,6]]
+blocks.reduce([UInt8](), combine: { item1,item2 -> [UInt8] in
+    println("a \(item1), b \(item2)")
+    return item2
+})
 
-let qq = create(S1())
+
+//let arr2 = arr.map { num in
+//    return num + 10
+//}
+
+
+//typealias Byte = UInt8
+//
+//protocol GenericIntegerType: IntegerType {
+//    init(_ v: Int)
+//    init(_ v: UInt)
+//    init(_ v: Int8)
+//    init(_ v: UInt8)
+//    init(_ v: Int16)
+//    init(_ v: UInt16)
+//    init(_ v: Int32)
+//    init(_ v: UInt32)
+//    init(_ v: Int64)
+//    init(_ v: UInt64)
+//}
+//
+//protocol GenericSignedIntegerBitPattern {
+//    init(bitPattern: UIntMax)
+//    init(truncatingBitPattern: IntMax)
+//}
+//
+//protocol GenericUnsignedIntegerBitPattern {
+//    init(truncatingBitPattern: UIntMax)
+//}
+//
+//extension Int:GenericIntegerType, GenericSignedIntegerBitPattern  {
+//    init(bitPattern: UIntMax) {
+//        self.init(bitPattern: UInt(truncatingBitPattern: bitPattern))
+//    }
+//}
+//extension UInt:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
+//extension Int8:GenericIntegerType, GenericSignedIntegerBitPattern {
+//    init(bitPattern: UIntMax) {
+//        self.init(bitPattern: UInt8(truncatingBitPattern: bitPattern))
+//    }
+//}
+//extension UInt8:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
+//extension Int16:GenericIntegerType, GenericSignedIntegerBitPattern {
+//    init(bitPattern: UIntMax) {
+//        self.init(bitPattern: UInt16(truncatingBitPattern: bitPattern))
+//    }
+//}
+//extension UInt16:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
+//extension Int32:GenericIntegerType, GenericSignedIntegerBitPattern {
+//    init(bitPattern: UIntMax) {
+//        self.init(bitPattern: UInt32(truncatingBitPattern: bitPattern))
+//    }
+//}
+//extension UInt32:GenericIntegerType, GenericUnsignedIntegerBitPattern {}
+//extension Int64:GenericIntegerType, GenericSignedIntegerBitPattern {
+//    // init(bitPattern: UInt64) already defined
+//    
+//    init(truncatingBitPattern: IntMax) {
+//        self.init(truncatingBitPattern)
+//    }
+//}
+//extension UInt64:GenericIntegerType, GenericUnsignedIntegerBitPattern {
+//    // init(bitPattern: Int64) already defined
+//    
+//    init(truncatingBitPattern: UIntMax) {
+//        self.init(truncatingBitPattern)
+//    }
+//}
+//
+//func integerWithBytes<T: GenericIntegerType where T: UnsignedIntegerType, T: GenericUnsignedIntegerBitPattern>(bytes:[UInt8]) -> T? {
+//    if (bytes.count < sizeof(T)) {
+//        return nil
+//    }
+//
+//    let maxBytes = sizeof(T)
+//    var i:UIntMax = 0
+//    for (var j = 0; j < maxBytes; j++) {
+//        i = i | T(bytes[j]).toUIntMax() << UIntMax(j * 8)
+//    }
+//    return T(truncatingBitPattern: i)
+//}
+//
+//func integerWithBytes<T: GenericIntegerType where T: SignedIntegerType, T:  GenericSignedIntegerBitPattern>(bytes:[UInt8]) -> T? {
+//    if (bytes.count < sizeof(T)) {
+//        return nil
+//    }
+//    
+//    let maxBytes = sizeof(T)
+//    var i:IntMax = 0
+//    for (var j = 0; j < maxBytes; j++) {
+//        i = i | T(bitPattern: UIntMax(bytes[j].toUIntMax())).toIntMax() << (j * 8).toIntMax()
+//    }
+//    return T(truncatingBitPattern: i)
+//}
+//
+//let bytes:[UInt8] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
+//integerWithBytes(bytes) as Int8?
+//integerWithBytes(bytes) as UInt8?
+//integerWithBytes(bytes) as Int16?
+//integerWithBytes(bytes) as UInt16?
+//integerWithBytes(bytes) as Int32?
+//integerWithBytes(bytes) as UInt32?
+//integerWithBytes(bytes) as Int64?
+//integerWithBytes(bytes) as UInt64?