Skip to content

Commit

Permalink
Support AES with GCM mode
Browse files Browse the repository at this point in the history
  • Loading branch information
harryzcy authored and Finb committed Dec 20, 2023
1 parent 67c9dbb commit 95d98ac
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Bark/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion Bark/tr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion Bark/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ exportOrImport = "导出或导入消息列表";
items = "条消息";

enterKey = "请输入%d位Key";
enterIv = "请输入16位Iv";
enterIv = "请输入%d位Iv";
encryptionSettings = "加密设置";
algorithm = "算法";
mode = "模式";
Expand Down
8 changes: 4 additions & 4 deletions Controller/CryptoSettingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit

class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
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 = {
Expand All @@ -24,7 +24,7 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
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
}()

Expand Down Expand Up @@ -222,8 +222,8 @@ class CryptoSettingController: BaseViewController<CryptoSettingViewModel> {
.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
Expand Down
14 changes: 7 additions & 7 deletions Controller/CryptoSettingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>
let keyLengthChanged: Driver<Int>
let showSnackbar: Driver<String>
let done: Driver<Void>
let copy: Driver<String>
Expand Down Expand Up @@ -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 },
])

// 保存配置
Expand Down Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions Model/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum Algorithm: String {
var modes: [String] {
switch self {
case .aes128, .aes192, .aes256:
return ["CBC", "ECB"]
return ["CBC", "ECB", "GCM"]
}
}

Expand All @@ -28,7 +28,7 @@ enum Algorithm: String {
}
}

var keyLenght: Int {
var keyLength: Int {
switch self {
case .aes128:
return 16
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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"
}
Expand Down

0 comments on commit 95d98ac

Please sign in to comment.