Browse Source

Convenience ciphers initializers for String key and iv

Marcin Krzyżanowski 10 years ago
parent
commit
4191a7ab5b
2 changed files with 43 additions and 21 deletions
  1. 33 21
      CryptoSwift/AES.swift
  2. 10 0
      CryptoSwift/ChaCha20.swift

+ 33 - 21
CryptoSwift/AES.swift

@@ -125,6 +125,15 @@ public class AES {
         self.init(key: key, iv: defaultIV, blockMode: blockMode)
     }
     
+    convenience public init?(key:String, iv:String, blockMode:CipherBlockMode = .CBC) {
+        if let kkey = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.bytes(), let iiv = iv.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.bytes() {
+            self.init(key: kkey, iv: iiv, blockMode: blockMode)
+        } else {
+            self.init(key: [UInt8](), iv: [UInt8](), blockMode: blockMode) //FIXME: this is due Swift bug, remove this line later, when fixed
+            return nil
+        }
+    }
+    
     /**
     Encrypt message. If padding is necessary, then PKCS7 padding is addedd and need to be removed after decryption.
     
@@ -149,34 +158,37 @@ public class AES {
     }
     
     private func encryptBlock(block:[UInt8]) -> [UInt8]? {
-        var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
-        for (i, row) in enumerate(state) {
-            for (j, val) in enumerate(row) {
-                state[j][i] = block[i * row.count + j]
-            }
-        }
-        
-        state = addRoundKey(state,expandedKey, 0)
+        var out:[UInt8] = [UInt8]()
         
-        for roundCount in 1..<variant.Nr {
+        autoreleasepool { () -> () in
+            var state:[[UInt8]] = [[UInt8]](count: variant.Nb, repeatedValue: [UInt8](count: variant.Nb, repeatedValue: 0))
+            for (i, row) in enumerate(state) {
+                for (j, val) in enumerate(row) {
+                    state[j][i] = block[i * row.count + j]
+                }
+            }
+            
+            state = addRoundKey(state,expandedKey, 0)
+            
+            for roundCount in 1..<variant.Nr {
+                subBytes(&state)
+                state = shiftRows(state)
+                state = mixColumns(state)
+                state = addRoundKey(state, expandedKey, roundCount)
+            }
+            
             subBytes(&state)
             state = shiftRows(state)
-            state = mixColumns(state)
-            state = addRoundKey(state, expandedKey, roundCount)
-        }
-        
-        subBytes(&state)
-        state = shiftRows(state)
-        state = addRoundKey(state, expandedKey, variant.Nr)
+            state = addRoundKey(state, expandedKey, variant.Nr)
 
 
-        var out = [UInt8](count: state.count * state.first!.count, repeatedValue: 0)
-        for i in 0..<state.count {
-            for j in 0..<state[i].count {
-                out[(i * 4) + j] = state[j][i]
+            out = [UInt8](count: state.count * state.first!.count, repeatedValue: 0)
+            for i in 0..<state.count {
+                for j in 0..<state[i].count {
+                    out[(i * 4) + j] = state[j][i]
+                }
             }
         }
-        
         return out
     }
     

+ 10 - 0
CryptoSwift/ChaCha20.swift

@@ -32,6 +32,16 @@ public class ChaCha20 {
         }
     }
     
+    convenience public init?(key:String, iv:String) {
+        if let kkey = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.bytes(), let iiv = iv.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)?.bytes() {
+            self.init(key: kkey, iv: iiv)
+        } else {
+            self.init(key: [UInt8](), iv: [UInt8]()) //FIXME: this is due Swift bug, remove this line later, when fixed
+            return nil
+        }
+    }
+
+    
     public func encrypt(bytes:[UInt8]) -> [UInt8]? {
         if (context == nil) {
             return nil