Selaa lähdekoodia

XOR is generic (but local, sic!) and buffer based

Marcin Krzyżanowski 7 vuotta sitten
vanhempi
commit
88edd208fc

+ 4 - 3
CryptoSwift.xcodeproj/project.pbxproj

@@ -345,7 +345,6 @@
 				75EC52521EE8B6CA0048EB3B /* Foundation */,
 				75EC525C1EE8B6CA0048EB3B /* Generics.swift */,
 				75EC525D1EE8B6CA0048EB3B /* HMAC.swift */,
-				75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */,
 				75EC52611EE8B6CA0048EB3B /* MD5.swift */,
 				75EC52621EE8B6CA0048EB3B /* NoPadding.swift */,
 				75EC52631EE8B6CA0048EB3B /* Operators.swift */,
@@ -360,6 +359,7 @@
 				75EC52701EE8B6CA0048EB3B /* SHA2.swift */,
 				75EC52711EE8B6CA0048EB3B /* SHA3.swift */,
 				75EC52721EE8B6CA0048EB3B /* String+Extension.swift */,
+				75EC525F1EE8B6CA0048EB3B /* Int+Extension.swift */,
 				75EC52761EE8B6CA0048EB3B /* UInt8+Extension.swift */,
 				75EC52731EE8B6CA0048EB3B /* UInt16+Extension.swift */,
 				75EC52741EE8B6CA0048EB3B /* UInt32+Extension.swift */,
@@ -689,13 +689,13 @@
 				CODE_SIGNING_REQUIRED = NO;
 				COPY_PHASE_STRIP = NO;
 				CURRENT_PROJECT_VERSION = 1;
+				DEBUG_INFORMATION_FORMAT = dwarf;
 				DEFINES_MODULE = YES;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;
 				GCC_C_LANGUAGE_STANDARD = gnu99;
 				GCC_DYNAMIC_NO_PIC = NO;
 				GCC_NO_COMMON_BLOCKS = YES;
-				GCC_OPTIMIZATION_LEVEL = 0;
 				GCC_PREPROCESSOR_DEFINITIONS = (
 					"DEBUG=1",
 					"$(inherited)",
@@ -750,6 +750,7 @@
 				CODE_SIGNING_REQUIRED = NO;
 				COPY_PHASE_STRIP = YES;
 				CURRENT_PROJECT_VERSION = 1;
+				DEBUG_INFORMATION_FORMAT = dwarf;
 				DEFINES_MODULE = YES;
 				ENABLE_NS_ASSERTIONS = NO;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -897,6 +898,7 @@
 				CODE_SIGNING_REQUIRED = NO;
 				COPY_PHASE_STRIP = NO;
 				CURRENT_PROJECT_VERSION = 1;
+				DEBUG_INFORMATION_FORMAT = dwarf;
 				DEFINES_MODULE = YES;
 				ENABLE_NS_ASSERTIONS = NO;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -939,7 +941,6 @@
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_BITCODE = YES;
 				"ENABLE_BITCODE[sdk=macosx*]" = NO;
-				GCC_GENERATE_DEBUGGING_SYMBOLS = NO;
 				GCC_UNROLL_LOOPS = YES;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				IPHONEOS_DEPLOYMENT_TARGET = 8.0;

+ 1 - 1
Sources/CryptoSwift/BlockMode/CBC.swift

@@ -39,7 +39,7 @@ struct CBCModeWorker: BlockModeWorker {
         guard let plaintext = cipherOperation(ciphertext) else {
             return Array(ciphertext)
         }
-        let result = xor(prev ?? iv, plaintext)
+        let result: Array<UInt8> = xor(prev ?? iv, plaintext)
         prev = ciphertext
         return result
     }

+ 2 - 2
Sources/CryptoSwift/BlockMode/CFB.swift

@@ -39,8 +39,8 @@ struct CFBModeWorker: BlockModeWorker {
         guard let plaintext = cipherOperation(prev ?? iv) else {
             return Array(ciphertext)
         }
-        let result = xor(plaintext, ciphertext)
+        let result: Array<UInt8> = xor(plaintext, ciphertext)
         prev = ciphertext
-        return Array(result)
+        return result
     }
 }

+ 1 - 1
Sources/CryptoSwift/BlockMode/OFB.swift

@@ -39,7 +39,7 @@ struct OFBModeWorker: BlockModeWorker {
         guard let decrypted = cipherOperation(prev ?? iv.slice) else {
             return Array(ciphertext)
         }
-        let plaintext = xor(decrypted, ciphertext)
+        let plaintext: Array<UInt8> = xor(decrypted, ciphertext)
         prev = decrypted.slice
         return plaintext
     }

+ 1 - 1
Sources/CryptoSwift/BlockMode/PCBC.swift

@@ -39,7 +39,7 @@ struct PCBCModeWorker: BlockModeWorker {
         guard let plaintext = cipherOperation(ciphertext) else {
             return Array(ciphertext)
         }
-        let result = xor(prev ?? iv, plaintext)
+        let result: Array<UInt8> = xor(prev ?? iv, plaintext)
         prev = xor(plaintext.slice, ciphertext)
         return result
     }

+ 16 - 18
Sources/CryptoSwift/Utils.swift

@@ -60,31 +60,29 @@ func reversed(_ uint32: UInt32) -> UInt32 {
     return v
 }
 
-func xor(_ a: Array<UInt8>, _ b: Array<UInt8>) -> Array<UInt8> {
-    return xor(a.suffix(from: a.startIndex), b.suffix(from: b.startIndex))
+func xor<T,V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
+    return Array(xor(left, right) as ArraySlice<UInt8>)
 }
 
-func xor(_ a: Array<UInt8>, _ b: ArraySlice<UInt8>) -> Array<UInt8> {
-    return xor(a.suffix(from: a.startIndex), b.suffix(from: b.startIndex))
-}
-
-func xor(_ a: ArraySlice<UInt8>, _ b: Array<UInt8>) -> Array<UInt8> {
-    return xor(a.suffix(from: a.startIndex), b.suffix(from: b.startIndex))
-}
+func xor<T,V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, T.IndexDistance == Int, V.Element == UInt8, V.IndexDistance == Int, V.Index == Int {
+    let length = Swift.min(left.count, right.count)
 
-func xor(_ a: ArraySlice<UInt8>, _ b: ArraySlice<UInt8>) -> Array<UInt8> {
-    return Array(xor(a, b) as ArraySlice<UInt8>)
-}
+    let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
+    buf.initialize(to: 0, count: length)
+    defer {
+        buf.deinitialize()
+        buf.deallocate(capacity: length)
+    }
 
-func xor(_ a: ArraySlice<UInt8>, _ b: ArraySlice<UInt8>) -> ArraySlice<UInt8> {
-    var xored = Array<UInt8>(repeating: 0, count: min(a.count, b.count))
-    for i in 0..<xored.count {
-        xored[xored.startIndex.advanced(by: i)] = a[a.startIndex.advanced(by: i)] ^ b[b.startIndex.advanced(by: i)]
+    // xor
+    for i in 0..<length {
+        buf[i] = left[left.startIndex.advanced(by: i)] ^ right[right.startIndex.advanced(by: i)]
     }
-    // don't want to modify slice in place
-    return xored.slice
+
+    return Array(UnsafeBufferPointer(start: buf, count: length)).slice
 }
 
+
 /**
  ISO/IEC 9797-1 Padding method 2.
  Add a single bit with value 1 to the end of the data.