1
0
mirror of https://github.com/golang/go synced 2024-11-11 18:21:40 -07:00

crypto/internal/boring: use internal/byteorder

Change-Id: I882dd4160fb24b1d605af28bca01227716dac717
GitHub-Last-Rev: c631b19068
GitHub-Pull-Request: golang/go#68697
Reviewed-on: https://go-review.googlesource.com/c/go/+/602179
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Zxilly Chou <zxilly@outlook.com>
Auto-Submit: Roland Shoemaker <roland@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Mateusz Poliwczak 2024-08-03 07:51:45 +00:00 committed by Gopher Robot
parent b915399e7e
commit 0ea534b899

View File

@ -59,6 +59,7 @@ import "C"
import (
"errors"
"hash"
"internal/byteorder"
"unsafe"
)
@ -165,14 +166,14 @@ func (h *sha1Hash) MarshalBinary() ([]byte, error) {
func (h *sha1Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha1Ctx)(unsafe.Pointer(&h.ctx))
b = append(b, sha1Magic...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[2])
b = appendUint32(b, d.h[3])
b = appendUint32(b, d.h[4])
b = byteorder.BeAppendUint32(b, d.h[0])
b = byteorder.BeAppendUint32(b, d.h[1])
b = byteorder.BeAppendUint32(b, d.h[2])
b = byteorder.BeAppendUint32(b, d.h[3])
b = byteorder.BeAppendUint32(b, d.h[4])
b = append(b, d.x[:d.nx]...)
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
b = byteorder.BeAppendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
return b, nil
}
@ -294,17 +295,17 @@ func (h *sha224Hash) MarshalBinary() ([]byte, error) {
func (h *sha224Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
b = append(b, magic224...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[2])
b = appendUint32(b, d.h[3])
b = appendUint32(b, d.h[4])
b = appendUint32(b, d.h[5])
b = appendUint32(b, d.h[6])
b = appendUint32(b, d.h[7])
b = byteorder.BeAppendUint32(b, d.h[0])
b = byteorder.BeAppendUint32(b, d.h[1])
b = byteorder.BeAppendUint32(b, d.h[2])
b = byteorder.BeAppendUint32(b, d.h[3])
b = byteorder.BeAppendUint32(b, d.h[4])
b = byteorder.BeAppendUint32(b, d.h[5])
b = byteorder.BeAppendUint32(b, d.h[6])
b = byteorder.BeAppendUint32(b, d.h[7])
b = append(b, d.x[:d.nx]...)
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
b = byteorder.BeAppendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
return b, nil
}
@ -315,17 +316,17 @@ func (h *sha256Hash) MarshalBinary() ([]byte, error) {
func (h *sha256Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
b = append(b, magic256...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[2])
b = appendUint32(b, d.h[3])
b = appendUint32(b, d.h[4])
b = appendUint32(b, d.h[5])
b = appendUint32(b, d.h[6])
b = appendUint32(b, d.h[7])
b = byteorder.BeAppendUint32(b, d.h[0])
b = byteorder.BeAppendUint32(b, d.h[1])
b = byteorder.BeAppendUint32(b, d.h[2])
b = byteorder.BeAppendUint32(b, d.h[3])
b = byteorder.BeAppendUint32(b, d.h[4])
b = byteorder.BeAppendUint32(b, d.h[5])
b = byteorder.BeAppendUint32(b, d.h[6])
b = byteorder.BeAppendUint32(b, d.h[7])
b = append(b, d.x[:d.nx]...)
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
b = byteorder.BeAppendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
return b, nil
}
@ -477,17 +478,17 @@ func (h *sha384Hash) MarshalBinary() ([]byte, error) {
func (h *sha384Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
b = append(b, magic384...)
b = appendUint64(b, d.h[0])
b = appendUint64(b, d.h[1])
b = appendUint64(b, d.h[2])
b = appendUint64(b, d.h[3])
b = appendUint64(b, d.h[4])
b = appendUint64(b, d.h[5])
b = appendUint64(b, d.h[6])
b = appendUint64(b, d.h[7])
b = byteorder.BeAppendUint64(b, d.h[0])
b = byteorder.BeAppendUint64(b, d.h[1])
b = byteorder.BeAppendUint64(b, d.h[2])
b = byteorder.BeAppendUint64(b, d.h[3])
b = byteorder.BeAppendUint64(b, d.h[4])
b = byteorder.BeAppendUint64(b, d.h[5])
b = byteorder.BeAppendUint64(b, d.h[6])
b = byteorder.BeAppendUint64(b, d.h[7])
b = append(b, d.x[:d.nx]...)
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, d.nl>>3|d.nh<<61)
b = byteorder.BeAppendUint64(b, d.nl>>3|d.nh<<61)
return b, nil
}
@ -498,17 +499,17 @@ func (h *sha512Hash) MarshalBinary() ([]byte, error) {
func (h *sha512Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
b = append(b, magic512...)
b = appendUint64(b, d.h[0])
b = appendUint64(b, d.h[1])
b = appendUint64(b, d.h[2])
b = appendUint64(b, d.h[3])
b = appendUint64(b, d.h[4])
b = appendUint64(b, d.h[5])
b = appendUint64(b, d.h[6])
b = appendUint64(b, d.h[7])
b = byteorder.BeAppendUint64(b, d.h[0])
b = byteorder.BeAppendUint64(b, d.h[1])
b = byteorder.BeAppendUint64(b, d.h[2])
b = byteorder.BeAppendUint64(b, d.h[3])
b = byteorder.BeAppendUint64(b, d.h[4])
b = byteorder.BeAppendUint64(b, d.h[5])
b = byteorder.BeAppendUint64(b, d.h[6])
b = byteorder.BeAppendUint64(b, d.h[7])
b = append(b, d.x[:d.nx]...)
b = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, d.nl>>3|d.nh<<61)
b = byteorder.BeAppendUint64(b, d.nl>>3|d.nh<<61)
return b, nil
}
@ -568,47 +569,10 @@ func (h *sha512Hash) UnmarshalBinary(b []byte) error {
return nil
}
func appendUint64(b []byte, x uint64) []byte {
var a [8]byte
putUint64(a[:], x)
return append(b, a[:]...)
}
func appendUint32(b []byte, x uint32) []byte {
var a [4]byte
putUint32(a[:], x)
return append(b, a[:]...)
}
func consumeUint64(b []byte) ([]byte, uint64) {
_ = b[7]
x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
return b[8:], x
return b[8:], byteorder.BeUint64(b)
}
func consumeUint32(b []byte) ([]byte, uint32) {
_ = b[3]
x := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
return b[4:], x
}
func putUint64(x []byte, s uint64) {
_ = x[7]
x[0] = byte(s >> 56)
x[1] = byte(s >> 48)
x[2] = byte(s >> 40)
x[3] = byte(s >> 32)
x[4] = byte(s >> 24)
x[5] = byte(s >> 16)
x[6] = byte(s >> 8)
x[7] = byte(s)
}
func putUint32(x []byte, s uint32) {
_ = x[3]
x[0] = byte(s >> 24)
x[1] = byte(s >> 16)
x[2] = byte(s >> 8)
x[3] = byte(s)
return b[4:], byteorder.BeUint32(b)
}