sum_amd64.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // +build amd64,!gccgo,!appengine
  5. package poly1305
  6. //go:noescape
  7. func update(state *macState, msg []byte)
  8. func sum(out *[16]byte, m []byte, key *[32]byte) {
  9. h := newMAC(key)
  10. h.Write(m)
  11. h.Sum(out)
  12. }
  13. func newMAC(key *[32]byte) (h mac) {
  14. initialize(key, &h.r, &h.s)
  15. return
  16. }
  17. // mac is a wrapper for macGeneric that redirects calls that would have gone to
  18. // updateGeneric to update.
  19. //
  20. // Its Write and Sum methods are otherwise identical to the macGeneric ones, but
  21. // using function pointers would carry a major performance cost.
  22. type mac struct{ macGeneric }
  23. func (h *mac) Write(p []byte) (int, error) {
  24. nn := len(p)
  25. if h.offset > 0 {
  26. n := copy(h.buffer[h.offset:], p)
  27. if h.offset+n < TagSize {
  28. h.offset += n
  29. return nn, nil
  30. }
  31. p = p[n:]
  32. h.offset = 0
  33. update(&h.macState, h.buffer[:])
  34. }
  35. if n := len(p) - (len(p) % TagSize); n > 0 {
  36. update(&h.macState, p[:n])
  37. p = p[n:]
  38. }
  39. if len(p) > 0 {
  40. h.offset += copy(h.buffer[h.offset:], p)
  41. }
  42. return nn, nil
  43. }
  44. func (h *mac) Sum(out *[16]byte) {
  45. state := h.macState
  46. if h.offset > 0 {
  47. update(&state, h.buffer[:h.offset])
  48. }
  49. finalize(out, &state.h, &state.s)
  50. }