Non-encrypted confidential information - Hexadecimal - Swift

Non-encrypted confidential information - Hexadecimal - Swift

Need

Secure encryption of confidential information

Context

  • Usage of Swift for iOS and macOS app development
  • Usage of UIKit for building modern and responsive user interfaces

Description

Non compliant code

        import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Here we are converting the confidential information to hexadecimal
        let confidentialInfo = "This is a confidential information"
        let hexConfidentialInfo = confidentialInfo.data(using: .utf8)?.map{ String(format:"%02x", $0) }.joined()
        print(hexConfidentialInfo!)
    }
}
        
        

In the above code, the confidentialInfo string is encoded into hexadecimal format using the data(using: .utf8)?.map{ String(format:"%02x", $0) }.joined() function. The encoded data is then printed on the console using the print() function.

This is a vulnerability because the confidential information, although encoded in hexadecimal, is not encrypted. An attacker who has access to this hexadecimal data can easily decode it back to plain text using any standard hexadecimal decoder. This means that the confidential information is exposed and can be read by anyone who can access the hexadecimal data.

The vulnerability lies in the fact that encoding is not equivalent to encryption. While encoding transforms data into another format, encryption makes data unreadable without a specific key. Therefore, while the data is encoded in the above code, it is not protected from unauthorized access or viewing.

Steps

  • Use encryption algorithms to protect the confidential information instead of encoding it in hexadecimal.
  • Choose a strong encryption algorithm that is appropriate for the sensitivity of the information.
  • Ensure that the encryption key is securely stored and not hardcoded in the code.
  • Implement secure key management practices to protect the encryption key.
  • Use secure communication protocols (e.g., HTTPS) to transmit the encrypted information.
  • Regularly update and patch the encryption libraries and dependencies used in the application.
  • Perform security testing and code reviews to identify and fix any other potential vulnerabilities.

Compliant code

        import UIKit
import CommonCrypto

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Here we are converting the confidential information to hexadecimal
        let confidentialInfo = "This is a confidential information"
        let encryptedConfidentialInfo = self.encryptAESData(confidentialInfo)
        print(encryptedConfidentialInfo)
    }
    
    func encryptAESData(_ data: String) -> String {
        let keyData = "yourEncryptionKey".data(using: .utf8)!
        let ivData = "yourIV".data(using: .utf8)!
        let data = data.data(using: .utf8)!
        let cryptData    = NSMutableData(length: Int((data.count)) + kCCBlockSizeAES128)!
        
        let keyLength              = size_t(kCCKeySizeAES128)
        let operation: CCOperation = UInt32(kCCEncrypt)
        let algorithm:  CCAlgorithm = UInt32(kCCAlgorithmAES)
        let options:   CCOptions   = UInt32(kCCOptionPKCS7Padding + kCCOptionECBMode)
        
        var numBytesEncrypted :size_t = 0
        
        let cryptStatus = CCCrypt(operation,
                                  algorithm,
                                  options,
                                  (keyData as NSData).bytes, keyLength,
                                  (ivData as NSData).bytes,
                                  (data as NSData).bytes, data.count,
                                  cryptData.mutableBytes, cryptData.length,
                                  &numBytesEncrypted)
        
        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            cryptData.length = Int(numBytesEncrypted)
            let base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
            return base64cryptString
        } else {
            return ""
        }
    }
}
        
        

The above code encrypts the confidential information using the AES encryption algorithm. The encryptAESData function takes a string as input and returns the encrypted string. The encryption key and initialization vector (IV) are provided as inputs to the encryption function.

The CCCrypt function is used to perform the encryption. It takes several parameters including the operation (encryption or decryption), the encryption algorithm (AES), the encryption options (PKCS7Padding and ECBMode), the encryption key, the initialization vector, the data to be encrypted, and the output buffer for the encrypted data.

The encrypted data is then converted to a base64 string for easier handling. The base64 string is returned by the function.

Please note that the encryption key and IV are hardcoded in this example for simplicity. In a real-world application, you should securely generate and store these values. You should also handle any errors that may occur during the encryption process.

References