浏览代码

ChaCha20 is working. Utils functions moved to Utils.

Marcin Krzyżanowski 11 年之前
父节点
当前提交
ea7c40e050

+ 8 - 0
CryptoSwift.xcodeproj/project.pbxproj

@@ -8,7 +8,9 @@
 
 
 /* Begin PBXBuildFile section */
 /* Begin PBXBuildFile section */
 		750A54601992D2680017DA75 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750A545F1992D2680017DA75 /* MD5.swift */; };
 		750A54601992D2680017DA75 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750A545F1992D2680017DA75 /* MD5.swift */; };
+		75100F8F19B0BC890005C5F5 /* CipherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* CipherTests.swift */; };
 		75153D4219AA3C7900750381 /* SHA2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75153D4119AA3C7900750381 /* SHA2.swift */; };
 		75153D4219AA3C7900750381 /* SHA2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75153D4119AA3C7900750381 /* SHA2.swift */; };
+		75164E4919AD30AC00737F30 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75164E4819AD30AC00737F30 /* Utils.swift */; };
 		752DEF7719693EA000E17557 /* NSDataExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752DEF7619693EA000E17557 /* NSDataExtension.swift */; };
 		752DEF7719693EA000E17557 /* NSDataExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752DEF7619693EA000E17557 /* NSDataExtension.swift */; };
 		752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752E087A199FF27C005B0EA0 /* SHA1.swift */; };
 		752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 752E087A199FF27C005B0EA0 /* SHA1.swift */; };
 		75445821196AA2A5002FF20E /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75445820196AA2A5002FF20E /* Security.framework */; settings = {ATTRIBUTES = (Required, ); }; };
 		75445821196AA2A5002FF20E /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75445820196AA2A5002FF20E /* Security.framework */; settings = {ATTRIBUTES = (Required, ); }; };
@@ -93,7 +95,9 @@
 
 
 /* Begin PBXFileReference section */
 /* Begin PBXFileReference section */
 		750A545F1992D2680017DA75 /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MD5.swift; sourceTree = "<group>"; };
 		750A545F1992D2680017DA75 /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MD5.swift; sourceTree = "<group>"; };
+		75100F8E19B0BC890005C5F5 /* CipherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CipherTests.swift; sourceTree = "<group>"; };
 		75153D4119AA3C7900750381 /* SHA2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA2.swift; sourceTree = "<group>"; };
 		75153D4119AA3C7900750381 /* SHA2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA2.swift; sourceTree = "<group>"; };
+		75164E4819AD30AC00737F30 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
 		752DEF7619693EA000E17557 /* NSDataExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataExtension.swift; sourceTree = "<group>"; };
 		752DEF7619693EA000E17557 /* NSDataExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataExtension.swift; sourceTree = "<group>"; };
 		752E087A199FF27C005B0EA0 /* SHA1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA1.swift; sourceTree = "<group>"; };
 		752E087A199FF27C005B0EA0 /* SHA1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SHA1.swift; sourceTree = "<group>"; };
 		75445820196AA2A5002FF20E /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
 		75445820196AA2A5002FF20E /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
@@ -179,6 +183,7 @@
 				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
 				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
 				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
 				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
+				75164E4819AD30AC00737F30 /* Utils.swift */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 			);
 			);
 			path = CryptoSwift;
 			path = CryptoSwift;
@@ -196,6 +201,7 @@
 			isa = PBXGroup;
 			isa = PBXGroup;
 			children = (
 			children = (
 				754BE46719693E190098E6F3 /* HashTests.swift */,
 				754BE46719693E190098E6F3 /* HashTests.swift */,
+				75100F8E19B0BC890005C5F5 /* CipherTests.swift */,
 				755FB1D9199E347D00475437 /* ExtensionsTest.swift */,
 				755FB1D9199E347D00475437 /* ExtensionsTest.swift */,
 				754BE46519693E190098E6F3 /* Supporting Files */,
 				754BE46519693E190098E6F3 /* Supporting Files */,
 			);
 			);
