mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			100 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			100 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | import Clibsodium | ||
|  | import Sodium | ||
|  | 
 | ||
|  | extension Sign { | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      Converts an Ed25519 public key to an X25519 public key. | ||
|  |      - Parameter ed25519PublicKey: The Ed25519 public key to convert. | ||
|  |      - Returns: The X25519 public key if conversion is successful. | ||
|  |      */ | ||
|  |     public func toX25519(ed25519PublicKey: PublicKey) -> PublicKey? { | ||
|  |         var x25519PublicKey = PublicKey(repeating: 0, count: 32) | ||
|  | 
 | ||
|  |         // FIXME: It'd be nice to check the exit code here, but all the properties of the object | ||
|  |         // returned by the call below are internal. | ||
|  |         let _ = crypto_sign_ed25519_pk_to_curve25519 ( | ||
|  |             &x25519PublicKey, | ||
|  |             ed25519PublicKey | ||
|  |         ) | ||
|  | 
 | ||
|  |         return x25519PublicKey | ||
|  |     } | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      Converts an Ed25519 secret key to an X25519 secret key. | ||
|  |      - Parameter ed25519SecretKey: The Ed25519 secret key to convert. | ||
|  |      - Returns: The X25519 secret key if conversion is successful. | ||
|  |      */ | ||
|  |     public func toX25519(ed25519SecretKey: SecretKey) -> SecretKey? { | ||
|  |         var x25519SecretKey = SecretKey(repeating: 0, count: 32) | ||
|  | 
 | ||
|  |         // FIXME: It'd be nice to check the exit code here, but all the properties of the object | ||
|  |         // returned by the call below are internal. | ||
|  |         let _ = crypto_sign_ed25519_sk_to_curve25519 ( | ||
|  |             &x25519SecretKey, | ||
|  |             ed25519SecretKey | ||
|  |         ) | ||
|  | 
 | ||
|  |         return x25519SecretKey | ||
|  |     } | ||
|  | } | ||
| 
											4 years ago
										 | 
 | ||
|  | extension Sodium { | ||
| 
											4 years ago
										 |     public typealias SharedSecret = Data | ||
| 
											4 years ago
										 |      | ||
|  |     private static let publicKeyBytes: Int = Int(crypto_scalarmult_bytes()) | ||
|  |     private static let sharedSecretBytes: Int = Int(crypto_scalarmult_bytes()) | ||
|  |      | ||
| 
											4 years ago
										 |     public func sharedSecret(_ firstKeyBytes: [UInt8], _ secondKeyBytes: [UInt8]) -> SharedSecret? { | ||
|  |         guard firstKeyBytes.count == Sodium.publicKeyBytes && secondKeyBytes.count == Sodium.publicKeyBytes else { | ||
|  |             return nil | ||
|  |         } | ||
| 
											4 years ago
										 |          | ||
|  |         let sharedSecretPtr: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: Sodium.sharedSecretBytes) | ||
| 
											4 years ago
										 |         let result = secondKeyBytes.withUnsafeBytes { (secondKeyPtr: UnsafeRawBufferPointer) -> Int32 in | ||
|  |             return firstKeyBytes.withUnsafeBytes { (firstKeyPtr: UnsafeRawBufferPointer) -> Int32 in | ||
|  |                 guard let firstKeyBaseAddress: UnsafePointer<UInt8> = firstKeyPtr.baseAddress?.assumingMemoryBound(to: UInt8.self) else { | ||
|  |                     return -1 | ||
|  |                 } | ||
|  |                 guard let secondKeyBaseAddress: UnsafePointer<UInt8> = secondKeyPtr.baseAddress?.assumingMemoryBound(to: UInt8.self) else { | ||
| 
											4 years ago
										 |                     return -1 | ||
|  |                 } | ||
|  |                  | ||
| 
											4 years ago
										 |                 return crypto_scalarmult(sharedSecretPtr, firstKeyBaseAddress, secondKeyBaseAddress) | ||
| 
											4 years ago
										 |             } | ||
|  |         } | ||
|  |          | ||
|  |         guard result == 0 else { return nil } | ||
|  |          | ||
|  |         return Data(bytes: sharedSecretPtr, count: Sodium.sharedSecretBytes) | ||
|  |     } | ||
|  | } | ||
| 
											4 years ago
										 | 
 | ||
|  | extension GenericHash { | ||
|  |     public func hashSaltPersonal( | ||
|  |         message: Bytes, | ||
|  |         outputLength: Int, | ||
|  |         key: Bytes? = nil, | ||
|  |         salt: Bytes, | ||
|  |         personal: Bytes | ||
|  |     ) -> Bytes? { | ||
|  |         var output: [UInt8] = [UInt8](repeating: 0, count: outputLength) | ||
|  |          | ||
|  |         let result = crypto_generichash_blake2b_salt_personal( | ||
|  |             &output, | ||
|  |             outputLength, | ||
|  |             message, | ||
|  |             UInt64(message.count), | ||
|  |             key, | ||
|  |             (key?.count ?? 0), | ||
|  |             salt, | ||
|  |             personal | ||
|  |         ) | ||
|  |          | ||
|  |         guard result == 0 else { return nil } | ||
|  |          | ||
|  |         return output | ||
|  |     } | ||
|  | } |