Skip to content

Commit

Permalink
Swift 5 update
Browse files Browse the repository at this point in the history
- protobuf files regenerated
- Updated deprecated APIs
  • Loading branch information
christophhagen committed Apr 18, 2019
1 parent 5793223 commit acfe723
Show file tree
Hide file tree
Showing 18 changed files with 774 additions and 872 deletions.
4 changes: 2 additions & 2 deletions LibSignalProtocolSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Pod::Spec.new do |spec|
spec.summary = 'A Swift implementation of the Signal Protocol'
spec.license = 'MIT'

spec.version = '1.2.1'
spec.version = '1.3'
spec.source = {
:git => 'https://github.com/christophhagen/LibSignalProtocolSwift.git',
:tag => spec.version
}
spec.swift_version = '4.1'
spec.swift_version = '5.0'
spec.module_name = 'SignalProtocol'

spec.authors = { 'Christoph Hagen' => '[email protected]' }
Expand Down
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ abstract_target 'LibSignalProtocolSwift' do
# Pods for LibSignalProtocolSwift

# Protocol Buffers in Swift
pod 'SwiftProtobuf'
pod 'SwiftProtobuf', '~> 1.5.0'

# Elliptic Curve functions
pod 'Curve25519'
pod 'Curve25519', '~> 1.1'

# Cryptographic functions powered by CommonCrypto
pod 'CommonCryptoModule', '~> 1.0.2'
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- CommonCryptoModule (1.0.2)
- Curve25519 (1.0)
- SwiftProtobuf (1.0.3)
- Curve25519 (1.1)
- SwiftProtobuf (1.5.0)

DEPENDENCIES:
- CommonCryptoModule (~> 1.0.2)
Expand All @@ -16,8 +16,8 @@ SPEC REPOS:

SPEC CHECKSUMS:
CommonCryptoModule: 63a68b57abed79655b3a01011419bf037a05b0a0
Curve25519: 849314b353906cffa572125f31c2d006ce635f32
SwiftProtobuf: 5ccc0e4054e37c75800e5744acb2aa80bb72b39c
Curve25519: 5f595e906bd962248665978e2992a1fe96e2d831
SwiftProtobuf: 241400280f912735c1e1b9fe675fdd2c6c4d42e2

PODFILE CHECKSUM: b5550621b144cb6064196a28676c938b681e2db2

