Skip to content

Commit 0ea534b

Browse files
mateusz834gopherbot
authored andcommitted
crypto/internal/boring: use internal/byteorder
Change-Id: I882dd4160fb24b1d605af28bca01227716dac717 GitHub-Last-Rev: c631b19 GitHub-Pull-Request: #68697 Reviewed-on: https://go-review.googlesource.com/c/go/+/602179 Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Zxilly Chou <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent b915399 commit 0ea534b

File tree

1 file changed

+45
-81
lines changed
  • src/crypto/internal/boring

1 file changed

+45
-81
lines changed

src/crypto/internal/boring/sha.go

Lines changed: 45 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import "C"
5959
import (
6060
"errors"
6161
"hash"
62+
"internal/byteorder"
6263
"unsafe"
6364
)
6465

@@ -165,14 +166,14 @@ func (h *sha1Hash) MarshalBinary() ([]byte, error) {
165166
func (h *sha1Hash) AppendBinary(b []byte) ([]byte, error) {
166167
d := (*sha1Ctx)(unsafe.Pointer(&h.ctx))
167168
b = append(b, sha1Magic...)
168-
b = appendUint32(b, d.h[0])
169-
b = appendUint32(b, d.h[1])
170-
b = appendUint32(b, d.h[2])
171-
b = appendUint32(b, d.h[3])
172-
b = appendUint32(b, d.h[4])
169+
b = byteorder.BeAppendUint32(b, d.h[0])
170+
b = byteorder.BeAppendUint32(b, d.h[1])
171+
b = byteorder.BeAppendUint32(b, d.h[2])
172+
b = byteorder.BeAppendUint32(b, d.h[3])
173+
b = byteorder.BeAppendUint32(b, d.h[4])
173174
b = append(b, d.x[:d.nx]...)
174175
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
175-
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
176+
b = byteorder.BeAppendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
176177
return b, nil
177178
}
178179

@@ -294,17 +295,17 @@ func (h *sha224Hash) MarshalBinary() ([]byte, error) {
294295
func (h *sha224Hash) AppendBinary(b []byte) ([]byte, error) {
295296
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
296297
b = append(b, magic224...)
297-
b = appendUint32(b, d.h[0])
298-
b = appendUint32(b, d.h[1])
299-
b = appendUint32(b, d.h[2])
300-
b = appendUint32(b, d.h[3])
301-
b = appendUint32(b, d.h[4])
302-
b = appendUint32(b, d.h[5])
303-
b = appendUint32(b, d.h[6])
304-
b = appendUint32(b, d.h[7])
298+
b = byteorder.BeAppendUint32(b, d.h[0])
299+
b = byteorder.BeAppendUint32(b, d.h[1])
300+
b = byteorder.BeAppendUint32(b, d.h[2])
301+
b = byteorder.BeAppendUint32(b, d.h[3])
302+
b = byteorder.BeAppendUint32(b, d.h[4])
303+
b = byteorder.BeAppendUint32(b, d.h[5])
304+
b = byteorder.BeAppendUint32(b, d.h[6])
305+
b = byteorder.BeAppendUint32(b, d.h[7])
305306
b = append(b, d.x[:d.nx]...)
306307
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
307-
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
308+
b = byteorder.BeAppendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
308309
return b, nil
309310
}
310311

@@ -315,17 +316,17 @@ func (h *sha256Hash) MarshalBinary() ([]byte, error) {
315316
func (h *sha256Hash) AppendBinary(b []byte) ([]byte, error) {
316317
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
317318
b = append(b, magic256...)
318-
b = appendUint32(b, d.h[0])
319-
b = appendUint32(b, d.h[1])
320-
b = appendUint32(b, d.h[2])
321-
b = appendUint32(b, d.h[3])
322-
b = appendUint32(b, d.h[4])
323-
b = appendUint32(b, d.h[5])
324-
b = appendUint32(b, d.h[6])
325-
b = appendUint32(b, d.h[7])
319+
b = byteorder.BeAppendUint32(b, d.h[0])
320+
b = byteorder.BeAppendUint32(b, d.h[1])
321+
b = byteorder.BeAppendUint32(b, d.h[2])
322+
b = byteorder.BeAppendUint32(b, d.h[3])
323+
b = byteorder.BeAppendUint32(b, d.h[4])
324+
b = byteorder.BeAppendUint32(b, d.h[5])
325+
b = byteorder.BeAppendUint32(b, d.h[6])
326+
b = byteorder.BeAppendUint32(b, d.h[7])
326327
b = append(b, d.x[:d.nx]...)
327328
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
328-
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
329+
b = byteorder.BeAppendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
329330
return b, nil
330331
}
331332

@@ -477,17 +478,17 @@ func (h *sha384Hash) MarshalBinary() ([]byte, error) {
477478
func (h *sha384Hash) AppendBinary(b []byte) ([]byte, error) {
478479
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
479480
b = append(b, magic384...)
480-
b = appendUint64(b, d.h[0])
481-
b = appendUint64(b, d.h[1])
482-
b = appendUint64(b, d.h[2])
483-
b = appendUint64(b, d.h[3])
484-
b = appendUint64(b, d.h[4])
485-
b = appendUint64(b, d.h[5])
486-
b = appendUint64(b, d.h[6])
487-
b = appendUint64(b, d.h[7])
481+
b = byteorder.BeAppendUint64(b, d.h[0])
482+
b = byteorder.BeAppendUint64(b, d.h[1])
483+
b = byteorder.BeAppendUint64(b, d.h[2])
484+
b = byteorder.BeAppendUint64(b, d.h[3])
485+
b = byteorder.BeAppendUint64(b, d.h[4])
486+
b = byteorder.BeAppendUint64(b, d.h[5])
487+
b = byteorder.BeAppendUint64(b, d.h[6])
488+
b = byteorder.BeAppendUint64(b, d.h[7])
488489
b = append(b, d.x[:d.nx]...)
489490
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
490-
b = appendUint64(b, d.nl>>3|d.nh<<61)
491+
b = byteorder.BeAppendUint64(b, d.nl>>3|d.nh<<61)
491492
return b, nil
492493
}
493494

@@ -498,17 +499,17 @@ func (h *sha512Hash) MarshalBinary() ([]byte, error) {
498499
func (h *sha512Hash) AppendBinary(b []byte) ([]byte, error) {
499500
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
500501
b = append(b, magic512...)
501-
b = appendUint64(b, d.h[0])
502-
b = appendUint64(b, d.h[1])
503-
b = appendUint64(b, d.h[2])
504-
b = appendUint64(b, d.h[3])
505-
b = appendUint64(b, d.h[4])
506-
b = appendUint64(b, d.h[5])
507-
b = appendUint64(b, d.h[6])
508-
b = appendUint64(b, d.h[7])
502+
b = byteorder.BeAppendUint64(b, d.h[0])
503+
b = byteorder.BeAppendUint64(b, d.h[1])
504+
b = byteorder.BeAppendUint64(b, d.h[2])
505+
b = byteorder.BeAppendUint64(b, d.h[3])
506+
b = byteorder.BeAppendUint64(b, d.h[4])
507+
b = byteorder.BeAppendUint64(b, d.h[5])
508+
b = byteorder.BeAppendUint64(b, d.h[6])
509+
b = byteorder.BeAppendUint64(b, d.h[7])
509510
b = append(b, d.x[:d.nx]...)
510511
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
511-
b = appendUint64(b, d.nl>>3|d.nh<<61)
512+
b = byteorder.BeAppendUint64(b, d.nl>>3|d.nh<<61)
512513
return b, nil
513514
}
514515

@@ -568,47 +569,10 @@ func (h *sha512Hash) UnmarshalBinary(b []byte) error {
568569
return nil
569570
}
570571

571-
func appendUint64(b []byte, x uint64) []byte {
572-
var a [8]byte
573-
putUint64(a[:], x)
574-
return append(b, a[:]...)
575-
}
576-
577-
func appendUint32(b []byte, x uint32) []byte {
578-
var a [4]byte
579-
putUint32(a[:], x)
580-
return append(b, a[:]...)
581-
}
582-
583572
func consumeUint64(b []byte) ([]byte, uint64) {
584-
_ = b[7]
585-
x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
586-
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
587-
return b[8:], x
573+
return b[8:], byteorder.BeUint64(b)
588574
}
589575

590576
func consumeUint32(b []byte) ([]byte, uint32) {
591-
_ = b[3]
592-
x := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
593-
return b[4:], x
594-
}
595-
596-
func putUint64(x []byte, s uint64) {
597-
_ = x[7]
598-
x[0] = byte(s >> 56)
599-
x[1] = byte(s >> 48)
600-
x[2] = byte(s >> 40)
601-
x[3] = byte(s >> 32)
602-
x[4] = byte(s >> 24)
603-
x[5] = byte(s >> 16)
604-
x[6] = byte(s >> 8)
605-
x[7] = byte(s)
606-
}
607-
608-
func putUint32(x []byte, s uint32) {
609-
_ = x[3]
610-
x[0] = byte(s >> 24)
611-
x[1] = byte(s >> 16)
612-
x[2] = byte(s >> 8)
613-
x[3] = byte(s)
577+
return b[4:], byteorder.BeUint32(b)
614578
}

0 commit comments

Comments
 (0)