Skip to content

Commit 6068a2e

Browse files
rolandshoemakerFiloSottile
authored andcommitted
ssh: ignore MAC if AEAD ciphers negotiated
If the server/client cipher chosen is one of the two AEAD ciphers that we support ([email protected] and [email protected]), don't attempt to find a common MAC algorithm in findAgreedAlgorithms. Similarly in newPacketCipher, don't attempt to generate a MAC key if we are using a AEAD cipher. Fixes golang/go#51406 Change-Id: Id48ae72f052cb0a0c597b32e9901a0f218e4161f Reviewed-on: https://go-review.googlesource.com/c/crypto/+/389214 Trust: Roland Shoemaker <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent efcb850 commit 6068a2e

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

ssh/common.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ func (a *directionAlgorithms) rekeyBytes() int64 {
152152
return 1 << 30
153153
}
154154

155+
var aeadCiphers = map[string]bool{
156+
gcmCipherID: true,
157+
chacha20Poly1305ID: true,
158+
}
159+
155160
type algorithms struct {
156161
kex string
157162
hostKey string
@@ -187,14 +192,18 @@ func findAgreedAlgorithms(isClient bool, clientKexInit, serverKexInit *kexInitMs
187192
return
188193
}
189194

190-
ctos.MAC, err = findCommon("client to server MAC", clientKexInit.MACsClientServer, serverKexInit.MACsClientServer)
191-
if err != nil {
192-
return
195+
if !aeadCiphers[ctos.Cipher] {
196+
ctos.MAC, err = findCommon("client to server MAC", clientKexInit.MACsClientServer, serverKexInit.MACsClientServer)
197+
if err != nil {
198+
return
199+
}
193200
}
194201

195-
stoc.MAC, err = findCommon("server to client MAC", clientKexInit.MACsServerClient, serverKexInit.MACsServerClient)
196-
if err != nil {
197-
return
202+
if !aeadCiphers[stoc.Cipher] {
203+
stoc.MAC, err = findCommon("server to client MAC", clientKexInit.MACsServerClient, serverKexInit.MACsServerClient)
204+
if err != nil {
205+
return
206+
}
198207
}
199208

200209
ctos.Compression, err = findCommon("client to server compression", clientKexInit.CompressionClientServer, serverKexInit.CompressionClientServer)

ssh/handshake_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,26 @@ func TestHandshakeRekeyDefault(t *testing.T) {
560560
t.Errorf("got rekey after %dG write, want 64G", wgb)
561561
}
562562
}
563+
564+
func TestHandshakeAEADCipherNoMAC(t *testing.T) {
565+
for _, cipher := range []string{chacha20Poly1305ID, gcmCipherID} {
566+
checker := &syncChecker{
567+
called: make(chan int, 1),
568+
}
569+
clientConf := &ClientConfig{
570+
Config: Config{
571+
Ciphers: []string{cipher},
572+
MACs: []string{},
573+
},
574+
HostKeyCallback: checker.Check,
575+
}
576+
trC, trS, err := handshakePair(clientConf, "addr", false)
577+
if err != nil {
578+
t.Fatalf("handshakePair: %v", err)
579+
}
580+
defer trC.Close()
581+
defer trS.Close()
582+
583+
<-checker.called
584+
}
585+
}

ssh/transport.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,19 @@ var (
238238
// (to setup server->client keys) or clientKeys (for client->server keys).
239239
func newPacketCipher(d direction, algs directionAlgorithms, kex *kexResult) (packetCipher, error) {
240240
cipherMode := cipherModes[algs.Cipher]
241-
macMode := macModes[algs.MAC]
242241

243242
iv := make([]byte, cipherMode.ivSize)
244243
key := make([]byte, cipherMode.keySize)
245-
macKey := make([]byte, macMode.keySize)
246244

247245
generateKeyMaterial(iv, d.ivTag, kex)
248246
generateKeyMaterial(key, d.keyTag, kex)
249-
generateKeyMaterial(macKey, d.macKeyTag, kex)
247+
248+
var macKey []byte
249+
if !aeadCiphers[algs.Cipher] {
250+
macMode := macModes[algs.MAC]
251+
macKey = make([]byte, macMode.keySize)
252+
generateKeyMaterial(macKey, d.macKeyTag, kex)
253+
}
250254

251255
return cipherModes[algs.Cipher].create(key, iv, macKey, algs)
252256
}

0 commit comments

Comments
 (0)