Skip to content

Commit 212c947

Browse files
mundaymbradfitz
authored andcommitted
vendor/golang_org/x/crypto: revendor
This change updates the vendored copy of golang.org/x/crypto to commit 1a580b3. An import of golang.org/x/sys/cpu was replaced with an import of internal/cpu as required by #24843 (comment). The following bash command can be used to replicate this import update: find `pwd` -name '*.go' -exec sed -i 's/golang\.org\/x\/sys\/cpu/internal\/cpu/g' '{}' \; Change-Id: Ic80d361f940a96c70e4196f594d791c63421d73c Reviewed-on: https://go-review.googlesource.com/113175 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 3fb3ca0 commit 212c947

33 files changed

+5899
-453
lines changed

src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package chacha20poly1305 // import "golang.org/x/crypto/chacha20poly1305"
77

88
import (
99
"crypto/cipher"
10+
"encoding/binary"
1011
"errors"
1112
)
1213

@@ -18,7 +19,7 @@ const (
1819
)
1920

2021
type chacha20poly1305 struct {
21-
key [32]byte
22+
key [8]uint32
2223
}
2324

2425
// New returns a ChaCha20-Poly1305 AEAD that uses the given, 256-bit key.
@@ -27,7 +28,14 @@ func New(key []byte) (cipher.AEAD, error) {
2728
return nil, errors.New("chacha20poly1305: bad key length")
2829
}
2930
ret := new(chacha20poly1305)
30-
copy(ret.key[:], key)
31+
ret.key[0] = binary.LittleEndian.Uint32(key[0:4])
32+
ret.key[1] = binary.LittleEndian.Uint32(key[4:8])
33+
ret.key[2] = binary.LittleEndian.Uint32(key[8:12])
34+
ret.key[3] = binary.LittleEndian.Uint32(key[12:16])
35+
ret.key[4] = binary.LittleEndian.Uint32(key[16:20])
36+
ret.key[5] = binary.LittleEndian.Uint32(key[20:24])
37+
ret.key[6] = binary.LittleEndian.Uint32(key[24:28])
38+
ret.key[7] = binary.LittleEndian.Uint32(key[28:32])
3139
return ret, nil
3240
}
3341

src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_amd64.go

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,39 @@
66

77
package chacha20poly1305
88

9-
import "encoding/binary"
9+
import (
10+
"encoding/binary"
11+
12+
"internal/cpu"
13+
)
1014

1115
//go:noescape
1216
func chacha20Poly1305Open(dst []byte, key []uint32, src, ad []byte) bool
1317

1418
//go:noescape
1519
func chacha20Poly1305Seal(dst []byte, key []uint32, src, ad []byte)
1620

17-
// cpuid is implemented in chacha20poly1305_amd64.s.
18-
func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)
19-
20-
// xgetbv with ecx = 0 is implemented in chacha20poly1305_amd64.s.
21-
func xgetbv() (eax, edx uint32)
22-
2321
var (
24-
useASM bool
25-
useAVX2 bool
22+
useASM = cpu.X86.HasSSSE3
23+
useAVX2 = cpu.X86.HasAVX2 && cpu.X86.HasBMI2
2624
)
2725

