CMAC.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2025 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. self.key = SecureBytes(bytes: key)
  25. }
  26. // MARK: Authenticator
  27. // AES-CMAC
  28. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  29. let cipher = try AES(key: Array(key), blockMode: CBC(iv: CMAC.Zero), padding: .noPadding)
  30. return try self.authenticate(bytes, cipher: cipher)
  31. }
  32. // CMAC using a Cipher
  33. public func authenticate(_ bytes: Array<UInt8>, cipher: Cipher) throws -> Array<UInt8> {
  34. let l = try cipher.encrypt(CMAC.Zero)
  35. var subKey1 = self.leftShiftOneBit(l)
  36. if (l[0] & 0x80) != 0 {
  37. subKey1 = xor(CMAC.Rb, subKey1)
  38. }
  39. var subKey2 = self.leftShiftOneBit(subKey1)
  40. if (subKey1[0] & 0x80) != 0 {
  41. subKey2 = xor(CMAC.Rb, subKey2)
  42. }
  43. let lastBlockComplete: Bool
  44. let blockCount = (bytes.count + CMAC.BlockSize - 1) / CMAC.BlockSize
  45. if blockCount == 0 {
  46. lastBlockComplete = false
  47. } else {
  48. lastBlockComplete = bytes.count % CMAC.BlockSize == 0
  49. }
  50. var paddedBytes = bytes
  51. if !lastBlockComplete {
  52. bitPadding(to: &paddedBytes, blockSize: CMAC.BlockSize)
  53. }
  54. var blocks = Array(paddedBytes.batched(by: CMAC.BlockSize))
  55. var lastBlock = blocks.popLast()!
  56. if lastBlockComplete {
  57. lastBlock = xor(lastBlock, subKey1)
  58. } else {
  59. lastBlock = xor(lastBlock, subKey2)
  60. }
  61. var x = Array<UInt8>(repeating: 0x00, count: CMAC.BlockSize)
  62. var y = Array<UInt8>(repeating: 0x00, count: CMAC.BlockSize)
  63. for block in blocks {
  64. y = xor(block, x)
  65. x = try cipher.encrypt(y)
  66. }
  67. // the difference between CMAC and CBC-MAC is that CMAC xors the final block with a secret value
  68. y = self.process(lastBlock: lastBlock, with: x)
  69. return try cipher.encrypt(y)
  70. }
  71. func process(lastBlock: ArraySlice<UInt8>, with x: [UInt8]) -> [UInt8] {
  72. xor(lastBlock, x)
  73. }
  74. // MARK: Helper methods
  75. /**
  76. Performs left shift by one bit to the bit string acquired after concatenating al bytes in the byte array
  77. - parameters:
  78. - bytes: byte array
  79. - returns: bit shifted bit string split again in array of bytes
  80. */
  81. private func leftShiftOneBit(_ bytes: Array<UInt8>) -> Array<UInt8> {
  82. var shifted = Array<UInt8>(repeating: 0x00, count: bytes.count)
  83. let last = bytes.count - 1
  84. for index in 0..<last {
  85. shifted[index] = bytes[index] << 1
  86. if (bytes[index + 1] & 0x80) != 0 {
  87. shifted[index] += 0x01
  88. }
  89. }
  90. shifted[last] = bytes[last] << 1
  91. return shifted
  92. }
  93. }