HMAC.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. public final class HMAC: Authenticator {
  16. public enum Error: Swift.Error {
  17. case authenticateError
  18. case invalidInput
  19. }
  20. public enum Variant {
  21. case md5
  22. case sha1
  23. case sha2(SHA2.Variant)
  24. case sha3(SHA3.Variant)
  25. @available(*, deprecated, message: "Use sha2(variant) instead.")
  26. case sha256, sha384, sha512
  27. var digestLength: Int {
  28. switch self {
  29. case .sha1:
  30. return SHA1.digestLength
  31. case .sha256:
  32. return SHA2.Variant.sha256.digestLength
  33. case .sha384:
  34. return SHA2.Variant.sha384.digestLength
  35. case .sha512:
  36. return SHA2.Variant.sha512.digestLength
  37. case .sha2(let variant):
  38. return variant.digestLength
  39. case .sha3(let variant):
  40. return variant.digestLength
  41. case .md5:
  42. return MD5.digestLength
  43. }
  44. }
  45. func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8> {
  46. switch self {
  47. case .sha1:
  48. return Digest.sha1(bytes)
  49. case .sha256:
  50. return Digest.sha256(bytes)
  51. case .sha384:
  52. return Digest.sha384(bytes)
  53. case .sha512:
  54. return Digest.sha512(bytes)
  55. case .sha2(let variant):
  56. return Digest.sha2(bytes, variant: variant)
  57. case .sha3(let variant):
  58. return Digest.sha3(bytes, variant: variant)
  59. case .md5:
  60. return Digest.md5(bytes)
  61. }
  62. }
  63. func blockSize() -> Int {
  64. switch self {
  65. case .md5:
  66. return MD5.blockSize
  67. case .sha1:
  68. return SHA1.blockSize
  69. case .sha256:
  70. return SHA2.Variant.sha256.blockSize
  71. case .sha384:
  72. return SHA2.Variant.sha384.blockSize
  73. case .sha512:
  74. return SHA2.Variant.sha512.blockSize
  75. case .sha2(let variant):
  76. return variant.blockSize
  77. case .sha3(let variant):
  78. return variant.blockSize
  79. }
  80. }
  81. }
  82. var key: Array<UInt8>
  83. let variant: Variant
  84. public init(key: Array<UInt8>, variant: HMAC.Variant = .md5) {
  85. self.variant = variant
  86. self.key = key
  87. if key.count > variant.blockSize() {
  88. let hash = variant.calculateHash(key)
  89. self.key = hash
  90. }
  91. if key.count < variant.blockSize() {
  92. self.key = ZeroPadding().add(to: key, blockSize: variant.blockSize())
  93. }
  94. }
  95. // MARK: Authenticator
  96. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  97. var opad = Array<UInt8>(repeating: 0x5c, count: variant.blockSize())
  98. for idx in self.key.indices {
  99. opad[idx] = self.key[idx] ^ opad[idx]
  100. }
  101. var ipad = Array<UInt8>(repeating: 0x36, count: variant.blockSize())
  102. for idx in self.key.indices {
  103. ipad[idx] = self.key[idx] ^ ipad[idx]
  104. }
  105. let ipadAndMessageHash = self.variant.calculateHash(ipad + bytes)
  106. let result = self.variant.calculateHash(opad + ipadAndMessageHash)
  107. // return Array(result[0..<10]) // 80 bits
  108. return result
  109. }
  110. }