28-
func init() {
29-
detectCpuFeatures()
30-
}
31-
32-
// detectCpuFeatures is used to detect if cpu instructions
33-
// used by the functions implemented in assembler in
34-
// chacha20poly1305_amd64.s are supported.
35-
func detectCpuFeatures() {
36-
maxId, _, _, _ := cpuid(0, 0)
37-
if maxId < 1 {
38-
return
39-
}
40-
41-
_, _, ecx1, _ := cpuid(1, 0)
42-
43-
haveSSSE3 := isSet(9, ecx1)
44-
useASM = haveSSSE3
45-
46-
haveOSXSAVE := isSet(27, ecx1)
47-
48-
osSupportsAVX := false
49-
// For XGETBV, OSXSAVE bit is required and sufficient.
50-
if haveOSXSAVE {
51-
eax, _ := xgetbv()
52-
// Check if XMM and YMM registers have OS support.
53-
osSupportsAVX = isSet(1, eax) && isSet(2, eax)
54-
}
55-
haveAVX := isSet(28, ecx1) && osSupportsAVX
56-
57-
if maxId < 7 {
58-
return
59-
}
60-
61-
_, ebx7, _, _ := cpuid(7, 0)
62-
haveAVX2 := isSet(5, ebx7) && haveAVX
63-
haveBMI2 := isSet(8, ebx7)
64-
65-
useAVX2 = haveAVX2 && haveBMI2
66-
}
67-
68-
// isSet checks if bit at bitpos is set in value.
69-
func isSet(bitpos uint, value uint32) bool {
70-
return value&(1<<bitpos) != 0
71-
}
72-
7326
// setupState writes a ChaCha20 input matrix to state. See
7427
// https://tools.ietf.org/html/rfc7539#section-2.3.
75-
func setupState(state *[16]uint32, key *[32]byte, nonce []byte) {
28+
func setupState(state *[16]uint32, key *[8]uint32, nonce []byte) {
7629
state[0] = 0x61707865
7730
state[1] = 0x3320646e
7831
state[2] = 0x79622d32
7932
state[3] = 0x6b206574
8033

81-
state[4] = binary.LittleEndian.Uint32(key[:4])
82-
state[5] = binary.LittleEndian.Uint32(key[4:8])
83-
state[6] = binary.LittleEndian.Uint32(key[8:12])
84-
state[7] = binary.LittleEndian.Uint32(key[12:16])
85-
state[8] = binary.LittleEndian.Uint32(key[16:20])
86-
state[9] = binary.LittleEndian.Uint32(key[20:24])
87-
state[10] = binary.LittleEndian.Uint32(key[24:28])
88-
state[11] = binary.LittleEndian.Uint32(key[28:32])
34+
state[4] = key[0]
35+
state[5] = key[1]
36+
state[6] = key[2]
37+
state[7] = key[3]
38+
state[8] = key[4]
39+
state[9] = key[5]
40+
state[10] = key[6]
41+
state[11] = key[7]
8942

9043
state[12] = 0
9144
state[13] = binary.LittleEndian.Uint32(nonce[:4])

src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_amd64.s

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,22 +2693,3 @@ sealAVX2Tail512LoopB:
26932693
VPERM2I128 $0x13, tmpStoreAVX2, DD3, DD0
26942694

26952695
JMP sealAVX2SealHash
2696-
2697-
// func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)
2698-
TEXT ·cpuid(SB), NOSPLIT, $0-24
2699-
MOVL eaxArg+0(FP), AX
2700-
MOVL ecxArg+4(FP), CX
2701-
CPUID
2702-
MOVL AX, eax+8(FP)
2703-
MOVL BX, ebx+12(FP)
2704-
MOVL CX, ecx+16(FP)
2705-
MOVL DX, edx+20(FP)
2706-
RET
2707-
2708-
// func xgetbv() (eax, edx uint32)
2709-
TEXT ·xgetbv(SB),NOSPLIT,$0-8
2710-
MOVL $0, CX
2711-
XGETBV
2712-
MOVL AX, eax+0(FP)
2713-
MOVL DX, edx+4(FP)
2714-
RET

src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_generic.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package chacha20poly1305
77
import (
88
"encoding/binary"
99

10-
"golang_org/x/crypto/chacha20poly1305/internal/chacha20"
10+
"golang_org/x/crypto/internal/chacha20"
1111
"golang_org/x/crypto/poly1305"
1212
)
1313

@@ -16,15 +16,17 @@ func roundTo16(n int) int {
1616
}
1717

1818
func (c *chacha20poly1305) sealGeneric(dst, nonce, plaintext, additionalData []byte) []byte {
19-
var counter [16]byte
20-
copy(counter[4:], nonce)
19+
ret, out := sliceForAppend(dst, len(plaintext)+poly1305.TagSize)
2120

2221
var polyKey [32]byte
23-
chacha20.XORKeyStream(polyKey[:], polyKey[:], &counter, &c.key)
24-
25-
ret, out := sliceForAppend(dst, len(plaintext)+poly1305.TagSize)
26-
counter[0] = 1
27-
chacha20.XORKeyStream(out, plaintext, &counter, &c.key)
22+
s := chacha20.New(c.key, [3]uint32{
23+
binary.LittleEndian.Uint32(nonce[0:4]),
24+
binary.LittleEndian.Uint32(nonce[4:8]),
25+
binary.LittleEndian.Uint32(nonce[8:12]),
26+
})
27+
s.XORKeyStream(polyKey[:], polyKey[:])
28+
s.Advance() // skip the next 32 bytes
29+
s.XORKeyStream(out, plaintext)
2830

2931
polyInput := make([]byte, roundTo16(len(additionalData))+roundTo16(len(plaintext))+8+8)
3032
copy(polyInput, additionalData)
@@ -44,11 +46,14 @@ func (c *chacha20poly1305) openGeneric(dst, nonce, ciphertext, additionalData []
4446
copy(tag[:], ciphertext[len(ciphertext)-16:])
4547
ciphertext = ciphertext[:len(ciphertext)-16]
4648

47-
var counter [16]byte
48-
copy(counter[4:], nonce)
49-
5049
var polyKey [32]byte
51-
chacha20.XORKeyStream(polyKey[:], polyKey[:], &counter, &c.key)
50+
s := chacha20.New(c.key, [3]uint32{
51+
binary.LittleEndian.Uint32(nonce[0:4]),
52+
binary.LittleEndian.Uint32(nonce[4:8]),
53+
binary.LittleEndian.Uint32(nonce[8:12]),
54+
})
55+
s.XORKeyStream(polyKey[:], polyKey[:])
56+
s.Advance() // skip the next 32 bytes
5257

5358
polyInput := make([]byte, roundTo16(len(additionalData))+roundTo16(len(ciphertext))+8+8)
5459
copy(polyInput, additionalData)
@@ -64,7 +69,6 @@ func (c *chacha20poly1305) openGeneric(dst, nonce, ciphertext, additionalData []
6469
return nil, errOpen
6570
}
6671

67-
counter[0] = 1
68-
chacha20.XORKeyStream(out, ciphertext, &counter, &c.key)
72+
s.XORKeyStream(out, ciphertext)
6973
return ret, nil
7074
}

src/vendor/golang_org/x/crypto/chacha20poly1305/chacha20poly1305_vectors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ package chacha20poly1305
77
var chacha20Poly1305Tests = []struct {
88
plaintext, aad, key, nonce, out string
99
}{
10+
{
11+
"",
12+
"",
13+
"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
14+
"070000004041424344454647",
15+
"a0784d7a4716f3feb4f64e7f4b39bf04",
16+
},
1017
{
1118
"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e",
1219
"50515253c0c1c2c3c4c5c6c7",

0 commit comments

Comments
 (0)