Browse Source

Merge branch 'new-AES-Gladman' into develop

Marcin Krzyżanowski 10 years ago
parent
commit
9abedbaef3

+ 42 - 0
CryptoSwift.xcodeproj/xcshareddata/xcbaselines/754BE45F19693E190098E6F3.xcbaseline/85434D17-7706-4DE9-AFA7-B1DD0BD9FA39.plist

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>classNames</key>
+	<dict>
+		<key>AESTests</key>
+		<dict>
+			<key>testAESPerformance()</key>
+			<dict>
+				<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
+				<dict>
+					<key>baselineAverage</key>
+					<real>0.62</real>
+					<key>baselineIntegrationDisplayName</key>
+					<string>Local Baseline</string>
+				</dict>
+			</dict>
+			<key>testAES_decrypt_performance()</key>
+			<dict>
+				<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
+				<dict>
+					<key>baselineAverage</key>
+					<real>0.624</real>
+					<key>baselineIntegrationDisplayName</key>
+					<string>Local Baseline</string>
+				</dict>
+			</dict>
+			<key>testAES_encrypt_performance()</key>
+			<dict>
+				<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
+				<dict>
+					<key>baselineAverage</key>
+					<real>0.5895</real>
+					<key>baselineIntegrationDisplayName</key>
+					<string>Local Baseline</string>
+				</dict>
+			</dict>
+		</dict>
+	</dict>
+</dict>
+</plist>

+ 31 - 0
CryptoSwift.xcodeproj/xcshareddata/xcbaselines/754BE45F19693E190098E6F3.xcbaseline/Info.plist

@@ -4,6 +4,37 @@
 <dict>
 	<key>runDestinationsByUUID</key>
 	<dict>
+		<key>85434D17-7706-4DE9-AFA7-B1DD0BD9FA39</key>
+		<dict>
+			<key>localComputer</key>
+			<dict>
+				<key>busSpeedInMHz</key>
+				<integer>100</integer>
+				<key>cpuCount</key>
+				<integer>1</integer>
+				<key>cpuKind</key>
+				<string>Intel Core i7</string>
+				<key>cpuSpeedInMHz</key>
+				<integer>2200</integer>
+				<key>logicalCPUCoresPerPackage</key>
+				<integer>4</integer>
+				<key>modelCode</key>
+				<string>MacBookAir7,2</string>
+				<key>physicalCPUCoresPerPackage</key>
+				<integer>2</integer>
+				<key>platformIdentifier</key>
+				<string>com.apple.platform.macosx</string>
+			</dict>
+			<key>targetArchitecture</key>
+			<string>x86_64</string>
+			<key>targetDevice</key>
+			<dict>
+				<key>modelCode</key>
+				<string>iPhone8,1</string>
+				<key>platformIdentifier</key>
+				<string>com.apple.platform.iphonesimulator</string>
+			</dict>
+		</dict>
 		<key>9396A4FE-F8F7-4542-8F75-DE77E1FEBEB9</key>
 		<dict>
 			<key>localComputer</key>

+ 1 - 4
CryptoSwift.xcodeproj/xcshareddata/xcschemes/CryptoSwift.xcscheme

@@ -37,7 +37,7 @@
       </BuildActionEntries>
    </BuildAction>
    <TestAction
-      buildConfiguration = "Debug"
+      buildConfiguration = "Release"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       shouldUseLaunchSchemeArgsEnv = "YES"
@@ -53,9 +53,6 @@
                ReferencedContainer = "container:CryptoSwift.xcodeproj">
             </BuildableReference>
             <SkippedTests>
-               <Test
-                  Identifier = "AESTests/testAESPerformance()">
-               </Test>
                <Test
                   Identifier = "AESTests/testAESPerformanceCommonCrypto()">
                </Test>

File diff suppressed because it is too large
+ 5 - 4
CryptoSwift/AES.swift


+ 13 - 0
CryptoSwift/Array+Extension.swift

@@ -22,5 +22,18 @@ extension Array {
         }
         return words
     }
+    
+    /*
+    This helper call is slow, therefore don't use it. It is due to extension, or due to optimization that can be done
+    
+    subscript(index: UInt32) -> Element {
+        get {
+            return self[Int(index)]
+        }
+        set {
+            self[Int(index)] = newValue
+        }
+    }
+    */
 }
 

+ 1 - 1
CryptoSwift/ArraySlice<UInt8>+Bytes.swift

@@ -10,6 +10,7 @@ extension ArraySlice where Element: _UInt8Type {
     
     func toUInt32Array() -> Array<UInt32> {
         var result = Array<UInt32>()
+        result.reserveCapacity(self.count / sizeof(UInt32))
         for idx in self.startIndex.stride(to: self.endIndex, by: sizeof(UInt32)) {
             var val:UInt32 = 0
             val |= UInt32(self[idx.advancedBy(3)] as! UInt8) << 24
@@ -37,5 +38,4 @@ extension ArraySlice where Element: _UInt8Type {
         }
         return result
     }
-
 }

+ 1 - 1
CryptoSwift/Generics.swift

@@ -48,7 +48,7 @@ func integerWithBytes<T: IntegerType where T:ByteConvertible, T: BitshiftOperati
     }
     
     var result: T = 0
-    for byte in Array(bytes.reverse()) { //FIXME: Array??
+    for byte in bytes.reverse() {
         result = result << 8 | T(byte)
     }
     return result

+ 13 - 91
CryptoSwiftTests/AESTests.swift

@@ -118,104 +118,26 @@ final class AESTests: XCTestCase {
         }
     }
     
