sum_generic.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package poly1305
  5. import "encoding/binary"
  6. const (
  7. msgBlock = uint32(1 << 24)
  8. finalBlock = uint32(0)
  9. )
  10. // sumGeneric generates an authenticator for msg using a one-time key and
  11. // puts the 16-byte result into out. This is the generic implementation of
  12. // Sum and should be called if no assembly implementation is available.
  13. func sumGeneric(out *[TagSize]byte, msg []byte, key *[32]byte) {
  14. h := newMACGeneric(key)
  15. h.Write(msg)
  16. h.Sum(out)
  17. }
  18. func newMACGeneric(key *[32]byte) (h macGeneric) {
  19. h.r[0] = binary.LittleEndian.Uint32(key[0:]) & 0x3ffffff
  20. h.r[1] = (binary.LittleEndian.Uint32(key[3:]) >> 2) & 0x3ffff03
  21. h.r[2] = (binary.LittleEndian.Uint32(key[6:]) >> 4) & 0x3ffc0ff
  22. h.r[3] = (binary.LittleEndian.Uint32(key[9:]) >> 6) & 0x3f03fff
  23. h.r[4] = (binary.LittleEndian.Uint32(key[12:]) >> 8) & 0x00fffff
  24. h.s[0] = binary.LittleEndian.Uint32(key[16:])
  25. h.s[1] = binary.LittleEndian.Uint32(key[20:])
  26. h.s[2] = binary.LittleEndian.Uint32(key[24:])
  27. h.s[3] = binary.LittleEndian.Uint32(key[28:])
  28. return
  29. }
  30. type macGeneric struct {
  31. h, r [5]uint32
  32. s [4]uint32
  33. buffer [TagSize]byte
  34. offset int
  35. }
  36. func (h *macGeneric) Write(p []byte) (n int, err error) {
  37. n = len(p)
  38. if h.offset > 0 {
  39. remaining := TagSize - h.offset
  40. if n < remaining {
  41. h.offset += copy(h.buffer[h.offset:], p)
  42. return n, nil
  43. }
  44. copy(h.buffer[h.offset:], p[:remaining])
  45. p = p[remaining:]
  46. h.offset = 0
  47. updateGeneric(h.buffer[:], msgBlock, &(h.h), &(h.r))
  48. }
  49. if nn := len(p) - (len(p) % TagSize); nn > 0 {
  50. updateGeneric(p, msgBlock, &(h.h), &(h.r))
  51. p = p[nn:]
  52. }
  53. if len(p) > 0 {
  54. h.offset += copy(h.buffer[h.offset:], p)
  55. }
  56. return n, nil
  57. }
  58. func (h *macGeneric) Sum(out *[16]byte) {
  59. H, R := h.h, h.r
  60. if h.offset > 0 {
  61. var buffer [TagSize]byte
  62. copy(buffer[:], h.buffer[:h.offset])
  63. buffer[h.offset] = 1 // invariant: h.offset < TagSize
  64. updateGeneric(buffer[:], finalBlock, &H, &R)
  65. }
  66. finalizeGeneric(out, &H, &(h.s))
  67. }
  68. func updateGeneric(msg []byte, flag uint32, h, r *[5]uint32) {
  69. h0, h1, h2, h3, h4 := h[0], h[1], h[2], h[3], h[4]
  70. r0, r1, r2, r3, r4 := uint64(r[0]), uint64(r[1]), uint64(r[2]), uint64(r[3]), uint64(r[4])
  71. R1, R2, R3, R4 := r1*5, r2*5, r3*5, r4*5
  72. for len(msg) >= TagSize {
  73. // h += msg
  74. h0 += binary.LittleEndian.Uint32(msg[0:]) & 0x3ffffff
  75. h1 += (binary.LittleEndian.Uint32(msg[3:]) >> 2) & 0x3ffffff
  76. h2 += (binary.LittleEndian.Uint32(msg[6:]) >> 4) & 0x3ffffff
  77. h3 += (binary.LittleEndian.Uint32(msg[9:]) >> 6) & 0x3ffffff
  78. h4 += (binary.LittleEndian.Uint32(msg[12:]) >> 8) | flag
  79. // h *= r
  80. d0 := (uint64(h0) * r0) + (uint64(h1) * R4) + (uint64(h2) * R3) + (uint64(h3) * R2) + (uint64(h4) * R1)
  81. d1 := (d0 >> 26) + (uint64(h0) * r1) + (uint64(h1) * r0) + (uint64(h2) * R4) + (uint64(h3) * R3) + (uint64(h4) * R2)
  82. d2 := (d1 >> 26) + (uint64(h0) * r2) + (uint64(h1) * r1) + (uint64(h2) * r0) + (uint64(h3) * R4) + (uint64(h4) * R3)
  83. d3 := (d2 >> 26) + (uint64(h0) * r3) + (uint64(h1) * r2) + (uint64(h2) * r1) + (uint64(h3) * r0) + (uint64(h4) * R4)
  84. d4 := (d3 >> 26) + (uint64(h0) * r4) + (uint64(h1) * r3) + (uint64(h2) * r2) + (uint64(h3) * r1) + (uint64(h4) * r0)
  85. // h %= p
  86. h0 = uint32(d0) & 0x3ffffff
  87. h1 = uint32(d1) & 0x3ffffff
  88. h2 = uint32(d2) & 0x3ffffff
  89. h3 = uint32(d3) & 0x3ffffff
  90. h4 = uint32(d4) & 0x3ffffff
  91. h0 += uint32(d4>>26) * 5
  92. h1 += h0 >> 26
  93. h0 = h0 & 0x3ffffff
  94. msg = msg[TagSize:]
  95. }
  96. h[0], h[1], h[2], h[3], h[4] = h0, h1, h2, h3, h4
  97. }
  98. func finalizeGeneric(out *[TagSize]byte, h *[5]uint32, s *[4]uint32) {
  99. h0, h1, h2, h3, h4 := h[0], h[1], h[2], h[3], h[4]
  100. // h %= p reduction
  101. h2 += h1 >> 26
  102. h1 &= 0x3ffffff
  103. h3 += h2 >> 26
  104. h2 &= 0x3ffffff
  105. h4 += h3 >> 26
  106. h3 &= 0x3ffffff
  107. h0 += 5 * (h4 >> 26)
  108. h4 &= 0x3ffffff
  109. h1 += h0 >> 26
  110. h0 &= 0x3ffffff
  111. // h - p
  112. t0 := h0 + 5
  113. t1 := h1 + (t0 >> 26)
  114. t2 := h2 + (t1 >> 26)
  115. t3 := h3 + (t2 >> 26)
  116. t4 := h4 + (t3 >> 26) - (1 << 26)
  117. t0 &= 0x3ffffff
  118. t1 &= 0x3ffffff
  119. t2 &= 0x3ffffff
  120. t3 &= 0x3ffffff
  121. // select h if h < p else h - p
  122. t_mask := (t4 >> 31) - 1
  123. h_mask := ^t_mask
  124. h0 = (h0 & h_mask) | (t0 & t_mask)
  125. h1 = (h1 & h_mask) | (t1 & t_mask)
  126. h2 = (h2 & h_mask) | (t2 & t_mask)
  127. h3 = (h3 & h_mask) | (t3 & t_mask)
  128. h4 = (h4 & h_mask) | (t4 & t_mask)
  129. // h %= 2^128
  130. h0 |= h1 << 26
  131. h1 = ((h1 >> 6) | (h2 << 20))
  132. h2 = ((h2 >> 12) | (h3 << 14))
  133. h3 = ((h3 >> 18) | (h4 << 8))
  134. // s: the s part of the key
  135. // tag = (h + s) % (2^128)
  136. t := uint64(h0) + uint64(s[0])
  137. h0 = uint32(t)
  138. t = uint64(h1) + uint64(s[1]) + (t >> 32)
  139. h1 = uint32(t)
  140. t = uint64(h2) + uint64(s[2]) + (t >> 32)
  141. h2 = uint32(t)
  142. t = uint64(h3) + uint64(s[3]) + (t >> 32)
  143. h3 = uint32(t)
  144. binary.LittleEndian.PutUint32(out[0:], h0)
  145. binary.LittleEndian.PutUint32(out[4:], h1)
  146. binary.LittleEndian.PutUint32(out[8:], h2)
  147. binary.LittleEndian.PutUint32(out[12:], h3)
  148. }