KeyDerivation.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // KeyDerivation.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePassKit is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Crypto
  19. import Foundation
  20. typealias KeyDerivationParameters = [String: Variant]
  21. extension AESKeyDerivation {
  22. static let UUID = Foundation.UUID(uuid: (0xC9, 0xD9, 0xF3, 0x9A, 0x62, 0x8A, 0x44, 0x60,
  23. 0xBF, 0x74, 0x0D, 0x08, 0xC1, 0x8A, 0x4F, 0xEA))
  24. static let TransformSeedKey = "S"
  25. static let TransformRoundsKey = "R"
  26. convenience init(parameters: [String: Variant]) throws {
  27. guard
  28. let seed = parameters[AESKeyDerivation.TransformSeedKey],
  29. let rounds = parameters[AESKeyDerivation.TransformRoundsKey]
  30. else { throw KDBXError.corruptedDatabase }
  31. try self.init(seed: try seed.unwrap(),
  32. rounds: try rounds.unwrap())
  33. }
  34. }
  35. extension Argon2 {
  36. static let UUID = Foundation.UUID(uuid: (0xEF, 0x63, 0x6D, 0xDF, 0x8C, 0x29, 0x44, 0x4B,
  37. 0x91, 0xF7, 0xA9, 0xA4, 0x03, 0xE3, 0x0A, 0x0C))
  38. static let SaltKey = "S"
  39. static let ParallelismKey = "P"
  40. static let MemoryKey = "M"
  41. static let IterationsKey = "I"
  42. static let VersionKey = "V"
  43. convenience init(parameters: [String: Variant]) throws {
  44. guard
  45. let salt = parameters[Argon2.SaltKey],
  46. let parallelism = parameters[Argon2.ParallelismKey],
  47. let memory = parameters[Argon2.MemoryKey],
  48. let iterations = parameters[Argon2.IterationsKey],
  49. let version = parameters[Argon2.VersionKey]
  50. else { throw KDBXError.corruptedDatabase }
  51. self.init(salt: try salt.unwrap(),
  52. parallelism: try parallelism.unwrap(),
  53. memory: UInt32(try memory.unwrap() as UInt64) / 1024,
  54. iterations: UInt32(try iterations.unwrap() as UInt64),
  55. version: try version.unwrap())
  56. }
  57. }