Header.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Header.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 Foundation
  19. import Binary
  20. import Crypto
  21. struct Header {
  22. let cipher: Flag
  23. let version: UInt32
  24. let masterSeed: Bytes
  25. let initialVector: Bytes
  26. let groups: UInt32
  27. let entries: UInt32
  28. let 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 masterKey(from compositeKey: CompositeKey) throws -> Bytes {
  65. let key = try compositeKey.serialize()
  66. // Key Derivation
  67. let kdf = try AESKeyDerivation(seed: transformSeed, rounds: UInt64(transformRounds))
  68. let derivedKey = try kdf.derive(key: key)
  69. return SHA256.hash( masterSeed + derivedKey )
  70. }
  71. }