Skip to content

Commit b26134b

Browse files
mateusz834gopherbot
authored andcommitted
crypto: implement encoding.BinaryAppender for all crypto hashes
For #62384 Change-Id: I6fc7a7b8b85e02c880f1d16e0467f5076d477f0f GitHub-Last-Rev: 90ba7ba GitHub-Pull-Request: #68651 Reviewed-on: https://go-review.googlesource.com/c/go/+/601776 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 09eefb3 commit b26134b

File tree

13 files changed

+127
-31
lines changed

13 files changed

+127
-31
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The value returned by [md5.New] now also implements the [encoding.BinaryAppender] interface.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The value returned by [sha1.New] now also implements the [encoding.BinaryAppender] interface.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The values returned by [sha256.New] and [sha256.New224] now also implement the [encoding.BinaryAppender] interface
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The values returned by [sha512.New], [sha512.New384], [sha512.New512_224] and [sha512.New512_256] now also implement the [encoding.BinaryAppender] interface.

src/crypto/internal/boring/sha.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,19 @@ const (
159159
)
160160

161161
func (h *sha1Hash) MarshalBinary() ([]byte, error) {
162+
return h.AppendBinary(make([]byte, 0, sha1MarshaledSize))
163+
}
164+
165+
func (h *sha1Hash) AppendBinary(b []byte) ([]byte, error) {
162166
d := (*sha1Ctx)(unsafe.Pointer(&h.ctx))
163-
b := make([]byte, 0, sha1MarshaledSize)
164167
b = append(b, sha1Magic...)
165168
b = appendUint32(b, d.h[0])
166169
b = appendUint32(b, d.h[1])
167170
b = appendUint32(b, d.h[2])
168171
b = appendUint32(b, d.h[3])
169172
b = appendUint32(b, d.h[4])
170173
b = append(b, d.x[:d.nx]...)
171-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
174+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
172175
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
173176
return b, nil
174177
}
@@ -285,8 +288,11 @@ type sha256Ctx struct {
285288
}
286289

