From 95d98ac5d4ba163968c940c2bf9727c4718d087e Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Tue, 19 Dec 2023 20:39:34 -0500 Subject: [PATCH] Support AES with GCM mode --- Bark/en.lproj/Localizable.strings | 2 +- Bark/tr.lproj/Localizable.strings | 2 +- Bark/zh-Hans.lproj/Localizable.strings | 2 +- Controller/CryptoSettingController.swift | 8 ++++---- Controller/CryptoSettingViewModel.swift | 14 +++++++------- Model/Algorithm.swift | 24 +++++++++++++++++------- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Bark/en.lproj/Localizable.strings b/Bark/en.lproj/Localizable.strings index 109590d4..1c995958 100644 --- a/Bark/en.lproj/Localizable.strings +++ b/Bark/en.lproj/Localizable.strings @@ -129,7 +129,7 @@ exportOrImport = "Export and import messages"; items = "messages"; enterKey = "Please enter %d-bit Key"; -enterIv = "Please enter 16-bit Iv"; +enterIv = "Please enter %d-bit Iv"; encryptionSettings = "Encryption Settings"; algorithm = "Algorithm"; mode = "Mode"; diff --git a/Bark/tr.lproj/Localizable.strings b/Bark/tr.lproj/Localizable.strings index 02f8063e..e7df3771 100644 --- a/Bark/tr.lproj/Localizable.strings +++ b/Bark/tr.lproj/Localizable.strings @@ -129,7 +129,7 @@ exportOrImport = "Mesajları dışa ve içe aktarma"; items = "mesajlar"; enterKey = "Lütfen %d-bit Anahtar girin"; -enterIv = "Lütfen 16 bit Iv girin"; +enterIv = "Lütfen %d-bit Iv girin"; encryptionSettings = "Şifreleme Ayarları"; algorithm = "Algoritma"; mode = "Mod"; diff --git a/Bark/zh-Hans.lproj/Localizable.strings b/Bark/zh-Hans.lproj/Localizable.strings index d6230915..2b0d6a31 100644 --- a/Bark/zh-Hans.lproj/Localizable.strings +++ b/Bark/zh-Hans.lproj/Localizable.strings @@ -129,7 +129,7 @@ exportOrImport = "导出或导入消息列表"; items = "条消息"; enterKey = "请输入%d位Key"; -enterIv = "请输入16位Iv"; +enterIv = "请输入%d位Iv"; encryptionSettings = "加密设置"; algorithm = "算法"; mode = "模式"; diff --git a/Controller/CryptoSettingController.swift b/Controller/CryptoSettingController.swift index 1661f469..3a3ca9f4 100644 --- a/Controller/CryptoSettingController.swift +++ b/Controller/CryptoSettingController.swift @@ -11,7 +11,7 @@ import UIKit class CryptoSettingController: BaseViewController { let algorithmFeild = DropBoxView(values: ["AES128", "AES192", "AES256"]) - let modeFeild = DropBoxView(values: ["CBC", "ECB"]) + let modeFeild = DropBoxView(values: ["CBC", "ECB", "GCM"]) let paddingField = DropBoxView(values: ["pkcs7"]) let keyTextField: BorderTextField = { @@ -24,7 +24,7 @@ class CryptoSettingController: BaseViewController { let ivTextField: BorderTextField = { let textField = BorderTextField(title: "IV") textField.font = UIFont.systemFont(ofSize: 14) - textField.placeholder = NSLocalizedString("enterIv") + textField.placeholder = String(format: NSLocalizedString("enterIv"), 16) return textField }() @@ -222,8 +222,8 @@ class CryptoSettingController: BaseViewController { .drive(self.paddingField.rx.values) .disposed(by: rx.disposeBag) - output.keyLenghtChanged.drive(onNext: { [weak self] keyLenght in - self?.keyTextField.placeholder = String(format: NSLocalizedString("enterKey"), keyLenght) + output.keyLengthChanged.drive(onNext: { [weak self] keyLength in + self?.keyTextField.placeholder = String(format: NSLocalizedString("enterKey"), keyLength) }).disposed(by: rx.disposeBag) output.showSnackbar.drive(onNext: { text in diff --git a/Controller/CryptoSettingViewModel.swift b/Controller/CryptoSettingViewModel.swift index 7389a78e..0297d0c6 100644 --- a/Controller/CryptoSettingViewModel.swift +++ b/Controller/CryptoSettingViewModel.swift @@ -22,7 +22,7 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { let initial: Driver<(algorithmList: [Algorithm], modeList: [String], paddingList: [String], initialFields: CryptoSettingFields?)> let modeListChanged: Driver<[String]> let paddingListChanged: Driver<[String]> - let keyLenghtChanged: Driver + let keyLengthChanged: Driver let showSnackbar: Driver let done: Driver let copy: Driver @@ -56,14 +56,14 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { .compactMap { Algorithm(rawValue: $0) } .map { $0.modes } - let keyLenght = + let keyLength = Driver.merge([ Driver.just(dependencies.settingFieldRelay.value) .compactMap { $0 } - .compactMap { Algorithm(rawValue: $0.algorithm)?.keyLenght }, + .compactMap { Algorithm(rawValue: $0.algorithm)?.keyLength }, input .algorithmChanged - .compactMap { Algorithm(rawValue: $0)?.keyLenght }, + .compactMap { Algorithm(rawValue: $0)?.keyLength }, ]) // 保存配置 @@ -135,13 +135,13 @@ class CryptoSettingViewModel: ViewModel, ViewModelType { return Output( initial: Driver.just(( algorithmList: [Algorithm.aes128, Algorithm.aes192, Algorithm.aes256], - modeList: ["CBC", "ECB"], - paddingList: ["okcs7"], + modeList: ["CBC", "ECB", "GCM"], + paddingList: ["pkcs7"], initialFields: dependencies.settingFieldRelay.value )), modeListChanged: modeList, paddingListChanged: Driver.just(["pkcs7"]), - keyLenghtChanged: keyLenght, + keyLengthChanged: keyLength, showSnackbar: showSnackbar.asDriver(onErrorDriveWith: .empty()), done: done.map { _ in () }, copy: copy diff --git a/Model/Algorithm.swift b/Model/Algorithm.swift index 441258d7..585ef317 100644 --- a/Model/Algorithm.swift +++ b/Model/Algorithm.swift @@ -17,7 +17,7 @@ enum Algorithm: String { var modes: [String] { switch self { case .aes128, .aes192, .aes256: - return ["CBC", "ECB"] + return ["CBC", "ECB", "GCM"] } } @@ -28,7 +28,7 @@ enum Algorithm: String { } } - var keyLenght: Int { + var keyLength: Int { switch self { case .aes128: return 16 @@ -61,17 +61,25 @@ struct AESCryptoModel { throw "Key is missing" } - guard algorithm.keyLenght == key.count else { - throw String(format: NSLocalizedString("enterKey"), algorithm.keyLenght) + guard algorithm.keyLength == key.count else { + throw String(format: NSLocalizedString("enterKey"), algorithm.keyLength) } var iv = "" - if ["CBC"].contains(cryptoFields.mode) { - if let ivField = cryptoFields.iv, ivField.count == 16 { + if ["CBC", "GCM"].contains(cryptoFields.mode) { + var expectIVLength = 0 + if cryptoFields.mode == "CBC" { + expectIVLength = 16 + } + else if cryptoFields.mode == "GCM" { + expectIVLength = 12 + } + + if let ivField = cryptoFields.iv, ivField.count == expectIVLength { iv = ivField } else { - throw NSLocalizedString("enterIv") + throw String(format: NSLocalizedString("enterIv"), expectIVLength) } } @@ -81,6 +89,8 @@ struct AESCryptoModel { mode = CBC(iv: iv.bytes) case "ECB": mode = ECB() + case "GCM": + mode = GCM(iv: iv.bytes) default: throw "Invalid Mode" }