Poly1305.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-4
  16. // nacl/crypto_onetimeauth/poly1305/ref/auth.c
  17. //
  18. /// Poly1305 takes a 32-byte, one-time key and a message and produces a 16-byte tag that authenticates the
  19. /// message such that an attacker has a negligible chance of producing a valid tag for an inauthentic message.
  20. public final class Poly1305: Authenticator {
  21. public enum Error: Swift.Error {
  22. case authenticateError
  23. }
  24. public static let blockSize: Int = 16
  25. private let key: SecureBytes
  26. /// - parameter key: 32-byte key
  27. public init(key: Array<UInt8>) {
  28. self.key = SecureBytes(bytes: key)
  29. }
  30. private func squeeze(h: inout Array<UInt32>) {
  31. assert(h.count == 17)
  32. var u: UInt32 = 0
  33. for j in 0..<16 {
  34. u = u &+ h[j]
  35. h[j] = u & 255
  36. u = u >> 8
  37. }
  38. u = u &+ h[16]
  39. h[16] = u & 3
  40. u = 5 * (u >> 2)
  41. for j in 0..<16 {
  42. u = u &+ h[j]
  43. h[j] = u & 255
  44. u = u >> 8
  45. }
  46. u = u &+ h[16]
  47. h[16] = u
  48. }
  49. private func add(h: inout Array<UInt32>, c: Array<UInt32>) {
  50. assert(h.count == 17 && c.count == 17)
  51. var u: UInt32 = 0
  52. for j in 0..<17 {
  53. u = u &+ (h[j] &+ c[j])
  54. h[j] = u & 255
  55. u = u >> 8
  56. }
  57. }
  58. private func mulmod(h: inout Array<UInt32>, r: Array<UInt32>) {
  59. var hr = Array<UInt32>(repeating: 0, count: 17)
  60. var u: UInt32 = 0
  61. for i in 0..<17 {
  62. u = 0
  63. for j in 0...i {
  64. u = u &+ (h[j] * r[i &- j])
  65. }
  66. for j in (i + 1)..<17 {
  67. u = u &+ (320 * h[j] * r[i &+ 17 &- j])
  68. }
  69. hr[i] = u
  70. }
  71. h = hr
  72. self.squeeze(h: &h)
  73. }
  74. private func freeze(h: inout Array<UInt32>) {
  75. let horig = h
  76. self.add(h: &h, c: [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252])
  77. let negative = UInt32(bitPattern: -Int32(h[16] >> 7))
  78. for j in 0..<17 {
  79. h[j] ^= negative & (horig[j] ^ h[j])
  80. }
  81. }
  82. /// the key is partitioned into two parts, called "r" and "s"
  83. fileprivate func onetimeauth(message input: Array<UInt8>, key k: Array<UInt8>) -> Array<UInt8> {
  84. // clamp
  85. var r = Array<UInt32>(repeating: 0, count: 17)
  86. var h = Array<UInt32>(repeating: 0, count: 17)
  87. var c = Array<UInt32>(repeating: 0, count: 17)
  88. r[0] = UInt32(k[0])
  89. r[1] = UInt32(k[1])
  90. r[2] = UInt32(k[2])
  91. r[3] = UInt32(k[3] & 15)
  92. r[4] = UInt32(k[4] & 252)
  93. r[5] = UInt32(k[5])
  94. r[6] = UInt32(k[6])
  95. r[7] = UInt32(k[7] & 15)
  96. r[8] = UInt32(k[8] & 252)
  97. r[9] = UInt32(k[9])
  98. r[10] = UInt32(k[10])
  99. r[11] = UInt32(k[11] & 15)
  100. r[12] = UInt32(k[12] & 252)
  101. r[13] = UInt32(k[13])
  102. r[14] = UInt32(k[14])
  103. r[15] = UInt32(k[15] & 15)
  104. r[16] = 0
  105. var inlen = input.count
  106. var inpos = 0
  107. while inlen > 0 {
  108. for j in 0..<c.count {
  109. c[j] = 0
  110. }
  111. let maxj = min(inlen, 16)
  112. for j in 0..<maxj {
  113. c[j] = UInt32(input[inpos + j])
  114. }
  115. c[maxj] = 1
  116. inpos = inpos + maxj
  117. inlen = inlen - maxj
  118. self.add(h: &h, c: c)
  119. self.mulmod(h: &h, r: r)
  120. }
  121. self.freeze(h: &h)
  122. for j in 0..<16 {
  123. c[j] = UInt32(k[j + 16])
  124. }
  125. c[16] = 0
  126. self.add(h: &h, c: c)
  127. return h[0..<16].map {
  128. UInt8($0 & 0xff)
  129. }
  130. }
  131. // MARK: - Authenticator
  132. /**
  133. Calculate Message Authentication Code (MAC) for message.
  134. Calculation context is discarder on instance deallocation.
  135. - parameter bytes: Message
  136. - returns: 16-byte tag that authenticates the message
  137. */
  138. public func authenticate(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
  139. self.onetimeauth(message: bytes, key: Array(self.key))
  140. }
  141. }