Header.swift 3.4 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 Binary
  19. import Crypto
  20. import Foundation
  21. typealias Header<T, L> = [TLV<T, L>] where L: BinaryInteger
  22. enum OuterHeader: UInt8, Streamable, Endable {
  23. case end = 0
  24. case comment = 1
  25. case cipherID = 2
  26. case compressionFlags = 3
  27. case masterSeed = 4
  28. case transformSeed = 5
  29. case transformRounds = 6
  30. case initialVector = 7
  31. case protectedStreamKey = 8
  32. case streamStartBytes = 9
  33. case innerRandomStreamID = 10
  34. case kdfParameters = 11
  35. case publicCustomData = 12
  36. public static var endValue: OuterHeader { .end }
  37. }
  38. enum InnerHeader: UInt8, Streamable, Endable {
  39. case end = 0
  40. case innerRandomStreamID = 1
  41. case innerRandomStreamKey = 2
  42. case binary = 3
  43. public static var endValue: InnerHeader { .end }
  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 Array where Element: TypeLenghtValue, Element.Type_ == OuterHeader, Element.Value == Bytes {
  58. func cipher(key: Bytes) throws -> Cipher {
  59. guard
  60. let uuid: UUID = self[.cipherID],
  61. let iv: Bytes = self[.initialVector]
  62. else { throw KDBXError.corruptedDatabase }
  63. switch uuid {
  64. case AESCipher.UUID:
  65. return try AESCipher(key: key, iv: iv)
  66. case Twofish.UUID:
  67. return try Twofish(key: key, iv: iv)
  68. case ChaCha20.UUID:
  69. return try ChaCha20(key: key, iv: iv)
  70. default:
  71. throw KDBXError.unsupportedCipher
  72. }
  73. }
  74. func masterKey(from compositeKey: CompositeKey) throws -> Bytes {
  75. guard
  76. let masterSeed: Bytes = self[.masterSeed]
  77. else { throw KDBXError.corruptedDatabase }
  78. let key = try compositeKey.serialize()
  79. // Key Derivation
  80. let derivedKey = try kdf().derive(key: key)
  81. return masterSeed + derivedKey
  82. }
  83. func kdf() throws -> KeyDerivation {
  84. if let seed: Bytes = self[.transformSeed], let rounds: UInt64 = self[.transformRounds] {
  85. return try AESKeyDerivation(seed: seed, rounds: rounds)
  86. }
  87. guard
  88. let parameters: KeyDerivationParameters = self[.kdfParameters],
  89. let uuid: UUID = try parameters["$UUID"]?.unwrap()
  90. else { throw KDBXError.corruptedDatabase }
  91. switch uuid {
  92. case Argon2.UUID:
  93. return try Argon2(parameters: parameters)
  94. case AESKeyDerivation.UUID:
  95. return try AESKeyDerivation(parameters: parameters)
  96. default:
  97. throw KDBXError.unsupportedKeyDerivation
  98. }
  99. }
  100. }