Explorar el Código

cleanup code. Separate Int and UInt32 Extensions. New operators defined in Operators.swift. Byte.shiftRight addedd. Poly1305 minor improvements

Marcin Krzyżanowski hace 11 años
padre
commit
72a6ec3cf9

+ 8 - 0
CryptoSwift.xcodeproj/project.pbxproj

@@ -29,6 +29,8 @@
 		7599C9C6199EA28700A3988B /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7599C9C5199EA28700A3988B /* StringExtension.swift */; };
 		759D481119B517BC005FF7FC /* BitExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759D481019B517BC005FF7FC /* BitExtension.swift */; };
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
+		75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2319B60C08007CB2A4 /* Operators.swift */; };
+		75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */; };
 		75EB380119ABDD710002375A /* ChaCha20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EB380019ABDD710002375A /* ChaCha20.swift */; };
 /* End PBXBuildFile section */
 
@@ -122,6 +124,8 @@
 		758F3F771992F6CE0014BBDA /* ByteExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteExtension.swift; sourceTree = "<group>"; };
 		7599C9C5199EA28700A3988B /* StringExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = "<group>"; };
 		759D481019B517BC005FF7FC /* BitExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BitExtension.swift; sourceTree = "<group>"; };
+		75D94E2319B60C08007CB2A4 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
+		75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt32Extension.swift; sourceTree = "<group>"; };
 		75EB380019ABDD710002375A /* ChaCha20.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChaCha20.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -189,6 +193,8 @@
 				759D481019B517BC005FF7FC /* BitExtension.swift */,
 				758F3F771992F6CE0014BBDA /* ByteExtension.swift */,
 				7547195019931802002FA5F1 /* IntExtension.swift */,
+				75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */,
+				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
 				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
@@ -353,7 +359,9 @@
 				759D481119B517BC005FF7FC /* BitExtension.swift in Sources */,
 				754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
+				75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */,
 				757EF7F519AAA82400586276 /* CRC.swift in Sources */,
+				75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */,
 				754DD76E19A149AF00E52288 /* CryptoHashBase.swift in Sources */,
 				758F3F781992F6CE0014BBDA /* ByteExtension.swift in Sources */,
 				75153D4219AA3C7900750381 /* SHA2.swift in Sources */,

+ 33 - 0
CryptoSwift/ByteExtension.swift

@@ -48,4 +48,37 @@ extension Byte {
         }
         return s
     }
+
+    /** Shift bits to the right. All bits are shifted (including sign bit) */
+    mutating func shiftRight(count: Byte) -> Byte {
+        if (self == 0) {
+            return self;
+        }
+
+        var bitsCount = Byte(sizeof(Byte) * 8)
+
+        if (count >= bitsCount) {
+            return 0
+        }
+
+        var maxBitsForValue = Byte(floor(log2(Double(self) + 1)))
+        var shiftCount = Swift.min(count, maxBitsForValue - 1)
+        var shiftedValue:Byte = 0;
+        
+        for bitIdx in 0..<bitsCount {
+            var byte = 1 << bitIdx
+            if ((self & byte) == byte) {
+                shiftedValue = shiftedValue | (byte >> shiftCount)
+            }
+        }
+        self = shiftedValue
+        return self
+    }
+}
+
+/** shift right and assign with bits truncation */
+func &>> (lhs: Byte, rhs: Byte) -> Byte {
+    var l = lhs;
+    l.shiftRight(rhs)
+    return l
 }

+ 8 - 126
CryptoSwift/IntExtension.swift

@@ -33,48 +33,6 @@ j &<<= 2        //shift left and assign
 
 import Foundation
 
