AES.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // AES.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 CommonCrypto
  20. import Foundation
  21. public final class AESCipher: Cipher {
  22. public let key: Bytes
  23. public let iv: Bytes
  24. public init(key: Bytes, iv: Bytes) throws {
  25. guard key.lenght == kCCKeySizeAES256 else {
  26. throw CryptoError.keyLenght(expecting: "Equal \(kCCKeySizeAES256)", got: key.lenght)
  27. }
  28. guard iv.lenght == kCCBlockSizeAES128 else {
  29. throw CryptoError.ivLenght(expecting: "Equal \(kCCBlockSizeAES128)", got: iv.lenght)
  30. }
  31. self.key = key
  32. self.iv = iv
  33. }
  34. public func encrypt(data: Bytes) throws -> Bytes {
  35. let operation: CCOperation = UInt32(kCCEncrypt)
  36. let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES)
  37. let options: CCOptions = UInt32(kCCOptionPKCS7Padding)
  38. var out = Bytes(lenght: data.lenght + kCCBlockSizeAES128)
  39. var count: Int = 0
  40. let status = CCCrypt(operation, algoritm, options,
  41. key.rawValue, key.lenght,
  42. iv.rawValue,
  43. data.rawValue, data.lenght,
  44. &out.rawValue, out.lenght,
  45. &count)
  46. guard status == kCCSuccess else { throw CryptoError.crypto(status: status) }
  47. return out.prefix(count)
  48. }
  49. public func decrypt(data: Bytes) throws -> Bytes {
  50. guard data.lenght % kCCBlockSizeAES128 == 0 else { throw CryptoError.invalidLenght }
  51. let operation: CCOperation = UInt32(kCCDecrypt)
  52. let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES)
  53. let options: CCOptions = UInt32(kCCOptionPKCS7Padding)
  54. var out = Bytes(lenght: data.lenght)
  55. var count: Int = 0
  56. let status = CCCrypt(operation, algoritm, options,
  57. key.rawValue, key.lenght,
  58. iv.rawValue,
  59. data.rawValue, data.lenght,
  60. &out.rawValue, out.lenght,
  61. &count)
  62. guard status == kCCSuccess else { throw CryptoError.crypto(status: status) }
  63. return out.prefix(count)
  64. }
  65. }
  66. public final class AESKeyDerivation: KeyDerivation {
  67. public let seed: Bytes
  68. public let rounds: UInt64
  69. public init(seed: Bytes, rounds: UInt64) throws {
  70. guard seed.lenght == kCCKeySizeAES256 else { throw CryptoError.invalidLenght }
  71. self.seed = seed
  72. self.rounds = rounds
  73. }
  74. public func derive(key: Bytes) throws -> Bytes {
  75. guard key.lenght == kCCKeySizeAES256 else { throw CryptoError.invalidLenght }
  76. let cryptor = UnsafeMutablePointer<CCCryptorRef?>.allocate(capacity: 1)
  77. var status = CCCryptorCreate(CCOperation(kCCEncrypt),
  78. CCAlgorithm(kCCAlgorithmAES),
  79. CCOptions(kCCOptionECBMode),
  80. seed.rawValue,
  81. seed.lenght,
  82. nil,
  83. cryptor)
  84. guard status == kCCSuccess else { throw CryptoError.crypto(status: status) }
  85. var out = key
  86. var count = rounds
  87. var dataOutMoved: Int = 0
  88. while count > 0 {
  89. status = CCCryptorUpdate(cryptor.pointee,
  90. out.rawValue,
  91. out.lenght,
  92. &out.rawValue,
  93. out.lenght,
  94. &dataOutMoved)
  95. guard status == kCCSuccess else { throw CryptoError.crypto(status: status) }
  96. count -= 1
  97. }
  98. CCCryptorRelease(cryptor.pointee)
  99. cryptor.deallocate()
  100. return SHA256.hash(out)
  101. }
  102. }