Source file src/vendor/golang.org/x/crypto/poly1305/sum_generic.go
1
2
3
4
5 package poly1305
6
7 import "encoding/binary"
8
9 const (
10 msgBlock = uint32(1 << 24)
11 finalBlock = uint32(0)
12 )
13
14
15
16
17 func sumGeneric(out *[TagSize]byte, msg []byte, key *[32]byte) {
18 h := newMACGeneric(key)
19 h.Write(msg)
20 h.Sum(out)
21 }
22
23 func newMACGeneric(key *[32]byte) (h macGeneric) {
24 h.r[0] = binary.LittleEndian.Uint32(key[0:]) & 0x3ffffff
25 h.r[1] = (binary.LittleEndian.Uint32(key[3:]) >> 2) & 0x3ffff03
26 h.r[2] = (binary.LittleEndian.Uint32(key[6:]) >> 4) & 0x3ffc0ff
27 h.r[3] = (binary.LittleEndian.Uint32(key[9:]) >> 6) & 0x3f03fff
28 h.r[4] = (binary.LittleEndian.Uint32(key[12:]) >> 8) & 0x00fffff
29
30 h.s[0] = binary.LittleEndian.Uint32(key[16:])
31 h.s[1] = binary.LittleEndian.Uint32(key[20:])
32 h.s[2] = binary.LittleEndian.Uint32(key[24:])
33 h.s[3] = binary.LittleEndian.Uint32(key[28:])
34 return
35 }
36
37 type macGeneric struct {
38 h, r [5]uint32
39 s [4]uint32
40
41 buffer [TagSize]byte
42 offset int
43 }
44
45 func (h *macGeneric) Write(p []byte) (n int, err error) {
46 n = len(p)
47 if h.offset > 0 {
48 remaining := TagSize - h.offset
49 if n < remaining {
50 h.offset += copy(h.buffer[h.offset:], p)
51 return n, nil
52 }
53 copy(h.buffer[h.offset:], p[:remaining])
54 p = p[remaining:]
55 h.offset = 0
56 updateGeneric(h.buffer[:], msgBlock, &(h.h), &(h.r))
57 }
58 if nn := len(p) - (len(p) % TagSize); nn > 0 {
59 updateGeneric(p, msgBlock, &(h.h), &(h.r))
60 p = p[nn:]
61 }
62 if len(p) > 0 {
63 h.offset += copy(h.buffer[h.offset:], p)
64 }
65 return n, nil
66 }
67
68 func (h *macGeneric) Sum(out *[16]byte) {
69 H, R := h.h, h.r
70 if h.offset > 0 {
71 var buffer [TagSize]byte
72 copy(buffer[:], h.buffer[:h.offset])
73 buffer[h.offset] = 1
74 updateGeneric(buffer[:], finalBlock, &H, &R)
75 }
76 finalizeGeneric(out, &H, &(h.s))
77 }
78
79 func updateGeneric(msg []byte, flag uint32, h, r *[5]uint32) {
80 h0, h1, h2, h3, h4 := h[0], h[1], h[2], h[3], h[4]
81 r0, r1, r2, r3, r4 := uint64(r[0]), uint64(r[1]), uint64(r[2]), uint64(r[3]), uint64(r[4])
82 R1, R2, R3, R4 := r1*5, r2*5, r3*5, r4*5
83
84 for len(msg) >= TagSize {
85
86 h0 += binary.LittleEndian.Uint32(msg[0:]) & 0x3ffffff
87 h1 += (binary.LittleEndian.Uint32(msg[3:]) >> 2) & 0x3ffffff
88 h2 += (binary.LittleEndian.Uint32(msg[6:]) >> 4) & 0x3ffffff
89 h3 += (binary.LittleEndian.Uint32(msg[9:]) >> 6) & 0x3ffffff
90 h4 += (binary.LittleEndian.Uint32(msg[12:]) >> 8) | flag
91
92
93 d0 := (uint64(h0) * r0) + (uint64(h1) * R4) + (uint64(h2) * R3) + (uint64(h3) * R2) + (uint64(h4) * R1)
94 d1 := (d0 >> 26) + (uint64(h0) * r1) + (uint64(h1) * r0) + (uint64(h2) * R4) + (uint64(h3) * R3) + (uint64(h4) * R2)
95 d2 := (d1 >> 26) + (uint64(h0) * r2) + (uint64(h1) * r1) + (uint64(h2) * r0) + (uint64(h3) * R4) + (uint64(h4) * R3)
96 d3 := (d2 >> 26) + (uint64(h0) * r3) + (uint64(h1) * r2) + (uint64(h2) * r1) + (uint64(h3) * r0) + (uint64(h4) * R4)
97 d4 := (d3 >> 26) + (uint64(h0) * r4) + (uint64(h1) * r3) + (uint64(h2) * r2) + (uint64(h3) * r1) + (uint64(h4) * r0)
98
99
100 h0 = uint32(d0) & 0x3ffffff
101 h1 = uint32(d1) & 0x3ffffff
102 h2 = uint32(d2) & 0x3ffffff
103 h3 = uint32(d3) & 0x3ffffff
104 h4 = uint32(d4) & 0x3ffffff
105
106 h0 += uint32(d4>>26) * 5
107 h1 += h0 >> 26
108 h0 = h0 & 0x3ffffff
109
110 msg = msg[TagSize:]
111 }
112
113 h[0], h[1], h[2], h[3], h[4] = h0, h1, h2, h3, h4
114 }
115
116 func finalizeGeneric(out *[TagSize]byte, h *[5]uint32, s *[4]uint32) {
117 h0, h1, h2, h3, h4 := h[0], h[1], h[2], h[3], h[4]
118
119
120 h2 += h1 >> 26
121 h1 &= 0x3ffffff
122 h3 += h2 >> 26
123 h2 &= 0x3ffffff
124 h4 += h3 >> 26
125 h3 &= 0x3ffffff
126 h0 += 5 * (h4 >> 26)
127 h4 &= 0x3ffffff
128 h1 += h0 >> 26
129 h0 &= 0x3ffffff
130
131
132 t0 := h0 + 5
133 t1 := h1 + (t0 >> 26)
134 t2 := h2 + (t1 >> 26)
135 t3 := h3 + (t2 >> 26)
136 t4 := h4 + (t3 >> 26) - (1 << 26)
137 t0 &= 0x3ffffff
138 t1 &= 0x3ffffff
139 t2 &= 0x3ffffff
140 t3 &= 0x3ffffff
141
142
143 t_mask := (t4 >> 31) - 1
144 h_mask := ^t_mask
145 h0 = (h0 & h_mask) | (t0 & t_mask)
146 h1 = (h1 & h_mask) | (t1 & t_mask)
147 h2 = (h2 & h_mask) | (t2 & t_mask)
148 h3 = (h3 & h_mask) | (t3 & t_mask)
149 h4 = (h4 & h_mask) | (t4 & t_mask)
150
151
152 h0 |= h1 << 26
153 h1 = ((h1 >> 6) | (h2 << 20))
154 h2 = ((h2 >> 12) | (h3 << 14))
155 h3 = ((h3 >> 18) | (h4 << 8))
156
157
158
159 t := uint64(h0) + uint64(s[0])
160 h0 = uint32(t)
161 t = uint64(h1) + uint64(s[1]) + (t >> 32)
162 h1 = uint32(t)
163 t = uint64(h2) + uint64(s[2]) + (t >> 32)
164 h2 = uint32(t)
165 t = uint64(h3) + uint64(s[3]) + (t >> 32)
166 h3 = uint32(t)
167
168 binary.LittleEndian.PutUint32(out[0:], h0)
169 binary.LittleEndian.PutUint32(out[4:], h1)
170 binary.LittleEndian.PutUint32(out[8:], h2)
171 binary.LittleEndian.PutUint32(out[12:], h3)
172 }
173
View as plain text