route/vendor/github.com/lucas-clemente/quic-go/internal/crypto/null_aead_fnv128a.go

72 lines
1.7 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package crypto
import (
"encoding/binary"
"errors"
"github.com/lucas-clemente/fnv128a"
2018-01-03 19:19:49 +00:00
"github.com/lucas-clemente/quic-go/internal/protocol"
2017-12-12 02:51:45 +00:00
)
2018-01-03 19:19:49 +00:00
// nullAEAD handles not-yet encrypted packets
type nullAEADFNV128a struct {
perspective protocol.Perspective
}
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
var _ AEAD = &nullAEADFNV128a{}
2017-12-12 02:51:45 +00:00
// Open and verify the ciphertext
2018-01-03 19:19:49 +00:00
func (n *nullAEADFNV128a) Open(dst, src []byte, packetNumber protocol.PacketNumber, associatedData []byte) ([]byte, error) {
2017-12-12 02:51:45 +00:00
if len(src) < 12 {
return nil, errors.New("NullAEAD: ciphertext cannot be less than 12 bytes long")
}
hash := fnv128a.New()
hash.Write(associatedData)
hash.Write(src[12:])
2018-01-03 19:19:49 +00:00
if n.perspective == protocol.PerspectiveServer {
hash.Write([]byte("Client"))
} else {
hash.Write([]byte("Server"))
}
2017-12-12 02:51:45 +00:00
testHigh, testLow := hash.Sum128()
low := binary.LittleEndian.Uint64(src)
high := binary.LittleEndian.Uint32(src[8:])
if uint32(testHigh&0xffffffff) != high || testLow != low {
return nil, errors.New("NullAEAD: failed to authenticate received data")
}
return src[12:], nil
}
// Seal writes hash and ciphertext to the buffer
2018-01-03 19:19:49 +00:00
func (n *nullAEADFNV128a) Seal(dst, src []byte, packetNumber protocol.PacketNumber, associatedData []byte) []byte {
2017-12-12 02:51:45 +00:00
if cap(dst) < 12+len(src) {
dst = make([]byte, 12+len(src))
} else {
dst = dst[:12+len(src)]
}
hash := fnv128a.New()
hash.Write(associatedData)
hash.Write(src)
2018-01-03 19:19:49 +00:00
if n.perspective == protocol.PerspectiveServer {
hash.Write([]byte("Server"))
} else {
hash.Write([]byte("Client"))
}
2017-12-12 02:51:45 +00:00
high, low := hash.Sum128()
copy(dst[12:], src)
binary.LittleEndian.PutUint64(dst, low)
binary.LittleEndian.PutUint32(dst[8:], uint32(high))
return dst
}
2018-01-03 19:19:49 +00:00
func (n *nullAEADFNV128a) Overhead() int {
return 12
}