-    func testAES_SubBytes() {
-        let input:[[UInt8]] = [[0x00, 0x10, 0x20, 0x30],
-            [0x40, 0x50, 0x60, 0x70],
-            [0x80, 0x90, 0xa0, 0xb0],
-            [0xc0, 0xd0, 0xe0, 0xf0]]
-        
-        let expected:[[UInt8]] = [[0x63, 0xca, 0xb7, 0x04],
-            [0x09, 0x53, 0xd0, 0x51],
-            [0xcd, 0x60, 0xe0, 0xe7],
-            [0xba, 0x70, 0xe1, 0x8c]]
-        
-        var substituted = input
-        AES(key: aesKey, blockMode: .CBC)!.subBytes(&substituted)
-        XCTAssertTrue(compareMatrix(expected, b: substituted), "subBytes failed")
-        let inverted = AES(key: aesKey, blockMode: .CBC)!.invSubBytes(substituted)
-        XCTAssertTrue(compareMatrix(input, b: inverted), "invSubBytes failed")
-    }
-    
-    func testAES_shiftRows() {
-        let input:[[UInt8]] = [[0x63, 0x09, 0xcd, 0xba],
-            [0xca, 0x53, 0x60, 0x70],
-            [0xb7, 0xd0, 0xe0, 0xe1],
-            [0x04, 0x51, 0xe7, 0x8c]]
-        
-        let expected:[[UInt8]] = [[0x63, 0x9, 0xcd, 0xba],
-            [0x53, 0x60, 0x70, 0xca],
-            [0xe0, 0xe1, 0xb7, 0xd0],
-            [0x8c, 0x4, 0x51, 0xe7]]
-        
-        let shifted = AES(key: aesKey, blockMode: .CBC)!.shiftRows(input)
-        XCTAssertTrue(compareMatrix(expected, b: shifted), "shiftRows failed")
-        let inverted = AES(key: aesKey, blockMode: .CBC)!.invShiftRows(shifted)
-        XCTAssertTrue(compareMatrix(input, b: inverted), "invShiftRows failed")
-    }
-    
-    func testAES_multiply() {
-        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: aesKey, blockMode: .CBC) {
-            XCTAssertEqual(expected, aes.expandedKey, "expandKey failed")
-        } else {
-            XCTAssert(false, "")
-        }
-    }
-    
-    func testAES_addRoundKey() {
-        let input:[[UInt8]] = [[0x00, 0x44, 0x88, 0xcc],
-            [0x11, 0x55, 0x99, 0xdd],
-            [0x22, 0x66, 0xaa, 0xee],
-            [0x33, 0x77, 0xbb, 0xff]]
-        
-        let expected:[[UInt8]] = [[0, 64, 128, 192],
-            [16, 80, 144, 208],
-            [32, 96, 160, 224],
-            [48, 112, 176, 240]]
-        
-        if let aes = AES(key: aesKey, blockMode: .CBC) {
-            let result = aes.addRoundKey(input, aes.expandedKey, 0)
-            XCTAssertTrue(compareMatrix(expected, b: result), "addRoundKey failed")
-        } else {
-            XCTAssert(false, "")
-        }
-    }
-    
-    func testAES_mixColumns() {
-        let input:[[UInt8]] = [[0x63, 0x9, 0xcd, 0xba],
-            [0x53, 0x60, 0x70, 0xca],
-            [0xe0, 0xe1, 0xb7, 0xd0],
-            [0x8c, 0x4, 0x51, 0xe7]]
-        
-        let expected:[[UInt8]] = [[0x5f, 0x57, 0xf7, 0x1d],
-            [0x72, 0xf5, 0xbe, 0xb9],
-            [0x64, 0xbc, 0x3b, 0xf9],
-            [0x15, 0x92, 0x29, 0x1a]]
-        
-        if let aes = AES(key: aesKey, blockMode: .CBC) {
-            let mixed = aes.mixColumns(input)
-            XCTAssertTrue(compareMatrix(expected, b: mixed), "mixColumns failed")
-            let inverted = aes.invMixColumns(mixed)
-            XCTAssertTrue(compareMatrix(input, b: inverted), "invMixColumns failed")
-        } else {
-            XCTAssert(false, "")
-        }
+    func testAES_encrypt_performance() {
+        let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
+        let iv:[UInt8] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
+        let message = [UInt8](count: 1024 * 1024, repeatedValue: 7)
+        let aes = AES(key: key, iv: iv, blockMode: .CBC)
+        measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, forBlock: { () -> Void in
+            try! aes?.encrypt(message, padding: PKCS7())
+        })
     }
-    
-    func testAESPerformance() {
+
+    func testAES_decrypt_performance() {
         let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:[UInt8] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
         let message = [UInt8](count: 1024 * 1024, repeatedValue: 7)
+        let aes = AES(key: key, iv: iv, blockMode: .CBC)
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true, forBlock: { () -> Void in
-            try! AES(key: key, iv: iv, blockMode: .CBC)?.encrypt(message, padding: PKCS7())
+            try! aes?.decrypt(message, padding: PKCS7())
         })
     }
-    
+
     func testAESPerformanceCommonCrypto() {
         let key:[UInt8] = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
         let iv:[UInt8] = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]

Some files were not shown because too many files changed in this diff