@@ -331,6 +337,7 @@
 				752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */,
 				752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */,
 				75EB380119ABDD710002375A /* ChaCha20.swift in Sources */,
 				75EB380119ABDD710002375A /* ChaCha20.swift in Sources */,
 				750A54601992D2680017DA75 /* MD5.swift in Sources */,
 				750A54601992D2680017DA75 /* MD5.swift in Sources */,
+				75164E4919AD30AC00737F30 /* Utils.swift in Sources */,
 				752DEF7719693EA000E17557 /* NSDataExtension.swift in Sources */,
 				752DEF7719693EA000E17557 /* NSDataExtension.swift in Sources */,
 				754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */,
 				754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
@@ -345,6 +352,7 @@
 			isa = PBXSourcesBuildPhase;
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
+				75100F8F19B0BC890005C5F5 /* CipherTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* HashTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* HashTests.swift in Sources */,
 				755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */,
 				755FB1DA199E347D00475437 /* ExtensionsTest.swift in Sources */,
 			);
 			);

+ 108 - 37
CryptoSwift/ChaCha20.swift

@@ -14,20 +14,23 @@ public class ChaCha20 {
     let stateSize = 16
     let stateSize = 16
     let blockSize = 16 * 4
     let blockSize = 16 * 4
     
     
+    public class Context {
+        var input:[UInt32] = [UInt32](count: 16, repeatedValue: 0)
+    }
+    
     public init() {
     public init() {
     }
     }
     
     
-    public func wordToByte(input:[UInt32] /* 64 */) -> [Byte]? /* 16 */ {
+    // checked
+    private func wordToByte(input:[UInt32] /* 64 */) -> [Byte]? /* 16 */ {
         if (input.count != stateSize) {
         if (input.count != stateSize) {
             return nil;
             return nil;
         }
         }
         
         
-        var x:[UInt32] = [UInt32]()
-        for val in input[0...15] {
-            x.append(val)
-        }
+        var x = input
         
         
-        for (var i = 8; i > 0; i -= 2) {
+        var i = 20
+        while (i  > 0) {
             quarterround(&x[0], &x[4], &x[8], &x[12])
             quarterround(&x[0], &x[4], &x[8], &x[12])
             quarterround(&x[1], &x[5], &x[9],  &x[13])
             quarterround(&x[1], &x[5], &x[9],  &x[13])
             quarterround(&x[2], &x[6], &x[10], &x[14])
             quarterround(&x[2], &x[6], &x[10], &x[14])
@@ -36,58 +39,126 @@ public class ChaCha20 {
             quarterround(&x[1], &x[6], &x[11], &x[12])
             quarterround(&x[1], &x[6], &x[11], &x[12])
             quarterround(&x[2], &x[7], &x[8],  &x[13])
             quarterround(&x[2], &x[7], &x[8],  &x[13])
             quarterround(&x[3], &x[4], &x[9],  &x[14])
             quarterround(&x[3], &x[4], &x[9],  &x[14])
+            i -= 2
         }
         }
         
         
-        for (idx,val) in enumerate(input[0...15]) {
-            x[idx] = plus(x[idx],val)
+        for i in 0..<16 {
+            x[i] = x[i] &+ input[i]
         }
         }
 
 
         var output:[Byte] = [Byte](count: 64, repeatedValue: 0)
         var output:[Byte] = [Byte](count: 64, repeatedValue: 0)
-        for (i,xval) in enumerate(x[0...15]) {
-            let bytes = x[i].bytes()
-            let start = (i * 4)
-            for o in start..<(start + 4) {
-                output[o] = bytes[o - start]
+        
+        // build output
+        // TODO: improve
+        for i in 0..<16 {
+            let reversed = x[i].bytes().reverse()
+            for j in 0..<reversed.count {
+                output[(i*4)+j] = reversed[j]
             }
             }
         }
         }
-        
+
         return output;
         return output;
     }
     }
     
     
-    private func UInt32To8Little(p:UInt32, _ v:UInt32) -> UInt8 {
-        var tmp1 = (v >> 0)  & 0xff | (v >> 8)  & 0xff
-        var tmp2 = (v >> 16) & 0xff | (v >> 24) & 0xff
-        return UInt8(tmp1 | tmp2)
+    public func keySetup(# iv:NSData, key:NSData, kbits:UInt32 = 256) -> Context {
+        return keySetup(iv: iv.arrayOfBytes(), key: key.arrayOfBytes(), kbits: kbits)
     }
     }
     
     
-    // rotate left
-    private func rotate(v:UInt32, _ c:UInt32) -> UInt32 {
-        return ((v << c) & 0xFFFFFFFF) | (v >> (32 - c))
+    private func keySetup(# iv:[Byte], key:[Byte], kbits:UInt32 = 256) -> Context {
+        var context = Context()
+        
+        // 4 - 8
+        for (var i = 0; i < 4; i++) {
+            let start = i * 4
+            context.input[i + 4] = UInt32.withBytes(key[start..<(start + 4)]).bigEndian
+        }
+        
+        var addPos = 0;
+        switch (kbits) {
+        case 256:
+            addPos += 16
+            // sigma
+            context.input[0] = 0x61707865 //apxe
+            context.input[1] = 0x3320646e //3 dn
+            context.input[2] = 0x79622d32 //yb-2
+            context.input[3] = 0x6b206574 //k et
+        default:
+            // tau
+            context.input[0] = 0x61707865 //apxe
+            context.input[1] = 0x3620646e //6 dn
+            context.input[2] = 0x79622d31 //yb-1
+            context.input[3] = 0x6b206574 //k et
+        break;
+        }
+        
+        for (var i = 0; i < 4; i++) {
+            let start = addPos + (i*4)
+            context.input[i + 8] = UInt32.withBytes(key[start..<(start + 4)]).bigEndian
+        }
+
+        // iv - checked
+        context.input[12] = 0
+        context.input[13] = 0
+        context.input[14] = UInt32.withBytes(iv[0..<4]).bigEndian
+        context.input[15] = UInt32.withBytes(iv[4..<8]).bigEndian
+        
+        println(iv)
+        
+        context.input.map({ (value) -> () in
+            var st: String = NSString(format:"%02X", value)
+            print(st)
+        })
+        println()
+
+        return context
     }
     }
     
     
-    private func u32v(x:UInt32) -> UInt32 {
-        return x & 0xFFFFFFFF
+    public func encrypt(context:Context, message:NSData) -> NSData {
+        let output = encryptBytes(context, message: message.arrayOfBytes())
+        return NSData(bytes: output, length: output.count)
     }
     }
     
     
-    private func plusone(v:UInt32) -> UInt32 {
-        return plus(v, 1)
+    private func encryptBytes(context:Context, message:[Byte]) -> [Byte] {
+        var cPos:Int = 0
+        var mPos:Int = 0
+        var bytes = message.count
+        
+        var c:[Byte] = [Byte](count: message.count, repeatedValue: 0)
+        
+        while (true) {
+            if let output = wordToByte(context.input) {
+                context.input[12] = context.input[12] &+ 1
+                if (context.input[12] == 0) {
+                    context.input[13] = context.input[13] &+ 1
+                    /* stopping at 2^70 bytes per nonce is user's responsibility */
+                }
+                if (bytes <= 64) {
+                    for (var i = 0; i < bytes; i++) {
+                        c[i + cPos] = message[i + mPos] ^ output[i]
+                    }
+                    return c
+                }
+                for (var i = 0; i < 64; i++) {
+                    c[i + cPos] = message[i + mPos] ^ output[i]
+                }
+                bytes -= 64
+                cPos += 64
+                mPos += 64
+            }
+        }
     }
     }
     
     
-    private func plus(v:UInt32, _ w:UInt32) -> UInt32 {
-        return v &+ w
-    }
-
     private func quarterround(inout a:UInt32, inout _ b:UInt32, inout _ c:UInt32, inout _ d:UInt32) {
     private func quarterround(inout a:UInt32, inout _ b:UInt32, inout _ c:UInt32, inout _ d:UInt32) {
-        a = plus(a,b);
-        d = rotate((d ^ a), 16)
+        a = a &+ b
+        d = rotateLeft((d ^ a), 16)
         
         
-        c = plus(c,d);
-        b = rotate((b ^ c), 12);
+        c = c &+ d
+        b = rotateLeft((b ^ c), 12);
         
         
-        a = plus(a,b);
-        d = rotate((d ^ a), 8);
+        a = a &+ b
+        d = rotateLeft((d ^ a), 8);
 
 
-        c = plus(c,d);
-        b = rotate((b ^ c), 7);
+        c = c &+ d
+        b = rotateLeft((b ^ c), 7);
     }
     }
 }
 }

