Header.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Header.swift
  2. // This file is part of KeePass.swift
  3. //
  4. // Copyright © 2021 Maxime Epain. All rights reserved.
  5. //
  6. // KeePass.swift 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. // KeePass.swift 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 KeePass.swift. If not, see <https://www.gnu.org/licenses/>.
  18. import Binary
  19. import Crypto
  20. import Foundation
  21. struct Header {
  22. let cipher: Flag
  23. let version: UInt32
  24. let masterSeed: Bytes
  25. let initialVector: Bytes
  26. var groups: UInt32
  27. var entries: UInt32
  28. var contentHash: Bytes
  29. let transformSeed: Bytes
  30. let transformRounds: UInt32
  31. }
  32. struct Flag: OptionSet, Streamable {
  33. let rawValue: UInt32
  34. static let sha2 = Flag(rawValue: 1 << 0)
  35. static let aes = Flag(rawValue: 1 << 1)
  36. static let arc4 = Flag(rawValue: 1 << 2)
  37. static let twofish = Flag(rawValue: 1 << 3)
  38. }
  39. extension Header: Streamable {
  40. init(from input: Input) throws {
  41. cipher = try input.read()
  42. version = try input.read()
  43. masterSeed = try input.read(lenght: 16)
  44. initialVector = try input.read(lenght: 16)
  45. groups = try input.read()
  46. entries = try input.read()
  47. contentHash = try input.read(lenght: 32)
  48. transformSeed = try input.read(lenght: 32)
  49. transformRounds = try input.read()
  50. }
  51. func write(to output: Output) throws {
  52. try output.write(cipher)
  53. try output.write(version)
  54. try output.write(masterSeed)
  55. try output.write(initialVector)
  56. try output.write(groups)
  57. try output.write(entries)
  58. try output.write(contentHash)
  59. try output.write(transformSeed)
  60. try output.write(transformRounds)
  61. }
  62. }
  63. extension Header {
  64. func cipher(key: Bytes) throws -> Cipher {
  65. if cipher.contains(.aes) {
  66. return try AESCipher(key: key, iv: initialVector)
  67. }
  68. if cipher.contains(.twofish) {
  69. return try Twofish(key: key, iv: initialVector)
  70. }
  71. throw KDBError.unsupportedCipher
  72. }
  73. func masterKey(from compositeKey: CompositeKey) throws -> Bytes {
  74. let key = try compositeKey.serialize()
  75. // Key Derivation
  76. let kdf = try AESKeyDerivation(seed: transformSeed, rounds: UInt64(transformRounds))
  77. let derivedKey = try kdf.derive(key: key)
  78. return SHA256.hash(masterSeed + derivedKey)
  79. }
  80. }