Header.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. protocol Header {
  22. subscript(_ type: OuterHeader) -> Bytes? { get }
  23. }
  24. enum OuterHeader: UInt8, Streamable {
  25. case end = 0
  26. case comment = 1
  27. case cipherID = 2
  28. case compressionFlags = 3
  29. case masterSeed = 4
  30. case transformSeed = 5
  31. case transformRounds = 6
  32. case initialVector = 7
  33. case protectedStreamKey = 8
  34. case streamStartBytes = 9
  35. case innerRandomStreamID = 10
  36. case kdfParameters = 11
  37. case publicCustomData = 12
  38. }
  39. enum InnerHeader: UInt8, Streamable {
  40. case end = 0
  41. case innerRandomStreamID = 1
  42. case innerRandomStreamKey = 2
  43. case binary = 3
  44. }
  45. enum Compression: UInt32, BytesRepresentable {
  46. case none = 0
  47. case gzip = 1
  48. case count = 2
  49. }
  50. enum RandomStream: UInt32, BytesRepresentable {
  51. case none = 0
  52. case arc4 = 1
  53. case salsa20 = 2
  54. case chacha20 = 3
  55. case count = 4
  56. }
  57. extension Header {
  58. subscript<T>(_ type: OuterHeader) -> T? where T: BytesRepresentable {
  59. guard let bytes = self[type] else { return nil }
  60. return try? T(bytes)
  61. }
  62. func cipher(key: Bytes) throws -> Cipher {
  63. guard
  64. let uuid: UUID = self[.cipherID],
  65. let iv = self[.initialVector]
  66. else { throw KDBXError.corruptedDatabase }
  67. switch uuid {
  68. case AESCipher.UUID:
  69. return try AESCipher(key: key, iv: iv)
  70. case Twofish.UUID:
  71. return try Twofish(key: key, iv: iv)
  72. case ChaCha20.UUID:
  73. return try ChaCha20(key: key, iv: iv)
  74. default:
  75. throw KDBXError.unsupportedCipher
  76. }
  77. }
  78. func masterKey(from compositeKey: CompositeKey) throws -> Bytes {
  79. guard let masterSeed = self[.masterSeed] else { throw KDBXError.corruptedDatabase }
  80. let key = try compositeKey.serialize()
  81. // Key Derivation
  82. let derivedKey = try kdf().derive(key: key)
  83. return masterSeed + derivedKey
  84. }
  85. func kdf() throws -> KeyDerivation {
  86. if let seed: Bytes = self[.transformSeed], let rounds: UInt64 = self[.transformRounds] {
  87. return try AESKeyDerivation(seed: seed, rounds: rounds)
  88. }
  89. guard
  90. let parameters: KeyDerivationParameters = self[.kdfParameters],
  91. let uuid: UUID = try parameters["$UUID"]?.unwrap()
  92. else { throw KDBXError.corruptedDatabase }
  93. switch uuid {
  94. case Argon2.UUID:
  95. return try Argon2(parameters: parameters)
  96. case AESKeyDerivation.UUID:
  97. return try AESKeyDerivation(parameters: parameters)
  98. default:
  99. throw KDBXError.unsupportedKeyDerivation
  100. }
  101. }
  102. }