mirror of https://github.com/oxen-io/session-ios
Removed duplicate DiffieHellman and FallbackSessionCipher files
parent
251a93e529
commit
ff54cc4030
@ -1 +1 @@
|
||||
Subproject commit 6fae72d48c06c35c8219ebfc58116450c473b8f1
|
||||
Subproject commit 693c9ae5f51386e0570110a98541952bdfd62963
|
@ -1,47 +0,0 @@
|
||||
import CryptoSwift
|
||||
import Curve25519Kit
|
||||
|
||||
@objc public final class DiffieHellman : NSObject {
|
||||
|
||||
@objc public class DiffieHellmanError : NSError { // Not called `Error` for Obj-C interoperablity
|
||||
|
||||
@objc public static let decryptionFailed = DiffieHellmanError(domain: "DiffieHellmanErrorDomain", code: 1, userInfo: [ NSLocalizedDescriptionKey : "Couldn't decrypt data." ])
|
||||
}
|
||||
|
||||
public static let ivLength: Int32 = 16;
|
||||
|
||||
private override init() { }
|
||||
|
||||
public static func encrypt(_ plainTextData: Data, using symmetricKey: Data) throws -> Data {
|
||||
let iv = Randomness.generateRandomBytes(ivLength)!
|
||||
let ivBytes = [UInt8](iv)
|
||||
let symmetricKeyBytes = [UInt8](symmetricKey)
|
||||
let messageBytes = [UInt8](plainTextData)
|
||||
let blockMode = CBC(iv: ivBytes)
|
||||
let aes = try AES(key: symmetricKeyBytes, blockMode: blockMode)
|
||||
let cipherText = try aes.encrypt(messageBytes)
|
||||
let ivAndCipher = ivBytes + cipherText
|
||||
return Data(bytes: ivAndCipher, count: ivAndCipher.count)
|
||||
}
|
||||
|
||||
public static func encrypt(_ plainTextData: Data, publicKey: Data, privateKey: Data) throws -> Data {
|
||||
let symmetricKey = try Curve25519.generateSharedSecret(fromPublicKey: publicKey, privateKey: privateKey)
|
||||
return try encrypt(plainTextData, using: symmetricKey)
|
||||
}
|
||||
|
||||
public static func decrypt(_ encryptedData: Data, using symmetricKey: Data) throws -> Data {
|
||||
let symmetricKeyBytes = [UInt8](symmetricKey)
|
||||
guard encryptedData.count >= ivLength else { throw DiffieHellmanError.decryptionFailed }
|
||||
let ivBytes = [UInt8](encryptedData[..<ivLength])
|
||||
let cipherBytes = [UInt8](encryptedData[ivLength...])
|
||||
let blockMode = CBC(iv: ivBytes)
|
||||
let aes = try AES(key: symmetricKeyBytes, blockMode: blockMode)
|
||||
let decrypted = try aes.decrypt(cipherBytes)
|
||||
return Data(bytes: decrypted, count: decrypted.count)
|
||||
}
|
||||
|
||||
public static func decrypt(_ encryptedData: Data, publicKey: Data, privateKey: Data) throws -> Data {
|
||||
let symmetricKey = try Curve25519.generateSharedSecret(fromPublicKey: publicKey, privateKey: privateKey)
|
||||
return try decrypt(encryptedData, using: symmetricKey)
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
import CryptoSwift
|
||||
import Curve25519Kit
|
||||
|
||||
private extension String {
|
||||
|
||||
// Convert hex string to Data
|
||||
fileprivate var hexData: Data {
|
||||
var hex = self
|
||||
var data = Data()
|
||||
while(hex.count > 0) {
|
||||
let subIndex = hex.index(hex.startIndex, offsetBy: 2)
|
||||
let c = String(hex[..<subIndex])
|
||||
hex = String(hex[subIndex...])
|
||||
var ch: UInt32 = 0
|
||||
Scanner(string: c).scanHexInt32(&ch)
|
||||
var char = UInt8(ch)
|
||||
data.append(&char, count: 1)
|
||||
}
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
/// A fallback session cipher which uses the the recipients public key to encrypt data
|
||||
@objc public final class FallBackSessionCipher : NSObject {
|
||||
// The pubkey hex string of the recipient
|
||||
private let recipientId: String
|
||||
// The identity manager
|
||||
private let identityKeyStore: OWSIdentityManager
|
||||
|
||||
// The length of the iv
|
||||
private let ivLength: Int32 = 16;
|
||||
|
||||
// The pubkey representation of the hex id
|
||||
private lazy var recipientPubKey: Data = {
|
||||
var recipientId = self.recipientId
|
||||
|
||||
// We need to check here if the id is prefix with '05'
|
||||
// We only need to do this if the length is 66
|
||||
if (recipientId.count == 66 && recipientId.hasPrefix("05")) {
|
||||
recipientId = recipientId.substring(from: 2)
|
||||
}
|
||||
|
||||
return recipientId.hexData
|
||||
}()
|
||||
|
||||
// Our identity key
|
||||
private lazy var userIdentityKeyPair: ECKeyPair? = identityKeyStore.identityKeyPair()
|
||||
|
||||
// A symmetric key used for encryption and decryption
|
||||
private lazy var symmetricKey: Data? = {
|
||||
guard let userIdentityKeyPair = userIdentityKeyPair else { return nil }
|
||||
|
||||
return try? Curve25519.generateSharedSecret(fromPublicKey: recipientPubKey, privateKey: userIdentityKeyPair.privateKey)
|
||||
}()
|
||||
|
||||
/// Create a FallBackSessionCipher.
|
||||
/// This is a very basic cipher and should only be used in special cases such as Friend Requests.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - recipientId: The pubkey string of the recipient
|
||||
/// - identityKeyStore: The identity manager
|
||||
@objc public init(recipientId: String, identityKeyStore: OWSIdentityManager) {
|
||||
self.recipientId = recipientId
|
||||
self.identityKeyStore = identityKeyStore
|
||||
super.init()
|
||||
}
|
||||
|
||||
/// Encrypt a message
|
||||
///
|
||||
/// - Parameter message: The message to encrypt
|
||||
/// - Returns: The encypted message or `nil` if it failed
|
||||
@objc public func encrypt(message: Data) -> Data? {
|
||||
guard let symmetricKey = symmetricKey else { return nil }
|
||||
do {
|
||||
return try DiffieHellman.encrypt(message, using: symmetricKey)
|
||||
} catch {
|
||||
Logger.warn("FallBackSessionCipher: Failed to encrypt message")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypt a message
|
||||
///
|
||||
/// - Parameter message: The message to decrypt
|
||||
/// - Returns: The decrypted message or `nil` if it failed
|
||||
@objc public func decrypt(message: Data) -> Data? {
|
||||
guard let symmetricKey = symmetricKey else { return nil }
|
||||
do {
|
||||
return try DiffieHellman.decrypt(message, using: symmetricKey)
|
||||
} catch {
|
||||
Logger.warn("FallBackSessionCipher: Failed to decrypt message")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue