Skip to content

Commit c98867d

Browse files
mateusz834gopherbot
authored andcommitted
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 {

0 commit comments

Comments
 (0)