SoftEtherCrypto.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Foundation
  2. import CryptoKit
  3. /// Handles encryption operations for SoftEther protocol
  4. class SoftEtherCrypto {
  5. // MARK: - Constants
  6. private enum Constants {
  7. static let sha1Size = 20
  8. static let md5Size = 16
  9. }
  10. // MARK: - Public Methods
  11. /// Generate secure random bytes
  12. /// - Parameter count: Number of random bytes to generate
  13. /// - Returns: Data containing random bytes
  14. static func randomBytes(count: Int) -> Data {
  15. var data = Data(count: count)
  16. _ = data.withUnsafeMutableBytes {
  17. SecRandomCopyBytes(kSecRandomDefault, count, $0.baseAddress!)
  18. }
  19. return data
  20. }
  21. /// Calculate SHA-1 hash
  22. /// - Parameter data: Input data
  23. /// - Returns: SHA-1 hash of the input data
  24. static func sha1(_ data: Data) -> Data {
  25. let digest = SHA1.hash(data: data)
  26. return Data(digest)
  27. }
  28. /// Calculate MD5 hash
  29. /// - Parameter data: Input data
  30. /// - Returns: MD5 hash of the input data
  31. static func md5(_ data: Data) -> Data {
  32. let digest = Insecure.MD5.hash(data: data)
  33. return Data(digest)
  34. }
  35. /// Encrypt data using RC4 algorithm (for SoftEther compatibility)
  36. /// - Parameters:
  37. /// - data: Data to encrypt
  38. /// - key: Encryption key
  39. /// - Returns: Encrypted data
  40. static func rc4Encrypt(data: Data, key: Data) -> Data {
  41. let rc4 = RC4(key: key)
  42. return rc4.process(data)
  43. }
  44. /// Decrypt data using RC4 algorithm (for SoftEther compatibility)
  45. /// - Parameters:
  46. /// - data: Data to decrypt
  47. /// - key: Decryption key
  48. /// - Returns: Decrypted data
  49. static func rc4Decrypt(data: Data, key: Data) -> Data {
  50. // RC4 is symmetric, so encryption and decryption are the same operation
  51. return rc4Encrypt(data: data, key: key)
  52. }
  53. }
  54. /// Simple RC4 implementation for SoftEther compatibility
  55. /// Note: RC4 is considered insecure, but SoftEther uses it in parts of its protocol
  56. private class RC4 {
  57. private var state: [UInt8]
  58. init(key: Data) {
  59. state = Array(0...255)
  60. var j: Int = 0
  61. // Key scheduling algorithm
  62. for i in 0..<256 {
  63. let keyByte = key[i % key.count]
  64. j = (j + Int(state[i]) + Int(keyByte)) & 0xFF
  65. state.swapAt(i, j)
  66. }
  67. }
  68. func process(_ data: Data) -> Data {
  69. var result = Data(count: data.count)
  70. var i: Int = 0
  71. var j: Int = 0
  72. // Generate keystream and XOR with plaintext
  73. for k in 0..<data.count {
  74. i = (i + 1) & 0xFF
  75. j = (j + Int(state[i])) & 0xFF
  76. state.swapAt(i, j)
  77. let keyStreamByte = state[(Int(state[i]) + Int(state[j])) & 0xFF]
  78. result[k] = data[k] ^ keyStreamByte
  79. }
  80. return result
  81. }
  82. }