2015-02-20 04:25:42 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
2015-02-20 21:38:27 +01:00
|
|
|
"bytes"
|
2015-02-20 04:25:42 +01:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
)
|
|
|
|
|
2015-02-21 06:21:14 +01:00
|
|
|
// ConnectionIDGenerator represents the logic to generate 64-bit UDP
|
|
|
|
// connection IDs from peer IP addresses.
|
2015-02-20 21:38:27 +01:00
|
|
|
type ConnectionIDGenerator struct {
|
|
|
|
iv, iv2 []byte
|
|
|
|
block cipher.Block
|
|
|
|
}
|
2015-02-20 04:25:42 +01:00
|
|
|
|
2015-02-22 22:58:43 +01:00
|
|
|
// NewConnectionIDGenerator creates a ConnectionIDGenerator and generates its
|
|
|
|
// AES key and first initialization vector.
|
|
|
|
func NewConnectionIDGenerator() (gen *ConnectionIDGenerator, err error) {
|
|
|
|
gen = &ConnectionIDGenerator{}
|
2015-02-20 21:38:27 +01:00
|
|
|
key := make([]byte, 16)
|
2015-02-22 22:58:43 +01:00
|
|
|
|
|
|
|
_, err = rand.Read(key)
|
2015-02-20 04:25:42 +01:00
|
|
|
if err != nil {
|
2015-02-22 22:58:43 +01:00
|
|
|
return
|
2015-02-20 04:25:42 +01:00
|
|
|
}
|
|
|
|
|
2015-02-22 22:58:43 +01:00
|
|
|
gen.block, err = aes.NewCipher(key)
|
2015-02-20 04:25:42 +01:00
|
|
|
if err != nil {
|
2015-02-22 22:58:43 +01:00
|
|
|
return
|
2015-02-20 04:25:42 +01:00
|
|
|
}
|
|
|
|
|
2015-02-22 22:58:43 +01:00
|
|
|
err = gen.NewIV()
|
|
|
|
return
|
2015-02-20 21:38:27 +01:00
|
|
|
}
|
|
|
|
|
2015-02-21 06:21:14 +01:00
|
|
|
// Generate returns the 64-bit connection ID for an IP
|
2015-02-20 21:38:27 +01:00
|
|
|
func (g *ConnectionIDGenerator) Generate(ip []byte) []byte {
|
|
|
|
return g.generate(ip, g.iv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ConnectionIDGenerator) generate(ip []byte, iv []byte) []byte {
|
2015-02-20 04:25:42 +01:00
|
|
|
if len(ip) > 16 {
|
|
|
|
panic("IP larger than 16 bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(ip) < 8 {
|
|
|
|
ip = append(ip, ip...) // Not enough bits in output.
|
|
|
|
}
|
|
|
|
|
|
|
|
ct := make([]byte, 16)
|
2015-02-20 21:38:27 +01:00
|
|
|
stream := cipher.NewCFBDecrypter(g.block, iv)
|
2015-02-20 04:25:42 +01:00
|
|
|
stream.XORKeyStream(ct, ip)
|
|
|
|
|
|
|
|
for i := len(ip) - 1; i >= 8; i-- {
|
|
|
|
ct[i-8] ^= ct[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ct[:8]
|
|
|
|
}
|
2015-02-20 17:44:05 +01:00
|
|
|
|
2015-02-21 06:21:14 +01:00
|
|
|
// Matches checks if the given connection ID matches an IP with the current or
|
|
|
|
// previous initialization vectors.
|
2015-02-20 21:38:27 +01:00
|
|
|
func (g *ConnectionIDGenerator) Matches(id []byte, ip []byte) bool {
|
|
|
|
if expected := g.generate(ip, g.iv); bytes.Equal(id, expected) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if iv2 := g.iv2; iv2 != nil {
|
|
|
|
if expected := g.generate(ip, iv2); bytes.Equal(id, expected) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-02-21 06:21:14 +01:00
|
|
|
// NewIV generates a new initialization vector and rotates the current one.
|
2015-02-20 21:38:27 +01:00
|
|
|
func (g *ConnectionIDGenerator) NewIV() error {
|
|
|
|
newiv := make([]byte, 16)
|
|
|
|
if _, err := rand.Read(newiv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
g.iv2 = g.iv
|
|
|
|
g.iv = newiv
|
|
|
|
|
|
|
|
return nil
|
2015-02-20 17:44:05 +01:00
|
|
|
}
|