CMAC.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 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 CMAC: Authenticator {
  16. public enum Error: Swift.Error {
  17. case wrongKeyLength
  18. }
  19. private let key: Array<UInt8>
  20. private static let BlockSize: Int = 16
  21. private static let Zero: Array<UInt8> = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  22. private static let Rb: Array<UInt8> = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87]
  23. public init(key: Array<UInt8>) throws {
  24. self.key = key
  25. if key.count != 16 {
  26. throw Error.wrongKeyLength
  27. }
  28. }
  29. // MARK: Authenticator
  30. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  31. let aes = try AES(key: key, blockMode: .CBC(iv: CMAC.Zero), padding: .noPadding)
  32. let l = try aes.encrypt(CMAC.Zero)
  33. var subKey1 = leftShiftOneBit(l)
  34. if (l[0] & 0x80) != 0 {
  35. subKey1 = xor(CMAC.Rb, subKey1)
  36. }
  37. var subKey2 = leftShiftOneBit(subKey1)
  38. if (subKey1[0] & 0x80) != 0 {
  39. subKey2 = xor(CMAC.Rb, subKey2)
  40. }
  41. let lastBlockComplete: Bool
  42. let blockCount = (bytes.count + CMAC.BlockSize - 1) / CMAC.BlockSize
  43. if blockCount == 0 {
  44. lastBlockComplete = false
  45. } else {
  46. lastBlockComplete = bytes.count % CMAC.BlockSize == 0
  47. }
  48. var paddedBytes = bytes
  49. if !lastBlockComplete {
  50. bitPadding(to: &paddedBytes, blockSize: CMAC.BlockSize)
  51. }
  52. var blocks = Array(paddedBytes.batched(by: CMAC.BlockSize))
  53. var lastBlock = blocks.popLast()!
  54. if lastBlockComplete {
  55. lastBlock = xor(lastBlock, subKey1)
  56. } else {
  57. lastBlock = xor(lastBlock, subKey2)
  58. }
  59. var x = Array<UInt8>(repeating: 0x00, count: CMAC.BlockSize)
  60. var y = Array<UInt8>(repeating: 0x00, count: CMAC.BlockSize)
  61. for block in blocks {
  62. y = xor(block, x)
  63. x = try aes.encrypt(y)
  64. }
  65. y = xor(lastBlock, x)
  66. return try aes.encrypt(y)
  67. }
  68. // MARK: Helper methods
  69. /**
  70. Performs left shift by one bit to the bit string aquired after concatenating al bytes in the byte array
  71. - parameters:
  72. - bytes: byte array
  73. - returns: bit shifted bit string split again in array of bytes
  74. */
  75. private func leftShiftOneBit(_ bytes: Array<UInt8>) -> Array<UInt8> {
  76. var shifted = Array<UInt8>(repeating: 0x00, count: bytes.count)
  77. let last = bytes.count - 1
  78. for index in 0..<last {
  79. shifted[index] = bytes[index] << 1
  80. if (bytes[index + 1] & 0x80) != 0 {
  81. shifted[index] += 0x01
  82. }
  83. }
  84. shifted[last] = bytes[last] << 1
  85. return shifted
  86. }
  87. }