-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyval.go
276 lines (243 loc) · 6.9 KB
/
polyval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Package polyval implements POLYVAL per RFC 8452.
//
// The universal hash function POLYVAL is the byte-wise reverse
// of GHASH.
//
// [rfc8452]: https://datatracker.ietf.org/doc/html/rfc8452#section-3
// [gueron]: https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf
package polyval
import (
"encoding"
"encoding/binary"
"errors"
"fmt"
"github.com/ericlagergren/subtle"
)
//go:generate go run github.com/ericlagergren/polyval/internal/cmd/gen ctmul
const (
// Size is the size in bytes of a POLYVAL checksum.
Size = 16
)
// Sum returns the POLYVAL hash of data.
func Sum(key, data []byte) [Size]byte {
var p Polyval
if err := p.Init(key); err != nil {
panic(err)
}
p.Update(data)
return *(*[Size]byte)(p.Sum(nil))
}
// Polyval is an implementation of POLYVAL.
//
// It operates similar to the standard library's Hash interface,
// but only accepts full blocks. Callers should pad the input
// accordingly.
//
// POLYVAL is similar to GHASH. It operates in GF(2^128) defined
// by the irreducible polynomial
//
// x^128 + x^127 + x^126 + x^121 + 1.
//
// The field has characteristic 2, so addition is performed with
// XOR. Multiplication is polynomial multiplication reduced
// modulo the polynomial.
//
// For more information on POLYVAL, see [rfc8452].
type Polyval struct {
// Make Polyval non-comparable to prevent accidental
// non-constant time comparisons.
_ [0]func()
// h is the hash key.
h fieldElement
// y is the running state.
y fieldElement
// pow is a pre-computed table of powers of h for writing
// groups of eight blocks.
pow [8]fieldElement
}
var (
_ encoding.BinaryMarshaler
_ encoding.BinaryUnmarshaler
)
// New creates a Polyval.
//
// The key must be exactly 16 bytes long and cannot be all zero.
func New(key []byte) (*Polyval, error) {
var p Polyval
if err := p.Init(key); err != nil {
return nil, err
}
return &p, nil
}
// Init initializes a Polyval.
//
// The key must be exactly 16 bytes long and cannot be all zero.
func (p *Polyval) Init(key []byte) error {
if len(key) != 16 {
return fmt.Errorf("invalid key size: %d", len(key))
}
if subtle.ConstantTimeBigEndianZero(key) == 1 {
return errors.New("the zero key is invalid")
}
p.h.setBytes(key)
p.pow[len(p.pow)-1] = p.h
for i := len(p.pow) - 2; i >= 0; i-- {
p.pow[i] = p.h
polymul(&p.pow[i], &p.pow[i+1])
}
return nil
}
// Size returns the size of a POLYVAL digest.
func (p *Polyval) Size() int {
return Size
}
// BlockSize returns the size of a POLYVAL block.
func (p *Polyval) BlockSize() int {
return 16
}
// Reset sets the hash to its original state.
func (p *Polyval) Reset() {
p.y = fieldElement{}
}
// Update writes one or more blocks to the running hash.
//
// If len(block) is not divisible by BlockSize, Update will panic.
func (p *Polyval) Update(blocks []byte) {
if len(blocks)%16 != 0 {
panic("polyval: invalid input length")
}
polymulBlocks(&p.y, &p.pow, blocks)
}
// Sum appends the current hash to b and returns the resulting
// slice.
//
// It does not change the underlying hash state.
func (p *Polyval) Sum(b []byte) []byte {
ret, out := subtle.SliceForAppend(b, 16)
binary.LittleEndian.PutUint64(out[0:8], p.y.lo)
binary.LittleEndian.PutUint64(out[8:16], p.y.hi)
return ret
}
// MarshalBinary implements BinaryMarshaler.
//
// It does not return an error.
func (p *Polyval) MarshalBinary() ([]byte, error) {
buf := make([]byte, 16*(2+len(p.pow)))
binary.LittleEndian.PutUint64(buf[0:], p.h.lo)
binary.LittleEndian.PutUint64(buf[8:], p.h.hi)
binary.LittleEndian.PutUint64(buf[16:], p.y.lo)
binary.LittleEndian.PutUint64(buf[24:], p.y.hi)
for i, x := range p.pow {
binary.LittleEndian.PutUint64(buf[32+(i*16):], x.lo)
binary.LittleEndian.PutUint64(buf[40+(i*16):], x.hi)
}
return buf, nil
}
// Unmarshalbinary implements BinaryUnmarshaler.
//
// data must be exactly 160 bytes.
func (p *Polyval) UnmarshalBinary(data []byte) error {
if len(data) != 16*(2+len(p.pow)) {
return fmt.Errorf("invalid data size: %d", len(data))
}
p.h.lo = binary.LittleEndian.Uint64(data[0:8])
p.h.hi = binary.LittleEndian.Uint64(data[8:16])
p.y.lo = binary.LittleEndian.Uint64(data[16:24])
p.y.hi = binary.LittleEndian.Uint64(data[24:32])
for i, x := range p.pow {
x.lo = binary.LittleEndian.Uint64(data[32+(i*16):])
x.hi = binary.LittleEndian.Uint64(data[40+(i*16):])
p.pow[i] = x
}
return nil
}
func polymulGeneric(acc, key *fieldElement) {
x, y := key, acc
// We perform schoolbook multiplication of x and y:
//
// (x1,x0)*(y1,y0) = (x1*y1) + (x1*y0 + x0*y1) + (x0*y0)
// H M M L
//
// The middle result (M) can be simplified with Karatsuba
// multiplication:
//
// (x1*y0 + x0*y1) = (x1+x0) * (y1+x0) + (x1*y1) + (x0*y0)
// M H L
//
// This requires one less 64-bit multiplication and reuses
// the existing results H and L. (H and L are added to M in
// the montgomery reduction; see x1 and x2.)
//
// This gives us a 256-bit product, X.
//
// Use the "Shift-XOR reflected reduction" method to reduce
// it modulo x^128 + x^127 + x^126 + x^121 + 1.
//
// This is faster than Gueron's "Fast reduction ..." method
// because Go doesn't have CMUL/PMULL intrinsics.
//
// See [gueron] page 17-19.
h1, h0 := ctmul(x.hi, y.hi) // H
l1, l0 := ctmul(x.lo, y.lo) // L
m1, m0 := ctmul(x.hi^x.lo, y.hi^y.lo) // M
m0 ^= l0 ^ h0
m1 ^= l1 ^ h1
l1 ^= m0 ^ (l0 << 63) ^ (l0 << 62) ^ (l0 << 57)
h0 ^= l0 ^ (l0 >> 1) ^ (l0 >> 2) ^ (l0 >> 7)
h0 ^= m1 ^ (l1 << 63) ^ (l1 << 62) ^ (l1 << 57)
h1 ^= l1 ^ (l1 >> 1) ^ (l1 >> 2) ^ (l1 >> 7)
y.hi = h1
y.lo = h0
}
func polymulBlocksGeneric(acc *fieldElement, pow *[8]fieldElement, blocks []byte) {
for (len(blocks)/16)%8 != 0 {
acc.lo ^= binary.LittleEndian.Uint64(blocks[0:8])
acc.hi ^= binary.LittleEndian.Uint64(blocks[8:16])
polymulGeneric(acc, &pow[len(pow)-1])
blocks = blocks[16:]
}
const (
wide = 16 * len(pow)
)
for len(blocks) >= wide {
var h1, h0, l1, l0, m1, m0 uint64
for i, x := range pow {
var y fieldElement
y.setBytes(blocks[:16])
if i == 0 {
y.lo ^= acc.lo
y.hi ^= acc.hi
}
t1, t0 := ctmul(x.hi, y.hi)
h1 ^= t1
h0 ^= t0
t1, t0 = ctmul(x.lo, y.lo)
l1 ^= t1
l0 ^= t0
t1, t0 = ctmul(x.hi^x.lo, y.hi^y.lo)
m1 ^= t1
m0 ^= t0
blocks = blocks[16:]
}
m0 ^= l0 ^ h0
m1 ^= l1 ^ h1
l1 ^= m0 ^ (l0 << 63) ^ (l0 << 62) ^ (l0 << 57)
h0 ^= l0 ^ (l0 >> 1) ^ (l0 >> 2) ^ (l0 >> 7)
h0 ^= m1 ^ (l1 << 63) ^ (l1 << 62) ^ (l1 << 57)
h1 ^= l1 ^ (l1 >> 1) ^ (l1 >> 2) ^ (l1 >> 7)
acc.hi = h1
acc.lo = h0
}
}
// fieldElement is a little-endian element in GF(2^128).
type fieldElement struct {
lo, hi uint64
}
func (f fieldElement) String() string {
return fmt.Sprintf("%#0.16x%0.16x", f.hi, f.lo)
}
// setBytes sets z to the little-endian element p.
func (z *fieldElement) setBytes(p []byte) {
z.lo = binary.LittleEndian.Uint64(p[0:8])
z.hi = binary.LittleEndian.Uint64(p[8:16])
}