Expand Down
24 changes: 13 additions & 11 deletions SignalProtocol.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,12 @@
TargetAttributes = {
CEB45A44201CB2E6002BB845 = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 0920;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
CEB45A5A201CB3AE002BB845 = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
CEB45AFC201CC35A002BB845 = {
Expand All @@ -836,6 +837,7 @@
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = CEB45A3B201CB2E6002BB845;
productRefGroup = CEB45A46201CB2E6002BB845 /* Products */;
Expand Down Expand Up @@ -1418,7 +1420,7 @@
PRODUCT_NAME = SignalProtocol;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -1442,7 +1444,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "christophhagen.SignalProtocol-iOS";
PRODUCT_NAME = SignalProtocol;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -1458,7 +1460,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = christophhagen.SignalProtocolTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -1474,7 +1476,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = christophhagen.SignalProtocolTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -1500,7 +1502,7 @@
PRODUCT_NAME = SignalProtocol;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -1525,7 +1527,7 @@
PRODUCT_NAME = SignalProtocol;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -1548,7 +1550,7 @@
PRODUCT_NAME = SignalProtocol;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 3;
};
name = Debug;
Expand All @@ -1572,7 +1574,7 @@
PRODUCT_NAME = SignalProtocol;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 3;
};
name = Release;
Expand All @@ -1597,7 +1599,7 @@
PRODUCT_NAME = SignalProtocol;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 4;
};
name = Debug;
Expand All @@ -1622,7 +1624,7 @@
PRODUCT_NAME = SignalProtocol;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 4;
};
name = Release;
Expand Down
4 changes: 2 additions & 2 deletions SignalProtocol.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Sources/Additional/SignalError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public enum SignalErrorType: String {

/// An invalid key produced an error
case invalidKey = "Invalid key"

case invalidIV = "Invalid iv"

/// The (key) id is invalid
case invalidId = "Invalid Id"
Expand Down
73 changes: 48 additions & 25 deletions Sources/Encryption/SignalCommonCrypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public struct SignalCommonCrypto: SignalCryptoProvider {
let bytes = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
withUnsafeMutablePointer(to: &context) { (ptr: UnsafeMutablePointer<CCHmacContext>) in
// Pointer to salt
salt.withUnsafeBytes {(ptr2: UnsafePointer<UInt8>) in
let saltPtr = UnsafeRawPointer(ptr2)
salt.withUnsafeBytes { ptr2 in
let saltPtr = ptr2.baseAddress!
// Pointer to message
message.withUnsafeBytes {(ptr3: UnsafePointer<UInt8>) in
let messagePtr = UnsafeRawPointer(ptr3)
message.withUnsafeBytes { ptr3 in
let messagePtr = ptr3.baseAddress!
// Authenticate
CCHmacInit(ptr, CCHmacAlgorithm(kCCHmacAlgSHA256), saltPtr, salt.count)
CCHmacUpdate(ptr, messagePtr, message.count)
Expand All @@ -68,38 +68,51 @@ public struct SignalCommonCrypto: SignalCryptoProvider {
- throws: `SignalError.digestError`
*/
public func sha512(for message: Data) throws -> Data {
guard message.count > 0 else {
throw SignalError(.invalidMessage, "Message length is 0")
}
var context = CC_SHA512_CTX()
return try withUnsafeMutablePointer(to: &context) { contextPtr in
CC_SHA512_Init(contextPtr)
// Pointer to message
let result: Int32 = message.withUnsafeBytes {(ptr2: UnsafePointer<UInt8>) in
let messagePtr = UnsafeRawPointer(ptr2)
let result: Int32 = message.withUnsafeBytes {ptr2 in
let messagePtr = ptr2.baseAddress!
return CC_SHA512_Update(contextPtr, messagePtr, CC_LONG(message.count))
}
guard result == 1 else {
throw SignalError(.digestError, "Error on SHA512 Update: \(result)")
}
var md = Data(count: Int(CC_SHA512_DIGEST_LENGTH))
let result2 = md.withUnsafeMutableBytes {
CC_SHA512_Final($0, contextPtr)
let result2: Int32 = md.withUnsafeMutableBytes { ptr4 in
let a = ptr4.baseAddress!.assumingMemoryBound(to: UInt8.self)
return CC_SHA512_Final(a, contextPtr)
}
guard result2 == 1 else {
throw SignalError(.digestError, "Error on SHA512 Final: \(result)")
}
return md
}
}

/**
Encrypt a message with AES
- parameter message: The input message to encrypt
- parameter cipher: THe encryption scheme to use
- parameter key: The key for encryption (`kCCKeySizeAES128` bytes)
- parameter iv: The initialization vector
- parameter key: The key for encryption (`kCCKeySizeAES256` bytes)
- parameter iv: The initialization vector (`kCCBlockSizeAES128` bytes)
- returns: The encrypted message
- throws: `SignalError.encryptionError`
*/
public func encrypt(message: Data, with cipher: SignalEncryptionScheme, key: Data, iv: Data) throws -> Data {
guard key.count == kCCKeySizeAES256 else {
throw SignalError(.invalidKey, "Invalid key length")
}
guard message.count > 0 else {
throw SignalError(.invalidMessage, "Message length is 0")
}
guard iv.count == kCCBlockSizeAES128 else {
throw SignalError(.invalidIV, "The length of the IV is not correct")
}
switch cipher {
case .AES_CBCwithPKCS5:
return try process(cbc: message, key: key, iv: iv, encrypt: true)
Expand All @@ -112,12 +125,21 @@ public struct SignalCommonCrypto: SignalCryptoProvider {
Decrypt a message with AES
- parameter message: The input message to decrypt
- parameter cipher: THe encryption scheme to use
- parameter key: The key for decryption (`kCCKeySizeAES128` bytes)
- parameter iv: The initialization vector
- parameter key: The key for decryption (`kCCKeySizeAES256` bytes)
- parameter iv: The initialization vector (`kCCBlockSizeAES128` bytes)
- returns: The decrypted message
- throws: `SignalError.decryptionError`
*/
public func decrypt(message: Data, with cipher: SignalEncryptionScheme, key: Data, iv: Data) throws -> Data {
guard key.count == kCCKeySizeAES256 else {
throw SignalError(.invalidKey, "Invalid key length")
}
guard message.count > 0 else {
throw SignalError(.invalidMessage, "Message length is 0")
}
guard iv.count == kCCBlockSizeAES128 else {
throw SignalError(.invalidIV, "The length of the IV is not correct")
}
switch cipher {
case .AES_CBCwithPKCS5:
return try process(cbc: message, key: key, iv: iv, encrypt: false)
Expand Down Expand Up @@ -146,14 +168,14 @@ public struct SignalCommonCrypto: SignalCryptoProvider {

var dataOutMoved: Int = 0
// Pointer to key
let status: Int32 = key.withUnsafeBytes { (ptr1: UnsafePointer<UInt8>) in
let keyPtr = UnsafeRawPointer(ptr1)
let status: Int32 = key.withUnsafeBytes { ptr1 in
let keyPtr = ptr1.baseAddress!
// Pointer to IV
return iv.withUnsafeBytes { (ptr2: UnsafePointer<UInt8>) in
let ivPtr = UnsafeRawPointer(ptr2)
return iv.withUnsafeBytes { ptr2 in
let ivPtr = ptr2.baseAddress!
// Pointer to message
return message.withUnsafeBytes { (ptr3: UnsafePointer<UInt8>) in
let messagePtr = UnsafeRawPointer(ptr3)
return message.withUnsafeBytes { ptr3 in
let messagePtr = ptr3.baseAddress!
// Options
let algorithm = CCAlgorithm(kCCAlgorithmAES)
let padding = CCOptions(kCCOptionPKCS7Padding)
Expand Down Expand Up @@ -212,11 +234,12 @@ public struct SignalCommonCrypto: SignalCryptoProvider {
*/
private func process(ctr message: Data, key: Data, iv: Data, encrypt: Bool) throws -> Data {
var cryptoRef: CCCryptorRef? = nil
var status: Int32 = key.withUnsafeBytes { (ptr1: UnsafePointer<UInt8>) in
let keyPtr = UnsafeRawPointer(ptr1)

var status: Int32 = key.withUnsafeBytes { ptr1 in
let keyPtr = ptr1.baseAddress!
// Pointer to IV
return iv.withUnsafeBytes { (ptr2: UnsafePointer<UInt8>) in
let ivPtr = UnsafeRawPointer(ptr2)
return iv.withUnsafeBytes { ptr2 in
let ivPtr = ptr2.baseAddress!
let operation = encrypt ? CCOperation(kCCEncrypt) : CCOperation(kCCDecrypt)
let mode = CCMode(kCCModeCTR)
let algorithm = CCAlgorithm(kCCAlgorithmAES)
Expand Down Expand Up @@ -246,8 +269,8 @@ public struct SignalCommonCrypto: SignalCryptoProvider {

var updateMovedLength = 0
status = withUnsafeMutablePointer(to: &updateMovedLength) { updatedPtr in
message.withUnsafeBytes { (ptr3: UnsafePointer<UInt8>) in
let messagePtr = UnsafeRawPointer(ptr3)
message.withUnsafeBytes { ptr3 in
let messagePtr = ptr3.baseAddress!
return CCCryptorUpdate(ref, messagePtr, message.count, ptr, outputLength, updatedPtr)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Encryption/SignalCrypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public struct SignalCrypto {
*/
static func generateSenderKeyId() throws -> UInt32 {
let data = try random(bytes: 4)
let value: UInt32 = data.withUnsafeBytes { $0.pointee }
let value = data.withUnsafeBytes { $0.baseAddress!.assumingMemoryBound(to: UInt32.self).pointee }
return value & 0x7FFFFFFF
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Info/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.2.1</string>
<string>1.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
Loading

0 comments on commit acfe723

Please sign in to comment.