287290
func (h *sha224Hash) MarshalBinary() ([]byte, error) {
291+
return h.AppendBinary(make([]byte, 0, marshaledSize256))
292+
}
293+
294+
func (h *sha224Hash) AppendBinary(b []byte) ([]byte, error) {
288295
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
289-
b := make([]byte, 0, marshaledSize256)
290296
b = append(b, magic224...)
291297
b = appendUint32(b, d.h[0])
292298
b = appendUint32(b, d.h[1])
@@ -297,14 +303,17 @@ func (h *sha224Hash) MarshalBinary() ([]byte, error) {
297303
b = appendUint32(b, d.h[6])
298304
b = appendUint32(b, d.h[7])
299305
b = append(b, d.x[:d.nx]...)
300-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
306+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
301307
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
302308
return b, nil
303309
}
304310

305311
func (h *sha256Hash) MarshalBinary() ([]byte, error) {
312+
return h.AppendBinary(make([]byte, 0, marshaledSize256))
313+
}
314+
315+
func (h *sha256Hash) AppendBinary(b []byte) ([]byte, error) {
306316
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
307-
b := make([]byte, 0, marshaledSize256)
308317
b = append(b, magic256...)
309318
b = appendUint32(b, d.h[0])
310319
b = appendUint32(b, d.h[1])
@@ -315,7 +324,7 @@ func (h *sha256Hash) MarshalBinary() ([]byte, error) {
315324
b = appendUint32(b, d.h[6])
316325
b = appendUint32(b, d.h[7])
317326
b = append(b, d.x[:d.nx]...)
318-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
327+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
319328
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
320329
return b, nil
321330
}
@@ -462,8 +471,11 @@ const (
462471
)
463472

464473
func (h *sha384Hash) MarshalBinary() ([]byte, error) {
474+
return h.AppendBinary(make([]byte, 0, marshaledSize512))
475+
}
476+
477+
func (h *sha384Hash) AppendBinary(b []byte) ([]byte, error) {
465478
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
466-
b := make([]byte, 0, marshaledSize512)
467479
b = append(b, magic384...)
468480
b = appendUint64(b, d.h[0])
469481
b = appendUint64(b, d.h[1])
@@ -474,14 +486,17 @@ func (h *sha384Hash) MarshalBinary() ([]byte, error) {
474486
b = appendUint64(b, d.h[6])
475487
b = appendUint64(b, d.h[7])
476488
b = append(b, d.x[:d.nx]...)
477-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
489+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
478490
b = appendUint64(b, d.nl>>3|d.nh<<61)
479491
return b, nil
480492
}
481493

482494
func (h *sha512Hash) MarshalBinary() ([]byte, error) {
495+
return h.AppendBinary(make([]byte, 0, marshaledSize512))
496+
}
497+
498+
func (h *sha512Hash) AppendBinary(b []byte) ([]byte, error) {
483499
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
484-
b := make([]byte, 0, marshaledSize512)
485500
b = append(b, magic512...)
486501
b = appendUint64(b, d.h[0])
487502
b = appendUint64(b, d.h[1])
@@ -492,7 +507,7 @@ func (h *sha512Hash) MarshalBinary() ([]byte, error) {
492507
b = appendUint64(b, d.h[6])
493508
b = appendUint64(b, d.h[7])
494509
b = append(b, d.x[:d.nx]...)
495-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
510+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
496511
b = appendUint64(b, d.nl>>3|d.nh<<61)
497512
return b, nil
498513
}

src/crypto/md5/md5.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ const (
5757
)
5858

5959
func (d *digest) MarshalBinary() ([]byte, error) {
60-
b := make([]byte, 0, marshaledSize)
60+
return d.AppendBinary(make([]byte, 0, marshaledSize))
61+
}
62+
63+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
6164
b = append(b, magic...)
6265
b = byteorder.BeAppendUint32(b, d.s[0])
6366
b = byteorder.BeAppendUint32(b, d.s[1])
6467
b = byteorder.BeAppendUint32(b, d.s[2])
6568
b = byteorder.BeAppendUint32(b, d.s[3])
6669
b = append(b, d.x[:d.nx]...)
67-
b = b[:len(b)+len(d.x)-d.nx] // already zero
70+
b = append(b, make([]byte, len(d.x)-d.nx)...)
6871
b = byteorder.BeAppendUint64(b, d.len)
6972
return b, nil
7073
}
@@ -95,9 +98,10 @@ func consumeUint32(b []byte) ([]byte, uint32) {
9598
return b[4:], byteorder.BeUint32(b[0:4])
9699
}
97100

98-
// New returns a new hash.Hash computing the MD5 checksum. The Hash also
99-
// implements [encoding.BinaryMarshaler] and [encoding.BinaryUnmarshaler] to
100-
// marshal and unmarshal the internal state of the hash.
101+
// New returns a new [hash.Hash] computing the MD5 checksum. The Hash
102+
// also implements [encoding.BinaryMarshaler], [encoding.AppendBinary] and
103+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
104+
// state of the hash.
101105
func New() hash.Hash {
102106
d := new(digest)
103107
d.Reset()

src/crypto/md5/md5_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,23 @@ func TestGoldenMarshal(t *testing.T) {
100100
continue
101101
}
102102

103+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
104+
if err != nil {
105+
t.Errorf("could not marshal: %v", err)
106+
continue
107+
}
108+
stateAppend = stateAppend[4:]
109+
103110
if string(state) != g.halfState {
104111
t.Errorf("md5(%q) state = %q, want %q", g.in, state, g.halfState)
105112
continue
106113
}
107114

115+
if string(stateAppend) != g.halfState {
116+
t.Errorf("md5(%q) stateAppend = %q, want %q", g.in, stateAppend, g.halfState)
117+
continue
118+
}
119+
108120
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
109121
t.Errorf("could not unmarshal: %v", err)
110122
continue

src/crypto/sha1/sha1.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ const (
4949
)
5050

5151
func (d *digest) MarshalBinary() ([]byte, error) {
52-
b := make([]byte, 0, marshaledSize)
52+
return d.AppendBinary(make([]byte, 0, marshaledSize))
53+
}
54+
55+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
5356
b = append(b, magic...)
5457
b = byteorder.BeAppendUint32(b, d.h[0])
5558
b = byteorder.BeAppendUint32(b, d.h[1])
5659
b = byteorder.BeAppendUint32(b, d.h[2])
5760
b = byteorder.BeAppendUint32(b, d.h[3])
5861
b = byteorder.BeAppendUint32(b, d.h[4])
5962
b = append(b, d.x[:d.nx]...)
60-
b = b[:len(b)+len(d.x)-d.nx] // already zero
63+
b = append(b, make([]byte, len(d.x)-d.nx)...)
6164
b = byteorder.BeAppendUint64(b, d.len)
6265
return b, nil
6366
}
@@ -99,9 +102,10 @@ func (d *digest) Reset() {
99102
d.len = 0
100103
}
101104

102-
// New returns a new hash.Hash computing the SHA1 checksum. The Hash also
103-
// implements [encoding.BinaryMarshaler] and [encoding.BinaryUnmarshaler] to
104-
// marshal and unmarshal the internal state of the hash.
105+
// New512_224 returns a new [hash.Hash] computing the SHA1 checksum. The Hash
106+
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
107+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
108+
// state of the hash.
105109
func New() hash.Hash {
106110
if boring.Enabled {
107111
return boring.NewSHA1()

src/crypto/sha1/sha1_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,23 @@ func TestGoldenMarshal(t *testing.T) {
111111
continue
112112
}
113113

114+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
115+
if err != nil {
116+
t.Errorf("could not marshal: %v", err)
117+
continue
118+
}
119+
stateAppend = stateAppend[4:]
120+
114121
if string(state) != g.halfState {
115122
t.Errorf("sha1(%q) state = %+q, want %+q", g.in, state, g.halfState)
116123
continue
117124
}
118125

126+
if string(stateAppend) != g.halfState {
127+
t.Errorf("sha1(%q) stateAppend = %+q, want %+q", g.in, stateAppend, g.halfState)
128+
continue
129+
}
130+
119131
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
120132
t.Errorf("could not unmarshal: %v", err)
121133
continue

src/crypto/sha256/sha256.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ const (
6464
)
6565

6666
func (d *digest) MarshalBinary() ([]byte, error) {
67-
b := make([]byte, 0, marshaledSize)
67+
return d.AppendBinary(make([]byte, 0, marshaledSize))
68+
}
69+
70+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
6871
if d.is224 {
6972
b = append(b, magic224...)
7073
} else {
@@ -79,7 +82,7 @@ func (d *digest) MarshalBinary() ([]byte, error) {
7982
b = byteorder.BeAppendUint32(b, d.h[6])
8083
b = byteorder.BeAppendUint32(b, d.h[7])
8184
b = append(b, d.x[:d.nx]...)
82-
b = b[:len(b)+len(d.x)-d.nx] // already zero
85+
b = append(b, make([]byte, len(d.x)-d.nx)...)
8386
b = byteorder.BeAppendUint64(b, d.len)
8487
return b, nil
8588
}
@@ -138,8 +141,8 @@ func (d *digest) Reset() {
138141
d.len = 0
139142
}
140143

141-
// New returns a new hash.Hash computing the SHA256 checksum. The Hash
142-
// also implements [encoding.BinaryMarshaler] and
144+
// New returns a new [hash.Hash] computing the SHA256 checksum. The Hash
145+
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
143146
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
144147
// state of the hash.
145148
func New() hash.Hash {
@@ -151,7 +154,10 @@ func New() hash.Hash {
151154
return d
152155
}
153156

154-
// New224 returns a new hash.Hash computing the SHA224 checksum.
157+
// New224 returns a new [hash.Hash] computing the SHA224 checksum. The Hash
158+
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
159+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
160+
// state of the hash.
155161
func New224() hash.Hash {
156162
if boring.Enabled {
157163
return boring.NewSHA224()

src/crypto/sha256/sha256_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,23 @@ func TestGoldenMarshal(t *testing.T) {
163163
continue
164164
}
165165

166+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
167+
if err != nil {
168+
t.Errorf("could not marshal: %v", err)
169+
continue
170+
}
171+
stateAppend = stateAppend[4:]
172+
166173
if string(state) != g.halfState {
167174
t.Errorf("sha%s(%q) state = %q, want %q", tt.name, g.in, state, g.halfState)
168175
continue
169176
}
170177

178+
if string(stateAppend) != g.halfState {
179+
t.Errorf("sha%s(%q) stateAppend = %q, want %q", tt.name, g.in, stateAppend, g.halfState)
180+
continue
181+
}
182+
171183
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
172184
t.Errorf("could not unmarshal: %v", err)
173185
continue

src/crypto/sha512/sha512.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ const (
140140
)
141141

142142
func (d *digest) MarshalBinary() ([]byte, error) {
143-
b := make([]byte, 0, marshaledSize)
143+
return d.AppendBinary(make([]byte, 0, marshaledSize))
144+
}
145+
146+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
144147
switch d.function {
145148
case crypto.SHA384:
146149
b = append(b, magic384...)
@@ -162,7 +165,7 @@ func (d *digest) MarshalBinary() ([]byte, error) {
162165
b = byteorder.BeAppendUint64(b, d.h[6])
163166
b = byteorder.BeAppendUint64(b, d.h[7])
164167
b = append(b, d.x[:d.nx]...)
165-
b = b[:len(b)+len(d.x)-d.nx] // already zero
168+
b = append(b, make([]byte, len(d.x)-d.nx)...)
166169
b = byteorder.BeAppendUint64(b, d.len)
167170
return b, nil
168171
}
@@ -201,7 +204,10 @@ func consumeUint64(b []byte) ([]byte, uint64) {
201204
return b[8:], byteorder.BeUint64(b)
202205
}
203206

204-
// New returns a new hash.Hash computing the SHA-512 checksum.
207+
// New returns a new [hash.Hash] computing the SHA-512 checksum. The Hash
208+
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
209+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
210+
// state of the hash.
205211
func New() hash.Hash {
206212
if boring.Enabled {
207213
return boring.NewSHA512()
@@ -211,21 +217,30 @@ func New() hash.Hash {
211217
return d
212218
}
213219

214-
// New512_224 returns a new hash.Hash computing the SHA-512/224 checksum.
220+
// New512_224 returns a new [hash.Hash] computing the SHA-512/224 checksum. The Hash
221+
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
222+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
223+
// state of the hash.
215224
func New512_224() hash.Hash {
216225
d := &digest{function: crypto.SHA512_224}
217226
d.Reset()
218227
return d
219228
}
220229

221-
// New512_256 returns a new hash.Hash computing the SHA-512/256 checksum.
230+
// New512_256 returns a new [hash.Hash] computing the SHA-512/256 checksum. The Hash
231+
// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
232+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
233+
// state of the hash.
222234
func New512_256() hash.Hash {
223235
d := &digest{function: crypto.SHA512_256}
224236
d.Reset()
225237
return d
226238
}
227239

228-
// New384 returns a new hash.Hash computing the SHA-384 checksum.
240+
// New384 returns a new [hash.Hash] computing the SHA-384 checksum. The Hash
241+
// also implements [encoding.BinaryMarshaler], [encoding.AppendBinary] and
242+
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
243+
// state of the hash.
229244
func New384() hash.Hash {
230245
if boring.Enabled {
231246
return boring.NewSHA384()

src/crypto/sha512/sha512_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,23 @@ func TestGoldenMarshal(t *testing.T) {
745745
return
746746
}
747747

748+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
749+
if err != nil {
750+
t.Errorf("could not marshal: %v", err)
751+
return
752+
}
753+
stateAppend = stateAppend[4:]
754+
748755
if string(state) != test.halfState {
749756
t.Errorf("New%s(%q) state = %q, want %q", tt.name, test.in, state, test.halfState)
750757
continue
751758
}
752759

760+
if string(stateAppend) != test.halfState {
761+
t.Errorf("New%s(%q) stateAppend = %q, want %q", tt.name, test.in, stateAppend, test.halfState)
762+
continue
763+
}
764+
753765
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
754766
t.Errorf("could not unmarshal: %v", err)
755767
return

0 commit comments

Comments
 (0)