Skip to content

Commit

Permalink
r5
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiSubira committed Jul 19, 2022
1 parent e12cd21 commit 39c6505
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
20 changes: 10 additions & 10 deletions pkg/slayers/extn.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,28 +474,28 @@ const (
)

func (p PacketAuthenticatorSPI) Type() PacketAuthenticatorDRKeyType {
if p&(1<<13) == 0 {
if p&(1<<18) == 0 {
return ASHost
}
return HostHost
}

func (p PacketAuthenticatorSPI) Direction() PacketAuthenticatorDRKeyDirection {
if p&(1<<14) == 0 {
if p&(1<<17) == 0 {
return SenderSide
}
return ReceiverSide
}

func (p PacketAuthenticatorSPI) Epoch() PacketAuthenticatorDRKeyEpochType {
if p&(1<<15) == 0 {
if p&(1<<16) == 0 {
return Later
}
return Earlier
}

func (p PacketAuthenticatorSPI) DRKeyProto() uint16 {
return uint16(p >> 16)
return uint16(p)
}

func (p PacketAuthenticatorSPI) IsDRKey() bool {
Expand All @@ -521,10 +521,10 @@ func MakePacketAuthSPIDrkey(
if epoch > 1 {
panic("Invalid DRKeyEpochType value")
}
spi := uint32((drkeyType & 0x1)) << 13
spi |= uint32((dir & 0x1)) << 14
spi |= uint32((epoch & 0x1)) << 15
spi |= uint32(proto) << 16
spi := uint32((drkeyType & 0x1)) << 18
spi |= uint32((dir & 0x1)) << 17
spi |= uint32((epoch & 0x1)) << 16
spi |= uint32(proto)

return PacketAuthenticatorSPI(spi)
}
Expand Down Expand Up @@ -602,7 +602,7 @@ func (o PacketAuthenticatorOption) Reset(
} else {
o.OptData = make([]byte, n)
}
binary.LittleEndian.PutUint32(o.OptData[:4], uint32(spi))
binary.BigEndian.PutUint32(o.OptData[:4], uint32(spi))
o.OptData[4] = byte(alg)
o.OptData[5] = byte(ts >> 16)
o.OptData[6] = byte(ts >> 8)
Expand All @@ -621,7 +621,7 @@ func (o PacketAuthenticatorOption) Reset(

// SPI returns returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) SPI() PacketAuthenticatorSPI {
return PacketAuthenticatorSPI(binary.LittleEndian.Uint32(o.OptData[:4]))
return PacketAuthenticatorSPI(binary.BigEndian.Uint32(o.OptData[:4]))
}

// Algorithm returns the algorithm type stored in the data buffer.
Expand Down
15 changes: 11 additions & 4 deletions pkg/slayers/extn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,11 @@ func prepRawPacketWithExtn(t *testing.T, extns ...slayers.L4ProtocolType) []byte
return buf.Bytes()
}

var spi = slayers.PacketAuthenticatorSPI(binary.LittleEndian.Uint32([]byte{0, 0x80, 1, 0}))
var spi = slayers.MakePacketAuthSPIDrkey(
1,
slayers.ASHost,
slayers.ReceiverSide,
slayers.Later)
var algo = slayers.PacketAuthSHA1_AES_CBC
var ts = binary.LittleEndian.Uint32([]byte{1, 2, 3, 0})
var sn = binary.LittleEndian.Uint32([]byte{4, 5, 6, 0})
Expand All @@ -570,7 +574,7 @@ var optAuthMAC = []byte("16byte_mac_foooo")
var rawE2EOptAuth = append(
[]byte{
0x11, 0x7, 0x2, 0x1c,
0x0, 0x80, 0x1, 0x0,
0x0, 0x2, 0x0, 0x1,
0x1, 0x3, 0x2, 0x1,
0x0, 0x6, 0x5, 0x4,
},
Expand Down Expand Up @@ -635,7 +639,6 @@ func TestOptAuthenticatorSerialize(t *testing.T) {
b := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{FixLengths: true}
assert.NoError(t, e2e.SerializeTo(b, opts), "SerializeTo")

assert.Equal(t, rawE2EOptAuth, b.Bytes(), "Raw Buffer")
})
}
Expand All @@ -654,6 +657,10 @@ func TestOptAuthenticatorDeserialize(t *testing.T) {
auth, err := slayers.ParsePacketAuthenticatorOption(optAuth)
require.NoError(t, err, "ParsePacketAuthenticatorOption")
assert.Equal(t, spi, auth.SPI(), "SPI")
assert.Equal(t, slayers.ASHost, auth.SPI().Type())
assert.Equal(t, slayers.ReceiverSide, auth.SPI().Direction())
assert.Equal(t, slayers.Later, auth.SPI().Epoch())
assert.Equal(t, true, auth.SPI().IsDRKey())
assert.Equal(t, algo, auth.Algorithm(), "Algorithm Type")
assert.Equal(t, ts, auth.Timestamp(), "Timestamp")
assert.Equal(t, sn, auth.SequenceNumber(), "Sequence Number")
Expand All @@ -662,7 +669,7 @@ func TestOptAuthenticatorDeserialize(t *testing.T) {

func TestMakePacketAuthSPIDrkey(t *testing.T) {
spi := slayers.MakePacketAuthSPIDrkey(1, slayers.ASHost, slayers.SenderSide, slayers.Later)
assert.EqualValues(t, binary.LittleEndian.Uint32([]byte{0, 0, 1, 0}), spi)
assert.EqualValues(t, binary.BigEndian.Uint32([]byte{0, 0, 0, 1}), spi)
}

func TestOptAuthenticatorDeserializeCorrupt(t *testing.T) {
Expand Down

0 comments on commit 39c6505

Please sign in to comment.