Debian Codegraph @

poly1305/poly1305.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 // Package poly1305 implements Poly1305 one-time message authentication code as
5 // specified in http://cr.yp.to/mac/poly1305-20050329.pdf.
6 //
7 // Poly1305 is a fast, one-time authentication function. It is infeasible for an
8 // attacker to generate an authenticator for a message without the key.
9 // However, a key must only be used for a single message. Authenticating two
10 // different messages with the same key allows an attacker to forge
11 // authenticators for other messages with the same key.
12 //
13 // Poly1305 was originally coupled with AES in order to make Poly1305-AES.
14 // AES was used with a fixed key in order to generate one-time keys from an
15 // nonce. However, in this package AES isn't used and the one-time key is
16 // specified directly.
17 package poly1305
18
19 import (
20 "crypto/subtle"
21 "errors"
22 )
23
24 // The size of the poly1305 authentication tag in bytes.
25 const TagSize = 16
26
27 const (
28 msgBlock = uint32(1 << 24)
29 finalBlock = uint32(0)
30 )
31
32 // Verify returns true if and only if the mac is a valid authenticator
33 // for msg with the given key.
34 func Verify(mac *[TagSize]byte, msg []byte, key *[32]byte) bool {
35 var sum [TagSize]byte
36 Sum(&sum, msg, key)
37 return subtle.ConstantTimeCompare(sum[:], mac[:]) == 1
38 }
39
40 // New returns a hash.Hash computing the poly1305 sum.
41 // Notice that Poly1305 is inseure if one key is used twice.
42 func New(key *[32]byte) *Hash {
43 p := new(Hash)
44 initialize(&(p.r), &(p.pad), key)
45 return p
46 }
47
48 var writeAfterSumErr error = errors.New("checksum already computed - adding more data is not allowed")
49
50 // Hash implements a Poly1305 writer interface.
51 // Poly1305 cannot used like common hash.Hash implementations,
52 // beause of using a Poly1305 key twice breaks its security.
53 // So poly1305.Hash does not support some kind of reset.
54 type Hash struct {
55 h, r [5]uint32
56 pad [4]uint32
57
58 buf [TagSize]byte
59 off int
60 done bool
61 }
62
63 // Write adds more data to the running Poly1305 hash.
64 // This function returns an non-nil error, if a call
65 // to Write happens after the hash's Sum function was
66 // called. So it's not possible to compute the checksum
67 // and than add more data.
68 func (p *Hash) Write(msg []byte) (int, error) {
69 if p.done {
70 return 0, writeAfterSumErr
71 }
72 n := len(msg)
73
74 if p.off > 0 {
75 dif := TagSize - p.off
76 if n > dif {
77 p.off += copy(p.buf[p.off:], msg[:dif])
78 msg = msg[dif:]
79 core(p.buf[:], msgBlock, &(p.h), &(p.r))
80 p.off = 0
81 } else {
82 p.off += copy(p.buf[p.off:], msg)
83 return n, nil
84 }
85 }
86
87 length := len(msg) & (^(TagSize - 1))
88 if length > 0 {
89 core(msg[:length], msgBlock, &(p.h), &(p.r))
90 msg = msg[length:]
91 }
92 if len(msg) > 0 {
93 p.off += copy(p.buf[p.off:], msg)
94 }
95
96 return n, nil
97 }
98
99 // Sum computes the Poly1305 checksum of the prevouisly
100 // processed data and writes it to out. It is legal to
101 // call this function more than one time.
102 func (p *Hash) Sum(out *[TagSize]byte) {
103 h, r := p.h, p.r
104 pad := p.pad
105
106 if p.off > 0 {
107 var buf [TagSize]byte
108 copy(buf[:], p.buf[:p.off])
109 buf[p.off] = 1 // invariant: p.off < TagSize
110
111 core(buf[:], finalBlock, &h, &r)
112 }
113
114 finalize(out, &h, &pad)
115 p.done = true
116 }
117
118 func finalize(tag *[TagSize]byte, h *[5]uint32, pad *[4]uint32) {
119 var g0, g1, g2, g3, g4 uint32
120
121 // fully carry h
122 h0, h1, h2, h3, h4 := h[0], h[1], h[2], h[3], h[4]
123
124 h2 += h1 >> 26
125 h1 &= 0x3ffffff
126 h3 += h2 >> 26
127 h2 &= 0x3ffffff
128 h4 += h3 >> 26
129 h3 &= 0x3ffffff
130 h0 += 5 * (h4 >> 26)
131 h4 &= 0x3ffffff
132 h1 += h0 >> 26
133 h0 &= 0x3ffffff
134
135 // h + -p
136 g0 = h0 + 5
137
138 g1 = h1 + (g0 >> 26)
139 g0 &= 0x3ffffff
140 g2 = h2 + (g1 >> 26)
141 g1 &= 0x3ffffff
142 g3 = h3 + (g2 >> 26)
143 g2 &= 0x3ffffff
144 g4 = h4 + (g3 >> 26) - (1 << 26)
145 g3 &= 0x3ffffff
146
147 // select h if h < p else h + -p
148 mask := (g4 >> (32 - 1)) - 1
149 g0 &= mask
150 g1 &= mask
151 g2 &= mask
152 g3 &= mask
153 g4 &= mask
154 mask = ^mask
155 h0 = (h0 & mask) | g0
156 h1 = (h1 & mask) | g1
157 h2 = (h2 & mask) | g2
158 h3 = (h3 & mask) | g3
159 h4 = (h4 & mask) | g4
160
161 // h %= 2^128
162 h0 |= h1 << 26
163 h1 = ((h1 >> 6) | (h2 << 20))
164 h2 = ((h2 >> 12) | (h3 << 14))
165 h3 = ((h3 >> 18) | (h4 << 8))
166
167 // tag = (h + pad) % (2^128)
168 f := uint64(h0) + uint64(pad[0])
169 h0 = uint32(f)
170 f = uint64(h1) + uint64(pad[1]) + (f >> 32)
171 h1 = uint32(f)
172 f = uint64(h2) + uint64(pad[2]) + (f >> 32)
173 h2 = uint32(f)
174 f = uint64(h3) + uint64(pad[3]) + (f >> 32)
175 h3 = uint32(f)
176
177 extractHash(tag, h0, h1, h2, h3)
178 }