| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import Foundation
- import CryptoKit
- import CommonCrypto
- import Security
- enum PlatformCrypto {
- enum CryptoError: Error { case invalidEnvelope, invalidCiphertext, invalidText, randomFailed, cryptFailed }
- private static let key = Data(SHA256.hash(data: Data("ios-platform-api-v2".utf8)))
- static func encrypt(_ text: String) throws -> String {
- var iv = Data(count: kCCBlockSizeAES128)
- let status = iv.withUnsafeMutableBytes { SecRandomCopyBytes(kSecRandomDefault, $0.count, $0.baseAddress!) }
- guard status == errSecSuccess else { throw CryptoError.randomFailed }
- return try encrypt(text, iv: iv)
- }
- // Injectable IV for interoperability tests. Production callers use encrypt(_:).
- static func encrypt(_ text: String, iv: Data) throws -> String {
- guard iv.count == kCCBlockSizeAES128 else { throw CryptoError.invalidEnvelope }
- let ciphertext = try crypt(Data(text.utf8), iv: iv, operation: CCOperation(kCCEncrypt))
- return "v2:" + (iv + ciphertext).base64EncodedString()
- .replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "")
- }
- static func decrypt(_ envelope: String, maximumEnvelopeBytes: Int = 350_000) throws -> String {
- guard envelope.hasPrefix("v2:"), envelope.utf8.count <= maximumEnvelopeBytes else { throw CryptoError.invalidEnvelope }
- let encoded = String(envelope.dropFirst(3))
- guard !encoded.isEmpty, encoded.utf8.allSatisfy({ (65...90).contains($0) || (97...122).contains($0) || (48...57).contains($0) || $0 == 45 || $0 == 95 }), encoded.count % 4 != 1 else {
- throw CryptoError.invalidEnvelope
- }
- var base64 = encoded.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
- base64 += String(repeating: "=", count: (4 - base64.count % 4) % 4)
- guard let bytes = Data(base64Encoded: base64), bytes.count >= 32, (bytes.count - 16) % 16 == 0 else {
- throw CryptoError.invalidCiphertext
- }
- let plaintext = try crypt(Data(bytes.dropFirst(16)), iv: Data(bytes.prefix(16)), operation: CCOperation(kCCDecrypt))
- guard let result = String(data: plaintext, encoding: .utf8) else { throw CryptoError.invalidText }
- return result
- }
- private static func crypt(_ data: Data, iv: Data, operation: CCOperation) throws -> Data {
- var output = Data(count: data.count + kCCBlockSizeAES128)
- var outputLength = 0
- let capacity = output.count
- let status = output.withUnsafeMutableBytes { outputBytes in
- data.withUnsafeBytes { inputBytes in
- key.withUnsafeBytes { keyBytes in
- iv.withUnsafeBytes { ivBytes in
- CCCrypt(operation, CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding),
- keyBytes.baseAddress, key.count, ivBytes.baseAddress,
- inputBytes.baseAddress, data.count, outputBytes.baseAddress, capacity, &outputLength)
- }
- }
- }
- }
- guard status == kCCSuccess else { throw CryptoError.cryptFailed }
- return Data(output.prefix(outputLength))
- }
- }
|