Skip to content

Commit 6aeab9e

Browse files
committed
boringcrypto support
Change-Id: Ic633fe6dea984cdf06301b687990ad871ed6f4b5
1 parent b99c30a commit 6aeab9e

File tree

1 file changed

+25
-12
lines changed
  • src/crypto/internal/boring

1 file changed

+25
-12
lines changed

src/crypto/internal/boring/sha.go

Lines changed: 25 additions & 12 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
}
@@ -455,15 +464,16 @@ type sha512Ctx struct {
455464

456465
const (
457466
magic384 = "sha\x04"
458-
magic512_224 = "sha\x05"
459-
magic512_256 = "sha\x06"
460467
magic512 = "sha\x07"
461468
marshaledSize512 = len(magic512) + 8*8 + 128 + 8
462469
)
463470

464471
func (h *sha384Hash) MarshalBinary() ([]byte, error) {
472+
return h.AppendBinary(make([]byte, 0, marshaledSize512))
473+
}
474+
475+
func (h *sha384Hash) AppendBinary(b []byte) ([]byte, error) {
465476
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
466-
b := make([]byte, 0, marshaledSize512)
467477
b = append(b, magic384...)
468478
b = appendUint64(b, d.h[0])
469479
b = appendUint64(b, d.h[1])
@@ -474,14 +484,17 @@ func (h *sha384Hash) MarshalBinary() ([]byte, error) {
474484
b = appendUint64(b, d.h[6])
475485
b = appendUint64(b, d.h[7])
476486
b = append(b, d.x[:d.nx]...)
477-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
487+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
478488
b = appendUint64(b, d.nl>>3|d.nh<<61)
479489
return b, nil
480490
}
481491

482492
func (h *sha512Hash) MarshalBinary() ([]byte, error) {
493+
return h.AppendBinary(make([]byte, 0, marshaledSize512))
494+
}
495+
496+
func (h *sha512Hash) AppendBinary(b []byte) ([]byte, error) {
483497
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
484-
b := make([]byte, 0, marshaledSize512)
485498
b = append(b, magic512...)
486499
b = appendUint64(b, d.h[0])
487500
b = appendUint64(b, d.h[1])
@@ -492,7 +505,7 @@ func (h *sha512Hash) MarshalBinary() ([]byte, error) {
492505
b = appendUint64(b, d.h[6])
493506
b = appendUint64(b, d.h[7])
494507
b = append(b, d.x[:d.nx]...)
495-
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
508+
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
496509
b = appendUint64(b, d.nl>>3|d.nh<<61)
497510
return b, nil
498511
}

0 commit comments

Comments
 (0)