+ 0 - 28
CryptoSwift/CryptoHashBase.swift

@@ -30,32 +30,4 @@ class CryptoHashBase {
         
         
         return tmpMessage
         return tmpMessage
     }
     }
-    
-    @availability(*,deprecated=0.1)
-    func reverseByte(value: UInt32) -> UInt32 {
-        // rdar://18060945 - not working since Xcode6-Beta6, need to split in two variables
-        // return = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8) | ((value & 0x00FF0000) >> 8)  | ((value & 0xFF000000) >> 24);
-        
-        // workaround
-        var tmp1 = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8)
-        var tmp2 = ((value & 0x00FF0000) >> 8)  | ((value & 0xFF000000) >> 24)
-        return tmp1 | tmp2
-    }
-    
-    func rotateLeft(x:UInt32, _ n:UInt32) -> UInt32 {
-        return ((x &<< n) & 0xffffffff) | (x &>> (32 - n))
-    }
-    
-    func rotateLeft(x:UInt64, _ n:UInt64) -> UInt64 {
-        return (x << n) | (x >> (64 - n))
-    }
-    
-    func rotateRight(x:UInt32, _ n:UInt32) -> UInt32 {
-        return (x >> n) | (x << (32 - n))
-    }
-
-    func rotateRight(x:UInt64, _ n:UInt64) -> UInt64 {
-        return ((x >> n) | (x << (64 - n)))
-    }
-
 }
 }

