Browse Source

Replace NSData with [UInt8] for AES interface

Marcin Krzyżanowski 10 years ago
parent
commit
9788ebe7c1
4 changed files with 27 additions and 28 deletions
  1. 12 13
      CryptoSwift/AES.swift
  2. 1 1
      CryptoSwift/Cipher.swift
  3. 2 2
      CryptoSwift/SHA1.swift
  4. 12 12
      CryptoSwiftTests/AESTests.swift

+ 12 - 13
CryptoSwift/AES.swift

@@ -27,8 +27,8 @@ private enum AESVariant:Int {
 public class AES {
     public let blockMode:CipherBlockMode
     private let variant:AESVariant
-    private let key:NSData
-    private let iv:NSData?
+    private let key:[UInt8]
+    private let iv:[UInt8]?
     
     private let sBox:[UInt8] = [
         0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 
@@ -93,12 +93,12 @@ public class AES {
         0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
         0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d]
     
-    public init?(key:NSData, iv:NSData, blockMode:CipherBlockMode = .CBC) {
+    public init?(key:[UInt8], iv:[UInt8], blockMode:CipherBlockMode = .CBC) {
         self.key = key
-        self.blockMode = blockMode
         self.iv = iv
+        self.blockMode = blockMode
         
-        switch (key.length * 8) {
+        switch (key.count * 8) {
         case 128:
             self.variant = .aes128
             break
@@ -113,15 +113,15 @@ public class AES {
             return nil
         }
         
-        if (blockMode.needIV && iv.length != AES.blockSizeBytes()) {
+        if (blockMode.needIV && iv.count != AES.blockSizeBytes()) {
             assert(false, "Block size and Initialization Vector must be the same length!")
             return nil
         }
     }
     
-    convenience public init?(key:NSData, blockMode:CipherBlockMode = .CBC) {
+    convenience public init?(key:[UInt8], blockMode:CipherBlockMode = .CBC) {
         // default IV is all 0x00...
-        let defaultIV = NSMutableData(length: AES.blockSizeBytes())!
+        let defaultIV = [UInt8](count: AES.blockSizeBytes(), repeatedValue: 0)
         self.init(key: key, iv: defaultIV, blockMode: blockMode)
     }
     
@@ -149,7 +149,7 @@ public class AES {
         }
         
         let blocks = finalBytes.chunks(AES.blockSizeBytes())
-        return blockMode.encryptBlocks(blocks, iv: self.iv?.bytes(), cipherOperation: encryptBlock)
+        return blockMode.encryptBlocks(blocks, iv: self.iv, cipherOperation: encryptBlock)
     }
     
     private func encryptBlock(block:[UInt8]) -> [UInt8]? {
@@ -196,9 +196,9 @@ public class AES {
         var out:[UInt8]?
         if (blockMode == .CFB) {
             // CFB uses encryptBlock to decrypt
-            out = blockMode.decryptBlocks(blocks, iv: self.iv?.bytes(), cipherOperation: encryptBlock)
+            out = blockMode.decryptBlocks(blocks, iv: self.iv, cipherOperation: encryptBlock)
         } else {
-            out = blockMode.decryptBlocks(blocks, iv: self.iv?.bytes(), cipherOperation: decryptBlock)
+            out = blockMode.decryptBlocks(blocks, iv: self.iv, cipherOperation: decryptBlock)
         }
         
         if let out = out, let padding = padding {
@@ -257,10 +257,9 @@ public class AES {
         }
 
         var w:[UInt8] = [UInt8](count: variant.Nb * (variant.Nr + 1) * 4, repeatedValue: 0)
-        let keyBytes:[UInt8] = key.bytes()
         for i in 0..<variant.Nk {
             for wordIdx in 0..<4 {
-                w[(4*i)+wordIdx] = keyBytes[(4*i)+wordIdx]
+                w[(4*i)+wordIdx] = key[(4*i)+wordIdx]
             }
         }
         

+ 1 - 1
CryptoSwift/Cipher.swift

@@ -27,7 +27,7 @@ public enum Cipher {
     
     :returns: Value of Cipher
     */
-    case AES(key: NSData, iv: NSData, blockMode: CipherBlockMode)
+    case AES(key: [UInt8], iv: [UInt8], blockMode: CipherBlockMode)
     
     /**
     Encrypt message

+ 2 - 2
CryptoSwift/SHA1.swift

@@ -92,10 +92,10 @@ class SHA1 : CryptoSwift.HashBase {
         
         // Produce the final hash value (big-endian) as a 160 bit number:
         var buf: NSMutableData = NSMutableData();
-        hh.map({ (item) -> () in
+        for item in hh {
             var i:UInt32 = item.bigEndian
             buf.appendBytes(&i, length: sizeofValue(i))
-        })
+        }
         
         return buf.copy() as! NSData;
     }    

+ 12 - 12
CryptoSwiftTests/AESTests.swift

@@ -21,7 +21,7 @@ class AESTests: XCTestCase {
         
         let expected:[UInt8] = [0xae,0x8c,0x59,0x95,0xb2,0x6f,0x8e,0x3d,0xb0,0x6f,0x0a,0xa5,0xfe,0xc4,0xf0,0xc2];
         
-        if let aes = AES(key: NSData.withBytes(key), iv: NSData.withBytes(iv), blockMode: .CBC) {
+        if let aes = AES(key: key, iv: iv, blockMode: .CBC) {
             let encrypted = aes.encrypt(input, padding: nil)
             XCTAssertEqual(encrypted!, expected, "encryption failed")
             let decrypted = aes.decrypt(encrypted!, padding: nil)
@@ -42,7 +42,7 @@ class AESTests: XCTestCase {
             0xd8, 0xcd, 0xb7, 0x80,
             0x70, 0xb4, 0xc5, 0x5a];
         
-        if let aes = AES(key: NSData.withBytes(aesKey), blockMode: .ECB) {
+        if let aes = AES(key: aesKey, blockMode: .ECB) {
             let encrypted = aes.encrypt(input, padding: nil)
             XCTAssertEqual(encrypted!, expected, "encryption failed")
             let decrypted = aes.decrypt(encrypted!, padding: nil)
@@ -58,7 +58,7 @@ class AESTests: XCTestCase {
         let plaintext:[UInt8] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
         let expected:[UInt8] = [0x76,0x49,0xab,0xac,0x81,0x19,0xb2,0x46,0xce,0xe9,0x8e,0x9b,0x12,0xe9,0x19,0x7d];
         
-        if let aes = AES(key: NSData.withBytes(key), iv:NSData.withBytes(iv), blockMode: .CBC) {
+        if let aes = AES(key: key, iv:iv, blockMode: .CBC) {
             XCTAssertTrue(aes.blockMode == .CBC, "Invalid block mode")
             let encrypted = aes.encrypt(plaintext, padding: nil)
             XCTAssertEqual(encrypted!, expected, "encryption failed")
@@ -75,7 +75,7 @@ class AESTests: XCTestCase {
         let plaintext:[UInt8] = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a]
         let expected:[UInt8] = [0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a];
         
-        if let aes = AES(key: NSData.withBytes(key), iv:NSData.withBytes(iv), blockMode: .CFB) {
+        if let aes = AES(key: key, iv:iv, blockMode: .CFB) {
             XCTAssertTrue(aes.blockMode == .CFB, "Invalid block mode")
             let encrypted = aes.encrypt(plaintext, padding: nil)
             XCTAssertEqual(encrypted!, expected, "encryption failed")
@@ -97,9 +97,9 @@ class AESTests: XCTestCase {
             [0xcd, 0x60, 0xe0, 0xe7],
             [0xba, 0x70, 0xe1, 0x8c]]
         
-        let substituted = AES(key: NSData.withBytes(aesKey), blockMode: .CBC)!.subBytes(input)
+        let substituted = AES(key: aesKey, blockMode: .CBC)!.subBytes(input)
         XCTAssertTrue(compareMatrix(expected, substituted), "subBytes failed")
-        let inverted = AES(key: NSData.withBytes(aesKey), blockMode: .CBC)!.invSubBytes(substituted)
+        let inverted = AES(key: aesKey, blockMode: .CBC)!.invSubBytes(substituted)
         XCTAssertTrue(compareMatrix(input, inverted), "invSubBytes failed")
     }
     
@@ -114,20 +114,20 @@ class AESTests: XCTestCase {
             [0xe0, 0xe1, 0xb7, 0xd0],
             [0x8c, 0x4, 0x51, 0xe7]]
         
-        let shifted = AES(key: NSData.withBytes(aesKey), blockMode: .CBC)!.shiftRows(input)
+        let shifted = AES(key: aesKey, blockMode: .CBC)!.shiftRows(input)
         XCTAssertTrue(compareMatrix(expected, shifted), "shiftRows failed")
-        let inverted = AES(key: NSData.withBytes(aesKey), blockMode: .CBC)!.invShiftRows(shifted)
+        let inverted = AES(key: aesKey, blockMode: .CBC)!.invShiftRows(shifted)
         XCTAssertTrue(compareMatrix(input, inverted), "invShiftRows failed")
     }
     
     func testAES_multiply() {
-        XCTAssertTrue(AES(key: NSData.withBytes(aesKey), blockMode: .CBC)?.multiplyPolys(0x0e, 0x5f) == 0x17, "Multiplication failed")
+        XCTAssertTrue(AES(key: aesKey, blockMode: .CBC)?.multiplyPolys(0x0e, 0x5f) == 0x17, "Multiplication failed")
     }
     
     func testAES_expandKey() {
         let expected:[UInt8] = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xd6, 0xaa, 0x74, 0xfd, 0xd2, 0xaf, 0x72, 0xfa, 0xda, 0xa6, 0x78, 0xf1, 0xd6, 0xab, 0x76, 0xfe, 0xb6, 0x92, 0xcf, 0xb, 0x64, 0x3d, 0xbd, 0xf1, 0xbe, 0x9b, 0xc5, 0x0, 0x68, 0x30, 0xb3, 0xfe, 0xb6, 0xff, 0x74, 0x4e, 0xd2, 0xc2, 0xc9, 0xbf, 0x6c, 0x59, 0xc, 0xbf, 0x4, 0x69, 0xbf, 0x41, 0x47, 0xf7, 0xf7, 0xbc, 0x95, 0x35, 0x3e, 0x3, 0xf9, 0x6c, 0x32, 0xbc, 0xfd, 0x5, 0x8d, 0xfd, 0x3c, 0xaa, 0xa3, 0xe8, 0xa9, 0x9f, 0x9d, 0xeb, 0x50, 0xf3, 0xaf, 0x57, 0xad, 0xf6, 0x22, 0xaa, 0x5e, 0x39, 0xf, 0x7d, 0xf7, 0xa6, 0x92, 0x96, 0xa7, 0x55, 0x3d, 0xc1, 0xa, 0xa3, 0x1f, 0x6b, 0x14, 0xf9, 0x70, 0x1a, 0xe3, 0x5f, 0xe2, 0x8c, 0x44, 0xa, 0xdf, 0x4d, 0x4e, 0xa9, 0xc0, 0x26, 0x47, 0x43, 0x87, 0x35, 0xa4, 0x1c, 0x65, 0xb9, 0xe0, 0x16, 0xba, 0xf4, 0xae, 0xbf, 0x7a, 0xd2, 0x54, 0x99, 0x32, 0xd1, 0xf0, 0x85, 0x57, 0x68, 0x10, 0x93, 0xed, 0x9c, 0xbe, 0x2c, 0x97, 0x4e, 0x13, 0x11, 0x1d, 0x7f, 0xe3, 0x94, 0x4a, 0x17, 0xf3, 0x7, 0xa7, 0x8b, 0x4d, 0x2b, 0x30, 0xc5]
         
-        if let aes = AES(key: NSData.withBytes(aesKey), blockMode: .CBC) {
+        if let aes = AES(key: aesKey, blockMode: .CBC) {
             XCTAssertEqual(expected, aes.expandKey(), "expandKey failed")
         } else {
             XCTAssert(false, "")
@@ -145,7 +145,7 @@ class AESTests: XCTestCase {
             [32, 96, 160, 224],
             [48, 112, 176, 240]]
         
-        if let aes = AES(key: NSData.withBytes(aesKey), blockMode: .CBC) {
+        if let aes = AES(key: aesKey, blockMode: .CBC) {
             let result = aes.addRoundKey(input, aes.expandKey(), 0)
             XCTAssertTrue(compareMatrix(expected, result), "addRoundKey failed")
         } else {
@@ -164,7 +164,7 @@ class AESTests: XCTestCase {
             [0x64, 0xbc, 0x3b, 0xf9],
             [0x15, 0x92, 0x29, 0x1a]]
         
-        if let aes = AES(key: NSData.withBytes(aesKey), blockMode: .CBC) {
+        if let aes = AES(key: aesKey, blockMode: .CBC) {
             let mixed = aes.mixColumns(input)
             XCTAssertTrue(compareMatrix(expected, mixed), "mixColumns failed")
             let inverted = aes.invMixColumns(mixed)