mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-03 20:27:35 +03:00
use separate opener interfaces for long and short header packets
This commit is contained in:
parent
b2d3ef691e
commit
598628d05b
12 changed files with 320 additions and 149 deletions
|
@ -5,12 +5,13 @@ import (
|
|||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/internal/protocol"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("AEAD", func() {
|
||||
getSealerAndOpener := func(is1RTT bool) (ShortHeaderSealer, Opener) {
|
||||
getLongHeaderSealerAndOpener := func() (LongHeaderSealer, LongHeaderOpener) {
|
||||
key := make([]byte, 16)
|
||||
hpKey := make([]byte, 16)
|
||||
rand.Read(key)
|
||||
|
@ -24,36 +25,55 @@ var _ = Describe("AEAD", func() {
|
|||
|
||||
iv := make([]byte, 12)
|
||||
rand.Read(iv)
|
||||
return newSealer(aead, hpBlock, is1RTT), newOpener(aead, hpBlock, is1RTT)
|
||||
return newSealer(aead, hpBlock, false), newLongHeaderOpener(aead, hpBlock)
|
||||
}
|
||||
|
||||
getShortHeaderSealerAndOpener := func() (ShortHeaderSealer, ShortHeaderOpener) {
|
||||
key := make([]byte, 16)
|
||||
hpKey := make([]byte, 16)
|
||||
rand.Read(key)
|
||||
rand.Read(hpKey)
|
||||
block, err := aes.NewCipher(key)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
aead, err := cipher.NewGCM(block)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
hpBlock, err := aes.NewCipher(hpKey)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
iv := make([]byte, 12)
|
||||
rand.Read(iv)
|
||||
return newSealer(aead, hpBlock, true), newShortHeaderOpener(aead, hpBlock)
|
||||
}
|
||||
|
||||
Context("message encryption", func() {
|
||||
var (
|
||||
sealer ShortHeaderSealer
|
||||
opener Opener
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
sealer, opener = getSealerAndOpener(false)
|
||||
})
|
||||
|
||||
msg := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
|
||||
ad := []byte("Donec in velit neque.")
|
||||
|
||||
It("encrypts and decrypts a message", func() {
|
||||
It("encrypts and decrypts a message, for long headers", func() {
|
||||
sealer, opener := getLongHeaderSealerAndOpener()
|
||||
encrypted := sealer.Seal(nil, msg, 0x1337, ad)
|
||||
opened, err := opener.Open(nil, encrypted, 0x1337, ad)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(opened).To(Equal(msg))
|
||||
})
|
||||
|
||||
It("encrypts and decrypts a message, for short headers", func() {
|
||||
sealer, opener := getShortHeaderSealerAndOpener()
|
||||
encrypted := sealer.Seal(nil, msg, 0x1337, ad)
|
||||
opened, err := opener.Open(nil, encrypted, 0x1337, protocol.KeyPhaseZero, ad)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(opened).To(Equal(msg))
|
||||
})
|
||||
|
||||
It("fails to open a message if the associated data is not the same", func() {
|
||||
sealer, opener := getLongHeaderSealerAndOpener()
|
||||
encrypted := sealer.Seal(nil, msg, 0x1337, ad)
|
||||
_, err := opener.Open(nil, encrypted, 0x1337, []byte("wrong ad"))
|
||||
Expect(err).To(MatchError("cipher: message authentication failed"))
|
||||
})
|
||||
|
||||
It("fails to open a message if the packet number is not the same", func() {
|
||||
sealer, opener := getLongHeaderSealerAndOpener()
|
||||
encrypted := sealer.Seal(nil, msg, 0x1337, ad)
|
||||
_, err := opener.Open(nil, encrypted, 0x42, ad)
|
||||
Expect(err).To(MatchError("cipher: message authentication failed"))
|
||||
|
@ -62,7 +82,7 @@ var _ = Describe("AEAD", func() {
|
|||
|
||||
Context("header encryption", func() {
|
||||
It("encrypts and encrypts the header, for long headers", func() {
|
||||
sealer, opener := getSealerAndOpener(false)
|
||||
sealer, opener := getLongHeaderSealerAndOpener()
|
||||
var lastFourBitsDifferent int
|
||||
for i := 0; i < 100; i++ {
|
||||
sample := make([]byte, 16)
|
||||
|
@ -82,7 +102,7 @@ var _ = Describe("AEAD", func() {
|
|||
})
|
||||
|
||||
It("encrypts and encrypts the header, for short headers", func() {
|
||||
sealer, opener := getSealerAndOpener(true)
|
||||
sealer, opener := getShortHeaderSealerAndOpener()
|
||||
var lastFiveBitsDifferent int
|
||||
for i := 0; i < 100; i++ {
|
||||
sample := make([]byte, 16)
|
||||
|
@ -102,7 +122,7 @@ var _ = Describe("AEAD", func() {
|
|||
})
|
||||
|
||||
It("fails to decrypt the header when using a different sample", func() {
|
||||
sealer, opener := getSealerAndOpener(true)
|
||||
sealer, opener := getLongHeaderSealerAndOpener()
|
||||
header := []byte{0xb5, 1, 2, 3, 4, 5, 6, 7, 8, 0xde, 0xad, 0xbe, 0xef}
|
||||
sample := make([]byte, 16)
|
||||
rand.Read(sample)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue