use the draft-17 initial encryption

This commit is contained in:
Marten Seemann
2018-12-25 21:44:08 +06:30
parent 90738b5e0e
commit b1592d0616
10 changed files with 56 additions and 195 deletions

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"github.com/lucas-clemente/quic-go/internal/crypto"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/marten-seemann/qtls"
@@ -411,9 +410,9 @@ func (h *cryptoSetup) ReadHandshakeMessage() ([]byte, error) {
}
func (h *cryptoSetup) SetReadKey(suite *qtls.CipherSuite, trafficSecret []byte) {
key := crypto.HkdfExpandLabel(suite.Hash(), trafficSecret, "key", suite.KeyLen())
iv := crypto.HkdfExpandLabel(suite.Hash(), trafficSecret, "iv", suite.IVLen())
pnKey := crypto.HkdfExpandLabel(suite.Hash(), trafficSecret, "pn", suite.KeyLen())
key := qtls.HkdfExpandLabel(suite.Hash(), trafficSecret, []byte{}, "key", suite.KeyLen())
iv := qtls.HkdfExpandLabel(suite.Hash(), trafficSecret, []byte{}, "iv", suite.IVLen())
pnKey := qtls.HkdfExpandLabel(suite.Hash(), trafficSecret, []byte{}, "pn", suite.KeyLen())
pnDecrypter, err := aes.NewCipher(pnKey)
if err != nil {
panic(fmt.Sprintf("error creating new AES cipher: %s", err))
@@ -441,9 +440,9 @@ func (h *cryptoSetup) SetReadKey(suite *qtls.CipherSuite, trafficSecret []byte)
}
func (h *cryptoSetup) SetWriteKey(suite *qtls.CipherSuite, trafficSecret []byte) {
key := crypto.HkdfExpandLabel(suite.Hash(), trafficSecret, "key", suite.KeyLen())
iv := crypto.HkdfExpandLabel(suite.Hash(), trafficSecret, "iv", suite.IVLen())
pnKey := crypto.HkdfExpandLabel(suite.Hash(), trafficSecret, "pn", suite.KeyLen())
key := qtls.HkdfExpandLabel(suite.Hash(), trafficSecret, []byte{}, "key", suite.KeyLen())
iv := qtls.HkdfExpandLabel(suite.Hash(), trafficSecret, []byte{}, "iv", suite.IVLen())
pnKey := qtls.HkdfExpandLabel(suite.Hash(), trafficSecret, []byte{}, "pn", suite.KeyLen())
pnEncrypter, err := aes.NewCipher(pnKey)
if err != nil {
panic(fmt.Sprintf("error creating new AES cipher: %s", err))

View File

@@ -1,15 +1,15 @@
package handshake
import (
gocrypto "crypto"
"crypto"
"crypto/aes"
"crypto/cipher"
"github.com/lucas-clemente/quic-go/internal/crypto"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/marten-seemann/qtls"
)
var quicVersion1Salt = []byte{0x9c, 0x10, 0x8f, 0x98, 0x52, 0x0a, 0x5c, 0x5c, 0x32, 0x96, 0x8e, 0x95, 0x0e, 0x8a, 0x2c, 0x5f, 0xe0, 0x6d, 0x6c, 0x38}
var quicVersion1Salt = []byte{0xef, 0x4f, 0xb0, 0xab, 0xb4, 0x74, 0x70, 0xc4, 0x1b, 0xef, 0xcf, 0x80, 0x31, 0x33, 0x4f, 0xae, 0x48, 0x5e, 0x09, 0xa0}
func newInitialAEAD(connID protocol.ConnectionID, pers protocol.Perspective) (Sealer, Opener, error) {
clientSecret, serverSecret := computeSecrets(connID)
@@ -52,15 +52,15 @@ func newInitialAEAD(connID protocol.ConnectionID, pers protocol.Perspective) (Se
}
func computeSecrets(connID protocol.ConnectionID) (clientSecret, serverSecret []byte) {
initialSecret := crypto.HkdfExtract(gocrypto.SHA256, connID, quicVersion1Salt)
clientSecret = crypto.HkdfExpandLabel(gocrypto.SHA256, initialSecret, "client in", gocrypto.SHA256.Size())
serverSecret = crypto.HkdfExpandLabel(gocrypto.SHA256, initialSecret, "server in", gocrypto.SHA256.Size())
initialSecret := qtls.HkdfExtract(crypto.SHA256, connID, quicVersion1Salt)
clientSecret = qtls.HkdfExpandLabel(crypto.SHA256, initialSecret, []byte{}, "client in", crypto.SHA256.Size())
serverSecret = qtls.HkdfExpandLabel(crypto.SHA256, initialSecret, []byte{}, "server in", crypto.SHA256.Size())
return
}
func computeInitialKeyAndIV(secret []byte) (key, pnKey, iv []byte) {
key = crypto.HkdfExpandLabel(gocrypto.SHA256, secret, "key", 16)
pnKey = crypto.HkdfExpandLabel(gocrypto.SHA256, secret, "pn", 16)
iv = crypto.HkdfExpandLabel(gocrypto.SHA256, secret, "iv", 12)
key = qtls.HkdfExpandLabel(crypto.SHA256, secret, []byte{}, "quic key", 16)
pnKey = qtls.HkdfExpandLabel(crypto.SHA256, secret, []byte{}, "quic hp", 16)
iv = qtls.HkdfExpandLabel(crypto.SHA256, secret, []byte{}, "quic iv", 12)
return
}

View File

@@ -12,21 +12,21 @@ import (
var _ = Describe("Initial AEAD using AES-GCM", func() {
// values taken from https://github.com/quicwg/base-drafts/wiki/Test-Vector-for-the-Clear-Text-AEAD-key-derivation
Context("using the test vector from the QUIC WG Wiki", func() {
connID := protocol.ConnectionID([]byte{0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08})
connID := protocol.ConnectionID([]byte{0xc6, 0x54, 0xef, 0xd8, 0xa3, 0x1b, 0x47, 0x92})
It("computes the secrets", func() {
clientSecret, serverSecret := computeSecrets(connID)
Expect(clientSecret).To(Equal([]byte{
0x9f, 0x53, 0x64, 0x57, 0xf3, 0x2a, 0x1e, 0x0a,
0xe8, 0x64, 0xbc, 0xb3, 0xca, 0xf1, 0x23, 0x51,
0x10, 0x63, 0x0e, 0x1d, 0x1f, 0xb3, 0x38, 0x35,
0xbd, 0x05, 0x41, 0x70, 0xf9, 0x9b, 0xf7, 0xdc,
0x0c, 0x74, 0xbb, 0x95, 0xa1, 0x04, 0x8e, 0x52,
0xef, 0x3b, 0x72, 0xe1, 0x28, 0x89, 0x35, 0x1c,
0xd7, 0x3a, 0x55, 0x0f, 0xb6, 0x2c, 0x4b, 0xb0,
0x87, 0xe9, 0x15, 0xcc, 0xe9, 0x6c, 0xe3, 0xa0,
}))
Expect(serverSecret).To(Equal([]byte{
0xb0, 0x87, 0xdc, 0xd7, 0x47, 0x8d, 0xda, 0x8a,
0x85, 0x8f, 0xbf, 0x3d, 0x60, 0x5c, 0x88, 0x85,
0x86, 0xc0, 0xa3, 0xa9, 0x87, 0x54, 0x23, 0xad,
0x4f, 0x11, 0x4f, 0x0b, 0xa3, 0x8e, 0x5a, 0x2e,
0x4c, 0x9e, 0xdf, 0x24, 0xb0, 0xe5, 0xe5, 0x06,
0xdd, 0x3b, 0xfa, 0x4e, 0x0a, 0x03, 0x11, 0xe8,
0xc4, 0x1f, 0x35, 0x42, 0x73, 0xd8, 0xcb, 0x49,
0xdd, 0xd8, 0x46, 0x41, 0x38, 0xd4, 0x7e, 0xc6,
}))
})
@@ -34,16 +34,16 @@ var _ = Describe("Initial AEAD using AES-GCM", func() {
clientSecret, _ := computeSecrets(connID)
key, pnKey, iv := computeInitialKeyAndIV(clientSecret)
Expect(key).To(Equal([]byte{
0xf2, 0x92, 0x8f, 0x26, 0x14, 0xad, 0x6c, 0x20,
0xb9, 0xbd, 0x00, 0x8e, 0x9c, 0x89, 0x63, 0x1c,
0x86, 0xd1, 0x83, 0x04, 0x80, 0xb4, 0x0f, 0x86,
0xcf, 0x9d, 0x68, 0xdc, 0xad, 0xf3, 0x5d, 0xfe,
}))
Expect(pnKey).To(Equal([]byte{
0x68, 0xc3, 0xf6, 0x4e, 0x2d, 0x66, 0x34, 0x41,
0x2b, 0x8e, 0x32, 0x94, 0x62, 0x8d, 0x76, 0xf1,
0xcd, 0x25, 0x3a, 0x36, 0xff, 0x93, 0x93, 0x7c,
0x46, 0x93, 0x84, 0xa8, 0x23, 0xaf, 0x6c, 0x56,
}))
Expect(iv).To(Equal([]byte{
0xab, 0x95, 0x0b, 0x01, 0x98, 0x63, 0x79, 0x78,
0xcf, 0x44, 0xaa, 0xb9,
0x12, 0xf3, 0x93, 0x8a, 0xca, 0x34, 0xaa, 0x02,
0x54, 0x31, 0x63, 0xd4,
}))
})
@@ -51,16 +51,16 @@ var _ = Describe("Initial AEAD using AES-GCM", func() {
_, serverSecret := computeSecrets(connID)
key, pnKey, iv := computeInitialKeyAndIV(serverSecret)
Expect(key).To(Equal([]byte{
0xf5, 0x68, 0x17, 0xd0, 0xfc, 0x59, 0x5c, 0xfc,
0x0a, 0x2b, 0x0b, 0xcf, 0xb1, 0x87, 0x35, 0xec,
0x2c, 0x78, 0x63, 0x3e, 0x20, 0x6e, 0x99, 0xad,
0x25, 0x19, 0x64, 0xf1, 0x9f, 0x6d, 0xcd, 0x6d,
}))
Expect(pnKey).To(Equal([]byte{
0xa3, 0x13, 0xc8, 0x6d, 0x13, 0x73, 0xec, 0xbc,
0xcb, 0x32, 0x94, 0xb1, 0x49, 0x74, 0x22, 0x6c,
0x25, 0x79, 0xd8, 0x69, 0x6f, 0x85, 0xed, 0xa6,
0x8d, 0x35, 0x02, 0xb6, 0x55, 0x96, 0x58, 0x6b,
}))
Expect(iv).To(Equal([]byte{
0x32, 0x05, 0x03, 0x5a, 0x3c, 0x93, 0x7c, 0x90,
0x2e, 0xe4, 0xf4, 0xd6,
0x7b, 0x50, 0xbf, 0x36, 0x98, 0xa0, 0x6d, 0xfa,
0xbf, 0x75, 0xf2, 0x87,
}))
})
})