+ 23 - 0
CryptoSwift/IntExtension.swift

@@ -50,6 +50,29 @@ extension UInt32 {
     public func bytes(_ totalBytes: Int = sizeof(UInt32)) -> [Byte] {
     public func bytes(_ totalBytes: Int = sizeof(UInt32)) -> [Byte] {
         return bytesArray(self, totalBytes)
         return bytesArray(self, totalBytes)
     }
     }
+
+    public static func withBytes(bytes: Slice<Byte>) -> UInt32 {
+        return UInt32.withBytes(Array(bytes))
+    }
+
+    /** Int with array bytes (little-endian) */
+    public static func withBytes(bytes: [Byte]) -> UInt32 {
+        var i:UInt32 = 0
+        var totalBytes = Swift.min(bytes.count, sizeofValue(i))
+        
+        // get slice of Int
+        var start = Swift.max(bytes.count - sizeofValue(i),0)
+        var intarr = [Byte](bytes[start..<(start + totalBytes)])
+        
+        // extend to Int size if necessary
+        while (intarr.count < sizeofValue(i)) {
+            intarr.insert(0 as Byte, atIndex: 0)
+        }
+        
+        var data = NSData(bytes: intarr, length: intarr.count)
+        data.getBytes(&i, length: sizeofValue(i));
+        return i.byteSwapped
+    }
 }
 }
 
 
 extension UInt64 {
 extension UInt64 {

+ 28 - 13
CryptoSwift/Playground/MyPlayground.playground/section-1.swift

@@ -1,21 +1,36 @@
 // Playground - noun: a place where people can play
 // Playground - noun: a place where people can play
 
 
-import UIKit
+import Foundation
 
 
-// test inside class
-class Test {
-    func test(a:Int, b:Int) -> Int {
-        return a + b
-    }
+struct CTX {
+    var q = 1
 }
 }
 
 
-// test with no class
-func test(a:Int, b:Int) -> Int
-{
-    return a + b
+// WONT update q value of passed struct instance, but copy of it
+func foo1(var x:CTX) {
+    x.q = 3
 }
 }
 
 
-Test().test(3, b: 3) // parameter name "b" WITH specified name is required
-// BUT
-test(3,4) // no parameter name "b"
+// WILL update passed struct instance
+func foo2(inout x:CTX) {
+    x.q = 2
+}
+
+var x = CTX()
+
+foo1(x)
+println(x.q)
+
+foo2(&x)
+println(x.q)
 
 
+
+
+struct FixedLengthRange {
+    var firstValue: Int
+    let length: Int
+}
+var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
+// the range represents integer values 0, 1, and 2
+rangeOfThreeItems.firstValue = 6
+// the range now represents integer values 6, 7, and 8

+ 4 - 1
CryptoSwift/Playground/MyPlayground.playground/timeline.xctimeline

@@ -3,7 +3,10 @@
    version = "3.0">
    version = "3.0">
    <TimelineItems>
    <TimelineItems>
       <LoggerValueHistoryTimelineItem
       <LoggerValueHistoryTimelineItem
-         documentLocation = "file:///Users/marcinkrzyzanowski/Devel/CryptoSwift/CryptoSwift/Playground/MyPlayground.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=66&amp;EndingLineNumber=3&amp;StartingLineNumber=3&amp;Timestamp=429786824.478078">
+         documentLocation = "file:///Users/marcinkrzyzanowski/Devel/CryptoSwift/CryptoSwift/Playground/MyPlayground.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=72&amp;EndingLineNumber=3&amp;StartingLineNumber=3&amp;Timestamp=430787997.74212">
+      </LoggerValueHistoryTimelineItem>
+      <LoggerValueHistoryTimelineItem
+         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=72&amp;EndingColumnNumber=7&amp;EndingLineNumber=3&amp;StartingColumnNumber=5&amp;StartingLineNumber=3&amp;Timestamp=430787997.742359">
       </LoggerValueHistoryTimelineItem>
       </LoggerValueHistoryTimelineItem>
    </TimelineItems>
    </TimelineItems>
 </Timeline>
 </Timeline>

+ 37 - 0
CryptoSwift/Utils.swift

@@ -0,0 +1,37 @@
+//
+//  Utils.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 26/08/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+func rotateLeft(v:UInt32, n:UInt32) -> UInt32 {
+    return ((v << n) & 0xFFFFFFFF) | (v >> (32 - n))
+}
+
+func rotateLeft(x:UInt64, n:UInt64) -> UInt64 {
+    return (x << n) | (x >> (64 - n))
+}
+
+func rotateRight(x:UInt32, n:UInt32) -> UInt32 {
+    return (x >> n) | (x << (32 - n))
+}
+
+func rotateRight(x:UInt64, n:UInt64) -> UInt64 {
+    return ((x >> n) | (x << (64 - n)))
+}
+
+
+
+func reverseBytes(value: UInt32) -> UInt32 {
+    // rdar://18060945 - not working since Xcode6-Beta6, need to split in two variables
+    // return = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8) | ((value & 0x00FF0000) >> 8)  | ((value & 0xFF000000) >> 24);
+    
+    // workaround
+    var tmp1 = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8)
+    var tmp2 = ((value & 0x00FF0000) >> 8)  | ((value & 0xFF000000) >> 24)
+    return tmp1 | tmp2
+}

