You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

140 lines
4.7 KiB

  1. // Copyright 2012 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. // sumGeneric generates an authenticator for msg using a one-time key and
  7. // puts the 16-byte result into out. This is the generic implementation of
  8. // Sum and should be called if no assembly implementation is available.
  9. func sumGeneric(out *[TagSize]byte, msg []byte, key *[32]byte) {
  10. var (
  11. h0, h1, h2, h3, h4 uint32 // the hash accumulators
  12. r0, r1, r2, r3, r4 uint64 // the r part of the key
  13. )
  14. r0 = uint64(binary.LittleEndian.Uint32(key[0:]) & 0x3ffffff)
  15. r1 = uint64((binary.LittleEndian.Uint32(key[3:]) >> 2) & 0x3ffff03)
  16. r2 = uint64((binary.LittleEndian.Uint32(key[6:]) >> 4) & 0x3ffc0ff)
  17. r3 = uint64((binary.LittleEndian.Uint32(key[9:]) >> 6) & 0x3f03fff)
  18. r4 = uint64((binary.LittleEndian.Uint32(key[12:]) >> 8) & 0x00fffff)
  19. R1, R2, R3, R4 := r1*5, r2*5, r3*5, r4*5
  20. for len(msg) >= TagSize {
  21. // h += msg
  22. h0 += binary.LittleEndian.Uint32(msg[0:]) & 0x3ffffff
  23. h1 += (binary.LittleEndian.Uint32(msg[3:]) >> 2) & 0x3ffffff
  24. h2 += (binary.LittleEndian.Uint32(msg[6:]) >> 4) & 0x3ffffff
  25. h3 += (binary.LittleEndian.Uint32(msg[9:]) >> 6) & 0x3ffffff
  26. h4 += (binary.LittleEndian.Uint32(msg[12:]) >> 8) | (1 << 24)
  27. // h *= r
  28. d0 := (uint64(h0) * r0) + (uint64(h1) * R4) + (uint64(h2) * R3) + (uint64(h3) * R2) + (uint64(h4) * R1)
  29. d1 := (d0 >> 26) + (uint64(h0) * r1) + (uint64(h1) * r0) + (uint64(h2) * R4) + (uint64(h3) * R3) + (uint64(h4) * R2)
  30. d2 := (d1 >> 26) + (uint64(h0) * r2) + (uint64(h1) * r1) + (uint64(h2) * r0) + (uint64(h3) * R4) + (uint64(h4) * R3)
  31. d3 := (d2 >> 26) + (uint64(h0) * r3) + (uint64(h1) * r2) + (uint64(h2) * r1) + (uint64(h3) * r0) + (uint64(h4) * R4)
  32. d4 := (d3 >> 26) + (uint64(h0) * r4) + (uint64(h1) * r3) + (uint64(h2) * r2) + (uint64(h3) * r1) + (uint64(h4) * r0)
  33. // h %= p
  34. h0 = uint32(d0) & 0x3ffffff
  35. h1 = uint32(d1) & 0x3ffffff
  36. h2 = uint32(d2) & 0x3ffffff
  37. h3 = uint32(d3) & 0x3ffffff
  38. h4 = uint32(d4) & 0x3ffffff
  39. h0 += uint32(d4>>26) * 5
  40. h1 += h0 >> 26
  41. h0 = h0 & 0x3ffffff
  42. msg = msg[TagSize:]
  43. }
  44. if len(msg) > 0 {
  45. var block [TagSize]byte
  46. off := copy(block[:], msg)
  47. block[off] = 0x01
  48. // h += msg
  49. h0 += binary.LittleEndian.Uint32(block[0:]) & 0x3ffffff
  50. h1 += (binary.LittleEndian.Uint32(block[3:]) >> 2) & 0x3ffffff
  51. h2 += (binary.LittleEndian.Uint32(block[6:]) >> 4) & 0x3ffffff
  52. h3 += (binary.LittleEndian.Uint32(block[9:]) >> 6) & 0x3ffffff
  53. h4 += (binary.LittleEndian.Uint32(block[12:]) >> 8)
  54. // h *= r
  55. d0 := (uint64(h0) * r0) + (uint64(h1) * R4) + (uint64(h2) * R3) + (uint64(h3) * R2) + (uint64(h4) * R1)
  56. d1 := (d0 >> 26) + (uint64(h0) * r1) + (uint64(h1) * r0) + (uint64(h2) * R4) + (uint64(h3) * R3) + (uint64(h4) * R2)
  57. d2 := (d1 >> 26) + (uint64(h0) * r2) + (uint64(h1) * r1) + (uint64(h2) * r0) + (uint64(h3) * R4) + (uint64(h4) * R3)
  58. d3 := (d2 >> 26) + (uint64(h0) * r3) + (uint64(h1) * r2) + (uint64(h2) * r1) + (uint64(h3) * r0) + (uint64(h4) * R4)
  59. d4 := (d3 >> 26) + (uint64(h0) * r4) + (uint64(h1) * r3) + (uint64(h2) * r2) + (uint64(h3) * r1) + (uint64(h4) * r0)
  60. // h %= p
  61. h0 = uint32(d0) & 0x3ffffff
  62. h1 = uint32(d1) & 0x3ffffff
  63. h2 = uint32(d2) & 0x3ffffff
  64. h3 = uint32(d3) & 0x3ffffff
  65. h4 = uint32(d4) & 0x3ffffff
  66. h0 += uint32(d4>>26) * 5
  67. h1 += h0 >> 26
  68. h0 = h0 & 0x3ffffff
  69. }
  70. // h %= p reduction
  71. h2 += h1 >> 26
  72. h1 &= 0x3ffffff
  73. h3 += h2 >> 26
  74. h2 &= 0x3ffffff
  75. h4 += h3 >> 26
  76. h3 &= 0x3ffffff
  77. h0 += 5 * (h4 >> 26)
  78. h4 &= 0x3ffffff
  79. h1 += h0 >> 26
  80. h0 &= 0x3ffffff
  81. // h - p
  82. t0 := h0 + 5
  83. t1 := h1 + (t0 >> 26)
  84. t2 := h2 + (t1 >> 26)
  85. t3 := h3 + (t2 >> 26)
  86. t4 := h4 + (t3 >> 26) - (1 << 26)
  87. t0 &= 0x3ffffff
  88. t1 &= 0x3ffffff
  89. t2 &= 0x3ffffff
  90. t3 &= 0x3ffffff
  91. // select h if h < p else h - p
  92. t_mask := (t4 >> 31) - 1
  93. h_mask := ^t_mask
  94. h0 = (h0 & h_mask) | (t0 & t_mask)
  95. h1 = (h1 & h_mask) | (t1 & t_mask)
  96. h2 = (h2 & h_mask) | (t2 & t_mask)
  97. h3 = (h3 & h_mask) | (t3 & t_mask)
  98. h4 = (h4 & h_mask) | (t4 & t_mask)
  99. // h %= 2^128
  100. h0 |= h1 << 26
  101. h1 = ((h1 >> 6) | (h2 << 20))
  102. h2 = ((h2 >> 12) | (h3 << 14))
  103. h3 = ((h3 >> 18) | (h4 << 8))
  104. // s: the s part of the key
  105. // tag = (h + s) % (2^128)
  106. t := uint64(h0) + uint64(binary.LittleEndian.Uint32(key[16:]))
  107. h0 = uint32(t)
  108. t = uint64(h1) + uint64(binary.LittleEndian.Uint32(key[20:])) + (t >> 32)
  109. h1 = uint32(t)
  110. t = uint64(h2) + uint64(binary.LittleEndian.Uint32(key[24:])) + (t >> 32)
  111. h2 = uint32(t)
  112. t = uint64(h3) + uint64(binary.LittleEndian.Uint32(key[28:])) + (t >> 32)
  113. h3 = uint32(t)
  114. binary.LittleEndian.PutUint32(out[0:], h0)
  115. binary.LittleEndian.PutUint32(out[4:], h1)
  116. binary.LittleEndian.PutUint32(out[8:], h2)
  117. binary.LittleEndian.PutUint32(out[12:], h3)
  118. }