sum_amd64.go 1.1 KB

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