2022-02-15 06:00:51 +01:00
|
|
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Sodium
|
2022-02-21 00:01:53 +01:00
|
|
|
import Curve25519Kit
|
2023-03-09 23:42:36 +01:00
|
|
|
import SessionUtilitiesKit
|
2022-02-15 06:00:51 +01:00
|
|
|
|
|
|
|
public protocol SodiumType {
|
2022-03-18 06:39:25 +01:00
|
|
|
func getBox() -> BoxType
|
2022-02-15 06:00:51 +01:00
|
|
|
func getGenericHash() -> GenericHashType
|
2022-02-21 00:01:53 +01:00
|
|
|
func getSign() -> SignType
|
2022-03-18 06:39:25 +01:00
|
|
|
func getAeadXChaCha20Poly1305Ietf() -> AeadXChaCha20Poly1305IetfType
|
2022-02-15 06:00:51 +01:00
|
|
|
|
2022-03-18 06:39:25 +01:00
|
|
|
func generateBlindingFactor(serverPublicKey: String, genericHash: GenericHashType) -> Bytes?
|
2023-03-09 23:42:36 +01:00
|
|
|
func blindedKeyPair(serverPublicKey: String, edKeyPair: KeyPair, genericHash: GenericHashType) -> KeyPair?
|
2022-02-21 00:01:53 +01:00
|
|
|
func sogsSignature(message: Bytes, secretKey: Bytes, blindedSecretKey ka: Bytes, blindedPublicKey kA: Bytes) -> Bytes?
|
|
|
|
|
2022-02-25 01:59:29 +01:00
|
|
|
func combineKeys(lhsKeyBytes: Bytes, rhsKeyBytes: Bytes) -> Bytes?
|
|
|
|
func sharedBlindedEncryptionKey(secretKey a: Bytes, otherBlindedPublicKey: Bytes, fromBlindedPublicKey kA: Bytes, toBlindedPublicKey kB: Bytes, genericHash: GenericHashType) -> Bytes?
|
2022-02-25 07:48:09 +01:00
|
|
|
|
2022-03-18 06:39:25 +01:00
|
|
|
func sessionId(_ sessionId: String, matchesBlindedId blindedSessionId: String, serverPublicKey: String, genericHash: GenericHashType) -> Bool
|
2022-02-15 06:00:51 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 08:33:23 +01:00
|
|
|
public protocol AeadXChaCha20Poly1305IetfType {
|
2022-02-25 01:59:29 +01:00
|
|
|
var KeyBytes: Int { get }
|
|
|
|
var ABytes: Int { get }
|
|
|
|
|
|
|
|
func encrypt(message: Bytes, secretKey: Bytes, nonce: Bytes, additionalData: Bytes?) -> Bytes?
|
|
|
|
func decrypt(authenticatedCipherText: Bytes, secretKey: Bytes, nonce: Bytes, additionalData: Bytes?) -> Bytes?
|
2022-02-17 08:33:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 00:01:53 +01:00
|
|
|
public protocol Ed25519Type {
|
2023-03-09 23:42:36 +01:00
|
|
|
func sign(data: Bytes, keyPair: KeyPair) throws -> Bytes?
|
2022-03-11 06:57:28 +01:00
|
|
|
func verifySignature(_ signature: Data, publicKey: Data, data: Data) throws -> Bool
|
2022-02-21 00:01:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 06:39:25 +01:00
|
|
|
public protocol BoxType {
|
|
|
|
func seal(message: Bytes, recipientPublicKey: Bytes) -> Bytes?
|
|
|
|
func open(anonymousCipherText: Bytes, recipientPublicKey: Bytes, recipientSecretKey: Bytes) -> Bytes?
|
2022-02-21 00:01:53 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 06:00:51 +01:00
|
|
|
public protocol GenericHashType {
|
2022-02-17 08:33:23 +01:00
|
|
|
func hash(message: Bytes, key: Bytes?) -> Bytes?
|
|
|
|
func hash(message: Bytes, outputLength: Int) -> Bytes?
|
2022-02-15 06:00:51 +01:00
|
|
|
func hashSaltPersonal(message: Bytes, outputLength: Int, key: Bytes?, salt: Bytes, personal: Bytes) -> Bytes?
|
|
|
|
}
|
|
|
|
|
2022-03-18 06:39:25 +01:00
|
|
|
public protocol SignType {
|
|
|
|
var Bytes: Int { get }
|
|
|
|
var PublicKeyBytes: Int { get }
|
|
|
|
|
|
|
|
func toX25519(ed25519PublicKey: Bytes) -> Bytes?
|
|
|
|
func signature(message: Bytes, secretKey: Bytes) -> Bytes?
|
|
|
|
func verify(message: Bytes, publicKey: Bytes, signature: Bytes) -> Bool
|
|
|
|
}
|
|
|
|
|
2022-02-17 08:33:23 +01:00
|
|
|
// MARK: - Default Values
|
|
|
|
|
2022-03-18 06:39:25 +01:00
|
|
|
extension GenericHashType {
|
|
|
|
func hash(message: Bytes) -> Bytes? { return hash(message: message, key: nil) }
|
|
|
|
|
|
|
|
func hashSaltPersonal(message: Bytes, outputLength: Int, salt: Bytes, personal: Bytes) -> Bytes? {
|
|
|
|
return hashSaltPersonal(message: message, outputLength: outputLength, key: nil, salt: salt, personal: personal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 08:33:23 +01:00
|
|
|
extension AeadXChaCha20Poly1305IetfType {
|
2022-02-25 01:59:29 +01:00
|
|
|
func encrypt(message: Bytes, secretKey: Bytes, nonce: Bytes) -> Bytes? {
|
|
|
|
return encrypt(message: message, secretKey: secretKey, nonce: nonce, additionalData: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrypt(authenticatedCipherText: Bytes, secretKey: Bytes, nonce: Bytes) -> Bytes? {
|
|
|
|
return decrypt(authenticatedCipherText: authenticatedCipherText, secretKey: secretKey, nonce: nonce, additionalData: nil)
|
2022-02-17 08:33:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Conformance
|
|
|
|
|
2022-02-15 06:00:51 +01:00
|
|
|
extension Sodium: SodiumType {
|
2022-03-18 06:39:25 +01:00
|
|
|
public func getBox() -> BoxType { return box }
|
2022-02-15 06:00:51 +01:00
|
|
|
public func getGenericHash() -> GenericHashType { return genericHash }
|
2022-02-21 00:01:53 +01:00
|
|
|
public func getSign() -> SignType { return sign }
|
2022-02-17 08:33:23 +01:00
|
|
|
public func getAeadXChaCha20Poly1305Ietf() -> AeadXChaCha20Poly1305IetfType { return aead.xchacha20poly1305ietf }
|
|
|
|
|
2023-03-09 23:42:36 +01:00
|
|
|
public func blindedKeyPair(serverPublicKey: String, edKeyPair: KeyPair) -> KeyPair? {
|
2022-02-17 08:33:23 +01:00
|
|
|
return blindedKeyPair(serverPublicKey: serverPublicKey, edKeyPair: edKeyPair, genericHash: getGenericHash())
|
|
|
|
}
|
2022-02-15 06:00:51 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 06:39:25 +01:00
|
|
|
extension Box: BoxType {}
|
2022-02-15 06:00:51 +01:00
|
|
|
extension GenericHash: GenericHashType {}
|
2022-03-18 06:39:25 +01:00
|
|
|
extension Sign: SignType {}
|
|
|
|
extension Aead.XChaCha20Poly1305Ietf: AeadXChaCha20Poly1305IetfType {}
|
2022-03-11 06:57:28 +01:00
|
|
|
|
|
|
|
struct Ed25519Wrapper: Ed25519Type {
|
2023-03-09 23:42:36 +01:00
|
|
|
func sign(data: Bytes, keyPair: KeyPair) throws -> Bytes? {
|
2022-06-09 10:37:44 +02:00
|
|
|
let ecKeyPair: ECKeyPair = try ECKeyPair(
|
|
|
|
publicKeyData: Data(keyPair.publicKey),
|
|
|
|
privateKeyData: Data(keyPair.secretKey)
|
|
|
|
)
|
|
|
|
|
|
|
|
return try Ed25519.sign(Data(data), with: ecKeyPair).bytes
|
2022-03-15 05:19:23 +01:00
|
|
|
}
|
|
|
|
|
2022-03-11 06:57:28 +01:00
|
|
|
func verifySignature(_ signature: Data, publicKey: Data, data: Data) throws -> Bool {
|
|
|
|
return try Ed25519.verifySignature(signature, publicKey: publicKey, data: data)
|
|
|
|
}
|
|
|
|
}
|