+ 78 - 0
CryptoSwiftTests/CipherTests.swift

@@ -0,0 +1,78 @@
+//
+//  CipherTests.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 29/08/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+import UIKit
+import XCTest
+import CryptoSwift
+
+class CipherTests: XCTestCase {
+
+    override func setUp() {
+        super.setUp()
+        // Put setup code here. This method is called before the invocation of each test method in the class.
+    }
+    
+    override func tearDown() {
+        // Put teardown code here. This method is called after the invocation of each test method in the class.
+        super.tearDown()
+    }
+
+    func testChaCha20() {
+        let ch = ChaCha20()
+        
+        let keys:[[Byte]] = [
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01],
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
+            [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F]
+        ]
+        
+        let ivs:[[Byte]] = [
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
+            [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01],
+            [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
+            [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07]
+        ]
+        
+        let expectedHex = [
+            "76B8E0ADA0F13D90405D6AE55386BD28BDD219B8A08DED1AA836EFCC8B770DC7DA41597C5157488D7724E03FB8D84A376A43B8F41518A11CC387B669",
+            "4540F05A9F1FB296D7736E7B208E3C96EB4FE1834688D2604F450952ED432D41BBE2A0B6EA7566D2A5D1E7E20D42AF2C53D792B1C43FEA817E9AD275",
+            "DE9CBA7BF3D69EF5E786DC63973F653A0B49E015ADBFF7134FCB7DF137821031E85A050278A7084527214F73EFC7FA5B5277062EB7A0433E445F41E3",
+            "EF3FDFD6C61578FBF5CF35BD3DD33B8009631634D21E42AC33960BD138E50D32111E4CAF237EE53CA8AD6426194A88545DDC497A0B466E7D6BBDB004",
+            "F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C134A4547B733B46413042C9440049176905D3BE59EA1C53F15916155C2BE8241A38008B9A26BC35941E2444177C8ADE6689DE95264986D95889FB60E84629C9BD9A5ACB1CC118BE563EB9B3A4A472F82E09A7E778492B562EF7130E88DFE031C79DB9D4F7C7A899151B9A475032B63FC385245FE054E3DD5A97A5F576FE064025D3CE042C566AB2C507B138DB853E3D6959660996546CC9C4A6EAFDC777C040D70EAF46F76DAD3979E5C5360C3317166A1C894C94A371876A94DF7628FE4EAAF2CCB27D5AAAE0AD7AD0F9D4B6AD3B54098746D4524D38407A6DEB"
+        ]
+
+        for (var idx = 0; idx < keys.count; idx++) {
+            let keyData = NSData(bytes: keys[idx], length: keys[idx].count)
+            let ivData = NSData(bytes: ivs[idx], length: ivs[idx].count)
+            
+            let context = ch.keySetup(iv: ivData, key: keyData)
+            
+            let expected = expectedHex[idx]
+            let message = [Byte](count: (countElements(expected) / 2), repeatedValue: 0)
+            let messageData = NSData(bytes: message, length: message.count);
+            let encrypted = ch.encrypt(context, message: messageData)
+            
+            XCTAssertEqual(encrypted.hexString, expected, "ChaCha20 failed");
+        }
+        
+//        let key:NSData = NSData(bytes: [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] as [Byte], length: 32)
+//        let iv:NSData = NSData(bytes: [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] as [Byte], length: 32)
+
+    }
+
+//    func testPerformanceExample() {
+//        // This is an example of a performance test case.
+//        self.measureBlock() {
+//            // Put the code you want to measure the time of here.
+//        }
+//    }
+
+}

+ 0 - 6
CryptoSwiftTests/HashTests.swift

@@ -113,10 +113,4 @@ class CryptoSwiftTests: XCTestCase {
         var crc = data.crc32()
         var crc = data.crc32()
         XCTAssertEqual(crc.hexString, "884863D2", "CRC32 calculation failed");
         XCTAssertEqual(crc.hexString, "884863D2", "CRC32 calculation failed");
     }
     }
-
-    func testChaCha20() {
-        var ch = ChaCha20()
-        let bytes = ch.wordToByte([49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64])
-        println(bytes)
-    }
 }
 }