1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package poly1305
- func initialize(state *[7]uint64, key *[32]byte)
- func update(state *[7]uint64, msg []byte)
- func finalize(tag *[TagSize]byte, state *[7]uint64)
- func Sum(out *[16]byte, m []byte, key *[32]byte) {
- h := newMAC(key)
- h.Write(m)
- h.Sum(out)
- }
- func newMAC(key *[32]byte) (h mac) {
- initialize(&h.state, key)
- return
- }
- type mac struct {
- state [7]uint64
- buffer [TagSize]byte
- offset int
- }
- func (h *mac) Write(p []byte) (n int, err error) {
- n = len(p)
- if h.offset > 0 {
- remaining := TagSize - h.offset
- if n < remaining {
- h.offset += copy(h.buffer[h.offset:], p)
- return n, nil
- }
- copy(h.buffer[h.offset:], p[:remaining])
- p = p[remaining:]
- h.offset = 0
- update(&h.state, h.buffer[:])
- }
- if nn := len(p) - (len(p) % TagSize); nn > 0 {
- update(&h.state, p[:nn])
- p = p[nn:]
- }
- if len(p) > 0 {
- h.offset += copy(h.buffer[h.offset:], p)
- }
- return n, nil
- }
- func (h *mac) Sum(out *[16]byte) {
- state := h.state
- if h.offset > 0 {
- update(&state, h.buffer[:h.offset])
- }
- finalize(out, &state)
- }
|