CMAC.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 class CMAC: Authenticator {
  16. public enum Error: Swift.Error {
  17. case wrongKeyLength
  18. }
  19. internal let key: SecureBytes
  20. internal static let BlockSize: Int = 16
  21. internal 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. if key.count != 16 {
  25. throw Error.wrongKeyLength
  26. }
  27. self.key = SecureBytes(bytes: key)
  28. }
  29. // MARK: Authenticator
  30. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  31. let aes = try AES(key: Array(key), blockMode: CBC(iv: CMAC.Zero), padding: .noPadding)
  32. let l = try aes.encrypt(CMAC.Zero)
  33. var subKey1 = self.leftShiftOneBit(l)
  34. if (l[0] & 0x80) != 0 {
  35. subKey1 = xor(CMAC.Rb, subKey1)
  36. }
  37. var subKey2 = self.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. // the difference between CMAC and CBC-MAC is that CMAC xors the final block with a secret value
  66. y = self.process(lastBlock: lastBlock, with: x)
  67. return try aes.encrypt(y)
  68. }
  69. func process(lastBlock: ArraySlice<UInt8>, with x: [UInt8]) -> [UInt8] {
  70. xor(lastBlock, x)
  71. }
  72. // MARK: Helper methods
  73. /**
  74. Performs left shift by one bit to the bit string aquired after concatenating al bytes in the byte array
  75. - parameters:
  76. - bytes: byte array
  77. - returns: bit shifted bit string split again in array of bytes
  78. */
  79. private func leftShiftOneBit(_ bytes: Array<UInt8>) -> Array<UInt8> {
  80. var shifted = Array<UInt8>(repeating: 0x00, count: bytes.count)
  81. let last = bytes.count - 1
  82. for index in 0..<last {
  83. shifted[index] = bytes[index] << 1
  84. if (bytes[index + 1] & 0x80) != 0 {
  85. shifted[index] += 0x01
  86. }
  87. }
  88. shifted[last] = bytes[last] << 1
  89. return shifted
  90. }
  91. }