sum_amd64.go 1.1 KB

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