Ver Fonte

Merge branch 'master' into swift3

Marcin Krzyżanowski há 9 anos atrás
pai
commit
c8fd09f19e

+ 10 - 0
CHANGELOG

@@ -1,3 +1,13 @@
+0.5.2
+- Fix AES-CTR incremental updates. #287
+- Fixed PBKDF2 tests. #295
+- Fixed assertion check in PKCS7. #288
+- Updatable protocol accept SequenceType in place of Array
+
+0.5.1
+- Fixed PBKDF2 not taking key length parameter into account
+- Switch to Array<> in code
+
 0.5
 0.5
 - Added PBKDF1 https://tools.ietf.org/html/rfc2898#section-5.1
 - Added PBKDF1 https://tools.ietf.org/html/rfc2898#section-5.1
 - Added PBKDF2 https://tools.ietf.org/html/rfc2898#section-5.2
 - Added PBKDF2 https://tools.ietf.org/html/rfc2898#section-5.2

+ 3 - 3
CryptoSwift.playground/Contents.swift

@@ -11,7 +11,7 @@ import Foundation
  Encrypt all data at once.
  Encrypt all data at once.
  */
  */
 do {
 do {
-    let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap")
+    let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap") // aes128
     let ciphertext = try aes.encrypt("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8.map({$0}))
     let ciphertext = try aes.encrypt("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8.map({$0}))
     print(ciphertext.toHexString())
     print(ciphertext.toHexString())
 } catch {
 } catch {
@@ -72,8 +72,8 @@ do {
     while (inputStream.hasBytesAvailable) {
     while (inputStream.hasBytesAvailable) {
         let readCount = inputStream.read(&buffer, maxLength: buffer.count)
         let readCount = inputStream.read(&buffer, maxLength: buffer.count)
         if (readCount > 0) {
         if (readCount > 0) {
-            try encryptor.update(withBytes: Array(buffer[0..<readCount])) { (bytes) in
-                writeToStream(stream: outputStream, bytes: bytes)
+            try encryptor.update(withBytes: buffer[0..<readCount]) { (bytes) in
+                writeToStream(outputStream, bytes: bytes)
             }
             }
         }
         }
     }
     }

+ 2 - 2
CryptoSwift.podspec

@@ -1,7 +1,7 @@
 Pod::Spec.new do |s|
 Pod::Spec.new do |s|
   s.name         = "CryptoSwift"
   s.name         = "CryptoSwift"
-  s.version      = "0.5"
-  s.summary      = "Cryptography in Swift. SHA, MD5, CRC, PBKDF2, Poly1305, HMAC, ChaCha20, Rabbit, AES."
+  s.version      = "0.5.2"
+  s.summary      = "Cryptography in Swift. SHA, MD5, CRC, PBKDF, Poly1305, HMAC, ChaCha20, Rabbit, AES."
   s.description  = "Cryptography functions and helpers for Swift implemented in Swift. SHA, MD5, PBKDF1, PBKDF2, CRC, Poly1305, HMAC, ChaCha20, Rabbit, AES."
   s.description  = "Cryptography functions and helpers for Swift implemented in Swift. SHA, MD5, PBKDF1, PBKDF2, CRC, Poly1305, HMAC, ChaCha20, Rabbit, AES."
   s.homepage     = "https://github.com/krzyzanowskim/CryptoSwift"
   s.homepage     = "https://github.com/krzyzanowskim/CryptoSwift"
   s.license      = {:type => "Attribution License", :file => "LICENSE"}
   s.license      = {:type => "Attribution License", :file => "LICENSE"}

+ 31 - 6
CryptoSwiftTests/AESTests.swift

@@ -85,9 +85,9 @@ final class AESTests: XCTestCase {
 
 
         var ciphertext = Array<UInt8>()
         var ciphertext = Array<UInt8>()
         var encryptor = aes.makeEncryptor()
         var encryptor = aes.makeEncryptor()
-        ciphertext += try! encryptor.update(withBytes: Array(plaintext[0..<8]))
-        ciphertext += try! encryptor.update(withBytes: Array(plaintext[8..<16]))
-        ciphertext += try! encryptor.update(withBytes: Array(plaintext[16..<32]))
+        ciphertext += try! encryptor.update(withBytes: plaintext[0..<8])
+        ciphertext += try! encryptor.update(withBytes: plaintext[8..<16])
+        ciphertext += try! encryptor.update(withBytes: plaintext[16..<32])
         ciphertext += try! encryptor.finish()
         ciphertext += try! encryptor.finish()
         XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
         XCTAssertEqual(try! aes.encrypt(plaintext), ciphertext, "encryption failed")
     }
     }
@@ -115,9 +115,9 @@ final class AESTests: XCTestCase {
         let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
         let aes = try! AES(key: key, iv:iv, blockMode: .CBC, padding: PKCS7())
         var plaintext = Array<UInt8>()
         var plaintext = Array<UInt8>()
         var decryptor = aes.makeDecryptor()
         var decryptor = aes.makeDecryptor()
-        plaintext += try! decryptor.update(withBytes: Array(ciphertext[0..<8]))
-        plaintext += try! decryptor.update(withBytes: Array(ciphertext[8..<16]))
-        plaintext += try! decryptor.update(withBytes: Array(ciphertext[16..<32]))
+        plaintext += try! decryptor.update(withBytes: ciphertext[0..<8])
+        plaintext += try! decryptor.update(withBytes: ciphertext[8..<16])
+        plaintext += try! decryptor.update(withBytes: ciphertext[16..<32])
         plaintext += try! decryptor.finish()
         plaintext += try! decryptor.finish()
         XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
         XCTAssertEqual(try! aes.decrypt(ciphertext), plaintext, "encryption failed")
     }
     }
@@ -276,6 +276,31 @@ final class AESTests: XCTestCase {
         part4 += try! decryptor.finish()
         part4 += try! decryptor.finish()
         XCTAssertEqual(part4, Array(plaintext[0..<80]), "seek decryption failed")
         XCTAssertEqual(part4, Array(plaintext[0..<80]), "seek decryption failed")
     }
     }
+    
+    // https://github.com/krzyzanowskim/CryptoSwift/pull/289
+    func testAES_encrypt_ctr_irregular_length_incremental_update() {
+        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
+        let iv:Array<UInt8> = [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff]
+        let plaintext:Array<UInt8> = [0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01,0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0x01]
+        let expected:Array<UInt8> = [0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,0x1b,0xef,0x68,0x64,0x99,0xd,0xb6,0xce,0x37,0x40,0xbd,0x82,0x85,0x5d,0x11,0xfc,0x8e,0x49,0x4a,0xa9,0xed,0x23,0xe0,0xb9,0x40,0x2d]
+
+        let aes = try! AES(key: key, iv:iv, blockMode: .CTR, padding: NoPadding())
+        var encryptor = aes.makeEncryptor()
+        var encrypted = Array<UInt8>()
+        encrypted += try! encryptor.update(withBytes: plaintext[0..<5])
+        encrypted += try! encryptor.update(withBytes: plaintext[5..<15])
+        encrypted += try! encryptor.update(withBytes: plaintext[15..<plaintext.count])
+        encrypted += try! encryptor.finish()
+        XCTAssertEqual(encrypted, expected, "encryption failed")
+        
+        var decryptor = aes.makeDecryptor()
+        var decrypted = Array<UInt8>()
+        decrypted += try! decryptor.update(withBytes: expected[0..<5])
+        decrypted += try! decryptor.update(withBytes: expected[5..<15])
+        decrypted += try! decryptor.update(withBytes: expected[15..<plaintext.count])
+        decrypted += try! decryptor.finish()
+        XCTAssertEqual(decrypted, plaintext, "decryption failed")
+    }
 
 
 //    func testAES_encrypt_performance() {
 //    func testAES_encrypt_performance() {
 //        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
 //        let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];

+ 4 - 0
README.md

@@ -2,6 +2,10 @@
 
 
 Crypto related functions and helpers for [Swift](https://developer.apple.com/swift/) implemented in Swift. ([#PureSwift](https://twitter.com/hashtag/pureswift))
 Crypto related functions and helpers for [Swift](https://developer.apple.com/swift/) implemented in Swift. ([#PureSwift](https://twitter.com/hashtag/pureswift))
 
 
+## Swift 3
+
+You can find preliminary Swift 3 support from branch [swift3](https://github.com/krzyzanowskim/CryptoSwift/tree/swift3).
+
 #Table of Contents
 #Table of Contents
 - [Requirements](#requirements)
 - [Requirements](#requirements)
 - [Features](#features)
 - [Features](#features)

+ 2 - 2
Sources/CryptoSwift/AES.swift

@@ -403,7 +403,7 @@ extension AES {
             self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired)
             self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired)
         }
         }
 
 
-        mutating public func update(withBytes bytes:Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
+        mutating public func update<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> {
             self.accumulated += bytes
             self.accumulated += bytes
 
 
             if isLast {
             if isLast {
@@ -448,7 +448,7 @@ extension AES {
             self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired);
             self.paddingRequired = aes.blockMode.options.contains(.PaddingRequired);
         }
         }
 
 
-        mutating public func update(withBytes bytes:Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
+        mutating public func update<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false) throws -> Array<UInt8> {
             // prepend "offset" number of bytes at the begining
             // prepend "offset" number of bytes at the begining
             if self.offset > 0 {
             if self.offset > 0 {
                 self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
                 self.accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes

+ 1 - 1
Sources/CryptoSwift/Info.plist

@@ -15,7 +15,7 @@
 	<key>CFBundlePackageType</key>
 	<key>CFBundlePackageType</key>
 	<string>FMWK</string>
 	<string>FMWK</string>
 	<key>CFBundleShortVersionString</key>
 	<key>CFBundleShortVersionString</key>
-	<string>0.5</string>
+	<string>0.5.2</string>
 	<key>CFBundleSignature</key>
 	<key>CFBundleSignature</key>
 	<string>????</string>
 	<string>????</string>
 	<key>CFBundleVersion</key>
 	<key>CFBundleVersion</key>

+ 15 - 7
Sources/CryptoSwift/Updatable.swift

@@ -12,7 +12,7 @@ public protocol Updatable {
     /// - parameter bytes: Bytes to process
     /// - parameter bytes: Bytes to process
     /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
     /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
     /// - returns: Processed data or empty array.
     /// - returns: Processed data or empty array.
-    mutating func update(withBytes bytes:Array<UInt8>, isLast: Bool) throws -> Array<UInt8>
+    mutating func update<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, isLast: Bool) throws -> Array<UInt8>
 
 
     /// Update given bytes in chunks.
     /// Update given bytes in chunks.
     ///
     ///
@@ -20,36 +20,44 @@ public protocol Updatable {
     /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
     /// - parameter isLast: (Optional) Given chunk is the last one. No more updates after this call.
     /// - parameter output: Resulting data
     /// - parameter output: Resulting data
     /// - returns: Processed data or empty array.
     /// - returns: Processed data or empty array.
-    mutating func update(withBytes bytes:Array<UInt8>, isLast: Bool, output: (Array<UInt8>) -> Void) throws
+    mutating func update<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, isLast: Bool, output: (Array<UInt8>) -> Void) throws
 
 
     /// Finish updates. This may apply padding.
     /// Finish updates. This may apply padding.
     /// - parameter bytes: Bytes to process
     /// - parameter bytes: Bytes to process
     /// - returns: Processed data.
     /// - returns: Processed data.
-    mutating func finish(withBytes bytes:Array<UInt8>) throws -> Array<UInt8>
+    mutating func finish<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T) throws -> Array<UInt8>
 
 
     /// Finish updates. This may apply padding.
     /// Finish updates. This may apply padding.
     /// - parameter bytes: Bytes to process
     /// - parameter bytes: Bytes to process
     /// - parameter output: Resulting data
     /// - parameter output: Resulting data
     /// - returns: Processed data.
     /// - returns: Processed data.
-    mutating func finish(withBytes bytes:Array<UInt8>, output: (Array<UInt8>) -> Void) throws
+    mutating func finish<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, output: (Array<UInt8>) -> Void) throws
 }
 }
 
 
 extension Updatable {
 extension Updatable {
-    mutating public func update(withBytes bytes:Array<UInt8>, isLast: Bool = false, output: (Array<UInt8>) -> Void) throws {
+    mutating public func update<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, isLast: Bool = false, output: (Array<UInt8>) -> Void) throws {
         let processed = try self.update(withBytes: bytes, isLast: isLast)
         let processed = try self.update(withBytes: bytes, isLast: isLast)
         if (!processed.isEmpty) {
         if (!processed.isEmpty) {
             output(processed)
             output(processed)
         }
         }
     }
     }
 
 
-    mutating public func finish(withBytes bytes:Array<UInt8> = []) throws  -> Array<UInt8> {
+    mutating public func finish<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T) throws -> Array<UInt8> {
         return try self.update(withBytes: bytes, isLast: true)
         return try self.update(withBytes: bytes, isLast: true)
     }
     }
+    
+    mutating public func finish() throws  -> Array<UInt8> {
+        return try self.update(withBytes: [], isLast: true)
+    }
 
 
-    mutating public func finish(withBytes bytes:Array<UInt8> = [], output: (Array<UInt8>) -> Void) throws {
+    mutating public func finish<T: SequenceType where T.Generator.Element == UInt8>(withBytes bytes:T, output: (Array<UInt8>) -> Void) throws {
         let processed = try self.update(withBytes: bytes, isLast: true)
         let processed = try self.update(withBytes: bytes, isLast: true)
         if (!processed.isEmpty) {
         if (!processed.isEmpty) {
             output(processed)
             output(processed)
         }
         }
     }
     }
+    
+    mutating public func finish(output: (Array<UInt8>) -> Void) throws {
+        try self.finish(withBytes: [], output: output)
+    }
 }
 }