Browse Source

poly init

Marcin Krzyżanowski 11 years ago
parent
commit
9b9d06b9e6
1 changed files with 33 additions and 4 deletions
  1. 33 4
      CryptoSwift/Poly1305.swift

+ 33 - 4
CryptoSwift/Poly1305.swift

@@ -6,16 +6,45 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 //  http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-4
+//
+//  Poly1305 takes a 32-byte, one-time key and a message and produces a 16-byte tag that authenticates the
+//  message such that an attacker has a negligible chance of producing a valid tag for an inauthentic message.
 
 import Foundation
 
 class Poly1305 {
 
-    var r:[Byte] = []
-    var h:[Byte] = []
+    var r:[Byte] = [Byte](count: 17, repeatedValue: 0)
+    var h:[Byte] = [Byte](count: 17, repeatedValue: 0)
+    var pad:[Byte] = [Byte](count: 17, repeatedValue: 0)
+    var final:Byte = 0
     
-    func setupForKey(key:NSData) {
-        let keyBytes = key.arrayOfBytes()
+    init (key: [Byte]) {
+        if (key.count != 32) {
+            return;
+        }
+        
+        for i in 0...15 {
+            h[i] = 0
+            r[i] = key[i] & 0x0f
+            pad[i] = key[i + 16]
+        }
         
+        h[16] = 0
+        r[16] = 0
+        pad[16] = 0
+        
+        final = 0
     }
+    
+    deinit {
+        for i in 0...(r.count) {
+            r[i] = 0
+            h[i] = 0
+            pad[i] = 0
+            final = 0
+        }
+    }
+    
+    
 }