Skip to content

Commit

Permalink
Uses the AEAD ciphers from pigotls
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiraux committed Nov 20, 2018
1 parent 64b3596 commit 6808f1d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
13 changes: 6 additions & 7 deletions agents/parse_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package agents

import (
. "github.com/QUIC-Tracker/quic-tracker"
. "github.com/QUIC-Tracker/quic-tracker/lib"
"unsafe"
"bytes"
)
Expand Down Expand Up @@ -78,9 +77,9 @@ func (a *ParsingAgent) Run(conn *Connection) {
break packetSelect
}

payload, err := cryptoState.Read.Open(nil, EncodeArgs(header.PacketNumber()), ciphertext[hLen:hLen+pLen], ciphertext[:hLen])
if err != nil {
a.Logger.Printf("Could not decrypt packet {type=%s, number=%d}: %s\n", header.PacketType().String(), header.PacketNumber(), err.Error())
payload := cryptoState.Read.Decrypt(ciphertext[hLen:hLen+pLen], uint64(header.PacketNumber()), ciphertext[:hLen])
if payload == nil {
a.Logger.Printf("Could not decrypt packet {type=%s, number=%d}\n", header.PacketType().String(), header.PacketNumber())
break packetSelect
}

Expand All @@ -94,9 +93,9 @@ func (a *ParsingAgent) Run(conn *Connection) {

off += hLen + pLen
case ShortHeaderPacket: // Packets with a short header always include a 1-RTT protected payload.
payload, err := cryptoState.Read.Open(nil, EncodeArgs(header.PacketNumber()), ciphertext[hLen:], ciphertext[:hLen])
if err != nil {
a.Logger.Printf("Could not decrypt packet {type=%s, number=%d}: %s\n", header.PacketType().String(), header.PacketNumber(), err.Error())
payload := cryptoState.Read.Decrypt(ciphertext[hLen:], uint64(header.PacketNumber()), ciphertext[:hLen])
if payload == nil {
a.Logger.Printf("Could not decrypt packet {type=%s, number=%d}\n", header.PacketType().String(), header.PacketNumber())
break packetSelect
}
cleartext = append(append(cleartext, udpPayload[off:off+hLen]...), payload...)
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *Connection) SendPacket(packet Packet, level EncryptionLevel) {
}

header := packet.EncodeHeader()
protectedPayload := cryptoState.Write.Seal(nil, EncodeArgs(packet.Header().PacketNumber()), payload, header)
protectedPayload := cryptoState.Write.Encrypt(payload, uint64(packet.Header().PacketNumber()), header)
packetBytes := append(header, protectedPayload...)

sample, pnOffset := GetPacketSample(packet.Header(), packetBytes)
Expand Down
21 changes: 4 additions & 17 deletions crypto.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package quictracker

import (
"crypto/cipher"
"github.com/mpiraux/pigotls"
. "github.com/QUIC-Tracker/quic-tracker/lib"
)

var quicVersionSalt = []byte{ // See https://tools.ietf.org/html/draft-ietf-quic-tls-10#section-5.2.2
Expand Down Expand Up @@ -62,19 +60,19 @@ type DirectionalEncryptionLevel struct {
}

type CryptoState struct {
Read cipher.AEAD
Write cipher.AEAD
Read *pigotls.AEAD
Write *pigotls.AEAD
PacketRead *pigotls.Cipher
PacketWrite *pigotls.Cipher
}

func (s *CryptoState) InitRead(tls *pigotls.Connection, readSecret []byte) {
s.Read = newProtectedAead(tls, readSecret)
s.Read = tls.NewAEAD(readSecret, false)
s.PacketRead = tls.NewCipher(tls.HkdfExpandLabel(readSecret, "pn", nil, tls.AEADKeySize()))
}

func (s *CryptoState) InitWrite(tls *pigotls.Connection, writeSecret []byte) {
s.Write = newProtectedAead(tls, writeSecret)
s.Write = tls.NewAEAD(writeSecret, true)
s.PacketWrite = tls.NewCipher(tls.HkdfExpandLabel(writeSecret, "pn", nil, tls.AEADKeySize()))
}

Expand All @@ -96,17 +94,6 @@ func NewProtectedCryptoState(tls *pigotls.Connection, readSecret []byte, writeSe
return s
}

func newProtectedAead(tls *pigotls.Connection, secret []byte) cipher.AEAD {
k := tls.HkdfExpandLabel(secret, "key", nil, tls.AEADKeySize())
iv := tls.HkdfExpandLabel(secret, "iv", nil, tls.AEADIvSize())

aead, err := NewWrappedAESGCM(k, iv)
if err != nil {
panic(err)
}
return aead
}

func GetPacketSample(header Header, packetBytes []byte) ([]byte, int) {
var pnOffset int
sampleLength := 16
Expand Down

0 comments on commit 6808f1d

Please sign in to comment.