-/** array of bytes, little-endian representation */
-private func bytesArray<T>(value:T, totalBytes:Int) -> [Byte] {
-    var bytes = [Byte](count: totalBytes, repeatedValue: 0)
-    var data = NSData(bytes: [value] as [T], length: min(sizeof(T),totalBytes))
-    
-    // then convert back to bytes, byte by byte
-    for i in 0..<data.length {
-        data.getBytes(&bytes[totalBytes - 1 - i], range:NSRange(location:i, length:sizeof(Byte)))
-    }
-    
-    return bytes
-}
-
-extension UInt32 {
-    public func bytes(_ totalBytes: Int = sizeof(UInt32)) -> [Byte] {
-        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 {
     public func bytes(_ totalBytes: Int = sizeof(UInt64)) -> [Byte] {
         return bytesArray(self, totalBytes)
@@ -120,53 +78,7 @@ extension Int {
     }
 }
 
-/** Bit operations */
-extension UInt32 {
-    
-    /** Shift bits to the left. All bits are shifted (including sign bit) */
-    private mutating func shiftLeft(count: UInt32) -> UInt32 {
-        if (self == 0) {
-            return self;
-        }
-        
-        var bitsCount = UInt32(sizeof(UInt32) * 8)
-        var shiftCount = Swift.min(count, bitsCount - 1)
-        var shiftedValue:UInt32 = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            // if bit is set then copy to result and shift left 1
-            var bit = 1 << bitIdx
-            if ((self & bit) == bit) {
-                shiftedValue = shiftedValue | (bit << shiftCount)
-            }
-        }
-        self = shiftedValue
-        return self
-    }
-    
-    /** Shift bits to the right. All bits are shifted (including sign bit) */
-    private mutating func shiftRight(count: UInt32) -> UInt32 {
-        if (self == 0) {
-            return self;
-        }
-        
-        var bitsCount = UInt32(sizeof(UInt32) * 8)
-        var maxBitsForValue = UInt32(floor(log2(Double(self)) + 1))
-        var shiftCount = Swift.min(count, maxBitsForValue)
-        var shiftedValue:UInt32 = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            // if bit is set then copy to result and shift left 1
-            var bit = 1 << bitIdx
-            if ((self & bit) == bit) {
-                shiftedValue = shiftedValue | (bit >> shiftCount)
-            }
-        }
-        self = shiftedValue
-        return self
-    }
 
-}
 
 /** Bit operations */
 extension Int {
@@ -177,7 +89,7 @@ extension Int {
             return self;
         }
         
-        var bitsCount = sizeof(Int) * 8
+        var bitsCount = sizeofValue(self) * 8
         var shiftCount = Swift.min(count, bitsCount - 1)
         var shiftedValue:Int = 0;
         
@@ -198,9 +110,14 @@ extension Int {
             return self;
         }
         
-        var bitsCount = sizeof(Int) * 8
+        var bitsCount = sizeofValue(self) * 8
+
+        if (count >= bitsCount) {
+            return 0
+        }
+
         var maxBitsForValue = Int(floor(log2(Double(self)) + 1))
-        var shiftCount = Swift.min(count, maxBitsForValue)
+        var shiftCount = Swift.min(count, maxBitsForValue - 1)
         var shiftedValue:Int = 0;
         
         for bitIdx in 0..<bitsCount {
@@ -217,22 +134,11 @@ extension Int {
 
 // Left operator
 
-infix operator &<<= {
-    associativity none
-    precedence 160
-}
-
-
 /** shift left and assign with bits truncation */
 func &<<= (inout lhs: Int, rhs: Int) {
     lhs.shiftLeft(rhs)
 }
 
-infix operator &<< {
-    associativity none
-    precedence 160
-}
-
 /** shift left with bits truncation */
 func &<< (lhs: Int, rhs: Int) -> Int {
     var l = lhs;
@@ -240,40 +146,16 @@ func &<< (lhs: Int, rhs: Int) -> Int {
     return l
 }
 
-/** shift left with bits truncation */
-func &<< (lhs: UInt32, rhs: UInt32) -> UInt32 {
-    var l = lhs;
-    l.shiftLeft(rhs)
-    return l
-}
-
 // Right operator
 
-infix operator &>>= {
-    associativity none
-    precedence 160
-}
-
 /** shift right and assign with bits truncation */
 func &>>= (inout lhs: Int, rhs: Int) {
     lhs.shiftRight(rhs)
 }
 
-infix operator &>> {
-associativity none
-precedence 160
-}
-
 /** shift right and assign with bits truncation */
 func &>> (lhs: Int, rhs: Int) -> Int {
     var l = lhs;
     l.shiftRight(rhs)
     return l
 }
-
-/** shift right and assign with bits truncation */
-func &>> (lhs: UInt32, rhs: UInt32) -> UInt32 {
-    var l = lhs;
-    l.shiftRight(rhs)
-    return l
-}

+ 29 - 0
CryptoSwift/Operators.swift

@@ -0,0 +1,29 @@
+//
+//  Operators.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/09/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+infix operator &<<= {
+associativity none
+precedence 160
+}
+
+infix operator &<< {
+associativity none
+precedence 160
+}
+
+infix operator &>>= {
+associativity none
+precedence 160
+}
+
+infix operator &>> {
+associativity none
+precedence 160
+}

+ 61 - 0
CryptoSwift/Playground/MyPlayground.playground/section-1.swift

@@ -2,4 +2,65 @@
 
 import Foundation
 
+extension Byte {
+    mutating func shiftRight(count: Byte) -> Byte {
+        if (self == 0) {
+            return self;
+        }
+
+        var bitsCount = Byte(sizeof(Byte) * 8)
+
+        if (count >= bitsCount) {
+            return 0
+        }
+
+        var maxBitsForValue = Byte(floor(log2(Double(self) + 1)))
+        var shiftCount = Swift.min(count, maxBitsForValue - 1)
+        var shiftedValue:Byte = 0;
+        
+        for bitIdx in 0..<bitsCount {
+            var byte = 1 << bitIdx
+            if ((self & byte) == byte) {
+                shiftedValue = shiftedValue | (byte >> shiftCount)
+            }
+        }
+        self = shiftedValue
+        return self
+    }
+}
+
+var b:Byte = 255
+b >> 7
+b.shiftRight(8)
+
+//
+///** Bit operations */
+//extension Int {
+//    /** Shift bits to the right. All bits are shifted (including sign bit) */
+//    private mutating func shiftRight(count: Int) -> Int {
+//        if (self == 0) {
+//            return self;
+//        }
+//        
+//        var bitsCount = sizeof(Int) * 8
+//        var maxBitsForValue = Int(floor(log2(Double(self)) + 1)) - 1
+//        var shiftCount = Swift.min(count, maxBitsForValue)
+//        var shiftedValue:Int = 0;
+//        
+//        for bitIdx in 0..<bitsCount {
+//            // if bit is set then copy to result and shift left 1
+//            var bit = 1 << bitIdx
+//            if ((self & bit) == bit) {
+//                shiftedValue = shiftedValue | (bit >> shiftCount)
+//            }
+//        }
+//        self = Int(shiftedValue)
+//        return self
+//    }
+//}
+//
+//var a:Int = 9223372036854775807
+//a >> 63
+//a.shiftRight(63)
+
 

+ 5 - 2
CryptoSwift/Playground/MyPlayground.playground/timeline.xctimeline

@@ -3,10 +3,13 @@
    version = "3.0">
    <TimelineItems>
       <LoggerValueHistoryTimelineItem
-         documentLocation = "file:///Users/marcinkrzyzanowski/Devel/CryptoSwift/CryptoSwift/Playground/MyPlayground.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=71&amp;EndingLineNumber=3&amp;StartingLineNumber=3&amp;Timestamp=431184872.224454">
+         documentLocation = "file:///Users/marcinkrzyzanowski/Devel/CryptoSwift/CryptoSwift/Playground/MyPlayground.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=792&amp;EndingLineNumber=3&amp;StartingLineNumber=3&amp;Timestamp=431360683.801244">
       </LoggerValueHistoryTimelineItem>
       <LoggerValueHistoryTimelineItem
-         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=71&amp;EndingColumnNumber=7&amp;EndingLineNumber=3&amp;StartingColumnNumber=5&amp;StartingLineNumber=3&amp;Timestamp=431184872.224689">
+         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=792&amp;EndingColumnNumber=7&amp;EndingLineNumber=3&amp;StartingColumnNumber=5&amp;StartingLineNumber=3&amp;Timestamp=431360683.801244">
+      </LoggerValueHistoryTimelineItem>
+      <LoggerValueHistoryTimelineItem
+         documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=792&amp;EndingColumnNumber=20&amp;EndingLineNumber=51&amp;StartingColumnNumber=17&amp;StartingLineNumber=51&amp;Timestamp=431360683.801244">
       </LoggerValueHistoryTimelineItem>
    </TimelineItems>
 </Timeline>

+ 12 - 10
CryptoSwift/Poly1305.swift

@@ -12,7 +12,7 @@
 
 import Foundation
 
-class Poly1305 {
+public class Poly1305 {
     let blockSize = 16
     
     var buffer:[Byte] = [Byte](count: 16, repeatedValue: 0)
@@ -22,7 +22,7 @@ class Poly1305 {
     var final:Byte = 0
     var leftover:Int = 0
     
-    init (key: [Byte]) {
+    public init (key: [Byte]) {
         if (key.count != 32) {
             return;
         }
@@ -45,11 +45,12 @@ class Poly1305 {
     }
     
     deinit {
-        for i in 0...(r.count) {
+        for i in 0..<(r.count) {
             r[i] = 0
             h[i] = 0
             pad[i] = 0
             final = 0
+            leftover = 0
         }
     }
     
@@ -62,7 +63,8 @@ class Poly1305 {
         for i in 0..<h.count {
             u = u &+ h[i] &+ c[i]
             h[0] = u
-            u >>= 8
+            println(u)
+            u.shiftRight(8) // u = u >> 8
         }
         return true
     }
@@ -77,17 +79,17 @@ class Poly1305 {
         for i in 0..<16 {
             u = u &+ hr[i];
             h[i] = Byte(u) & 0xff;
-            u >>= 8;
+            u = u >> 8;
         }
         
         u = u &+ hr[16]
         h[16] = Byte(u) & 0x03
-        u >>= 2
+        u = u >> 2
         u += (u << 2); /* u *= 5; */
         for i in 0..<16 {
             u = u &+ UInt32(h[i])
             h[i] = Byte(u) & 0xff
-            u >>= 8
+            u = u >> 8
         }
         h[16] = h[16] &+ Byte(u);
         
@@ -161,7 +163,7 @@ class Poly1305 {
         return mPos
     }
     
-    func finish(inout mac:[Byte]) -> Bool {
+    public func finish(inout mac:[Byte]) -> Bool {
         if (h.count != 16) {
             return false
         }
@@ -189,7 +191,7 @@ class Poly1305 {
         return true
     }
     
-    func update(m:[Byte]) {
+    public func update(m:[Byte]) {
         var bytes = m.count
         var mPos = 0
         
@@ -234,7 +236,7 @@ class Poly1305 {
         }
     }
     
-    func auth(mac:[Byte], m:[Byte]) {
+    public func auth(mac:[Byte], m:[Byte]) {
         update(m)
         var macLocal = mac
         finish(&macLocal)

+ 116 - 0
CryptoSwift/UInt32Extension.swift

@@ -0,0 +1,116 @@
+//
+//  UInt32Extension.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/09/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+/* bytes of array */
+extension UInt32 {
+    public func bytes(_ totalBytes: Int = sizeof(UInt32)) -> [Byte] {
+        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
+    }
+}
+
+/** Bit operations */
+extension UInt32 {
+    
+    /** Shift bits to the left. All bits are shifted (including sign bit) */
+    private mutating func shiftLeft(count: UInt32) -> UInt32 {
+        if (self == 0) {
+            return self;
+        }
+        
+        var bitsCount = UInt32(sizeof(UInt32) * 8)
+        var shiftCount = Swift.min(count, bitsCount - 1)
+        var shiftedValue:UInt32 = 0;
+        
+        for bitIdx in 0..<bitsCount {
+            // if bit is set then copy to result and shift left 1
+            var bit = 1 << bitIdx
+            if ((self & bit) == bit) {
+                shiftedValue = shiftedValue | (bit << shiftCount)
+            }
+        }
+        self = shiftedValue
+        return self
+    }
+    
+    /** Shift bits to the right. All bits are shifted (including sign bit) */
+    private mutating func shiftRight(count: UInt32) -> UInt32 {
+        if (self == 0) {
+            return self;
+        }
+        
+        var bitsCount = UInt32(sizeofValue(self) * 8)
+
+        if (count >= bitsCount) {
+            return 0
+        }
+
+        var maxBitsForValue = UInt32(floor(log2(Double(self)) + 1))
+        var shiftCount = Swift.min(count, maxBitsForValue - 1)
+        var shiftedValue:UInt32 = 0;
+        
+        for bitIdx in 0..<bitsCount {
+            // if bit is set then copy to result and shift left 1
+            var bit = 1 << bitIdx
+            if ((self & bit) == bit) {
+                shiftedValue = shiftedValue | (bit >> shiftCount)
+            }
+        }
+        self = shiftedValue
+        return self
+    }
+
+}
+
+/** shift left and assign with bits truncation */
+func &<<= (inout lhs: UInt32, rhs: UInt32) {
+    lhs.shiftLeft(rhs)
+}
+
+/** shift left with bits truncation */
+func &<< (lhs: UInt32, rhs: UInt32) -> UInt32 {
+    var l = lhs;
+    l.shiftLeft(rhs)
+    return l
+}
+
+/** shift right and assign with bits truncation */
+func &>>= (inout lhs: UInt32, rhs: UInt32) {
+    lhs.shiftRight(rhs)
+}
+
+/** shift right and assign with bits truncation */
+func &>> (lhs: UInt32, rhs: UInt32) -> UInt32 {
+    var l = lhs;
+    l.shiftRight(rhs)
+    return l
+}

+ 14 - 2
CryptoSwift/Utils.swift

@@ -24,8 +24,6 @@ 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);
@@ -35,3 +33,17 @@ func reverseBytes(value: UInt32) -> UInt32 {
     var tmp2 = ((value & 0x00FF0000) >> 8)  | ((value & 0xFF000000) >> 24)
     return tmp1 | tmp2
 }
+
+
+/** array of bytes, little-endian representation */
+func bytesArray<T>(value:T, totalBytes:Int) -> [Byte] {
+    var bytes = [Byte](count: totalBytes, repeatedValue: 0)
+    var data = NSData(bytes: [value] as [T], length: min(sizeof(T),totalBytes))
+    
+    // then convert back to bytes, byte by byte
+    for i in 0..<data.length {
+        data.getBytes(&bytes[totalBytes - 1 - i], range:NSRange(location:i, length:sizeof(Byte)))
+    }
+    
+    return bytes
+}

+ 33 - 0
CryptoSwiftTests/CipherTests.swift

@@ -19,6 +19,39 @@ class CipherTests: XCTestCase {
     override func tearDown() {
         super.tearDown()
     }
+    
+    func testPoly1305() {
+        let key:[Byte] = [0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
+            0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
+            0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
+            0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80]
+        
+        let msg:[Byte] = [0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
+            0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
+            0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
+            0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
+            0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
+            0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
+            0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
+            0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
+            0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
+            0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
+            0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
+            0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
+            0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
+            0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
+            0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
+            0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
+            0xe3,0x55,0xa5]
+        
+        let verify_mac:[Byte] = [0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
+                                 0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9]
+        
+        
+        let poly = Poly1305(key: key);
+        let mac:[Byte] = [Byte](count: 16, repeatedValue: 0);
+        poly.auth(mac, m: msg)
+    }
 
     func testChaCha20() {
         let keys:[[Byte]] = [