Skip to content

Commit c98867d

Browse files
mateusz834gopherbot
authored andcommittedMay 13, 2024
crypto: replace encoding/binary in favour of internal/byteorder
Updates #54097 Change-Id: I827a5efd1736ce057b76f079466f2d9ead225898 GitHub-Last-Rev: 40af104 GitHub-Pull-Request: #67321 Reviewed-on: https://go-review.googlesource.com/c/go/+/585017 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a32e94d commit c98867d

24 files changed

+174
-176
lines changed
 

‎src/crypto/aes/block.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@
3636

3737
package aes
3838

39-
import (
40-
"encoding/binary"
41-
)
39+
import "internal/byteorder"
4240

4341
// Encrypt one block from src into dst, using the expanded key xk.
4442
func encryptBlockGo(xk []uint32, dst, src []byte) {
4543
_ = src[15] // early bounds check
46-
s0 := binary.BigEndian.Uint32(src[0:4])
47-
s1 := binary.BigEndian.Uint32(src[4:8])
48-
s2 := binary.BigEndian.Uint32(src[8:12])
49-
s3 := binary.BigEndian.Uint32(src[12:16])
44+
s0 := byteorder.BeUint32(src[0:4])
45+
s1 := byteorder.BeUint32(src[4:8])
46+
s2 := byteorder.BeUint32(src[8:12])
47+
s3 := byteorder.BeUint32(src[12:16])
5048

5149
// First round just XORs input with key.
5250
s0 ^= xk[0]
@@ -80,19 +78,19 @@ func encryptBlockGo(xk []uint32, dst, src []byte) {
8078
s3 ^= xk[k+3]
8179

8280
_ = dst[15] // early bounds check
83-
binary.BigEndian.PutUint32(dst[0:4], s0)
84-
binary.BigEndian.PutUint32(dst[4:8], s1)
85-
binary.BigEndian.PutUint32(dst[8:12], s2)
86-
binary.BigEndian.PutUint32(dst[12:16], s3)
81+
byteorder.BePutUint32(dst[0:4], s0)
82+
byteorder.BePutUint32(dst[4:8], s1)
83+
byteorder.BePutUint32(dst[8:12], s2)
84+
byteorder.BePutUint32(dst[12:16], s3)
8785
}
8886

8987
// Decrypt one block from src into dst, using the expanded key xk.
9088
func decryptBlockGo(xk []uint32, dst, src []byte) {
9189
_ = src[15] // early bounds check
92-
s0 := binary.BigEndian.Uint32(src[0:4])
93-
s1 := binary.BigEndian.Uint32(src[4:8])
94-
s2 := binary.BigEndian.Uint32(src[8:12])
95-
s3 := binary.BigEndian.Uint32(src[12:16])
90+
s0 := byteorder.BeUint32(src[0:4])
91+
s1 := byteorder.BeUint32(src[4:8])
92+
s2 := byteorder.BeUint32(src[8:12])
93+
s3 := byteorder.BeUint32(src[12:16])
9694

9795
// First round just XORs input with key.
9896
s0 ^= xk[0]
@@ -126,10 +124,10 @@ func decryptBlockGo(xk []uint32, dst, src []byte) {
126124
s3 ^= xk[k+3]
127125

128126
_ = dst[15] // early bounds check
129-
binary.BigEndian.PutUint32(dst[0:4], s0)
130-
binary.BigEndian.PutUint32(dst[4:8], s1)
131-
binary.BigEndian.PutUint32(dst[8:12], s2)
132-
binary.BigEndian.PutUint32(dst[12:16], s3)
127+
byteorder.BePutUint32(dst[0:4], s0)
128+
byteorder.BePutUint32(dst[4:8], s1)
129+
byteorder.BePutUint32(dst[8:12], s2)
130+
byteorder.BePutUint32(dst[12:16], s3)
133131
}
134132

135133
// Apply sbox0 to each byte in w.
@@ -150,7 +148,7 @@ func expandKeyGo(key []byte, enc, dec []uint32) {
150148
var i int
151149
nk := len(key) / 4
152150
for i = 0; i < nk; i++ {
153-
enc[i] = binary.BigEndian.Uint32(key[4*i:])
151+
enc[i] = byteorder.BeUint32(key[4*i:])
154152
}
155153
for ; i < len(enc); i++ {
156154
t := enc[i-1]

‎src/crypto/aes/ctr_s390x.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package aes
99
import (
1010
"crypto/cipher"
1111
"crypto/internal/alias"
12-
"encoding/binary"
12+
"internal/byteorder"
1313
)
1414

1515
// Assert that aesCipherAsm implements the ctrAble interface.
@@ -41,8 +41,8 @@ func (c *aesCipherAsm) NewCTR(iv []byte) cipher.Stream {
4141
}
4242
var ac aesctr
4343
ac.block = c
44-
ac.ctr[0] = binary.BigEndian.Uint64(iv[0:]) // high bits
45-
ac.ctr[1] = binary.BigEndian.Uint64(iv[8:]) // low bits
44+
ac.ctr[0] = byteorder.BeUint64(iv[0:]) // high bits
45+
ac.ctr[1] = byteorder.BeUint64(iv[8:]) // low bits
4646
ac.buffer = ac.storage[:0]
4747
return &ac
4848
}
@@ -52,8 +52,8 @@ func (c *aesctr) refill() {
5252
c.buffer = c.storage[:streamBufferSize]
5353
c0, c1 := c.ctr[0], c.ctr[1]
5454
for i := 0; i < streamBufferSize; i += 16 {
55-
binary.BigEndian.PutUint64(c.buffer[i+0:], c0)
56-
binary.BigEndian.PutUint64(c.buffer[i+8:], c1)
55+
byteorder.BePutUint64(c.buffer[i+0:], c0)
56+
byteorder.BePutUint64(c.buffer[i+8:], c1)
5757

5858
// Increment in big endian: c0 is high, c1 is low.
5959
c1++

‎src/crypto/aes/gcm_ppc64x.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ package aes
99
import (
1010
"crypto/cipher"
1111
"crypto/subtle"
12-
"encoding/binary"
1312
"errors"
13+
"internal/byteorder"
1414
"runtime"
1515
)
1616

@@ -66,14 +66,14 @@ func (c *aesCipherAsm) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
6666
// Reverse the bytes in each 8 byte chunk
6767
// Load little endian, store big endian
6868
if runtime.GOARCH == "ppc64le" {
69-
h1 = binary.LittleEndian.Uint64(hle[:8])
70-
h2 = binary.LittleEndian.Uint64(hle[8:])
69+
h1 = byteorder.LeUint64(hle[:8])
70+
h2 = byteorder.LeUint64(hle[8:])
7171
} else {
72-
h1 = binary.BigEndian.Uint64(hle[:8])
73-
h2 = binary.BigEndian.Uint64(hle[8:])
72+
h1 = byteorder.BeUint64(hle[:8])
73+
h2 = byteorder.BeUint64(hle[8:])
7474
}
75-
binary.BigEndian.PutUint64(hle[:8], h1)
76-
binary.BigEndian.PutUint64(hle[8:], h2)
75+
byteorder.BePutUint64(hle[:8], h1)
76+
byteorder.BePutUint64(hle[8:], h2)
7777
gcmInit(&g.productTable, hle)
7878

7979
return g, nil
@@ -126,8 +126,8 @@ func (g *gcmAsm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte) {
126126
// increments the rightmost 32-bits of the count value by 1.
127127
func gcmInc32(counterBlock *[16]byte) {
128128
c := counterBlock[len(counterBlock)-4:]
129-
x := binary.BigEndian.Uint32(c) + 1
130-
binary.BigEndian.PutUint32(c, x)
129+
x := byteorder.BeUint32(c) + 1
130+
byteorder.BePutUint32(c, x)
131131
}
132132

133133
// paddedGHASH pads data with zeroes until its length is a multiple of

‎src/crypto/aes/gcm_s390x.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"crypto/cipher"
1111
"crypto/internal/alias"
1212
"crypto/subtle"
13-
"encoding/binary"
1413
"errors"
14+
"internal/byteorder"
1515
"internal/cpu"
1616
)
1717

@@ -25,14 +25,14 @@ type gcmCount [16]byte
2525

2626
// inc increments the rightmost 32-bits of the count value by 1.
2727
func (x *gcmCount) inc() {
28-
binary.BigEndian.PutUint32(x[len(x)-4:], binary.BigEndian.Uint32(x[len(x)-4:])+1)
28+
byteorder.BePutUint32(x[len(x)-4:], byteorder.BeUint32(x[len(x)-4:])+1)
2929
}
3030

3131
// gcmLengths writes len0 || len1 as big-endian values to a 16-byte array.
3232
func gcmLengths(len0, len1 uint64) [16]byte {
3333
v := [16]byte{}
34-
binary.BigEndian.PutUint64(v[0:], len0)
35-
binary.BigEndian.PutUint64(v[8:], len1)
34+
byteorder.BePutUint64(v[0:], len0)
35+
byteorder.BePutUint64(v[8:], len1)
3636
return v
3737
}
3838

‎src/crypto/cipher/gcm.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package cipher
77
import (
88
"crypto/internal/alias"
99
"crypto/subtle"
10-
"encoding/binary"
1110
"errors"
11+
"internal/byteorder"
1212
)
1313

1414
// AEAD is a cipher mode providing authenticated encryption with associated
@@ -137,8 +137,8 @@ func newGCMWithNonceAndTagSize(cipher Block, nonceSize, tagSize int) (AEAD, erro
137137
// would expect, say, 4*key to be in index 4 of the table but due to
138138
// this bit ordering it will actually be in index 0010 (base 2) = 2.
139139
x := gcmFieldElement{
140-
binary.BigEndian.Uint64(key[:8]),
141-
binary.BigEndian.Uint64(key[8:]),
140+
byteorder.BeUint64(key[:8]),
141+
byteorder.BeUint64(key[8:]),
142142
}
143143
g.productTable[reverseBits(1)] = x
144144

@@ -321,8 +321,8 @@ func (g *gcm) mul(y *gcmFieldElement) {
321321
// Horner's rule. There must be a multiple of gcmBlockSize bytes in blocks.
322322
func (g *gcm) updateBlocks(y *gcmFieldElement, blocks []byte) {
323323
for len(blocks) > 0 {
324-
y.low ^= binary.BigEndian.Uint64(blocks)
325-
y.high ^= binary.BigEndian.Uint64(blocks[8:])
324+
y.low ^= byteorder.BeUint64(blocks)
325+
y.high ^= byteorder.BeUint64(blocks[8:])
326326
g.mul(y)
327327
blocks = blocks[gcmBlockSize:]
328328
}
@@ -345,7 +345,7 @@ func (g *gcm) update(y *gcmFieldElement, data []byte) {
345345
// and increments it.
346346
func gcmInc32(counterBlock *[16]byte) {
347347
ctr := counterBlock[len(counterBlock)-4:]
348-
binary.BigEndian.PutUint32(ctr, binary.BigEndian.Uint32(ctr)+1)
348+
byteorder.BePutUint32(ctr, byteorder.BeUint32(ctr)+1)
349349
}
350350

351351
// sliceForAppend takes a slice and a requested number of bytes. It returns a
@@ -401,8 +401,8 @@ func (g *gcm) deriveCounter(counter *[gcmBlockSize]byte, nonce []byte) {
401401
g.update(&y, nonce)
402402
y.high ^= uint64(len(nonce)) * 8
403403
g.mul(&y)
404-
binary.BigEndian.PutUint64(counter[:8], y.low)
405-
binary.BigEndian.PutUint64(counter[8:], y.high)
404+
byteorder.BePutUint64(counter[:8], y.low)
405+
byteorder.BePutUint64(counter[8:], y.high)
406406
}
407407
}
408408

@@ -418,8 +418,8 @@ func (g *gcm) auth(out, ciphertext, additionalData []byte, tagMask *[gcmTagSize]
418418

419419
g.mul(&y)
420420

421-
binary.BigEndian.PutUint64(out, y.low)
422-
binary.BigEndian.PutUint64(out[8:], y.high)
421+
byteorder.BePutUint64(out, y.low)
422+
byteorder.BePutUint64(out[8:], y.high)
423423

424424
subtle.XORBytes(out, out, tagMask[:])
425425
}

‎src/crypto/des/block.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
package des
66

77
import (
8-
"encoding/binary"
8+
"internal/byteorder"
99
"sync"
1010
)
1111

1212
func cryptBlock(subkeys []uint64, dst, src []byte, decrypt bool) {
13-
b := binary.BigEndian.Uint64(src)
13+
b := byteorder.BeUint64(src)
1414
b = permuteInitialBlock(b)
1515
left, right := uint32(b>>32), uint32(b)
1616

@@ -32,7 +32,7 @@ func cryptBlock(subkeys []uint64, dst, src []byte, decrypt bool) {
3232

3333
// switch left & right and perform final permutation
3434
preOutput := (uint64(right) << 32) | uint64(left)
35-
binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
35+
byteorder.BePutUint64(dst, permuteFinalBlock(preOutput))
3636
}
3737

3838
// DES Feistel function. feistelBox must be initialized via
@@ -218,7 +218,7 @@ func (c *desCipher) generateSubkeys(keyBytes []byte) {
218218
feistelBoxOnce.Do(initFeistelBox)
219219

220220
// apply PC1 permutation to key
221-
key := binary.BigEndian.Uint64(keyBytes)
221+
key := byteorder.BeUint64(keyBytes)
222222
permutedKey := permuteBlock(key, permutedChoice1[:])
223223

224224
// rotate halves of permuted key according to the rotation schedule

‎src/crypto/des/cipher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package des
77
import (
88
"crypto/cipher"
99
"crypto/internal/alias"
10-
"encoding/binary"
10+
"internal/byteorder"
1111
"strconv"
1212
)
1313

@@ -95,7 +95,7 @@ func (c *tripleDESCipher) Encrypt(dst, src []byte) {
9595
panic("crypto/des: invalid buffer overlap")
9696
}
9797

98-
b := binary.BigEndian.Uint64(src)
98+
b := byteorder.BeUint64(src)
9999
b = permuteInitialBlock(b)
100100
left, right := uint32(b>>32), uint32(b)
101101

@@ -116,7 +116,7 @@ func (c *tripleDESCipher) Encrypt(dst, src []byte) {
116116
right = (right << 31) | (right >> 1)
117117

118118
preOutput := (uint64(right) << 32) | uint64(left)
119-
binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
119+
byteorder.BePutUint64(dst, permuteFinalBlock(preOutput))
120120
}
121121

122122
func (c *tripleDESCipher) Decrypt(dst, src []byte) {
@@ -130,7 +130,7 @@ func (c *tripleDESCipher) Decrypt(dst, src []byte) {
130130
panic("crypto/des: invalid buffer overlap")
131131
}
132132

133-
b := binary.BigEndian.Uint64(src)
133+
b := byteorder.BeUint64(src)
134134
b = permuteInitialBlock(b)
135135
left, right := uint32(b>>32), uint32(b)
136136

@@ -151,5 +151,5 @@ func (c *tripleDESCipher) Decrypt(dst, src []byte) {
151151
right = (right << 31) | (right >> 1)
152152

153153
preOutput := (uint64(right) << 32) | uint64(left)
154-
binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
154+
byteorder.BePutUint64(dst, permuteFinalBlock(preOutput))
155155
}

‎src/crypto/ecdh/nist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"crypto/internal/boring"
99
"crypto/internal/nistec"
1010
"crypto/internal/randutil"
11-
"encoding/binary"
1211
"errors"
12+
"internal/byteorder"
1313
"io"
1414
"math/bits"
1515
)
@@ -156,7 +156,7 @@ func isLess(a, b []byte) bool {
156156
// Perform a subtraction with borrow.
157157
var borrow uint64
158158
for i := 0; i < len(bufA); i += 8 {
159-
limbA, limbB := binary.LittleEndian.Uint64(bufA[i:]), binary.LittleEndian.Uint64(bufB[i:])
159+
limbA, limbB := byteorder.LeUint64(bufA[i:]), byteorder.LeUint64(bufB[i:])
160160
_, borrow = bits.Sub64(limbA, limbB, borrow)
161161
}
162162

‎src/crypto/internal/bigmod/nat.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
package bigmod
66

77
import (
8-
"encoding/binary"
98
"errors"
9+
"internal/byteorder"
1010
"math/big"
1111
"math/bits"
1212
)
@@ -170,9 +170,9 @@ func (x *Nat) SetOverflowingBytes(b []byte, m *Modulus) (*Nat, error) {
170170
// big-endian encoded uint value.
171171
func bigEndianUint(buf []byte) uint {
172172
if _W == 64 {
173-
return uint(binary.BigEndian.Uint64(buf))
173+
return uint(byteorder.BeUint64(buf))
174174
}
175-
return uint(binary.BigEndian.Uint32(buf))
175+
return uint(byteorder.BeUint32(buf))
176176
}
177177

178178
func (x *Nat) setBytes(b []byte, m *Modulus) error {

‎src/crypto/internal/edwards25519/field/fe.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package field
77

88
import (
99
"crypto/subtle"
10-
"encoding/binary"
1110
"errors"
11+
"internal/byteorder"
1212
"math/bits"
1313
)
1414

@@ -201,20 +201,20 @@ func (v *Element) SetBytes(x []byte) (*Element, error) {
201201
}
202202

203203
// Bits 0:51 (bytes 0:8, bits 0:64, shift 0, mask 51).
204-
v.l0 = binary.LittleEndian.Uint64(x[0:8])
204+
v.l0 = byteorder.LeUint64(x[0:8])
205205
v.l0 &= maskLow51Bits
206206
// Bits 51:102 (bytes 6:14, bits 48:112, shift 3, mask 51).
207-
v.l1 = binary.LittleEndian.Uint64(x[6:14]) >> 3
207+
v.l1 = byteorder.LeUint64(x[6:14]) >> 3
208208
v.l1 &= maskLow51Bits
209209
// Bits 102:153 (bytes 12:20, bits 96:160, shift 6, mask 51).
210-
v.l2 = binary.LittleEndian.Uint64(x[12:20]) >> 6
210+
v.l2 = byteorder.LeUint64(x[12:20]) >> 6
211211
v.l2 &= maskLow51Bits
212212
// Bits 153:204 (bytes 19:27, bits 152:216, shift 1, mask 51).
213-
v.l3 = binary.LittleEndian.Uint64(x[19:27]) >> 1
213+
v.l3 = byteorder.LeUint64(x[19:27]) >> 1
214214
v.l3 &= maskLow51Bits
215215
// Bits 204:255 (bytes 24:32, bits 192:256, shift 12, mask 51).
216216
// Note: not bytes 25:33, shift 4, to avoid overread.
217-
v.l4 = binary.LittleEndian.Uint64(x[24:32]) >> 12
217+
v.l4 = byteorder.LeUint64(x[24:32]) >> 12
218218
v.l4 &= maskLow51Bits
219219

220220
return v, nil
@@ -235,7 +235,7 @@ func (v *Element) bytes(out *[32]byte) []byte {
235235
var buf [8]byte
236236
for i, l := range [5]uint64{t.l0, t.l1, t.l2, t.l3, t.l4} {
237237
bitsOffset := i * 51
238-
binary.LittleEndian.PutUint64(buf[:], l<<uint(bitsOffset%8))
238+
byteorder.LePutUint64(buf[:], l<<uint(bitsOffset%8))
239239
for i, bb := range buf {
240240
off := bitsOffset/8 + i
241241
if off >= len(out) {

‎src/crypto/internal/edwards25519/scalar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
package edwards25519
66

77
import (
8-
"encoding/binary"
98
"errors"
9+
"internal/byteorder"
1010
)
1111

1212
// A Scalar is an integer modulo
@@ -271,7 +271,7 @@ func (s *Scalar) nonAdjacentForm(w uint) [256]int8 {
271271
var digits [5]uint64
272272

273273
for i := 0; i < 4; i++ {
274-
digits[i] = binary.LittleEndian.Uint64(b[i*8:])
274+
digits[i] = byteorder.LeUint64(b[i*8:])
275275
}
276276

277277
width := uint64(1 << w)

‎src/crypto/internal/mlkem768/mlkem768.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ package mlkem768
3030
import (
3131
"crypto/rand"
3232
"crypto/subtle"
33-
"encoding/binary"
3433
"errors"
34+
"internal/byteorder"
3535

3636
"golang.org/x/crypto/sha3"
3737
)
@@ -864,8 +864,8 @@ func sampleNTT(rho []byte, ii, jj byte) nttElement {
864864
B.Read(buf[:])
865865
off = 0
866866
}
867-
d1 := binary.LittleEndian.Uint16(buf[off:]) & 0b1111_1111_1111
868-
d2 := binary.LittleEndian.Uint16(buf[off+1:]) >> 4
867+
d1 := byteorder.LeUint16(buf[off:]) & 0b1111_1111_1111
868+
d2 := byteorder.LeUint16(buf[off+1:]) >> 4
869869
off += 3
870870
if d1 < q {
871871
a[j] = fieldElement(d1)

‎src/crypto/internal/nistec/p256_asm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ package nistec
1616

1717
import (
1818
_ "embed"
19-
"encoding/binary"
2019
"errors"
20+
"internal/byteorder"
2121
"math/bits"
2222
"runtime"
2323
"unsafe"
@@ -327,7 +327,7 @@ func init() {
327327
if runtime.GOARCH == "s390x" {
328328
var newTable [43 * 32 * 2 * 4]uint64
329329
for i, x := range (*[43 * 32 * 2 * 4][8]byte)(*p256PrecomputedPtr) {
330-
newTable[i] = binary.LittleEndian.Uint64(x[:])
330+
newTable[i] = byteorder.LeUint64(x[:])
331331
}
332332
newTablePtr := unsafe.Pointer(&newTable)
333333
p256PrecomputedPtr = &newTablePtr

‎src/crypto/md5/gen.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ var program = `// Copyright 2013 The Go Authors. All rights reserved.
201201
package md5
202202
203203
import (
204-
"encoding/binary"
204+
"internal/byteorder"
205205
"math/bits"
206206
)
207207
@@ -219,27 +219,27 @@ func blockGeneric(dig *digest, p []byte) {
219219
220220
// load input block
221221
{{range $i := seq 16 -}}
222-
{{printf "x%x := binary.LittleEndian.Uint32(q[4*%#x:])" $i $i}}
222+
{{printf "x%x := byteorder.LeUint32(q[4*%#x:])" $i $i}}
223223
{{end}}
224224
225225
// round 1
226226
{{range $i, $s := dup 4 .Shift1 -}}
227227
{{printf "arg0 = arg1 + bits.RotateLeft32((((arg2^arg3)&arg1)^arg3)+arg0+x%x+%#08x, %d)" (idx 1 $i) (index $.Table1 $i) $s | relabel}}
228228
{{rotate -}}
229229
{{end}}
230-
230+
231231
// round 2
232232
{{range $i, $s := dup 4 .Shift2 -}}
233233
{{printf "arg0 = arg1 + bits.RotateLeft32((((arg1^arg2)&arg3)^arg2)+arg0+x%x+%#08x, %d)" (idx 2 $i) (index $.Table2 $i) $s | relabel}}
234234
{{rotate -}}
235235
{{end}}
236-
236+
237237
// round 3
238238
{{range $i, $s := dup 4 .Shift3 -}}
239239
{{printf "arg0 = arg1 + bits.RotateLeft32((arg1^arg2^arg3)+arg0+x%x+%#08x, %d)" (idx 3 $i) (index $.Table3 $i) $s | relabel}}
240240
{{rotate -}}
241241
{{end}}
242-
242+
243243
// round 4
244244
{{range $i, $s := dup 4 .Shift4 -}}
245245
{{printf "arg0 = arg1 + bits.RotateLeft32((arg2^(arg1|^arg3))+arg0+x%x+%#08x, %d)" (idx 4 $i) (index $.Table4 $i) $s | relabel}}

‎src/crypto/md5/md5.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ package md5
1212

1313
import (
1414
"crypto"
15-
"encoding/binary"
1615
"errors"
1716
"hash"
17+
"internal/byteorder"
1818
)
1919

2020
func init() {
@@ -59,13 +59,13 @@ const (
5959
func (d *digest) MarshalBinary() ([]byte, error) {
6060
b := make([]byte, 0, marshaledSize)
6161
b = append(b, magic...)
62-
b = binary.BigEndian.AppendUint32(b, d.s[0])
63-
b = binary.BigEndian.AppendUint32(b, d.s[1])
64-
b = binary.BigEndian.AppendUint32(b, d.s[2])
65-
b = binary.BigEndian.AppendUint32(b, d.s[3])
62+
b = byteorder.BeAppendUint32(b, d.s[0])
63+
b = byteorder.BeAppendUint32(b, d.s[1])
64+
b = byteorder.BeAppendUint32(b, d.s[2])
65+
b = byteorder.BeAppendUint32(b, d.s[3])
6666
b = append(b, d.x[:d.nx]...)
6767
b = b[:len(b)+len(d.x)-d.nx] // already zero
68-
b = binary.BigEndian.AppendUint64(b, d.len)
68+
b = byteorder.BeAppendUint64(b, d.len)
6969
return b, nil
7070
}
7171

@@ -88,11 +88,11 @@ func (d *digest) UnmarshalBinary(b []byte) error {
8888
}
8989

9090
func consumeUint64(b []byte) ([]byte, uint64) {
91-
return b[8:], binary.BigEndian.Uint64(b[0:8])
91+
return b[8:], byteorder.BeUint64(b[0:8])
9292
}
9393

9494
func consumeUint32(b []byte) ([]byte, uint32) {
95-
return b[4:], binary.BigEndian.Uint32(b[0:4])
95+
return b[4:], byteorder.BeUint32(b[0:4])
9696
}
9797

9898
// New returns a new hash.Hash computing the MD5 checksum. The Hash also
@@ -156,8 +156,8 @@ func (d *digest) checkSum() [Size]byte {
156156
//
157157
// 1 byte end marker :: 0-63 padding bytes :: 8 byte length
158158
tmp := [1 + 63 + 8]byte{0x80}
159-
pad := (55 - d.len) % 64 // calculate number of padding bytes
160-
binary.LittleEndian.PutUint64(tmp[1+pad:], d.len<<3) // append length in bits
159+
pad := (55 - d.len) % 64 // calculate number of padding bytes
160+
byteorder.LePutUint64(tmp[1+pad:], d.len<<3) // append length in bits
161161
d.Write(tmp[:1+pad+8])
162162

163163
// The previous write ensures that a whole number of
@@ -167,10 +167,10 @@ func (d *digest) checkSum() [Size]byte {
167167
}
168168

169169
var digest [Size]byte
170-
binary.LittleEndian.PutUint32(digest[0:], d.s[0])
171-
binary.LittleEndian.PutUint32(digest[4:], d.s[1])
172-
binary.LittleEndian.PutUint32(digest[8:], d.s[2])
173-
binary.LittleEndian.PutUint32(digest[12:], d.s[3])
170+
byteorder.LePutUint32(digest[0:], d.s[0])
171+
byteorder.LePutUint32(digest[4:], d.s[1])
172+
byteorder.LePutUint32(digest[8:], d.s[2])
173+
byteorder.LePutUint32(digest[12:], d.s[3])
174174
return digest
175175
}
176176

‎src/crypto/md5/md5block.go

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/crypto/rand/rand_plan9.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package rand
99

1010
import (
1111
"crypto/aes"
12-
"encoding/binary"
12+
"internal/byteorder"
1313
"io"
1414
"os"
1515
"sync"
@@ -66,7 +66,7 @@ func (r *reader) Read(b []byte) (n int, err error) {
6666
if counter == 0 {
6767
panic("crypto/rand counter wrapped")
6868
}
69-
binary.LittleEndian.PutUint64(block[:], counter)
69+
byteorder.LePutUint64(block[:], counter)
7070
}
7171
blockCipher.Encrypt(r.key[:aes.BlockSize], block[:])
7272
inc()

‎src/crypto/sha1/sha1.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ package sha1
1111
import (
1212
"crypto"
1313
"crypto/internal/boring"
14-
"encoding/binary"
1514
"errors"
1615
"hash"
16+
"internal/byteorder"
1717
)
1818

1919
func init() {
@@ -51,14 +51,14 @@ const (
5151
func (d *digest) MarshalBinary() ([]byte, error) {
5252
b := make([]byte, 0, marshaledSize)
5353
b = append(b, magic...)
54-
b = binary.BigEndian.AppendUint32(b, d.h[0])
55-
b = binary.BigEndian.AppendUint32(b, d.h[1])
56-
b = binary.BigEndian.AppendUint32(b, d.h[2])
57-
b = binary.BigEndian.AppendUint32(b, d.h[3])
58-
b = binary.BigEndian.AppendUint32(b, d.h[4])
54+
b = byteorder.BeAppendUint32(b, d.h[0])
55+
b = byteorder.BeAppendUint32(b, d.h[1])
56+
b = byteorder.BeAppendUint32(b, d.h[2])
57+
b = byteorder.BeAppendUint32(b, d.h[3])
58+
b = byteorder.BeAppendUint32(b, d.h[4])
5959
b = append(b, d.x[:d.nx]...)
6060
b = b[:len(b)+len(d.x)-d.nx] // already zero
61-
b = binary.BigEndian.AppendUint64(b, d.len)
61+
b = byteorder.BeAppendUint64(b, d.len)
6262
return b, nil
6363
}
6464

@@ -167,7 +167,7 @@ func (d *digest) checkSum() [Size]byte {
167167
// Length in bits.
168168
len <<= 3
169169
padlen := tmp[:t+8]
170-
binary.BigEndian.PutUint64(padlen[t:], len)
170+
byteorder.BePutUint64(padlen[t:], len)
171171
d.Write(padlen)
172172

173173
if d.nx != 0 {
@@ -176,11 +176,11 @@ func (d *digest) checkSum() [Size]byte {
176176

177177
var digest [Size]byte
178178

179-
binary.BigEndian.PutUint32(digest[0:], d.h[0])
180-
binary.BigEndian.PutUint32(digest[4:], d.h[1])
181-
binary.BigEndian.PutUint32(digest[8:], d.h[2])
182-
binary.BigEndian.PutUint32(digest[12:], d.h[3])
183-
binary.BigEndian.PutUint32(digest[16:], d.h[4])
179+
byteorder.BePutUint32(digest[0:], d.h[0])
180+
byteorder.BePutUint32(digest[4:], d.h[1])
181+
byteorder.BePutUint32(digest[8:], d.h[2])
182+
byteorder.BePutUint32(digest[12:], d.h[3])
183+
byteorder.BePutUint32(digest[16:], d.h[4])
184184

185185
return digest
186186
}

‎src/crypto/sha256/sha256.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ package sha256
99
import (
1010
"crypto"
1111
"crypto/internal/boring"
12-
"encoding/binary"
1312
"errors"
1413
"hash"
14+
"internal/byteorder"
1515
)
1616

1717
func init() {
@@ -70,17 +70,17 @@ func (d *digest) MarshalBinary() ([]byte, error) {
7070
} else {
7171
b = append(b, magic256...)
7272
}
73-
b = binary.BigEndian.AppendUint32(b, d.h[0])
74-
b = binary.BigEndian.AppendUint32(b, d.h[1])
75-
b = binary.BigEndian.AppendUint32(b, d.h[2])
76-
b = binary.BigEndian.AppendUint32(b, d.h[3])
77-
b = binary.BigEndian.AppendUint32(b, d.h[4])
78-
b = binary.BigEndian.AppendUint32(b, d.h[5])
79-
b = binary.BigEndian.AppendUint32(b, d.h[6])
80-
b = binary.BigEndian.AppendUint32(b, d.h[7])
73+
b = byteorder.BeAppendUint32(b, d.h[0])
74+
b = byteorder.BeAppendUint32(b, d.h[1])
75+
b = byteorder.BeAppendUint32(b, d.h[2])
76+
b = byteorder.BeAppendUint32(b, d.h[3])
77+
b = byteorder.BeAppendUint32(b, d.h[4])
78+
b = byteorder.BeAppendUint32(b, d.h[5])
79+
b = byteorder.BeAppendUint32(b, d.h[6])
80+
b = byteorder.BeAppendUint32(b, d.h[7])
8181
b = append(b, d.x[:d.nx]...)
8282
b = b[:len(b)+len(d.x)-d.nx] // already zero
83-
b = binary.BigEndian.AppendUint64(b, d.len)
83+
b = byteorder.BeAppendUint64(b, d.len)
8484
return b, nil
8585
}
8686

@@ -226,7 +226,7 @@ func (d *digest) checkSum() [Size]byte {
226226
// Length in bits.
227227
len <<= 3
228228
padlen := tmp[:t+8]
229-
binary.BigEndian.PutUint64(padlen[t+0:], len)
229+
byteorder.BePutUint64(padlen[t+0:], len)
230230
d.Write(padlen)
231231

232232
if d.nx != 0 {
@@ -235,15 +235,15 @@ func (d *digest) checkSum() [Size]byte {
235235

236236
var digest [Size]byte
237237

238-
binary.BigEndian.PutUint32(digest[0:], d.h[0])
239-
binary.BigEndian.PutUint32(digest[4:], d.h[1])
240-
binary.BigEndian.PutUint32(digest[8:], d.h[2])
241-
binary.BigEndian.PutUint32(digest[12:], d.h[3])
242-
binary.BigEndian.PutUint32(digest[16:], d.h[4])
243-
binary.BigEndian.PutUint32(digest[20:], d.h[5])
244-
binary.BigEndian.PutUint32(digest[24:], d.h[6])
238+
byteorder.BePutUint32(digest[0:], d.h[0])
239+
byteorder.BePutUint32(digest[4:], d.h[1])
240+
byteorder.BePutUint32(digest[8:], d.h[2])
241+
byteorder.BePutUint32(digest[12:], d.h[3])
242+
byteorder.BePutUint32(digest[16:], d.h[4])
243+
byteorder.BePutUint32(digest[20:], d.h[5])
244+
byteorder.BePutUint32(digest[24:], d.h[6])
245245
if !d.is224 {
246-
binary.BigEndian.PutUint32(digest[28:], d.h[7])
246+
byteorder.BePutUint32(digest[28:], d.h[7])
247247
}
248248

249249
return digest

‎src/crypto/sha512/sha512.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ package sha512
1313
import (
1414
"crypto"
1515
"crypto/internal/boring"
16-
"encoding/binary"
1716
"errors"
1817
"hash"
18+
"internal/byteorder"
1919
)
2020

2121
func init() {
@@ -153,17 +153,17 @@ func (d *digest) MarshalBinary() ([]byte, error) {
153153
default:
154154
return nil, errors.New("crypto/sha512: invalid hash function")
155155
}
156-
b = binary.BigEndian.AppendUint64(b, d.h[0])
157-
b = binary.BigEndian.AppendUint64(b, d.h[1])
158-
b = binary.BigEndian.AppendUint64(b, d.h[2])
159-
b = binary.BigEndian.AppendUint64(b, d.h[3])
160-
b = binary.BigEndian.AppendUint64(b, d.h[4])
161-
b = binary.BigEndian.AppendUint64(b, d.h[5])
162-
b = binary.BigEndian.AppendUint64(b, d.h[6])
163-
b = binary.BigEndian.AppendUint64(b, d.h[7])
156+
b = byteorder.BeAppendUint64(b, d.h[0])
157+
b = byteorder.BeAppendUint64(b, d.h[1])
158+
b = byteorder.BeAppendUint64(b, d.h[2])
159+
b = byteorder.BeAppendUint64(b, d.h[3])
160+
b = byteorder.BeAppendUint64(b, d.h[4])
161+
b = byteorder.BeAppendUint64(b, d.h[5])
162+
b = byteorder.BeAppendUint64(b, d.h[6])
163+
b = byteorder.BeAppendUint64(b, d.h[7])
164164
b = append(b, d.x[:d.nx]...)
165165
b = b[:len(b)+len(d.x)-d.nx] // already zero
166-
b = binary.BigEndian.AppendUint64(b, d.len)
166+
b = byteorder.BeAppendUint64(b, d.len)
167167
return b, nil
168168
}
169169

@@ -316,24 +316,24 @@ func (d *digest) checkSum() [Size]byte {
316316
padlen := tmp[:t+16]
317317
// Upper 64 bits are always zero, because len variable has type uint64,
318318
// and tmp is already zeroed at that index, so we can skip updating it.
319-
// binary.BigEndian.PutUint64(padlen[t+0:], 0)
320-
binary.BigEndian.PutUint64(padlen[t+8:], len)
319+
// byteorder.BePutUint64(padlen[t+0:], 0)
320+
byteorder.BePutUint64(padlen[t+8:], len)
321321
d.Write(padlen)
322322

323323
if d.nx != 0 {
324324
panic("d.nx != 0")
325325
}
326326

327327
var digest [Size]byte
328-
binary.BigEndian.PutUint64(digest[0:], d.h[0])
329-
binary.BigEndian.PutUint64(digest[8:], d.h[1])
330-
binary.BigEndian.PutUint64(digest[16:], d.h[2])
331-
binary.BigEndian.PutUint64(digest[24:], d.h[3])
332-
binary.BigEndian.PutUint64(digest[32:], d.h[4])
333-
binary.BigEndian.PutUint64(digest[40:], d.h[5])
328+
byteorder.BePutUint64(digest[0:], d.h[0])
329+
byteorder.BePutUint64(digest[8:], d.h[1])
330+
byteorder.BePutUint64(digest[16:], d.h[2])
331+
byteorder.BePutUint64(digest[24:], d.h[3])
332+
byteorder.BePutUint64(digest[32:], d.h[4])
333+
byteorder.BePutUint64(digest[40:], d.h[5])
334334
if d.function != crypto.SHA384 {
335-
binary.BigEndian.PutUint64(digest[48:], d.h[6])
336-
binary.BigEndian.PutUint64(digest[56:], d.h[7])
335+
byteorder.BePutUint64(digest[48:], d.h[6])
336+
byteorder.BePutUint64(digest[56:], d.h[7])
337337
}
338338

339339
return digest

‎src/crypto/tls/bogo_shim_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package tls
22

33
import (
44
"crypto/x509"
5-
"encoding/binary"
65
"encoding/json"
76
"encoding/pem"
87
"flag"
98
"fmt"
9+
"internal/byteorder"
1010
"internal/testenv"
1111
"io"
1212
"log"
@@ -186,7 +186,7 @@ func bogoShim() {
186186

187187
// Write the shim ID we were passed as a little endian uint64
188188
shimIDBytes := make([]byte, 8)
189-
binary.LittleEndian.PutUint64(shimIDBytes, *shimID)
189+
byteorder.LePutUint64(shimIDBytes, *shimID)
190190
if _, err := conn.Write(shimIDBytes); err != nil {
191191
log.Fatalf("failed to write shim id: %s", err)
192192
}

‎src/crypto/tls/handshake_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"crypto/rsa"
1111
"crypto/x509"
1212
"encoding/base64"
13-
"encoding/binary"
1413
"encoding/pem"
1514
"errors"
1615
"fmt"
16+
"internal/byteorder"
1717
"io"
1818
"math/big"
1919
"net"
@@ -202,7 +202,7 @@ func (test *clientTest) connFromCommand() (conn *recordingConn, child *exec.Cmd,
202202
var serverInfo bytes.Buffer
203203
for _, ext := range test.extensions {
204204
pem.Encode(&serverInfo, &pem.Block{
205-
Type: fmt.Sprintf("SERVERINFO FOR EXTENSION %d", binary.BigEndian.Uint16(ext)),
205+
Type: fmt.Sprintf("SERVERINFO FOR EXTENSION %d", byteorder.BeUint16(ext)),
206206
Bytes: ext,
207207
})
208208
}

‎src/crypto/tls/handshake_server_tls13.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"crypto"
1111
"crypto/hmac"
1212
"crypto/rsa"
13-
"encoding/binary"
1413
"errors"
1514
"hash"
15+
"internal/byteorder"
1616
"io"
1717
"time"
1818
)
@@ -866,7 +866,7 @@ func (c *Conn) sendSessionTicket(earlyData bool) error {
866866
if _, err := c.config.rand().Read(ageAdd); err != nil {
867867
return err
868868
}
869-
m.ageAdd = binary.LittleEndian.Uint32(ageAdd)
869+
m.ageAdd = byteorder.LeUint32(ageAdd)
870870

871871
if earlyData {
872872
// RFC 9001, Section 4.6.1

‎src/go/build/deps_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,8 @@ var depsRules = `
430430
crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
431431
432432
# CRYPTO is core crypto algorithms - no cgo, fmt, net.
433-
# Unfortunately, stuck with reflect via encoding/binary.
434433
crypto/internal/boring/sig,
435434
crypto/internal/boring/syso,
436-
encoding/binary,
437435
golang.org/x/sys/cpu,
438436
hash, embed
439437
< crypto
@@ -455,12 +453,14 @@ var depsRules = `
455453
456454
crypto/boring
457455
< crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
458-
crypto/sha1, crypto/sha256, crypto/sha512,
459-
golang.org/x/crypto/sha3;
456+
crypto/sha1, crypto/sha256, crypto/sha512;
460457
461458
crypto/boring, crypto/internal/edwards25519/field
462459
< crypto/ecdh;
463460
461+
# Unfortunately, stuck with reflect via encoding/binary.
462+
encoding/binary, crypto/boring < golang.org/x/crypto/sha3;
463+
464464
crypto/aes,
465465
crypto/des,
466466
crypto/ecdh,

0 commit comments

Comments
 (0)
Please sign in to comment.